blob: 9f4c2349e700962ec90062c75c88f1dcfe531a0b [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010036#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020037#include "thread.h"
38#include "utils/assembler.h"
39#include "utils/mips/assembler_mips.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace mips {
44
45static constexpr int kCurrentMethodStackOffset = 0;
46static constexpr Register kMethodRegisterArgument = A0;
47
Alexey Frunze4147fcc2017-06-17 19:57:27 -070048// Flags controlling the use of thunks for Baker read barriers.
49constexpr bool kBakerReadBarrierThunksEnableForFields = true;
50constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
51constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
52
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020054 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010055 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010057 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010058 case DataType::Type::kInt8:
59 case DataType::Type::kUint16:
60 case DataType::Type::kInt16:
61 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020062 return Location::RegisterLocation(V0);
63
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010064 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020065 return Location::RegisterPairLocation(V0, V1);
66
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kFloat32:
68 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020069 return Location::FpuRegisterLocation(F0);
70
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010071 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020072 return Location();
73 }
74 UNREACHABLE();
75}
76
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010077Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020078 return MipsReturnLocation(type);
79}
80
81Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
82 return Location::RegisterLocation(kMethodRegisterArgument);
83}
84
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010085Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020086 Location next_location;
87
88 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010089 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010090 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010091 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 case DataType::Type::kInt8:
93 case DataType::Type::kUint16:
94 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010095 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020096 uint32_t gp_index = gp_index_++;
97 if (gp_index < calling_convention.GetNumberOfRegisters()) {
98 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
99 } else {
100 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
101 next_location = Location::StackSlot(stack_offset);
102 }
103 break;
104 }
105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100106 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200107 uint32_t gp_index = gp_index_;
108 gp_index_ += 2;
109 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800110 Register reg = calling_convention.GetRegisterAt(gp_index);
111 if (reg == A1 || reg == A3) {
112 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200113 gp_index++;
114 }
115 Register low_even = calling_convention.GetRegisterAt(gp_index);
116 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
117 DCHECK_EQ(low_even + 1, high_odd);
118 next_location = Location::RegisterPairLocation(low_even, high_odd);
119 } else {
120 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
121 next_location = Location::DoubleStackSlot(stack_offset);
122 }
123 break;
124 }
125
126 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
127 // will take up the even/odd pair, while floats are stored in even regs only.
128 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100129 case DataType::Type::kFloat32:
130 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200131 uint32_t float_index = float_index_++;
132 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
133 next_location = Location::FpuRegisterLocation(
134 calling_convention.GetFpuRegisterAt(float_index));
135 } else {
136 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100137 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
138 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200139 }
140 break;
141 }
142
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100143 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200144 LOG(FATAL) << "Unexpected parameter type " << type;
145 break;
146 }
147
148 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100149 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200150
151 return next_location;
152}
153
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155 return MipsReturnLocation(type);
156}
157
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100158// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
159#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700160#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200161
162class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
163 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000164 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200165
166 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
167 LocationSummary* locations = instruction_->GetLocations();
168 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
169 __ Bind(GetEntryLabel());
170 if (instruction_->CanThrowIntoCatchBlock()) {
171 // Live registers will be restored in the catch block if caught.
172 SaveLiveRegisters(codegen, instruction_->GetLocations());
173 }
174 // We're moving two locations to locations that could overlap, so we need a parallel
175 // move resolver.
176 InvokeRuntimeCallingConvention calling_convention;
177 codegen->EmitParallelMoves(locations->InAt(0),
178 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100179 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200180 locations->InAt(1),
181 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100182 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100183 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
184 ? kQuickThrowStringBounds
185 : kQuickThrowArrayBounds;
186 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100187 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200188 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
189 }
190
191 bool IsFatal() const OVERRIDE { return true; }
192
193 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
194
195 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200196 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
197};
198
199class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
200 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000201 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200202
203 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
204 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
205 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100206 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
208 }
209
210 bool IsFatal() const OVERRIDE { return true; }
211
212 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
213
214 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200215 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
216};
217
218class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
219 public:
220 LoadClassSlowPathMIPS(HLoadClass* cls,
221 HInstruction* at,
222 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000223 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700224 : SlowPathCodeMIPS(at),
225 cls_(cls),
226 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000227 do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200228 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
229 }
230
231 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000232 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700233 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200234 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700235 InvokeRuntimeCallingConvention calling_convention;
236 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200237 __ Bind(GetEntryLabel());
238 SaveLiveRegisters(codegen, locations);
239
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000240 dex::TypeIndex type_index = cls_->GetTypeIndex();
241 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100242 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
243 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200245 if (do_clinit_) {
246 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
247 } else {
248 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
249 }
250
251 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200252 if (out.IsValid()) {
253 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100254 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700255 mips_codegen->MoveLocation(out,
256 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
257 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200258 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200259 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700260
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200261 __ B(GetExitLabel());
262 }
263
264 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
265
266 private:
267 // The class this slow path will load.
268 HLoadClass* const cls_;
269
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200270 // The dex PC of `at_`.
271 const uint32_t dex_pc_;
272
273 // Whether to initialize the class.
274 const bool do_clinit_;
275
276 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
277};
278
279class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
280 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000281 explicit LoadStringSlowPathMIPS(HLoadString* instruction)
282 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200283
284 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700285 DCHECK(instruction_->IsLoadString());
286 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287 LocationSummary* locations = instruction_->GetLocations();
288 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000289 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200290 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700291 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200292 __ Bind(GetEntryLabel());
293 SaveLiveRegisters(codegen, locations);
294
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000295 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100296 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200297 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100299 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200300 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700301 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200302 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200303 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000304
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200305 __ B(GetExitLabel());
306 }
307
308 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
309
310 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200311 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
312};
313
314class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
315 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000316 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200317
318 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
319 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
320 __ Bind(GetEntryLabel());
321 if (instruction_->CanThrowIntoCatchBlock()) {
322 // Live registers will be restored in the catch block if caught.
323 SaveLiveRegisters(codegen, instruction_->GetLocations());
324 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100325 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200326 instruction_,
327 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100328 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200329 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
330 }
331
332 bool IsFatal() const OVERRIDE { return true; }
333
334 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
335
336 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200337 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
338};
339
340class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
341 public:
342 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000343 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200344
345 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200346 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200347 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
348 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200349 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100350 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200352 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200353 if (successor_ == nullptr) {
354 __ B(GetReturnLabel());
355 } else {
356 __ B(mips_codegen->GetLabelOf(successor_));
357 }
358 }
359
360 MipsLabel* GetReturnLabel() {
361 DCHECK(successor_ == nullptr);
362 return &return_label_;
363 }
364
365 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
366
Chris Larsena2045912017-11-02 12:39:54 -0700367 HBasicBlock* GetSuccessor() const {
368 return successor_;
369 }
370
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200371 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200372 // If not null, the block to branch to after the suspend check.
373 HBasicBlock* const successor_;
374
375 // If `successor_` is null, the label to branch to after the suspend check.
376 MipsLabel return_label_;
377
378 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
379};
380
381class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
382 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800383 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
384 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200385
386 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
387 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200388 uint32_t dex_pc = instruction_->GetDexPc();
389 DCHECK(instruction_->IsCheckCast()
390 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
391 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
392
393 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800394 if (!is_fatal_) {
395 SaveLiveRegisters(codegen, locations);
396 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200397
398 // We're moving two locations to locations that could overlap, so we need a parallel
399 // move resolver.
400 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800401 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200402 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100403 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800404 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200405 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100406 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100408 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800409 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100410 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200411 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
412 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200413 } else {
414 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800415 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
416 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 }
418
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800419 if (!is_fatal_) {
420 RestoreLiveRegisters(codegen, locations);
421 __ B(GetExitLabel());
422 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 }
424
425 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
426
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800427 bool IsFatal() const OVERRIDE { return is_fatal_; }
428
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200429 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800430 const bool is_fatal_;
431
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200432 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
433};
434
435class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
436 public:
Aart Bik42249c32016-01-07 15:33:50 -0800437 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000438 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200439
440 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800441 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200442 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100443 LocationSummary* locations = instruction_->GetLocations();
444 SaveLiveRegisters(codegen, locations);
445 InvokeRuntimeCallingConvention calling_convention;
446 __ LoadConst32(calling_convention.GetRegisterAt(0),
447 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100448 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100449 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200450 }
451
452 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
453
454 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
456};
457
Alexey Frunze15958152017-02-09 19:08:30 -0800458class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
459 public:
460 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
461
462 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
463 LocationSummary* locations = instruction_->GetLocations();
464 __ Bind(GetEntryLabel());
465 SaveLiveRegisters(codegen, locations);
466
467 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100468 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800469 parallel_move.AddMove(
470 locations->InAt(0),
471 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100472 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800473 nullptr);
474 parallel_move.AddMove(
475 locations->InAt(1),
476 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100477 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800478 nullptr);
479 parallel_move.AddMove(
480 locations->InAt(2),
481 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100482 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800483 nullptr);
484 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
485
486 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
487 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
488 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
489 RestoreLiveRegisters(codegen, locations);
490 __ B(GetExitLabel());
491 }
492
493 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
494
495 private:
496 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
497};
498
499// Slow path marking an object reference `ref` during a read
500// barrier. The field `obj.field` in the object `obj` holding this
501// reference does not get updated by this slow path after marking (see
502// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
503//
504// This means that after the execution of this slow path, `ref` will
505// always be up-to-date, but `obj.field` may not; i.e., after the
506// flip, `ref` will be a to-space reference, but `obj.field` will
507// probably still be a from-space reference (unless it gets updated by
508// another thread, or if another thread installed another object
509// reference (different from `ref`) in `obj.field`).
510//
511// If `entrypoint` is a valid location it is assumed to already be
512// holding the entrypoint. The case where the entrypoint is passed in
513// is for the GcRoot read barrier.
514class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
515 public:
516 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
517 Location ref,
518 Location entrypoint = Location::NoLocation())
519 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
520 DCHECK(kEmitCompilerReadBarrier);
521 }
522
523 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
524
525 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
526 LocationSummary* locations = instruction_->GetLocations();
527 Register ref_reg = ref_.AsRegister<Register>();
528 DCHECK(locations->CanCall());
529 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
530 DCHECK(instruction_->IsInstanceFieldGet() ||
531 instruction_->IsStaticFieldGet() ||
532 instruction_->IsArrayGet() ||
533 instruction_->IsArraySet() ||
534 instruction_->IsLoadClass() ||
535 instruction_->IsLoadString() ||
536 instruction_->IsInstanceOf() ||
537 instruction_->IsCheckCast() ||
538 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
539 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
540 << "Unexpected instruction in read barrier marking slow path: "
541 << instruction_->DebugName();
542
543 __ Bind(GetEntryLabel());
544 // No need to save live registers; it's taken care of by the
545 // entrypoint. Also, there is no need to update the stack mask,
546 // as this runtime call will not trigger a garbage collection.
547 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
548 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
549 (S2 <= ref_reg && ref_reg <= S7) ||
550 (ref_reg == FP)) << ref_reg;
551 // "Compact" slow path, saving two moves.
552 //
553 // Instead of using the standard runtime calling convention (input
554 // and output in A0 and V0 respectively):
555 //
556 // A0 <- ref
557 // V0 <- ReadBarrierMark(A0)
558 // ref <- V0
559 //
560 // we just use rX (the register containing `ref`) as input and output
561 // of a dedicated entrypoint:
562 //
563 // rX <- ReadBarrierMarkRegX(rX)
564 //
565 if (entrypoint_.IsValid()) {
566 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
567 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
568 __ Jalr(entrypoint_.AsRegister<Register>());
569 __ NopIfNoReordering();
570 } else {
571 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100572 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800573 // This runtime call does not require a stack map.
574 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
575 instruction_,
576 this,
577 /* direct */ false);
578 }
579 __ B(GetExitLabel());
580 }
581
582 private:
583 // The location (register) of the marked object reference.
584 const Location ref_;
585
586 // The location of the entrypoint if already loaded.
587 const Location entrypoint_;
588
589 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
590};
591
592// Slow path marking an object reference `ref` during a read barrier,
593// and if needed, atomically updating the field `obj.field` in the
594// object `obj` holding this reference after marking (contrary to
595// ReadBarrierMarkSlowPathMIPS above, which never tries to update
596// `obj.field`).
597//
598// This means that after the execution of this slow path, both `ref`
599// and `obj.field` will be up-to-date; i.e., after the flip, both will
600// hold the same to-space reference (unless another thread installed
601// another object reference (different from `ref`) in `obj.field`).
602class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
603 public:
604 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
605 Location ref,
606 Register obj,
607 Location field_offset,
608 Register temp1)
609 : SlowPathCodeMIPS(instruction),
610 ref_(ref),
611 obj_(obj),
612 field_offset_(field_offset),
613 temp1_(temp1) {
614 DCHECK(kEmitCompilerReadBarrier);
615 }
616
617 const char* GetDescription() const OVERRIDE {
618 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
619 }
620
621 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
622 LocationSummary* locations = instruction_->GetLocations();
623 Register ref_reg = ref_.AsRegister<Register>();
624 DCHECK(locations->CanCall());
625 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
626 // This slow path is only used by the UnsafeCASObject intrinsic.
627 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
628 << "Unexpected instruction in read barrier marking and field updating slow path: "
629 << instruction_->DebugName();
630 DCHECK(instruction_->GetLocations()->Intrinsified());
631 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
632 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
633
634 __ Bind(GetEntryLabel());
635
636 // Save the old reference.
637 // Note that we cannot use AT or TMP to save the old reference, as those
638 // are used by the code that follows, but we need the old reference after
639 // the call to the ReadBarrierMarkRegX entry point.
640 DCHECK_NE(temp1_, AT);
641 DCHECK_NE(temp1_, TMP);
642 __ Move(temp1_, ref_reg);
643
644 // No need to save live registers; it's taken care of by the
645 // entrypoint. Also, there is no need to update the stack mask,
646 // as this runtime call will not trigger a garbage collection.
647 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
648 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
649 (S2 <= ref_reg && ref_reg <= S7) ||
650 (ref_reg == FP)) << ref_reg;
651 // "Compact" slow path, saving two moves.
652 //
653 // Instead of using the standard runtime calling convention (input
654 // and output in A0 and V0 respectively):
655 //
656 // A0 <- ref
657 // V0 <- ReadBarrierMark(A0)
658 // ref <- V0
659 //
660 // we just use rX (the register containing `ref`) as input and output
661 // of a dedicated entrypoint:
662 //
663 // rX <- ReadBarrierMarkRegX(rX)
664 //
665 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100666 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800667 // This runtime call does not require a stack map.
668 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
669 instruction_,
670 this,
671 /* direct */ false);
672
673 // If the new reference is different from the old reference,
674 // update the field in the holder (`*(obj_ + field_offset_)`).
675 //
676 // Note that this field could also hold a different object, if
677 // another thread had concurrently changed it. In that case, the
678 // the compare-and-set (CAS) loop below would abort, leaving the
679 // field as-is.
680 MipsLabel done;
681 __ Beq(temp1_, ref_reg, &done);
682
683 // Update the the holder's field atomically. This may fail if
684 // mutator updates before us, but it's OK. This is achieved
685 // using a strong compare-and-set (CAS) operation with relaxed
686 // memory synchronization ordering, where the expected value is
687 // the old reference and the desired value is the new reference.
688
689 // Convenience aliases.
690 Register base = obj_;
691 // The UnsafeCASObject intrinsic uses a register pair as field
692 // offset ("long offset"), of which only the low part contains
693 // data.
694 Register offset = field_offset_.AsRegisterPairLow<Register>();
695 Register expected = temp1_;
696 Register value = ref_reg;
697 Register tmp_ptr = TMP; // Pointer to actual memory.
698 Register tmp = AT; // Value in memory.
699
700 __ Addu(tmp_ptr, base, offset);
701
702 if (kPoisonHeapReferences) {
703 __ PoisonHeapReference(expected);
704 // Do not poison `value` if it is the same register as
705 // `expected`, which has just been poisoned.
706 if (value != expected) {
707 __ PoisonHeapReference(value);
708 }
709 }
710
711 // do {
712 // tmp = [r_ptr] - expected;
713 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
714
715 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
716 MipsLabel loop_head, exit_loop;
717 __ Bind(&loop_head);
718 if (is_r6) {
719 __ LlR6(tmp, tmp_ptr);
720 } else {
721 __ LlR2(tmp, tmp_ptr);
722 }
723 __ Bne(tmp, expected, &exit_loop);
724 __ Move(tmp, value);
725 if (is_r6) {
726 __ ScR6(tmp, tmp_ptr);
727 } else {
728 __ ScR2(tmp, tmp_ptr);
729 }
730 __ Beqz(tmp, &loop_head);
731 __ Bind(&exit_loop);
732
733 if (kPoisonHeapReferences) {
734 __ UnpoisonHeapReference(expected);
735 // Do not unpoison `value` if it is the same register as
736 // `expected`, which has just been unpoisoned.
737 if (value != expected) {
738 __ UnpoisonHeapReference(value);
739 }
740 }
741
742 __ Bind(&done);
743 __ B(GetExitLabel());
744 }
745
746 private:
747 // The location (register) of the marked object reference.
748 const Location ref_;
749 // The register containing the object holding the marked object reference field.
750 const Register obj_;
751 // The location of the offset of the marked reference field within `obj_`.
752 Location field_offset_;
753
754 const Register temp1_;
755
756 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
757};
758
759// Slow path generating a read barrier for a heap reference.
760class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
761 public:
762 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
763 Location out,
764 Location ref,
765 Location obj,
766 uint32_t offset,
767 Location index)
768 : SlowPathCodeMIPS(instruction),
769 out_(out),
770 ref_(ref),
771 obj_(obj),
772 offset_(offset),
773 index_(index) {
774 DCHECK(kEmitCompilerReadBarrier);
775 // If `obj` is equal to `out` or `ref`, it means the initial object
776 // has been overwritten by (or after) the heap object reference load
777 // to be instrumented, e.g.:
778 //
779 // __ LoadFromOffset(kLoadWord, out, out, offset);
780 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
781 //
782 // In that case, we have lost the information about the original
783 // object, and the emitted read barrier cannot work properly.
784 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
785 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
786 }
787
788 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
789 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
790 LocationSummary* locations = instruction_->GetLocations();
791 Register reg_out = out_.AsRegister<Register>();
792 DCHECK(locations->CanCall());
793 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
794 DCHECK(instruction_->IsInstanceFieldGet() ||
795 instruction_->IsStaticFieldGet() ||
796 instruction_->IsArrayGet() ||
797 instruction_->IsInstanceOf() ||
798 instruction_->IsCheckCast() ||
799 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
800 << "Unexpected instruction in read barrier for heap reference slow path: "
801 << instruction_->DebugName();
802
803 __ Bind(GetEntryLabel());
804 SaveLiveRegisters(codegen, locations);
805
806 // We may have to change the index's value, but as `index_` is a
807 // constant member (like other "inputs" of this slow path),
808 // introduce a copy of it, `index`.
809 Location index = index_;
810 if (index_.IsValid()) {
811 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
812 if (instruction_->IsArrayGet()) {
813 // Compute the actual memory offset and store it in `index`.
814 Register index_reg = index_.AsRegister<Register>();
815 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
816 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
817 // We are about to change the value of `index_reg` (see the
818 // calls to art::mips::MipsAssembler::Sll and
819 // art::mips::MipsAssembler::Addiu32 below), but it has
820 // not been saved by the previous call to
821 // art::SlowPathCode::SaveLiveRegisters, as it is a
822 // callee-save register --
823 // art::SlowPathCode::SaveLiveRegisters does not consider
824 // callee-save registers, as it has been designed with the
825 // assumption that callee-save registers are supposed to be
826 // handled by the called function. So, as a callee-save
827 // register, `index_reg` _would_ eventually be saved onto
828 // the stack, but it would be too late: we would have
829 // changed its value earlier. Therefore, we manually save
830 // it here into another freely available register,
831 // `free_reg`, chosen of course among the caller-save
832 // registers (as a callee-save `free_reg` register would
833 // exhibit the same problem).
834 //
835 // Note we could have requested a temporary register from
836 // the register allocator instead; but we prefer not to, as
837 // this is a slow path, and we know we can find a
838 // caller-save register that is available.
839 Register free_reg = FindAvailableCallerSaveRegister(codegen);
840 __ Move(free_reg, index_reg);
841 index_reg = free_reg;
842 index = Location::RegisterLocation(index_reg);
843 } else {
844 // The initial register stored in `index_` has already been
845 // saved in the call to art::SlowPathCode::SaveLiveRegisters
846 // (as it is not a callee-save register), so we can freely
847 // use it.
848 }
849 // Shifting the index value contained in `index_reg` by the scale
850 // factor (2) cannot overflow in practice, as the runtime is
851 // unable to allocate object arrays with a size larger than
852 // 2^26 - 1 (that is, 2^28 - 4 bytes).
853 __ Sll(index_reg, index_reg, TIMES_4);
854 static_assert(
855 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
856 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
857 __ Addiu32(index_reg, index_reg, offset_);
858 } else {
859 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
860 // intrinsics, `index_` is not shifted by a scale factor of 2
861 // (as in the case of ArrayGet), as it is actually an offset
862 // to an object field within an object.
863 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
864 DCHECK(instruction_->GetLocations()->Intrinsified());
865 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
866 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
867 << instruction_->AsInvoke()->GetIntrinsic();
868 DCHECK_EQ(offset_, 0U);
869 DCHECK(index_.IsRegisterPair());
870 // UnsafeGet's offset location is a register pair, the low
871 // part contains the correct offset.
872 index = index_.ToLow();
873 }
874 }
875
876 // We're moving two or three locations to locations that could
877 // overlap, so we need a parallel move resolver.
878 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100879 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800880 parallel_move.AddMove(ref_,
881 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100882 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800883 nullptr);
884 parallel_move.AddMove(obj_,
885 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100886 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800887 nullptr);
888 if (index.IsValid()) {
889 parallel_move.AddMove(index,
890 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100891 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800892 nullptr);
893 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
894 } else {
895 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
896 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
897 }
898 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
899 instruction_,
900 instruction_->GetDexPc(),
901 this);
902 CheckEntrypointTypes<
903 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200904 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100905 calling_convention.GetReturnLocation(DataType::Type::kReference),
906 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800907
908 RestoreLiveRegisters(codegen, locations);
909 __ B(GetExitLabel());
910 }
911
912 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
913
914 private:
915 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
916 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
917 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
918 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
919 if (i != ref &&
920 i != obj &&
921 !codegen->IsCoreCalleeSaveRegister(i) &&
922 !codegen->IsBlockedCoreRegister(i)) {
923 return static_cast<Register>(i);
924 }
925 }
926 // We shall never fail to find a free caller-save register, as
927 // there are more than two core caller-save registers on MIPS
928 // (meaning it is possible to find one which is different from
929 // `ref` and `obj`).
930 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
931 LOG(FATAL) << "Could not find a free caller-save register";
932 UNREACHABLE();
933 }
934
935 const Location out_;
936 const Location ref_;
937 const Location obj_;
938 const uint32_t offset_;
939 // An additional location containing an index to an array.
940 // Only used for HArrayGet and the UnsafeGetObject &
941 // UnsafeGetObjectVolatile intrinsics.
942 const Location index_;
943
944 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
945};
946
947// Slow path generating a read barrier for a GC root.
948class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
949 public:
950 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
951 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
952 DCHECK(kEmitCompilerReadBarrier);
953 }
954
955 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
956 LocationSummary* locations = instruction_->GetLocations();
957 Register reg_out = out_.AsRegister<Register>();
958 DCHECK(locations->CanCall());
959 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
960 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
961 << "Unexpected instruction in read barrier for GC root slow path: "
962 << instruction_->DebugName();
963
964 __ Bind(GetEntryLabel());
965 SaveLiveRegisters(codegen, locations);
966
967 InvokeRuntimeCallingConvention calling_convention;
968 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +0200969 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
970 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100971 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800972 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
973 instruction_,
974 instruction_->GetDexPc(),
975 this);
976 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +0200977 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100978 calling_convention.GetReturnLocation(DataType::Type::kReference),
979 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800980
981 RestoreLiveRegisters(codegen, locations);
982 __ B(GetExitLabel());
983 }
984
985 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
986
987 private:
988 const Location out_;
989 const Location root_;
990
991 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
992};
993
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200994CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
995 const MipsInstructionSetFeatures& isa_features,
996 const CompilerOptions& compiler_options,
997 OptimizingCompilerStats* stats)
998 : CodeGenerator(graph,
999 kNumberOfCoreRegisters,
1000 kNumberOfFRegisters,
1001 kNumberOfRegisterPairs,
1002 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1003 arraysize(kCoreCalleeSaves)),
1004 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1005 arraysize(kFpuCalleeSaves)),
1006 compiler_options,
1007 stats),
1008 block_labels_(nullptr),
1009 location_builder_(graph, this),
1010 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001011 move_resolver_(graph->GetAllocator(), this),
1012 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001013 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001014 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001015 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1016 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1017 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1018 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1019 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1020 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1021 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1022 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1023 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001024 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001025 // Save RA (containing the return address) to mimic Quick.
1026 AddAllocatedRegister(Location::RegisterLocation(RA));
1027}
1028
1029#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001030// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1031#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001032#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001033
1034void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1035 // Ensure that we fix up branches.
1036 __ FinalizeCode();
1037
1038 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001039 StackMapStream* stack_map_stream = GetStackMapStream();
1040 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001041 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +00001042 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001043 uint32_t new_position = __ GetAdjustedPosition(old_position);
1044 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001045 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001046 }
1047
1048 // Adjust pc offsets for the disassembly information.
1049 if (disasm_info_ != nullptr) {
1050 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1051 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1052 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1053 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1054 it.second.start = __ GetAdjustedPosition(it.second.start);
1055 it.second.end = __ GetAdjustedPosition(it.second.end);
1056 }
1057 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1058 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1059 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1060 }
1061 }
1062
1063 CodeGenerator::Finalize(allocator);
1064}
1065
1066MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1067 return codegen_->GetAssembler();
1068}
1069
1070void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1071 DCHECK_LT(index, moves_.size());
1072 MoveOperands* move = moves_[index];
1073 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1074}
1075
1076void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1077 DCHECK_LT(index, moves_.size());
1078 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001079 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001080 Location loc1 = move->GetDestination();
1081 Location loc2 = move->GetSource();
1082
1083 DCHECK(!loc1.IsConstant());
1084 DCHECK(!loc2.IsConstant());
1085
1086 if (loc1.Equals(loc2)) {
1087 return;
1088 }
1089
1090 if (loc1.IsRegister() && loc2.IsRegister()) {
1091 // Swap 2 GPRs.
1092 Register r1 = loc1.AsRegister<Register>();
1093 Register r2 = loc2.AsRegister<Register>();
1094 __ Move(TMP, r2);
1095 __ Move(r2, r1);
1096 __ Move(r1, TMP);
1097 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001098 if (codegen_->GetGraph()->HasSIMD()) {
1099 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1100 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1101 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001102 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001103 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1104 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1105 if (type == DataType::Type::kFloat32) {
1106 __ MovS(FTMP, f2);
1107 __ MovS(f2, f1);
1108 __ MovS(f1, FTMP);
1109 } else {
1110 DCHECK_EQ(type, DataType::Type::kFloat64);
1111 __ MovD(FTMP, f2);
1112 __ MovD(f2, f1);
1113 __ MovD(f1, FTMP);
1114 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001115 }
1116 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1117 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1118 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001119 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001120 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1121 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001122 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001123 __ Move(TMP, r2);
1124 __ Mfc1(r2, f1);
1125 __ Mtc1(TMP, f1);
1126 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1127 // Swap 2 GPR register pairs.
1128 Register r1 = loc1.AsRegisterPairLow<Register>();
1129 Register r2 = loc2.AsRegisterPairLow<Register>();
1130 __ Move(TMP, r2);
1131 __ Move(r2, r1);
1132 __ Move(r1, TMP);
1133 r1 = loc1.AsRegisterPairHigh<Register>();
1134 r2 = loc2.AsRegisterPairHigh<Register>();
1135 __ Move(TMP, r2);
1136 __ Move(r2, r1);
1137 __ Move(r1, TMP);
1138 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1139 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1140 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001141 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001142 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1143 : loc2.AsFpuRegister<FRegister>();
1144 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1145 : loc2.AsRegisterPairLow<Register>();
1146 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1147 : loc2.AsRegisterPairHigh<Register>();
1148 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1149 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1150 // unpredictable and the following mfch1 will fail.
1151 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001152 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001153 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001154 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001155 __ Move(r2_l, TMP);
1156 __ Move(r2_h, AT);
1157 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1158 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1159 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1160 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001161 } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) {
1162 ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
David Brazdilcc0f3112016-01-28 17:14:52 +00001163 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1164 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001165 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1166 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001167 __ Move(TMP, reg);
1168 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1169 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1170 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1171 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1172 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1173 : loc2.AsRegisterPairLow<Register>();
1174 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1175 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001176 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001177 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1178 : loc2.GetHighStackIndex(kMipsWordSize);
1179 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001180 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001181 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001182 __ Move(TMP, reg_h);
1183 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1184 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001185 } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) ||
1186 (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) {
1187 Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2;
1188 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1189 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(fp_loc));
1190 __ LoadQFromOffset(fp_loc.AsFpuRegister<FRegister>(), SP, offset);
1191 __ StoreQToOffset(FTMP, SP, offset);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001192 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1193 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1194 : loc2.AsFpuRegister<FRegister>();
1195 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001196 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001197 __ MovS(FTMP, reg);
1198 __ LoadSFromOffset(reg, SP, offset);
1199 __ StoreSToOffset(FTMP, SP, offset);
1200 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001201 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001202 __ MovD(FTMP, reg);
1203 __ LoadDFromOffset(reg, SP, offset);
1204 __ StoreDToOffset(FTMP, SP, offset);
1205 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001206 } else {
1207 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1208 }
1209}
1210
1211void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1212 __ Pop(static_cast<Register>(reg));
1213}
1214
1215void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1216 __ Push(static_cast<Register>(reg));
1217}
1218
1219void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1220 // Allocate a scratch register other than TMP, if available.
1221 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1222 // automatically unspilled when the scratch scope object is destroyed).
1223 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1224 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001225 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001226 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1227 __ LoadFromOffset(kLoadWord,
1228 Register(ensure_scratch.GetRegister()),
1229 SP,
1230 index1 + stack_offset);
1231 __ LoadFromOffset(kLoadWord,
1232 TMP,
1233 SP,
1234 index2 + stack_offset);
1235 __ StoreToOffset(kStoreWord,
1236 Register(ensure_scratch.GetRegister()),
1237 SP,
1238 index2 + stack_offset);
1239 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1240 }
1241}
1242
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001243void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) {
1244 __ LoadQFromOffset(FTMP, SP, index1);
1245 __ LoadQFromOffset(FTMP2, SP, index2);
1246 __ StoreQToOffset(FTMP, SP, index2);
1247 __ StoreQToOffset(FTMP2, SP, index1);
1248}
1249
Alexey Frunze73296a72016-06-03 22:51:46 -07001250void CodeGeneratorMIPS::ComputeSpillMask() {
1251 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1252 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1253 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1254 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1255 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1256 // within the stack frame.
1257 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1258 core_spill_mask_ |= (1 << ZERO);
1259 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001260}
1261
1262bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001263 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001264 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1265 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1266 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001267 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001268}
1269
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001270static dwarf::Reg DWARFReg(Register reg) {
1271 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1272}
1273
1274// TODO: mapping of floating-point registers to DWARF.
1275
1276void CodeGeneratorMIPS::GenerateFrameEntry() {
1277 __ Bind(&frame_entry_label_);
1278
Vladimir Marko33bff252017-11-01 14:35:42 +00001279 bool do_overflow_check =
1280 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001281
1282 if (do_overflow_check) {
1283 __ LoadFromOffset(kLoadWord,
1284 ZERO,
1285 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001286 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001287 RecordPcInfo(nullptr, 0);
1288 }
1289
1290 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001291 CHECK_EQ(fpu_spill_mask_, 0u);
1292 CHECK_EQ(core_spill_mask_, 1u << RA);
1293 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001294 return;
1295 }
1296
1297 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001298 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1299 LOG(FATAL) << "Stack frame larger than "
1300 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001301 }
1302
1303 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304
Alexey Frunze73296a72016-06-03 22:51:46 -07001305 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001306 __ IncreaseFrameSize(ofs);
1307
Alexey Frunze73296a72016-06-03 22:51:46 -07001308 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1309 Register reg = static_cast<Register>(MostSignificantBit(mask));
1310 mask ^= 1u << reg;
1311 ofs -= kMipsWordSize;
1312 // The ZERO register is only included for alignment.
1313 if (reg != ZERO) {
1314 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001315 __ cfi().RelOffset(DWARFReg(reg), ofs);
1316 }
1317 }
1318
Alexey Frunze73296a72016-06-03 22:51:46 -07001319 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1320 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1321 mask ^= 1u << reg;
1322 ofs -= kMipsDoublewordSize;
1323 __ StoreDToOffset(reg, SP, ofs);
1324 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001325 }
1326
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001327 // Save the current method if we need it. Note that we do not
1328 // do this in HCurrentMethod, as the instruction might have been removed
1329 // in the SSA graph.
1330 if (RequiresCurrentMethod()) {
1331 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1332 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001333
1334 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1335 // Initialize should deoptimize flag to 0.
1336 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1337 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001338}
1339
1340void CodeGeneratorMIPS::GenerateFrameExit() {
1341 __ cfi().RememberState();
1342
1343 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001344 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001345
Alexey Frunze73296a72016-06-03 22:51:46 -07001346 // For better instruction scheduling restore RA before other registers.
1347 uint32_t ofs = GetFrameSize();
1348 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1349 Register reg = static_cast<Register>(MostSignificantBit(mask));
1350 mask ^= 1u << reg;
1351 ofs -= kMipsWordSize;
1352 // The ZERO register is only included for alignment.
1353 if (reg != ZERO) {
1354 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355 __ cfi().Restore(DWARFReg(reg));
1356 }
1357 }
1358
Alexey Frunze73296a72016-06-03 22:51:46 -07001359 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1360 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1361 mask ^= 1u << reg;
1362 ofs -= kMipsDoublewordSize;
1363 __ LoadDFromOffset(reg, SP, ofs);
1364 // TODO: __ cfi().Restore(DWARFReg(reg));
1365 }
1366
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001367 size_t frame_size = GetFrameSize();
1368 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1369 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1370 bool reordering = __ SetReorder(false);
1371 if (exchange) {
1372 __ Jr(RA);
1373 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1374 } else {
1375 __ DecreaseFrameSize(frame_size);
1376 __ Jr(RA);
1377 __ Nop(); // In delay slot.
1378 }
1379 __ SetReorder(reordering);
1380 } else {
1381 __ Jr(RA);
1382 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001383 }
1384
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001385 __ cfi().RestoreState();
1386 __ cfi().DefCFAOffset(GetFrameSize());
1387}
1388
1389void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1390 __ Bind(GetLabelOf(block));
1391}
1392
Lena Djokicca8c2952017-05-29 11:31:46 +02001393VectorRegister VectorRegisterFrom(Location location) {
1394 DCHECK(location.IsFpuRegister());
1395 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1396}
1397
Lena Djokic8098da92017-06-28 12:07:50 +02001398void CodeGeneratorMIPS::MoveLocation(Location destination,
1399 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001400 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001401 if (source.Equals(destination)) {
1402 return;
1403 }
1404
Lena Djokic8098da92017-06-28 12:07:50 +02001405 if (source.IsConstant()) {
1406 MoveConstant(destination, source.GetConstant());
1407 } else {
1408 if (destination.IsRegister()) {
1409 if (source.IsRegister()) {
1410 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1411 } else if (source.IsFpuRegister()) {
1412 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1413 } else {
1414 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001415 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001416 }
1417 } else if (destination.IsRegisterPair()) {
1418 if (source.IsRegisterPair()) {
1419 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1420 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1421 } else if (source.IsFpuRegister()) {
1422 Register dst_high = destination.AsRegisterPairHigh<Register>();
1423 Register dst_low = destination.AsRegisterPairLow<Register>();
1424 FRegister src = source.AsFpuRegister<FRegister>();
1425 __ Mfc1(dst_low, src);
1426 __ MoveFromFpuHigh(dst_high, src);
1427 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001428 DCHECK(source.IsDoubleStackSlot())
1429 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001430 int32_t off = source.GetStackIndex();
1431 Register r = destination.AsRegisterPairLow<Register>();
1432 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1433 }
1434 } else if (destination.IsFpuRegister()) {
1435 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001436 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001437 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1438 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001439 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001440 FRegister dst = destination.AsFpuRegister<FRegister>();
1441 Register src_high = source.AsRegisterPairHigh<Register>();
1442 Register src_low = source.AsRegisterPairLow<Register>();
1443 __ Mtc1(src_low, dst);
1444 __ MoveToFpuHigh(src_high, dst);
1445 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001446 if (GetGraph()->HasSIMD()) {
1447 __ MoveV(VectorRegisterFrom(destination),
1448 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001449 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001450 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001451 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1452 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001453 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001454 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1455 }
Lena Djokic8098da92017-06-28 12:07:50 +02001456 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001457 } else if (source.IsSIMDStackSlot()) {
1458 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001459 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001461 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1462 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001464 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1465 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1466 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001467 } else if (destination.IsSIMDStackSlot()) {
1468 if (source.IsFpuRegister()) {
1469 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1470 } else {
1471 DCHECK(source.IsSIMDStackSlot());
1472 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1473 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1474 }
Lena Djokic8098da92017-06-28 12:07:50 +02001475 } else if (destination.IsDoubleStackSlot()) {
1476 int32_t dst_offset = destination.GetStackIndex();
1477 if (source.IsRegisterPair()) {
1478 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1479 } else if (source.IsFpuRegister()) {
1480 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1481 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001482 DCHECK(source.IsDoubleStackSlot())
1483 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001484 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1485 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1486 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1487 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001489 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001490 DCHECK(destination.IsStackSlot()) << destination;
1491 int32_t dst_offset = destination.GetStackIndex();
1492 if (source.IsRegister()) {
1493 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1494 } else if (source.IsFpuRegister()) {
1495 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1496 } else {
1497 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1498 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1499 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1500 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001501 }
1502 }
1503}
1504
1505void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1506 if (c->IsIntConstant() || c->IsNullConstant()) {
1507 // Move 32 bit constant.
1508 int32_t value = GetInt32ValueOf(c);
1509 if (destination.IsRegister()) {
1510 Register dst = destination.AsRegister<Register>();
1511 __ LoadConst32(dst, value);
1512 } else {
1513 DCHECK(destination.IsStackSlot())
1514 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001515 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001516 }
1517 } else if (c->IsLongConstant()) {
1518 // Move 64 bit constant.
1519 int64_t value = GetInt64ValueOf(c);
1520 if (destination.IsRegisterPair()) {
1521 Register r_h = destination.AsRegisterPairHigh<Register>();
1522 Register r_l = destination.AsRegisterPairLow<Register>();
1523 __ LoadConst64(r_h, r_l, value);
1524 } else {
1525 DCHECK(destination.IsDoubleStackSlot())
1526 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001527 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001528 }
1529 } else if (c->IsFloatConstant()) {
1530 // Move 32 bit float constant.
1531 int32_t value = GetInt32ValueOf(c);
1532 if (destination.IsFpuRegister()) {
1533 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1534 } else {
1535 DCHECK(destination.IsStackSlot())
1536 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001537 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001538 }
1539 } else {
1540 // Move 64 bit double constant.
1541 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1542 int64_t value = GetInt64ValueOf(c);
1543 if (destination.IsFpuRegister()) {
1544 FRegister fd = destination.AsFpuRegister<FRegister>();
1545 __ LoadDConst64(fd, value, TMP);
1546 } else {
1547 DCHECK(destination.IsDoubleStackSlot())
1548 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001549 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001550 }
1551 }
1552}
1553
1554void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1555 DCHECK(destination.IsRegister());
1556 Register dst = destination.AsRegister<Register>();
1557 __ LoadConst32(dst, value);
1558}
1559
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001560void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1561 if (location.IsRegister()) {
1562 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001563 } else if (location.IsRegisterPair()) {
1564 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1565 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001566 } else {
1567 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1568 }
1569}
1570
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001571template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001572inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1573 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001574 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001575 for (const PcRelativePatchInfo& info : infos) {
1576 const DexFile& dex_file = info.target_dex_file;
1577 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001578 DCHECK(info.label.IsBound());
1579 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001580 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1581 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001582 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1583 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1584 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001585 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001586 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001587 }
1588}
1589
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001590void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001591 DCHECK(linker_patches->empty());
1592 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001593 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001594 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001595 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001596 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001597 pc_relative_string_patches_.size() +
1598 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001599 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001600 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001601 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1602 pc_relative_method_patches_, linker_patches);
1603 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1604 pc_relative_type_patches_, linker_patches);
1605 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1606 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001607 } else {
1608 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001609 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1610 pc_relative_type_patches_, linker_patches);
1611 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1612 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001613 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001614 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1615 method_bss_entry_patches_, linker_patches);
1616 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1617 type_bss_entry_patches_, linker_patches);
1618 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1619 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001620 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001621}
1622
Vladimir Marko65979462017-05-19 17:25:12 +01001623CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001624 MethodReference target_method,
1625 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001626 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001627 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001628 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001629 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001630}
1631
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001632CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001633 MethodReference target_method,
1634 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001635 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001636 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001637 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001638 &method_bss_entry_patches_);
1639}
1640
Alexey Frunze06a46c42016-07-19 15:00:40 -07001641CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001642 const DexFile& dex_file,
1643 dex::TypeIndex type_index,
1644 const PcRelativePatchInfo* info_high) {
1645 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001646}
1647
Vladimir Marko1998cd02017-01-13 13:02:58 +00001648CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001649 const DexFile& dex_file,
1650 dex::TypeIndex type_index,
1651 const PcRelativePatchInfo* info_high) {
1652 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001653}
1654
Vladimir Marko65979462017-05-19 17:25:12 +01001655CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001656 const DexFile& dex_file,
1657 dex::StringIndex string_index,
1658 const PcRelativePatchInfo* info_high) {
1659 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001660}
1661
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001662CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1663 const DexFile& dex_file,
1664 dex::StringIndex string_index,
1665 const PcRelativePatchInfo* info_high) {
1666 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1667}
1668
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001669CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001670 const DexFile& dex_file,
1671 uint32_t offset_or_index,
1672 const PcRelativePatchInfo* info_high,
1673 ArenaDeque<PcRelativePatchInfo>* patches) {
1674 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001675 return &patches->back();
1676}
1677
Alexey Frunze06a46c42016-07-19 15:00:40 -07001678Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1679 return map->GetOrCreate(
1680 value,
1681 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1682}
1683
Alexey Frunze06a46c42016-07-19 15:00:40 -07001684Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001685 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001686}
1687
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001688void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001689 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001690 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001691 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001692 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001693 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001694 if (GetInstructionSetFeatures().IsR6()) {
1695 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001696 __ Bind(&info_high->label);
1697 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001698 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001699 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001700 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001701 } else {
1702 // If base is ZERO, emit NAL to obtain the actual base.
1703 if (base == ZERO) {
1704 // Generate a dummy PC-relative call to obtain PC.
1705 __ Nal();
1706 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001707 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001708 __ Lui(out, /* placeholder */ 0x1234);
1709 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1710 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1711 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001712 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001713 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001714 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001715 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001716 __ Addu(out, out, (base == ZERO) ? RA : base);
1717 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001718 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001719 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001720}
1721
Alexey Frunze627c1a02017-01-30 19:28:14 -08001722CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1723 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001724 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001725 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001726 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1727 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001728 return &jit_string_patches_.back();
1729}
1730
1731CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1732 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001733 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001734 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001735 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1736 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001737 return &jit_class_patches_.back();
1738}
1739
1740void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1741 const uint8_t* roots_data,
1742 const CodeGeneratorMIPS::JitPatchInfo& info,
1743 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001744 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1745 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001746 uintptr_t address =
1747 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1748 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1749 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001750 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1751 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1752 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1753 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001754 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001755 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1756 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001757 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001758 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001759 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1760 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001761 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001762 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1763 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001764}
1765
1766void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1767 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001768 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1769 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001770 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001771 }
1772 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001773 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1774 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001775 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001776 }
1777}
1778
Goran Jakovljevice114da22016-12-26 14:21:43 +01001779void CodeGeneratorMIPS::MarkGCCard(Register object,
1780 Register value,
1781 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001782 MipsLabel done;
1783 Register card = AT;
1784 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001785 if (value_can_be_null) {
1786 __ Beqz(value, &done);
1787 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001788 __ LoadFromOffset(kLoadWord,
1789 card,
1790 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001791 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001792 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1793 __ Addu(temp, card, temp);
1794 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001795 if (value_can_be_null) {
1796 __ Bind(&done);
1797 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001798}
1799
David Brazdil58282f42016-01-14 12:45:10 +00001800void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001801 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1802 blocked_core_registers_[ZERO] = true;
1803 blocked_core_registers_[K0] = true;
1804 blocked_core_registers_[K1] = true;
1805 blocked_core_registers_[GP] = true;
1806 blocked_core_registers_[SP] = true;
1807 blocked_core_registers_[RA] = true;
1808
1809 // AT and TMP(T8) are used as temporary/scratch registers
1810 // (similar to how AT is used by MIPS assemblers).
1811 blocked_core_registers_[AT] = true;
1812 blocked_core_registers_[TMP] = true;
1813 blocked_fpu_registers_[FTMP] = true;
1814
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001815 if (GetInstructionSetFeatures().HasMsa()) {
1816 // To be used just for MSA instructions.
1817 blocked_fpu_registers_[FTMP2] = true;
1818 }
1819
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001820 // Reserve suspend and thread registers.
1821 blocked_core_registers_[S0] = true;
1822 blocked_core_registers_[TR] = true;
1823
1824 // Reserve T9 for function calls
1825 blocked_core_registers_[T9] = true;
1826
1827 // Reserve odd-numbered FPU registers.
1828 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1829 blocked_fpu_registers_[i] = true;
1830 }
1831
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001832 if (GetGraph()->IsDebuggable()) {
1833 // Stubs do not save callee-save floating point registers. If the graph
1834 // is debuggable, we need to deal with these registers differently. For
1835 // now, just block them.
1836 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1837 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1838 }
1839 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001840}
1841
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001842size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1843 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1844 return kMipsWordSize;
1845}
1846
1847size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1848 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1849 return kMipsWordSize;
1850}
1851
1852size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001853 if (GetGraph()->HasSIMD()) {
1854 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1855 } else {
1856 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1857 }
1858 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001859}
1860
1861size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001862 if (GetGraph()->HasSIMD()) {
1863 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1864 } else {
1865 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1866 }
1867 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001868}
1869
1870void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001871 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001872}
1873
1874void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001875 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876}
1877
Serban Constantinescufca16662016-07-14 09:21:59 +01001878constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1879
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1881 HInstruction* instruction,
1882 uint32_t dex_pc,
1883 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001884 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001885 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1886 IsDirectEntrypoint(entrypoint));
1887 if (EntrypointRequiresStackMap(entrypoint)) {
1888 RecordPcInfo(instruction, dex_pc, slow_path);
1889 }
1890}
1891
1892void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1893 HInstruction* instruction,
1894 SlowPathCode* slow_path,
1895 bool direct) {
1896 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1897 GenerateInvokeRuntime(entry_point_offset, direct);
1898}
1899
1900void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001901 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001902 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001903 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001904 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001905 // Reserve argument space on stack (for $a0-$a3) for
1906 // entrypoints that directly reference native implementations.
1907 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001908 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001909 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001910 } else {
1911 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001913 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001914}
1915
1916void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1917 Register class_reg) {
Igor Murashkin86083f72017-10-27 10:59:04 -07001918 __ LoadFromOffset(kLoadSignedByte, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1920 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1921 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1922 __ Sync(0);
1923 __ Bind(slow_path->GetExitLabel());
1924}
1925
1926void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1927 __ Sync(0); // Only stype 0 is supported.
1928}
1929
1930void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1931 HBasicBlock* successor) {
1932 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001933 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1934
1935 if (slow_path == nullptr) {
1936 slow_path =
1937 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1938 instruction->SetSlowPath(slow_path);
1939 codegen_->AddSlowPath(slow_path);
1940 if (successor != nullptr) {
1941 DCHECK(successor->IsLoopHeader());
1942 }
1943 } else {
1944 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1945 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946
1947 __ LoadFromOffset(kLoadUnsignedHalfword,
1948 TMP,
1949 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001950 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951 if (successor == nullptr) {
1952 __ Bnez(TMP, slow_path->GetEntryLabel());
1953 __ Bind(slow_path->GetReturnLabel());
1954 } else {
1955 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1956 __ B(slow_path->GetEntryLabel());
1957 // slow_path will return to GetLabelOf(successor).
1958 }
1959}
1960
1961InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1962 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001963 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001964 assembler_(codegen->GetAssembler()),
1965 codegen_(codegen) {}
1966
1967void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1968 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001969 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001970 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01001971 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001972 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001973 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001974 locations->SetInAt(0, Location::RequiresRegister());
1975 HInstruction* right = instruction->InputAt(1);
1976 bool can_use_imm = false;
1977 if (right->IsConstant()) {
1978 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1979 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1980 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001981 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001982 DCHECK(instruction->IsSub() || instruction->IsAdd());
1983 if (instruction->IsSub()) {
1984 imm = -imm;
1985 }
1986 if (isR6) {
1987 bool single_use = right->GetUses().HasExactlyOneElement();
1988 int16_t imm_high = High16Bits(imm);
1989 int16_t imm_low = Low16Bits(imm);
1990 if (imm_low < 0) {
1991 imm_high += 1;
1992 }
1993 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
1994 } else {
1995 can_use_imm = IsInt<16>(imm);
1996 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001997 }
1998 }
1999 if (can_use_imm)
2000 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2001 else
2002 locations->SetInAt(1, Location::RequiresRegister());
2003 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2004 break;
2005 }
2006
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002007 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002008 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002009 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2010 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002011 break;
2012 }
2013
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002014 case DataType::Type::kFloat32:
2015 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002016 DCHECK(instruction->IsAdd() || instruction->IsSub());
2017 locations->SetInAt(0, Location::RequiresFpuRegister());
2018 locations->SetInAt(1, Location::RequiresFpuRegister());
2019 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2020 break;
2021
2022 default:
2023 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2024 }
2025}
2026
2027void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002028 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002029 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002030 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002031
2032 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002033 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002034 Register dst = locations->Out().AsRegister<Register>();
2035 Register lhs = locations->InAt(0).AsRegister<Register>();
2036 Location rhs_location = locations->InAt(1);
2037
2038 Register rhs_reg = ZERO;
2039 int32_t rhs_imm = 0;
2040 bool use_imm = rhs_location.IsConstant();
2041 if (use_imm) {
2042 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2043 } else {
2044 rhs_reg = rhs_location.AsRegister<Register>();
2045 }
2046
2047 if (instruction->IsAnd()) {
2048 if (use_imm)
2049 __ Andi(dst, lhs, rhs_imm);
2050 else
2051 __ And(dst, lhs, rhs_reg);
2052 } else if (instruction->IsOr()) {
2053 if (use_imm)
2054 __ Ori(dst, lhs, rhs_imm);
2055 else
2056 __ Or(dst, lhs, rhs_reg);
2057 } else if (instruction->IsXor()) {
2058 if (use_imm)
2059 __ Xori(dst, lhs, rhs_imm);
2060 else
2061 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002062 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002063 DCHECK(instruction->IsAdd() || instruction->IsSub());
2064 if (use_imm) {
2065 if (instruction->IsSub()) {
2066 rhs_imm = -rhs_imm;
2067 }
2068 if (IsInt<16>(rhs_imm)) {
2069 __ Addiu(dst, lhs, rhs_imm);
2070 } else {
2071 DCHECK(isR6);
2072 int16_t rhs_imm_high = High16Bits(rhs_imm);
2073 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2074 if (rhs_imm_low < 0) {
2075 rhs_imm_high += 1;
2076 }
2077 __ Aui(dst, lhs, rhs_imm_high);
2078 if (rhs_imm_low != 0) {
2079 __ Addiu(dst, dst, rhs_imm_low);
2080 }
2081 }
2082 } else if (instruction->IsAdd()) {
2083 __ Addu(dst, lhs, rhs_reg);
2084 } else {
2085 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002087 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002088 }
2089 break;
2090 }
2091
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002092 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002093 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2094 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2095 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2096 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002097 Location rhs_location = locations->InAt(1);
2098 bool use_imm = rhs_location.IsConstant();
2099 if (!use_imm) {
2100 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2101 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2102 if (instruction->IsAnd()) {
2103 __ And(dst_low, lhs_low, rhs_low);
2104 __ And(dst_high, lhs_high, rhs_high);
2105 } else if (instruction->IsOr()) {
2106 __ Or(dst_low, lhs_low, rhs_low);
2107 __ Or(dst_high, lhs_high, rhs_high);
2108 } else if (instruction->IsXor()) {
2109 __ Xor(dst_low, lhs_low, rhs_low);
2110 __ Xor(dst_high, lhs_high, rhs_high);
2111 } else if (instruction->IsAdd()) {
2112 if (lhs_low == rhs_low) {
2113 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2114 __ Slt(TMP, lhs_low, ZERO);
2115 __ Addu(dst_low, lhs_low, rhs_low);
2116 } else {
2117 __ Addu(dst_low, lhs_low, rhs_low);
2118 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2119 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2120 }
2121 __ Addu(dst_high, lhs_high, rhs_high);
2122 __ Addu(dst_high, dst_high, TMP);
2123 } else {
2124 DCHECK(instruction->IsSub());
2125 __ Sltu(TMP, lhs_low, rhs_low);
2126 __ Subu(dst_low, lhs_low, rhs_low);
2127 __ Subu(dst_high, lhs_high, rhs_high);
2128 __ Subu(dst_high, dst_high, TMP);
2129 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002130 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002131 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2132 if (instruction->IsOr()) {
2133 uint32_t low = Low32Bits(value);
2134 uint32_t high = High32Bits(value);
2135 if (IsUint<16>(low)) {
2136 if (dst_low != lhs_low || low != 0) {
2137 __ Ori(dst_low, lhs_low, low);
2138 }
2139 } else {
2140 __ LoadConst32(TMP, low);
2141 __ Or(dst_low, lhs_low, TMP);
2142 }
2143 if (IsUint<16>(high)) {
2144 if (dst_high != lhs_high || high != 0) {
2145 __ Ori(dst_high, lhs_high, high);
2146 }
2147 } else {
2148 if (high != low) {
2149 __ LoadConst32(TMP, high);
2150 }
2151 __ Or(dst_high, lhs_high, TMP);
2152 }
2153 } else if (instruction->IsXor()) {
2154 uint32_t low = Low32Bits(value);
2155 uint32_t high = High32Bits(value);
2156 if (IsUint<16>(low)) {
2157 if (dst_low != lhs_low || low != 0) {
2158 __ Xori(dst_low, lhs_low, low);
2159 }
2160 } else {
2161 __ LoadConst32(TMP, low);
2162 __ Xor(dst_low, lhs_low, TMP);
2163 }
2164 if (IsUint<16>(high)) {
2165 if (dst_high != lhs_high || high != 0) {
2166 __ Xori(dst_high, lhs_high, high);
2167 }
2168 } else {
2169 if (high != low) {
2170 __ LoadConst32(TMP, high);
2171 }
2172 __ Xor(dst_high, lhs_high, TMP);
2173 }
2174 } else if (instruction->IsAnd()) {
2175 uint32_t low = Low32Bits(value);
2176 uint32_t high = High32Bits(value);
2177 if (IsUint<16>(low)) {
2178 __ Andi(dst_low, lhs_low, low);
2179 } else if (low != 0xFFFFFFFF) {
2180 __ LoadConst32(TMP, low);
2181 __ And(dst_low, lhs_low, TMP);
2182 } else if (dst_low != lhs_low) {
2183 __ Move(dst_low, lhs_low);
2184 }
2185 if (IsUint<16>(high)) {
2186 __ Andi(dst_high, lhs_high, high);
2187 } else if (high != 0xFFFFFFFF) {
2188 if (high != low) {
2189 __ LoadConst32(TMP, high);
2190 }
2191 __ And(dst_high, lhs_high, TMP);
2192 } else if (dst_high != lhs_high) {
2193 __ Move(dst_high, lhs_high);
2194 }
2195 } else {
2196 if (instruction->IsSub()) {
2197 value = -value;
2198 } else {
2199 DCHECK(instruction->IsAdd());
2200 }
2201 int32_t low = Low32Bits(value);
2202 int32_t high = High32Bits(value);
2203 if (IsInt<16>(low)) {
2204 if (dst_low != lhs_low || low != 0) {
2205 __ Addiu(dst_low, lhs_low, low);
2206 }
2207 if (low != 0) {
2208 __ Sltiu(AT, dst_low, low);
2209 }
2210 } else {
2211 __ LoadConst32(TMP, low);
2212 __ Addu(dst_low, lhs_low, TMP);
2213 __ Sltu(AT, dst_low, TMP);
2214 }
2215 if (IsInt<16>(high)) {
2216 if (dst_high != lhs_high || high != 0) {
2217 __ Addiu(dst_high, lhs_high, high);
2218 }
2219 } else {
2220 if (high != low) {
2221 __ LoadConst32(TMP, high);
2222 }
2223 __ Addu(dst_high, lhs_high, TMP);
2224 }
2225 if (low != 0) {
2226 __ Addu(dst_high, dst_high, AT);
2227 }
2228 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002229 }
2230 break;
2231 }
2232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002233 case DataType::Type::kFloat32:
2234 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002235 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2236 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2237 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2238 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002239 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002240 __ AddS(dst, lhs, rhs);
2241 } else {
2242 __ AddD(dst, lhs, rhs);
2243 }
2244 } else {
2245 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002246 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002247 __ SubS(dst, lhs, rhs);
2248 } else {
2249 __ SubD(dst, lhs, rhs);
2250 }
2251 }
2252 break;
2253 }
2254
2255 default:
2256 LOG(FATAL) << "Unexpected binary operation type " << type;
2257 }
2258}
2259
2260void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002261 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262
Vladimir Markoca6fff82017-10-03 14:49:14 +01002263 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002264 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002265 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002267 locations->SetInAt(0, Location::RequiresRegister());
2268 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2270 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002271 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002272 locations->SetInAt(0, Location::RequiresRegister());
2273 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2274 locations->SetOut(Location::RequiresRegister());
2275 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002276 default:
2277 LOG(FATAL) << "Unexpected shift type " << type;
2278 }
2279}
2280
2281static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2282
2283void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002284 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002285 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002286 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002287
2288 Location rhs_location = locations->InAt(1);
2289 bool use_imm = rhs_location.IsConstant();
2290 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2291 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002292 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002294 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002295 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2296 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002297
2298 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002300 Register dst = locations->Out().AsRegister<Register>();
2301 Register lhs = locations->InAt(0).AsRegister<Register>();
2302 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002303 if (shift_value == 0) {
2304 if (dst != lhs) {
2305 __ Move(dst, lhs);
2306 }
2307 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002308 __ Sll(dst, lhs, shift_value);
2309 } else if (instr->IsShr()) {
2310 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002311 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002312 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002313 } else {
2314 if (has_ins_rotr) {
2315 __ Rotr(dst, lhs, shift_value);
2316 } else {
2317 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2318 __ Srl(dst, lhs, shift_value);
2319 __ Or(dst, dst, TMP);
2320 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002321 }
2322 } else {
2323 if (instr->IsShl()) {
2324 __ Sllv(dst, lhs, rhs_reg);
2325 } else if (instr->IsShr()) {
2326 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002327 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002328 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002329 } else {
2330 if (has_ins_rotr) {
2331 __ Rotrv(dst, lhs, rhs_reg);
2332 } else {
2333 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002334 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2335 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2336 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2337 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2338 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002339 __ Sllv(TMP, lhs, TMP);
2340 __ Srlv(dst, lhs, rhs_reg);
2341 __ Or(dst, dst, TMP);
2342 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002343 }
2344 }
2345 break;
2346 }
2347
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002349 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2350 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2351 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2352 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2353 if (use_imm) {
2354 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002355 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002356 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002357 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002358 if (instr->IsShl()) {
2359 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2360 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2361 __ Sll(dst_low, lhs_low, shift_value);
2362 } else if (instr->IsShr()) {
2363 __ Srl(dst_low, lhs_low, shift_value);
2364 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2365 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002366 } else if (instr->IsUShr()) {
2367 __ Srl(dst_low, lhs_low, shift_value);
2368 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2369 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002370 } else {
2371 __ Srl(dst_low, lhs_low, shift_value);
2372 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2373 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002374 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002375 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002377 if (instr->IsShl()) {
2378 __ Sll(dst_low, lhs_low, shift_value);
2379 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2380 __ Sll(dst_high, lhs_high, shift_value);
2381 __ Or(dst_high, dst_high, TMP);
2382 } else if (instr->IsShr()) {
2383 __ Sra(dst_high, lhs_high, shift_value);
2384 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2385 __ Srl(dst_low, lhs_low, shift_value);
2386 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002387 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002388 __ Srl(dst_high, lhs_high, shift_value);
2389 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2390 __ Srl(dst_low, lhs_low, shift_value);
2391 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002392 } else {
2393 __ Srl(TMP, lhs_low, shift_value);
2394 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2395 __ Or(dst_low, dst_low, TMP);
2396 __ Srl(TMP, lhs_high, shift_value);
2397 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2398 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002399 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002400 }
2401 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002402 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002403 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002404 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002405 __ Move(dst_low, ZERO);
2406 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002407 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002408 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002409 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002410 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002411 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002412 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002413 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002414 // 64-bit rotation by 32 is just a swap.
2415 __ Move(dst_low, lhs_high);
2416 __ Move(dst_high, lhs_low);
2417 } else {
2418 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002419 __ Srl(dst_low, lhs_high, shift_value_high);
2420 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2421 __ Srl(dst_high, lhs_low, shift_value_high);
2422 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002423 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002424 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2425 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002426 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002427 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2428 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002429 __ Or(dst_high, dst_high, TMP);
2430 }
2431 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432 }
2433 }
2434 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002435 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002436 MipsLabel done;
2437 if (instr->IsShl()) {
2438 __ Sllv(dst_low, lhs_low, rhs_reg);
2439 __ Nor(AT, ZERO, rhs_reg);
2440 __ Srl(TMP, lhs_low, 1);
2441 __ Srlv(TMP, TMP, AT);
2442 __ Sllv(dst_high, lhs_high, rhs_reg);
2443 __ Or(dst_high, dst_high, TMP);
2444 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002445 if (isR6) {
2446 __ Beqzc(TMP, &done, /* is_bare */ true);
2447 __ Move(dst_high, dst_low);
2448 __ Move(dst_low, ZERO);
2449 } else {
2450 __ Movn(dst_high, dst_low, TMP);
2451 __ Movn(dst_low, ZERO, TMP);
2452 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002453 } else if (instr->IsShr()) {
2454 __ Srav(dst_high, lhs_high, rhs_reg);
2455 __ Nor(AT, ZERO, rhs_reg);
2456 __ Sll(TMP, lhs_high, 1);
2457 __ Sllv(TMP, TMP, AT);
2458 __ Srlv(dst_low, lhs_low, rhs_reg);
2459 __ Or(dst_low, dst_low, TMP);
2460 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002461 if (isR6) {
2462 __ Beqzc(TMP, &done, /* is_bare */ true);
2463 __ Move(dst_low, dst_high);
2464 __ Sra(dst_high, dst_high, 31);
2465 } else {
2466 __ Sra(AT, dst_high, 31);
2467 __ Movn(dst_low, dst_high, TMP);
2468 __ Movn(dst_high, AT, TMP);
2469 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002470 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002471 __ Srlv(dst_high, lhs_high, rhs_reg);
2472 __ Nor(AT, ZERO, rhs_reg);
2473 __ Sll(TMP, lhs_high, 1);
2474 __ Sllv(TMP, TMP, AT);
2475 __ Srlv(dst_low, lhs_low, rhs_reg);
2476 __ Or(dst_low, dst_low, TMP);
2477 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002478 if (isR6) {
2479 __ Beqzc(TMP, &done, /* is_bare */ true);
2480 __ Move(dst_low, dst_high);
2481 __ Move(dst_high, ZERO);
2482 } else {
2483 __ Movn(dst_low, dst_high, TMP);
2484 __ Movn(dst_high, ZERO, TMP);
2485 }
2486 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002487 __ Nor(AT, ZERO, rhs_reg);
2488 __ Srlv(TMP, lhs_low, rhs_reg);
2489 __ Sll(dst_low, lhs_high, 1);
2490 __ Sllv(dst_low, dst_low, AT);
2491 __ Or(dst_low, dst_low, TMP);
2492 __ Srlv(TMP, lhs_high, rhs_reg);
2493 __ Sll(dst_high, lhs_low, 1);
2494 __ Sllv(dst_high, dst_high, AT);
2495 __ Or(dst_high, dst_high, TMP);
2496 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002497 if (isR6) {
2498 __ Beqzc(TMP, &done, /* is_bare */ true);
2499 __ Move(TMP, dst_high);
2500 __ Move(dst_high, dst_low);
2501 __ Move(dst_low, TMP);
2502 } else {
2503 __ Movn(AT, dst_high, TMP);
2504 __ Movn(dst_high, dst_low, TMP);
2505 __ Movn(dst_low, AT, TMP);
2506 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002507 }
2508 __ Bind(&done);
2509 }
2510 break;
2511 }
2512
2513 default:
2514 LOG(FATAL) << "Unexpected shift operation type " << type;
2515 }
2516}
2517
2518void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2519 HandleBinaryOp(instruction);
2520}
2521
2522void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2523 HandleBinaryOp(instruction);
2524}
2525
2526void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2527 HandleBinaryOp(instruction);
2528}
2529
2530void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2531 HandleBinaryOp(instruction);
2532}
2533
2534void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002535 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002536 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002537 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002538 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002539 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2540 object_array_get_with_read_barrier
2541 ? LocationSummary::kCallOnSlowPath
2542 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002543 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2544 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2545 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002546 locations->SetInAt(0, Location::RequiresRegister());
2547 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002549 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2550 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002551 // The output overlaps in the case of an object array get with
2552 // read barriers enabled: we do not want the move to overwrite the
2553 // array's location, as we need it to emit the read barrier.
2554 locations->SetOut(Location::RequiresRegister(),
2555 object_array_get_with_read_barrier
2556 ? Location::kOutputOverlap
2557 : Location::kNoOutputOverlap);
2558 }
2559 // We need a temporary register for the read barrier marking slow
2560 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2561 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002562 bool temp_needed = instruction->GetIndex()->IsConstant()
2563 ? !kBakerReadBarrierThunksEnableForFields
2564 : !kBakerReadBarrierThunksEnableForArrays;
2565 if (temp_needed) {
2566 locations->AddTemp(Location::RequiresRegister());
2567 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002568 }
2569}
2570
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002571static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2572 auto null_checker = [codegen, instruction]() {
2573 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002574 };
2575 return null_checker;
2576}
2577
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002578void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2579 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002580 Location obj_loc = locations->InAt(0);
2581 Register obj = obj_loc.AsRegister<Register>();
2582 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002583 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002584 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002585 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002586
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002587 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002588 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2589 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002590 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002591 case DataType::Type::kBool:
2592 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002593 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002594 if (index.IsConstant()) {
2595 size_t offset =
2596 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002597 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002598 } else {
2599 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002600 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002601 }
2602 break;
2603 }
2604
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002605 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002606 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 if (index.IsConstant()) {
2608 size_t offset =
2609 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002610 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002611 } else {
2612 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002613 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002614 }
2615 break;
2616 }
2617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002618 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002619 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002620 if (maybe_compressed_char_at) {
2621 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2622 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2623 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2624 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2625 "Expecting 0=compressed, 1=uncompressed");
2626 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002627 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002628 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2629 if (maybe_compressed_char_at) {
2630 MipsLabel uncompressed_load, done;
2631 __ Bnez(TMP, &uncompressed_load);
2632 __ LoadFromOffset(kLoadUnsignedByte,
2633 out,
2634 obj,
2635 data_offset + (const_index << TIMES_1));
2636 __ B(&done);
2637 __ Bind(&uncompressed_load);
2638 __ LoadFromOffset(kLoadUnsignedHalfword,
2639 out,
2640 obj,
2641 data_offset + (const_index << TIMES_2));
2642 __ Bind(&done);
2643 } else {
2644 __ LoadFromOffset(kLoadUnsignedHalfword,
2645 out,
2646 obj,
2647 data_offset + (const_index << TIMES_2),
2648 null_checker);
2649 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002650 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002651 Register index_reg = index.AsRegister<Register>();
2652 if (maybe_compressed_char_at) {
2653 MipsLabel uncompressed_load, done;
2654 __ Bnez(TMP, &uncompressed_load);
2655 __ Addu(TMP, obj, index_reg);
2656 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2657 __ B(&done);
2658 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002659 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002660 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2661 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002662 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2663 __ Addu(TMP, index_reg, obj);
2664 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002665 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002666 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002667 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2668 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669 }
2670 break;
2671 }
2672
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002673 case DataType::Type::kInt16: {
2674 Register out = out_loc.AsRegister<Register>();
2675 if (index.IsConstant()) {
2676 size_t offset =
2677 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2678 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002679 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2680 __ Addu(TMP, index.AsRegister<Register>(), obj);
2681 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002682 } else {
2683 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2684 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2685 }
2686 break;
2687 }
2688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002689 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002690 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002691 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002692 if (index.IsConstant()) {
2693 size_t offset =
2694 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002695 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002696 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2697 __ Addu(TMP, index.AsRegister<Register>(), obj);
2698 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002700 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002701 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002702 }
2703 break;
2704 }
2705
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002706 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002707 static_assert(
2708 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2709 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2710 // /* HeapReference<Object> */ out =
2711 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2712 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002713 bool temp_needed = index.IsConstant()
2714 ? !kBakerReadBarrierThunksEnableForFields
2715 : !kBakerReadBarrierThunksEnableForArrays;
2716 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002717 // Note that a potential implicit null check is handled in this
2718 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002719 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2720 if (index.IsConstant()) {
2721 // Array load with a constant index can be treated as a field load.
2722 size_t offset =
2723 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2724 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2725 out_loc,
2726 obj,
2727 offset,
2728 temp,
2729 /* needs_null_check */ false);
2730 } else {
2731 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2732 out_loc,
2733 obj,
2734 data_offset,
2735 index,
2736 temp,
2737 /* needs_null_check */ false);
2738 }
Alexey Frunze15958152017-02-09 19:08:30 -08002739 } else {
2740 Register out = out_loc.AsRegister<Register>();
2741 if (index.IsConstant()) {
2742 size_t offset =
2743 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2744 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2745 // If read barriers are enabled, emit read barriers other than
2746 // Baker's using a slow path (and also unpoison the loaded
2747 // reference, if heap poisoning is enabled).
2748 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2749 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002750 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002751 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2752 // If read barriers are enabled, emit read barriers other than
2753 // Baker's using a slow path (and also unpoison the loaded
2754 // reference, if heap poisoning is enabled).
2755 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2756 out_loc,
2757 out_loc,
2758 obj_loc,
2759 data_offset,
2760 index);
2761 }
2762 }
2763 break;
2764 }
2765
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002766 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002767 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002768 if (index.IsConstant()) {
2769 size_t offset =
2770 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002771 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002772 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2773 __ Addu(TMP, index.AsRegister<Register>(), obj);
2774 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002775 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002776 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002777 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002778 }
2779 break;
2780 }
2781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002782 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002783 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002784 if (index.IsConstant()) {
2785 size_t offset =
2786 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002787 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002788 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2789 __ Addu(TMP, index.AsRegister<Register>(), obj);
2790 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002792 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002793 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002794 }
2795 break;
2796 }
2797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002799 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002800 if (index.IsConstant()) {
2801 size_t offset =
2802 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002803 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002804 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2805 __ Addu(TMP, index.AsRegister<Register>(), obj);
2806 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002807 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002808 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002809 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002810 }
2811 break;
2812 }
2813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002815 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2816 UNREACHABLE();
2817 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002818}
2819
2820void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002821 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002822 locations->SetInAt(0, Location::RequiresRegister());
2823 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2824}
2825
2826void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2827 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002828 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002829 Register obj = locations->InAt(0).AsRegister<Register>();
2830 Register out = locations->Out().AsRegister<Register>();
2831 __ LoadFromOffset(kLoadWord, out, obj, offset);
2832 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002833 // Mask out compression flag from String's array length.
2834 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2835 __ Srl(out, out, 1u);
2836 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002837}
2838
Alexey Frunzef58b2482016-09-02 22:14:06 -07002839Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2840 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2841 ? Location::ConstantLocation(instruction->AsConstant())
2842 : Location::RequiresRegister();
2843}
2844
2845Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2846 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2847 // We can store a non-zero float or double constant without first loading it into the FPU,
2848 // but we should only prefer this if the constant has a single use.
2849 if (instruction->IsConstant() &&
2850 (instruction->AsConstant()->IsZeroBitPattern() ||
2851 instruction->GetUses().HasExactlyOneElement())) {
2852 return Location::ConstantLocation(instruction->AsConstant());
2853 // Otherwise fall through and require an FPU register for the constant.
2854 }
2855 return Location::RequiresFpuRegister();
2856}
2857
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002858void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002859 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002860
2861 bool needs_write_barrier =
2862 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2863 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2864
Vladimir Markoca6fff82017-10-03 14:49:14 +01002865 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002866 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002867 may_need_runtime_call_for_type_check ?
2868 LocationSummary::kCallOnSlowPath :
2869 LocationSummary::kNoCall);
2870
2871 locations->SetInAt(0, Location::RequiresRegister());
2872 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002873 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002874 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002875 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002876 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2877 }
2878 if (needs_write_barrier) {
2879 // Temporary register for the write barrier.
2880 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002881 }
2882}
2883
2884void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2885 LocationSummary* locations = instruction->GetLocations();
2886 Register obj = locations->InAt(0).AsRegister<Register>();
2887 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002888 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002889 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002890 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002891 bool needs_write_barrier =
2892 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002893 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002894 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002895
2896 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002897 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002898 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002899 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002900 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002901 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002902 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002903 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002904 __ Addu(base_reg, obj, index.AsRegister<Register>());
2905 }
2906 if (value_location.IsConstant()) {
2907 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2908 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2909 } else {
2910 Register value = value_location.AsRegister<Register>();
2911 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002912 }
2913 break;
2914 }
2915
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002916 case DataType::Type::kUint16:
2917 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002918 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002919 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002920 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002921 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2922 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002923 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002924 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002925 }
2926 if (value_location.IsConstant()) {
2927 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2928 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2929 } else {
2930 Register value = value_location.AsRegister<Register>();
2931 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002932 }
2933 break;
2934 }
2935
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002936 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002937 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2938 if (index.IsConstant()) {
2939 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002940 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2941 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002942 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002943 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002944 }
2945 if (value_location.IsConstant()) {
2946 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2947 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2948 } else {
2949 Register value = value_location.AsRegister<Register>();
2950 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2951 }
2952 break;
2953 }
2954
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002956 if (value_location.IsConstant()) {
2957 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002958 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002959 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002960 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002961 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002962 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002963 }
Alexey Frunze15958152017-02-09 19:08:30 -08002964 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2965 DCHECK_EQ(value, 0);
2966 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2967 DCHECK(!needs_write_barrier);
2968 DCHECK(!may_need_runtime_call_for_type_check);
2969 break;
2970 }
2971
2972 DCHECK(needs_write_barrier);
2973 Register value = value_location.AsRegister<Register>();
2974 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2975 Register temp2 = TMP; // Doesn't need to survive slow path.
2976 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2977 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2978 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2979 MipsLabel done;
2980 SlowPathCodeMIPS* slow_path = nullptr;
2981
2982 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002983 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002984 codegen_->AddSlowPath(slow_path);
2985 if (instruction->GetValueCanBeNull()) {
2986 MipsLabel non_zero;
2987 __ Bnez(value, &non_zero);
2988 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2989 if (index.IsConstant()) {
2990 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002991 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2992 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08002993 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002994 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002995 }
Alexey Frunze15958152017-02-09 19:08:30 -08002996 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2997 __ B(&done);
2998 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002999 }
Alexey Frunze15958152017-02-09 19:08:30 -08003000
3001 // Note that when read barriers are enabled, the type checks
3002 // are performed without read barriers. This is fine, even in
3003 // the case where a class object is in the from-space after
3004 // the flip, as a comparison involving such a type would not
3005 // produce a false positive; it may of course produce a false
3006 // negative, in which case we would take the ArraySet slow
3007 // path.
3008
3009 // /* HeapReference<Class> */ temp1 = obj->klass_
3010 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3011 __ MaybeUnpoisonHeapReference(temp1);
3012
3013 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3014 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3015 // /* HeapReference<Class> */ temp2 = value->klass_
3016 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3017 // If heap poisoning is enabled, no need to unpoison `temp1`
3018 // nor `temp2`, as we are comparing two poisoned references.
3019
3020 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3021 MipsLabel do_put;
3022 __ Beq(temp1, temp2, &do_put);
3023 // If heap poisoning is enabled, the `temp1` reference has
3024 // not been unpoisoned yet; unpoison it now.
3025 __ MaybeUnpoisonHeapReference(temp1);
3026
3027 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3028 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3029 // If heap poisoning is enabled, no need to unpoison
3030 // `temp1`, as we are comparing against null below.
3031 __ Bnez(temp1, slow_path->GetEntryLabel());
3032 __ Bind(&do_put);
3033 } else {
3034 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3035 }
3036 }
3037
3038 Register source = value;
3039 if (kPoisonHeapReferences) {
3040 // Note that in the case where `value` is a null reference,
3041 // we do not enter this block, as a null reference does not
3042 // need poisoning.
3043 __ Move(temp1, value);
3044 __ PoisonHeapReference(temp1);
3045 source = temp1;
3046 }
3047
3048 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3049 if (index.IsConstant()) {
3050 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
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_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003053 }
3054 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3055
3056 if (!may_need_runtime_call_for_type_check) {
3057 codegen_->MaybeRecordImplicitNullCheck(instruction);
3058 }
3059
3060 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3061
3062 if (done.IsLinked()) {
3063 __ Bind(&done);
3064 }
3065
3066 if (slow_path != nullptr) {
3067 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003068 }
3069 break;
3070 }
3071
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003072 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003074 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003075 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003076 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3077 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003078 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003079 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003080 }
3081 if (value_location.IsConstant()) {
3082 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3083 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3084 } else {
3085 Register value = value_location.AsRegisterPairLow<Register>();
3086 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003087 }
3088 break;
3089 }
3090
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003091 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003092 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003094 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003095 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3096 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003097 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003098 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003099 }
3100 if (value_location.IsConstant()) {
3101 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3102 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3103 } else {
3104 FRegister value = value_location.AsFpuRegister<FRegister>();
3105 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003106 }
3107 break;
3108 }
3109
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003110 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003112 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003113 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003114 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3115 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003116 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003117 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003118 }
3119 if (value_location.IsConstant()) {
3120 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3121 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3122 } else {
3123 FRegister value = value_location.AsFpuRegister<FRegister>();
3124 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003125 }
3126 break;
3127 }
3128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003129 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003130 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3131 UNREACHABLE();
3132 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003133}
3134
Lena Djokica2901602017-09-21 13:50:52 +02003135void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3136 HIntermediateArrayAddressIndex* instruction) {
3137 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003138 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003139
3140 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3141
3142 locations->SetInAt(0, Location::RequiresRegister());
3143 locations->SetInAt(1, Location::ConstantLocation(shift));
3144 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3145}
3146
3147void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3148 HIntermediateArrayAddressIndex* instruction) {
3149 LocationSummary* locations = instruction->GetLocations();
3150 Register index_reg = locations->InAt(0).AsRegister<Register>();
3151 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3152 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3153}
3154
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003155void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003156 RegisterSet caller_saves = RegisterSet::Empty();
3157 InvokeRuntimeCallingConvention calling_convention;
3158 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3159 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3160 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003161
3162 HInstruction* index = instruction->InputAt(0);
3163 HInstruction* length = instruction->InputAt(1);
3164
3165 bool const_index = false;
3166 bool const_length = false;
3167
3168 if (index->IsConstant()) {
3169 if (length->IsConstant()) {
3170 const_index = true;
3171 const_length = true;
3172 } else {
3173 int32_t index_value = index->AsIntConstant()->GetValue();
3174 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3175 const_index = true;
3176 }
3177 }
3178 } else if (length->IsConstant()) {
3179 int32_t length_value = length->AsIntConstant()->GetValue();
3180 if (IsUint<15>(length_value)) {
3181 const_length = true;
3182 }
3183 }
3184
3185 locations->SetInAt(0, const_index
3186 ? Location::ConstantLocation(index->AsConstant())
3187 : Location::RequiresRegister());
3188 locations->SetInAt(1, const_length
3189 ? Location::ConstantLocation(length->AsConstant())
3190 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003191}
3192
3193void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3194 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003195 Location index_loc = locations->InAt(0);
3196 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003197
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003198 if (length_loc.IsConstant()) {
3199 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3200 if (index_loc.IsConstant()) {
3201 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3202 if (index < 0 || index >= length) {
3203 BoundsCheckSlowPathMIPS* slow_path =
3204 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3205 codegen_->AddSlowPath(slow_path);
3206 __ B(slow_path->GetEntryLabel());
3207 } else {
3208 // Nothing to be done.
3209 }
3210 return;
3211 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003212
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003213 BoundsCheckSlowPathMIPS* slow_path =
3214 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3215 codegen_->AddSlowPath(slow_path);
3216 Register index = index_loc.AsRegister<Register>();
3217 if (length == 0) {
3218 __ B(slow_path->GetEntryLabel());
3219 } else if (length == 1) {
3220 __ Bnez(index, slow_path->GetEntryLabel());
3221 } else {
3222 DCHECK(IsUint<15>(length)) << length;
3223 __ Sltiu(TMP, index, length);
3224 __ Beqz(TMP, slow_path->GetEntryLabel());
3225 }
3226 } else {
3227 Register length = length_loc.AsRegister<Register>();
3228 BoundsCheckSlowPathMIPS* slow_path =
3229 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3230 codegen_->AddSlowPath(slow_path);
3231 if (index_loc.IsConstant()) {
3232 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3233 if (index < 0) {
3234 __ B(slow_path->GetEntryLabel());
3235 } else if (index == 0) {
3236 __ Blez(length, slow_path->GetEntryLabel());
3237 } else {
3238 DCHECK(IsInt<16>(index + 1)) << index;
3239 __ Sltiu(TMP, length, index + 1);
3240 __ Bnez(TMP, slow_path->GetEntryLabel());
3241 }
3242 } else {
3243 Register index = index_loc.AsRegister<Register>();
3244 __ Bgeu(index, length, slow_path->GetEntryLabel());
3245 }
3246 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003247}
3248
Alexey Frunze15958152017-02-09 19:08:30 -08003249// Temp is used for read barrier.
3250static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3251 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003252 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003253 (kUseBakerReadBarrier ||
3254 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3255 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3256 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3257 return 1;
3258 }
3259 return 0;
3260}
3261
3262// Extra temp is used for read barrier.
3263static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3264 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3265}
3266
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003267void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003268 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3269 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3270
3271 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3272 switch (type_check_kind) {
3273 case TypeCheckKind::kExactCheck:
3274 case TypeCheckKind::kAbstractClassCheck:
3275 case TypeCheckKind::kClassHierarchyCheck:
3276 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003277 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003278 ? LocationSummary::kCallOnSlowPath
3279 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3280 break;
3281 case TypeCheckKind::kArrayCheck:
3282 case TypeCheckKind::kUnresolvedCheck:
3283 case TypeCheckKind::kInterfaceCheck:
3284 call_kind = LocationSummary::kCallOnSlowPath;
3285 break;
3286 }
3287
Vladimir Markoca6fff82017-10-03 14:49:14 +01003288 LocationSummary* locations =
3289 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003290 locations->SetInAt(0, Location::RequiresRegister());
3291 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003292 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003293}
3294
3295void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003296 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003297 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003298 Location obj_loc = locations->InAt(0);
3299 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003300 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003301 Location temp_loc = locations->GetTemp(0);
3302 Register temp = temp_loc.AsRegister<Register>();
3303 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3304 DCHECK_LE(num_temps, 2u);
3305 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003306 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3307 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3308 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3309 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3310 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3311 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3312 const uint32_t object_array_data_offset =
3313 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3314 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003315
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003316 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3317 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3318 // read barriers is done for performance and code size reasons.
3319 bool is_type_check_slow_path_fatal = false;
3320 if (!kEmitCompilerReadBarrier) {
3321 is_type_check_slow_path_fatal =
3322 (type_check_kind == TypeCheckKind::kExactCheck ||
3323 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3324 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3325 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3326 !instruction->CanThrowIntoCatchBlock();
3327 }
3328 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003329 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3330 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003331 codegen_->AddSlowPath(slow_path);
3332
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003333 // Avoid this check if we know `obj` is not null.
3334 if (instruction->MustDoNullCheck()) {
3335 __ Beqz(obj, &done);
3336 }
3337
3338 switch (type_check_kind) {
3339 case TypeCheckKind::kExactCheck:
3340 case TypeCheckKind::kArrayCheck: {
3341 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003342 GenerateReferenceLoadTwoRegisters(instruction,
3343 temp_loc,
3344 obj_loc,
3345 class_offset,
3346 maybe_temp2_loc,
3347 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003348 // Jump to slow path for throwing the exception or doing a
3349 // more involved array check.
3350 __ Bne(temp, cls, slow_path->GetEntryLabel());
3351 break;
3352 }
3353
3354 case TypeCheckKind::kAbstractClassCheck: {
3355 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003356 GenerateReferenceLoadTwoRegisters(instruction,
3357 temp_loc,
3358 obj_loc,
3359 class_offset,
3360 maybe_temp2_loc,
3361 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003362 // If the class is abstract, we eagerly fetch the super class of the
3363 // object to avoid doing a comparison we know will fail.
3364 MipsLabel loop;
3365 __ Bind(&loop);
3366 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003367 GenerateReferenceLoadOneRegister(instruction,
3368 temp_loc,
3369 super_offset,
3370 maybe_temp2_loc,
3371 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003372 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3373 // exception.
3374 __ Beqz(temp, slow_path->GetEntryLabel());
3375 // Otherwise, compare the classes.
3376 __ Bne(temp, cls, &loop);
3377 break;
3378 }
3379
3380 case TypeCheckKind::kClassHierarchyCheck: {
3381 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003382 GenerateReferenceLoadTwoRegisters(instruction,
3383 temp_loc,
3384 obj_loc,
3385 class_offset,
3386 maybe_temp2_loc,
3387 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003388 // Walk over the class hierarchy to find a match.
3389 MipsLabel loop;
3390 __ Bind(&loop);
3391 __ Beq(temp, cls, &done);
3392 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003393 GenerateReferenceLoadOneRegister(instruction,
3394 temp_loc,
3395 super_offset,
3396 maybe_temp2_loc,
3397 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003398 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3399 // exception. Otherwise, jump to the beginning of the loop.
3400 __ Bnez(temp, &loop);
3401 __ B(slow_path->GetEntryLabel());
3402 break;
3403 }
3404
3405 case TypeCheckKind::kArrayObjectCheck: {
3406 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003407 GenerateReferenceLoadTwoRegisters(instruction,
3408 temp_loc,
3409 obj_loc,
3410 class_offset,
3411 maybe_temp2_loc,
3412 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003413 // Do an exact check.
3414 __ Beq(temp, cls, &done);
3415 // Otherwise, we need to check that the object's class is a non-primitive array.
3416 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003417 GenerateReferenceLoadOneRegister(instruction,
3418 temp_loc,
3419 component_offset,
3420 maybe_temp2_loc,
3421 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003422 // If the component type is null, jump to the slow path to throw the exception.
3423 __ Beqz(temp, slow_path->GetEntryLabel());
3424 // Otherwise, the object is indeed an array, further check that this component
3425 // type is not a primitive type.
3426 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3427 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3428 __ Bnez(temp, slow_path->GetEntryLabel());
3429 break;
3430 }
3431
3432 case TypeCheckKind::kUnresolvedCheck:
3433 // We always go into the type check slow path for the unresolved check case.
3434 // We cannot directly call the CheckCast runtime entry point
3435 // without resorting to a type checking slow path here (i.e. by
3436 // calling InvokeRuntime directly), as it would require to
3437 // assign fixed registers for the inputs of this HInstanceOf
3438 // instruction (following the runtime calling convention), which
3439 // might be cluttered by the potential first read barrier
3440 // emission at the beginning of this method.
3441 __ B(slow_path->GetEntryLabel());
3442 break;
3443
3444 case TypeCheckKind::kInterfaceCheck: {
3445 // Avoid read barriers to improve performance of the fast path. We can not get false
3446 // positives by doing this.
3447 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003448 GenerateReferenceLoadTwoRegisters(instruction,
3449 temp_loc,
3450 obj_loc,
3451 class_offset,
3452 maybe_temp2_loc,
3453 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003454 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003455 GenerateReferenceLoadTwoRegisters(instruction,
3456 temp_loc,
3457 temp_loc,
3458 iftable_offset,
3459 maybe_temp2_loc,
3460 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003461 // Iftable is never null.
3462 __ Lw(TMP, temp, array_length_offset);
3463 // Loop through the iftable and check if any class matches.
3464 MipsLabel loop;
3465 __ Bind(&loop);
3466 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3467 __ Beqz(TMP, slow_path->GetEntryLabel());
3468 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3469 __ MaybeUnpoisonHeapReference(AT);
3470 // Go to next interface.
3471 __ Addiu(TMP, TMP, -2);
3472 // Compare the classes and continue the loop if they do not match.
3473 __ Bne(AT, cls, &loop);
3474 break;
3475 }
3476 }
3477
3478 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003479 __ Bind(slow_path->GetExitLabel());
3480}
3481
3482void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3483 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003484 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003485 locations->SetInAt(0, Location::RequiresRegister());
3486 if (check->HasUses()) {
3487 locations->SetOut(Location::SameAsFirstInput());
3488 }
3489}
3490
3491void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3492 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003493 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003494 check->GetLoadClass(),
3495 check,
3496 check->GetDexPc(),
3497 true);
3498 codegen_->AddSlowPath(slow_path);
3499 GenerateClassInitializationCheck(slow_path,
3500 check->GetLocations()->InAt(0).AsRegister<Register>());
3501}
3502
3503void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003504 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003505
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003506 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003507 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003508
3509 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003510 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003511 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003513 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003514 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003515 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003516 locations->SetInAt(0, Location::RequiresRegister());
3517 locations->SetInAt(1, Location::RequiresRegister());
3518 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3519 break;
3520
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003521 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003522 locations->SetInAt(0, Location::RequiresRegister());
3523 locations->SetInAt(1, Location::RequiresRegister());
3524 // Output overlaps because it is written before doing the low comparison.
3525 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3526 break;
3527
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003528 case DataType::Type::kFloat32:
3529 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003530 locations->SetInAt(0, Location::RequiresFpuRegister());
3531 locations->SetInAt(1, Location::RequiresFpuRegister());
3532 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003533 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003534
3535 default:
3536 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3537 }
3538}
3539
3540void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3541 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003542 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003543 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003544 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003545
3546 // 0 if: left == right
3547 // 1 if: left > right
3548 // -1 if: left < right
3549 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003550 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003551 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003552 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003553 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003554 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003555 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003556 Register lhs = locations->InAt(0).AsRegister<Register>();
3557 Register rhs = locations->InAt(1).AsRegister<Register>();
3558 __ Slt(TMP, lhs, rhs);
3559 __ Slt(res, rhs, lhs);
3560 __ Subu(res, res, TMP);
3561 break;
3562 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003563 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003564 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003565 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3566 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3567 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3568 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3569 // TODO: more efficient (direct) comparison with a constant.
3570 __ Slt(TMP, lhs_high, rhs_high);
3571 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3572 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3573 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3574 __ Sltu(TMP, lhs_low, rhs_low);
3575 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3576 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3577 __ Bind(&done);
3578 break;
3579 }
3580
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003581 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003582 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003583 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3584 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3585 MipsLabel done;
3586 if (isR6) {
3587 __ CmpEqS(FTMP, lhs, rhs);
3588 __ LoadConst32(res, 0);
3589 __ Bc1nez(FTMP, &done);
3590 if (gt_bias) {
3591 __ CmpLtS(FTMP, lhs, rhs);
3592 __ LoadConst32(res, -1);
3593 __ Bc1nez(FTMP, &done);
3594 __ LoadConst32(res, 1);
3595 } else {
3596 __ CmpLtS(FTMP, rhs, lhs);
3597 __ LoadConst32(res, 1);
3598 __ Bc1nez(FTMP, &done);
3599 __ LoadConst32(res, -1);
3600 }
3601 } else {
3602 if (gt_bias) {
3603 __ ColtS(0, lhs, rhs);
3604 __ LoadConst32(res, -1);
3605 __ Bc1t(0, &done);
3606 __ CeqS(0, lhs, rhs);
3607 __ LoadConst32(res, 1);
3608 __ Movt(res, ZERO, 0);
3609 } else {
3610 __ ColtS(0, rhs, lhs);
3611 __ LoadConst32(res, 1);
3612 __ Bc1t(0, &done);
3613 __ CeqS(0, lhs, rhs);
3614 __ LoadConst32(res, -1);
3615 __ Movt(res, ZERO, 0);
3616 }
3617 }
3618 __ Bind(&done);
3619 break;
3620 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003621 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003622 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003623 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3624 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3625 MipsLabel done;
3626 if (isR6) {
3627 __ CmpEqD(FTMP, lhs, rhs);
3628 __ LoadConst32(res, 0);
3629 __ Bc1nez(FTMP, &done);
3630 if (gt_bias) {
3631 __ CmpLtD(FTMP, lhs, rhs);
3632 __ LoadConst32(res, -1);
3633 __ Bc1nez(FTMP, &done);
3634 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003635 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003636 __ CmpLtD(FTMP, rhs, lhs);
3637 __ LoadConst32(res, 1);
3638 __ Bc1nez(FTMP, &done);
3639 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003640 }
3641 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003642 if (gt_bias) {
3643 __ ColtD(0, lhs, rhs);
3644 __ LoadConst32(res, -1);
3645 __ Bc1t(0, &done);
3646 __ CeqD(0, lhs, rhs);
3647 __ LoadConst32(res, 1);
3648 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003649 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003650 __ ColtD(0, rhs, lhs);
3651 __ LoadConst32(res, 1);
3652 __ Bc1t(0, &done);
3653 __ CeqD(0, lhs, rhs);
3654 __ LoadConst32(res, -1);
3655 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003656 }
3657 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003658 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003659 break;
3660 }
3661
3662 default:
3663 LOG(FATAL) << "Unimplemented compare type " << in_type;
3664 }
3665}
3666
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003667void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003668 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003669 switch (instruction->InputAt(0)->GetType()) {
3670 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003671 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003672 locations->SetInAt(0, Location::RequiresRegister());
3673 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3674 break;
3675
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003676 case DataType::Type::kFloat32:
3677 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003678 locations->SetInAt(0, Location::RequiresFpuRegister());
3679 locations->SetInAt(1, Location::RequiresFpuRegister());
3680 break;
3681 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003682 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003683 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3684 }
3685}
3686
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003687void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003688 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003689 return;
3690 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003692 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003693 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003694
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003695 switch (type) {
3696 default:
3697 // Integer case.
3698 GenerateIntCompare(instruction->GetCondition(), locations);
3699 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003700
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003701 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003702 GenerateLongCompare(instruction->GetCondition(), locations);
3703 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003705 case DataType::Type::kFloat32:
3706 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003707 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3708 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003709 }
3710}
3711
Alexey Frunze7e99e052015-11-24 19:28:01 -08003712void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3713 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003714 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003715
3716 LocationSummary* locations = instruction->GetLocations();
3717 Location second = locations->InAt(1);
3718 DCHECK(second.IsConstant());
3719
3720 Register out = locations->Out().AsRegister<Register>();
3721 Register dividend = locations->InAt(0).AsRegister<Register>();
3722 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3723 DCHECK(imm == 1 || imm == -1);
3724
3725 if (instruction->IsRem()) {
3726 __ Move(out, ZERO);
3727 } else {
3728 if (imm == -1) {
3729 __ Subu(out, ZERO, dividend);
3730 } else if (out != dividend) {
3731 __ Move(out, dividend);
3732 }
3733 }
3734}
3735
3736void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3737 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003738 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003739
3740 LocationSummary* locations = instruction->GetLocations();
3741 Location second = locations->InAt(1);
3742 DCHECK(second.IsConstant());
3743
3744 Register out = locations->Out().AsRegister<Register>();
3745 Register dividend = locations->InAt(0).AsRegister<Register>();
3746 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003747 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003748 int ctz_imm = CTZ(abs_imm);
3749
3750 if (instruction->IsDiv()) {
3751 if (ctz_imm == 1) {
3752 // Fast path for division by +/-2, which is very common.
3753 __ Srl(TMP, dividend, 31);
3754 } else {
3755 __ Sra(TMP, dividend, 31);
3756 __ Srl(TMP, TMP, 32 - ctz_imm);
3757 }
3758 __ Addu(out, dividend, TMP);
3759 __ Sra(out, out, ctz_imm);
3760 if (imm < 0) {
3761 __ Subu(out, ZERO, out);
3762 }
3763 } else {
3764 if (ctz_imm == 1) {
3765 // Fast path for modulo +/-2, which is very common.
3766 __ Sra(TMP, dividend, 31);
3767 __ Subu(out, dividend, TMP);
3768 __ Andi(out, out, 1);
3769 __ Addu(out, out, TMP);
3770 } else {
3771 __ Sra(TMP, dividend, 31);
3772 __ Srl(TMP, TMP, 32 - ctz_imm);
3773 __ Addu(out, dividend, TMP);
3774 if (IsUint<16>(abs_imm - 1)) {
3775 __ Andi(out, out, abs_imm - 1);
3776 } else {
3777 __ Sll(out, out, 32 - ctz_imm);
3778 __ Srl(out, out, 32 - ctz_imm);
3779 }
3780 __ Subu(out, out, TMP);
3781 }
3782 }
3783}
3784
3785void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3786 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003787 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003788
3789 LocationSummary* locations = instruction->GetLocations();
3790 Location second = locations->InAt(1);
3791 DCHECK(second.IsConstant());
3792
3793 Register out = locations->Out().AsRegister<Register>();
3794 Register dividend = locations->InAt(0).AsRegister<Register>();
3795 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3796
3797 int64_t magic;
3798 int shift;
3799 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3800
3801 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3802
3803 __ LoadConst32(TMP, magic);
3804 if (isR6) {
3805 __ MuhR6(TMP, dividend, TMP);
3806 } else {
3807 __ MultR2(dividend, TMP);
3808 __ Mfhi(TMP);
3809 }
3810 if (imm > 0 && magic < 0) {
3811 __ Addu(TMP, TMP, dividend);
3812 } else if (imm < 0 && magic > 0) {
3813 __ Subu(TMP, TMP, dividend);
3814 }
3815
3816 if (shift != 0) {
3817 __ Sra(TMP, TMP, shift);
3818 }
3819
3820 if (instruction->IsDiv()) {
3821 __ Sra(out, TMP, 31);
3822 __ Subu(out, TMP, out);
3823 } else {
3824 __ Sra(AT, TMP, 31);
3825 __ Subu(AT, TMP, AT);
3826 __ LoadConst32(TMP, imm);
3827 if (isR6) {
3828 __ MulR6(TMP, AT, TMP);
3829 } else {
3830 __ MulR2(TMP, AT, TMP);
3831 }
3832 __ Subu(out, dividend, TMP);
3833 }
3834}
3835
3836void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3837 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003838 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003839
3840 LocationSummary* locations = instruction->GetLocations();
3841 Register out = locations->Out().AsRegister<Register>();
3842 Location second = locations->InAt(1);
3843
3844 if (second.IsConstant()) {
3845 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3846 if (imm == 0) {
3847 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3848 } else if (imm == 1 || imm == -1) {
3849 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003850 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003851 DivRemByPowerOfTwo(instruction);
3852 } else {
3853 DCHECK(imm <= -2 || imm >= 2);
3854 GenerateDivRemWithAnyConstant(instruction);
3855 }
3856 } else {
3857 Register dividend = locations->InAt(0).AsRegister<Register>();
3858 Register divisor = second.AsRegister<Register>();
3859 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3860 if (instruction->IsDiv()) {
3861 if (isR6) {
3862 __ DivR6(out, dividend, divisor);
3863 } else {
3864 __ DivR2(out, dividend, divisor);
3865 }
3866 } else {
3867 if (isR6) {
3868 __ ModR6(out, dividend, divisor);
3869 } else {
3870 __ ModR2(out, dividend, divisor);
3871 }
3872 }
3873 }
3874}
3875
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003876void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003877 DataType::Type type = div->GetResultType();
3878 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003879 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003880 : LocationSummary::kNoCall;
3881
Vladimir Markoca6fff82017-10-03 14:49:14 +01003882 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003883
3884 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003885 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003886 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003887 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003888 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3889 break;
3890
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003891 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003892 InvokeRuntimeCallingConvention calling_convention;
3893 locations->SetInAt(0, Location::RegisterPairLocation(
3894 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3895 locations->SetInAt(1, Location::RegisterPairLocation(
3896 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3897 locations->SetOut(calling_convention.GetReturnLocation(type));
3898 break;
3899 }
3900
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003901 case DataType::Type::kFloat32:
3902 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003903 locations->SetInAt(0, Location::RequiresFpuRegister());
3904 locations->SetInAt(1, Location::RequiresFpuRegister());
3905 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3906 break;
3907
3908 default:
3909 LOG(FATAL) << "Unexpected div type " << type;
3910 }
3911}
3912
3913void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003914 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003915 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003916
3917 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003918 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003919 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003920 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003921 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003922 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003923 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3924 break;
3925 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003926 case DataType::Type::kFloat32:
3927 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003928 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3929 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3930 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003931 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003932 __ DivS(dst, lhs, rhs);
3933 } else {
3934 __ DivD(dst, lhs, rhs);
3935 }
3936 break;
3937 }
3938 default:
3939 LOG(FATAL) << "Unexpected div type " << type;
3940 }
3941}
3942
3943void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003944 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003945 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003946}
3947
3948void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003949 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003950 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003951 codegen_->AddSlowPath(slow_path);
3952 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003953 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003954
3955 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003956 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003957 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003958 case DataType::Type::kInt8:
3959 case DataType::Type::kUint16:
3960 case DataType::Type::kInt16:
3961 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003962 if (value.IsConstant()) {
3963 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3964 __ B(slow_path->GetEntryLabel());
3965 } else {
3966 // A division by a non-null constant is valid. We don't need to perform
3967 // any check, so simply fall through.
3968 }
3969 } else {
3970 DCHECK(value.IsRegister()) << value;
3971 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3972 }
3973 break;
3974 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003975 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003976 if (value.IsConstant()) {
3977 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3978 __ B(slow_path->GetEntryLabel());
3979 } else {
3980 // A division by a non-null constant is valid. We don't need to perform
3981 // any check, so simply fall through.
3982 }
3983 } else {
3984 DCHECK(value.IsRegisterPair()) << value;
3985 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3986 __ Beqz(TMP, slow_path->GetEntryLabel());
3987 }
3988 break;
3989 }
3990 default:
3991 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3992 }
3993}
3994
3995void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3996 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003997 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003998 locations->SetOut(Location::ConstantLocation(constant));
3999}
4000
4001void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4002 // Will be generated at use site.
4003}
4004
4005void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4006 exit->SetLocations(nullptr);
4007}
4008
4009void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4010}
4011
4012void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4013 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004014 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004015 locations->SetOut(Location::ConstantLocation(constant));
4016}
4017
4018void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4019 // Will be generated at use site.
4020}
4021
4022void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4023 got->SetLocations(nullptr);
4024}
4025
4026void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
4027 DCHECK(!successor->IsExitBlock());
4028 HBasicBlock* block = got->GetBlock();
4029 HInstruction* previous = got->GetPrevious();
4030 HLoopInformation* info = block->GetLoopInformation();
4031
4032 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004033 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4034 return;
4035 }
4036 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4037 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4038 }
4039 if (!codegen_->GoesToNextBlock(block, successor)) {
4040 __ B(codegen_->GetLabelOf(successor));
4041 }
4042}
4043
4044void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4045 HandleGoto(got, got->GetSuccessor());
4046}
4047
4048void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4049 try_boundary->SetLocations(nullptr);
4050}
4051
4052void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4053 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4054 if (!successor->IsExitBlock()) {
4055 HandleGoto(try_boundary, successor);
4056 }
4057}
4058
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004059void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4060 LocationSummary* locations) {
4061 Register dst = locations->Out().AsRegister<Register>();
4062 Register lhs = locations->InAt(0).AsRegister<Register>();
4063 Location rhs_location = locations->InAt(1);
4064 Register rhs_reg = ZERO;
4065 int64_t rhs_imm = 0;
4066 bool use_imm = rhs_location.IsConstant();
4067 if (use_imm) {
4068 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4069 } else {
4070 rhs_reg = rhs_location.AsRegister<Register>();
4071 }
4072
4073 switch (cond) {
4074 case kCondEQ:
4075 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004076 if (use_imm && IsInt<16>(-rhs_imm)) {
4077 if (rhs_imm == 0) {
4078 if (cond == kCondEQ) {
4079 __ Sltiu(dst, lhs, 1);
4080 } else {
4081 __ Sltu(dst, ZERO, lhs);
4082 }
4083 } else {
4084 __ Addiu(dst, lhs, -rhs_imm);
4085 if (cond == kCondEQ) {
4086 __ Sltiu(dst, dst, 1);
4087 } else {
4088 __ Sltu(dst, ZERO, dst);
4089 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004090 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004091 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004092 if (use_imm && IsUint<16>(rhs_imm)) {
4093 __ Xori(dst, lhs, rhs_imm);
4094 } else {
4095 if (use_imm) {
4096 rhs_reg = TMP;
4097 __ LoadConst32(rhs_reg, rhs_imm);
4098 }
4099 __ Xor(dst, lhs, rhs_reg);
4100 }
4101 if (cond == kCondEQ) {
4102 __ Sltiu(dst, dst, 1);
4103 } else {
4104 __ Sltu(dst, ZERO, dst);
4105 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004106 }
4107 break;
4108
4109 case kCondLT:
4110 case kCondGE:
4111 if (use_imm && IsInt<16>(rhs_imm)) {
4112 __ Slti(dst, lhs, rhs_imm);
4113 } else {
4114 if (use_imm) {
4115 rhs_reg = TMP;
4116 __ LoadConst32(rhs_reg, rhs_imm);
4117 }
4118 __ Slt(dst, lhs, rhs_reg);
4119 }
4120 if (cond == kCondGE) {
4121 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4122 // only the slt instruction but no sge.
4123 __ Xori(dst, dst, 1);
4124 }
4125 break;
4126
4127 case kCondLE:
4128 case kCondGT:
4129 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4130 // Simulate lhs <= rhs via lhs < rhs + 1.
4131 __ Slti(dst, lhs, rhs_imm + 1);
4132 if (cond == kCondGT) {
4133 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4134 // only the slti instruction but no sgti.
4135 __ Xori(dst, dst, 1);
4136 }
4137 } else {
4138 if (use_imm) {
4139 rhs_reg = TMP;
4140 __ LoadConst32(rhs_reg, rhs_imm);
4141 }
4142 __ Slt(dst, rhs_reg, lhs);
4143 if (cond == kCondLE) {
4144 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4145 // only the slt instruction but no sle.
4146 __ Xori(dst, dst, 1);
4147 }
4148 }
4149 break;
4150
4151 case kCondB:
4152 case kCondAE:
4153 if (use_imm && IsInt<16>(rhs_imm)) {
4154 // Sltiu sign-extends its 16-bit immediate operand before
4155 // the comparison and thus lets us compare directly with
4156 // unsigned values in the ranges [0, 0x7fff] and
4157 // [0xffff8000, 0xffffffff].
4158 __ Sltiu(dst, lhs, rhs_imm);
4159 } else {
4160 if (use_imm) {
4161 rhs_reg = TMP;
4162 __ LoadConst32(rhs_reg, rhs_imm);
4163 }
4164 __ Sltu(dst, lhs, rhs_reg);
4165 }
4166 if (cond == kCondAE) {
4167 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4168 // only the sltu instruction but no sgeu.
4169 __ Xori(dst, dst, 1);
4170 }
4171 break;
4172
4173 case kCondBE:
4174 case kCondA:
4175 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4176 // Simulate lhs <= rhs via lhs < rhs + 1.
4177 // Note that this only works if rhs + 1 does not overflow
4178 // to 0, hence the check above.
4179 // Sltiu sign-extends its 16-bit immediate operand before
4180 // the comparison and thus lets us compare directly with
4181 // unsigned values in the ranges [0, 0x7fff] and
4182 // [0xffff8000, 0xffffffff].
4183 __ Sltiu(dst, lhs, rhs_imm + 1);
4184 if (cond == kCondA) {
4185 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4186 // only the sltiu instruction but no sgtiu.
4187 __ Xori(dst, dst, 1);
4188 }
4189 } else {
4190 if (use_imm) {
4191 rhs_reg = TMP;
4192 __ LoadConst32(rhs_reg, rhs_imm);
4193 }
4194 __ Sltu(dst, rhs_reg, lhs);
4195 if (cond == kCondBE) {
4196 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4197 // only the sltu instruction but no sleu.
4198 __ Xori(dst, dst, 1);
4199 }
4200 }
4201 break;
4202 }
4203}
4204
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004205bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4206 LocationSummary* input_locations,
4207 Register dst) {
4208 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4209 Location rhs_location = input_locations->InAt(1);
4210 Register rhs_reg = ZERO;
4211 int64_t rhs_imm = 0;
4212 bool use_imm = rhs_location.IsConstant();
4213 if (use_imm) {
4214 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4215 } else {
4216 rhs_reg = rhs_location.AsRegister<Register>();
4217 }
4218
4219 switch (cond) {
4220 case kCondEQ:
4221 case kCondNE:
4222 if (use_imm && IsInt<16>(-rhs_imm)) {
4223 __ Addiu(dst, lhs, -rhs_imm);
4224 } else if (use_imm && IsUint<16>(rhs_imm)) {
4225 __ Xori(dst, lhs, rhs_imm);
4226 } else {
4227 if (use_imm) {
4228 rhs_reg = TMP;
4229 __ LoadConst32(rhs_reg, rhs_imm);
4230 }
4231 __ Xor(dst, lhs, rhs_reg);
4232 }
4233 return (cond == kCondEQ);
4234
4235 case kCondLT:
4236 case kCondGE:
4237 if (use_imm && IsInt<16>(rhs_imm)) {
4238 __ Slti(dst, lhs, rhs_imm);
4239 } else {
4240 if (use_imm) {
4241 rhs_reg = TMP;
4242 __ LoadConst32(rhs_reg, rhs_imm);
4243 }
4244 __ Slt(dst, lhs, rhs_reg);
4245 }
4246 return (cond == kCondGE);
4247
4248 case kCondLE:
4249 case kCondGT:
4250 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4251 // Simulate lhs <= rhs via lhs < rhs + 1.
4252 __ Slti(dst, lhs, rhs_imm + 1);
4253 return (cond == kCondGT);
4254 } else {
4255 if (use_imm) {
4256 rhs_reg = TMP;
4257 __ LoadConst32(rhs_reg, rhs_imm);
4258 }
4259 __ Slt(dst, rhs_reg, lhs);
4260 return (cond == kCondLE);
4261 }
4262
4263 case kCondB:
4264 case kCondAE:
4265 if (use_imm && IsInt<16>(rhs_imm)) {
4266 // Sltiu sign-extends its 16-bit immediate operand before
4267 // the comparison and thus lets us compare directly with
4268 // unsigned values in the ranges [0, 0x7fff] and
4269 // [0xffff8000, 0xffffffff].
4270 __ Sltiu(dst, lhs, rhs_imm);
4271 } else {
4272 if (use_imm) {
4273 rhs_reg = TMP;
4274 __ LoadConst32(rhs_reg, rhs_imm);
4275 }
4276 __ Sltu(dst, lhs, rhs_reg);
4277 }
4278 return (cond == kCondAE);
4279
4280 case kCondBE:
4281 case kCondA:
4282 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4283 // Simulate lhs <= rhs via lhs < rhs + 1.
4284 // Note that this only works if rhs + 1 does not overflow
4285 // to 0, hence the check above.
4286 // Sltiu sign-extends its 16-bit immediate operand before
4287 // the comparison and thus lets us compare directly with
4288 // unsigned values in the ranges [0, 0x7fff] and
4289 // [0xffff8000, 0xffffffff].
4290 __ Sltiu(dst, lhs, rhs_imm + 1);
4291 return (cond == kCondA);
4292 } else {
4293 if (use_imm) {
4294 rhs_reg = TMP;
4295 __ LoadConst32(rhs_reg, rhs_imm);
4296 }
4297 __ Sltu(dst, rhs_reg, lhs);
4298 return (cond == kCondBE);
4299 }
4300 }
4301}
4302
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004303void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4304 LocationSummary* locations,
4305 MipsLabel* label) {
4306 Register lhs = locations->InAt(0).AsRegister<Register>();
4307 Location rhs_location = locations->InAt(1);
4308 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004309 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004310 bool use_imm = rhs_location.IsConstant();
4311 if (use_imm) {
4312 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4313 } else {
4314 rhs_reg = rhs_location.AsRegister<Register>();
4315 }
4316
4317 if (use_imm && rhs_imm == 0) {
4318 switch (cond) {
4319 case kCondEQ:
4320 case kCondBE: // <= 0 if zero
4321 __ Beqz(lhs, label);
4322 break;
4323 case kCondNE:
4324 case kCondA: // > 0 if non-zero
4325 __ Bnez(lhs, label);
4326 break;
4327 case kCondLT:
4328 __ Bltz(lhs, label);
4329 break;
4330 case kCondGE:
4331 __ Bgez(lhs, label);
4332 break;
4333 case kCondLE:
4334 __ Blez(lhs, label);
4335 break;
4336 case kCondGT:
4337 __ Bgtz(lhs, label);
4338 break;
4339 case kCondB: // always false
4340 break;
4341 case kCondAE: // always true
4342 __ B(label);
4343 break;
4344 }
4345 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004346 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4347 if (isR6 || !use_imm) {
4348 if (use_imm) {
4349 rhs_reg = TMP;
4350 __ LoadConst32(rhs_reg, rhs_imm);
4351 }
4352 switch (cond) {
4353 case kCondEQ:
4354 __ Beq(lhs, rhs_reg, label);
4355 break;
4356 case kCondNE:
4357 __ Bne(lhs, rhs_reg, label);
4358 break;
4359 case kCondLT:
4360 __ Blt(lhs, rhs_reg, label);
4361 break;
4362 case kCondGE:
4363 __ Bge(lhs, rhs_reg, label);
4364 break;
4365 case kCondLE:
4366 __ Bge(rhs_reg, lhs, label);
4367 break;
4368 case kCondGT:
4369 __ Blt(rhs_reg, lhs, label);
4370 break;
4371 case kCondB:
4372 __ Bltu(lhs, rhs_reg, label);
4373 break;
4374 case kCondAE:
4375 __ Bgeu(lhs, rhs_reg, label);
4376 break;
4377 case kCondBE:
4378 __ Bgeu(rhs_reg, lhs, label);
4379 break;
4380 case kCondA:
4381 __ Bltu(rhs_reg, lhs, label);
4382 break;
4383 }
4384 } else {
4385 // Special cases for more efficient comparison with constants on R2.
4386 switch (cond) {
4387 case kCondEQ:
4388 __ LoadConst32(TMP, rhs_imm);
4389 __ Beq(lhs, TMP, label);
4390 break;
4391 case kCondNE:
4392 __ LoadConst32(TMP, rhs_imm);
4393 __ Bne(lhs, TMP, label);
4394 break;
4395 case kCondLT:
4396 if (IsInt<16>(rhs_imm)) {
4397 __ Slti(TMP, lhs, rhs_imm);
4398 __ Bnez(TMP, label);
4399 } else {
4400 __ LoadConst32(TMP, rhs_imm);
4401 __ Blt(lhs, TMP, label);
4402 }
4403 break;
4404 case kCondGE:
4405 if (IsInt<16>(rhs_imm)) {
4406 __ Slti(TMP, lhs, rhs_imm);
4407 __ Beqz(TMP, label);
4408 } else {
4409 __ LoadConst32(TMP, rhs_imm);
4410 __ Bge(lhs, TMP, label);
4411 }
4412 break;
4413 case kCondLE:
4414 if (IsInt<16>(rhs_imm + 1)) {
4415 // Simulate lhs <= rhs via lhs < rhs + 1.
4416 __ Slti(TMP, lhs, rhs_imm + 1);
4417 __ Bnez(TMP, label);
4418 } else {
4419 __ LoadConst32(TMP, rhs_imm);
4420 __ Bge(TMP, lhs, label);
4421 }
4422 break;
4423 case kCondGT:
4424 if (IsInt<16>(rhs_imm + 1)) {
4425 // Simulate lhs > rhs via !(lhs < rhs + 1).
4426 __ Slti(TMP, lhs, rhs_imm + 1);
4427 __ Beqz(TMP, label);
4428 } else {
4429 __ LoadConst32(TMP, rhs_imm);
4430 __ Blt(TMP, lhs, label);
4431 }
4432 break;
4433 case kCondB:
4434 if (IsInt<16>(rhs_imm)) {
4435 __ Sltiu(TMP, lhs, rhs_imm);
4436 __ Bnez(TMP, label);
4437 } else {
4438 __ LoadConst32(TMP, rhs_imm);
4439 __ Bltu(lhs, TMP, label);
4440 }
4441 break;
4442 case kCondAE:
4443 if (IsInt<16>(rhs_imm)) {
4444 __ Sltiu(TMP, lhs, rhs_imm);
4445 __ Beqz(TMP, label);
4446 } else {
4447 __ LoadConst32(TMP, rhs_imm);
4448 __ Bgeu(lhs, TMP, label);
4449 }
4450 break;
4451 case kCondBE:
4452 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4453 // Simulate lhs <= rhs via lhs < rhs + 1.
4454 // Note that this only works if rhs + 1 does not overflow
4455 // to 0, hence the check above.
4456 __ Sltiu(TMP, lhs, rhs_imm + 1);
4457 __ Bnez(TMP, label);
4458 } else {
4459 __ LoadConst32(TMP, rhs_imm);
4460 __ Bgeu(TMP, lhs, label);
4461 }
4462 break;
4463 case kCondA:
4464 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4465 // Simulate lhs > rhs via !(lhs < rhs + 1).
4466 // Note that this only works if rhs + 1 does not overflow
4467 // to 0, hence the check above.
4468 __ Sltiu(TMP, lhs, rhs_imm + 1);
4469 __ Beqz(TMP, label);
4470 } else {
4471 __ LoadConst32(TMP, rhs_imm);
4472 __ Bltu(TMP, lhs, label);
4473 }
4474 break;
4475 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004476 }
4477 }
4478}
4479
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004480void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4481 LocationSummary* locations) {
4482 Register dst = locations->Out().AsRegister<Register>();
4483 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4484 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4485 Location rhs_location = locations->InAt(1);
4486 Register rhs_high = ZERO;
4487 Register rhs_low = ZERO;
4488 int64_t imm = 0;
4489 uint32_t imm_high = 0;
4490 uint32_t imm_low = 0;
4491 bool use_imm = rhs_location.IsConstant();
4492 if (use_imm) {
4493 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4494 imm_high = High32Bits(imm);
4495 imm_low = Low32Bits(imm);
4496 } else {
4497 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4498 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4499 }
4500 if (use_imm && imm == 0) {
4501 switch (cond) {
4502 case kCondEQ:
4503 case kCondBE: // <= 0 if zero
4504 __ Or(dst, lhs_high, lhs_low);
4505 __ Sltiu(dst, dst, 1);
4506 break;
4507 case kCondNE:
4508 case kCondA: // > 0 if non-zero
4509 __ Or(dst, lhs_high, lhs_low);
4510 __ Sltu(dst, ZERO, dst);
4511 break;
4512 case kCondLT:
4513 __ Slt(dst, lhs_high, ZERO);
4514 break;
4515 case kCondGE:
4516 __ Slt(dst, lhs_high, ZERO);
4517 __ Xori(dst, dst, 1);
4518 break;
4519 case kCondLE:
4520 __ Or(TMP, lhs_high, lhs_low);
4521 __ Sra(AT, lhs_high, 31);
4522 __ Sltu(dst, AT, TMP);
4523 __ Xori(dst, dst, 1);
4524 break;
4525 case kCondGT:
4526 __ Or(TMP, lhs_high, lhs_low);
4527 __ Sra(AT, lhs_high, 31);
4528 __ Sltu(dst, AT, TMP);
4529 break;
4530 case kCondB: // always false
4531 __ Andi(dst, dst, 0);
4532 break;
4533 case kCondAE: // always true
4534 __ Ori(dst, ZERO, 1);
4535 break;
4536 }
4537 } else if (use_imm) {
4538 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4539 switch (cond) {
4540 case kCondEQ:
4541 __ LoadConst32(TMP, imm_high);
4542 __ Xor(TMP, TMP, lhs_high);
4543 __ LoadConst32(AT, imm_low);
4544 __ Xor(AT, AT, lhs_low);
4545 __ Or(dst, TMP, AT);
4546 __ Sltiu(dst, dst, 1);
4547 break;
4548 case kCondNE:
4549 __ LoadConst32(TMP, imm_high);
4550 __ Xor(TMP, TMP, lhs_high);
4551 __ LoadConst32(AT, imm_low);
4552 __ Xor(AT, AT, lhs_low);
4553 __ Or(dst, TMP, AT);
4554 __ Sltu(dst, ZERO, dst);
4555 break;
4556 case kCondLT:
4557 case kCondGE:
4558 if (dst == lhs_low) {
4559 __ LoadConst32(TMP, imm_low);
4560 __ Sltu(dst, lhs_low, TMP);
4561 }
4562 __ LoadConst32(TMP, imm_high);
4563 __ Slt(AT, lhs_high, TMP);
4564 __ Slt(TMP, TMP, lhs_high);
4565 if (dst != lhs_low) {
4566 __ LoadConst32(dst, imm_low);
4567 __ Sltu(dst, lhs_low, dst);
4568 }
4569 __ Slt(dst, TMP, dst);
4570 __ Or(dst, dst, AT);
4571 if (cond == kCondGE) {
4572 __ Xori(dst, dst, 1);
4573 }
4574 break;
4575 case kCondGT:
4576 case kCondLE:
4577 if (dst == lhs_low) {
4578 __ LoadConst32(TMP, imm_low);
4579 __ Sltu(dst, TMP, lhs_low);
4580 }
4581 __ LoadConst32(TMP, imm_high);
4582 __ Slt(AT, TMP, lhs_high);
4583 __ Slt(TMP, lhs_high, TMP);
4584 if (dst != lhs_low) {
4585 __ LoadConst32(dst, imm_low);
4586 __ Sltu(dst, dst, lhs_low);
4587 }
4588 __ Slt(dst, TMP, dst);
4589 __ Or(dst, dst, AT);
4590 if (cond == kCondLE) {
4591 __ Xori(dst, dst, 1);
4592 }
4593 break;
4594 case kCondB:
4595 case kCondAE:
4596 if (dst == lhs_low) {
4597 __ LoadConst32(TMP, imm_low);
4598 __ Sltu(dst, lhs_low, TMP);
4599 }
4600 __ LoadConst32(TMP, imm_high);
4601 __ Sltu(AT, lhs_high, TMP);
4602 __ Sltu(TMP, TMP, lhs_high);
4603 if (dst != lhs_low) {
4604 __ LoadConst32(dst, imm_low);
4605 __ Sltu(dst, lhs_low, dst);
4606 }
4607 __ Slt(dst, TMP, dst);
4608 __ Or(dst, dst, AT);
4609 if (cond == kCondAE) {
4610 __ Xori(dst, dst, 1);
4611 }
4612 break;
4613 case kCondA:
4614 case kCondBE:
4615 if (dst == lhs_low) {
4616 __ LoadConst32(TMP, imm_low);
4617 __ Sltu(dst, TMP, lhs_low);
4618 }
4619 __ LoadConst32(TMP, imm_high);
4620 __ Sltu(AT, TMP, lhs_high);
4621 __ Sltu(TMP, lhs_high, TMP);
4622 if (dst != lhs_low) {
4623 __ LoadConst32(dst, imm_low);
4624 __ Sltu(dst, dst, lhs_low);
4625 }
4626 __ Slt(dst, TMP, dst);
4627 __ Or(dst, dst, AT);
4628 if (cond == kCondBE) {
4629 __ Xori(dst, dst, 1);
4630 }
4631 break;
4632 }
4633 } else {
4634 switch (cond) {
4635 case kCondEQ:
4636 __ Xor(TMP, lhs_high, rhs_high);
4637 __ Xor(AT, lhs_low, rhs_low);
4638 __ Or(dst, TMP, AT);
4639 __ Sltiu(dst, dst, 1);
4640 break;
4641 case kCondNE:
4642 __ Xor(TMP, lhs_high, rhs_high);
4643 __ Xor(AT, lhs_low, rhs_low);
4644 __ Or(dst, TMP, AT);
4645 __ Sltu(dst, ZERO, dst);
4646 break;
4647 case kCondLT:
4648 case kCondGE:
4649 __ Slt(TMP, rhs_high, lhs_high);
4650 __ Sltu(AT, lhs_low, rhs_low);
4651 __ Slt(TMP, TMP, AT);
4652 __ Slt(AT, lhs_high, rhs_high);
4653 __ Or(dst, AT, TMP);
4654 if (cond == kCondGE) {
4655 __ Xori(dst, dst, 1);
4656 }
4657 break;
4658 case kCondGT:
4659 case kCondLE:
4660 __ Slt(TMP, lhs_high, rhs_high);
4661 __ Sltu(AT, rhs_low, lhs_low);
4662 __ Slt(TMP, TMP, AT);
4663 __ Slt(AT, rhs_high, lhs_high);
4664 __ Or(dst, AT, TMP);
4665 if (cond == kCondLE) {
4666 __ Xori(dst, dst, 1);
4667 }
4668 break;
4669 case kCondB:
4670 case kCondAE:
4671 __ Sltu(TMP, rhs_high, lhs_high);
4672 __ Sltu(AT, lhs_low, rhs_low);
4673 __ Slt(TMP, TMP, AT);
4674 __ Sltu(AT, lhs_high, rhs_high);
4675 __ Or(dst, AT, TMP);
4676 if (cond == kCondAE) {
4677 __ Xori(dst, dst, 1);
4678 }
4679 break;
4680 case kCondA:
4681 case kCondBE:
4682 __ Sltu(TMP, lhs_high, rhs_high);
4683 __ Sltu(AT, rhs_low, lhs_low);
4684 __ Slt(TMP, TMP, AT);
4685 __ Sltu(AT, rhs_high, lhs_high);
4686 __ Or(dst, AT, TMP);
4687 if (cond == kCondBE) {
4688 __ Xori(dst, dst, 1);
4689 }
4690 break;
4691 }
4692 }
4693}
4694
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004695void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4696 LocationSummary* locations,
4697 MipsLabel* label) {
4698 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4699 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4700 Location rhs_location = locations->InAt(1);
4701 Register rhs_high = ZERO;
4702 Register rhs_low = ZERO;
4703 int64_t imm = 0;
4704 uint32_t imm_high = 0;
4705 uint32_t imm_low = 0;
4706 bool use_imm = rhs_location.IsConstant();
4707 if (use_imm) {
4708 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4709 imm_high = High32Bits(imm);
4710 imm_low = Low32Bits(imm);
4711 } else {
4712 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4713 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4714 }
4715
4716 if (use_imm && imm == 0) {
4717 switch (cond) {
4718 case kCondEQ:
4719 case kCondBE: // <= 0 if zero
4720 __ Or(TMP, lhs_high, lhs_low);
4721 __ Beqz(TMP, label);
4722 break;
4723 case kCondNE:
4724 case kCondA: // > 0 if non-zero
4725 __ Or(TMP, lhs_high, lhs_low);
4726 __ Bnez(TMP, label);
4727 break;
4728 case kCondLT:
4729 __ Bltz(lhs_high, label);
4730 break;
4731 case kCondGE:
4732 __ Bgez(lhs_high, label);
4733 break;
4734 case kCondLE:
4735 __ Or(TMP, lhs_high, lhs_low);
4736 __ Sra(AT, lhs_high, 31);
4737 __ Bgeu(AT, TMP, label);
4738 break;
4739 case kCondGT:
4740 __ Or(TMP, lhs_high, lhs_low);
4741 __ Sra(AT, lhs_high, 31);
4742 __ Bltu(AT, TMP, label);
4743 break;
4744 case kCondB: // always false
4745 break;
4746 case kCondAE: // always true
4747 __ B(label);
4748 break;
4749 }
4750 } else if (use_imm) {
4751 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4752 switch (cond) {
4753 case kCondEQ:
4754 __ LoadConst32(TMP, imm_high);
4755 __ Xor(TMP, TMP, lhs_high);
4756 __ LoadConst32(AT, imm_low);
4757 __ Xor(AT, AT, lhs_low);
4758 __ Or(TMP, TMP, AT);
4759 __ Beqz(TMP, label);
4760 break;
4761 case kCondNE:
4762 __ LoadConst32(TMP, imm_high);
4763 __ Xor(TMP, TMP, lhs_high);
4764 __ LoadConst32(AT, imm_low);
4765 __ Xor(AT, AT, lhs_low);
4766 __ Or(TMP, TMP, AT);
4767 __ Bnez(TMP, label);
4768 break;
4769 case kCondLT:
4770 __ LoadConst32(TMP, imm_high);
4771 __ Blt(lhs_high, TMP, label);
4772 __ Slt(TMP, TMP, lhs_high);
4773 __ LoadConst32(AT, imm_low);
4774 __ Sltu(AT, lhs_low, AT);
4775 __ Blt(TMP, AT, label);
4776 break;
4777 case kCondGE:
4778 __ LoadConst32(TMP, imm_high);
4779 __ Blt(TMP, lhs_high, label);
4780 __ Slt(TMP, lhs_high, TMP);
4781 __ LoadConst32(AT, imm_low);
4782 __ Sltu(AT, lhs_low, AT);
4783 __ Or(TMP, TMP, AT);
4784 __ Beqz(TMP, label);
4785 break;
4786 case kCondLE:
4787 __ LoadConst32(TMP, imm_high);
4788 __ Blt(lhs_high, TMP, label);
4789 __ Slt(TMP, TMP, lhs_high);
4790 __ LoadConst32(AT, imm_low);
4791 __ Sltu(AT, AT, lhs_low);
4792 __ Or(TMP, TMP, AT);
4793 __ Beqz(TMP, label);
4794 break;
4795 case kCondGT:
4796 __ LoadConst32(TMP, imm_high);
4797 __ Blt(TMP, lhs_high, label);
4798 __ Slt(TMP, lhs_high, TMP);
4799 __ LoadConst32(AT, imm_low);
4800 __ Sltu(AT, AT, lhs_low);
4801 __ Blt(TMP, AT, label);
4802 break;
4803 case kCondB:
4804 __ LoadConst32(TMP, imm_high);
4805 __ Bltu(lhs_high, TMP, label);
4806 __ Sltu(TMP, TMP, lhs_high);
4807 __ LoadConst32(AT, imm_low);
4808 __ Sltu(AT, lhs_low, AT);
4809 __ Blt(TMP, AT, label);
4810 break;
4811 case kCondAE:
4812 __ LoadConst32(TMP, imm_high);
4813 __ Bltu(TMP, lhs_high, label);
4814 __ Sltu(TMP, lhs_high, TMP);
4815 __ LoadConst32(AT, imm_low);
4816 __ Sltu(AT, lhs_low, AT);
4817 __ Or(TMP, TMP, AT);
4818 __ Beqz(TMP, label);
4819 break;
4820 case kCondBE:
4821 __ LoadConst32(TMP, imm_high);
4822 __ Bltu(lhs_high, TMP, label);
4823 __ Sltu(TMP, TMP, lhs_high);
4824 __ LoadConst32(AT, imm_low);
4825 __ Sltu(AT, AT, lhs_low);
4826 __ Or(TMP, TMP, AT);
4827 __ Beqz(TMP, label);
4828 break;
4829 case kCondA:
4830 __ LoadConst32(TMP, imm_high);
4831 __ Bltu(TMP, lhs_high, label);
4832 __ Sltu(TMP, lhs_high, TMP);
4833 __ LoadConst32(AT, imm_low);
4834 __ Sltu(AT, AT, lhs_low);
4835 __ Blt(TMP, AT, label);
4836 break;
4837 }
4838 } else {
4839 switch (cond) {
4840 case kCondEQ:
4841 __ Xor(TMP, lhs_high, rhs_high);
4842 __ Xor(AT, lhs_low, rhs_low);
4843 __ Or(TMP, TMP, AT);
4844 __ Beqz(TMP, label);
4845 break;
4846 case kCondNE:
4847 __ Xor(TMP, lhs_high, rhs_high);
4848 __ Xor(AT, lhs_low, rhs_low);
4849 __ Or(TMP, TMP, AT);
4850 __ Bnez(TMP, label);
4851 break;
4852 case kCondLT:
4853 __ Blt(lhs_high, rhs_high, label);
4854 __ Slt(TMP, rhs_high, lhs_high);
4855 __ Sltu(AT, lhs_low, rhs_low);
4856 __ Blt(TMP, AT, label);
4857 break;
4858 case kCondGE:
4859 __ Blt(rhs_high, lhs_high, label);
4860 __ Slt(TMP, lhs_high, rhs_high);
4861 __ Sltu(AT, lhs_low, rhs_low);
4862 __ Or(TMP, TMP, AT);
4863 __ Beqz(TMP, label);
4864 break;
4865 case kCondLE:
4866 __ Blt(lhs_high, rhs_high, label);
4867 __ Slt(TMP, rhs_high, lhs_high);
4868 __ Sltu(AT, rhs_low, lhs_low);
4869 __ Or(TMP, TMP, AT);
4870 __ Beqz(TMP, label);
4871 break;
4872 case kCondGT:
4873 __ Blt(rhs_high, lhs_high, label);
4874 __ Slt(TMP, lhs_high, rhs_high);
4875 __ Sltu(AT, rhs_low, lhs_low);
4876 __ Blt(TMP, AT, label);
4877 break;
4878 case kCondB:
4879 __ Bltu(lhs_high, rhs_high, label);
4880 __ Sltu(TMP, rhs_high, lhs_high);
4881 __ Sltu(AT, lhs_low, rhs_low);
4882 __ Blt(TMP, AT, label);
4883 break;
4884 case kCondAE:
4885 __ Bltu(rhs_high, lhs_high, label);
4886 __ Sltu(TMP, lhs_high, rhs_high);
4887 __ Sltu(AT, lhs_low, rhs_low);
4888 __ Or(TMP, TMP, AT);
4889 __ Beqz(TMP, label);
4890 break;
4891 case kCondBE:
4892 __ Bltu(lhs_high, rhs_high, label);
4893 __ Sltu(TMP, rhs_high, lhs_high);
4894 __ Sltu(AT, rhs_low, lhs_low);
4895 __ Or(TMP, TMP, AT);
4896 __ Beqz(TMP, label);
4897 break;
4898 case kCondA:
4899 __ Bltu(rhs_high, lhs_high, label);
4900 __ Sltu(TMP, lhs_high, rhs_high);
4901 __ Sltu(AT, rhs_low, lhs_low);
4902 __ Blt(TMP, AT, label);
4903 break;
4904 }
4905 }
4906}
4907
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004908void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4909 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004910 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004911 LocationSummary* locations) {
4912 Register dst = locations->Out().AsRegister<Register>();
4913 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4914 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4915 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004916 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004917 if (isR6) {
4918 switch (cond) {
4919 case kCondEQ:
4920 __ CmpEqS(FTMP, lhs, rhs);
4921 __ Mfc1(dst, FTMP);
4922 __ Andi(dst, dst, 1);
4923 break;
4924 case kCondNE:
4925 __ CmpEqS(FTMP, lhs, rhs);
4926 __ Mfc1(dst, FTMP);
4927 __ Addiu(dst, dst, 1);
4928 break;
4929 case kCondLT:
4930 if (gt_bias) {
4931 __ CmpLtS(FTMP, lhs, rhs);
4932 } else {
4933 __ CmpUltS(FTMP, lhs, rhs);
4934 }
4935 __ Mfc1(dst, FTMP);
4936 __ Andi(dst, dst, 1);
4937 break;
4938 case kCondLE:
4939 if (gt_bias) {
4940 __ CmpLeS(FTMP, lhs, rhs);
4941 } else {
4942 __ CmpUleS(FTMP, lhs, rhs);
4943 }
4944 __ Mfc1(dst, FTMP);
4945 __ Andi(dst, dst, 1);
4946 break;
4947 case kCondGT:
4948 if (gt_bias) {
4949 __ CmpUltS(FTMP, rhs, lhs);
4950 } else {
4951 __ CmpLtS(FTMP, rhs, lhs);
4952 }
4953 __ Mfc1(dst, FTMP);
4954 __ Andi(dst, dst, 1);
4955 break;
4956 case kCondGE:
4957 if (gt_bias) {
4958 __ CmpUleS(FTMP, rhs, lhs);
4959 } else {
4960 __ CmpLeS(FTMP, rhs, lhs);
4961 }
4962 __ Mfc1(dst, FTMP);
4963 __ Andi(dst, dst, 1);
4964 break;
4965 default:
4966 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4967 UNREACHABLE();
4968 }
4969 } else {
4970 switch (cond) {
4971 case kCondEQ:
4972 __ CeqS(0, lhs, rhs);
4973 __ LoadConst32(dst, 1);
4974 __ Movf(dst, ZERO, 0);
4975 break;
4976 case kCondNE:
4977 __ CeqS(0, lhs, rhs);
4978 __ LoadConst32(dst, 1);
4979 __ Movt(dst, ZERO, 0);
4980 break;
4981 case kCondLT:
4982 if (gt_bias) {
4983 __ ColtS(0, lhs, rhs);
4984 } else {
4985 __ CultS(0, lhs, rhs);
4986 }
4987 __ LoadConst32(dst, 1);
4988 __ Movf(dst, ZERO, 0);
4989 break;
4990 case kCondLE:
4991 if (gt_bias) {
4992 __ ColeS(0, lhs, rhs);
4993 } else {
4994 __ CuleS(0, lhs, rhs);
4995 }
4996 __ LoadConst32(dst, 1);
4997 __ Movf(dst, ZERO, 0);
4998 break;
4999 case kCondGT:
5000 if (gt_bias) {
5001 __ CultS(0, rhs, lhs);
5002 } else {
5003 __ ColtS(0, rhs, lhs);
5004 }
5005 __ LoadConst32(dst, 1);
5006 __ Movf(dst, ZERO, 0);
5007 break;
5008 case kCondGE:
5009 if (gt_bias) {
5010 __ CuleS(0, rhs, lhs);
5011 } else {
5012 __ ColeS(0, rhs, lhs);
5013 }
5014 __ LoadConst32(dst, 1);
5015 __ Movf(dst, ZERO, 0);
5016 break;
5017 default:
5018 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5019 UNREACHABLE();
5020 }
5021 }
5022 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005023 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005024 if (isR6) {
5025 switch (cond) {
5026 case kCondEQ:
5027 __ CmpEqD(FTMP, lhs, rhs);
5028 __ Mfc1(dst, FTMP);
5029 __ Andi(dst, dst, 1);
5030 break;
5031 case kCondNE:
5032 __ CmpEqD(FTMP, lhs, rhs);
5033 __ Mfc1(dst, FTMP);
5034 __ Addiu(dst, dst, 1);
5035 break;
5036 case kCondLT:
5037 if (gt_bias) {
5038 __ CmpLtD(FTMP, lhs, rhs);
5039 } else {
5040 __ CmpUltD(FTMP, lhs, rhs);
5041 }
5042 __ Mfc1(dst, FTMP);
5043 __ Andi(dst, dst, 1);
5044 break;
5045 case kCondLE:
5046 if (gt_bias) {
5047 __ CmpLeD(FTMP, lhs, rhs);
5048 } else {
5049 __ CmpUleD(FTMP, lhs, rhs);
5050 }
5051 __ Mfc1(dst, FTMP);
5052 __ Andi(dst, dst, 1);
5053 break;
5054 case kCondGT:
5055 if (gt_bias) {
5056 __ CmpUltD(FTMP, rhs, lhs);
5057 } else {
5058 __ CmpLtD(FTMP, rhs, lhs);
5059 }
5060 __ Mfc1(dst, FTMP);
5061 __ Andi(dst, dst, 1);
5062 break;
5063 case kCondGE:
5064 if (gt_bias) {
5065 __ CmpUleD(FTMP, rhs, lhs);
5066 } else {
5067 __ CmpLeD(FTMP, rhs, lhs);
5068 }
5069 __ Mfc1(dst, FTMP);
5070 __ Andi(dst, dst, 1);
5071 break;
5072 default:
5073 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5074 UNREACHABLE();
5075 }
5076 } else {
5077 switch (cond) {
5078 case kCondEQ:
5079 __ CeqD(0, lhs, rhs);
5080 __ LoadConst32(dst, 1);
5081 __ Movf(dst, ZERO, 0);
5082 break;
5083 case kCondNE:
5084 __ CeqD(0, lhs, rhs);
5085 __ LoadConst32(dst, 1);
5086 __ Movt(dst, ZERO, 0);
5087 break;
5088 case kCondLT:
5089 if (gt_bias) {
5090 __ ColtD(0, lhs, rhs);
5091 } else {
5092 __ CultD(0, lhs, rhs);
5093 }
5094 __ LoadConst32(dst, 1);
5095 __ Movf(dst, ZERO, 0);
5096 break;
5097 case kCondLE:
5098 if (gt_bias) {
5099 __ ColeD(0, lhs, rhs);
5100 } else {
5101 __ CuleD(0, lhs, rhs);
5102 }
5103 __ LoadConst32(dst, 1);
5104 __ Movf(dst, ZERO, 0);
5105 break;
5106 case kCondGT:
5107 if (gt_bias) {
5108 __ CultD(0, rhs, lhs);
5109 } else {
5110 __ ColtD(0, rhs, lhs);
5111 }
5112 __ LoadConst32(dst, 1);
5113 __ Movf(dst, ZERO, 0);
5114 break;
5115 case kCondGE:
5116 if (gt_bias) {
5117 __ CuleD(0, rhs, lhs);
5118 } else {
5119 __ ColeD(0, rhs, lhs);
5120 }
5121 __ LoadConst32(dst, 1);
5122 __ Movf(dst, ZERO, 0);
5123 break;
5124 default:
5125 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5126 UNREACHABLE();
5127 }
5128 }
5129 }
5130}
5131
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005132bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5133 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005134 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005135 LocationSummary* input_locations,
5136 int cc) {
5137 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5138 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5139 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005140 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005141 switch (cond) {
5142 case kCondEQ:
5143 __ CeqS(cc, lhs, rhs);
5144 return false;
5145 case kCondNE:
5146 __ CeqS(cc, lhs, rhs);
5147 return true;
5148 case kCondLT:
5149 if (gt_bias) {
5150 __ ColtS(cc, lhs, rhs);
5151 } else {
5152 __ CultS(cc, lhs, rhs);
5153 }
5154 return false;
5155 case kCondLE:
5156 if (gt_bias) {
5157 __ ColeS(cc, lhs, rhs);
5158 } else {
5159 __ CuleS(cc, lhs, rhs);
5160 }
5161 return false;
5162 case kCondGT:
5163 if (gt_bias) {
5164 __ CultS(cc, rhs, lhs);
5165 } else {
5166 __ ColtS(cc, rhs, lhs);
5167 }
5168 return false;
5169 case kCondGE:
5170 if (gt_bias) {
5171 __ CuleS(cc, rhs, lhs);
5172 } else {
5173 __ ColeS(cc, rhs, lhs);
5174 }
5175 return false;
5176 default:
5177 LOG(FATAL) << "Unexpected non-floating-point condition";
5178 UNREACHABLE();
5179 }
5180 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005181 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005182 switch (cond) {
5183 case kCondEQ:
5184 __ CeqD(cc, lhs, rhs);
5185 return false;
5186 case kCondNE:
5187 __ CeqD(cc, lhs, rhs);
5188 return true;
5189 case kCondLT:
5190 if (gt_bias) {
5191 __ ColtD(cc, lhs, rhs);
5192 } else {
5193 __ CultD(cc, lhs, rhs);
5194 }
5195 return false;
5196 case kCondLE:
5197 if (gt_bias) {
5198 __ ColeD(cc, lhs, rhs);
5199 } else {
5200 __ CuleD(cc, lhs, rhs);
5201 }
5202 return false;
5203 case kCondGT:
5204 if (gt_bias) {
5205 __ CultD(cc, rhs, lhs);
5206 } else {
5207 __ ColtD(cc, rhs, lhs);
5208 }
5209 return false;
5210 case kCondGE:
5211 if (gt_bias) {
5212 __ CuleD(cc, rhs, lhs);
5213 } else {
5214 __ ColeD(cc, rhs, lhs);
5215 }
5216 return false;
5217 default:
5218 LOG(FATAL) << "Unexpected non-floating-point condition";
5219 UNREACHABLE();
5220 }
5221 }
5222}
5223
5224bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5225 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005226 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005227 LocationSummary* input_locations,
5228 FRegister dst) {
5229 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5230 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5231 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005232 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005233 switch (cond) {
5234 case kCondEQ:
5235 __ CmpEqS(dst, lhs, rhs);
5236 return false;
5237 case kCondNE:
5238 __ CmpEqS(dst, lhs, rhs);
5239 return true;
5240 case kCondLT:
5241 if (gt_bias) {
5242 __ CmpLtS(dst, lhs, rhs);
5243 } else {
5244 __ CmpUltS(dst, lhs, rhs);
5245 }
5246 return false;
5247 case kCondLE:
5248 if (gt_bias) {
5249 __ CmpLeS(dst, lhs, rhs);
5250 } else {
5251 __ CmpUleS(dst, lhs, rhs);
5252 }
5253 return false;
5254 case kCondGT:
5255 if (gt_bias) {
5256 __ CmpUltS(dst, rhs, lhs);
5257 } else {
5258 __ CmpLtS(dst, rhs, lhs);
5259 }
5260 return false;
5261 case kCondGE:
5262 if (gt_bias) {
5263 __ CmpUleS(dst, rhs, lhs);
5264 } else {
5265 __ CmpLeS(dst, rhs, lhs);
5266 }
5267 return false;
5268 default:
5269 LOG(FATAL) << "Unexpected non-floating-point condition";
5270 UNREACHABLE();
5271 }
5272 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005273 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005274 switch (cond) {
5275 case kCondEQ:
5276 __ CmpEqD(dst, lhs, rhs);
5277 return false;
5278 case kCondNE:
5279 __ CmpEqD(dst, lhs, rhs);
5280 return true;
5281 case kCondLT:
5282 if (gt_bias) {
5283 __ CmpLtD(dst, lhs, rhs);
5284 } else {
5285 __ CmpUltD(dst, lhs, rhs);
5286 }
5287 return false;
5288 case kCondLE:
5289 if (gt_bias) {
5290 __ CmpLeD(dst, lhs, rhs);
5291 } else {
5292 __ CmpUleD(dst, lhs, rhs);
5293 }
5294 return false;
5295 case kCondGT:
5296 if (gt_bias) {
5297 __ CmpUltD(dst, rhs, lhs);
5298 } else {
5299 __ CmpLtD(dst, rhs, lhs);
5300 }
5301 return false;
5302 case kCondGE:
5303 if (gt_bias) {
5304 __ CmpUleD(dst, rhs, lhs);
5305 } else {
5306 __ CmpLeD(dst, rhs, lhs);
5307 }
5308 return false;
5309 default:
5310 LOG(FATAL) << "Unexpected non-floating-point condition";
5311 UNREACHABLE();
5312 }
5313 }
5314}
5315
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005316void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5317 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005318 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005319 LocationSummary* locations,
5320 MipsLabel* label) {
5321 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5322 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5323 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005324 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005325 if (isR6) {
5326 switch (cond) {
5327 case kCondEQ:
5328 __ CmpEqS(FTMP, lhs, rhs);
5329 __ Bc1nez(FTMP, label);
5330 break;
5331 case kCondNE:
5332 __ CmpEqS(FTMP, lhs, rhs);
5333 __ Bc1eqz(FTMP, label);
5334 break;
5335 case kCondLT:
5336 if (gt_bias) {
5337 __ CmpLtS(FTMP, lhs, rhs);
5338 } else {
5339 __ CmpUltS(FTMP, lhs, rhs);
5340 }
5341 __ Bc1nez(FTMP, label);
5342 break;
5343 case kCondLE:
5344 if (gt_bias) {
5345 __ CmpLeS(FTMP, lhs, rhs);
5346 } else {
5347 __ CmpUleS(FTMP, lhs, rhs);
5348 }
5349 __ Bc1nez(FTMP, label);
5350 break;
5351 case kCondGT:
5352 if (gt_bias) {
5353 __ CmpUltS(FTMP, rhs, lhs);
5354 } else {
5355 __ CmpLtS(FTMP, rhs, lhs);
5356 }
5357 __ Bc1nez(FTMP, label);
5358 break;
5359 case kCondGE:
5360 if (gt_bias) {
5361 __ CmpUleS(FTMP, rhs, lhs);
5362 } else {
5363 __ CmpLeS(FTMP, rhs, lhs);
5364 }
5365 __ Bc1nez(FTMP, label);
5366 break;
5367 default:
5368 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005369 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005370 }
5371 } else {
5372 switch (cond) {
5373 case kCondEQ:
5374 __ CeqS(0, lhs, rhs);
5375 __ Bc1t(0, label);
5376 break;
5377 case kCondNE:
5378 __ CeqS(0, lhs, rhs);
5379 __ Bc1f(0, label);
5380 break;
5381 case kCondLT:
5382 if (gt_bias) {
5383 __ ColtS(0, lhs, rhs);
5384 } else {
5385 __ CultS(0, lhs, rhs);
5386 }
5387 __ Bc1t(0, label);
5388 break;
5389 case kCondLE:
5390 if (gt_bias) {
5391 __ ColeS(0, lhs, rhs);
5392 } else {
5393 __ CuleS(0, lhs, rhs);
5394 }
5395 __ Bc1t(0, label);
5396 break;
5397 case kCondGT:
5398 if (gt_bias) {
5399 __ CultS(0, rhs, lhs);
5400 } else {
5401 __ ColtS(0, rhs, lhs);
5402 }
5403 __ Bc1t(0, label);
5404 break;
5405 case kCondGE:
5406 if (gt_bias) {
5407 __ CuleS(0, rhs, lhs);
5408 } else {
5409 __ ColeS(0, rhs, lhs);
5410 }
5411 __ Bc1t(0, label);
5412 break;
5413 default:
5414 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005415 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005416 }
5417 }
5418 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005419 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005420 if (isR6) {
5421 switch (cond) {
5422 case kCondEQ:
5423 __ CmpEqD(FTMP, lhs, rhs);
5424 __ Bc1nez(FTMP, label);
5425 break;
5426 case kCondNE:
5427 __ CmpEqD(FTMP, lhs, rhs);
5428 __ Bc1eqz(FTMP, label);
5429 break;
5430 case kCondLT:
5431 if (gt_bias) {
5432 __ CmpLtD(FTMP, lhs, rhs);
5433 } else {
5434 __ CmpUltD(FTMP, lhs, rhs);
5435 }
5436 __ Bc1nez(FTMP, label);
5437 break;
5438 case kCondLE:
5439 if (gt_bias) {
5440 __ CmpLeD(FTMP, lhs, rhs);
5441 } else {
5442 __ CmpUleD(FTMP, lhs, rhs);
5443 }
5444 __ Bc1nez(FTMP, label);
5445 break;
5446 case kCondGT:
5447 if (gt_bias) {
5448 __ CmpUltD(FTMP, rhs, lhs);
5449 } else {
5450 __ CmpLtD(FTMP, rhs, lhs);
5451 }
5452 __ Bc1nez(FTMP, label);
5453 break;
5454 case kCondGE:
5455 if (gt_bias) {
5456 __ CmpUleD(FTMP, rhs, lhs);
5457 } else {
5458 __ CmpLeD(FTMP, rhs, lhs);
5459 }
5460 __ Bc1nez(FTMP, label);
5461 break;
5462 default:
5463 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005464 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005465 }
5466 } else {
5467 switch (cond) {
5468 case kCondEQ:
5469 __ CeqD(0, lhs, rhs);
5470 __ Bc1t(0, label);
5471 break;
5472 case kCondNE:
5473 __ CeqD(0, lhs, rhs);
5474 __ Bc1f(0, label);
5475 break;
5476 case kCondLT:
5477 if (gt_bias) {
5478 __ ColtD(0, lhs, rhs);
5479 } else {
5480 __ CultD(0, lhs, rhs);
5481 }
5482 __ Bc1t(0, label);
5483 break;
5484 case kCondLE:
5485 if (gt_bias) {
5486 __ ColeD(0, lhs, rhs);
5487 } else {
5488 __ CuleD(0, lhs, rhs);
5489 }
5490 __ Bc1t(0, label);
5491 break;
5492 case kCondGT:
5493 if (gt_bias) {
5494 __ CultD(0, rhs, lhs);
5495 } else {
5496 __ ColtD(0, rhs, lhs);
5497 }
5498 __ Bc1t(0, label);
5499 break;
5500 case kCondGE:
5501 if (gt_bias) {
5502 __ CuleD(0, rhs, lhs);
5503 } else {
5504 __ ColeD(0, rhs, lhs);
5505 }
5506 __ Bc1t(0, label);
5507 break;
5508 default:
5509 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005510 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005511 }
5512 }
5513 }
5514}
5515
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005516void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005517 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005518 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005519 MipsLabel* false_target) {
5520 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005521
David Brazdil0debae72015-11-12 18:37:00 +00005522 if (true_target == nullptr && false_target == nullptr) {
5523 // Nothing to do. The code always falls through.
5524 return;
5525 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005526 // Constant condition, statically compared against "true" (integer value 1).
5527 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005528 if (true_target != nullptr) {
5529 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005530 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005531 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005532 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005533 if (false_target != nullptr) {
5534 __ B(false_target);
5535 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005536 }
David Brazdil0debae72015-11-12 18:37:00 +00005537 return;
5538 }
5539
5540 // The following code generates these patterns:
5541 // (1) true_target == nullptr && false_target != nullptr
5542 // - opposite condition true => branch to false_target
5543 // (2) true_target != nullptr && false_target == nullptr
5544 // - condition true => branch to true_target
5545 // (3) true_target != nullptr && false_target != nullptr
5546 // - condition true => branch to true_target
5547 // - branch to false_target
5548 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005549 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005550 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005551 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005552 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005553 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5554 } else {
5555 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5556 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005557 } else {
5558 // The condition instruction has not been materialized, use its inputs as
5559 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005560 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005561 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005562 LocationSummary* locations = cond->GetLocations();
5563 IfCondition if_cond = condition->GetCondition();
5564 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005565
David Brazdil0debae72015-11-12 18:37:00 +00005566 if (true_target == nullptr) {
5567 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005568 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005569 }
5570
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005571 switch (type) {
5572 default:
5573 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5574 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005575 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005576 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5577 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005578 case DataType::Type::kFloat32:
5579 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005580 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5581 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005582 }
5583 }
David Brazdil0debae72015-11-12 18:37:00 +00005584
5585 // If neither branch falls through (case 3), the conditional branch to `true_target`
5586 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5587 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005588 __ B(false_target);
5589 }
5590}
5591
5592void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005593 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005594 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005595 locations->SetInAt(0, Location::RequiresRegister());
5596 }
5597}
5598
5599void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005600 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5601 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5602 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5603 nullptr : codegen_->GetLabelOf(true_successor);
5604 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5605 nullptr : codegen_->GetLabelOf(false_successor);
5606 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005607}
5608
5609void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005610 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005611 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005612 InvokeRuntimeCallingConvention calling_convention;
5613 RegisterSet caller_saves = RegisterSet::Empty();
5614 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5615 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005616 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005617 locations->SetInAt(0, Location::RequiresRegister());
5618 }
5619}
5620
5621void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005622 SlowPathCodeMIPS* slow_path =
5623 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005624 GenerateTestAndBranch(deoptimize,
5625 /* condition_input_index */ 0,
5626 slow_path->GetEntryLabel(),
5627 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005628}
5629
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005630// This function returns true if a conditional move can be generated for HSelect.
5631// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5632// branches and regular moves.
5633//
5634// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5635//
5636// While determining feasibility of a conditional move and setting inputs/outputs
5637// are two distinct tasks, this function does both because they share quite a bit
5638// of common logic.
5639static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5640 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5641 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5642 HCondition* condition = cond->AsCondition();
5643
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005644 DataType::Type cond_type =
5645 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5646 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005647
5648 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5649 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5650 bool is_true_value_zero_constant =
5651 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5652 bool is_false_value_zero_constant =
5653 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5654
5655 bool can_move_conditionally = false;
5656 bool use_const_for_false_in = false;
5657 bool use_const_for_true_in = false;
5658
5659 if (!cond->IsConstant()) {
5660 switch (cond_type) {
5661 default:
5662 switch (dst_type) {
5663 default:
5664 // Moving int on int condition.
5665 if (is_r6) {
5666 if (is_true_value_zero_constant) {
5667 // seleqz out_reg, false_reg, cond_reg
5668 can_move_conditionally = true;
5669 use_const_for_true_in = true;
5670 } else if (is_false_value_zero_constant) {
5671 // selnez out_reg, true_reg, cond_reg
5672 can_move_conditionally = true;
5673 use_const_for_false_in = true;
5674 } else if (materialized) {
5675 // Not materializing unmaterialized int conditions
5676 // to keep the instruction count low.
5677 // selnez AT, true_reg, cond_reg
5678 // seleqz TMP, false_reg, cond_reg
5679 // or out_reg, AT, TMP
5680 can_move_conditionally = true;
5681 }
5682 } else {
5683 // movn out_reg, true_reg/ZERO, cond_reg
5684 can_move_conditionally = true;
5685 use_const_for_true_in = is_true_value_zero_constant;
5686 }
5687 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005688 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005689 // Moving long on int condition.
5690 if (is_r6) {
5691 if (is_true_value_zero_constant) {
5692 // seleqz out_reg_lo, false_reg_lo, cond_reg
5693 // seleqz out_reg_hi, false_reg_hi, cond_reg
5694 can_move_conditionally = true;
5695 use_const_for_true_in = true;
5696 } else if (is_false_value_zero_constant) {
5697 // selnez out_reg_lo, true_reg_lo, cond_reg
5698 // selnez out_reg_hi, true_reg_hi, cond_reg
5699 can_move_conditionally = true;
5700 use_const_for_false_in = true;
5701 }
5702 // Other long conditional moves would generate 6+ instructions,
5703 // which is too many.
5704 } else {
5705 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5706 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5707 can_move_conditionally = true;
5708 use_const_for_true_in = is_true_value_zero_constant;
5709 }
5710 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005711 case DataType::Type::kFloat32:
5712 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005713 // Moving float/double on int condition.
5714 if (is_r6) {
5715 if (materialized) {
5716 // Not materializing unmaterialized int conditions
5717 // to keep the instruction count low.
5718 can_move_conditionally = true;
5719 if (is_true_value_zero_constant) {
5720 // sltu TMP, ZERO, cond_reg
5721 // mtc1 TMP, temp_cond_reg
5722 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5723 use_const_for_true_in = true;
5724 } else if (is_false_value_zero_constant) {
5725 // sltu TMP, ZERO, cond_reg
5726 // mtc1 TMP, temp_cond_reg
5727 // selnez.fmt out_reg, true_reg, temp_cond_reg
5728 use_const_for_false_in = true;
5729 } else {
5730 // sltu TMP, ZERO, cond_reg
5731 // mtc1 TMP, temp_cond_reg
5732 // sel.fmt temp_cond_reg, false_reg, true_reg
5733 // mov.fmt out_reg, temp_cond_reg
5734 }
5735 }
5736 } else {
5737 // movn.fmt out_reg, true_reg, cond_reg
5738 can_move_conditionally = true;
5739 }
5740 break;
5741 }
5742 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005743 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005744 // We don't materialize long comparison now
5745 // and use conditional branches instead.
5746 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005747 case DataType::Type::kFloat32:
5748 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005749 switch (dst_type) {
5750 default:
5751 // Moving int on float/double condition.
5752 if (is_r6) {
5753 if (is_true_value_zero_constant) {
5754 // mfc1 TMP, temp_cond_reg
5755 // seleqz out_reg, false_reg, 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, true_reg, TMP
5761 can_move_conditionally = true;
5762 use_const_for_false_in = true;
5763 } else {
5764 // mfc1 TMP, temp_cond_reg
5765 // selnez AT, true_reg, TMP
5766 // seleqz TMP, false_reg, TMP
5767 // or out_reg, AT, TMP
5768 can_move_conditionally = true;
5769 }
5770 } else {
5771 // movt out_reg, true_reg/ZERO, cc
5772 can_move_conditionally = true;
5773 use_const_for_true_in = is_true_value_zero_constant;
5774 }
5775 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005776 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005777 // Moving long on float/double condition.
5778 if (is_r6) {
5779 if (is_true_value_zero_constant) {
5780 // mfc1 TMP, temp_cond_reg
5781 // seleqz out_reg_lo, false_reg_lo, TMP
5782 // seleqz out_reg_hi, false_reg_hi, TMP
5783 can_move_conditionally = true;
5784 use_const_for_true_in = true;
5785 } else if (is_false_value_zero_constant) {
5786 // mfc1 TMP, temp_cond_reg
5787 // selnez out_reg_lo, true_reg_lo, TMP
5788 // selnez out_reg_hi, true_reg_hi, TMP
5789 can_move_conditionally = true;
5790 use_const_for_false_in = true;
5791 }
5792 // Other long conditional moves would generate 6+ instructions,
5793 // which is too many.
5794 } else {
5795 // movt out_reg_lo, true_reg_lo/ZERO, cc
5796 // movt out_reg_hi, true_reg_hi/ZERO, cc
5797 can_move_conditionally = true;
5798 use_const_for_true_in = is_true_value_zero_constant;
5799 }
5800 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005801 case DataType::Type::kFloat32:
5802 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005803 // Moving float/double on float/double condition.
5804 if (is_r6) {
5805 can_move_conditionally = true;
5806 if (is_true_value_zero_constant) {
5807 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5808 use_const_for_true_in = true;
5809 } else if (is_false_value_zero_constant) {
5810 // selnez.fmt out_reg, true_reg, temp_cond_reg
5811 use_const_for_false_in = true;
5812 } else {
5813 // sel.fmt temp_cond_reg, false_reg, true_reg
5814 // mov.fmt out_reg, temp_cond_reg
5815 }
5816 } else {
5817 // movt.fmt out_reg, true_reg, cc
5818 can_move_conditionally = true;
5819 }
5820 break;
5821 }
5822 break;
5823 }
5824 }
5825
5826 if (can_move_conditionally) {
5827 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5828 } else {
5829 DCHECK(!use_const_for_false_in);
5830 DCHECK(!use_const_for_true_in);
5831 }
5832
5833 if (locations_to_set != nullptr) {
5834 if (use_const_for_false_in) {
5835 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5836 } else {
5837 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005838 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005839 ? Location::RequiresFpuRegister()
5840 : Location::RequiresRegister());
5841 }
5842 if (use_const_for_true_in) {
5843 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5844 } else {
5845 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005846 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005847 ? Location::RequiresFpuRegister()
5848 : Location::RequiresRegister());
5849 }
5850 if (materialized) {
5851 locations_to_set->SetInAt(2, Location::RequiresRegister());
5852 }
5853 // On R6 we don't require the output to be the same as the
5854 // first input for conditional moves unlike on R2.
5855 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5856 if (is_out_same_as_first_in) {
5857 locations_to_set->SetOut(Location::SameAsFirstInput());
5858 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005859 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005860 ? Location::RequiresFpuRegister()
5861 : Location::RequiresRegister());
5862 }
5863 }
5864
5865 return can_move_conditionally;
5866}
5867
5868void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5869 LocationSummary* locations = select->GetLocations();
5870 Location dst = locations->Out();
5871 Location src = locations->InAt(1);
5872 Register src_reg = ZERO;
5873 Register src_reg_high = ZERO;
5874 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5875 Register cond_reg = TMP;
5876 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005877 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005878 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005879 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005880
5881 if (IsBooleanValueOrMaterializedCondition(cond)) {
5882 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5883 } else {
5884 HCondition* condition = cond->AsCondition();
5885 LocationSummary* cond_locations = cond->GetLocations();
5886 IfCondition if_cond = condition->GetCondition();
5887 cond_type = condition->InputAt(0)->GetType();
5888 switch (cond_type) {
5889 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005890 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005891 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5892 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005893 case DataType::Type::kFloat32:
5894 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005895 cond_inverted = MaterializeFpCompareR2(if_cond,
5896 condition->IsGtBias(),
5897 cond_type,
5898 cond_locations,
5899 cond_cc);
5900 break;
5901 }
5902 }
5903
5904 DCHECK(dst.Equals(locations->InAt(0)));
5905 if (src.IsRegister()) {
5906 src_reg = src.AsRegister<Register>();
5907 } else if (src.IsRegisterPair()) {
5908 src_reg = src.AsRegisterPairLow<Register>();
5909 src_reg_high = src.AsRegisterPairHigh<Register>();
5910 } else if (src.IsConstant()) {
5911 DCHECK(src.GetConstant()->IsZeroBitPattern());
5912 }
5913
5914 switch (cond_type) {
5915 default:
5916 switch (dst_type) {
5917 default:
5918 if (cond_inverted) {
5919 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5920 } else {
5921 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5922 }
5923 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005924 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005925 if (cond_inverted) {
5926 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5927 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5928 } else {
5929 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5930 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5931 }
5932 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005933 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005934 if (cond_inverted) {
5935 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5936 } else {
5937 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5938 }
5939 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005941 if (cond_inverted) {
5942 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5943 } else {
5944 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5945 }
5946 break;
5947 }
5948 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005949 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005950 LOG(FATAL) << "Unreachable";
5951 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005952 case DataType::Type::kFloat32:
5953 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005954 switch (dst_type) {
5955 default:
5956 if (cond_inverted) {
5957 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5958 } else {
5959 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5960 }
5961 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005962 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005963 if (cond_inverted) {
5964 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5965 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5966 } else {
5967 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5968 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5969 }
5970 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005971 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005972 if (cond_inverted) {
5973 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5974 } else {
5975 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5976 }
5977 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005978 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005979 if (cond_inverted) {
5980 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5981 } else {
5982 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5983 }
5984 break;
5985 }
5986 break;
5987 }
5988}
5989
5990void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5991 LocationSummary* locations = select->GetLocations();
5992 Location dst = locations->Out();
5993 Location false_src = locations->InAt(0);
5994 Location true_src = locations->InAt(1);
5995 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5996 Register cond_reg = TMP;
5997 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005998 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005999 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006000 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006001
6002 if (IsBooleanValueOrMaterializedCondition(cond)) {
6003 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6004 } else {
6005 HCondition* condition = cond->AsCondition();
6006 LocationSummary* cond_locations = cond->GetLocations();
6007 IfCondition if_cond = condition->GetCondition();
6008 cond_type = condition->InputAt(0)->GetType();
6009 switch (cond_type) {
6010 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006011 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006012 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6013 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006014 case DataType::Type::kFloat32:
6015 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006016 cond_inverted = MaterializeFpCompareR6(if_cond,
6017 condition->IsGtBias(),
6018 cond_type,
6019 cond_locations,
6020 fcond_reg);
6021 break;
6022 }
6023 }
6024
6025 if (true_src.IsConstant()) {
6026 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6027 }
6028 if (false_src.IsConstant()) {
6029 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6030 }
6031
6032 switch (dst_type) {
6033 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006034 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006035 __ Mfc1(cond_reg, fcond_reg);
6036 }
6037 if (true_src.IsConstant()) {
6038 if (cond_inverted) {
6039 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6040 } else {
6041 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6042 }
6043 } else if (false_src.IsConstant()) {
6044 if (cond_inverted) {
6045 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6046 } else {
6047 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6048 }
6049 } else {
6050 DCHECK_NE(cond_reg, AT);
6051 if (cond_inverted) {
6052 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6053 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6054 } else {
6055 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6056 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6057 }
6058 __ Or(dst.AsRegister<Register>(), AT, TMP);
6059 }
6060 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006061 case DataType::Type::kInt64: {
6062 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006063 __ Mfc1(cond_reg, fcond_reg);
6064 }
6065 Register dst_lo = dst.AsRegisterPairLow<Register>();
6066 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6067 if (true_src.IsConstant()) {
6068 Register src_lo = false_src.AsRegisterPairLow<Register>();
6069 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6070 if (cond_inverted) {
6071 __ Selnez(dst_lo, src_lo, cond_reg);
6072 __ Selnez(dst_hi, src_hi, cond_reg);
6073 } else {
6074 __ Seleqz(dst_lo, src_lo, cond_reg);
6075 __ Seleqz(dst_hi, src_hi, cond_reg);
6076 }
6077 } else {
6078 DCHECK(false_src.IsConstant());
6079 Register src_lo = true_src.AsRegisterPairLow<Register>();
6080 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6081 if (cond_inverted) {
6082 __ Seleqz(dst_lo, src_lo, cond_reg);
6083 __ Seleqz(dst_hi, src_hi, cond_reg);
6084 } else {
6085 __ Selnez(dst_lo, src_lo, cond_reg);
6086 __ Selnez(dst_hi, src_hi, cond_reg);
6087 }
6088 }
6089 break;
6090 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006091 case DataType::Type::kFloat32: {
6092 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006093 // sel*.fmt tests bit 0 of the condition register, account for that.
6094 __ Sltu(TMP, ZERO, cond_reg);
6095 __ Mtc1(TMP, fcond_reg);
6096 }
6097 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6098 if (true_src.IsConstant()) {
6099 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6100 if (cond_inverted) {
6101 __ SelnezS(dst_reg, src_reg, fcond_reg);
6102 } else {
6103 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6104 }
6105 } else if (false_src.IsConstant()) {
6106 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6107 if (cond_inverted) {
6108 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6109 } else {
6110 __ SelnezS(dst_reg, src_reg, fcond_reg);
6111 }
6112 } else {
6113 if (cond_inverted) {
6114 __ SelS(fcond_reg,
6115 true_src.AsFpuRegister<FRegister>(),
6116 false_src.AsFpuRegister<FRegister>());
6117 } else {
6118 __ SelS(fcond_reg,
6119 false_src.AsFpuRegister<FRegister>(),
6120 true_src.AsFpuRegister<FRegister>());
6121 }
6122 __ MovS(dst_reg, fcond_reg);
6123 }
6124 break;
6125 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006126 case DataType::Type::kFloat64: {
6127 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006128 // sel*.fmt tests bit 0 of the condition register, account for that.
6129 __ Sltu(TMP, ZERO, cond_reg);
6130 __ Mtc1(TMP, fcond_reg);
6131 }
6132 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6133 if (true_src.IsConstant()) {
6134 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6135 if (cond_inverted) {
6136 __ SelnezD(dst_reg, src_reg, fcond_reg);
6137 } else {
6138 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6139 }
6140 } else if (false_src.IsConstant()) {
6141 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6142 if (cond_inverted) {
6143 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6144 } else {
6145 __ SelnezD(dst_reg, src_reg, fcond_reg);
6146 }
6147 } else {
6148 if (cond_inverted) {
6149 __ SelD(fcond_reg,
6150 true_src.AsFpuRegister<FRegister>(),
6151 false_src.AsFpuRegister<FRegister>());
6152 } else {
6153 __ SelD(fcond_reg,
6154 false_src.AsFpuRegister<FRegister>(),
6155 true_src.AsFpuRegister<FRegister>());
6156 }
6157 __ MovD(dst_reg, fcond_reg);
6158 }
6159 break;
6160 }
6161 }
6162}
6163
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006164void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006165 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006166 LocationSummary(flag, LocationSummary::kNoCall);
6167 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006168}
6169
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006170void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6171 __ LoadFromOffset(kLoadWord,
6172 flag->GetLocations()->Out().AsRegister<Register>(),
6173 SP,
6174 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006175}
6176
David Brazdil74eb1b22015-12-14 11:44:01 +00006177void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006178 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006179 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006180}
6181
6182void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006183 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6184 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6185 if (is_r6) {
6186 GenConditionalMoveR6(select);
6187 } else {
6188 GenConditionalMoveR2(select);
6189 }
6190 } else {
6191 LocationSummary* locations = select->GetLocations();
6192 MipsLabel false_target;
6193 GenerateTestAndBranch(select,
6194 /* condition_input_index */ 2,
6195 /* true_target */ nullptr,
6196 &false_target);
6197 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6198 __ Bind(&false_target);
6199 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006200}
6201
David Srbecky0cf44932015-12-09 14:09:59 +00006202void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006203 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006204}
6205
David Srbeckyd28f4a02016-03-14 17:14:24 +00006206void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6207 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006208}
6209
6210void CodeGeneratorMIPS::GenerateNop() {
6211 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006212}
6213
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006214void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006215 DataType::Type field_type = field_info.GetFieldType();
6216 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006217 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006218 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006219 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006220 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006221 instruction,
6222 generate_volatile
6223 ? LocationSummary::kCallOnMainOnly
6224 : (object_field_get_with_read_barrier
6225 ? LocationSummary::kCallOnSlowPath
6226 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006227
Alexey Frunzec61c0762017-04-10 13:54:23 -07006228 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6229 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6230 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006231 locations->SetInAt(0, Location::RequiresRegister());
6232 if (generate_volatile) {
6233 InvokeRuntimeCallingConvention calling_convention;
6234 // need A0 to hold base + offset
6235 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006236 if (field_type == DataType::Type::kInt64) {
6237 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006238 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006239 // Use Location::Any() to prevent situations when running out of available fp registers.
6240 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006241 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006242 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006243 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6244 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6245 }
6246 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006247 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006248 locations->SetOut(Location::RequiresFpuRegister());
6249 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006250 // The output overlaps in the case of an object field get with
6251 // read barriers enabled: we do not want the move to overwrite the
6252 // object's location, as we need it to emit the read barrier.
6253 locations->SetOut(Location::RequiresRegister(),
6254 object_field_get_with_read_barrier
6255 ? Location::kOutputOverlap
6256 : Location::kNoOutputOverlap);
6257 }
6258 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6259 // We need a temporary register for the read barrier marking slow
6260 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006261 if (!kBakerReadBarrierThunksEnableForFields) {
6262 locations->AddTemp(Location::RequiresRegister());
6263 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006264 }
6265 }
6266}
6267
6268void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6269 const FieldInfo& field_info,
6270 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006271 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6272 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006273 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006274 Location obj_loc = locations->InAt(0);
6275 Register obj = obj_loc.AsRegister<Register>();
6276 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006277 LoadOperandType load_type = kLoadUnsignedByte;
6278 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006279 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006280 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006281
6282 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006283 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006284 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006285 load_type = kLoadUnsignedByte;
6286 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006287 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006288 load_type = kLoadSignedByte;
6289 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006290 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006291 load_type = kLoadUnsignedHalfword;
6292 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006293 case DataType::Type::kInt16:
6294 load_type = kLoadSignedHalfword;
6295 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006296 case DataType::Type::kInt32:
6297 case DataType::Type::kFloat32:
6298 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006299 load_type = kLoadWord;
6300 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 case DataType::Type::kInt64:
6302 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006303 load_type = kLoadDoubleword;
6304 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006305 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006306 LOG(FATAL) << "Unreachable type " << type;
6307 UNREACHABLE();
6308 }
6309
6310 if (is_volatile && load_type == kLoadDoubleword) {
6311 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006312 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006313 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006314 __ LoadFromOffset(kLoadWord,
6315 ZERO,
6316 locations->GetTemp(0).AsRegister<Register>(),
6317 0,
6318 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006319 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006320 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006321 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006322 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006323 if (dst_loc.IsFpuRegister()) {
6324 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006325 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006326 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006327 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006328 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006329 __ StoreToOffset(kStoreWord,
6330 locations->GetTemp(1).AsRegister<Register>(),
6331 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006332 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006333 __ StoreToOffset(kStoreWord,
6334 locations->GetTemp(2).AsRegister<Register>(),
6335 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006336 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006337 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006338 }
6339 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006340 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006341 // /* HeapReference<Object> */ dst = *(obj + offset)
6342 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006343 Location temp_loc =
6344 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006345 // Note that a potential implicit null check is handled in this
6346 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6347 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6348 dst_loc,
6349 obj,
6350 offset,
6351 temp_loc,
6352 /* needs_null_check */ true);
6353 if (is_volatile) {
6354 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6355 }
6356 } else {
6357 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6358 if (is_volatile) {
6359 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6360 }
6361 // If read barriers are enabled, emit read barriers other than
6362 // Baker's using a slow path (and also unpoison the loaded
6363 // reference, if heap poisoning is enabled).
6364 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6365 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006366 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006367 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006368 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006369 DCHECK(dst_loc.IsRegisterPair());
6370 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006371 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006372 DCHECK(dst_loc.IsRegister());
6373 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006374 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006375 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006376 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006377 DCHECK(dst_loc.IsFpuRegister());
6378 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006379 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006380 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006381 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006382 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006383 }
6384 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006385 }
6386
Alexey Frunze15958152017-02-09 19:08:30 -08006387 // Memory barriers, in the case of references, are handled in the
6388 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006389 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006390 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6391 }
6392}
6393
6394void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006395 DataType::Type field_type = field_info.GetFieldType();
6396 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006397 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006398 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006399 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006400
6401 locations->SetInAt(0, Location::RequiresRegister());
6402 if (generate_volatile) {
6403 InvokeRuntimeCallingConvention calling_convention;
6404 // need A0 to hold base + offset
6405 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006406 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006407 locations->SetInAt(1, Location::RegisterPairLocation(
6408 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6409 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006410 // Use Location::Any() to prevent situations when running out of available fp registers.
6411 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006412 // Pass FP parameters in core registers.
6413 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6414 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6415 }
6416 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006417 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006418 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006419 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006420 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006421 }
6422 }
6423}
6424
6425void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6426 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006427 uint32_t dex_pc,
6428 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006429 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006430 LocationSummary* locations = instruction->GetLocations();
6431 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006432 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006433 StoreOperandType store_type = kStoreByte;
6434 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006435 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006436 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006437 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006438
6439 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006440 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006441 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006442 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006443 store_type = kStoreByte;
6444 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006445 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006446 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006447 store_type = kStoreHalfword;
6448 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006449 case DataType::Type::kInt32:
6450 case DataType::Type::kFloat32:
6451 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006452 store_type = kStoreWord;
6453 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006454 case DataType::Type::kInt64:
6455 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006456 store_type = kStoreDoubleword;
6457 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006458 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006459 LOG(FATAL) << "Unreachable type " << type;
6460 UNREACHABLE();
6461 }
6462
6463 if (is_volatile) {
6464 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6465 }
6466
6467 if (is_volatile && store_type == kStoreDoubleword) {
6468 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006469 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006470 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006471 __ LoadFromOffset(kLoadWord,
6472 ZERO,
6473 locations->GetTemp(0).AsRegister<Register>(),
6474 0,
6475 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006476 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006477 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006478 if (value_location.IsFpuRegister()) {
6479 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6480 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006481 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006482 value_location.AsFpuRegister<FRegister>());
6483 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006484 __ LoadFromOffset(kLoadWord,
6485 locations->GetTemp(1).AsRegister<Register>(),
6486 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006487 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006488 __ LoadFromOffset(kLoadWord,
6489 locations->GetTemp(2).AsRegister<Register>(),
6490 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006491 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006492 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006493 DCHECK(value_location.IsConstant());
6494 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6495 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006496 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6497 locations->GetTemp(1).AsRegister<Register>(),
6498 value);
6499 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006500 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006501 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006502 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6503 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006504 if (value_location.IsConstant()) {
6505 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6506 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006507 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006508 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006509 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006510 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006511 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006512 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006513 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006514 if (kPoisonHeapReferences && needs_write_barrier) {
6515 // Note that in the case where `value` is a null reference,
6516 // we do not enter this block, as a null reference does not
6517 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006518 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006519 __ PoisonHeapReference(TMP, src);
6520 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6521 } else {
6522 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6523 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006524 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006525 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006526 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006527 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006528 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006529 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006530 }
6531 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006532 }
6533
Alexey Frunzec061de12017-02-14 13:27:23 -08006534 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006535 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006536 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006537 }
6538
6539 if (is_volatile) {
6540 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6541 }
6542}
6543
6544void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6545 HandleFieldGet(instruction, instruction->GetFieldInfo());
6546}
6547
6548void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6549 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6550}
6551
6552void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6553 HandleFieldSet(instruction, instruction->GetFieldInfo());
6554}
6555
6556void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006557 HandleFieldSet(instruction,
6558 instruction->GetFieldInfo(),
6559 instruction->GetDexPc(),
6560 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006561}
6562
Alexey Frunze15958152017-02-09 19:08:30 -08006563void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6564 HInstruction* instruction,
6565 Location out,
6566 uint32_t offset,
6567 Location maybe_temp,
6568 ReadBarrierOption read_barrier_option) {
6569 Register out_reg = out.AsRegister<Register>();
6570 if (read_barrier_option == kWithReadBarrier) {
6571 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006572 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6573 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6574 }
Alexey Frunze15958152017-02-09 19:08:30 -08006575 if (kUseBakerReadBarrier) {
6576 // Load with fast path based Baker's read barrier.
6577 // /* HeapReference<Object> */ out = *(out + offset)
6578 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6579 out,
6580 out_reg,
6581 offset,
6582 maybe_temp,
6583 /* needs_null_check */ false);
6584 } else {
6585 // Load with slow path based read barrier.
6586 // Save the value of `out` into `maybe_temp` before overwriting it
6587 // in the following move operation, as we will need it for the
6588 // read barrier below.
6589 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6590 // /* HeapReference<Object> */ out = *(out + offset)
6591 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6592 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6593 }
6594 } else {
6595 // Plain load with no read barrier.
6596 // /* HeapReference<Object> */ out = *(out + offset)
6597 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6598 __ MaybeUnpoisonHeapReference(out_reg);
6599 }
6600}
6601
6602void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6603 HInstruction* instruction,
6604 Location out,
6605 Location obj,
6606 uint32_t offset,
6607 Location maybe_temp,
6608 ReadBarrierOption read_barrier_option) {
6609 Register out_reg = out.AsRegister<Register>();
6610 Register obj_reg = obj.AsRegister<Register>();
6611 if (read_barrier_option == kWithReadBarrier) {
6612 CHECK(kEmitCompilerReadBarrier);
6613 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006614 if (!kBakerReadBarrierThunksEnableForFields) {
6615 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6616 }
Alexey Frunze15958152017-02-09 19:08:30 -08006617 // Load with fast path based Baker's read barrier.
6618 // /* HeapReference<Object> */ out = *(obj + offset)
6619 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6620 out,
6621 obj_reg,
6622 offset,
6623 maybe_temp,
6624 /* needs_null_check */ false);
6625 } else {
6626 // Load with slow path based read barrier.
6627 // /* HeapReference<Object> */ out = *(obj + offset)
6628 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6629 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6630 }
6631 } else {
6632 // Plain load with no read barrier.
6633 // /* HeapReference<Object> */ out = *(obj + offset)
6634 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6635 __ MaybeUnpoisonHeapReference(out_reg);
6636 }
6637}
6638
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006639static inline int GetBakerMarkThunkNumber(Register reg) {
6640 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6641 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6642 return reg - V0;
6643 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6644 return 14 + (reg - S2);
6645 } else if (reg == FP) { // One more.
6646 return 20;
6647 }
6648 LOG(FATAL) << "Unexpected register " << reg;
6649 UNREACHABLE();
6650}
6651
6652static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6653 int num = GetBakerMarkThunkNumber(reg) +
6654 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6655 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6656}
6657
6658static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6659 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6660 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6661}
6662
Alexey Frunze15958152017-02-09 19:08:30 -08006663void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6664 Location root,
6665 Register obj,
6666 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006667 ReadBarrierOption read_barrier_option,
6668 MipsLabel* label_low) {
6669 bool reordering;
6670 if (label_low != nullptr) {
6671 DCHECK_EQ(offset, 0x5678u);
6672 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006673 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006674 if (read_barrier_option == kWithReadBarrier) {
6675 DCHECK(kEmitCompilerReadBarrier);
6676 if (kUseBakerReadBarrier) {
6677 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6678 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006679 if (kBakerReadBarrierThunksEnableForGcRoots) {
6680 // Note that we do not actually check the value of `GetIsGcMarking()`
6681 // to decide whether to mark the loaded GC root or not. Instead, we
6682 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6683 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6684 // vice versa.
6685 //
6686 // We use thunks for the slow path. That thunk checks the reference
6687 // and jumps to the entrypoint if needed.
6688 //
6689 // temp = Thread::Current()->pReadBarrierMarkReg00
6690 // // AKA &art_quick_read_barrier_mark_introspection.
6691 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6692 // if (temp != nullptr) {
6693 // temp = &gc_root_thunk<root_reg>
6694 // root = temp(root)
6695 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006696
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006697 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6698 const int32_t entry_point_offset =
6699 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6700 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6701 int16_t offset_low = Low16Bits(offset);
6702 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6703 // extension in lw.
6704 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6705 Register base = short_offset ? obj : TMP;
6706 // Loading the entrypoint does not require a load acquire since it is only changed when
6707 // threads are suspended or running a checkpoint.
6708 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6709 reordering = __ SetReorder(false);
6710 if (!short_offset) {
6711 DCHECK(!label_low);
6712 __ AddUpper(base, obj, offset_high);
6713 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006714 MipsLabel skip_call;
6715 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006716 if (label_low != nullptr) {
6717 DCHECK(short_offset);
6718 __ Bind(label_low);
6719 }
6720 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6721 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6722 // in delay slot.
6723 if (isR6) {
6724 __ Jialc(T9, thunk_disp);
6725 } else {
6726 __ Addiu(T9, T9, thunk_disp);
6727 __ Jalr(T9);
6728 __ Nop();
6729 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006730 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006731 __ SetReorder(reordering);
6732 } else {
6733 // Note that we do not actually check the value of `GetIsGcMarking()`
6734 // to decide whether to mark the loaded GC root or not. Instead, we
6735 // load into `temp` (T9) the read barrier mark entry point corresponding
6736 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6737 // is false, and vice versa.
6738 //
6739 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6740 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6741 // if (temp != null) {
6742 // root = temp(root)
6743 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006744
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006745 if (label_low != nullptr) {
6746 reordering = __ SetReorder(false);
6747 __ Bind(label_low);
6748 }
6749 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6750 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6751 if (label_low != nullptr) {
6752 __ SetReorder(reordering);
6753 }
6754 static_assert(
6755 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6756 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6757 "have different sizes.");
6758 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6759 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6760 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006761
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006762 // Slow path marking the GC root `root`.
6763 Location temp = Location::RegisterLocation(T9);
6764 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006765 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006766 instruction,
6767 root,
6768 /*entrypoint*/ temp);
6769 codegen_->AddSlowPath(slow_path);
6770
6771 const int32_t entry_point_offset =
6772 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6773 // Loading the entrypoint does not require a load acquire since it is only changed when
6774 // threads are suspended or running a checkpoint.
6775 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6776 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6777 __ Bind(slow_path->GetExitLabel());
6778 }
Alexey Frunze15958152017-02-09 19:08:30 -08006779 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006780 if (label_low != nullptr) {
6781 reordering = __ SetReorder(false);
6782 __ Bind(label_low);
6783 }
Alexey Frunze15958152017-02-09 19:08:30 -08006784 // GC root loaded through a slow path for read barriers other
6785 // than Baker's.
6786 // /* GcRoot<mirror::Object>* */ root = obj + offset
6787 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006788 if (label_low != nullptr) {
6789 __ SetReorder(reordering);
6790 }
Alexey Frunze15958152017-02-09 19:08:30 -08006791 // /* mirror::Object* */ root = root->Read()
6792 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6793 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006794 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006795 if (label_low != nullptr) {
6796 reordering = __ SetReorder(false);
6797 __ Bind(label_low);
6798 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006799 // Plain GC root load with no read barrier.
6800 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6801 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6802 // Note that GC roots are not affected by heap poisoning, thus we
6803 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006804 if (label_low != nullptr) {
6805 __ SetReorder(reordering);
6806 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006807 }
6808}
6809
Alexey Frunze15958152017-02-09 19:08:30 -08006810void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6811 Location ref,
6812 Register obj,
6813 uint32_t offset,
6814 Location temp,
6815 bool needs_null_check) {
6816 DCHECK(kEmitCompilerReadBarrier);
6817 DCHECK(kUseBakerReadBarrier);
6818
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006819 if (kBakerReadBarrierThunksEnableForFields) {
6820 // Note that we do not actually check the value of `GetIsGcMarking()`
6821 // to decide whether to mark the loaded reference or not. Instead, we
6822 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6823 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6824 // vice versa.
6825 //
6826 // We use thunks for the slow path. That thunk checks the reference
6827 // and jumps to the entrypoint if needed. If the holder is not gray,
6828 // it issues a load-load memory barrier and returns to the original
6829 // reference load.
6830 //
6831 // temp = Thread::Current()->pReadBarrierMarkReg00
6832 // // AKA &art_quick_read_barrier_mark_introspection.
6833 // if (temp != nullptr) {
6834 // temp = &field_array_thunk<holder_reg>
6835 // temp()
6836 // }
6837 // not_gray_return_address:
6838 // // If the offset is too large to fit into the lw instruction, we
6839 // // use an adjusted base register (TMP) here. This register
6840 // // receives bits 16 ... 31 of the offset before the thunk invocation
6841 // // and the thunk benefits from it.
6842 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6843 // gray_return_address:
6844
6845 DCHECK(temp.IsInvalid());
6846 bool isR6 = GetInstructionSetFeatures().IsR6();
6847 int16_t offset_low = Low16Bits(offset);
6848 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6849 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6850 bool reordering = __ SetReorder(false);
6851 const int32_t entry_point_offset =
6852 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6853 // There may have or may have not been a null check if the field offset is smaller than
6854 // the page size.
6855 // There must've been a null check in case it's actually a load from an array.
6856 // We will, however, perform an explicit null check in the thunk as it's easier to
6857 // do it than not.
6858 if (instruction->IsArrayGet()) {
6859 DCHECK(!needs_null_check);
6860 }
6861 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6862 // Loading the entrypoint does not require a load acquire since it is only changed when
6863 // threads are suspended or running a checkpoint.
6864 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6865 Register ref_reg = ref.AsRegister<Register>();
6866 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006867 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006868 if (short_offset) {
6869 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006870 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006871 __ Nop(); // In forbidden slot.
6872 __ Jialc(T9, thunk_disp);
6873 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006874 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006875 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6876 __ Jalr(T9);
6877 __ Nop(); // In delay slot.
6878 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006879 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006880 } else {
6881 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006882 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006883 __ Aui(base, obj, offset_high); // In delay slot.
6884 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006885 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006886 } else {
6887 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006888 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006889 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6890 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006891 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006892 __ Addu(base, base, obj); // In delay slot.
6893 }
6894 }
6895 // /* HeapReference<Object> */ ref = *(obj + offset)
6896 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6897 if (needs_null_check) {
6898 MaybeRecordImplicitNullCheck(instruction);
6899 }
6900 __ MaybeUnpoisonHeapReference(ref_reg);
6901 __ SetReorder(reordering);
6902 return;
6903 }
6904
Alexey Frunze15958152017-02-09 19:08:30 -08006905 // /* HeapReference<Object> */ ref = *(obj + offset)
6906 Location no_index = Location::NoLocation();
6907 ScaleFactor no_scale_factor = TIMES_1;
6908 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6909 ref,
6910 obj,
6911 offset,
6912 no_index,
6913 no_scale_factor,
6914 temp,
6915 needs_null_check);
6916}
6917
6918void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6919 Location ref,
6920 Register obj,
6921 uint32_t data_offset,
6922 Location index,
6923 Location temp,
6924 bool needs_null_check) {
6925 DCHECK(kEmitCompilerReadBarrier);
6926 DCHECK(kUseBakerReadBarrier);
6927
6928 static_assert(
6929 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6930 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006931 ScaleFactor scale_factor = TIMES_4;
6932
6933 if (kBakerReadBarrierThunksEnableForArrays) {
6934 // Note that we do not actually check the value of `GetIsGcMarking()`
6935 // to decide whether to mark the loaded reference or not. Instead, we
6936 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6937 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6938 // vice versa.
6939 //
6940 // We use thunks for the slow path. That thunk checks the reference
6941 // and jumps to the entrypoint if needed. If the holder is not gray,
6942 // it issues a load-load memory barrier and returns to the original
6943 // reference load.
6944 //
6945 // temp = Thread::Current()->pReadBarrierMarkReg00
6946 // // AKA &art_quick_read_barrier_mark_introspection.
6947 // if (temp != nullptr) {
6948 // temp = &field_array_thunk<holder_reg>
6949 // temp()
6950 // }
6951 // not_gray_return_address:
6952 // // The element address is pre-calculated in the TMP register before the
6953 // // thunk invocation and the thunk benefits from it.
6954 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6955 // gray_return_address:
6956
6957 DCHECK(temp.IsInvalid());
6958 DCHECK(index.IsValid());
6959 bool reordering = __ SetReorder(false);
6960 const int32_t entry_point_offset =
6961 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6962 // We will not do the explicit null check in the thunk as some form of a null check
6963 // must've been done earlier.
6964 DCHECK(!needs_null_check);
6965 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6966 // Loading the entrypoint does not require a load acquire since it is only changed when
6967 // threads are suspended or running a checkpoint.
6968 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6969 Register ref_reg = ref.AsRegister<Register>();
6970 Register index_reg = index.IsRegisterPair()
6971 ? index.AsRegisterPairLow<Register>()
6972 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006973 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006974 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006975 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006976 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6977 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006978 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006979 } else {
6980 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006981 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006982 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6983 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006984 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006985 __ Addu(TMP, TMP, obj); // In delay slot.
6986 }
6987 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6988 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6989 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6990 __ MaybeUnpoisonHeapReference(ref_reg);
6991 __ SetReorder(reordering);
6992 return;
6993 }
6994
Alexey Frunze15958152017-02-09 19:08:30 -08006995 // /* HeapReference<Object> */ ref =
6996 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006997 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6998 ref,
6999 obj,
7000 data_offset,
7001 index,
7002 scale_factor,
7003 temp,
7004 needs_null_check);
7005}
7006
7007void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7008 Location ref,
7009 Register obj,
7010 uint32_t offset,
7011 Location index,
7012 ScaleFactor scale_factor,
7013 Location temp,
7014 bool needs_null_check,
7015 bool always_update_field) {
7016 DCHECK(kEmitCompilerReadBarrier);
7017 DCHECK(kUseBakerReadBarrier);
7018
7019 // In slow path based read barriers, the read barrier call is
7020 // inserted after the original load. However, in fast path based
7021 // Baker's read barriers, we need to perform the load of
7022 // mirror::Object::monitor_ *before* the original reference load.
7023 // This load-load ordering is required by the read barrier.
7024 // The fast path/slow path (for Baker's algorithm) should look like:
7025 //
7026 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7027 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7028 // HeapReference<Object> ref = *src; // Original reference load.
7029 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7030 // if (is_gray) {
7031 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7032 // }
7033 //
7034 // Note: the original implementation in ReadBarrier::Barrier is
7035 // slightly more complex as it performs additional checks that we do
7036 // not do here for performance reasons.
7037
7038 Register ref_reg = ref.AsRegister<Register>();
7039 Register temp_reg = temp.AsRegister<Register>();
7040 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7041
7042 // /* int32_t */ monitor = obj->monitor_
7043 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7044 if (needs_null_check) {
7045 MaybeRecordImplicitNullCheck(instruction);
7046 }
7047 // /* LockWord */ lock_word = LockWord(monitor)
7048 static_assert(sizeof(LockWord) == sizeof(int32_t),
7049 "art::LockWord and int32_t have different sizes.");
7050
7051 __ Sync(0); // Barrier to prevent load-load reordering.
7052
7053 // The actual reference load.
7054 if (index.IsValid()) {
7055 // Load types involving an "index": ArrayGet,
7056 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7057 // intrinsics.
7058 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7059 if (index.IsConstant()) {
7060 size_t computed_offset =
7061 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7062 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7063 } else {
7064 // Handle the special case of the
7065 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7066 // intrinsics, which use a register pair as index ("long
7067 // offset"), of which only the low part contains data.
7068 Register index_reg = index.IsRegisterPair()
7069 ? index.AsRegisterPairLow<Register>()
7070 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007071 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007072 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7073 }
7074 } else {
7075 // /* HeapReference<Object> */ ref = *(obj + offset)
7076 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7077 }
7078
7079 // Object* ref = ref_addr->AsMirrorPtr()
7080 __ MaybeUnpoisonHeapReference(ref_reg);
7081
7082 // Slow path marking the object `ref` when it is gray.
7083 SlowPathCodeMIPS* slow_path;
7084 if (always_update_field) {
7085 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7086 // of the form `obj + field_offset`, where `obj` is a register and
7087 // `field_offset` is a register pair (of which only the lower half
7088 // is used). Thus `offset` and `scale_factor` above are expected
7089 // to be null in this code path.
7090 DCHECK_EQ(offset, 0u);
7091 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007092 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007093 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7094 ref,
7095 obj,
7096 /* field_offset */ index,
7097 temp_reg);
7098 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007099 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007100 }
7101 AddSlowPath(slow_path);
7102
7103 // if (rb_state == ReadBarrier::GrayState())
7104 // ref = ReadBarrier::Mark(ref);
7105 // Given the numeric representation, it's enough to check the low bit of the
7106 // rb_state. We do that by shifting the bit into the sign bit (31) and
7107 // performing a branch on less than zero.
7108 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7109 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7110 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7111 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7112 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7113 __ Bind(slow_path->GetExitLabel());
7114}
7115
7116void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7117 Location out,
7118 Location ref,
7119 Location obj,
7120 uint32_t offset,
7121 Location index) {
7122 DCHECK(kEmitCompilerReadBarrier);
7123
7124 // Insert a slow path based read barrier *after* the reference load.
7125 //
7126 // If heap poisoning is enabled, the unpoisoning of the loaded
7127 // reference will be carried out by the runtime within the slow
7128 // path.
7129 //
7130 // Note that `ref` currently does not get unpoisoned (when heap
7131 // poisoning is enabled), which is alright as the `ref` argument is
7132 // not used by the artReadBarrierSlow entry point.
7133 //
7134 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007135 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007136 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7137 AddSlowPath(slow_path);
7138
7139 __ B(slow_path->GetEntryLabel());
7140 __ Bind(slow_path->GetExitLabel());
7141}
7142
7143void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7144 Location out,
7145 Location ref,
7146 Location obj,
7147 uint32_t offset,
7148 Location index) {
7149 if (kEmitCompilerReadBarrier) {
7150 // Baker's read barriers shall be handled by the fast path
7151 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7152 DCHECK(!kUseBakerReadBarrier);
7153 // If heap poisoning is enabled, unpoisoning will be taken care of
7154 // by the runtime within the slow path.
7155 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7156 } else if (kPoisonHeapReferences) {
7157 __ UnpoisonHeapReference(out.AsRegister<Register>());
7158 }
7159}
7160
7161void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7162 Location out,
7163 Location root) {
7164 DCHECK(kEmitCompilerReadBarrier);
7165
7166 // Insert a slow path based read barrier *after* the GC root load.
7167 //
7168 // Note that GC roots are not affected by heap poisoning, so we do
7169 // not need to do anything special for this here.
7170 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007171 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007172 AddSlowPath(slow_path);
7173
7174 __ B(slow_path->GetEntryLabel());
7175 __ Bind(slow_path->GetExitLabel());
7176}
7177
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007178void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007179 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7180 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007181 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007182 switch (type_check_kind) {
7183 case TypeCheckKind::kExactCheck:
7184 case TypeCheckKind::kAbstractClassCheck:
7185 case TypeCheckKind::kClassHierarchyCheck:
7186 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007187 call_kind =
7188 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007189 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007190 break;
7191 case TypeCheckKind::kArrayCheck:
7192 case TypeCheckKind::kUnresolvedCheck:
7193 case TypeCheckKind::kInterfaceCheck:
7194 call_kind = LocationSummary::kCallOnSlowPath;
7195 break;
7196 }
7197
Vladimir Markoca6fff82017-10-03 14:49:14 +01007198 LocationSummary* locations =
7199 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007200 if (baker_read_barrier_slow_path) {
7201 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7202 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007203 locations->SetInAt(0, Location::RequiresRegister());
7204 locations->SetInAt(1, Location::RequiresRegister());
7205 // The output does overlap inputs.
7206 // Note that TypeCheckSlowPathMIPS uses this register too.
7207 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007208 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007209}
7210
7211void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007212 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007213 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007214 Location obj_loc = locations->InAt(0);
7215 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007216 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007217 Location out_loc = locations->Out();
7218 Register out = out_loc.AsRegister<Register>();
7219 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7220 DCHECK_LE(num_temps, 1u);
7221 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007222 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7223 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7224 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7225 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007226 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007227 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007228
7229 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007230 // Avoid this check if we know `obj` is not null.
7231 if (instruction->MustDoNullCheck()) {
7232 __ Move(out, ZERO);
7233 __ Beqz(obj, &done);
7234 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007235
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007236 switch (type_check_kind) {
7237 case TypeCheckKind::kExactCheck: {
7238 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007239 GenerateReferenceLoadTwoRegisters(instruction,
7240 out_loc,
7241 obj_loc,
7242 class_offset,
7243 maybe_temp_loc,
7244 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007245 // Classes must be equal for the instanceof to succeed.
7246 __ Xor(out, out, cls);
7247 __ Sltiu(out, out, 1);
7248 break;
7249 }
7250
7251 case TypeCheckKind::kAbstractClassCheck: {
7252 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007253 GenerateReferenceLoadTwoRegisters(instruction,
7254 out_loc,
7255 obj_loc,
7256 class_offset,
7257 maybe_temp_loc,
7258 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007259 // If the class is abstract, we eagerly fetch the super class of the
7260 // object to avoid doing a comparison we know will fail.
7261 MipsLabel loop;
7262 __ Bind(&loop);
7263 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007264 GenerateReferenceLoadOneRegister(instruction,
7265 out_loc,
7266 super_offset,
7267 maybe_temp_loc,
7268 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007269 // If `out` is null, we use it for the result, and jump to `done`.
7270 __ Beqz(out, &done);
7271 __ Bne(out, cls, &loop);
7272 __ LoadConst32(out, 1);
7273 break;
7274 }
7275
7276 case TypeCheckKind::kClassHierarchyCheck: {
7277 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007278 GenerateReferenceLoadTwoRegisters(instruction,
7279 out_loc,
7280 obj_loc,
7281 class_offset,
7282 maybe_temp_loc,
7283 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007284 // Walk over the class hierarchy to find a match.
7285 MipsLabel loop, success;
7286 __ Bind(&loop);
7287 __ Beq(out, cls, &success);
7288 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007289 GenerateReferenceLoadOneRegister(instruction,
7290 out_loc,
7291 super_offset,
7292 maybe_temp_loc,
7293 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007294 __ Bnez(out, &loop);
7295 // If `out` is null, we use it for the result, and jump to `done`.
7296 __ B(&done);
7297 __ Bind(&success);
7298 __ LoadConst32(out, 1);
7299 break;
7300 }
7301
7302 case TypeCheckKind::kArrayObjectCheck: {
7303 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007304 GenerateReferenceLoadTwoRegisters(instruction,
7305 out_loc,
7306 obj_loc,
7307 class_offset,
7308 maybe_temp_loc,
7309 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007310 // Do an exact check.
7311 MipsLabel success;
7312 __ Beq(out, cls, &success);
7313 // Otherwise, we need to check that the object's class is a non-primitive array.
7314 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007315 GenerateReferenceLoadOneRegister(instruction,
7316 out_loc,
7317 component_offset,
7318 maybe_temp_loc,
7319 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007320 // If `out` is null, we use it for the result, and jump to `done`.
7321 __ Beqz(out, &done);
7322 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7323 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7324 __ Sltiu(out, out, 1);
7325 __ B(&done);
7326 __ Bind(&success);
7327 __ LoadConst32(out, 1);
7328 break;
7329 }
7330
7331 case TypeCheckKind::kArrayCheck: {
7332 // No read barrier since the slow path will retry upon failure.
7333 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007334 GenerateReferenceLoadTwoRegisters(instruction,
7335 out_loc,
7336 obj_loc,
7337 class_offset,
7338 maybe_temp_loc,
7339 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007340 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007341 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7342 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007343 codegen_->AddSlowPath(slow_path);
7344 __ Bne(out, cls, slow_path->GetEntryLabel());
7345 __ LoadConst32(out, 1);
7346 break;
7347 }
7348
7349 case TypeCheckKind::kUnresolvedCheck:
7350 case TypeCheckKind::kInterfaceCheck: {
7351 // Note that we indeed only call on slow path, but we always go
7352 // into the slow path for the unresolved and interface check
7353 // cases.
7354 //
7355 // We cannot directly call the InstanceofNonTrivial runtime
7356 // entry point without resorting to a type checking slow path
7357 // here (i.e. by calling InvokeRuntime directly), as it would
7358 // require to assign fixed registers for the inputs of this
7359 // HInstanceOf instruction (following the runtime calling
7360 // convention), which might be cluttered by the potential first
7361 // read barrier emission at the beginning of this method.
7362 //
7363 // TODO: Introduce a new runtime entry point taking the object
7364 // to test (instead of its class) as argument, and let it deal
7365 // with the read barrier issues. This will let us refactor this
7366 // case of the `switch` code as it was previously (with a direct
7367 // call to the runtime not using a type checking slow path).
7368 // This should also be beneficial for the other cases above.
7369 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007370 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7371 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007372 codegen_->AddSlowPath(slow_path);
7373 __ B(slow_path->GetEntryLabel());
7374 break;
7375 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007376 }
7377
7378 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007379
7380 if (slow_path != nullptr) {
7381 __ Bind(slow_path->GetExitLabel());
7382 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007383}
7384
7385void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007386 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007387 locations->SetOut(Location::ConstantLocation(constant));
7388}
7389
7390void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7391 // Will be generated at use site.
7392}
7393
7394void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007395 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007396 locations->SetOut(Location::ConstantLocation(constant));
7397}
7398
7399void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7400 // Will be generated at use site.
7401}
7402
7403void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7404 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7405 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7406}
7407
7408void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7409 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007410 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007411 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007412 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007413}
7414
7415void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7416 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7417 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007418 Location receiver = invoke->GetLocations()->InAt(0);
7419 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007420 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007421
7422 // Set the hidden argument.
7423 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7424 invoke->GetDexMethodIndex());
7425
7426 // temp = object->GetClass();
7427 if (receiver.IsStackSlot()) {
7428 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7429 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7430 } else {
7431 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7432 }
7433 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007434 // Instead of simply (possibly) unpoisoning `temp` here, we should
7435 // emit a read barrier for the previous class reference load.
7436 // However this is not required in practice, as this is an
7437 // intermediate/temporary reference and because the current
7438 // concurrent copying collector keeps the from-space memory
7439 // intact/accessible until the end of the marking phase (the
7440 // concurrent copying collector may not in the future).
7441 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007442 __ LoadFromOffset(kLoadWord, temp, temp,
7443 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7444 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007445 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007446 // temp = temp->GetImtEntryAt(method_offset);
7447 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7448 // T9 = temp->GetEntryPoint();
7449 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7450 // T9();
7451 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007452 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007453 DCHECK(!codegen_->IsLeafMethod());
7454 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7455}
7456
7457void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007458 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7459 if (intrinsic.TryDispatch(invoke)) {
7460 return;
7461 }
7462
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007463 HandleInvoke(invoke);
7464}
7465
7466void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007467 // Explicit clinit checks triggered by static invokes must have been pruned by
7468 // art::PrepareForRegisterAllocation.
7469 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007470
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007471 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007472 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7473 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007474
Chris Larsen701566a2015-10-27 15:29:13 -07007475 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7476 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007477 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7478 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7479 }
Chris Larsen701566a2015-10-27 15:29:13 -07007480 return;
7481 }
7482
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007483 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007484
7485 // Add the extra input register if either the dex cache array base register
7486 // or the PC-relative base register for accessing literals is needed.
7487 if (has_extra_input) {
7488 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7489 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007490}
7491
Orion Hodsonac141392017-01-13 11:53:47 +00007492void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7493 HandleInvoke(invoke);
7494}
7495
7496void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7497 codegen_->GenerateInvokePolymorphicCall(invoke);
7498}
7499
Chris Larsen701566a2015-10-27 15:29:13 -07007500static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007501 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007502 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7503 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007504 return true;
7505 }
7506 return false;
7507}
7508
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007509HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007510 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007511 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007512 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007513 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007514 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007515 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007516 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007517 case HLoadString::LoadKind::kJitTableAddress:
7518 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007519 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007520 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007521 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007522 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007523 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007524 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007525}
7526
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007527HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7528 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007529 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007530 case HLoadClass::LoadKind::kInvalid:
7531 LOG(FATAL) << "UNREACHABLE";
7532 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007533 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007534 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007535 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007536 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007537 case HLoadClass::LoadKind::kBssEntry:
7538 DCHECK(!Runtime::Current()->UseJitCompilation());
7539 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007540 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007541 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007542 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007543 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007544 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007545 break;
7546 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007547 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007548}
7549
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007550Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7551 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007552 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007553 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007554 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7555 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7556 if (!invoke->GetLocations()->Intrinsified()) {
7557 return location.AsRegister<Register>();
7558 }
7559 // For intrinsics we allow any location, so it may be on the stack.
7560 if (!location.IsRegister()) {
7561 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7562 return temp;
7563 }
7564 // For register locations, check if the register was saved. If so, get it from the stack.
7565 // Note: There is a chance that the register was saved but not overwritten, so we could
7566 // save one load. However, since this is just an intrinsic slow path we prefer this
7567 // simple and more robust approach rather that trying to determine if that's the case.
7568 SlowPathCode* slow_path = GetCurrentSlowPath();
7569 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7570 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7571 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7572 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7573 return temp;
7574 }
7575 return location.AsRegister<Register>();
7576}
7577
Vladimir Markodc151b22015-10-15 18:02:30 +01007578HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7579 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007580 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007581 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007582}
7583
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007584void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7585 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007586 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007587 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007588 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7589 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007590 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007591 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7592 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007593 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7594 : ZERO;
7595
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007596 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007597 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007598 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007599 uint32_t offset =
7600 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007601 __ LoadFromOffset(kLoadWord,
7602 temp.AsRegister<Register>(),
7603 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007604 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007605 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007606 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007607 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007608 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007609 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007610 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7611 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007612 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7613 PcRelativePatchInfo* info_low =
7614 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007615 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007616 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7617 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007618 break;
7619 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007620 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7621 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7622 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007623 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007624 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007625 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007626 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7627 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007628 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007629 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7630 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007631 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007632 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007633 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7634 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7635 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007636 }
7637 }
7638
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007639 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007640 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007641 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007642 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007643 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7644 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007645 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007646 T9,
7647 callee_method.AsRegister<Register>(),
7648 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007649 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007650 // T9()
7651 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007652 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007653 break;
7654 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007655 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7656
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007657 DCHECK(!IsLeafMethod());
7658}
7659
7660void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007661 // Explicit clinit checks triggered by static invokes must have been pruned by
7662 // art::PrepareForRegisterAllocation.
7663 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007664
7665 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7666 return;
7667 }
7668
7669 LocationSummary* locations = invoke->GetLocations();
7670 codegen_->GenerateStaticOrDirectCall(invoke,
7671 locations->HasTemps()
7672 ? locations->GetTemp(0)
7673 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007674}
7675
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007676void CodeGeneratorMIPS::GenerateVirtualCall(
7677 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007678 // Use the calling convention instead of the location of the receiver, as
7679 // intrinsics may have put the receiver in a different register. In the intrinsics
7680 // slow path, the arguments have been moved to the right place, so here we are
7681 // guaranteed that the receiver is the first register of the calling convention.
7682 InvokeDexCallingConvention calling_convention;
7683 Register receiver = calling_convention.GetRegisterAt(0);
7684
Chris Larsen3acee732015-11-18 13:31:08 -08007685 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007686 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7687 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7688 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007689 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007690
7691 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007692 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007693 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007694 // Instead of simply (possibly) unpoisoning `temp` here, we should
7695 // emit a read barrier for the previous class reference load.
7696 // However this is not required in practice, as this is an
7697 // intermediate/temporary reference and because the current
7698 // concurrent copying collector keeps the from-space memory
7699 // intact/accessible until the end of the marking phase (the
7700 // concurrent copying collector may not in the future).
7701 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007702 // temp = temp->GetMethodAt(method_offset);
7703 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7704 // T9 = temp->GetEntryPoint();
7705 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7706 // T9();
7707 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007708 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007709 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007710}
7711
7712void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7713 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7714 return;
7715 }
7716
7717 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007718 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007719}
7720
7721void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007722 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007723 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007724 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007725 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7726 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007727 return;
7728 }
Vladimir Marko41559982017-01-06 14:04:23 +00007729 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007730 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007731 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007732 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7733 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007734 ? LocationSummary::kCallOnSlowPath
7735 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007736 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007737 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7738 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7739 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007740 switch (load_kind) {
7741 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007742 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007743 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007744 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007745 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007746 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007747 break;
7748 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007749 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007750 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7751 codegen_->ClobberRA();
7752 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007753 break;
7754 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007755 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007756 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007757 locations->SetInAt(0, Location::RequiresRegister());
7758 break;
7759 default:
7760 break;
7761 }
7762 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007763 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7764 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7765 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007766 RegisterSet caller_saves = RegisterSet::Empty();
7767 InvokeRuntimeCallingConvention calling_convention;
7768 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7769 locations->SetCustomSlowPathCallerSaves(caller_saves);
7770 } else {
7771 // For non-Baker read barriers we have a temp-clobbering call.
7772 }
7773 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007774}
7775
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007776// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7777// move.
7778void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007779 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007780 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007781 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007782 return;
7783 }
Vladimir Marko41559982017-01-06 14:04:23 +00007784 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007785
Vladimir Marko41559982017-01-06 14:04:23 +00007786 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007787 Location out_loc = locations->Out();
7788 Register out = out_loc.AsRegister<Register>();
7789 Register base_or_current_method_reg;
7790 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007791 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007792 switch (load_kind) {
7793 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007794 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007795 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007796 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007797 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007798 base_or_current_method_reg =
7799 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007800 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007801 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007802 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007803 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7804 break;
7805 default:
7806 base_or_current_method_reg = ZERO;
7807 break;
7808 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007809
Alexey Frunze15958152017-02-09 19:08:30 -08007810 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7811 ? kWithoutReadBarrier
7812 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007813 bool generate_null_check = false;
7814 switch (load_kind) {
7815 case HLoadClass::LoadKind::kReferrersClass: {
7816 DCHECK(!cls->CanCallRuntime());
7817 DCHECK(!cls->MustGenerateClinitCheck());
7818 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7819 GenerateGcRootFieldLoad(cls,
7820 out_loc,
7821 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007822 ArtMethod::DeclaringClassOffset().Int32Value(),
7823 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007824 break;
7825 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007826 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007827 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007828 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007829 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007830 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007831 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7832 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007833 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7834 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007835 base_or_current_method_reg);
7836 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007837 break;
7838 }
7839 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007840 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007841 uint32_t address = dchecked_integral_cast<uint32_t>(
7842 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7843 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007844 if (isR6 || !has_irreducible_loops) {
7845 __ LoadLiteral(out,
7846 base_or_current_method_reg,
7847 codegen_->DeduplicateBootImageAddressLiteral(address));
7848 } else {
7849 __ LoadConst32(out, address);
7850 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007851 break;
7852 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007853 case HLoadClass::LoadKind::kBootImageClassTable: {
7854 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7855 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7856 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7857 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7858 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7859 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7860 out,
7861 base_or_current_method_reg);
7862 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7863 // Extract the reference from the slot data, i.e. clear the hash bits.
7864 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7865 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7866 if (masked_hash != 0) {
7867 __ Addiu(out, out, -masked_hash);
7868 }
7869 break;
7870 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007871 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00007872 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
7873 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007874 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7875 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007876 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007877 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007878 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007879 GenerateGcRootFieldLoad(cls,
7880 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007881 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007882 /* placeholder */ 0x5678,
7883 read_barrier_option,
7884 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007885 generate_null_check = true;
7886 break;
7887 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007888 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007889 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7890 cls->GetTypeIndex(),
7891 cls->GetClass());
7892 bool reordering = __ SetReorder(false);
7893 __ Bind(&info->high_label);
7894 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007895 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007896 GenerateGcRootFieldLoad(cls,
7897 out_loc,
7898 out,
7899 /* placeholder */ 0x5678,
7900 read_barrier_option,
7901 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007902 break;
7903 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007904 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007905 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007906 LOG(FATAL) << "UNREACHABLE";
7907 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007908 }
7909
7910 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7911 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007912 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00007913 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007914 codegen_->AddSlowPath(slow_path);
7915 if (generate_null_check) {
7916 __ Beqz(out, slow_path->GetEntryLabel());
7917 }
7918 if (cls->MustGenerateClinitCheck()) {
7919 GenerateClassInitializationCheck(slow_path, out);
7920 } else {
7921 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007922 }
7923 }
7924}
7925
7926static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007927 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007928}
7929
7930void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7931 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007932 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007933 locations->SetOut(Location::RequiresRegister());
7934}
7935
7936void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7937 Register out = load->GetLocations()->Out().AsRegister<Register>();
7938 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7939}
7940
7941void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007942 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007943}
7944
7945void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7946 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7947}
7948
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007949void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007950 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007951 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007952 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007953 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007954 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007955 switch (load_kind) {
7956 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007957 case HLoadString::LoadKind::kBootImageAddress:
7958 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007959 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007960 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007961 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007962 break;
7963 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007964 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007965 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7966 codegen_->ClobberRA();
7967 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007968 break;
7969 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007970 FALLTHROUGH_INTENDED;
7971 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007972 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007973 locations->SetInAt(0, Location::RequiresRegister());
7974 break;
7975 default:
7976 break;
7977 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007978 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007979 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007980 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007981 } else {
7982 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007983 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7984 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7985 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007986 RegisterSet caller_saves = RegisterSet::Empty();
7987 InvokeRuntimeCallingConvention calling_convention;
7988 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7989 locations->SetCustomSlowPathCallerSaves(caller_saves);
7990 } else {
7991 // For non-Baker read barriers we have a temp-clobbering call.
7992 }
7993 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007994 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007995}
7996
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007997// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7998// move.
7999void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008000 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008001 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008002 Location out_loc = locations->Out();
8003 Register out = out_loc.AsRegister<Register>();
8004 Register base_or_current_method_reg;
8005 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008006 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008007 switch (load_kind) {
8008 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008009 case HLoadString::LoadKind::kBootImageAddress:
8010 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008011 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008012 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008013 base_or_current_method_reg =
8014 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008015 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008016 default:
8017 base_or_current_method_reg = ZERO;
8018 break;
8019 }
8020
8021 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008022 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008023 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008024 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008025 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008026 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8027 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008028 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8029 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008030 base_or_current_method_reg);
8031 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008032 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008033 }
8034 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008035 uint32_t address = dchecked_integral_cast<uint32_t>(
8036 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8037 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008038 if (isR6 || !has_irreducible_loops) {
8039 __ LoadLiteral(out,
8040 base_or_current_method_reg,
8041 codegen_->DeduplicateBootImageAddressLiteral(address));
8042 } else {
8043 __ LoadConst32(out, address);
8044 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008045 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008046 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008047 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008048 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008049 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008050 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008051 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8052 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008053 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8054 out,
8055 base_or_current_method_reg);
8056 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8057 return;
8058 }
8059 case HLoadString::LoadKind::kBssEntry: {
8060 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8061 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8062 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8063 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8064 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008065 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008066 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008067 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008068 GenerateGcRootFieldLoad(load,
8069 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008070 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008071 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008072 kCompilerReadBarrierOption,
8073 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008074 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008075 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008076 codegen_->AddSlowPath(slow_path);
8077 __ Beqz(out, slow_path->GetEntryLabel());
8078 __ Bind(slow_path->GetExitLabel());
8079 return;
8080 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008081 case HLoadString::LoadKind::kJitTableAddress: {
8082 CodeGeneratorMIPS::JitPatchInfo* info =
8083 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8084 load->GetStringIndex(),
8085 load->GetString());
8086 bool reordering = __ SetReorder(false);
8087 __ Bind(&info->high_label);
8088 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008089 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008090 GenerateGcRootFieldLoad(load,
8091 out_loc,
8092 out,
8093 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008094 kCompilerReadBarrierOption,
8095 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008096 return;
8097 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008098 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008099 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008100 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008101
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008102 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008103 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008104 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008105 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008106 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008107 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8108 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008109}
8110
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008111void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008112 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008113 locations->SetOut(Location::ConstantLocation(constant));
8114}
8115
8116void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8117 // Will be generated at use site.
8118}
8119
8120void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008121 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8122 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008123 InvokeRuntimeCallingConvention calling_convention;
8124 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8125}
8126
8127void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8128 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008129 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008130 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8131 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008132 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008133 }
8134 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8135}
8136
8137void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8138 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008139 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008140 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008141 case DataType::Type::kInt32:
8142 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008143 locations->SetInAt(0, Location::RequiresRegister());
8144 locations->SetInAt(1, Location::RequiresRegister());
8145 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8146 break;
8147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008148 case DataType::Type::kFloat32:
8149 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008150 locations->SetInAt(0, Location::RequiresFpuRegister());
8151 locations->SetInAt(1, Location::RequiresFpuRegister());
8152 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8153 break;
8154
8155 default:
8156 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8157 }
8158}
8159
8160void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008161 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008162 LocationSummary* locations = instruction->GetLocations();
8163 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8164
8165 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008166 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008167 Register dst = locations->Out().AsRegister<Register>();
8168 Register lhs = locations->InAt(0).AsRegister<Register>();
8169 Register rhs = locations->InAt(1).AsRegister<Register>();
8170
8171 if (isR6) {
8172 __ MulR6(dst, lhs, rhs);
8173 } else {
8174 __ MulR2(dst, lhs, rhs);
8175 }
8176 break;
8177 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008178 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008179 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8180 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8181 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8182 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8183 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8184 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8185
8186 // Extra checks to protect caused by the existance of A1_A2.
8187 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8188 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8189 DCHECK_NE(dst_high, lhs_low);
8190 DCHECK_NE(dst_high, rhs_low);
8191
8192 // A_B * C_D
8193 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8194 // dst_lo: [ low(B*D) ]
8195 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8196
8197 if (isR6) {
8198 __ MulR6(TMP, lhs_high, rhs_low);
8199 __ MulR6(dst_high, lhs_low, rhs_high);
8200 __ Addu(dst_high, dst_high, TMP);
8201 __ MuhuR6(TMP, lhs_low, rhs_low);
8202 __ Addu(dst_high, dst_high, TMP);
8203 __ MulR6(dst_low, lhs_low, rhs_low);
8204 } else {
8205 __ MulR2(TMP, lhs_high, rhs_low);
8206 __ MulR2(dst_high, lhs_low, rhs_high);
8207 __ Addu(dst_high, dst_high, TMP);
8208 __ MultuR2(lhs_low, rhs_low);
8209 __ Mfhi(TMP);
8210 __ Addu(dst_high, dst_high, TMP);
8211 __ Mflo(dst_low);
8212 }
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 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8218 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8219 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008220 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008221 __ MulS(dst, lhs, rhs);
8222 } else {
8223 __ MulD(dst, lhs, rhs);
8224 }
8225 break;
8226 }
8227 default:
8228 LOG(FATAL) << "Unexpected mul type " << type;
8229 }
8230}
8231
8232void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8233 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008234 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008235 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008236 case DataType::Type::kInt32:
8237 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008238 locations->SetInAt(0, Location::RequiresRegister());
8239 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8240 break;
8241
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008242 case DataType::Type::kFloat32:
8243 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008244 locations->SetInAt(0, Location::RequiresFpuRegister());
8245 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8246 break;
8247
8248 default:
8249 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8250 }
8251}
8252
8253void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008254 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008255 LocationSummary* locations = instruction->GetLocations();
8256
8257 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008258 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008259 Register dst = locations->Out().AsRegister<Register>();
8260 Register src = locations->InAt(0).AsRegister<Register>();
8261 __ Subu(dst, ZERO, src);
8262 break;
8263 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008264 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008265 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8266 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8267 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8268 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8269 __ Subu(dst_low, ZERO, src_low);
8270 __ Sltu(TMP, ZERO, dst_low);
8271 __ Subu(dst_high, ZERO, src_high);
8272 __ Subu(dst_high, dst_high, TMP);
8273 break;
8274 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008275 case DataType::Type::kFloat32:
8276 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008277 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8278 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008279 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008280 __ NegS(dst, src);
8281 } else {
8282 __ NegD(dst, src);
8283 }
8284 break;
8285 }
8286 default:
8287 LOG(FATAL) << "Unexpected neg type " << type;
8288 }
8289}
8290
8291void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008292 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8293 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008294 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008295 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008296 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8297 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008298}
8299
8300void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008301 // Note: if heap poisoning is enabled, the entry point takes care
8302 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008303 QuickEntrypointEnum entrypoint =
8304 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8305 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008306 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008307 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008308}
8309
8310void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008311 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8312 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008313 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008314 if (instruction->IsStringAlloc()) {
8315 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8316 } else {
8317 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008318 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008319 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008320}
8321
8322void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008323 // Note: if heap poisoning is enabled, the entry point takes care
8324 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008325 if (instruction->IsStringAlloc()) {
8326 // String is allocated through StringFactory. Call NewEmptyString entry point.
8327 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008328 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008329 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8330 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8331 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008332 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008333 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8334 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008335 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008336 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008337 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008338}
8339
8340void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008341 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008342 locations->SetInAt(0, Location::RequiresRegister());
8343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8344}
8345
8346void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008347 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008348 LocationSummary* locations = instruction->GetLocations();
8349
8350 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008351 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008352 Register dst = locations->Out().AsRegister<Register>();
8353 Register src = locations->InAt(0).AsRegister<Register>();
8354 __ Nor(dst, src, ZERO);
8355 break;
8356 }
8357
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008358 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008359 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8360 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8361 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8362 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8363 __ Nor(dst_high, src_high, ZERO);
8364 __ Nor(dst_low, src_low, ZERO);
8365 break;
8366 }
8367
8368 default:
8369 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8370 }
8371}
8372
8373void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008374 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008375 locations->SetInAt(0, Location::RequiresRegister());
8376 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8377}
8378
8379void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8380 LocationSummary* locations = instruction->GetLocations();
8381 __ Xori(locations->Out().AsRegister<Register>(),
8382 locations->InAt(0).AsRegister<Register>(),
8383 1);
8384}
8385
8386void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008387 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8388 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008389}
8390
Calin Juravle2ae48182016-03-16 14:05:09 +00008391void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8392 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008393 return;
8394 }
8395 Location obj = instruction->GetLocations()->InAt(0);
8396
8397 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008398 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008399}
8400
Calin Juravle2ae48182016-03-16 14:05:09 +00008401void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008402 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008403 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008404
8405 Location obj = instruction->GetLocations()->InAt(0);
8406
8407 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8408}
8409
8410void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008411 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008412}
8413
8414void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8415 HandleBinaryOp(instruction);
8416}
8417
8418void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8419 HandleBinaryOp(instruction);
8420}
8421
8422void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8423 LOG(FATAL) << "Unreachable";
8424}
8425
8426void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008427 if (instruction->GetNext()->IsSuspendCheck() &&
8428 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8429 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8430 // The back edge will generate the suspend check.
8431 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8432 }
8433
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008434 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8435}
8436
8437void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008438 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008439 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8440 if (location.IsStackSlot()) {
8441 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8442 } else if (location.IsDoubleStackSlot()) {
8443 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8444 }
8445 locations->SetOut(location);
8446}
8447
8448void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8449 ATTRIBUTE_UNUSED) {
8450 // Nothing to do, the parameter is already at its location.
8451}
8452
8453void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8454 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008455 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008456 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8457}
8458
8459void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8460 ATTRIBUTE_UNUSED) {
8461 // Nothing to do, the method is already at its location.
8462}
8463
8464void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008465 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008466 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008467 locations->SetInAt(i, Location::Any());
8468 }
8469 locations->SetOut(Location::Any());
8470}
8471
8472void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8473 LOG(FATAL) << "Unreachable";
8474}
8475
8476void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008477 DataType::Type type = rem->GetResultType();
8478 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8479 ? LocationSummary::kNoCall
8480 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008481 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008482
8483 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008484 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008485 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008486 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008487 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8488 break;
8489
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008490 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008491 InvokeRuntimeCallingConvention calling_convention;
8492 locations->SetInAt(0, Location::RegisterPairLocation(
8493 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8494 locations->SetInAt(1, Location::RegisterPairLocation(
8495 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8496 locations->SetOut(calling_convention.GetReturnLocation(type));
8497 break;
8498 }
8499
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008500 case DataType::Type::kFloat32:
8501 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008502 InvokeRuntimeCallingConvention calling_convention;
8503 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8504 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8505 locations->SetOut(calling_convention.GetReturnLocation(type));
8506 break;
8507 }
8508
8509 default:
8510 LOG(FATAL) << "Unexpected rem type " << type;
8511 }
8512}
8513
8514void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008515 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008516
8517 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008518 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008519 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008520 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008521 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008522 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008523 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8524 break;
8525 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008526 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008527 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008528 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008529 break;
8530 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008531 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008532 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008533 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008534 break;
8535 }
8536 default:
8537 LOG(FATAL) << "Unexpected rem type " << type;
8538 }
8539}
8540
Igor Murashkind01745e2017-04-05 16:40:31 -07008541void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8542 constructor_fence->SetLocations(nullptr);
8543}
8544
8545void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8546 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8547 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8548}
8549
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008550void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8551 memory_barrier->SetLocations(nullptr);
8552}
8553
8554void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8555 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8556}
8557
8558void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008559 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008560 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008561 locations->SetInAt(0, MipsReturnLocation(return_type));
8562}
8563
8564void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8565 codegen_->GenerateFrameExit();
8566}
8567
8568void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8569 ret->SetLocations(nullptr);
8570}
8571
8572void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8573 codegen_->GenerateFrameExit();
8574}
8575
Alexey Frunze92d90602015-12-18 18:16:36 -08008576void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8577 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008578}
8579
Alexey Frunze92d90602015-12-18 18:16:36 -08008580void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8581 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008582}
8583
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008584void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8585 HandleShift(shl);
8586}
8587
8588void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8589 HandleShift(shl);
8590}
8591
8592void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8593 HandleShift(shr);
8594}
8595
8596void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8597 HandleShift(shr);
8598}
8599
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008600void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8601 HandleBinaryOp(instruction);
8602}
8603
8604void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8605 HandleBinaryOp(instruction);
8606}
8607
8608void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8609 HandleFieldGet(instruction, instruction->GetFieldInfo());
8610}
8611
8612void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8613 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8614}
8615
8616void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8617 HandleFieldSet(instruction, instruction->GetFieldInfo());
8618}
8619
8620void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008621 HandleFieldSet(instruction,
8622 instruction->GetFieldInfo(),
8623 instruction->GetDexPc(),
8624 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008625}
8626
8627void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8628 HUnresolvedInstanceFieldGet* instruction) {
8629 FieldAccessCallingConventionMIPS calling_convention;
8630 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8631 instruction->GetFieldType(),
8632 calling_convention);
8633}
8634
8635void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8636 HUnresolvedInstanceFieldGet* instruction) {
8637 FieldAccessCallingConventionMIPS calling_convention;
8638 codegen_->GenerateUnresolvedFieldAccess(instruction,
8639 instruction->GetFieldType(),
8640 instruction->GetFieldIndex(),
8641 instruction->GetDexPc(),
8642 calling_convention);
8643}
8644
8645void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8646 HUnresolvedInstanceFieldSet* instruction) {
8647 FieldAccessCallingConventionMIPS calling_convention;
8648 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8649 instruction->GetFieldType(),
8650 calling_convention);
8651}
8652
8653void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8654 HUnresolvedInstanceFieldSet* instruction) {
8655 FieldAccessCallingConventionMIPS calling_convention;
8656 codegen_->GenerateUnresolvedFieldAccess(instruction,
8657 instruction->GetFieldType(),
8658 instruction->GetFieldIndex(),
8659 instruction->GetDexPc(),
8660 calling_convention);
8661}
8662
8663void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8664 HUnresolvedStaticFieldGet* instruction) {
8665 FieldAccessCallingConventionMIPS calling_convention;
8666 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8667 instruction->GetFieldType(),
8668 calling_convention);
8669}
8670
8671void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8672 HUnresolvedStaticFieldGet* instruction) {
8673 FieldAccessCallingConventionMIPS calling_convention;
8674 codegen_->GenerateUnresolvedFieldAccess(instruction,
8675 instruction->GetFieldType(),
8676 instruction->GetFieldIndex(),
8677 instruction->GetDexPc(),
8678 calling_convention);
8679}
8680
8681void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8682 HUnresolvedStaticFieldSet* instruction) {
8683 FieldAccessCallingConventionMIPS calling_convention;
8684 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8685 instruction->GetFieldType(),
8686 calling_convention);
8687}
8688
8689void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8690 HUnresolvedStaticFieldSet* instruction) {
8691 FieldAccessCallingConventionMIPS calling_convention;
8692 codegen_->GenerateUnresolvedFieldAccess(instruction,
8693 instruction->GetFieldType(),
8694 instruction->GetFieldIndex(),
8695 instruction->GetDexPc(),
8696 calling_convention);
8697}
8698
8699void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008700 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8701 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008702 // In suspend check slow path, usually there are no caller-save registers at all.
8703 // If SIMD instructions are present, however, we force spilling all live SIMD
8704 // registers in full width (since the runtime only saves/restores lower part).
8705 locations->SetCustomSlowPathCallerSaves(
8706 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008707}
8708
8709void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8710 HBasicBlock* block = instruction->GetBlock();
8711 if (block->GetLoopInformation() != nullptr) {
8712 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8713 // The back edge will generate the suspend check.
8714 return;
8715 }
8716 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8717 // The goto will generate the suspend check.
8718 return;
8719 }
8720 GenerateSuspendCheck(instruction, nullptr);
8721}
8722
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008723void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008724 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8725 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008726 InvokeRuntimeCallingConvention calling_convention;
8727 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8728}
8729
8730void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008731 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008732 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8733}
8734
8735void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008736 DataType::Type input_type = conversion->GetInputType();
8737 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008738 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8739 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008740 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008742 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8743 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008744 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8745 }
8746
8747 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008748 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008749 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8750 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008751 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008752 }
8753
Vladimir Markoca6fff82017-10-03 14:49:14 +01008754 LocationSummary* locations =
8755 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008756
8757 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008758 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008759 locations->SetInAt(0, Location::RequiresFpuRegister());
8760 } else {
8761 locations->SetInAt(0, Location::RequiresRegister());
8762 }
8763
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008764 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008765 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8766 } else {
8767 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8768 }
8769 } else {
8770 InvokeRuntimeCallingConvention calling_convention;
8771
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008772 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008773 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8774 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008775 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008776 locations->SetInAt(0, Location::RegisterPairLocation(
8777 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8778 }
8779
8780 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8781 }
8782}
8783
8784void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8785 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008786 DataType::Type result_type = conversion->GetResultType();
8787 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008788 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008789 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008790
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008791 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8792 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008793
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008794 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008795 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8796 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8797 Register src = locations->InAt(0).AsRegister<Register>();
8798
Alexey Frunzea871ef12016-06-27 15:20:11 -07008799 if (dst_low != src) {
8800 __ Move(dst_low, src);
8801 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008802 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008803 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008804 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008805 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008806 ? locations->InAt(0).AsRegisterPairLow<Register>()
8807 : locations->InAt(0).AsRegister<Register>();
8808
8809 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008810 case DataType::Type::kUint8:
8811 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008812 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008813 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008814 if (has_sign_extension) {
8815 __ Seb(dst, src);
8816 } else {
8817 __ Sll(dst, src, 24);
8818 __ Sra(dst, dst, 24);
8819 }
8820 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008821 case DataType::Type::kUint16:
8822 __ Andi(dst, src, 0xFFFF);
8823 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008824 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008825 if (has_sign_extension) {
8826 __ Seh(dst, src);
8827 } else {
8828 __ Sll(dst, src, 16);
8829 __ Sra(dst, dst, 16);
8830 }
8831 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008832 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008833 if (dst != src) {
8834 __ Move(dst, src);
8835 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008836 break;
8837
8838 default:
8839 LOG(FATAL) << "Unexpected type conversion from " << input_type
8840 << " to " << result_type;
8841 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008842 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8843 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008844 if (isR6) {
8845 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8846 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8847 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8848 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8849 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8850 __ Mtc1(src_low, FTMP);
8851 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008852 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008853 __ Cvtsl(dst, FTMP);
8854 } else {
8855 __ Cvtdl(dst, FTMP);
8856 }
8857 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008858 QuickEntrypointEnum entrypoint =
8859 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008860 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008861 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008862 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8863 } else {
8864 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8865 }
8866 }
8867 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008868 Register src = locations->InAt(0).AsRegister<Register>();
8869 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8870 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008871 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008872 __ Cvtsw(dst, FTMP);
8873 } else {
8874 __ Cvtdw(dst, FTMP);
8875 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008876 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008877 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8878 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008879
8880 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8881 // value of the output type if the input is outside of the range after the truncation or
8882 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8883 // results. This matches the desired float/double-to-int/long conversion exactly.
8884 //
8885 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8886 // value when the input is either a NaN or is outside of the range of the output type
8887 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8888 // the same result.
8889 //
8890 // The code takes care of the different behaviors by first comparing the input to the
8891 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8892 // If the input is greater than or equal to the minimum, it procedes to the truncate
8893 // instruction, which will handle such an input the same way irrespective of NAN2008.
8894 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8895 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008896 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008897 if (isR6) {
8898 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8899 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8900 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8901 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8902 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008903
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008904 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008905 __ TruncLS(FTMP, src);
8906 } else {
8907 __ TruncLD(FTMP, src);
8908 }
8909 __ Mfc1(dst_low, FTMP);
8910 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008911 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008912 QuickEntrypointEnum entrypoint =
8913 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008914 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008915 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008916 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8917 } else {
8918 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8919 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008920 }
8921 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008922 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8923 Register dst = locations->Out().AsRegister<Register>();
8924 MipsLabel truncate;
8925 MipsLabel done;
8926
Lena Djokicf4e23a82017-05-09 15:43:45 +02008927 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008928 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008929 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8930 __ LoadConst32(TMP, min_val);
8931 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008932 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008933 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8934 __ LoadConst32(TMP, High32Bits(min_val));
8935 __ Mtc1(ZERO, FTMP);
8936 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008937 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008938
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008939 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008940 __ ColeS(0, FTMP, src);
8941 } else {
8942 __ ColeD(0, FTMP, src);
8943 }
8944 __ Bc1t(0, &truncate);
8945
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008946 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008947 __ CeqS(0, src, src);
8948 } else {
8949 __ CeqD(0, src, src);
8950 }
8951 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8952 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008953
8954 __ B(&done);
8955
8956 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008957 }
8958
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008959 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008960 __ TruncWS(FTMP, src);
8961 } else {
8962 __ TruncWD(FTMP, src);
8963 }
8964 __ Mfc1(dst, FTMP);
8965
Lena Djokicf4e23a82017-05-09 15:43:45 +02008966 if (!isR6) {
8967 __ Bind(&done);
8968 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008969 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008970 } else if (DataType::IsFloatingPointType(result_type) &&
8971 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008972 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8973 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008974 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008975 __ Cvtsd(dst, src);
8976 } else {
8977 __ Cvtds(dst, src);
8978 }
8979 } else {
8980 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8981 << " to " << result_type;
8982 }
8983}
8984
8985void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8986 HandleShift(ushr);
8987}
8988
8989void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8990 HandleShift(ushr);
8991}
8992
8993void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8994 HandleBinaryOp(instruction);
8995}
8996
8997void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8998 HandleBinaryOp(instruction);
8999}
9000
9001void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9002 // Nothing to do, this should be removed during prepare for register allocator.
9003 LOG(FATAL) << "Unreachable";
9004}
9005
9006void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9007 // Nothing to do, this should be removed during prepare for register allocator.
9008 LOG(FATAL) << "Unreachable";
9009}
9010
9011void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009012 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009013}
9014
9015void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009016 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009017}
9018
9019void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009020 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009021}
9022
9023void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009024 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009025}
9026
9027void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009028 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009029}
9030
9031void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009032 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009033}
9034
9035void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009036 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009037}
9038
9039void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009040 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009041}
9042
9043void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009044 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009045}
9046
9047void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009048 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009049}
9050
9051void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009052 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009053}
9054
9055void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009056 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009057}
9058
9059void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009060 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009061}
9062
9063void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009064 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009065}
9066
9067void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009068 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009069}
9070
9071void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009072 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009073}
9074
9075void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009076 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009077}
9078
9079void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009080 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009081}
9082
9083void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009084 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009085}
9086
9087void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009088 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009089}
9090
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009091void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9092 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009093 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009094 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009095 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9096 uint32_t num_entries = switch_instr->GetNumEntries();
9097 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9098 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9099 // instruction to simulate PC-relative addressing when accessing the jump table.
9100 // NAL clobbers RA. Make sure RA is preserved.
9101 codegen_->ClobberRA();
9102 }
9103 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009104}
9105
Alexey Frunze96b66822016-09-10 02:32:44 -07009106void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9107 int32_t lower_bound,
9108 uint32_t num_entries,
9109 HBasicBlock* switch_block,
9110 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009111 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009112 Register temp_reg = TMP;
9113 __ Addiu32(temp_reg, value_reg, -lower_bound);
9114 // Jump to default if index is negative
9115 // Note: We don't check the case that index is positive while value < lower_bound, because in
9116 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9117 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9118
Alexey Frunze96b66822016-09-10 02:32:44 -07009119 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009120 // Jump to successors[0] if value == lower_bound.
9121 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9122 int32_t last_index = 0;
9123 for (; num_entries - last_index > 2; last_index += 2) {
9124 __ Addiu(temp_reg, temp_reg, -2);
9125 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9126 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9127 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9128 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9129 }
9130 if (num_entries - last_index == 2) {
9131 // The last missing case_value.
9132 __ Addiu(temp_reg, temp_reg, -1);
9133 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009134 }
9135
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009136 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009137 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009138 __ B(codegen_->GetLabelOf(default_block));
9139 }
9140}
9141
Alexey Frunze96b66822016-09-10 02:32:44 -07009142void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9143 Register constant_area,
9144 int32_t lower_bound,
9145 uint32_t num_entries,
9146 HBasicBlock* switch_block,
9147 HBasicBlock* default_block) {
9148 // Create a jump table.
9149 std::vector<MipsLabel*> labels(num_entries);
9150 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9151 for (uint32_t i = 0; i < num_entries; i++) {
9152 labels[i] = codegen_->GetLabelOf(successors[i]);
9153 }
9154 JumpTable* table = __ CreateJumpTable(std::move(labels));
9155
9156 // Is the value in range?
9157 __ Addiu32(TMP, value_reg, -lower_bound);
9158 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9159 __ Sltiu(AT, TMP, num_entries);
9160 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9161 } else {
9162 __ LoadConst32(AT, num_entries);
9163 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9164 }
9165
9166 // We are in the range of the table.
9167 // Load the target address from the jump table, indexing by the value.
9168 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009169 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009170 __ Lw(TMP, TMP, 0);
9171 // Compute the absolute target address by adding the table start address
9172 // (the table contains offsets to targets relative to its start).
9173 __ Addu(TMP, TMP, AT);
9174 // And jump.
9175 __ Jr(TMP);
9176 __ NopIfNoReordering();
9177}
9178
9179void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9180 int32_t lower_bound = switch_instr->GetStartValue();
9181 uint32_t num_entries = switch_instr->GetNumEntries();
9182 LocationSummary* locations = switch_instr->GetLocations();
9183 Register value_reg = locations->InAt(0).AsRegister<Register>();
9184 HBasicBlock* switch_block = switch_instr->GetBlock();
9185 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9186
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009187 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009188 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009189 //
9190 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9191 // to access the jump table and it is implemented by changing HPackedSwitch to
9192 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9193 // VisitMipsPackedSwitch()).
9194 //
9195 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9196 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9197 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009198 GenTableBasedPackedSwitch(value_reg,
9199 ZERO,
9200 lower_bound,
9201 num_entries,
9202 switch_block,
9203 default_block);
9204 } else {
9205 GenPackedSwitchWithCompares(value_reg,
9206 lower_bound,
9207 num_entries,
9208 switch_block,
9209 default_block);
9210 }
9211}
9212
9213void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9214 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009215 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009216 locations->SetInAt(0, Location::RequiresRegister());
9217 // Constant area pointer (HMipsComputeBaseMethodAddress).
9218 locations->SetInAt(1, Location::RequiresRegister());
9219}
9220
9221void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9222 int32_t lower_bound = switch_instr->GetStartValue();
9223 uint32_t num_entries = switch_instr->GetNumEntries();
9224 LocationSummary* locations = switch_instr->GetLocations();
9225 Register value_reg = locations->InAt(0).AsRegister<Register>();
9226 Register constant_area = locations->InAt(1).AsRegister<Register>();
9227 HBasicBlock* switch_block = switch_instr->GetBlock();
9228 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9229
9230 // This is an R2-only path. HPackedSwitch has been changed to
9231 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9232 // required to address the jump table relative to PC.
9233 GenTableBasedPackedSwitch(value_reg,
9234 constant_area,
9235 lower_bound,
9236 num_entries,
9237 switch_block,
9238 default_block);
9239}
9240
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009241void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9242 HMipsComputeBaseMethodAddress* insn) {
9243 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009244 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009245 locations->SetOut(Location::RequiresRegister());
9246}
9247
9248void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9249 HMipsComputeBaseMethodAddress* insn) {
9250 LocationSummary* locations = insn->GetLocations();
9251 Register reg = locations->Out().AsRegister<Register>();
9252
9253 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9254
9255 // Generate a dummy PC-relative call to obtain PC.
9256 __ Nal();
9257 // Grab the return address off RA.
9258 __ Move(reg, RA);
9259
9260 // Remember this offset (the obtained PC value) for later use with constant area.
9261 __ BindPcRelBaseLabel();
9262}
9263
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009264void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9265 // The trampoline uses the same calling convention as dex calling conventions,
9266 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9267 // the method_idx.
9268 HandleInvoke(invoke);
9269}
9270
9271void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9272 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9273}
9274
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009275void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9276 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009277 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009278 locations->SetInAt(0, Location::RequiresRegister());
9279 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009280}
9281
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009282void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9283 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009284 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009285 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009286 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009287 __ LoadFromOffset(kLoadWord,
9288 locations->Out().AsRegister<Register>(),
9289 locations->InAt(0).AsRegister<Register>(),
9290 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009291 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009292 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009293 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009294 __ LoadFromOffset(kLoadWord,
9295 locations->Out().AsRegister<Register>(),
9296 locations->InAt(0).AsRegister<Register>(),
9297 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009298 __ LoadFromOffset(kLoadWord,
9299 locations->Out().AsRegister<Register>(),
9300 locations->Out().AsRegister<Register>(),
9301 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009302 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009303}
9304
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009305void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9306 ATTRIBUTE_UNUSED) {
9307 LOG(FATAL) << "Unreachable";
9308}
9309
9310void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9311 ATTRIBUTE_UNUSED) {
9312 LOG(FATAL) << "Unreachable";
9313}
9314
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009315#undef __
9316#undef QUICK_ENTRY_POINT
9317
9318} // namespace mips
9319} // namespace art