blob: 60bbf4c9f030ad0b1822d057b3e1dea262943735 [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"
Vladimir Markoeebb8212018-06-05 14:57:24 +010029#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020031#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070032#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010033#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020034#include "mirror/array-inl.h"
35#include "mirror/class-inl.h"
36#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010037#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020038#include "thread.h"
39#include "utils/assembler.h"
40#include "utils/mips/assembler_mips.h"
41#include "utils/stack_checks.h"
42
43namespace art {
44namespace mips {
45
46static constexpr int kCurrentMethodStackOffset = 0;
47static constexpr Register kMethodRegisterArgument = A0;
48
Alexey Frunze4147fcc2017-06-17 19:57:27 -070049// Flags controlling the use of thunks for Baker read barriers.
50constexpr bool kBakerReadBarrierThunksEnableForFields = true;
51constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
52constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
53
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010054Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020055 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010056 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010057 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010058 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010059 case DataType::Type::kInt8:
60 case DataType::Type::kUint16:
61 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -080062 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020064 return Location::RegisterLocation(V0);
65
Aart Bik66c158e2018-01-31 12:55:04 -080066 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020068 return Location::RegisterPairLocation(V0, V1);
69
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010070 case DataType::Type::kFloat32:
71 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020072 return Location::FpuRegisterLocation(F0);
73
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010074 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020075 return Location();
76 }
77 UNREACHABLE();
78}
79
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010080Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020081 return MipsReturnLocation(type);
82}
83
84Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
85 return Location::RegisterLocation(kMethodRegisterArgument);
86}
87
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010088Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020089 Location next_location;
90
91 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010092 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010093 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010094 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010095 case DataType::Type::kInt8:
96 case DataType::Type::kUint16:
97 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010098 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020099 uint32_t gp_index = gp_index_++;
100 if (gp_index < calling_convention.GetNumberOfRegisters()) {
101 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
102 } else {
103 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
104 next_location = Location::StackSlot(stack_offset);
105 }
106 break;
107 }
108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100109 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200110 uint32_t gp_index = gp_index_;
111 gp_index_ += 2;
112 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800113 Register reg = calling_convention.GetRegisterAt(gp_index);
114 if (reg == A1 || reg == A3) {
115 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200116 gp_index++;
117 }
118 Register low_even = calling_convention.GetRegisterAt(gp_index);
119 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
120 DCHECK_EQ(low_even + 1, high_odd);
121 next_location = Location::RegisterPairLocation(low_even, high_odd);
122 } else {
123 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
124 next_location = Location::DoubleStackSlot(stack_offset);
125 }
126 break;
127 }
128
129 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
130 // will take up the even/odd pair, while floats are stored in even regs only.
131 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100132 case DataType::Type::kFloat32:
133 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200134 uint32_t float_index = float_index_++;
135 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
136 next_location = Location::FpuRegisterLocation(
137 calling_convention.GetFpuRegisterAt(float_index));
138 } else {
139 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100140 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
141 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200142 }
143 break;
144 }
145
Aart Bik66c158e2018-01-31 12:55:04 -0800146 case DataType::Type::kUint32:
147 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200149 LOG(FATAL) << "Unexpected parameter type " << type;
150 break;
151 }
152
153 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155
156 return next_location;
157}
158
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100159Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200160 return MipsReturnLocation(type);
161}
162
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100163// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
164#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700165#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200166
167class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
168 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000169 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200170
171 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
172 LocationSummary* locations = instruction_->GetLocations();
173 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
174 __ Bind(GetEntryLabel());
175 if (instruction_->CanThrowIntoCatchBlock()) {
176 // Live registers will be restored in the catch block if caught.
177 SaveLiveRegisters(codegen, instruction_->GetLocations());
178 }
179 // We're moving two locations to locations that could overlap, so we need a parallel
180 // move resolver.
181 InvokeRuntimeCallingConvention calling_convention;
182 codegen->EmitParallelMoves(locations->InAt(0),
183 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100184 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200185 locations->InAt(1),
186 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100187 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100188 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
189 ? kQuickThrowStringBounds
190 : kQuickThrowArrayBounds;
191 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100192 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
194 }
195
196 bool IsFatal() const OVERRIDE { return true; }
197
198 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
199
200 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200201 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
202};
203
204class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
205 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000206 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207
208 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
209 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
210 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100211 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200212 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
213 }
214
215 bool IsFatal() const OVERRIDE { return true; }
216
217 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
218
219 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200220 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
221};
222
223class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
224 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100225 LoadClassSlowPathMIPS(HLoadClass* cls, HInstruction* at)
226 : SlowPathCodeMIPS(at), cls_(cls) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100228 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200229 }
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();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100234 const uint32_t dex_pc = instruction_->GetDexPc();
235 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
236 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
237
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200238 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200239 __ Bind(GetEntryLabel());
240 SaveLiveRegisters(codegen, locations);
241
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100242 InvokeRuntimeCallingConvention calling_convention;
243 if (must_resolve_type) {
244 DCHECK(IsSameDexFile(cls_->GetDexFile(), mips_codegen->GetGraph()->GetDexFile()));
245 dex::TypeIndex type_index = cls_->GetTypeIndex();
246 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
247 mips_codegen->InvokeRuntime(kQuickInitializeType, instruction_, dex_pc, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200248 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100249 // If we also must_do_clinit, the resolved type is now in the correct register.
250 } else {
251 DCHECK(must_do_clinit);
252 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
253 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
254 source,
255 cls_->GetType());
256 }
257 if (must_do_clinit) {
258 mips_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
259 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200260 }
261
262 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200263 if (out.IsValid()) {
264 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100265 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700266 mips_codegen->MoveLocation(out,
267 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
268 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200269 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200270 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700271
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200272 __ B(GetExitLabel());
273 }
274
275 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
276
277 private:
278 // The class this slow path will load.
279 HLoadClass* const cls_;
280
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200281 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
282};
283
284class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
285 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000286 explicit LoadStringSlowPathMIPS(HLoadString* instruction)
287 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200288
289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700290 DCHECK(instruction_->IsLoadString());
291 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200292 LocationSummary* locations = instruction_->GetLocations();
293 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000294 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200295 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700296 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200297 __ Bind(GetEntryLabel());
298 SaveLiveRegisters(codegen, locations);
299
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000300 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100301 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200302 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700303
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100304 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200305 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700306 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200308 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000309
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200310 __ B(GetExitLabel());
311 }
312
313 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
314
315 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
317};
318
319class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
320 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
325 __ Bind(GetEntryLabel());
326 if (instruction_->CanThrowIntoCatchBlock()) {
327 // Live registers will be restored in the catch block if caught.
328 SaveLiveRegisters(codegen, instruction_->GetLocations());
329 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100330 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200331 instruction_,
332 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100333 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200334 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
335 }
336
337 bool IsFatal() const OVERRIDE { return true; }
338
339 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
340
341 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200342 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
343};
344
345class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
346 public:
347 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000348 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200349
350 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200351 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200352 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
353 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200354 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100355 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200356 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200357 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200358 if (successor_ == nullptr) {
359 __ B(GetReturnLabel());
360 } else {
361 __ B(mips_codegen->GetLabelOf(successor_));
362 }
363 }
364
365 MipsLabel* GetReturnLabel() {
366 DCHECK(successor_ == nullptr);
367 return &return_label_;
368 }
369
370 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
371
Chris Larsena2045912017-11-02 12:39:54 -0700372 HBasicBlock* GetSuccessor() const {
373 return successor_;
374 }
375
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200376 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200377 // If not null, the block to branch to after the suspend check.
378 HBasicBlock* const successor_;
379
380 // If `successor_` is null, the label to branch to after the suspend check.
381 MipsLabel return_label_;
382
383 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
384};
385
386class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
387 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800388 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
389 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200390
391 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
392 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200393 uint32_t dex_pc = instruction_->GetDexPc();
394 DCHECK(instruction_->IsCheckCast()
395 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
396 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
397
398 __ Bind(GetEntryLabel());
Alexey Frunzedfc30af2018-01-24 16:25:10 -0800399 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800400 SaveLiveRegisters(codegen, locations);
401 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200402
403 // We're moving two locations to locations that could overlap, so we need a parallel
404 // move resolver.
405 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800406 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100408 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800409 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100411 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200412 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100413 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800414 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100415 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200416 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
417 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200418 } else {
419 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800420 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
421 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200422 }
423
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800424 if (!is_fatal_) {
425 RestoreLiveRegisters(codegen, locations);
426 __ B(GetExitLabel());
427 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200428 }
429
430 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
431
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800432 bool IsFatal() const OVERRIDE { return is_fatal_; }
433
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800435 const bool is_fatal_;
436
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200437 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
438};
439
440class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
441 public:
Aart Bik42249c32016-01-07 15:33:50 -0800442 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000443 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200444
445 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800446 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200447 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100448 LocationSummary* locations = instruction_->GetLocations();
449 SaveLiveRegisters(codegen, locations);
450 InvokeRuntimeCallingConvention calling_convention;
451 __ LoadConst32(calling_convention.GetRegisterAt(0),
452 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100453 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100454 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 }
456
457 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
458
459 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200460 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
461};
462
Alexey Frunze15958152017-02-09 19:08:30 -0800463class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
464 public:
465 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
466
467 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
468 LocationSummary* locations = instruction_->GetLocations();
469 __ Bind(GetEntryLabel());
470 SaveLiveRegisters(codegen, locations);
471
472 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100473 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800474 parallel_move.AddMove(
475 locations->InAt(0),
476 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100477 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800478 nullptr);
479 parallel_move.AddMove(
480 locations->InAt(1),
481 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100482 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800483 nullptr);
484 parallel_move.AddMove(
485 locations->InAt(2),
486 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100487 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800488 nullptr);
489 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
490
491 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
492 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
493 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
494 RestoreLiveRegisters(codegen, locations);
495 __ B(GetExitLabel());
496 }
497
498 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
499
500 private:
501 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
502};
503
504// Slow path marking an object reference `ref` during a read
505// barrier. The field `obj.field` in the object `obj` holding this
506// reference does not get updated by this slow path after marking (see
507// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
508//
509// This means that after the execution of this slow path, `ref` will
510// always be up-to-date, but `obj.field` may not; i.e., after the
511// flip, `ref` will be a to-space reference, but `obj.field` will
512// probably still be a from-space reference (unless it gets updated by
513// another thread, or if another thread installed another object
514// reference (different from `ref`) in `obj.field`).
515//
516// If `entrypoint` is a valid location it is assumed to already be
517// holding the entrypoint. The case where the entrypoint is passed in
518// is for the GcRoot read barrier.
519class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
520 public:
521 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
522 Location ref,
523 Location entrypoint = Location::NoLocation())
524 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
525 DCHECK(kEmitCompilerReadBarrier);
526 }
527
528 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
529
530 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
531 LocationSummary* locations = instruction_->GetLocations();
532 Register ref_reg = ref_.AsRegister<Register>();
533 DCHECK(locations->CanCall());
534 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
535 DCHECK(instruction_->IsInstanceFieldGet() ||
536 instruction_->IsStaticFieldGet() ||
537 instruction_->IsArrayGet() ||
538 instruction_->IsArraySet() ||
539 instruction_->IsLoadClass() ||
540 instruction_->IsLoadString() ||
541 instruction_->IsInstanceOf() ||
542 instruction_->IsCheckCast() ||
543 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
544 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
545 << "Unexpected instruction in read barrier marking slow path: "
546 << instruction_->DebugName();
547
548 __ Bind(GetEntryLabel());
549 // No need to save live registers; it's taken care of by the
550 // entrypoint. Also, there is no need to update the stack mask,
551 // as this runtime call will not trigger a garbage collection.
552 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
553 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
554 (S2 <= ref_reg && ref_reg <= S7) ||
555 (ref_reg == FP)) << ref_reg;
556 // "Compact" slow path, saving two moves.
557 //
558 // Instead of using the standard runtime calling convention (input
559 // and output in A0 and V0 respectively):
560 //
561 // A0 <- ref
562 // V0 <- ReadBarrierMark(A0)
563 // ref <- V0
564 //
565 // we just use rX (the register containing `ref`) as input and output
566 // of a dedicated entrypoint:
567 //
568 // rX <- ReadBarrierMarkRegX(rX)
569 //
570 if (entrypoint_.IsValid()) {
571 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
572 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
573 __ Jalr(entrypoint_.AsRegister<Register>());
574 __ NopIfNoReordering();
575 } else {
576 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100577 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800578 // This runtime call does not require a stack map.
579 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
580 instruction_,
581 this,
582 /* direct */ false);
583 }
584 __ B(GetExitLabel());
585 }
586
587 private:
588 // The location (register) of the marked object reference.
589 const Location ref_;
590
591 // The location of the entrypoint if already loaded.
592 const Location entrypoint_;
593
594 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
595};
596
597// Slow path marking an object reference `ref` during a read barrier,
598// and if needed, atomically updating the field `obj.field` in the
599// object `obj` holding this reference after marking (contrary to
600// ReadBarrierMarkSlowPathMIPS above, which never tries to update
601// `obj.field`).
602//
603// This means that after the execution of this slow path, both `ref`
604// and `obj.field` will be up-to-date; i.e., after the flip, both will
605// hold the same to-space reference (unless another thread installed
606// another object reference (different from `ref`) in `obj.field`).
607class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
608 public:
609 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
610 Location ref,
611 Register obj,
612 Location field_offset,
613 Register temp1)
614 : SlowPathCodeMIPS(instruction),
615 ref_(ref),
616 obj_(obj),
617 field_offset_(field_offset),
618 temp1_(temp1) {
619 DCHECK(kEmitCompilerReadBarrier);
620 }
621
622 const char* GetDescription() const OVERRIDE {
623 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
624 }
625
626 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
627 LocationSummary* locations = instruction_->GetLocations();
628 Register ref_reg = ref_.AsRegister<Register>();
629 DCHECK(locations->CanCall());
630 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
631 // This slow path is only used by the UnsafeCASObject intrinsic.
632 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
633 << "Unexpected instruction in read barrier marking and field updating slow path: "
634 << instruction_->DebugName();
635 DCHECK(instruction_->GetLocations()->Intrinsified());
636 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
637 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
638
639 __ Bind(GetEntryLabel());
640
641 // Save the old reference.
642 // Note that we cannot use AT or TMP to save the old reference, as those
643 // are used by the code that follows, but we need the old reference after
644 // the call to the ReadBarrierMarkRegX entry point.
645 DCHECK_NE(temp1_, AT);
646 DCHECK_NE(temp1_, TMP);
647 __ Move(temp1_, ref_reg);
648
649 // No need to save live registers; it's taken care of by the
650 // entrypoint. Also, there is no need to update the stack mask,
651 // as this runtime call will not trigger a garbage collection.
652 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
653 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
654 (S2 <= ref_reg && ref_reg <= S7) ||
655 (ref_reg == FP)) << ref_reg;
656 // "Compact" slow path, saving two moves.
657 //
658 // Instead of using the standard runtime calling convention (input
659 // and output in A0 and V0 respectively):
660 //
661 // A0 <- ref
662 // V0 <- ReadBarrierMark(A0)
663 // ref <- V0
664 //
665 // we just use rX (the register containing `ref`) as input and output
666 // of a dedicated entrypoint:
667 //
668 // rX <- ReadBarrierMarkRegX(rX)
669 //
670 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100671 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800672 // This runtime call does not require a stack map.
673 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
674 instruction_,
675 this,
676 /* direct */ false);
677
678 // If the new reference is different from the old reference,
679 // update the field in the holder (`*(obj_ + field_offset_)`).
680 //
681 // Note that this field could also hold a different object, if
682 // another thread had concurrently changed it. In that case, the
683 // the compare-and-set (CAS) loop below would abort, leaving the
684 // field as-is.
685 MipsLabel done;
686 __ Beq(temp1_, ref_reg, &done);
687
688 // Update the the holder's field atomically. This may fail if
689 // mutator updates before us, but it's OK. This is achieved
690 // using a strong compare-and-set (CAS) operation with relaxed
691 // memory synchronization ordering, where the expected value is
692 // the old reference and the desired value is the new reference.
693
694 // Convenience aliases.
695 Register base = obj_;
696 // The UnsafeCASObject intrinsic uses a register pair as field
697 // offset ("long offset"), of which only the low part contains
698 // data.
699 Register offset = field_offset_.AsRegisterPairLow<Register>();
700 Register expected = temp1_;
701 Register value = ref_reg;
702 Register tmp_ptr = TMP; // Pointer to actual memory.
703 Register tmp = AT; // Value in memory.
704
705 __ Addu(tmp_ptr, base, offset);
706
707 if (kPoisonHeapReferences) {
708 __ PoisonHeapReference(expected);
709 // Do not poison `value` if it is the same register as
710 // `expected`, which has just been poisoned.
711 if (value != expected) {
712 __ PoisonHeapReference(value);
713 }
714 }
715
716 // do {
717 // tmp = [r_ptr] - expected;
718 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
719
720 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
721 MipsLabel loop_head, exit_loop;
722 __ Bind(&loop_head);
723 if (is_r6) {
724 __ LlR6(tmp, tmp_ptr);
725 } else {
726 __ LlR2(tmp, tmp_ptr);
727 }
728 __ Bne(tmp, expected, &exit_loop);
729 __ Move(tmp, value);
730 if (is_r6) {
731 __ ScR6(tmp, tmp_ptr);
732 } else {
733 __ ScR2(tmp, tmp_ptr);
734 }
735 __ Beqz(tmp, &loop_head);
736 __ Bind(&exit_loop);
737
738 if (kPoisonHeapReferences) {
739 __ UnpoisonHeapReference(expected);
740 // Do not unpoison `value` if it is the same register as
741 // `expected`, which has just been unpoisoned.
742 if (value != expected) {
743 __ UnpoisonHeapReference(value);
744 }
745 }
746
747 __ Bind(&done);
748 __ B(GetExitLabel());
749 }
750
751 private:
752 // The location (register) of the marked object reference.
753 const Location ref_;
754 // The register containing the object holding the marked object reference field.
755 const Register obj_;
756 // The location of the offset of the marked reference field within `obj_`.
757 Location field_offset_;
758
759 const Register temp1_;
760
761 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
762};
763
764// Slow path generating a read barrier for a heap reference.
765class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
766 public:
767 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
768 Location out,
769 Location ref,
770 Location obj,
771 uint32_t offset,
772 Location index)
773 : SlowPathCodeMIPS(instruction),
774 out_(out),
775 ref_(ref),
776 obj_(obj),
777 offset_(offset),
778 index_(index) {
779 DCHECK(kEmitCompilerReadBarrier);
780 // If `obj` is equal to `out` or `ref`, it means the initial object
781 // has been overwritten by (or after) the heap object reference load
782 // to be instrumented, e.g.:
783 //
784 // __ LoadFromOffset(kLoadWord, out, out, offset);
785 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
786 //
787 // In that case, we have lost the information about the original
788 // object, and the emitted read barrier cannot work properly.
789 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
790 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
791 }
792
793 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
794 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
795 LocationSummary* locations = instruction_->GetLocations();
796 Register reg_out = out_.AsRegister<Register>();
797 DCHECK(locations->CanCall());
798 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
799 DCHECK(instruction_->IsInstanceFieldGet() ||
800 instruction_->IsStaticFieldGet() ||
801 instruction_->IsArrayGet() ||
802 instruction_->IsInstanceOf() ||
803 instruction_->IsCheckCast() ||
804 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
805 << "Unexpected instruction in read barrier for heap reference slow path: "
806 << instruction_->DebugName();
807
808 __ Bind(GetEntryLabel());
809 SaveLiveRegisters(codegen, locations);
810
811 // We may have to change the index's value, but as `index_` is a
812 // constant member (like other "inputs" of this slow path),
813 // introduce a copy of it, `index`.
814 Location index = index_;
815 if (index_.IsValid()) {
816 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
817 if (instruction_->IsArrayGet()) {
818 // Compute the actual memory offset and store it in `index`.
819 Register index_reg = index_.AsRegister<Register>();
820 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
821 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
822 // We are about to change the value of `index_reg` (see the
823 // calls to art::mips::MipsAssembler::Sll and
824 // art::mips::MipsAssembler::Addiu32 below), but it has
825 // not been saved by the previous call to
826 // art::SlowPathCode::SaveLiveRegisters, as it is a
827 // callee-save register --
828 // art::SlowPathCode::SaveLiveRegisters does not consider
829 // callee-save registers, as it has been designed with the
830 // assumption that callee-save registers are supposed to be
831 // handled by the called function. So, as a callee-save
832 // register, `index_reg` _would_ eventually be saved onto
833 // the stack, but it would be too late: we would have
834 // changed its value earlier. Therefore, we manually save
835 // it here into another freely available register,
836 // `free_reg`, chosen of course among the caller-save
837 // registers (as a callee-save `free_reg` register would
838 // exhibit the same problem).
839 //
840 // Note we could have requested a temporary register from
841 // the register allocator instead; but we prefer not to, as
842 // this is a slow path, and we know we can find a
843 // caller-save register that is available.
844 Register free_reg = FindAvailableCallerSaveRegister(codegen);
845 __ Move(free_reg, index_reg);
846 index_reg = free_reg;
847 index = Location::RegisterLocation(index_reg);
848 } else {
849 // The initial register stored in `index_` has already been
850 // saved in the call to art::SlowPathCode::SaveLiveRegisters
851 // (as it is not a callee-save register), so we can freely
852 // use it.
853 }
854 // Shifting the index value contained in `index_reg` by the scale
855 // factor (2) cannot overflow in practice, as the runtime is
856 // unable to allocate object arrays with a size larger than
857 // 2^26 - 1 (that is, 2^28 - 4 bytes).
858 __ Sll(index_reg, index_reg, TIMES_4);
859 static_assert(
860 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
861 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
862 __ Addiu32(index_reg, index_reg, offset_);
863 } else {
864 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
865 // intrinsics, `index_` is not shifted by a scale factor of 2
866 // (as in the case of ArrayGet), as it is actually an offset
867 // to an object field within an object.
868 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
869 DCHECK(instruction_->GetLocations()->Intrinsified());
870 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
871 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
872 << instruction_->AsInvoke()->GetIntrinsic();
873 DCHECK_EQ(offset_, 0U);
874 DCHECK(index_.IsRegisterPair());
875 // UnsafeGet's offset location is a register pair, the low
876 // part contains the correct offset.
877 index = index_.ToLow();
878 }
879 }
880
881 // We're moving two or three locations to locations that could
882 // overlap, so we need a parallel move resolver.
883 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100884 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800885 parallel_move.AddMove(ref_,
886 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100887 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800888 nullptr);
889 parallel_move.AddMove(obj_,
890 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100891 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800892 nullptr);
893 if (index.IsValid()) {
894 parallel_move.AddMove(index,
895 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100896 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800897 nullptr);
898 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
899 } else {
900 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
901 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
902 }
903 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
904 instruction_,
905 instruction_->GetDexPc(),
906 this);
907 CheckEntrypointTypes<
908 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200909 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100910 calling_convention.GetReturnLocation(DataType::Type::kReference),
911 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800912
913 RestoreLiveRegisters(codegen, locations);
914 __ B(GetExitLabel());
915 }
916
917 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
918
919 private:
920 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
921 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
922 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
923 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
924 if (i != ref &&
925 i != obj &&
926 !codegen->IsCoreCalleeSaveRegister(i) &&
927 !codegen->IsBlockedCoreRegister(i)) {
928 return static_cast<Register>(i);
929 }
930 }
931 // We shall never fail to find a free caller-save register, as
932 // there are more than two core caller-save registers on MIPS
933 // (meaning it is possible to find one which is different from
934 // `ref` and `obj`).
935 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
936 LOG(FATAL) << "Could not find a free caller-save register";
937 UNREACHABLE();
938 }
939
940 const Location out_;
941 const Location ref_;
942 const Location obj_;
943 const uint32_t offset_;
944 // An additional location containing an index to an array.
945 // Only used for HArrayGet and the UnsafeGetObject &
946 // UnsafeGetObjectVolatile intrinsics.
947 const Location index_;
948
949 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
950};
951
952// Slow path generating a read barrier for a GC root.
953class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
954 public:
955 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
956 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
957 DCHECK(kEmitCompilerReadBarrier);
958 }
959
960 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
961 LocationSummary* locations = instruction_->GetLocations();
962 Register reg_out = out_.AsRegister<Register>();
963 DCHECK(locations->CanCall());
964 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
965 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
966 << "Unexpected instruction in read barrier for GC root slow path: "
967 << instruction_->DebugName();
968
969 __ Bind(GetEntryLabel());
970 SaveLiveRegisters(codegen, locations);
971
972 InvokeRuntimeCallingConvention calling_convention;
973 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +0200974 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
975 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100976 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800977 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
978 instruction_,
979 instruction_->GetDexPc(),
980 this);
981 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +0200982 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100983 calling_convention.GetReturnLocation(DataType::Type::kReference),
984 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800985
986 RestoreLiveRegisters(codegen, locations);
987 __ B(GetExitLabel());
988 }
989
990 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
991
992 private:
993 const Location out_;
994 const Location root_;
995
996 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
997};
998
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200999CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001000 const CompilerOptions& compiler_options,
1001 OptimizingCompilerStats* stats)
1002 : CodeGenerator(graph,
1003 kNumberOfCoreRegisters,
1004 kNumberOfFRegisters,
1005 kNumberOfRegisterPairs,
1006 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1007 arraysize(kCoreCalleeSaves)),
1008 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1009 arraysize(kFpuCalleeSaves)),
1010 compiler_options,
1011 stats),
1012 block_labels_(nullptr),
1013 location_builder_(graph, this),
1014 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001015 move_resolver_(graph->GetAllocator(), this),
Vladimir Markoa0431112018-06-25 09:32:54 +01001016 assembler_(graph->GetAllocator(),
1017 compiler_options.GetInstructionSetFeatures()->AsMipsInstructionSetFeatures()),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001018 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001019 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001020 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001021 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001022 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001023 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001024 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001025 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001026 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001027 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1028 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001029 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001030 // Save RA (containing the return address) to mimic Quick.
1031 AddAllocatedRegister(Location::RegisterLocation(RA));
1032}
1033
1034#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001035// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1036#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001037#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001038
1039void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1040 // Ensure that we fix up branches.
1041 __ FinalizeCode();
1042
1043 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001044 StackMapStream* stack_map_stream = GetStackMapStream();
1045 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
David Srbeckyd02b23f2018-05-29 23:27:22 +01001046 uint32_t old_position = stack_map_stream->GetStackMapNativePcOffset(i);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001047 uint32_t new_position = __ GetAdjustedPosition(old_position);
1048 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001049 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001050 }
1051
1052 // Adjust pc offsets for the disassembly information.
1053 if (disasm_info_ != nullptr) {
1054 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1055 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1056 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1057 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1058 it.second.start = __ GetAdjustedPosition(it.second.start);
1059 it.second.end = __ GetAdjustedPosition(it.second.end);
1060 }
1061 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1062 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1063 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1064 }
1065 }
1066
1067 CodeGenerator::Finalize(allocator);
1068}
1069
1070MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1071 return codegen_->GetAssembler();
1072}
1073
1074void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1075 DCHECK_LT(index, moves_.size());
1076 MoveOperands* move = moves_[index];
1077 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1078}
1079
1080void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1081 DCHECK_LT(index, moves_.size());
1082 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001083 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001084 Location loc1 = move->GetDestination();
1085 Location loc2 = move->GetSource();
1086
1087 DCHECK(!loc1.IsConstant());
1088 DCHECK(!loc2.IsConstant());
1089
1090 if (loc1.Equals(loc2)) {
1091 return;
1092 }
1093
1094 if (loc1.IsRegister() && loc2.IsRegister()) {
1095 // Swap 2 GPRs.
1096 Register r1 = loc1.AsRegister<Register>();
1097 Register r2 = loc2.AsRegister<Register>();
1098 __ Move(TMP, r2);
1099 __ Move(r2, r1);
1100 __ Move(r1, TMP);
1101 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001102 if (codegen_->GetGraph()->HasSIMD()) {
1103 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1104 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1105 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001106 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001107 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1108 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1109 if (type == DataType::Type::kFloat32) {
1110 __ MovS(FTMP, f2);
1111 __ MovS(f2, f1);
1112 __ MovS(f1, FTMP);
1113 } else {
1114 DCHECK_EQ(type, DataType::Type::kFloat64);
1115 __ MovD(FTMP, f2);
1116 __ MovD(f2, f1);
1117 __ MovD(f1, FTMP);
1118 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001119 }
1120 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1121 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1122 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001123 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001124 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1125 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001126 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001127 __ Move(TMP, r2);
1128 __ Mfc1(r2, f1);
1129 __ Mtc1(TMP, f1);
1130 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1131 // Swap 2 GPR register pairs.
1132 Register r1 = loc1.AsRegisterPairLow<Register>();
1133 Register r2 = loc2.AsRegisterPairLow<Register>();
1134 __ Move(TMP, r2);
1135 __ Move(r2, r1);
1136 __ Move(r1, TMP);
1137 r1 = loc1.AsRegisterPairHigh<Register>();
1138 r2 = loc2.AsRegisterPairHigh<Register>();
1139 __ Move(TMP, r2);
1140 __ Move(r2, r1);
1141 __ Move(r1, TMP);
1142 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1143 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1144 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001146 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1147 : loc2.AsFpuRegister<FRegister>();
1148 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1149 : loc2.AsRegisterPairLow<Register>();
1150 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1151 : loc2.AsRegisterPairHigh<Register>();
1152 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1153 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1154 // unpredictable and the following mfch1 will fail.
1155 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001156 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001157 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001158 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001159 __ Move(r2_l, TMP);
1160 __ Move(r2_h, AT);
1161 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1162 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1163 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1164 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001165 } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) {
1166 ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
David Brazdilcc0f3112016-01-28 17:14:52 +00001167 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1168 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001169 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1170 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001171 __ Move(TMP, reg);
1172 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1173 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1174 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1175 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1176 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1177 : loc2.AsRegisterPairLow<Register>();
1178 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1179 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001180 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001181 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1182 : loc2.GetHighStackIndex(kMipsWordSize);
1183 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001184 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001185 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001186 __ Move(TMP, reg_h);
1187 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1188 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001189 } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) ||
1190 (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) {
1191 Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2;
1192 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1193 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(fp_loc));
1194 __ LoadQFromOffset(fp_loc.AsFpuRegister<FRegister>(), SP, offset);
1195 __ StoreQToOffset(FTMP, SP, offset);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001196 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1197 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1198 : loc2.AsFpuRegister<FRegister>();
1199 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001200 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001201 __ MovS(FTMP, reg);
1202 __ LoadSFromOffset(reg, SP, offset);
1203 __ StoreSToOffset(FTMP, SP, offset);
1204 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001205 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001206 __ MovD(FTMP, reg);
1207 __ LoadDFromOffset(reg, SP, offset);
1208 __ StoreDToOffset(FTMP, SP, offset);
1209 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210 } else {
1211 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1212 }
1213}
1214
1215void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1216 __ Pop(static_cast<Register>(reg));
1217}
1218
1219void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1220 __ Push(static_cast<Register>(reg));
1221}
1222
1223void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1224 // Allocate a scratch register other than TMP, if available.
1225 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1226 // automatically unspilled when the scratch scope object is destroyed).
1227 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1228 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001229 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001230 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1231 __ LoadFromOffset(kLoadWord,
1232 Register(ensure_scratch.GetRegister()),
1233 SP,
1234 index1 + stack_offset);
1235 __ LoadFromOffset(kLoadWord,
1236 TMP,
1237 SP,
1238 index2 + stack_offset);
1239 __ StoreToOffset(kStoreWord,
1240 Register(ensure_scratch.GetRegister()),
1241 SP,
1242 index2 + stack_offset);
1243 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1244 }
1245}
1246
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001247void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) {
1248 __ LoadQFromOffset(FTMP, SP, index1);
1249 __ LoadQFromOffset(FTMP2, SP, index2);
1250 __ StoreQToOffset(FTMP, SP, index2);
1251 __ StoreQToOffset(FTMP2, SP, index1);
1252}
1253
Alexey Frunze73296a72016-06-03 22:51:46 -07001254void CodeGeneratorMIPS::ComputeSpillMask() {
1255 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1256 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1257 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1258 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1259 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1260 // within the stack frame.
1261 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1262 core_spill_mask_ |= (1 << ZERO);
1263 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001264}
1265
1266bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001267 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001268 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1269 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1270 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001271 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001272}
1273
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001274static dwarf::Reg DWARFReg(Register reg) {
1275 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1276}
1277
1278// TODO: mapping of floating-point registers to DWARF.
1279
1280void CodeGeneratorMIPS::GenerateFrameEntry() {
1281 __ Bind(&frame_entry_label_);
1282
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001283 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01001284 __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
1285 __ Addiu(TMP, TMP, 1);
1286 __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001287 }
1288
Vladimir Marko33bff252017-11-01 14:35:42 +00001289 bool do_overflow_check =
1290 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001291
1292 if (do_overflow_check) {
1293 __ LoadFromOffset(kLoadWord,
1294 ZERO,
1295 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001296 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 RecordPcInfo(nullptr, 0);
1298 }
1299
1300 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001301 CHECK_EQ(fpu_spill_mask_, 0u);
1302 CHECK_EQ(core_spill_mask_, 1u << RA);
1303 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304 return;
1305 }
1306
1307 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001308 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1309 LOG(FATAL) << "Stack frame larger than "
1310 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311 }
1312
1313 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001314
Alexey Frunze73296a72016-06-03 22:51:46 -07001315 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316 __ IncreaseFrameSize(ofs);
1317
Alexey Frunze73296a72016-06-03 22:51:46 -07001318 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1319 Register reg = static_cast<Register>(MostSignificantBit(mask));
1320 mask ^= 1u << reg;
1321 ofs -= kMipsWordSize;
1322 // The ZERO register is only included for alignment.
1323 if (reg != ZERO) {
1324 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001325 __ cfi().RelOffset(DWARFReg(reg), ofs);
1326 }
1327 }
1328
Alexey Frunze73296a72016-06-03 22:51:46 -07001329 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1330 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1331 mask ^= 1u << reg;
1332 ofs -= kMipsDoublewordSize;
1333 __ StoreDToOffset(reg, SP, ofs);
1334 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001335 }
1336
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001337 // Save the current method if we need it. Note that we do not
1338 // do this in HCurrentMethod, as the instruction might have been removed
1339 // in the SSA graph.
1340 if (RequiresCurrentMethod()) {
1341 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1342 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001343
1344 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1345 // Initialize should deoptimize flag to 0.
1346 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1347 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001348}
1349
1350void CodeGeneratorMIPS::GenerateFrameExit() {
1351 __ cfi().RememberState();
1352
1353 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001354 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355
Alexey Frunze73296a72016-06-03 22:51:46 -07001356 // For better instruction scheduling restore RA before other registers.
1357 uint32_t ofs = GetFrameSize();
1358 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1359 Register reg = static_cast<Register>(MostSignificantBit(mask));
1360 mask ^= 1u << reg;
1361 ofs -= kMipsWordSize;
1362 // The ZERO register is only included for alignment.
1363 if (reg != ZERO) {
1364 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001365 __ cfi().Restore(DWARFReg(reg));
1366 }
1367 }
1368
Alexey Frunze73296a72016-06-03 22:51:46 -07001369 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1370 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1371 mask ^= 1u << reg;
1372 ofs -= kMipsDoublewordSize;
1373 __ LoadDFromOffset(reg, SP, ofs);
1374 // TODO: __ cfi().Restore(DWARFReg(reg));
1375 }
1376
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001377 size_t frame_size = GetFrameSize();
1378 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1379 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1380 bool reordering = __ SetReorder(false);
1381 if (exchange) {
1382 __ Jr(RA);
1383 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1384 } else {
1385 __ DecreaseFrameSize(frame_size);
1386 __ Jr(RA);
1387 __ Nop(); // In delay slot.
1388 }
1389 __ SetReorder(reordering);
1390 } else {
1391 __ Jr(RA);
1392 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001393 }
1394
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001395 __ cfi().RestoreState();
1396 __ cfi().DefCFAOffset(GetFrameSize());
1397}
1398
1399void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1400 __ Bind(GetLabelOf(block));
1401}
1402
Lena Djokicca8c2952017-05-29 11:31:46 +02001403VectorRegister VectorRegisterFrom(Location location) {
1404 DCHECK(location.IsFpuRegister());
1405 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1406}
1407
Lena Djokic8098da92017-06-28 12:07:50 +02001408void CodeGeneratorMIPS::MoveLocation(Location destination,
1409 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001410 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 if (source.Equals(destination)) {
1412 return;
1413 }
1414
Lena Djokic8098da92017-06-28 12:07:50 +02001415 if (source.IsConstant()) {
1416 MoveConstant(destination, source.GetConstant());
1417 } else {
1418 if (destination.IsRegister()) {
1419 if (source.IsRegister()) {
1420 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1421 } else if (source.IsFpuRegister()) {
1422 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1423 } else {
1424 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001425 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001426 }
1427 } else if (destination.IsRegisterPair()) {
1428 if (source.IsRegisterPair()) {
1429 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1430 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1431 } else if (source.IsFpuRegister()) {
1432 Register dst_high = destination.AsRegisterPairHigh<Register>();
1433 Register dst_low = destination.AsRegisterPairLow<Register>();
1434 FRegister src = source.AsFpuRegister<FRegister>();
1435 __ Mfc1(dst_low, src);
1436 __ MoveFromFpuHigh(dst_high, src);
1437 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001438 DCHECK(source.IsDoubleStackSlot())
1439 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001440 int32_t off = source.GetStackIndex();
1441 Register r = destination.AsRegisterPairLow<Register>();
1442 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1443 }
1444 } else if (destination.IsFpuRegister()) {
1445 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001446 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001447 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1448 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001450 FRegister dst = destination.AsFpuRegister<FRegister>();
1451 Register src_high = source.AsRegisterPairHigh<Register>();
1452 Register src_low = source.AsRegisterPairLow<Register>();
1453 __ Mtc1(src_low, dst);
1454 __ MoveToFpuHigh(src_high, dst);
1455 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001456 if (GetGraph()->HasSIMD()) {
1457 __ MoveV(VectorRegisterFrom(destination),
1458 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001459 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001461 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1462 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001464 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1465 }
Lena Djokic8098da92017-06-28 12:07:50 +02001466 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001467 } else if (source.IsSIMDStackSlot()) {
1468 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001469 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001470 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001471 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1472 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001474 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1475 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1476 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001477 } else if (destination.IsSIMDStackSlot()) {
1478 if (source.IsFpuRegister()) {
1479 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1480 } else {
1481 DCHECK(source.IsSIMDStackSlot());
1482 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1483 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1484 }
Lena Djokic8098da92017-06-28 12:07:50 +02001485 } else if (destination.IsDoubleStackSlot()) {
1486 int32_t dst_offset = destination.GetStackIndex();
1487 if (source.IsRegisterPair()) {
1488 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1489 } else if (source.IsFpuRegister()) {
1490 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1491 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001492 DCHECK(source.IsDoubleStackSlot())
1493 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001494 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1495 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1496 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1497 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1498 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001499 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001500 DCHECK(destination.IsStackSlot()) << destination;
1501 int32_t dst_offset = destination.GetStackIndex();
1502 if (source.IsRegister()) {
1503 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1504 } else if (source.IsFpuRegister()) {
1505 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1506 } else {
1507 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1508 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1509 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1510 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001511 }
1512 }
1513}
1514
1515void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1516 if (c->IsIntConstant() || c->IsNullConstant()) {
1517 // Move 32 bit constant.
1518 int32_t value = GetInt32ValueOf(c);
1519 if (destination.IsRegister()) {
1520 Register dst = destination.AsRegister<Register>();
1521 __ LoadConst32(dst, value);
1522 } else {
1523 DCHECK(destination.IsStackSlot())
1524 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001525 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001526 }
1527 } else if (c->IsLongConstant()) {
1528 // Move 64 bit constant.
1529 int64_t value = GetInt64ValueOf(c);
1530 if (destination.IsRegisterPair()) {
1531 Register r_h = destination.AsRegisterPairHigh<Register>();
1532 Register r_l = destination.AsRegisterPairLow<Register>();
1533 __ LoadConst64(r_h, r_l, value);
1534 } else {
1535 DCHECK(destination.IsDoubleStackSlot())
1536 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001537 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001538 }
1539 } else if (c->IsFloatConstant()) {
1540 // Move 32 bit float constant.
1541 int32_t value = GetInt32ValueOf(c);
1542 if (destination.IsFpuRegister()) {
1543 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1544 } else {
1545 DCHECK(destination.IsStackSlot())
1546 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001547 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548 }
1549 } else {
1550 // Move 64 bit double constant.
1551 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1552 int64_t value = GetInt64ValueOf(c);
1553 if (destination.IsFpuRegister()) {
1554 FRegister fd = destination.AsFpuRegister<FRegister>();
1555 __ LoadDConst64(fd, value, TMP);
1556 } else {
1557 DCHECK(destination.IsDoubleStackSlot())
1558 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001559 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001560 }
1561 }
1562}
1563
1564void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1565 DCHECK(destination.IsRegister());
1566 Register dst = destination.AsRegister<Register>();
1567 __ LoadConst32(dst, value);
1568}
1569
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001570void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1571 if (location.IsRegister()) {
1572 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001573 } else if (location.IsRegisterPair()) {
1574 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1575 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576 } else {
1577 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1578 }
1579}
1580
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001581template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001582inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1583 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001584 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001585 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001586 const DexFile* dex_file = info.target_dex_file;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001587 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001588 DCHECK(info.label.IsBound());
1589 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001590 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1591 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001592 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1593 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1594 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001595 : __ GetPcRelBaseLabelLocation();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001596 linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001597 }
1598}
1599
Vladimir Marko6fd16062018-06-26 11:02:04 +01001600template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
1601linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
1602 const DexFile* target_dex_file,
1603 uint32_t pc_insn_offset,
1604 uint32_t boot_image_offset) {
1605 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
1606 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00001607}
1608
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001609void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001610 DCHECK(linker_patches->empty());
1611 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001612 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001613 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001614 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001615 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001616 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01001617 string_bss_entry_patches_.size() +
1618 boot_image_intrinsic_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001619 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001620 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001621 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001622 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001623 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001624 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001625 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001626 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001627 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
1628 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001629 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001630 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00001631 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001632 DCHECK(boot_image_type_patches_.empty());
1633 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001634 DCHECK(boot_image_intrinsic_patches_.empty());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001635 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001636 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1637 method_bss_entry_patches_, linker_patches);
1638 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1639 type_bss_entry_patches_, linker_patches);
1640 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1641 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001642 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001643}
1644
Vladimir Marko6fd16062018-06-26 11:02:04 +01001645CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageIntrinsicPatch(
1646 uint32_t intrinsic_data,
1647 const PcRelativePatchInfo* info_high) {
1648 return NewPcRelativePatch(
1649 /* dex_file */ nullptr, intrinsic_data, info_high, &boot_image_intrinsic_patches_);
1650}
1651
Vladimir Markob066d432018-01-03 13:14:37 +00001652CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageRelRoPatch(
1653 uint32_t boot_image_offset,
1654 const PcRelativePatchInfo* info_high) {
1655 return NewPcRelativePatch(
1656 /* dex_file */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_);
1657}
1658
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001659CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001660 MethodReference target_method,
1661 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001662 return NewPcRelativePatch(
1663 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001664}
1665
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001666CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001667 MethodReference target_method,
1668 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001669 return NewPcRelativePatch(
1670 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001671}
1672
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001673CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001674 const DexFile& dex_file,
1675 dex::TypeIndex type_index,
1676 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001677 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001678}
1679
Vladimir Marko1998cd02017-01-13 13:02:58 +00001680CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001681 const DexFile& dex_file,
1682 dex::TypeIndex type_index,
1683 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001684 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001685}
1686
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001687CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001688 const DexFile& dex_file,
1689 dex::StringIndex string_index,
1690 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001691 return NewPcRelativePatch(
1692 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001693}
1694
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001695CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1696 const DexFile& dex_file,
1697 dex::StringIndex string_index,
1698 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001699 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001700}
1701
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001702CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001703 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001704 uint32_t offset_or_index,
1705 const PcRelativePatchInfo* info_high,
1706 ArenaDeque<PcRelativePatchInfo>* patches) {
1707 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001708 return &patches->back();
1709}
1710
Alexey Frunze06a46c42016-07-19 15:00:40 -07001711Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1712 return map->GetOrCreate(
1713 value,
1714 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1715}
1716
Alexey Frunze06a46c42016-07-19 15:00:40 -07001717Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001718 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001719}
1720
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001721void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001722 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001723 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001724 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001725 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001726 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001727 if (GetInstructionSetFeatures().IsR6()) {
1728 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001729 __ Bind(&info_high->label);
1730 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001731 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001732 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001733 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001734 } else {
1735 // If base is ZERO, emit NAL to obtain the actual base.
1736 if (base == ZERO) {
1737 // Generate a dummy PC-relative call to obtain PC.
1738 __ Nal();
1739 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001740 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001741 __ Lui(out, /* placeholder */ 0x1234);
1742 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1743 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1744 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001745 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001746 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001747 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001748 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001749 __ Addu(out, out, (base == ZERO) ? RA : base);
1750 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001751 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001752 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001753}
1754
Vladimir Marko6fd16062018-06-26 11:02:04 +01001755void CodeGeneratorMIPS::LoadBootImageAddress(Register reg, uint32_t boot_image_reference) {
1756 if (GetCompilerOptions().IsBootImage()) {
1757 PcRelativePatchInfo* info_high = NewBootImageIntrinsicPatch(boot_image_reference);
1758 PcRelativePatchInfo* info_low = NewBootImageIntrinsicPatch(boot_image_reference, info_high);
1759 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, /* base */ ZERO);
1760 __ Addiu(reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001761 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001762 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_reference);
1763 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_reference, info_high);
Vladimir Markoeebb8212018-06-05 14:57:24 +01001764 EmitPcRelativeAddressPlaceholderHigh(info_high, reg, /* base */ ZERO);
1765 __ Lw(reg, reg, /* placeholder */ 0x5678, &info_low->label);
1766 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001767 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01001768 gc::Heap* heap = Runtime::Current()->GetHeap();
1769 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001770 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01001771 __ LoadConst32(reg, dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address)));
1772 }
1773}
1774
Vladimir Marko6fd16062018-06-26 11:02:04 +01001775void CodeGeneratorMIPS::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
1776 uint32_t boot_image_offset) {
1777 DCHECK(invoke->IsStatic());
1778 InvokeRuntimeCallingConvention calling_convention;
1779 Register argument = calling_convention.GetRegisterAt(0);
1780 if (GetCompilerOptions().IsBootImage()) {
1781 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
1782 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
1783 MethodReference target_method = invoke->GetTargetMethod();
1784 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
1785 PcRelativePatchInfo* info_high = NewBootImageTypePatch(*target_method.dex_file, type_idx);
1786 PcRelativePatchInfo* info_low =
1787 NewBootImageTypePatch(*target_method.dex_file, type_idx, info_high);
1788 EmitPcRelativeAddressPlaceholderHigh(info_high, argument, /* base */ ZERO);
1789 __ Addiu(argument, argument, /* placeholder */ 0x5678, &info_low->label);
1790 } else {
1791 LoadBootImageAddress(argument, boot_image_offset);
1792 }
1793 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
1794 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
1795}
1796
Alexey Frunze627c1a02017-01-30 19:28:14 -08001797CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1798 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001799 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001800 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001801 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1802 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001803 return &jit_string_patches_.back();
1804}
1805
1806CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1807 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001808 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001809 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001810 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1811 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001812 return &jit_class_patches_.back();
1813}
1814
1815void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1816 const uint8_t* roots_data,
1817 const CodeGeneratorMIPS::JitPatchInfo& info,
1818 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001819 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1820 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001821 uintptr_t address =
1822 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1823 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1824 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001825 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1826 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1827 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1828 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001829 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001830 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1831 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001832 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001833 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001834 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1835 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001836 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001837 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1838 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001839}
1840
1841void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1842 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001843 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1844 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001845 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001846 }
1847 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001848 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1849 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001850 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001851 }
1852}
1853
Goran Jakovljevice114da22016-12-26 14:21:43 +01001854void CodeGeneratorMIPS::MarkGCCard(Register object,
1855 Register value,
1856 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 MipsLabel done;
1858 Register card = AT;
1859 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001860 if (value_can_be_null) {
1861 __ Beqz(value, &done);
1862 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 __ LoadFromOffset(kLoadWord,
1864 card,
1865 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001866 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1868 __ Addu(temp, card, temp);
1869 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001870 if (value_can_be_null) {
1871 __ Bind(&done);
1872 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001873}
1874
David Brazdil58282f42016-01-14 12:45:10 +00001875void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1877 blocked_core_registers_[ZERO] = true;
1878 blocked_core_registers_[K0] = true;
1879 blocked_core_registers_[K1] = true;
1880 blocked_core_registers_[GP] = true;
1881 blocked_core_registers_[SP] = true;
1882 blocked_core_registers_[RA] = true;
1883
1884 // AT and TMP(T8) are used as temporary/scratch registers
1885 // (similar to how AT is used by MIPS assemblers).
1886 blocked_core_registers_[AT] = true;
1887 blocked_core_registers_[TMP] = true;
1888 blocked_fpu_registers_[FTMP] = true;
1889
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001890 if (GetInstructionSetFeatures().HasMsa()) {
1891 // To be used just for MSA instructions.
1892 blocked_fpu_registers_[FTMP2] = true;
1893 }
1894
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001895 // Reserve suspend and thread registers.
1896 blocked_core_registers_[S0] = true;
1897 blocked_core_registers_[TR] = true;
1898
1899 // Reserve T9 for function calls
1900 blocked_core_registers_[T9] = true;
1901
1902 // Reserve odd-numbered FPU registers.
1903 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1904 blocked_fpu_registers_[i] = true;
1905 }
1906
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001907 if (GetGraph()->IsDebuggable()) {
1908 // Stubs do not save callee-save floating point registers. If the graph
1909 // is debuggable, we need to deal with these registers differently. For
1910 // now, just block them.
1911 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1912 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1913 }
1914 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915}
1916
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001917size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1918 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1919 return kMipsWordSize;
1920}
1921
1922size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1923 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1924 return kMipsWordSize;
1925}
1926
1927size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001928 if (GetGraph()->HasSIMD()) {
1929 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1930 } else {
1931 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1932 }
1933 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934}
1935
1936size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001937 if (GetGraph()->HasSIMD()) {
1938 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1939 } else {
1940 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1941 }
1942 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001943}
1944
1945void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001946 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947}
1948
1949void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001950 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951}
1952
Vladimir Markoa0431112018-06-25 09:32:54 +01001953const MipsInstructionSetFeatures& CodeGeneratorMIPS::GetInstructionSetFeatures() const {
1954 return *GetCompilerOptions().GetInstructionSetFeatures()->AsMipsInstructionSetFeatures();
1955}
1956
Serban Constantinescufca16662016-07-14 09:21:59 +01001957constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1958
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1960 HInstruction* instruction,
1961 uint32_t dex_pc,
1962 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001963 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001964 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1965 IsDirectEntrypoint(entrypoint));
1966 if (EntrypointRequiresStackMap(entrypoint)) {
1967 RecordPcInfo(instruction, dex_pc, slow_path);
1968 }
1969}
1970
1971void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1972 HInstruction* instruction,
1973 SlowPathCode* slow_path,
1974 bool direct) {
1975 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1976 GenerateInvokeRuntime(entry_point_offset, direct);
1977}
1978
1979void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001980 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001981 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001982 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001983 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 // Reserve argument space on stack (for $a0-$a3) for
1985 // entrypoints that directly reference native implementations.
1986 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001987 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001988 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001989 } else {
1990 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001991 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001992 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001993}
1994
1995void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1996 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001997 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1998 const size_t status_byte_offset =
1999 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2000 constexpr uint32_t shifted_initialized_value =
2001 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
2002
2003 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01002004 __ Sltiu(TMP, TMP, shifted_initialized_value);
2005 __ Bnez(TMP, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002006 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
2007 __ Sync(0);
2008 __ Bind(slow_path->GetExitLabel());
2009}
2010
Vladimir Marko175e7862018-03-27 09:03:13 +00002011void InstructionCodeGeneratorMIPS::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
2012 Register temp) {
2013 uint32_t path_to_root = check->GetBitstringPathToRoot();
2014 uint32_t mask = check->GetBitstringMask();
2015 DCHECK(IsPowerOfTwo(mask + 1));
2016 size_t mask_bits = WhichPowerOf2(mask + 1);
2017
2018 if (mask_bits == 16u) {
2019 // Load only the bitstring part of the status word.
2020 __ LoadFromOffset(
2021 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
2022 // Compare the bitstring bits using XOR.
2023 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
2024 } else {
2025 // /* uint32_t */ temp = temp->status_
2026 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
2027 // Compare the bitstring bits using XOR.
2028 if (IsUint<16>(path_to_root)) {
2029 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
2030 } else {
2031 __ LoadConst32(TMP, path_to_root);
2032 __ Xor(temp, temp, TMP);
2033 }
2034 // Shift out bits that do not contribute to the comparison.
2035 __ Sll(temp, temp, 32 - mask_bits);
2036 }
2037}
2038
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002039void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
2040 __ Sync(0); // Only stype 0 is supported.
2041}
2042
2043void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
2044 HBasicBlock* successor) {
2045 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07002046 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
2047
2048 if (slow_path == nullptr) {
2049 slow_path =
2050 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
2051 instruction->SetSlowPath(slow_path);
2052 codegen_->AddSlowPath(slow_path);
2053 if (successor != nullptr) {
2054 DCHECK(successor->IsLoopHeader());
2055 }
2056 } else {
2057 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2058 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059
2060 __ LoadFromOffset(kLoadUnsignedHalfword,
2061 TMP,
2062 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002063 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 if (successor == nullptr) {
2065 __ Bnez(TMP, slow_path->GetEntryLabel());
2066 __ Bind(slow_path->GetReturnLabel());
2067 } else {
2068 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2069 __ B(slow_path->GetEntryLabel());
2070 // slow_path will return to GetLabelOf(successor).
2071 }
2072}
2073
2074InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2075 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002076 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002077 assembler_(codegen->GetAssembler()),
2078 codegen_(codegen) {}
2079
2080void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2081 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002082 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01002084 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002085 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002086 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 locations->SetInAt(0, Location::RequiresRegister());
2088 HInstruction* right = instruction->InputAt(1);
2089 bool can_use_imm = false;
2090 if (right->IsConstant()) {
2091 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2092 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2093 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002094 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002095 DCHECK(instruction->IsSub() || instruction->IsAdd());
2096 if (instruction->IsSub()) {
2097 imm = -imm;
2098 }
2099 if (isR6) {
2100 bool single_use = right->GetUses().HasExactlyOneElement();
2101 int16_t imm_high = High16Bits(imm);
2102 int16_t imm_low = Low16Bits(imm);
2103 if (imm_low < 0) {
2104 imm_high += 1;
2105 }
2106 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2107 } else {
2108 can_use_imm = IsInt<16>(imm);
2109 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002110 }
2111 }
2112 if (can_use_imm)
2113 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2114 else
2115 locations->SetInAt(1, Location::RequiresRegister());
2116 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2117 break;
2118 }
2119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002121 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002122 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2123 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 break;
2125 }
2126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 case DataType::Type::kFloat32:
2128 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002129 DCHECK(instruction->IsAdd() || instruction->IsSub());
2130 locations->SetInAt(0, Location::RequiresFpuRegister());
2131 locations->SetInAt(1, Location::RequiresFpuRegister());
2132 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2133 break;
2134
2135 default:
2136 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2137 }
2138}
2139
2140void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002141 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002142 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002143 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002144
2145 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002146 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002147 Register dst = locations->Out().AsRegister<Register>();
2148 Register lhs = locations->InAt(0).AsRegister<Register>();
2149 Location rhs_location = locations->InAt(1);
2150
2151 Register rhs_reg = ZERO;
2152 int32_t rhs_imm = 0;
2153 bool use_imm = rhs_location.IsConstant();
2154 if (use_imm) {
2155 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2156 } else {
2157 rhs_reg = rhs_location.AsRegister<Register>();
2158 }
2159
2160 if (instruction->IsAnd()) {
2161 if (use_imm)
2162 __ Andi(dst, lhs, rhs_imm);
2163 else
2164 __ And(dst, lhs, rhs_reg);
2165 } else if (instruction->IsOr()) {
2166 if (use_imm)
2167 __ Ori(dst, lhs, rhs_imm);
2168 else
2169 __ Or(dst, lhs, rhs_reg);
2170 } else if (instruction->IsXor()) {
2171 if (use_imm)
2172 __ Xori(dst, lhs, rhs_imm);
2173 else
2174 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002175 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002176 DCHECK(instruction->IsAdd() || instruction->IsSub());
2177 if (use_imm) {
2178 if (instruction->IsSub()) {
2179 rhs_imm = -rhs_imm;
2180 }
2181 if (IsInt<16>(rhs_imm)) {
2182 __ Addiu(dst, lhs, rhs_imm);
2183 } else {
2184 DCHECK(isR6);
2185 int16_t rhs_imm_high = High16Bits(rhs_imm);
2186 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2187 if (rhs_imm_low < 0) {
2188 rhs_imm_high += 1;
2189 }
2190 __ Aui(dst, lhs, rhs_imm_high);
2191 if (rhs_imm_low != 0) {
2192 __ Addiu(dst, dst, rhs_imm_low);
2193 }
2194 }
2195 } else if (instruction->IsAdd()) {
2196 __ Addu(dst, lhs, rhs_reg);
2197 } else {
2198 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002199 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002200 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002201 }
2202 break;
2203 }
2204
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002205 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002206 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2207 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2208 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2209 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002210 Location rhs_location = locations->InAt(1);
2211 bool use_imm = rhs_location.IsConstant();
2212 if (!use_imm) {
2213 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2214 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2215 if (instruction->IsAnd()) {
2216 __ And(dst_low, lhs_low, rhs_low);
2217 __ And(dst_high, lhs_high, rhs_high);
2218 } else if (instruction->IsOr()) {
2219 __ Or(dst_low, lhs_low, rhs_low);
2220 __ Or(dst_high, lhs_high, rhs_high);
2221 } else if (instruction->IsXor()) {
2222 __ Xor(dst_low, lhs_low, rhs_low);
2223 __ Xor(dst_high, lhs_high, rhs_high);
2224 } else if (instruction->IsAdd()) {
2225 if (lhs_low == rhs_low) {
2226 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2227 __ Slt(TMP, lhs_low, ZERO);
2228 __ Addu(dst_low, lhs_low, rhs_low);
2229 } else {
2230 __ Addu(dst_low, lhs_low, rhs_low);
2231 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2232 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2233 }
2234 __ Addu(dst_high, lhs_high, rhs_high);
2235 __ Addu(dst_high, dst_high, TMP);
2236 } else {
2237 DCHECK(instruction->IsSub());
2238 __ Sltu(TMP, lhs_low, rhs_low);
2239 __ Subu(dst_low, lhs_low, rhs_low);
2240 __ Subu(dst_high, lhs_high, rhs_high);
2241 __ Subu(dst_high, dst_high, TMP);
2242 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002243 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002244 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2245 if (instruction->IsOr()) {
2246 uint32_t low = Low32Bits(value);
2247 uint32_t high = High32Bits(value);
2248 if (IsUint<16>(low)) {
2249 if (dst_low != lhs_low || low != 0) {
2250 __ Ori(dst_low, lhs_low, low);
2251 }
2252 } else {
2253 __ LoadConst32(TMP, low);
2254 __ Or(dst_low, lhs_low, TMP);
2255 }
2256 if (IsUint<16>(high)) {
2257 if (dst_high != lhs_high || high != 0) {
2258 __ Ori(dst_high, lhs_high, high);
2259 }
2260 } else {
2261 if (high != low) {
2262 __ LoadConst32(TMP, high);
2263 }
2264 __ Or(dst_high, lhs_high, TMP);
2265 }
2266 } else if (instruction->IsXor()) {
2267 uint32_t low = Low32Bits(value);
2268 uint32_t high = High32Bits(value);
2269 if (IsUint<16>(low)) {
2270 if (dst_low != lhs_low || low != 0) {
2271 __ Xori(dst_low, lhs_low, low);
2272 }
2273 } else {
2274 __ LoadConst32(TMP, low);
2275 __ Xor(dst_low, lhs_low, TMP);
2276 }
2277 if (IsUint<16>(high)) {
2278 if (dst_high != lhs_high || high != 0) {
2279 __ Xori(dst_high, lhs_high, high);
2280 }
2281 } else {
2282 if (high != low) {
2283 __ LoadConst32(TMP, high);
2284 }
2285 __ Xor(dst_high, lhs_high, TMP);
2286 }
2287 } else if (instruction->IsAnd()) {
2288 uint32_t low = Low32Bits(value);
2289 uint32_t high = High32Bits(value);
2290 if (IsUint<16>(low)) {
2291 __ Andi(dst_low, lhs_low, low);
2292 } else if (low != 0xFFFFFFFF) {
2293 __ LoadConst32(TMP, low);
2294 __ And(dst_low, lhs_low, TMP);
2295 } else if (dst_low != lhs_low) {
2296 __ Move(dst_low, lhs_low);
2297 }
2298 if (IsUint<16>(high)) {
2299 __ Andi(dst_high, lhs_high, high);
2300 } else if (high != 0xFFFFFFFF) {
2301 if (high != low) {
2302 __ LoadConst32(TMP, high);
2303 }
2304 __ And(dst_high, lhs_high, TMP);
2305 } else if (dst_high != lhs_high) {
2306 __ Move(dst_high, lhs_high);
2307 }
2308 } else {
2309 if (instruction->IsSub()) {
2310 value = -value;
2311 } else {
2312 DCHECK(instruction->IsAdd());
2313 }
2314 int32_t low = Low32Bits(value);
2315 int32_t high = High32Bits(value);
2316 if (IsInt<16>(low)) {
2317 if (dst_low != lhs_low || low != 0) {
2318 __ Addiu(dst_low, lhs_low, low);
2319 }
2320 if (low != 0) {
2321 __ Sltiu(AT, dst_low, low);
2322 }
2323 } else {
2324 __ LoadConst32(TMP, low);
2325 __ Addu(dst_low, lhs_low, TMP);
2326 __ Sltu(AT, dst_low, TMP);
2327 }
2328 if (IsInt<16>(high)) {
2329 if (dst_high != lhs_high || high != 0) {
2330 __ Addiu(dst_high, lhs_high, high);
2331 }
2332 } else {
2333 if (high != low) {
2334 __ LoadConst32(TMP, high);
2335 }
2336 __ Addu(dst_high, lhs_high, TMP);
2337 }
2338 if (low != 0) {
2339 __ Addu(dst_high, dst_high, AT);
2340 }
2341 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002342 }
2343 break;
2344 }
2345
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002346 case DataType::Type::kFloat32:
2347 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002348 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2349 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2350 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2351 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002352 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002353 __ AddS(dst, lhs, rhs);
2354 } else {
2355 __ AddD(dst, lhs, rhs);
2356 }
2357 } else {
2358 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002359 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002360 __ SubS(dst, lhs, rhs);
2361 } else {
2362 __ SubD(dst, lhs, rhs);
2363 }
2364 }
2365 break;
2366 }
2367
2368 default:
2369 LOG(FATAL) << "Unexpected binary operation type " << type;
2370 }
2371}
2372
2373void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002374 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002375
Vladimir Markoca6fff82017-10-03 14:49:14 +01002376 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002380 locations->SetInAt(0, Location::RequiresRegister());
2381 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2383 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002384 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002385 locations->SetInAt(0, Location::RequiresRegister());
2386 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2387 locations->SetOut(Location::RequiresRegister());
2388 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 default:
2390 LOG(FATAL) << "Unexpected shift type " << type;
2391 }
2392}
2393
2394static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2395
2396void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002397 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002398 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002399 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002400
2401 Location rhs_location = locations->InAt(1);
2402 bool use_imm = rhs_location.IsConstant();
2403 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2404 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002405 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002407 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002408 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2409 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002410
2411 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002413 Register dst = locations->Out().AsRegister<Register>();
2414 Register lhs = locations->InAt(0).AsRegister<Register>();
2415 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002416 if (shift_value == 0) {
2417 if (dst != lhs) {
2418 __ Move(dst, lhs);
2419 }
2420 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 __ Sll(dst, lhs, shift_value);
2422 } else if (instr->IsShr()) {
2423 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002424 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002425 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002426 } else {
2427 if (has_ins_rotr) {
2428 __ Rotr(dst, lhs, shift_value);
2429 } else {
2430 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2431 __ Srl(dst, lhs, shift_value);
2432 __ Or(dst, dst, TMP);
2433 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434 }
2435 } else {
2436 if (instr->IsShl()) {
2437 __ Sllv(dst, lhs, rhs_reg);
2438 } else if (instr->IsShr()) {
2439 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002440 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002441 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002442 } else {
2443 if (has_ins_rotr) {
2444 __ Rotrv(dst, lhs, rhs_reg);
2445 } else {
2446 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002447 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2448 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2449 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2450 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2451 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002452 __ Sllv(TMP, lhs, TMP);
2453 __ Srlv(dst, lhs, rhs_reg);
2454 __ Or(dst, dst, TMP);
2455 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002456 }
2457 }
2458 break;
2459 }
2460
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002461 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002462 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2463 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2464 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2465 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2466 if (use_imm) {
2467 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002468 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002469 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002470 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002471 if (instr->IsShl()) {
2472 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2473 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2474 __ Sll(dst_low, lhs_low, shift_value);
2475 } else if (instr->IsShr()) {
2476 __ Srl(dst_low, lhs_low, shift_value);
2477 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2478 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002479 } else if (instr->IsUShr()) {
2480 __ Srl(dst_low, lhs_low, shift_value);
2481 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2482 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002483 } else {
2484 __ Srl(dst_low, lhs_low, shift_value);
2485 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2486 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002487 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002489 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002490 if (instr->IsShl()) {
2491 __ Sll(dst_low, lhs_low, shift_value);
2492 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2493 __ Sll(dst_high, lhs_high, shift_value);
2494 __ Or(dst_high, dst_high, TMP);
2495 } else if (instr->IsShr()) {
2496 __ Sra(dst_high, lhs_high, shift_value);
2497 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2498 __ Srl(dst_low, lhs_low, shift_value);
2499 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002500 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002501 __ Srl(dst_high, lhs_high, shift_value);
2502 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2503 __ Srl(dst_low, lhs_low, shift_value);
2504 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002505 } else {
2506 __ Srl(TMP, lhs_low, shift_value);
2507 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2508 __ Or(dst_low, dst_low, TMP);
2509 __ Srl(TMP, lhs_high, shift_value);
2510 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2511 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002512 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002513 }
2514 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002515 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002516 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002517 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002518 __ Move(dst_low, ZERO);
2519 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002520 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002521 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002522 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002523 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002524 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002525 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002526 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002527 // 64-bit rotation by 32 is just a swap.
2528 __ Move(dst_low, lhs_high);
2529 __ Move(dst_high, lhs_low);
2530 } else {
2531 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002532 __ Srl(dst_low, lhs_high, shift_value_high);
2533 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2534 __ Srl(dst_high, lhs_low, shift_value_high);
2535 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002536 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002537 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2538 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002539 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002540 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2541 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002542 __ Or(dst_high, dst_high, TMP);
2543 }
2544 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002545 }
2546 }
2547 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002548 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002549 MipsLabel done;
2550 if (instr->IsShl()) {
2551 __ Sllv(dst_low, lhs_low, rhs_reg);
2552 __ Nor(AT, ZERO, rhs_reg);
2553 __ Srl(TMP, lhs_low, 1);
2554 __ Srlv(TMP, TMP, AT);
2555 __ Sllv(dst_high, lhs_high, rhs_reg);
2556 __ Or(dst_high, dst_high, TMP);
2557 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002558 if (isR6) {
2559 __ Beqzc(TMP, &done, /* is_bare */ true);
2560 __ Move(dst_high, dst_low);
2561 __ Move(dst_low, ZERO);
2562 } else {
2563 __ Movn(dst_high, dst_low, TMP);
2564 __ Movn(dst_low, ZERO, TMP);
2565 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002566 } else if (instr->IsShr()) {
2567 __ Srav(dst_high, lhs_high, rhs_reg);
2568 __ Nor(AT, ZERO, rhs_reg);
2569 __ Sll(TMP, lhs_high, 1);
2570 __ Sllv(TMP, TMP, AT);
2571 __ Srlv(dst_low, lhs_low, rhs_reg);
2572 __ Or(dst_low, dst_low, TMP);
2573 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002574 if (isR6) {
2575 __ Beqzc(TMP, &done, /* is_bare */ true);
2576 __ Move(dst_low, dst_high);
2577 __ Sra(dst_high, dst_high, 31);
2578 } else {
2579 __ Sra(AT, dst_high, 31);
2580 __ Movn(dst_low, dst_high, TMP);
2581 __ Movn(dst_high, AT, TMP);
2582 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002583 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002584 __ Srlv(dst_high, lhs_high, rhs_reg);
2585 __ Nor(AT, ZERO, rhs_reg);
2586 __ Sll(TMP, lhs_high, 1);
2587 __ Sllv(TMP, TMP, AT);
2588 __ Srlv(dst_low, lhs_low, rhs_reg);
2589 __ Or(dst_low, dst_low, TMP);
2590 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002591 if (isR6) {
2592 __ Beqzc(TMP, &done, /* is_bare */ true);
2593 __ Move(dst_low, dst_high);
2594 __ Move(dst_high, ZERO);
2595 } else {
2596 __ Movn(dst_low, dst_high, TMP);
2597 __ Movn(dst_high, ZERO, TMP);
2598 }
2599 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002600 __ Nor(AT, ZERO, rhs_reg);
2601 __ Srlv(TMP, lhs_low, rhs_reg);
2602 __ Sll(dst_low, lhs_high, 1);
2603 __ Sllv(dst_low, dst_low, AT);
2604 __ Or(dst_low, dst_low, TMP);
2605 __ Srlv(TMP, lhs_high, rhs_reg);
2606 __ Sll(dst_high, lhs_low, 1);
2607 __ Sllv(dst_high, dst_high, AT);
2608 __ Or(dst_high, dst_high, TMP);
2609 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002610 if (isR6) {
2611 __ Beqzc(TMP, &done, /* is_bare */ true);
2612 __ Move(TMP, dst_high);
2613 __ Move(dst_high, dst_low);
2614 __ Move(dst_low, TMP);
2615 } else {
2616 __ Movn(AT, dst_high, TMP);
2617 __ Movn(dst_high, dst_low, TMP);
2618 __ Movn(dst_low, AT, TMP);
2619 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 }
2621 __ Bind(&done);
2622 }
2623 break;
2624 }
2625
2626 default:
2627 LOG(FATAL) << "Unexpected shift operation type " << type;
2628 }
2629}
2630
2631void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2632 HandleBinaryOp(instruction);
2633}
2634
2635void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2636 HandleBinaryOp(instruction);
2637}
2638
2639void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2640 HandleBinaryOp(instruction);
2641}
2642
2643void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2644 HandleBinaryOp(instruction);
2645}
2646
2647void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002648 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002649 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002651 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002652 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2653 object_array_get_with_read_barrier
2654 ? LocationSummary::kCallOnSlowPath
2655 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002656 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2657 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2658 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002659 locations->SetInAt(0, Location::RequiresRegister());
2660 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002662 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2663 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002664 // The output overlaps in the case of an object array get with
2665 // read barriers enabled: we do not want the move to overwrite the
2666 // array's location, as we need it to emit the read barrier.
2667 locations->SetOut(Location::RequiresRegister(),
2668 object_array_get_with_read_barrier
2669 ? Location::kOutputOverlap
2670 : Location::kNoOutputOverlap);
2671 }
2672 // We need a temporary register for the read barrier marking slow
2673 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2674 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002675 bool temp_needed = instruction->GetIndex()->IsConstant()
2676 ? !kBakerReadBarrierThunksEnableForFields
2677 : !kBakerReadBarrierThunksEnableForArrays;
2678 if (temp_needed) {
2679 locations->AddTemp(Location::RequiresRegister());
2680 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002681 }
2682}
2683
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002684static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2685 auto null_checker = [codegen, instruction]() {
2686 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002687 };
2688 return null_checker;
2689}
2690
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002691void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2692 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002693 Location obj_loc = locations->InAt(0);
2694 Register obj = obj_loc.AsRegister<Register>();
2695 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002697 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002698 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002701 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2702 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002703 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002704 case DataType::Type::kBool:
2705 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002706 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002707 if (index.IsConstant()) {
2708 size_t offset =
2709 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002710 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002711 } else {
2712 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002713 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002714 }
2715 break;
2716 }
2717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002719 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002720 if (index.IsConstant()) {
2721 size_t offset =
2722 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002723 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002724 } else {
2725 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002726 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002727 }
2728 break;
2729 }
2730
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002732 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002733 if (maybe_compressed_char_at) {
2734 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2735 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2736 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2737 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2738 "Expecting 0=compressed, 1=uncompressed");
2739 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002740 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002741 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2742 if (maybe_compressed_char_at) {
2743 MipsLabel uncompressed_load, done;
2744 __ Bnez(TMP, &uncompressed_load);
2745 __ LoadFromOffset(kLoadUnsignedByte,
2746 out,
2747 obj,
2748 data_offset + (const_index << TIMES_1));
2749 __ B(&done);
2750 __ Bind(&uncompressed_load);
2751 __ LoadFromOffset(kLoadUnsignedHalfword,
2752 out,
2753 obj,
2754 data_offset + (const_index << TIMES_2));
2755 __ Bind(&done);
2756 } else {
2757 __ LoadFromOffset(kLoadUnsignedHalfword,
2758 out,
2759 obj,
2760 data_offset + (const_index << TIMES_2),
2761 null_checker);
2762 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002763 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002764 Register index_reg = index.AsRegister<Register>();
2765 if (maybe_compressed_char_at) {
2766 MipsLabel uncompressed_load, done;
2767 __ Bnez(TMP, &uncompressed_load);
2768 __ Addu(TMP, obj, index_reg);
2769 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2770 __ B(&done);
2771 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002772 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002773 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2774 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002775 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2776 __ Addu(TMP, index_reg, obj);
2777 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002778 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002779 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002780 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2781 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002782 }
2783 break;
2784 }
2785
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002786 case DataType::Type::kInt16: {
2787 Register out = out_loc.AsRegister<Register>();
2788 if (index.IsConstant()) {
2789 size_t offset =
2790 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2791 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002792 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2793 __ Addu(TMP, index.AsRegister<Register>(), obj);
2794 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002795 } else {
2796 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2797 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2798 }
2799 break;
2800 }
2801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002803 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002804 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002805 if (index.IsConstant()) {
2806 size_t offset =
2807 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002808 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002809 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2810 __ Addu(TMP, index.AsRegister<Register>(), obj);
2811 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002812 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002813 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002814 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002815 }
2816 break;
2817 }
2818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002820 static_assert(
2821 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2822 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2823 // /* HeapReference<Object> */ out =
2824 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2825 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002826 bool temp_needed = index.IsConstant()
2827 ? !kBakerReadBarrierThunksEnableForFields
2828 : !kBakerReadBarrierThunksEnableForArrays;
2829 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002830 // Note that a potential implicit null check is handled in this
2831 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002832 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2833 if (index.IsConstant()) {
2834 // Array load with a constant index can be treated as a field load.
2835 size_t offset =
2836 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2837 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2838 out_loc,
2839 obj,
2840 offset,
2841 temp,
2842 /* needs_null_check */ false);
2843 } else {
2844 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2845 out_loc,
2846 obj,
2847 data_offset,
2848 index,
2849 temp,
2850 /* needs_null_check */ false);
2851 }
Alexey Frunze15958152017-02-09 19:08:30 -08002852 } else {
2853 Register out = out_loc.AsRegister<Register>();
2854 if (index.IsConstant()) {
2855 size_t offset =
2856 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2857 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2858 // If read barriers are enabled, emit read barriers other than
2859 // Baker's using a slow path (and also unpoison the loaded
2860 // reference, if heap poisoning is enabled).
2861 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2862 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002863 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002864 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2865 // If read barriers are enabled, emit read barriers other than
2866 // Baker's using a slow path (and also unpoison the loaded
2867 // reference, if heap poisoning is enabled).
2868 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2869 out_loc,
2870 out_loc,
2871 obj_loc,
2872 data_offset,
2873 index);
2874 }
2875 }
2876 break;
2877 }
2878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002880 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002881 if (index.IsConstant()) {
2882 size_t offset =
2883 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002884 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002885 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2886 __ Addu(TMP, index.AsRegister<Register>(), obj);
2887 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002888 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002889 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002890 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002891 }
2892 break;
2893 }
2894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002895 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002896 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002897 if (index.IsConstant()) {
2898 size_t offset =
2899 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002900 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002901 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2902 __ Addu(TMP, index.AsRegister<Register>(), obj);
2903 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002904 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002905 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002906 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002907 }
2908 break;
2909 }
2910
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002911 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002912 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002913 if (index.IsConstant()) {
2914 size_t offset =
2915 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002916 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002917 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2918 __ Addu(TMP, index.AsRegister<Register>(), obj);
2919 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002920 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002921 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002922 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002923 }
2924 break;
2925 }
2926
Aart Bik66c158e2018-01-31 12:55:04 -08002927 case DataType::Type::kUint32:
2928 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002930 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2931 UNREACHABLE();
2932 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002933}
2934
2935void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002936 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002937 locations->SetInAt(0, Location::RequiresRegister());
2938 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2939}
2940
2941void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2942 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002943 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002944 Register obj = locations->InAt(0).AsRegister<Register>();
2945 Register out = locations->Out().AsRegister<Register>();
2946 __ LoadFromOffset(kLoadWord, out, obj, offset);
2947 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002948 // Mask out compression flag from String's array length.
2949 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2950 __ Srl(out, out, 1u);
2951 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002952}
2953
Alexey Frunzef58b2482016-09-02 22:14:06 -07002954Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2955 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2956 ? Location::ConstantLocation(instruction->AsConstant())
2957 : Location::RequiresRegister();
2958}
2959
2960Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2961 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2962 // We can store a non-zero float or double constant without first loading it into the FPU,
2963 // but we should only prefer this if the constant has a single use.
2964 if (instruction->IsConstant() &&
2965 (instruction->AsConstant()->IsZeroBitPattern() ||
2966 instruction->GetUses().HasExactlyOneElement())) {
2967 return Location::ConstantLocation(instruction->AsConstant());
2968 // Otherwise fall through and require an FPU register for the constant.
2969 }
2970 return Location::RequiresFpuRegister();
2971}
2972
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002973void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002975
2976 bool needs_write_barrier =
2977 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2978 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2979
Vladimir Markoca6fff82017-10-03 14:49:14 +01002980 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002981 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002982 may_need_runtime_call_for_type_check ?
2983 LocationSummary::kCallOnSlowPath :
2984 LocationSummary::kNoCall);
2985
2986 locations->SetInAt(0, Location::RequiresRegister());
2987 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002988 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002989 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002990 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002991 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2992 }
2993 if (needs_write_barrier) {
2994 // Temporary register for the write barrier.
2995 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002996 }
2997}
2998
2999void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
3000 LocationSummary* locations = instruction->GetLocations();
3001 Register obj = locations->InAt(0).AsRegister<Register>();
3002 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003003 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08003005 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003006 bool needs_write_barrier =
3007 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003008 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003009 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003010
3011 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003013 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003014 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003015 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003016 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003017 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003018 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003019 __ Addu(base_reg, obj, index.AsRegister<Register>());
3020 }
3021 if (value_location.IsConstant()) {
3022 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3023 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
3024 } else {
3025 Register value = value_location.AsRegister<Register>();
3026 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003027 }
3028 break;
3029 }
3030
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003031 case DataType::Type::kUint16:
3032 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003033 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003034 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003035 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02003036 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3037 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003038 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003039 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003040 }
3041 if (value_location.IsConstant()) {
3042 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3043 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
3044 } else {
3045 Register value = value_location.AsRegister<Register>();
3046 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003047 }
3048 break;
3049 }
3050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003051 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08003052 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3053 if (index.IsConstant()) {
3054 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003055 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3056 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08003057 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003058 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003059 }
3060 if (value_location.IsConstant()) {
3061 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3062 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3063 } else {
3064 Register value = value_location.AsRegister<Register>();
3065 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3066 }
3067 break;
3068 }
3069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003070 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08003071 if (value_location.IsConstant()) {
3072 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_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_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003076 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003077 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003078 }
Alexey Frunze15958152017-02-09 19:08:30 -08003079 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3080 DCHECK_EQ(value, 0);
3081 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3082 DCHECK(!needs_write_barrier);
3083 DCHECK(!may_need_runtime_call_for_type_check);
3084 break;
3085 }
3086
3087 DCHECK(needs_write_barrier);
3088 Register value = value_location.AsRegister<Register>();
3089 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3090 Register temp2 = TMP; // Doesn't need to survive slow path.
3091 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3092 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3093 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3094 MipsLabel done;
3095 SlowPathCodeMIPS* slow_path = nullptr;
3096
3097 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003098 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003099 codegen_->AddSlowPath(slow_path);
3100 if (instruction->GetValueCanBeNull()) {
3101 MipsLabel non_zero;
3102 __ Bnez(value, &non_zero);
3103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3104 if (index.IsConstant()) {
3105 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003106 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3107 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003108 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003109 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003110 }
Alexey Frunze15958152017-02-09 19:08:30 -08003111 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3112 __ B(&done);
3113 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003114 }
Alexey Frunze15958152017-02-09 19:08:30 -08003115
3116 // Note that when read barriers are enabled, the type checks
3117 // are performed without read barriers. This is fine, even in
3118 // the case where a class object is in the from-space after
3119 // the flip, as a comparison involving such a type would not
3120 // produce a false positive; it may of course produce a false
3121 // negative, in which case we would take the ArraySet slow
3122 // path.
3123
3124 // /* HeapReference<Class> */ temp1 = obj->klass_
3125 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3126 __ MaybeUnpoisonHeapReference(temp1);
3127
3128 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3129 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3130 // /* HeapReference<Class> */ temp2 = value->klass_
3131 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3132 // If heap poisoning is enabled, no need to unpoison `temp1`
3133 // nor `temp2`, as we are comparing two poisoned references.
3134
3135 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3136 MipsLabel do_put;
3137 __ Beq(temp1, temp2, &do_put);
3138 // If heap poisoning is enabled, the `temp1` reference has
3139 // not been unpoisoned yet; unpoison it now.
3140 __ MaybeUnpoisonHeapReference(temp1);
3141
3142 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3143 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3144 // If heap poisoning is enabled, no need to unpoison
3145 // `temp1`, as we are comparing against null below.
3146 __ Bnez(temp1, slow_path->GetEntryLabel());
3147 __ Bind(&do_put);
3148 } else {
3149 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3150 }
3151 }
3152
3153 Register source = value;
3154 if (kPoisonHeapReferences) {
3155 // Note that in the case where `value` is a null reference,
3156 // we do not enter this block, as a null reference does not
3157 // need poisoning.
3158 __ Move(temp1, value);
3159 __ PoisonHeapReference(temp1);
3160 source = temp1;
3161 }
3162
3163 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3164 if (index.IsConstant()) {
3165 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003166 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003167 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003168 }
3169 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3170
3171 if (!may_need_runtime_call_for_type_check) {
3172 codegen_->MaybeRecordImplicitNullCheck(instruction);
3173 }
3174
3175 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3176
3177 if (done.IsLinked()) {
3178 __ Bind(&done);
3179 }
3180
3181 if (slow_path != nullptr) {
3182 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003183 }
3184 break;
3185 }
3186
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003187 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003188 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003189 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003190 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003191 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3192 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003193 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003194 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003195 }
3196 if (value_location.IsConstant()) {
3197 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3198 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3199 } else {
3200 Register value = value_location.AsRegisterPairLow<Register>();
3201 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003202 }
3203 break;
3204 }
3205
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003206 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003207 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003208 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003209 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003210 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3211 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003212 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003213 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003214 }
3215 if (value_location.IsConstant()) {
3216 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3217 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3218 } else {
3219 FRegister value = value_location.AsFpuRegister<FRegister>();
3220 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003221 }
3222 break;
3223 }
3224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003226 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003227 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003228 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003229 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3230 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003231 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003232 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003233 }
3234 if (value_location.IsConstant()) {
3235 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3236 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3237 } else {
3238 FRegister value = value_location.AsFpuRegister<FRegister>();
3239 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003240 }
3241 break;
3242 }
3243
Aart Bik66c158e2018-01-31 12:55:04 -08003244 case DataType::Type::kUint32:
3245 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003246 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003247 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3248 UNREACHABLE();
3249 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003250}
3251
Lena Djokica2901602017-09-21 13:50:52 +02003252void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3253 HIntermediateArrayAddressIndex* instruction) {
3254 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003255 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003256
3257 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3258
3259 locations->SetInAt(0, Location::RequiresRegister());
3260 locations->SetInAt(1, Location::ConstantLocation(shift));
3261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3262}
3263
3264void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3265 HIntermediateArrayAddressIndex* instruction) {
3266 LocationSummary* locations = instruction->GetLocations();
3267 Register index_reg = locations->InAt(0).AsRegister<Register>();
3268 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3269 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3270}
3271
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003272void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003273 RegisterSet caller_saves = RegisterSet::Empty();
3274 InvokeRuntimeCallingConvention calling_convention;
3275 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3276 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3277 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003278
3279 HInstruction* index = instruction->InputAt(0);
3280 HInstruction* length = instruction->InputAt(1);
3281
3282 bool const_index = false;
3283 bool const_length = false;
3284
3285 if (index->IsConstant()) {
3286 if (length->IsConstant()) {
3287 const_index = true;
3288 const_length = true;
3289 } else {
3290 int32_t index_value = index->AsIntConstant()->GetValue();
3291 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3292 const_index = true;
3293 }
3294 }
3295 } else if (length->IsConstant()) {
3296 int32_t length_value = length->AsIntConstant()->GetValue();
3297 if (IsUint<15>(length_value)) {
3298 const_length = true;
3299 }
3300 }
3301
3302 locations->SetInAt(0, const_index
3303 ? Location::ConstantLocation(index->AsConstant())
3304 : Location::RequiresRegister());
3305 locations->SetInAt(1, const_length
3306 ? Location::ConstantLocation(length->AsConstant())
3307 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003308}
3309
3310void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3311 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003312 Location index_loc = locations->InAt(0);
3313 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003314
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003315 if (length_loc.IsConstant()) {
3316 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3317 if (index_loc.IsConstant()) {
3318 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3319 if (index < 0 || index >= length) {
3320 BoundsCheckSlowPathMIPS* slow_path =
3321 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3322 codegen_->AddSlowPath(slow_path);
3323 __ B(slow_path->GetEntryLabel());
3324 } else {
3325 // Nothing to be done.
3326 }
3327 return;
3328 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003329
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003330 BoundsCheckSlowPathMIPS* slow_path =
3331 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3332 codegen_->AddSlowPath(slow_path);
3333 Register index = index_loc.AsRegister<Register>();
3334 if (length == 0) {
3335 __ B(slow_path->GetEntryLabel());
3336 } else if (length == 1) {
3337 __ Bnez(index, slow_path->GetEntryLabel());
3338 } else {
3339 DCHECK(IsUint<15>(length)) << length;
3340 __ Sltiu(TMP, index, length);
3341 __ Beqz(TMP, slow_path->GetEntryLabel());
3342 }
3343 } else {
3344 Register length = length_loc.AsRegister<Register>();
3345 BoundsCheckSlowPathMIPS* slow_path =
3346 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3347 codegen_->AddSlowPath(slow_path);
3348 if (index_loc.IsConstant()) {
3349 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3350 if (index < 0) {
3351 __ B(slow_path->GetEntryLabel());
3352 } else if (index == 0) {
3353 __ Blez(length, slow_path->GetEntryLabel());
3354 } else {
3355 DCHECK(IsInt<16>(index + 1)) << index;
3356 __ Sltiu(TMP, length, index + 1);
3357 __ Bnez(TMP, slow_path->GetEntryLabel());
3358 }
3359 } else {
3360 Register index = index_loc.AsRegister<Register>();
3361 __ Bgeu(index, length, slow_path->GetEntryLabel());
3362 }
3363 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003364}
3365
Alexey Frunze15958152017-02-09 19:08:30 -08003366// Temp is used for read barrier.
3367static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3368 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003369 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003370 (kUseBakerReadBarrier ||
3371 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3372 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3373 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3374 return 1;
3375 }
3376 return 0;
3377}
3378
3379// Extra temp is used for read barrier.
3380static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3381 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3382}
3383
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003384void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003385 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003386 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003387 LocationSummary* locations =
3388 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003389 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003390 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3391 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3392 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3393 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3394 } else {
3395 locations->SetInAt(1, Location::RequiresRegister());
3396 }
Alexey Frunze15958152017-02-09 19:08:30 -08003397 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003398}
3399
3400void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003401 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003402 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003403 Location obj_loc = locations->InAt(0);
3404 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko175e7862018-03-27 09:03:13 +00003405 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08003406 Location temp_loc = locations->GetTemp(0);
3407 Register temp = temp_loc.AsRegister<Register>();
3408 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3409 DCHECK_LE(num_temps, 2u);
3410 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003411 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3412 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3413 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3414 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3415 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3416 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3417 const uint32_t object_array_data_offset =
3418 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3419 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003420
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003421 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003422 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003423 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3424 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003425 codegen_->AddSlowPath(slow_path);
3426
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003427 // Avoid this check if we know `obj` is not null.
3428 if (instruction->MustDoNullCheck()) {
3429 __ Beqz(obj, &done);
3430 }
3431
3432 switch (type_check_kind) {
3433 case TypeCheckKind::kExactCheck:
3434 case TypeCheckKind::kArrayCheck: {
3435 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003436 GenerateReferenceLoadTwoRegisters(instruction,
3437 temp_loc,
3438 obj_loc,
3439 class_offset,
3440 maybe_temp2_loc,
3441 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003442 // Jump to slow path for throwing the exception or doing a
3443 // more involved array check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003444 __ Bne(temp, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003445 break;
3446 }
3447
3448 case TypeCheckKind::kAbstractClassCheck: {
3449 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003450 GenerateReferenceLoadTwoRegisters(instruction,
3451 temp_loc,
3452 obj_loc,
3453 class_offset,
3454 maybe_temp2_loc,
3455 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003456 // If the class is abstract, we eagerly fetch the super class of the
3457 // object to avoid doing a comparison we know will fail.
3458 MipsLabel loop;
3459 __ Bind(&loop);
3460 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003461 GenerateReferenceLoadOneRegister(instruction,
3462 temp_loc,
3463 super_offset,
3464 maybe_temp2_loc,
3465 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003466 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3467 // exception.
3468 __ Beqz(temp, slow_path->GetEntryLabel());
3469 // Otherwise, compare the classes.
Vladimir Marko175e7862018-03-27 09:03:13 +00003470 __ Bne(temp, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003471 break;
3472 }
3473
3474 case TypeCheckKind::kClassHierarchyCheck: {
3475 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003476 GenerateReferenceLoadTwoRegisters(instruction,
3477 temp_loc,
3478 obj_loc,
3479 class_offset,
3480 maybe_temp2_loc,
3481 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003482 // Walk over the class hierarchy to find a match.
3483 MipsLabel loop;
3484 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00003485 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003486 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003487 GenerateReferenceLoadOneRegister(instruction,
3488 temp_loc,
3489 super_offset,
3490 maybe_temp2_loc,
3491 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003492 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3493 // exception. Otherwise, jump to the beginning of the loop.
3494 __ Bnez(temp, &loop);
3495 __ B(slow_path->GetEntryLabel());
3496 break;
3497 }
3498
3499 case TypeCheckKind::kArrayObjectCheck: {
3500 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003501 GenerateReferenceLoadTwoRegisters(instruction,
3502 temp_loc,
3503 obj_loc,
3504 class_offset,
3505 maybe_temp2_loc,
3506 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003507 // Do an exact check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003508 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003509 // Otherwise, we need to check that the object's class is a non-primitive array.
3510 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003511 GenerateReferenceLoadOneRegister(instruction,
3512 temp_loc,
3513 component_offset,
3514 maybe_temp2_loc,
3515 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003516 // If the component type is null, jump to the slow path to throw the exception.
3517 __ Beqz(temp, slow_path->GetEntryLabel());
3518 // Otherwise, the object is indeed an array, further check that this component
3519 // type is not a primitive type.
3520 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3521 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3522 __ Bnez(temp, slow_path->GetEntryLabel());
3523 break;
3524 }
3525
3526 case TypeCheckKind::kUnresolvedCheck:
3527 // We always go into the type check slow path for the unresolved check case.
3528 // We cannot directly call the CheckCast runtime entry point
3529 // without resorting to a type checking slow path here (i.e. by
3530 // calling InvokeRuntime directly), as it would require to
3531 // assign fixed registers for the inputs of this HInstanceOf
3532 // instruction (following the runtime calling convention), which
3533 // might be cluttered by the potential first read barrier
3534 // emission at the beginning of this method.
3535 __ B(slow_path->GetEntryLabel());
3536 break;
3537
3538 case TypeCheckKind::kInterfaceCheck: {
3539 // Avoid read barriers to improve performance of the fast path. We can not get false
3540 // positives by doing this.
3541 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003542 GenerateReferenceLoadTwoRegisters(instruction,
3543 temp_loc,
3544 obj_loc,
3545 class_offset,
3546 maybe_temp2_loc,
3547 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003548 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003549 GenerateReferenceLoadTwoRegisters(instruction,
3550 temp_loc,
3551 temp_loc,
3552 iftable_offset,
3553 maybe_temp2_loc,
3554 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003555 // Iftable is never null.
3556 __ Lw(TMP, temp, array_length_offset);
3557 // Loop through the iftable and check if any class matches.
3558 MipsLabel loop;
3559 __ Bind(&loop);
3560 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3561 __ Beqz(TMP, slow_path->GetEntryLabel());
3562 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3563 __ MaybeUnpoisonHeapReference(AT);
3564 // Go to next interface.
3565 __ Addiu(TMP, TMP, -2);
3566 // Compare the classes and continue the loop if they do not match.
Vladimir Marko175e7862018-03-27 09:03:13 +00003567 __ Bne(AT, cls.AsRegister<Register>(), &loop);
3568 break;
3569 }
3570
3571 case TypeCheckKind::kBitstringCheck: {
3572 // /* HeapReference<Class> */ temp = obj->klass_
3573 GenerateReferenceLoadTwoRegisters(instruction,
3574 temp_loc,
3575 obj_loc,
3576 class_offset,
3577 maybe_temp2_loc,
3578 kWithoutReadBarrier);
3579
3580 GenerateBitstringTypeCheckCompare(instruction, temp);
3581 __ Bnez(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003582 break;
3583 }
3584 }
3585
3586 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003587 __ Bind(slow_path->GetExitLabel());
3588}
3589
3590void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3591 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003592 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003593 locations->SetInAt(0, Location::RequiresRegister());
3594 if (check->HasUses()) {
3595 locations->SetOut(Location::SameAsFirstInput());
3596 }
3597}
3598
3599void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3600 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01003601 SlowPathCodeMIPS* slow_path =
3602 new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(check->GetLoadClass(), check);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003603 codegen_->AddSlowPath(slow_path);
3604 GenerateClassInitializationCheck(slow_path,
3605 check->GetLocations()->InAt(0).AsRegister<Register>());
3606}
3607
3608void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003609 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003610
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003611 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003612 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003613
3614 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003615 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003616 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003617 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003618 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003619 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003620 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003621 locations->SetInAt(0, Location::RequiresRegister());
3622 locations->SetInAt(1, Location::RequiresRegister());
3623 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3624 break;
3625
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003626 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003627 locations->SetInAt(0, Location::RequiresRegister());
3628 locations->SetInAt(1, Location::RequiresRegister());
3629 // Output overlaps because it is written before doing the low comparison.
3630 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3631 break;
3632
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003633 case DataType::Type::kFloat32:
3634 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003635 locations->SetInAt(0, Location::RequiresFpuRegister());
3636 locations->SetInAt(1, Location::RequiresFpuRegister());
3637 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003638 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003639
3640 default:
3641 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3642 }
3643}
3644
3645void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3646 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003647 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003648 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003649 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003650
3651 // 0 if: left == right
3652 // 1 if: left > right
3653 // -1 if: left < right
3654 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003655 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003656 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003657 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003658 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003659 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003660 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003661 Register lhs = locations->InAt(0).AsRegister<Register>();
3662 Register rhs = locations->InAt(1).AsRegister<Register>();
3663 __ Slt(TMP, lhs, rhs);
3664 __ Slt(res, rhs, lhs);
3665 __ Subu(res, res, TMP);
3666 break;
3667 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003668 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003669 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003670 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3671 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3672 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3673 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3674 // TODO: more efficient (direct) comparison with a constant.
3675 __ Slt(TMP, lhs_high, rhs_high);
3676 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3677 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3678 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3679 __ Sltu(TMP, lhs_low, rhs_low);
3680 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3681 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3682 __ Bind(&done);
3683 break;
3684 }
3685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003687 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003688 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3689 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3690 MipsLabel done;
3691 if (isR6) {
3692 __ CmpEqS(FTMP, lhs, rhs);
3693 __ LoadConst32(res, 0);
3694 __ Bc1nez(FTMP, &done);
3695 if (gt_bias) {
3696 __ CmpLtS(FTMP, lhs, rhs);
3697 __ LoadConst32(res, -1);
3698 __ Bc1nez(FTMP, &done);
3699 __ LoadConst32(res, 1);
3700 } else {
3701 __ CmpLtS(FTMP, rhs, lhs);
3702 __ LoadConst32(res, 1);
3703 __ Bc1nez(FTMP, &done);
3704 __ LoadConst32(res, -1);
3705 }
3706 } else {
3707 if (gt_bias) {
3708 __ ColtS(0, lhs, rhs);
3709 __ LoadConst32(res, -1);
3710 __ Bc1t(0, &done);
3711 __ CeqS(0, lhs, rhs);
3712 __ LoadConst32(res, 1);
3713 __ Movt(res, ZERO, 0);
3714 } else {
3715 __ ColtS(0, rhs, lhs);
3716 __ LoadConst32(res, 1);
3717 __ Bc1t(0, &done);
3718 __ CeqS(0, lhs, rhs);
3719 __ LoadConst32(res, -1);
3720 __ Movt(res, ZERO, 0);
3721 }
3722 }
3723 __ Bind(&done);
3724 break;
3725 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003726 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003727 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003728 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3729 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3730 MipsLabel done;
3731 if (isR6) {
3732 __ CmpEqD(FTMP, lhs, rhs);
3733 __ LoadConst32(res, 0);
3734 __ Bc1nez(FTMP, &done);
3735 if (gt_bias) {
3736 __ CmpLtD(FTMP, lhs, rhs);
3737 __ LoadConst32(res, -1);
3738 __ Bc1nez(FTMP, &done);
3739 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003740 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003741 __ CmpLtD(FTMP, rhs, lhs);
3742 __ LoadConst32(res, 1);
3743 __ Bc1nez(FTMP, &done);
3744 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003745 }
3746 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003747 if (gt_bias) {
3748 __ ColtD(0, lhs, rhs);
3749 __ LoadConst32(res, -1);
3750 __ Bc1t(0, &done);
3751 __ CeqD(0, lhs, rhs);
3752 __ LoadConst32(res, 1);
3753 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003754 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003755 __ ColtD(0, rhs, lhs);
3756 __ LoadConst32(res, 1);
3757 __ Bc1t(0, &done);
3758 __ CeqD(0, lhs, rhs);
3759 __ LoadConst32(res, -1);
3760 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003761 }
3762 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003763 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003764 break;
3765 }
3766
3767 default:
3768 LOG(FATAL) << "Unimplemented compare type " << in_type;
3769 }
3770}
3771
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003772void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003773 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003774 switch (instruction->InputAt(0)->GetType()) {
3775 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003776 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003777 locations->SetInAt(0, Location::RequiresRegister());
3778 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3779 break;
3780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003781 case DataType::Type::kFloat32:
3782 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003783 locations->SetInAt(0, Location::RequiresFpuRegister());
3784 locations->SetInAt(1, Location::RequiresFpuRegister());
3785 break;
3786 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003787 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003788 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3789 }
3790}
3791
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003792void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003793 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003794 return;
3795 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003796
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003797 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003798 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003799
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003800 switch (type) {
3801 default:
3802 // Integer case.
3803 GenerateIntCompare(instruction->GetCondition(), locations);
3804 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003806 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003807 GenerateLongCompare(instruction->GetCondition(), locations);
3808 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003810 case DataType::Type::kFloat32:
3811 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003812 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3813 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003814 }
3815}
3816
Alexey Frunze7e99e052015-11-24 19:28:01 -08003817void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3818 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003819
3820 LocationSummary* locations = instruction->GetLocations();
3821 Location second = locations->InAt(1);
3822 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003823 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003824 DCHECK(imm == 1 || imm == -1);
3825
Lena Djokic4b8025c2017-12-21 16:15:50 +01003826 if (instruction->GetResultType() == DataType::Type::kInt32) {
3827 Register out = locations->Out().AsRegister<Register>();
3828 Register dividend = locations->InAt(0).AsRegister<Register>();
3829
3830 if (instruction->IsRem()) {
3831 __ Move(out, ZERO);
3832 } else {
3833 if (imm == -1) {
3834 __ Subu(out, ZERO, dividend);
3835 } else if (out != dividend) {
3836 __ Move(out, dividend);
3837 }
3838 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003839 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003840 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3841 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3842 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3843 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3844 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3845
3846 if (instruction->IsRem()) {
3847 __ Move(out_high, ZERO);
3848 __ Move(out_low, ZERO);
3849 } else {
3850 if (imm == -1) {
3851 __ Subu(out_low, ZERO, in_low);
3852 __ Sltu(AT, ZERO, out_low);
3853 __ Subu(out_high, ZERO, in_high);
3854 __ Subu(out_high, out_high, AT);
3855 } else {
3856 __ Move(out_low, in_low);
3857 __ Move(out_high, in_high);
3858 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003859 }
3860 }
3861}
3862
3863void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3864 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003865
3866 LocationSummary* locations = instruction->GetLocations();
3867 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003868 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3869 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003870 DCHECK(second.IsConstant());
3871
Lena Djokic4b8025c2017-12-21 16:15:50 +01003872 if (instruction->GetResultType() == DataType::Type::kInt32) {
3873 Register out = locations->Out().AsRegister<Register>();
3874 Register dividend = locations->InAt(0).AsRegister<Register>();
3875 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3876 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3877 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003878
Lena Djokic4b8025c2017-12-21 16:15:50 +01003879 if (instruction->IsDiv()) {
3880 if (ctz_imm == 1) {
3881 // Fast path for division by +/-2, which is very common.
3882 __ Srl(TMP, dividend, 31);
3883 } else {
3884 __ Sra(TMP, dividend, 31);
3885 __ Srl(TMP, TMP, 32 - ctz_imm);
3886 }
3887 __ Addu(out, dividend, TMP);
3888 __ Sra(out, out, ctz_imm);
3889 if (imm < 0) {
3890 __ Subu(out, ZERO, out);
3891 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003892 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003893 if (ctz_imm == 1) {
3894 // Fast path for modulo +/-2, which is very common.
3895 __ Sra(TMP, dividend, 31);
3896 __ Subu(out, dividend, TMP);
3897 __ Andi(out, out, 1);
3898 __ Addu(out, out, TMP);
3899 } else {
3900 __ Sra(TMP, dividend, 31);
3901 __ Srl(TMP, TMP, 32 - ctz_imm);
3902 __ Addu(out, dividend, TMP);
3903 if (IsUint<16>(abs_imm - 1)) {
3904 __ Andi(out, out, abs_imm - 1);
3905 } else {
3906 if (is_r2_or_newer) {
3907 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3908 } else {
3909 __ Sll(out, out, 32 - ctz_imm);
3910 __ Srl(out, out, 32 - ctz_imm);
3911 }
3912 }
3913 __ Subu(out, out, TMP);
3914 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003915 }
3916 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003917 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3918 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3919 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3920 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3921 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3922 int64_t imm = Int64FromConstant(second.GetConstant());
3923 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3924 int ctz_imm = CTZ(abs_imm);
3925
3926 if (instruction->IsDiv()) {
3927 if (ctz_imm < 32) {
3928 if (ctz_imm == 1) {
3929 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003930 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003931 __ Sra(AT, in_high, 31);
3932 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003933 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003934 __ Addu(AT, AT, in_low);
3935 __ Sltu(TMP, AT, in_low);
3936 __ Addu(out_high, in_high, TMP);
3937 __ Srl(out_low, AT, ctz_imm);
3938 if (is_r2_or_newer) {
3939 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3940 __ Sra(out_high, out_high, ctz_imm);
3941 } else {
3942 __ Sll(AT, out_high, 32 - ctz_imm);
3943 __ Sra(out_high, out_high, ctz_imm);
3944 __ Or(out_low, out_low, AT);
3945 }
3946 if (imm < 0) {
3947 __ Subu(out_low, ZERO, out_low);
3948 __ Sltu(AT, ZERO, out_low);
3949 __ Subu(out_high, ZERO, out_high);
3950 __ Subu(out_high, out_high, AT);
3951 }
3952 } else if (ctz_imm == 32) {
3953 __ Sra(AT, in_high, 31);
3954 __ Addu(AT, AT, in_low);
3955 __ Sltu(AT, AT, in_low);
3956 __ Addu(out_low, in_high, AT);
3957 if (imm < 0) {
3958 __ Srl(TMP, out_low, 31);
3959 __ Subu(out_low, ZERO, out_low);
3960 __ Sltu(AT, ZERO, out_low);
3961 __ Subu(out_high, TMP, AT);
3962 } else {
3963 __ Sra(out_high, out_low, 31);
3964 }
3965 } else if (ctz_imm < 63) {
3966 __ Sra(AT, in_high, 31);
3967 __ Srl(TMP, AT, 64 - ctz_imm);
3968 __ Addu(AT, AT, in_low);
3969 __ Sltu(AT, AT, in_low);
3970 __ Addu(out_low, in_high, AT);
3971 __ Addu(out_low, out_low, TMP);
3972 __ Sra(out_low, out_low, ctz_imm - 32);
3973 if (imm < 0) {
3974 __ Subu(out_low, ZERO, out_low);
3975 }
3976 __ Sra(out_high, out_low, 31);
3977 } else {
3978 DCHECK_LT(imm, 0);
3979 if (is_r6) {
3980 __ Aui(AT, in_high, 0x8000);
3981 } else {
3982 __ Lui(AT, 0x8000);
3983 __ Xor(AT, AT, in_high);
3984 }
3985 __ Or(AT, AT, in_low);
3986 __ Sltiu(out_low, AT, 1);
3987 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003988 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003989 } else {
3990 if ((ctz_imm == 1) && !is_r6) {
3991 __ Andi(AT, in_low, 1);
3992 __ Sll(TMP, in_low, 31);
3993 __ And(TMP, in_high, TMP);
3994 __ Sra(out_high, TMP, 31);
3995 __ Or(out_low, out_high, AT);
3996 } else if (ctz_imm < 32) {
3997 __ Sra(AT, in_high, 31);
3998 if (ctz_imm <= 16) {
3999 __ Andi(out_low, in_low, abs_imm - 1);
4000 } else if (is_r2_or_newer) {
4001 __ Ext(out_low, in_low, 0, ctz_imm);
4002 } else {
4003 __ Sll(out_low, in_low, 32 - ctz_imm);
4004 __ Srl(out_low, out_low, 32 - ctz_imm);
4005 }
4006 if (is_r6) {
4007 __ Selnez(out_high, AT, out_low);
4008 } else {
4009 __ Movz(AT, ZERO, out_low);
4010 __ Move(out_high, AT);
4011 }
4012 if (is_r2_or_newer) {
4013 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
4014 } else {
4015 __ Sll(AT, out_high, ctz_imm);
4016 __ Or(out_low, out_low, AT);
4017 }
4018 } else if (ctz_imm == 32) {
4019 __ Sra(AT, in_high, 31);
4020 __ Move(out_low, in_low);
4021 if (is_r6) {
4022 __ Selnez(out_high, AT, out_low);
4023 } else {
4024 __ Movz(AT, ZERO, out_low);
4025 __ Move(out_high, AT);
4026 }
4027 } else if (ctz_imm < 63) {
4028 __ Sra(AT, in_high, 31);
4029 __ Move(TMP, in_low);
4030 if (ctz_imm - 32 <= 16) {
4031 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
4032 } else if (is_r2_or_newer) {
4033 __ Ext(out_high, in_high, 0, ctz_imm - 32);
4034 } else {
4035 __ Sll(out_high, in_high, 64 - ctz_imm);
4036 __ Srl(out_high, out_high, 64 - ctz_imm);
4037 }
4038 __ Move(out_low, TMP);
4039 __ Or(TMP, TMP, out_high);
4040 if (is_r6) {
4041 __ Selnez(AT, AT, TMP);
4042 } else {
4043 __ Movz(AT, ZERO, TMP);
4044 }
4045 if (is_r2_or_newer) {
4046 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
4047 } else {
4048 __ Sll(AT, AT, ctz_imm - 32);
4049 __ Or(out_high, out_high, AT);
4050 }
4051 } else {
4052 if (is_r6) {
4053 __ Aui(AT, in_high, 0x8000);
4054 } else {
4055 __ Lui(AT, 0x8000);
4056 __ Xor(AT, AT, in_high);
4057 }
4058 __ Or(AT, AT, in_low);
4059 __ Sltiu(AT, AT, 1);
4060 __ Sll(AT, AT, 31);
4061 __ Move(out_low, in_low);
4062 __ Xor(out_high, in_high, AT);
4063 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08004064 }
4065 }
4066}
4067
4068void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4069 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004070 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004071
4072 LocationSummary* locations = instruction->GetLocations();
4073 Location second = locations->InAt(1);
4074 DCHECK(second.IsConstant());
4075
4076 Register out = locations->Out().AsRegister<Register>();
4077 Register dividend = locations->InAt(0).AsRegister<Register>();
4078 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4079
4080 int64_t magic;
4081 int shift;
4082 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4083
4084 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4085
4086 __ LoadConst32(TMP, magic);
4087 if (isR6) {
4088 __ MuhR6(TMP, dividend, TMP);
4089 } else {
4090 __ MultR2(dividend, TMP);
4091 __ Mfhi(TMP);
4092 }
4093 if (imm > 0 && magic < 0) {
4094 __ Addu(TMP, TMP, dividend);
4095 } else if (imm < 0 && magic > 0) {
4096 __ Subu(TMP, TMP, dividend);
4097 }
4098
4099 if (shift != 0) {
4100 __ Sra(TMP, TMP, shift);
4101 }
4102
4103 if (instruction->IsDiv()) {
4104 __ Sra(out, TMP, 31);
4105 __ Subu(out, TMP, out);
4106 } else {
4107 __ Sra(AT, TMP, 31);
4108 __ Subu(AT, TMP, AT);
4109 __ LoadConst32(TMP, imm);
4110 if (isR6) {
4111 __ MulR6(TMP, AT, TMP);
4112 } else {
4113 __ MulR2(TMP, AT, TMP);
4114 }
4115 __ Subu(out, dividend, TMP);
4116 }
4117}
4118
4119void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4120 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004121 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004122
4123 LocationSummary* locations = instruction->GetLocations();
4124 Register out = locations->Out().AsRegister<Register>();
4125 Location second = locations->InAt(1);
4126
4127 if (second.IsConstant()) {
4128 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4129 if (imm == 0) {
4130 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4131 } else if (imm == 1 || imm == -1) {
4132 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004133 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004134 DivRemByPowerOfTwo(instruction);
4135 } else {
4136 DCHECK(imm <= -2 || imm >= 2);
4137 GenerateDivRemWithAnyConstant(instruction);
4138 }
4139 } else {
4140 Register dividend = locations->InAt(0).AsRegister<Register>();
4141 Register divisor = second.AsRegister<Register>();
4142 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4143 if (instruction->IsDiv()) {
4144 if (isR6) {
4145 __ DivR6(out, dividend, divisor);
4146 } else {
4147 __ DivR2(out, dividend, divisor);
4148 }
4149 } else {
4150 if (isR6) {
4151 __ ModR6(out, dividend, divisor);
4152 } else {
4153 __ ModR2(out, dividend, divisor);
4154 }
4155 }
4156 }
4157}
4158
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004159void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004160 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004161 bool call_long_div = false;
4162 if (type == DataType::Type::kInt64) {
4163 if (div->InputAt(1)->IsConstant()) {
4164 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4165 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4166 } else {
4167 call_long_div = true;
4168 }
4169 }
4170 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004171 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004172 : LocationSummary::kNoCall;
4173
Vladimir Markoca6fff82017-10-03 14:49:14 +01004174 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004175
4176 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004177 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004178 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004179 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004180 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4181 break;
4182
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004183 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004184 if (call_long_div) {
4185 InvokeRuntimeCallingConvention calling_convention;
4186 locations->SetInAt(0, Location::RegisterPairLocation(
4187 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4188 locations->SetInAt(1, Location::RegisterPairLocation(
4189 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4190 locations->SetOut(calling_convention.GetReturnLocation(type));
4191 } else {
4192 locations->SetInAt(0, Location::RequiresRegister());
4193 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4194 locations->SetOut(Location::RequiresRegister());
4195 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004196 break;
4197 }
4198
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004199 case DataType::Type::kFloat32:
4200 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004201 locations->SetInAt(0, Location::RequiresFpuRegister());
4202 locations->SetInAt(1, Location::RequiresFpuRegister());
4203 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4204 break;
4205
4206 default:
4207 LOG(FATAL) << "Unexpected div type " << type;
4208 }
4209}
4210
4211void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004212 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004213 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004214
4215 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004216 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004217 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004218 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004219 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004220 if (locations->InAt(1).IsConstant()) {
4221 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4222 if (imm == 0) {
4223 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4224 } else if (imm == 1 || imm == -1) {
4225 DivRemOneOrMinusOne(instruction);
4226 } else {
4227 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4228 DivRemByPowerOfTwo(instruction);
4229 }
4230 } else {
4231 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4232 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4233 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004234 break;
4235 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004236 case DataType::Type::kFloat32:
4237 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004238 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4239 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4240 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004241 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004242 __ DivS(dst, lhs, rhs);
4243 } else {
4244 __ DivD(dst, lhs, rhs);
4245 }
4246 break;
4247 }
4248 default:
4249 LOG(FATAL) << "Unexpected div type " << type;
4250 }
4251}
4252
4253void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004254 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004255 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004256}
4257
4258void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004259 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004260 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004261 codegen_->AddSlowPath(slow_path);
4262 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004263 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004264
4265 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004266 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004267 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004268 case DataType::Type::kInt8:
4269 case DataType::Type::kUint16:
4270 case DataType::Type::kInt16:
4271 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004272 if (value.IsConstant()) {
4273 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4274 __ B(slow_path->GetEntryLabel());
4275 } else {
4276 // A division by a non-null constant is valid. We don't need to perform
4277 // any check, so simply fall through.
4278 }
4279 } else {
4280 DCHECK(value.IsRegister()) << value;
4281 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4282 }
4283 break;
4284 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004285 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004286 if (value.IsConstant()) {
4287 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4288 __ B(slow_path->GetEntryLabel());
4289 } else {
4290 // A division by a non-null constant is valid. We don't need to perform
4291 // any check, so simply fall through.
4292 }
4293 } else {
4294 DCHECK(value.IsRegisterPair()) << value;
4295 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4296 __ Beqz(TMP, slow_path->GetEntryLabel());
4297 }
4298 break;
4299 }
4300 default:
4301 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4302 }
4303}
4304
4305void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4306 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004307 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004308 locations->SetOut(Location::ConstantLocation(constant));
4309}
4310
4311void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4312 // Will be generated at use site.
4313}
4314
4315void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4316 exit->SetLocations(nullptr);
4317}
4318
4319void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4320}
4321
4322void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4323 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004324 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004325 locations->SetOut(Location::ConstantLocation(constant));
4326}
4327
4328void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4329 // Will be generated at use site.
4330}
4331
4332void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4333 got->SetLocations(nullptr);
4334}
4335
4336void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004337 if (successor->IsExitBlock()) {
4338 DCHECK(got->GetPrevious()->AlwaysThrows());
4339 return; // no code needed
4340 }
4341
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004342 HBasicBlock* block = got->GetBlock();
4343 HInstruction* previous = got->GetPrevious();
4344 HLoopInformation* info = block->GetLoopInformation();
4345
4346 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004347 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4348 __ Lw(AT, SP, kCurrentMethodStackOffset);
4349 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4350 __ Addiu(TMP, TMP, 1);
4351 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4352 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004353 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4354 return;
4355 }
4356 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4357 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4358 }
4359 if (!codegen_->GoesToNextBlock(block, successor)) {
4360 __ B(codegen_->GetLabelOf(successor));
4361 }
4362}
4363
4364void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4365 HandleGoto(got, got->GetSuccessor());
4366}
4367
4368void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4369 try_boundary->SetLocations(nullptr);
4370}
4371
4372void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4373 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4374 if (!successor->IsExitBlock()) {
4375 HandleGoto(try_boundary, successor);
4376 }
4377}
4378
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004379void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4380 LocationSummary* locations) {
4381 Register dst = locations->Out().AsRegister<Register>();
4382 Register lhs = locations->InAt(0).AsRegister<Register>();
4383 Location rhs_location = locations->InAt(1);
4384 Register rhs_reg = ZERO;
4385 int64_t rhs_imm = 0;
4386 bool use_imm = rhs_location.IsConstant();
4387 if (use_imm) {
4388 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4389 } else {
4390 rhs_reg = rhs_location.AsRegister<Register>();
4391 }
4392
4393 switch (cond) {
4394 case kCondEQ:
4395 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004396 if (use_imm && IsInt<16>(-rhs_imm)) {
4397 if (rhs_imm == 0) {
4398 if (cond == kCondEQ) {
4399 __ Sltiu(dst, lhs, 1);
4400 } else {
4401 __ Sltu(dst, ZERO, lhs);
4402 }
4403 } else {
4404 __ Addiu(dst, lhs, -rhs_imm);
4405 if (cond == kCondEQ) {
4406 __ Sltiu(dst, dst, 1);
4407 } else {
4408 __ Sltu(dst, ZERO, dst);
4409 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004410 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004411 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004412 if (use_imm && IsUint<16>(rhs_imm)) {
4413 __ Xori(dst, lhs, rhs_imm);
4414 } else {
4415 if (use_imm) {
4416 rhs_reg = TMP;
4417 __ LoadConst32(rhs_reg, rhs_imm);
4418 }
4419 __ Xor(dst, lhs, rhs_reg);
4420 }
4421 if (cond == kCondEQ) {
4422 __ Sltiu(dst, dst, 1);
4423 } else {
4424 __ Sltu(dst, ZERO, dst);
4425 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004426 }
4427 break;
4428
4429 case kCondLT:
4430 case kCondGE:
4431 if (use_imm && IsInt<16>(rhs_imm)) {
4432 __ Slti(dst, lhs, rhs_imm);
4433 } else {
4434 if (use_imm) {
4435 rhs_reg = TMP;
4436 __ LoadConst32(rhs_reg, rhs_imm);
4437 }
4438 __ Slt(dst, lhs, rhs_reg);
4439 }
4440 if (cond == kCondGE) {
4441 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4442 // only the slt instruction but no sge.
4443 __ Xori(dst, dst, 1);
4444 }
4445 break;
4446
4447 case kCondLE:
4448 case kCondGT:
4449 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4450 // Simulate lhs <= rhs via lhs < rhs + 1.
4451 __ Slti(dst, lhs, rhs_imm + 1);
4452 if (cond == kCondGT) {
4453 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4454 // only the slti instruction but no sgti.
4455 __ Xori(dst, dst, 1);
4456 }
4457 } else {
4458 if (use_imm) {
4459 rhs_reg = TMP;
4460 __ LoadConst32(rhs_reg, rhs_imm);
4461 }
4462 __ Slt(dst, rhs_reg, lhs);
4463 if (cond == kCondLE) {
4464 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4465 // only the slt instruction but no sle.
4466 __ Xori(dst, dst, 1);
4467 }
4468 }
4469 break;
4470
4471 case kCondB:
4472 case kCondAE:
4473 if (use_imm && IsInt<16>(rhs_imm)) {
4474 // Sltiu sign-extends its 16-bit immediate operand before
4475 // the comparison and thus lets us compare directly with
4476 // unsigned values in the ranges [0, 0x7fff] and
4477 // [0xffff8000, 0xffffffff].
4478 __ Sltiu(dst, lhs, rhs_imm);
4479 } else {
4480 if (use_imm) {
4481 rhs_reg = TMP;
4482 __ LoadConst32(rhs_reg, rhs_imm);
4483 }
4484 __ Sltu(dst, lhs, rhs_reg);
4485 }
4486 if (cond == kCondAE) {
4487 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4488 // only the sltu instruction but no sgeu.
4489 __ Xori(dst, dst, 1);
4490 }
4491 break;
4492
4493 case kCondBE:
4494 case kCondA:
4495 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4496 // Simulate lhs <= rhs via lhs < rhs + 1.
4497 // Note that this only works if rhs + 1 does not overflow
4498 // to 0, hence the check above.
4499 // Sltiu sign-extends its 16-bit immediate operand before
4500 // the comparison and thus lets us compare directly with
4501 // unsigned values in the ranges [0, 0x7fff] and
4502 // [0xffff8000, 0xffffffff].
4503 __ Sltiu(dst, lhs, rhs_imm + 1);
4504 if (cond == kCondA) {
4505 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4506 // only the sltiu instruction but no sgtiu.
4507 __ Xori(dst, dst, 1);
4508 }
4509 } else {
4510 if (use_imm) {
4511 rhs_reg = TMP;
4512 __ LoadConst32(rhs_reg, rhs_imm);
4513 }
4514 __ Sltu(dst, rhs_reg, lhs);
4515 if (cond == kCondBE) {
4516 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4517 // only the sltu instruction but no sleu.
4518 __ Xori(dst, dst, 1);
4519 }
4520 }
4521 break;
4522 }
4523}
4524
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004525bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4526 LocationSummary* input_locations,
4527 Register dst) {
4528 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4529 Location rhs_location = input_locations->InAt(1);
4530 Register rhs_reg = ZERO;
4531 int64_t rhs_imm = 0;
4532 bool use_imm = rhs_location.IsConstant();
4533 if (use_imm) {
4534 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4535 } else {
4536 rhs_reg = rhs_location.AsRegister<Register>();
4537 }
4538
4539 switch (cond) {
4540 case kCondEQ:
4541 case kCondNE:
4542 if (use_imm && IsInt<16>(-rhs_imm)) {
4543 __ Addiu(dst, lhs, -rhs_imm);
4544 } else if (use_imm && IsUint<16>(rhs_imm)) {
4545 __ Xori(dst, lhs, rhs_imm);
4546 } else {
4547 if (use_imm) {
4548 rhs_reg = TMP;
4549 __ LoadConst32(rhs_reg, rhs_imm);
4550 }
4551 __ Xor(dst, lhs, rhs_reg);
4552 }
4553 return (cond == kCondEQ);
4554
4555 case kCondLT:
4556 case kCondGE:
4557 if (use_imm && IsInt<16>(rhs_imm)) {
4558 __ Slti(dst, lhs, rhs_imm);
4559 } else {
4560 if (use_imm) {
4561 rhs_reg = TMP;
4562 __ LoadConst32(rhs_reg, rhs_imm);
4563 }
4564 __ Slt(dst, lhs, rhs_reg);
4565 }
4566 return (cond == kCondGE);
4567
4568 case kCondLE:
4569 case kCondGT:
4570 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4571 // Simulate lhs <= rhs via lhs < rhs + 1.
4572 __ Slti(dst, lhs, rhs_imm + 1);
4573 return (cond == kCondGT);
4574 } else {
4575 if (use_imm) {
4576 rhs_reg = TMP;
4577 __ LoadConst32(rhs_reg, rhs_imm);
4578 }
4579 __ Slt(dst, rhs_reg, lhs);
4580 return (cond == kCondLE);
4581 }
4582
4583 case kCondB:
4584 case kCondAE:
4585 if (use_imm && IsInt<16>(rhs_imm)) {
4586 // Sltiu sign-extends its 16-bit immediate operand before
4587 // the comparison and thus lets us compare directly with
4588 // unsigned values in the ranges [0, 0x7fff] and
4589 // [0xffff8000, 0xffffffff].
4590 __ Sltiu(dst, lhs, rhs_imm);
4591 } else {
4592 if (use_imm) {
4593 rhs_reg = TMP;
4594 __ LoadConst32(rhs_reg, rhs_imm);
4595 }
4596 __ Sltu(dst, lhs, rhs_reg);
4597 }
4598 return (cond == kCondAE);
4599
4600 case kCondBE:
4601 case kCondA:
4602 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4603 // Simulate lhs <= rhs via lhs < rhs + 1.
4604 // Note that this only works if rhs + 1 does not overflow
4605 // to 0, hence the check above.
4606 // Sltiu sign-extends its 16-bit immediate operand before
4607 // the comparison and thus lets us compare directly with
4608 // unsigned values in the ranges [0, 0x7fff] and
4609 // [0xffff8000, 0xffffffff].
4610 __ Sltiu(dst, lhs, rhs_imm + 1);
4611 return (cond == kCondA);
4612 } else {
4613 if (use_imm) {
4614 rhs_reg = TMP;
4615 __ LoadConst32(rhs_reg, rhs_imm);
4616 }
4617 __ Sltu(dst, rhs_reg, lhs);
4618 return (cond == kCondBE);
4619 }
4620 }
4621}
4622
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004623void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4624 LocationSummary* locations,
4625 MipsLabel* label) {
4626 Register lhs = locations->InAt(0).AsRegister<Register>();
4627 Location rhs_location = locations->InAt(1);
4628 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004629 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004630 bool use_imm = rhs_location.IsConstant();
4631 if (use_imm) {
4632 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4633 } else {
4634 rhs_reg = rhs_location.AsRegister<Register>();
4635 }
4636
4637 if (use_imm && rhs_imm == 0) {
4638 switch (cond) {
4639 case kCondEQ:
4640 case kCondBE: // <= 0 if zero
4641 __ Beqz(lhs, label);
4642 break;
4643 case kCondNE:
4644 case kCondA: // > 0 if non-zero
4645 __ Bnez(lhs, label);
4646 break;
4647 case kCondLT:
4648 __ Bltz(lhs, label);
4649 break;
4650 case kCondGE:
4651 __ Bgez(lhs, label);
4652 break;
4653 case kCondLE:
4654 __ Blez(lhs, label);
4655 break;
4656 case kCondGT:
4657 __ Bgtz(lhs, label);
4658 break;
4659 case kCondB: // always false
4660 break;
4661 case kCondAE: // always true
4662 __ B(label);
4663 break;
4664 }
4665 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004666 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4667 if (isR6 || !use_imm) {
4668 if (use_imm) {
4669 rhs_reg = TMP;
4670 __ LoadConst32(rhs_reg, rhs_imm);
4671 }
4672 switch (cond) {
4673 case kCondEQ:
4674 __ Beq(lhs, rhs_reg, label);
4675 break;
4676 case kCondNE:
4677 __ Bne(lhs, rhs_reg, label);
4678 break;
4679 case kCondLT:
4680 __ Blt(lhs, rhs_reg, label);
4681 break;
4682 case kCondGE:
4683 __ Bge(lhs, rhs_reg, label);
4684 break;
4685 case kCondLE:
4686 __ Bge(rhs_reg, lhs, label);
4687 break;
4688 case kCondGT:
4689 __ Blt(rhs_reg, lhs, label);
4690 break;
4691 case kCondB:
4692 __ Bltu(lhs, rhs_reg, label);
4693 break;
4694 case kCondAE:
4695 __ Bgeu(lhs, rhs_reg, label);
4696 break;
4697 case kCondBE:
4698 __ Bgeu(rhs_reg, lhs, label);
4699 break;
4700 case kCondA:
4701 __ Bltu(rhs_reg, lhs, label);
4702 break;
4703 }
4704 } else {
4705 // Special cases for more efficient comparison with constants on R2.
4706 switch (cond) {
4707 case kCondEQ:
4708 __ LoadConst32(TMP, rhs_imm);
4709 __ Beq(lhs, TMP, label);
4710 break;
4711 case kCondNE:
4712 __ LoadConst32(TMP, rhs_imm);
4713 __ Bne(lhs, TMP, label);
4714 break;
4715 case kCondLT:
4716 if (IsInt<16>(rhs_imm)) {
4717 __ Slti(TMP, lhs, rhs_imm);
4718 __ Bnez(TMP, label);
4719 } else {
4720 __ LoadConst32(TMP, rhs_imm);
4721 __ Blt(lhs, TMP, label);
4722 }
4723 break;
4724 case kCondGE:
4725 if (IsInt<16>(rhs_imm)) {
4726 __ Slti(TMP, lhs, rhs_imm);
4727 __ Beqz(TMP, label);
4728 } else {
4729 __ LoadConst32(TMP, rhs_imm);
4730 __ Bge(lhs, TMP, label);
4731 }
4732 break;
4733 case kCondLE:
4734 if (IsInt<16>(rhs_imm + 1)) {
4735 // Simulate lhs <= rhs via lhs < rhs + 1.
4736 __ Slti(TMP, lhs, rhs_imm + 1);
4737 __ Bnez(TMP, label);
4738 } else {
4739 __ LoadConst32(TMP, rhs_imm);
4740 __ Bge(TMP, lhs, label);
4741 }
4742 break;
4743 case kCondGT:
4744 if (IsInt<16>(rhs_imm + 1)) {
4745 // Simulate lhs > rhs via !(lhs < rhs + 1).
4746 __ Slti(TMP, lhs, rhs_imm + 1);
4747 __ Beqz(TMP, label);
4748 } else {
4749 __ LoadConst32(TMP, rhs_imm);
4750 __ Blt(TMP, lhs, label);
4751 }
4752 break;
4753 case kCondB:
4754 if (IsInt<16>(rhs_imm)) {
4755 __ Sltiu(TMP, lhs, rhs_imm);
4756 __ Bnez(TMP, label);
4757 } else {
4758 __ LoadConst32(TMP, rhs_imm);
4759 __ Bltu(lhs, TMP, label);
4760 }
4761 break;
4762 case kCondAE:
4763 if (IsInt<16>(rhs_imm)) {
4764 __ Sltiu(TMP, lhs, rhs_imm);
4765 __ Beqz(TMP, label);
4766 } else {
4767 __ LoadConst32(TMP, rhs_imm);
4768 __ Bgeu(lhs, TMP, label);
4769 }
4770 break;
4771 case kCondBE:
4772 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4773 // Simulate lhs <= rhs via lhs < rhs + 1.
4774 // Note that this only works if rhs + 1 does not overflow
4775 // to 0, hence the check above.
4776 __ Sltiu(TMP, lhs, rhs_imm + 1);
4777 __ Bnez(TMP, label);
4778 } else {
4779 __ LoadConst32(TMP, rhs_imm);
4780 __ Bgeu(TMP, lhs, label);
4781 }
4782 break;
4783 case kCondA:
4784 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4785 // Simulate lhs > rhs via !(lhs < rhs + 1).
4786 // Note that this only works if rhs + 1 does not overflow
4787 // to 0, hence the check above.
4788 __ Sltiu(TMP, lhs, rhs_imm + 1);
4789 __ Beqz(TMP, label);
4790 } else {
4791 __ LoadConst32(TMP, rhs_imm);
4792 __ Bltu(TMP, lhs, label);
4793 }
4794 break;
4795 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004796 }
4797 }
4798}
4799
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004800void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4801 LocationSummary* locations) {
4802 Register dst = locations->Out().AsRegister<Register>();
4803 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4804 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4805 Location rhs_location = locations->InAt(1);
4806 Register rhs_high = ZERO;
4807 Register rhs_low = ZERO;
4808 int64_t imm = 0;
4809 uint32_t imm_high = 0;
4810 uint32_t imm_low = 0;
4811 bool use_imm = rhs_location.IsConstant();
4812 if (use_imm) {
4813 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4814 imm_high = High32Bits(imm);
4815 imm_low = Low32Bits(imm);
4816 } else {
4817 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4818 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4819 }
4820 if (use_imm && imm == 0) {
4821 switch (cond) {
4822 case kCondEQ:
4823 case kCondBE: // <= 0 if zero
4824 __ Or(dst, lhs_high, lhs_low);
4825 __ Sltiu(dst, dst, 1);
4826 break;
4827 case kCondNE:
4828 case kCondA: // > 0 if non-zero
4829 __ Or(dst, lhs_high, lhs_low);
4830 __ Sltu(dst, ZERO, dst);
4831 break;
4832 case kCondLT:
4833 __ Slt(dst, lhs_high, ZERO);
4834 break;
4835 case kCondGE:
4836 __ Slt(dst, lhs_high, ZERO);
4837 __ Xori(dst, dst, 1);
4838 break;
4839 case kCondLE:
4840 __ Or(TMP, lhs_high, lhs_low);
4841 __ Sra(AT, lhs_high, 31);
4842 __ Sltu(dst, AT, TMP);
4843 __ Xori(dst, dst, 1);
4844 break;
4845 case kCondGT:
4846 __ Or(TMP, lhs_high, lhs_low);
4847 __ Sra(AT, lhs_high, 31);
4848 __ Sltu(dst, AT, TMP);
4849 break;
4850 case kCondB: // always false
4851 __ Andi(dst, dst, 0);
4852 break;
4853 case kCondAE: // always true
4854 __ Ori(dst, ZERO, 1);
4855 break;
4856 }
4857 } else if (use_imm) {
4858 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4859 switch (cond) {
4860 case kCondEQ:
4861 __ LoadConst32(TMP, imm_high);
4862 __ Xor(TMP, TMP, lhs_high);
4863 __ LoadConst32(AT, imm_low);
4864 __ Xor(AT, AT, lhs_low);
4865 __ Or(dst, TMP, AT);
4866 __ Sltiu(dst, dst, 1);
4867 break;
4868 case kCondNE:
4869 __ LoadConst32(TMP, imm_high);
4870 __ Xor(TMP, TMP, lhs_high);
4871 __ LoadConst32(AT, imm_low);
4872 __ Xor(AT, AT, lhs_low);
4873 __ Or(dst, TMP, AT);
4874 __ Sltu(dst, ZERO, dst);
4875 break;
4876 case kCondLT:
4877 case kCondGE:
4878 if (dst == lhs_low) {
4879 __ LoadConst32(TMP, imm_low);
4880 __ Sltu(dst, lhs_low, TMP);
4881 }
4882 __ LoadConst32(TMP, imm_high);
4883 __ Slt(AT, lhs_high, TMP);
4884 __ Slt(TMP, TMP, lhs_high);
4885 if (dst != lhs_low) {
4886 __ LoadConst32(dst, imm_low);
4887 __ Sltu(dst, lhs_low, dst);
4888 }
4889 __ Slt(dst, TMP, dst);
4890 __ Or(dst, dst, AT);
4891 if (cond == kCondGE) {
4892 __ Xori(dst, dst, 1);
4893 }
4894 break;
4895 case kCondGT:
4896 case kCondLE:
4897 if (dst == lhs_low) {
4898 __ LoadConst32(TMP, imm_low);
4899 __ Sltu(dst, TMP, lhs_low);
4900 }
4901 __ LoadConst32(TMP, imm_high);
4902 __ Slt(AT, TMP, lhs_high);
4903 __ Slt(TMP, lhs_high, TMP);
4904 if (dst != lhs_low) {
4905 __ LoadConst32(dst, imm_low);
4906 __ Sltu(dst, dst, lhs_low);
4907 }
4908 __ Slt(dst, TMP, dst);
4909 __ Or(dst, dst, AT);
4910 if (cond == kCondLE) {
4911 __ Xori(dst, dst, 1);
4912 }
4913 break;
4914 case kCondB:
4915 case kCondAE:
4916 if (dst == lhs_low) {
4917 __ LoadConst32(TMP, imm_low);
4918 __ Sltu(dst, lhs_low, TMP);
4919 }
4920 __ LoadConst32(TMP, imm_high);
4921 __ Sltu(AT, lhs_high, TMP);
4922 __ Sltu(TMP, TMP, lhs_high);
4923 if (dst != lhs_low) {
4924 __ LoadConst32(dst, imm_low);
4925 __ Sltu(dst, lhs_low, dst);
4926 }
4927 __ Slt(dst, TMP, dst);
4928 __ Or(dst, dst, AT);
4929 if (cond == kCondAE) {
4930 __ Xori(dst, dst, 1);
4931 }
4932 break;
4933 case kCondA:
4934 case kCondBE:
4935 if (dst == lhs_low) {
4936 __ LoadConst32(TMP, imm_low);
4937 __ Sltu(dst, TMP, lhs_low);
4938 }
4939 __ LoadConst32(TMP, imm_high);
4940 __ Sltu(AT, TMP, lhs_high);
4941 __ Sltu(TMP, lhs_high, TMP);
4942 if (dst != lhs_low) {
4943 __ LoadConst32(dst, imm_low);
4944 __ Sltu(dst, dst, lhs_low);
4945 }
4946 __ Slt(dst, TMP, dst);
4947 __ Or(dst, dst, AT);
4948 if (cond == kCondBE) {
4949 __ Xori(dst, dst, 1);
4950 }
4951 break;
4952 }
4953 } else {
4954 switch (cond) {
4955 case kCondEQ:
4956 __ Xor(TMP, lhs_high, rhs_high);
4957 __ Xor(AT, lhs_low, rhs_low);
4958 __ Or(dst, TMP, AT);
4959 __ Sltiu(dst, dst, 1);
4960 break;
4961 case kCondNE:
4962 __ Xor(TMP, lhs_high, rhs_high);
4963 __ Xor(AT, lhs_low, rhs_low);
4964 __ Or(dst, TMP, AT);
4965 __ Sltu(dst, ZERO, dst);
4966 break;
4967 case kCondLT:
4968 case kCondGE:
4969 __ Slt(TMP, rhs_high, lhs_high);
4970 __ Sltu(AT, lhs_low, rhs_low);
4971 __ Slt(TMP, TMP, AT);
4972 __ Slt(AT, lhs_high, rhs_high);
4973 __ Or(dst, AT, TMP);
4974 if (cond == kCondGE) {
4975 __ Xori(dst, dst, 1);
4976 }
4977 break;
4978 case kCondGT:
4979 case kCondLE:
4980 __ Slt(TMP, lhs_high, rhs_high);
4981 __ Sltu(AT, rhs_low, lhs_low);
4982 __ Slt(TMP, TMP, AT);
4983 __ Slt(AT, rhs_high, lhs_high);
4984 __ Or(dst, AT, TMP);
4985 if (cond == kCondLE) {
4986 __ Xori(dst, dst, 1);
4987 }
4988 break;
4989 case kCondB:
4990 case kCondAE:
4991 __ Sltu(TMP, rhs_high, lhs_high);
4992 __ Sltu(AT, lhs_low, rhs_low);
4993 __ Slt(TMP, TMP, AT);
4994 __ Sltu(AT, lhs_high, rhs_high);
4995 __ Or(dst, AT, TMP);
4996 if (cond == kCondAE) {
4997 __ Xori(dst, dst, 1);
4998 }
4999 break;
5000 case kCondA:
5001 case kCondBE:
5002 __ Sltu(TMP, lhs_high, rhs_high);
5003 __ Sltu(AT, rhs_low, lhs_low);
5004 __ Slt(TMP, TMP, AT);
5005 __ Sltu(AT, rhs_high, lhs_high);
5006 __ Or(dst, AT, TMP);
5007 if (cond == kCondBE) {
5008 __ Xori(dst, dst, 1);
5009 }
5010 break;
5011 }
5012 }
5013}
5014
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005015void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
5016 LocationSummary* locations,
5017 MipsLabel* label) {
5018 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5019 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5020 Location rhs_location = locations->InAt(1);
5021 Register rhs_high = ZERO;
5022 Register rhs_low = ZERO;
5023 int64_t imm = 0;
5024 uint32_t imm_high = 0;
5025 uint32_t imm_low = 0;
5026 bool use_imm = rhs_location.IsConstant();
5027 if (use_imm) {
5028 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
5029 imm_high = High32Bits(imm);
5030 imm_low = Low32Bits(imm);
5031 } else {
5032 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
5033 rhs_low = rhs_location.AsRegisterPairLow<Register>();
5034 }
5035
5036 if (use_imm && imm == 0) {
5037 switch (cond) {
5038 case kCondEQ:
5039 case kCondBE: // <= 0 if zero
5040 __ Or(TMP, lhs_high, lhs_low);
5041 __ Beqz(TMP, label);
5042 break;
5043 case kCondNE:
5044 case kCondA: // > 0 if non-zero
5045 __ Or(TMP, lhs_high, lhs_low);
5046 __ Bnez(TMP, label);
5047 break;
5048 case kCondLT:
5049 __ Bltz(lhs_high, label);
5050 break;
5051 case kCondGE:
5052 __ Bgez(lhs_high, label);
5053 break;
5054 case kCondLE:
5055 __ Or(TMP, lhs_high, lhs_low);
5056 __ Sra(AT, lhs_high, 31);
5057 __ Bgeu(AT, TMP, label);
5058 break;
5059 case kCondGT:
5060 __ Or(TMP, lhs_high, lhs_low);
5061 __ Sra(AT, lhs_high, 31);
5062 __ Bltu(AT, TMP, label);
5063 break;
5064 case kCondB: // always false
5065 break;
5066 case kCondAE: // always true
5067 __ B(label);
5068 break;
5069 }
5070 } else if (use_imm) {
5071 // TODO: more efficient comparison with constants without loading them into TMP/AT.
5072 switch (cond) {
5073 case kCondEQ:
5074 __ LoadConst32(TMP, imm_high);
5075 __ Xor(TMP, TMP, lhs_high);
5076 __ LoadConst32(AT, imm_low);
5077 __ Xor(AT, AT, lhs_low);
5078 __ Or(TMP, TMP, AT);
5079 __ Beqz(TMP, label);
5080 break;
5081 case kCondNE:
5082 __ LoadConst32(TMP, imm_high);
5083 __ Xor(TMP, TMP, lhs_high);
5084 __ LoadConst32(AT, imm_low);
5085 __ Xor(AT, AT, lhs_low);
5086 __ Or(TMP, TMP, AT);
5087 __ Bnez(TMP, label);
5088 break;
5089 case kCondLT:
5090 __ LoadConst32(TMP, imm_high);
5091 __ Blt(lhs_high, TMP, label);
5092 __ Slt(TMP, TMP, lhs_high);
5093 __ LoadConst32(AT, imm_low);
5094 __ Sltu(AT, lhs_low, AT);
5095 __ Blt(TMP, AT, label);
5096 break;
5097 case kCondGE:
5098 __ LoadConst32(TMP, imm_high);
5099 __ Blt(TMP, lhs_high, label);
5100 __ Slt(TMP, lhs_high, TMP);
5101 __ LoadConst32(AT, imm_low);
5102 __ Sltu(AT, lhs_low, AT);
5103 __ Or(TMP, TMP, AT);
5104 __ Beqz(TMP, label);
5105 break;
5106 case kCondLE:
5107 __ LoadConst32(TMP, imm_high);
5108 __ Blt(lhs_high, TMP, label);
5109 __ Slt(TMP, TMP, lhs_high);
5110 __ LoadConst32(AT, imm_low);
5111 __ Sltu(AT, AT, lhs_low);
5112 __ Or(TMP, TMP, AT);
5113 __ Beqz(TMP, label);
5114 break;
5115 case kCondGT:
5116 __ LoadConst32(TMP, imm_high);
5117 __ Blt(TMP, lhs_high, label);
5118 __ Slt(TMP, lhs_high, TMP);
5119 __ LoadConst32(AT, imm_low);
5120 __ Sltu(AT, AT, lhs_low);
5121 __ Blt(TMP, AT, label);
5122 break;
5123 case kCondB:
5124 __ LoadConst32(TMP, imm_high);
5125 __ Bltu(lhs_high, TMP, label);
5126 __ Sltu(TMP, TMP, lhs_high);
5127 __ LoadConst32(AT, imm_low);
5128 __ Sltu(AT, lhs_low, AT);
5129 __ Blt(TMP, AT, label);
5130 break;
5131 case kCondAE:
5132 __ LoadConst32(TMP, imm_high);
5133 __ Bltu(TMP, lhs_high, label);
5134 __ Sltu(TMP, lhs_high, TMP);
5135 __ LoadConst32(AT, imm_low);
5136 __ Sltu(AT, lhs_low, AT);
5137 __ Or(TMP, TMP, AT);
5138 __ Beqz(TMP, label);
5139 break;
5140 case kCondBE:
5141 __ LoadConst32(TMP, imm_high);
5142 __ Bltu(lhs_high, TMP, label);
5143 __ Sltu(TMP, TMP, lhs_high);
5144 __ LoadConst32(AT, imm_low);
5145 __ Sltu(AT, AT, lhs_low);
5146 __ Or(TMP, TMP, AT);
5147 __ Beqz(TMP, label);
5148 break;
5149 case kCondA:
5150 __ LoadConst32(TMP, imm_high);
5151 __ Bltu(TMP, lhs_high, label);
5152 __ Sltu(TMP, lhs_high, TMP);
5153 __ LoadConst32(AT, imm_low);
5154 __ Sltu(AT, AT, lhs_low);
5155 __ Blt(TMP, AT, label);
5156 break;
5157 }
5158 } else {
5159 switch (cond) {
5160 case kCondEQ:
5161 __ Xor(TMP, lhs_high, rhs_high);
5162 __ Xor(AT, lhs_low, rhs_low);
5163 __ Or(TMP, TMP, AT);
5164 __ Beqz(TMP, label);
5165 break;
5166 case kCondNE:
5167 __ Xor(TMP, lhs_high, rhs_high);
5168 __ Xor(AT, lhs_low, rhs_low);
5169 __ Or(TMP, TMP, AT);
5170 __ Bnez(TMP, label);
5171 break;
5172 case kCondLT:
5173 __ Blt(lhs_high, rhs_high, label);
5174 __ Slt(TMP, rhs_high, lhs_high);
5175 __ Sltu(AT, lhs_low, rhs_low);
5176 __ Blt(TMP, AT, label);
5177 break;
5178 case kCondGE:
5179 __ Blt(rhs_high, lhs_high, label);
5180 __ Slt(TMP, lhs_high, rhs_high);
5181 __ Sltu(AT, lhs_low, rhs_low);
5182 __ Or(TMP, TMP, AT);
5183 __ Beqz(TMP, label);
5184 break;
5185 case kCondLE:
5186 __ Blt(lhs_high, rhs_high, label);
5187 __ Slt(TMP, rhs_high, lhs_high);
5188 __ Sltu(AT, rhs_low, lhs_low);
5189 __ Or(TMP, TMP, AT);
5190 __ Beqz(TMP, label);
5191 break;
5192 case kCondGT:
5193 __ Blt(rhs_high, lhs_high, label);
5194 __ Slt(TMP, lhs_high, rhs_high);
5195 __ Sltu(AT, rhs_low, lhs_low);
5196 __ Blt(TMP, AT, label);
5197 break;
5198 case kCondB:
5199 __ Bltu(lhs_high, rhs_high, label);
5200 __ Sltu(TMP, rhs_high, lhs_high);
5201 __ Sltu(AT, lhs_low, rhs_low);
5202 __ Blt(TMP, AT, label);
5203 break;
5204 case kCondAE:
5205 __ Bltu(rhs_high, lhs_high, label);
5206 __ Sltu(TMP, lhs_high, rhs_high);
5207 __ Sltu(AT, lhs_low, rhs_low);
5208 __ Or(TMP, TMP, AT);
5209 __ Beqz(TMP, label);
5210 break;
5211 case kCondBE:
5212 __ Bltu(lhs_high, rhs_high, label);
5213 __ Sltu(TMP, rhs_high, lhs_high);
5214 __ Sltu(AT, rhs_low, lhs_low);
5215 __ Or(TMP, TMP, AT);
5216 __ Beqz(TMP, label);
5217 break;
5218 case kCondA:
5219 __ Bltu(rhs_high, lhs_high, label);
5220 __ Sltu(TMP, lhs_high, rhs_high);
5221 __ Sltu(AT, rhs_low, lhs_low);
5222 __ Blt(TMP, AT, label);
5223 break;
5224 }
5225 }
5226}
5227
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005228void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5229 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005230 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005231 LocationSummary* locations) {
5232 Register dst = locations->Out().AsRegister<Register>();
5233 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5234 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5235 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005236 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005237 if (isR6) {
5238 switch (cond) {
5239 case kCondEQ:
5240 __ CmpEqS(FTMP, lhs, rhs);
5241 __ Mfc1(dst, FTMP);
5242 __ Andi(dst, dst, 1);
5243 break;
5244 case kCondNE:
5245 __ CmpEqS(FTMP, lhs, rhs);
5246 __ Mfc1(dst, FTMP);
5247 __ Addiu(dst, dst, 1);
5248 break;
5249 case kCondLT:
5250 if (gt_bias) {
5251 __ CmpLtS(FTMP, lhs, rhs);
5252 } else {
5253 __ CmpUltS(FTMP, lhs, rhs);
5254 }
5255 __ Mfc1(dst, FTMP);
5256 __ Andi(dst, dst, 1);
5257 break;
5258 case kCondLE:
5259 if (gt_bias) {
5260 __ CmpLeS(FTMP, lhs, rhs);
5261 } else {
5262 __ CmpUleS(FTMP, lhs, rhs);
5263 }
5264 __ Mfc1(dst, FTMP);
5265 __ Andi(dst, dst, 1);
5266 break;
5267 case kCondGT:
5268 if (gt_bias) {
5269 __ CmpUltS(FTMP, rhs, lhs);
5270 } else {
5271 __ CmpLtS(FTMP, rhs, lhs);
5272 }
5273 __ Mfc1(dst, FTMP);
5274 __ Andi(dst, dst, 1);
5275 break;
5276 case kCondGE:
5277 if (gt_bias) {
5278 __ CmpUleS(FTMP, rhs, lhs);
5279 } else {
5280 __ CmpLeS(FTMP, rhs, lhs);
5281 }
5282 __ Mfc1(dst, FTMP);
5283 __ Andi(dst, dst, 1);
5284 break;
5285 default:
5286 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5287 UNREACHABLE();
5288 }
5289 } else {
5290 switch (cond) {
5291 case kCondEQ:
5292 __ CeqS(0, lhs, rhs);
5293 __ LoadConst32(dst, 1);
5294 __ Movf(dst, ZERO, 0);
5295 break;
5296 case kCondNE:
5297 __ CeqS(0, lhs, rhs);
5298 __ LoadConst32(dst, 1);
5299 __ Movt(dst, ZERO, 0);
5300 break;
5301 case kCondLT:
5302 if (gt_bias) {
5303 __ ColtS(0, lhs, rhs);
5304 } else {
5305 __ CultS(0, lhs, rhs);
5306 }
5307 __ LoadConst32(dst, 1);
5308 __ Movf(dst, ZERO, 0);
5309 break;
5310 case kCondLE:
5311 if (gt_bias) {
5312 __ ColeS(0, lhs, rhs);
5313 } else {
5314 __ CuleS(0, lhs, rhs);
5315 }
5316 __ LoadConst32(dst, 1);
5317 __ Movf(dst, ZERO, 0);
5318 break;
5319 case kCondGT:
5320 if (gt_bias) {
5321 __ CultS(0, rhs, lhs);
5322 } else {
5323 __ ColtS(0, rhs, lhs);
5324 }
5325 __ LoadConst32(dst, 1);
5326 __ Movf(dst, ZERO, 0);
5327 break;
5328 case kCondGE:
5329 if (gt_bias) {
5330 __ CuleS(0, rhs, lhs);
5331 } else {
5332 __ ColeS(0, rhs, lhs);
5333 }
5334 __ LoadConst32(dst, 1);
5335 __ Movf(dst, ZERO, 0);
5336 break;
5337 default:
5338 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5339 UNREACHABLE();
5340 }
5341 }
5342 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005343 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005344 if (isR6) {
5345 switch (cond) {
5346 case kCondEQ:
5347 __ CmpEqD(FTMP, lhs, rhs);
5348 __ Mfc1(dst, FTMP);
5349 __ Andi(dst, dst, 1);
5350 break;
5351 case kCondNE:
5352 __ CmpEqD(FTMP, lhs, rhs);
5353 __ Mfc1(dst, FTMP);
5354 __ Addiu(dst, dst, 1);
5355 break;
5356 case kCondLT:
5357 if (gt_bias) {
5358 __ CmpLtD(FTMP, lhs, rhs);
5359 } else {
5360 __ CmpUltD(FTMP, lhs, rhs);
5361 }
5362 __ Mfc1(dst, FTMP);
5363 __ Andi(dst, dst, 1);
5364 break;
5365 case kCondLE:
5366 if (gt_bias) {
5367 __ CmpLeD(FTMP, lhs, rhs);
5368 } else {
5369 __ CmpUleD(FTMP, lhs, rhs);
5370 }
5371 __ Mfc1(dst, FTMP);
5372 __ Andi(dst, dst, 1);
5373 break;
5374 case kCondGT:
5375 if (gt_bias) {
5376 __ CmpUltD(FTMP, rhs, lhs);
5377 } else {
5378 __ CmpLtD(FTMP, rhs, lhs);
5379 }
5380 __ Mfc1(dst, FTMP);
5381 __ Andi(dst, dst, 1);
5382 break;
5383 case kCondGE:
5384 if (gt_bias) {
5385 __ CmpUleD(FTMP, rhs, lhs);
5386 } else {
5387 __ CmpLeD(FTMP, rhs, lhs);
5388 }
5389 __ Mfc1(dst, FTMP);
5390 __ Andi(dst, dst, 1);
5391 break;
5392 default:
5393 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5394 UNREACHABLE();
5395 }
5396 } else {
5397 switch (cond) {
5398 case kCondEQ:
5399 __ CeqD(0, lhs, rhs);
5400 __ LoadConst32(dst, 1);
5401 __ Movf(dst, ZERO, 0);
5402 break;
5403 case kCondNE:
5404 __ CeqD(0, lhs, rhs);
5405 __ LoadConst32(dst, 1);
5406 __ Movt(dst, ZERO, 0);
5407 break;
5408 case kCondLT:
5409 if (gt_bias) {
5410 __ ColtD(0, lhs, rhs);
5411 } else {
5412 __ CultD(0, lhs, rhs);
5413 }
5414 __ LoadConst32(dst, 1);
5415 __ Movf(dst, ZERO, 0);
5416 break;
5417 case kCondLE:
5418 if (gt_bias) {
5419 __ ColeD(0, lhs, rhs);
5420 } else {
5421 __ CuleD(0, lhs, rhs);
5422 }
5423 __ LoadConst32(dst, 1);
5424 __ Movf(dst, ZERO, 0);
5425 break;
5426 case kCondGT:
5427 if (gt_bias) {
5428 __ CultD(0, rhs, lhs);
5429 } else {
5430 __ ColtD(0, rhs, lhs);
5431 }
5432 __ LoadConst32(dst, 1);
5433 __ Movf(dst, ZERO, 0);
5434 break;
5435 case kCondGE:
5436 if (gt_bias) {
5437 __ CuleD(0, rhs, lhs);
5438 } else {
5439 __ ColeD(0, rhs, lhs);
5440 }
5441 __ LoadConst32(dst, 1);
5442 __ Movf(dst, ZERO, 0);
5443 break;
5444 default:
5445 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5446 UNREACHABLE();
5447 }
5448 }
5449 }
5450}
5451
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005452bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5453 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005454 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005455 LocationSummary* input_locations,
5456 int cc) {
5457 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5458 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5459 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005460 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005461 switch (cond) {
5462 case kCondEQ:
5463 __ CeqS(cc, lhs, rhs);
5464 return false;
5465 case kCondNE:
5466 __ CeqS(cc, lhs, rhs);
5467 return true;
5468 case kCondLT:
5469 if (gt_bias) {
5470 __ ColtS(cc, lhs, rhs);
5471 } else {
5472 __ CultS(cc, lhs, rhs);
5473 }
5474 return false;
5475 case kCondLE:
5476 if (gt_bias) {
5477 __ ColeS(cc, lhs, rhs);
5478 } else {
5479 __ CuleS(cc, lhs, rhs);
5480 }
5481 return false;
5482 case kCondGT:
5483 if (gt_bias) {
5484 __ CultS(cc, rhs, lhs);
5485 } else {
5486 __ ColtS(cc, rhs, lhs);
5487 }
5488 return false;
5489 case kCondGE:
5490 if (gt_bias) {
5491 __ CuleS(cc, rhs, lhs);
5492 } else {
5493 __ ColeS(cc, rhs, lhs);
5494 }
5495 return false;
5496 default:
5497 LOG(FATAL) << "Unexpected non-floating-point condition";
5498 UNREACHABLE();
5499 }
5500 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005501 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005502 switch (cond) {
5503 case kCondEQ:
5504 __ CeqD(cc, lhs, rhs);
5505 return false;
5506 case kCondNE:
5507 __ CeqD(cc, lhs, rhs);
5508 return true;
5509 case kCondLT:
5510 if (gt_bias) {
5511 __ ColtD(cc, lhs, rhs);
5512 } else {
5513 __ CultD(cc, lhs, rhs);
5514 }
5515 return false;
5516 case kCondLE:
5517 if (gt_bias) {
5518 __ ColeD(cc, lhs, rhs);
5519 } else {
5520 __ CuleD(cc, lhs, rhs);
5521 }
5522 return false;
5523 case kCondGT:
5524 if (gt_bias) {
5525 __ CultD(cc, rhs, lhs);
5526 } else {
5527 __ ColtD(cc, rhs, lhs);
5528 }
5529 return false;
5530 case kCondGE:
5531 if (gt_bias) {
5532 __ CuleD(cc, rhs, lhs);
5533 } else {
5534 __ ColeD(cc, rhs, lhs);
5535 }
5536 return false;
5537 default:
5538 LOG(FATAL) << "Unexpected non-floating-point condition";
5539 UNREACHABLE();
5540 }
5541 }
5542}
5543
5544bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5545 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005546 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005547 LocationSummary* input_locations,
5548 FRegister dst) {
5549 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5550 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5551 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005552 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005553 switch (cond) {
5554 case kCondEQ:
5555 __ CmpEqS(dst, lhs, rhs);
5556 return false;
5557 case kCondNE:
5558 __ CmpEqS(dst, lhs, rhs);
5559 return true;
5560 case kCondLT:
5561 if (gt_bias) {
5562 __ CmpLtS(dst, lhs, rhs);
5563 } else {
5564 __ CmpUltS(dst, lhs, rhs);
5565 }
5566 return false;
5567 case kCondLE:
5568 if (gt_bias) {
5569 __ CmpLeS(dst, lhs, rhs);
5570 } else {
5571 __ CmpUleS(dst, lhs, rhs);
5572 }
5573 return false;
5574 case kCondGT:
5575 if (gt_bias) {
5576 __ CmpUltS(dst, rhs, lhs);
5577 } else {
5578 __ CmpLtS(dst, rhs, lhs);
5579 }
5580 return false;
5581 case kCondGE:
5582 if (gt_bias) {
5583 __ CmpUleS(dst, rhs, lhs);
5584 } else {
5585 __ CmpLeS(dst, rhs, lhs);
5586 }
5587 return false;
5588 default:
5589 LOG(FATAL) << "Unexpected non-floating-point condition";
5590 UNREACHABLE();
5591 }
5592 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005593 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005594 switch (cond) {
5595 case kCondEQ:
5596 __ CmpEqD(dst, lhs, rhs);
5597 return false;
5598 case kCondNE:
5599 __ CmpEqD(dst, lhs, rhs);
5600 return true;
5601 case kCondLT:
5602 if (gt_bias) {
5603 __ CmpLtD(dst, lhs, rhs);
5604 } else {
5605 __ CmpUltD(dst, lhs, rhs);
5606 }
5607 return false;
5608 case kCondLE:
5609 if (gt_bias) {
5610 __ CmpLeD(dst, lhs, rhs);
5611 } else {
5612 __ CmpUleD(dst, lhs, rhs);
5613 }
5614 return false;
5615 case kCondGT:
5616 if (gt_bias) {
5617 __ CmpUltD(dst, rhs, lhs);
5618 } else {
5619 __ CmpLtD(dst, rhs, lhs);
5620 }
5621 return false;
5622 case kCondGE:
5623 if (gt_bias) {
5624 __ CmpUleD(dst, rhs, lhs);
5625 } else {
5626 __ CmpLeD(dst, rhs, lhs);
5627 }
5628 return false;
5629 default:
5630 LOG(FATAL) << "Unexpected non-floating-point condition";
5631 UNREACHABLE();
5632 }
5633 }
5634}
5635
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005636void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5637 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005638 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005639 LocationSummary* locations,
5640 MipsLabel* label) {
5641 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5642 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5643 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005644 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005645 if (isR6) {
5646 switch (cond) {
5647 case kCondEQ:
5648 __ CmpEqS(FTMP, lhs, rhs);
5649 __ Bc1nez(FTMP, label);
5650 break;
5651 case kCondNE:
5652 __ CmpEqS(FTMP, lhs, rhs);
5653 __ Bc1eqz(FTMP, label);
5654 break;
5655 case kCondLT:
5656 if (gt_bias) {
5657 __ CmpLtS(FTMP, lhs, rhs);
5658 } else {
5659 __ CmpUltS(FTMP, lhs, rhs);
5660 }
5661 __ Bc1nez(FTMP, label);
5662 break;
5663 case kCondLE:
5664 if (gt_bias) {
5665 __ CmpLeS(FTMP, lhs, rhs);
5666 } else {
5667 __ CmpUleS(FTMP, lhs, rhs);
5668 }
5669 __ Bc1nez(FTMP, label);
5670 break;
5671 case kCondGT:
5672 if (gt_bias) {
5673 __ CmpUltS(FTMP, rhs, lhs);
5674 } else {
5675 __ CmpLtS(FTMP, rhs, lhs);
5676 }
5677 __ Bc1nez(FTMP, label);
5678 break;
5679 case kCondGE:
5680 if (gt_bias) {
5681 __ CmpUleS(FTMP, rhs, lhs);
5682 } else {
5683 __ CmpLeS(FTMP, rhs, lhs);
5684 }
5685 __ Bc1nez(FTMP, label);
5686 break;
5687 default:
5688 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005689 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005690 }
5691 } else {
5692 switch (cond) {
5693 case kCondEQ:
5694 __ CeqS(0, lhs, rhs);
5695 __ Bc1t(0, label);
5696 break;
5697 case kCondNE:
5698 __ CeqS(0, lhs, rhs);
5699 __ Bc1f(0, label);
5700 break;
5701 case kCondLT:
5702 if (gt_bias) {
5703 __ ColtS(0, lhs, rhs);
5704 } else {
5705 __ CultS(0, lhs, rhs);
5706 }
5707 __ Bc1t(0, label);
5708 break;
5709 case kCondLE:
5710 if (gt_bias) {
5711 __ ColeS(0, lhs, rhs);
5712 } else {
5713 __ CuleS(0, lhs, rhs);
5714 }
5715 __ Bc1t(0, label);
5716 break;
5717 case kCondGT:
5718 if (gt_bias) {
5719 __ CultS(0, rhs, lhs);
5720 } else {
5721 __ ColtS(0, rhs, lhs);
5722 }
5723 __ Bc1t(0, label);
5724 break;
5725 case kCondGE:
5726 if (gt_bias) {
5727 __ CuleS(0, rhs, lhs);
5728 } else {
5729 __ ColeS(0, rhs, lhs);
5730 }
5731 __ Bc1t(0, label);
5732 break;
5733 default:
5734 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005735 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005736 }
5737 }
5738 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005739 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005740 if (isR6) {
5741 switch (cond) {
5742 case kCondEQ:
5743 __ CmpEqD(FTMP, lhs, rhs);
5744 __ Bc1nez(FTMP, label);
5745 break;
5746 case kCondNE:
5747 __ CmpEqD(FTMP, lhs, rhs);
5748 __ Bc1eqz(FTMP, label);
5749 break;
5750 case kCondLT:
5751 if (gt_bias) {
5752 __ CmpLtD(FTMP, lhs, rhs);
5753 } else {
5754 __ CmpUltD(FTMP, lhs, rhs);
5755 }
5756 __ Bc1nez(FTMP, label);
5757 break;
5758 case kCondLE:
5759 if (gt_bias) {
5760 __ CmpLeD(FTMP, lhs, rhs);
5761 } else {
5762 __ CmpUleD(FTMP, lhs, rhs);
5763 }
5764 __ Bc1nez(FTMP, label);
5765 break;
5766 case kCondGT:
5767 if (gt_bias) {
5768 __ CmpUltD(FTMP, rhs, lhs);
5769 } else {
5770 __ CmpLtD(FTMP, rhs, lhs);
5771 }
5772 __ Bc1nez(FTMP, label);
5773 break;
5774 case kCondGE:
5775 if (gt_bias) {
5776 __ CmpUleD(FTMP, rhs, lhs);
5777 } else {
5778 __ CmpLeD(FTMP, rhs, lhs);
5779 }
5780 __ Bc1nez(FTMP, label);
5781 break;
5782 default:
5783 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005784 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005785 }
5786 } else {
5787 switch (cond) {
5788 case kCondEQ:
5789 __ CeqD(0, lhs, rhs);
5790 __ Bc1t(0, label);
5791 break;
5792 case kCondNE:
5793 __ CeqD(0, lhs, rhs);
5794 __ Bc1f(0, label);
5795 break;
5796 case kCondLT:
5797 if (gt_bias) {
5798 __ ColtD(0, lhs, rhs);
5799 } else {
5800 __ CultD(0, lhs, rhs);
5801 }
5802 __ Bc1t(0, label);
5803 break;
5804 case kCondLE:
5805 if (gt_bias) {
5806 __ ColeD(0, lhs, rhs);
5807 } else {
5808 __ CuleD(0, lhs, rhs);
5809 }
5810 __ Bc1t(0, label);
5811 break;
5812 case kCondGT:
5813 if (gt_bias) {
5814 __ CultD(0, rhs, lhs);
5815 } else {
5816 __ ColtD(0, rhs, lhs);
5817 }
5818 __ Bc1t(0, label);
5819 break;
5820 case kCondGE:
5821 if (gt_bias) {
5822 __ CuleD(0, rhs, lhs);
5823 } else {
5824 __ ColeD(0, rhs, lhs);
5825 }
5826 __ Bc1t(0, label);
5827 break;
5828 default:
5829 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005830 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005831 }
5832 }
5833 }
5834}
5835
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005836void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005837 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005838 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005839 MipsLabel* false_target) {
5840 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005841
David Brazdil0debae72015-11-12 18:37:00 +00005842 if (true_target == nullptr && false_target == nullptr) {
5843 // Nothing to do. The code always falls through.
5844 return;
5845 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005846 // Constant condition, statically compared against "true" (integer value 1).
5847 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005848 if (true_target != nullptr) {
5849 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005850 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005851 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005852 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005853 if (false_target != nullptr) {
5854 __ B(false_target);
5855 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005856 }
David Brazdil0debae72015-11-12 18:37:00 +00005857 return;
5858 }
5859
5860 // The following code generates these patterns:
5861 // (1) true_target == nullptr && false_target != nullptr
5862 // - opposite condition true => branch to false_target
5863 // (2) true_target != nullptr && false_target == nullptr
5864 // - condition true => branch to true_target
5865 // (3) true_target != nullptr && false_target != nullptr
5866 // - condition true => branch to true_target
5867 // - branch to false_target
5868 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005869 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005870 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005871 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005872 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005873 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5874 } else {
5875 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5876 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005877 } else {
5878 // The condition instruction has not been materialized, use its inputs as
5879 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005880 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005881 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005882 LocationSummary* locations = cond->GetLocations();
5883 IfCondition if_cond = condition->GetCondition();
5884 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005885
David Brazdil0debae72015-11-12 18:37:00 +00005886 if (true_target == nullptr) {
5887 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005888 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005889 }
5890
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005891 switch (type) {
5892 default:
5893 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5894 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005896 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5897 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005898 case DataType::Type::kFloat32:
5899 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005900 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5901 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005902 }
5903 }
David Brazdil0debae72015-11-12 18:37:00 +00005904
5905 // If neither branch falls through (case 3), the conditional branch to `true_target`
5906 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5907 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005908 __ B(false_target);
5909 }
5910}
5911
5912void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005913 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005914 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005915 locations->SetInAt(0, Location::RequiresRegister());
5916 }
5917}
5918
5919void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005920 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5921 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5922 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5923 nullptr : codegen_->GetLabelOf(true_successor);
5924 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5925 nullptr : codegen_->GetLabelOf(false_successor);
5926 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005927}
5928
5929void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005930 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005931 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005932 InvokeRuntimeCallingConvention calling_convention;
5933 RegisterSet caller_saves = RegisterSet::Empty();
5934 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5935 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005936 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005937 locations->SetInAt(0, Location::RequiresRegister());
5938 }
5939}
5940
5941void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005942 SlowPathCodeMIPS* slow_path =
5943 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005944 GenerateTestAndBranch(deoptimize,
5945 /* condition_input_index */ 0,
5946 slow_path->GetEntryLabel(),
5947 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005948}
5949
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005950// This function returns true if a conditional move can be generated for HSelect.
5951// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5952// branches and regular moves.
5953//
5954// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5955//
5956// While determining feasibility of a conditional move and setting inputs/outputs
5957// are two distinct tasks, this function does both because they share quite a bit
5958// of common logic.
5959static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5960 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5961 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5962 HCondition* condition = cond->AsCondition();
5963
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005964 DataType::Type cond_type =
5965 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5966 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005967
5968 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5969 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5970 bool is_true_value_zero_constant =
5971 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5972 bool is_false_value_zero_constant =
5973 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5974
5975 bool can_move_conditionally = false;
5976 bool use_const_for_false_in = false;
5977 bool use_const_for_true_in = false;
5978
5979 if (!cond->IsConstant()) {
5980 switch (cond_type) {
5981 default:
5982 switch (dst_type) {
5983 default:
5984 // Moving int on int condition.
5985 if (is_r6) {
5986 if (is_true_value_zero_constant) {
5987 // seleqz out_reg, false_reg, cond_reg
5988 can_move_conditionally = true;
5989 use_const_for_true_in = true;
5990 } else if (is_false_value_zero_constant) {
5991 // selnez out_reg, true_reg, cond_reg
5992 can_move_conditionally = true;
5993 use_const_for_false_in = true;
5994 } else if (materialized) {
5995 // Not materializing unmaterialized int conditions
5996 // to keep the instruction count low.
5997 // selnez AT, true_reg, cond_reg
5998 // seleqz TMP, false_reg, cond_reg
5999 // or out_reg, AT, TMP
6000 can_move_conditionally = true;
6001 }
6002 } else {
6003 // movn out_reg, true_reg/ZERO, cond_reg
6004 can_move_conditionally = true;
6005 use_const_for_true_in = is_true_value_zero_constant;
6006 }
6007 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006008 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006009 // Moving long on int condition.
6010 if (is_r6) {
6011 if (is_true_value_zero_constant) {
6012 // seleqz out_reg_lo, false_reg_lo, cond_reg
6013 // seleqz out_reg_hi, false_reg_hi, cond_reg
6014 can_move_conditionally = true;
6015 use_const_for_true_in = true;
6016 } else if (is_false_value_zero_constant) {
6017 // selnez out_reg_lo, true_reg_lo, cond_reg
6018 // selnez out_reg_hi, true_reg_hi, cond_reg
6019 can_move_conditionally = true;
6020 use_const_for_false_in = true;
6021 }
6022 // Other long conditional moves would generate 6+ instructions,
6023 // which is too many.
6024 } else {
6025 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
6026 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
6027 can_move_conditionally = true;
6028 use_const_for_true_in = is_true_value_zero_constant;
6029 }
6030 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006031 case DataType::Type::kFloat32:
6032 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006033 // Moving float/double on int condition.
6034 if (is_r6) {
6035 if (materialized) {
6036 // Not materializing unmaterialized int conditions
6037 // to keep the instruction count low.
6038 can_move_conditionally = true;
6039 if (is_true_value_zero_constant) {
6040 // sltu TMP, ZERO, cond_reg
6041 // mtc1 TMP, temp_cond_reg
6042 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6043 use_const_for_true_in = true;
6044 } else if (is_false_value_zero_constant) {
6045 // sltu TMP, ZERO, cond_reg
6046 // mtc1 TMP, temp_cond_reg
6047 // selnez.fmt out_reg, true_reg, temp_cond_reg
6048 use_const_for_false_in = true;
6049 } else {
6050 // sltu TMP, ZERO, cond_reg
6051 // mtc1 TMP, temp_cond_reg
6052 // sel.fmt temp_cond_reg, false_reg, true_reg
6053 // mov.fmt out_reg, temp_cond_reg
6054 }
6055 }
6056 } else {
6057 // movn.fmt out_reg, true_reg, cond_reg
6058 can_move_conditionally = true;
6059 }
6060 break;
6061 }
6062 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006063 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006064 // We don't materialize long comparison now
6065 // and use conditional branches instead.
6066 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006067 case DataType::Type::kFloat32:
6068 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006069 switch (dst_type) {
6070 default:
6071 // Moving int on float/double condition.
6072 if (is_r6) {
6073 if (is_true_value_zero_constant) {
6074 // mfc1 TMP, temp_cond_reg
6075 // seleqz out_reg, false_reg, TMP
6076 can_move_conditionally = true;
6077 use_const_for_true_in = true;
6078 } else if (is_false_value_zero_constant) {
6079 // mfc1 TMP, temp_cond_reg
6080 // selnez out_reg, true_reg, TMP
6081 can_move_conditionally = true;
6082 use_const_for_false_in = true;
6083 } else {
6084 // mfc1 TMP, temp_cond_reg
6085 // selnez AT, true_reg, TMP
6086 // seleqz TMP, false_reg, TMP
6087 // or out_reg, AT, TMP
6088 can_move_conditionally = true;
6089 }
6090 } else {
6091 // movt out_reg, true_reg/ZERO, cc
6092 can_move_conditionally = true;
6093 use_const_for_true_in = is_true_value_zero_constant;
6094 }
6095 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006096 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006097 // Moving long on float/double condition.
6098 if (is_r6) {
6099 if (is_true_value_zero_constant) {
6100 // mfc1 TMP, temp_cond_reg
6101 // seleqz out_reg_lo, false_reg_lo, TMP
6102 // seleqz out_reg_hi, false_reg_hi, TMP
6103 can_move_conditionally = true;
6104 use_const_for_true_in = true;
6105 } else if (is_false_value_zero_constant) {
6106 // mfc1 TMP, temp_cond_reg
6107 // selnez out_reg_lo, true_reg_lo, TMP
6108 // selnez out_reg_hi, true_reg_hi, TMP
6109 can_move_conditionally = true;
6110 use_const_for_false_in = true;
6111 }
6112 // Other long conditional moves would generate 6+ instructions,
6113 // which is too many.
6114 } else {
6115 // movt out_reg_lo, true_reg_lo/ZERO, cc
6116 // movt out_reg_hi, true_reg_hi/ZERO, cc
6117 can_move_conditionally = true;
6118 use_const_for_true_in = is_true_value_zero_constant;
6119 }
6120 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006121 case DataType::Type::kFloat32:
6122 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006123 // Moving float/double on float/double condition.
6124 if (is_r6) {
6125 can_move_conditionally = true;
6126 if (is_true_value_zero_constant) {
6127 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6128 use_const_for_true_in = true;
6129 } else if (is_false_value_zero_constant) {
6130 // selnez.fmt out_reg, true_reg, temp_cond_reg
6131 use_const_for_false_in = true;
6132 } else {
6133 // sel.fmt temp_cond_reg, false_reg, true_reg
6134 // mov.fmt out_reg, temp_cond_reg
6135 }
6136 } else {
6137 // movt.fmt out_reg, true_reg, cc
6138 can_move_conditionally = true;
6139 }
6140 break;
6141 }
6142 break;
6143 }
6144 }
6145
6146 if (can_move_conditionally) {
6147 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6148 } else {
6149 DCHECK(!use_const_for_false_in);
6150 DCHECK(!use_const_for_true_in);
6151 }
6152
6153 if (locations_to_set != nullptr) {
6154 if (use_const_for_false_in) {
6155 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6156 } else {
6157 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006158 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006159 ? Location::RequiresFpuRegister()
6160 : Location::RequiresRegister());
6161 }
6162 if (use_const_for_true_in) {
6163 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6164 } else {
6165 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006166 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006167 ? Location::RequiresFpuRegister()
6168 : Location::RequiresRegister());
6169 }
6170 if (materialized) {
6171 locations_to_set->SetInAt(2, Location::RequiresRegister());
6172 }
6173 // On R6 we don't require the output to be the same as the
6174 // first input for conditional moves unlike on R2.
6175 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6176 if (is_out_same_as_first_in) {
6177 locations_to_set->SetOut(Location::SameAsFirstInput());
6178 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006179 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006180 ? Location::RequiresFpuRegister()
6181 : Location::RequiresRegister());
6182 }
6183 }
6184
6185 return can_move_conditionally;
6186}
6187
6188void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6189 LocationSummary* locations = select->GetLocations();
6190 Location dst = locations->Out();
6191 Location src = locations->InAt(1);
6192 Register src_reg = ZERO;
6193 Register src_reg_high = ZERO;
6194 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6195 Register cond_reg = TMP;
6196 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006197 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006198 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006199 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006200
6201 if (IsBooleanValueOrMaterializedCondition(cond)) {
6202 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6203 } else {
6204 HCondition* condition = cond->AsCondition();
6205 LocationSummary* cond_locations = cond->GetLocations();
6206 IfCondition if_cond = condition->GetCondition();
6207 cond_type = condition->InputAt(0)->GetType();
6208 switch (cond_type) {
6209 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006210 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006211 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6212 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006213 case DataType::Type::kFloat32:
6214 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006215 cond_inverted = MaterializeFpCompareR2(if_cond,
6216 condition->IsGtBias(),
6217 cond_type,
6218 cond_locations,
6219 cond_cc);
6220 break;
6221 }
6222 }
6223
6224 DCHECK(dst.Equals(locations->InAt(0)));
6225 if (src.IsRegister()) {
6226 src_reg = src.AsRegister<Register>();
6227 } else if (src.IsRegisterPair()) {
6228 src_reg = src.AsRegisterPairLow<Register>();
6229 src_reg_high = src.AsRegisterPairHigh<Register>();
6230 } else if (src.IsConstant()) {
6231 DCHECK(src.GetConstant()->IsZeroBitPattern());
6232 }
6233
6234 switch (cond_type) {
6235 default:
6236 switch (dst_type) {
6237 default:
6238 if (cond_inverted) {
6239 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6240 } else {
6241 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6242 }
6243 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006244 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006245 if (cond_inverted) {
6246 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6247 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6248 } else {
6249 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6250 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6251 }
6252 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006253 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006254 if (cond_inverted) {
6255 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6256 } else {
6257 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6258 }
6259 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006260 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006261 if (cond_inverted) {
6262 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6263 } else {
6264 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6265 }
6266 break;
6267 }
6268 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006269 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006270 LOG(FATAL) << "Unreachable";
6271 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006272 case DataType::Type::kFloat32:
6273 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006274 switch (dst_type) {
6275 default:
6276 if (cond_inverted) {
6277 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6278 } else {
6279 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6280 }
6281 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006282 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006283 if (cond_inverted) {
6284 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6285 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6286 } else {
6287 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6288 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6289 }
6290 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006291 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006292 if (cond_inverted) {
6293 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6294 } else {
6295 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6296 }
6297 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006298 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006299 if (cond_inverted) {
6300 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6301 } else {
6302 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6303 }
6304 break;
6305 }
6306 break;
6307 }
6308}
6309
6310void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6311 LocationSummary* locations = select->GetLocations();
6312 Location dst = locations->Out();
6313 Location false_src = locations->InAt(0);
6314 Location true_src = locations->InAt(1);
6315 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6316 Register cond_reg = TMP;
6317 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006318 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006319 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006320 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006321
6322 if (IsBooleanValueOrMaterializedCondition(cond)) {
6323 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6324 } else {
6325 HCondition* condition = cond->AsCondition();
6326 LocationSummary* cond_locations = cond->GetLocations();
6327 IfCondition if_cond = condition->GetCondition();
6328 cond_type = condition->InputAt(0)->GetType();
6329 switch (cond_type) {
6330 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006331 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006332 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6333 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006334 case DataType::Type::kFloat32:
6335 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006336 cond_inverted = MaterializeFpCompareR6(if_cond,
6337 condition->IsGtBias(),
6338 cond_type,
6339 cond_locations,
6340 fcond_reg);
6341 break;
6342 }
6343 }
6344
6345 if (true_src.IsConstant()) {
6346 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6347 }
6348 if (false_src.IsConstant()) {
6349 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6350 }
6351
6352 switch (dst_type) {
6353 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006354 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006355 __ Mfc1(cond_reg, fcond_reg);
6356 }
6357 if (true_src.IsConstant()) {
6358 if (cond_inverted) {
6359 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6360 } else {
6361 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6362 }
6363 } else if (false_src.IsConstant()) {
6364 if (cond_inverted) {
6365 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6366 } else {
6367 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6368 }
6369 } else {
6370 DCHECK_NE(cond_reg, AT);
6371 if (cond_inverted) {
6372 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6373 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6374 } else {
6375 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6376 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6377 }
6378 __ Or(dst.AsRegister<Register>(), AT, TMP);
6379 }
6380 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006381 case DataType::Type::kInt64: {
6382 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006383 __ Mfc1(cond_reg, fcond_reg);
6384 }
6385 Register dst_lo = dst.AsRegisterPairLow<Register>();
6386 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6387 if (true_src.IsConstant()) {
6388 Register src_lo = false_src.AsRegisterPairLow<Register>();
6389 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6390 if (cond_inverted) {
6391 __ Selnez(dst_lo, src_lo, cond_reg);
6392 __ Selnez(dst_hi, src_hi, cond_reg);
6393 } else {
6394 __ Seleqz(dst_lo, src_lo, cond_reg);
6395 __ Seleqz(dst_hi, src_hi, cond_reg);
6396 }
6397 } else {
6398 DCHECK(false_src.IsConstant());
6399 Register src_lo = true_src.AsRegisterPairLow<Register>();
6400 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6401 if (cond_inverted) {
6402 __ Seleqz(dst_lo, src_lo, cond_reg);
6403 __ Seleqz(dst_hi, src_hi, cond_reg);
6404 } else {
6405 __ Selnez(dst_lo, src_lo, cond_reg);
6406 __ Selnez(dst_hi, src_hi, cond_reg);
6407 }
6408 }
6409 break;
6410 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006411 case DataType::Type::kFloat32: {
6412 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006413 // sel*.fmt tests bit 0 of the condition register, account for that.
6414 __ Sltu(TMP, ZERO, cond_reg);
6415 __ Mtc1(TMP, fcond_reg);
6416 }
6417 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6418 if (true_src.IsConstant()) {
6419 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6420 if (cond_inverted) {
6421 __ SelnezS(dst_reg, src_reg, fcond_reg);
6422 } else {
6423 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6424 }
6425 } else if (false_src.IsConstant()) {
6426 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6427 if (cond_inverted) {
6428 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6429 } else {
6430 __ SelnezS(dst_reg, src_reg, fcond_reg);
6431 }
6432 } else {
6433 if (cond_inverted) {
6434 __ SelS(fcond_reg,
6435 true_src.AsFpuRegister<FRegister>(),
6436 false_src.AsFpuRegister<FRegister>());
6437 } else {
6438 __ SelS(fcond_reg,
6439 false_src.AsFpuRegister<FRegister>(),
6440 true_src.AsFpuRegister<FRegister>());
6441 }
6442 __ MovS(dst_reg, fcond_reg);
6443 }
6444 break;
6445 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006446 case DataType::Type::kFloat64: {
6447 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006448 // sel*.fmt tests bit 0 of the condition register, account for that.
6449 __ Sltu(TMP, ZERO, cond_reg);
6450 __ Mtc1(TMP, fcond_reg);
6451 }
6452 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6453 if (true_src.IsConstant()) {
6454 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6455 if (cond_inverted) {
6456 __ SelnezD(dst_reg, src_reg, fcond_reg);
6457 } else {
6458 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6459 }
6460 } else if (false_src.IsConstant()) {
6461 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6462 if (cond_inverted) {
6463 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6464 } else {
6465 __ SelnezD(dst_reg, src_reg, fcond_reg);
6466 }
6467 } else {
6468 if (cond_inverted) {
6469 __ SelD(fcond_reg,
6470 true_src.AsFpuRegister<FRegister>(),
6471 false_src.AsFpuRegister<FRegister>());
6472 } else {
6473 __ SelD(fcond_reg,
6474 false_src.AsFpuRegister<FRegister>(),
6475 true_src.AsFpuRegister<FRegister>());
6476 }
6477 __ MovD(dst_reg, fcond_reg);
6478 }
6479 break;
6480 }
6481 }
6482}
6483
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006484void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006485 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006486 LocationSummary(flag, LocationSummary::kNoCall);
6487 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006488}
6489
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006490void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6491 __ LoadFromOffset(kLoadWord,
6492 flag->GetLocations()->Out().AsRegister<Register>(),
6493 SP,
6494 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006495}
6496
David Brazdil74eb1b22015-12-14 11:44:01 +00006497void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006498 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006499 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006500}
6501
6502void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006503 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6504 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6505 if (is_r6) {
6506 GenConditionalMoveR6(select);
6507 } else {
6508 GenConditionalMoveR2(select);
6509 }
6510 } else {
6511 LocationSummary* locations = select->GetLocations();
6512 MipsLabel false_target;
6513 GenerateTestAndBranch(select,
6514 /* condition_input_index */ 2,
6515 /* true_target */ nullptr,
6516 &false_target);
6517 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6518 __ Bind(&false_target);
6519 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006520}
6521
David Srbecky0cf44932015-12-09 14:09:59 +00006522void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006523 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006524}
6525
David Srbeckyd28f4a02016-03-14 17:14:24 +00006526void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6527 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006528}
6529
6530void CodeGeneratorMIPS::GenerateNop() {
6531 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006532}
6533
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006534void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006535 DataType::Type field_type = field_info.GetFieldType();
6536 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006537 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006538 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006539 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006540 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006541 instruction,
6542 generate_volatile
6543 ? LocationSummary::kCallOnMainOnly
6544 : (object_field_get_with_read_barrier
6545 ? LocationSummary::kCallOnSlowPath
6546 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006547
Alexey Frunzec61c0762017-04-10 13:54:23 -07006548 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6549 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6550 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006551 locations->SetInAt(0, Location::RequiresRegister());
6552 if (generate_volatile) {
6553 InvokeRuntimeCallingConvention calling_convention;
6554 // need A0 to hold base + offset
6555 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006556 if (field_type == DataType::Type::kInt64) {
6557 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006558 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006559 // Use Location::Any() to prevent situations when running out of available fp registers.
6560 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006561 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006562 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006563 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6564 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6565 }
6566 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006567 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006568 locations->SetOut(Location::RequiresFpuRegister());
6569 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006570 // The output overlaps in the case of an object field get with
6571 // read barriers enabled: we do not want the move to overwrite the
6572 // object's location, as we need it to emit the read barrier.
6573 locations->SetOut(Location::RequiresRegister(),
6574 object_field_get_with_read_barrier
6575 ? Location::kOutputOverlap
6576 : Location::kNoOutputOverlap);
6577 }
6578 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6579 // We need a temporary register for the read barrier marking slow
6580 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006581 if (!kBakerReadBarrierThunksEnableForFields) {
6582 locations->AddTemp(Location::RequiresRegister());
6583 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006584 }
6585 }
6586}
6587
6588void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6589 const FieldInfo& field_info,
6590 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006591 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6592 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006593 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006594 Location obj_loc = locations->InAt(0);
6595 Register obj = obj_loc.AsRegister<Register>();
6596 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006597 LoadOperandType load_type = kLoadUnsignedByte;
6598 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006599 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006600 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006601
6602 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006603 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006604 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006605 load_type = kLoadUnsignedByte;
6606 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006607 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006608 load_type = kLoadSignedByte;
6609 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006610 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006611 load_type = kLoadUnsignedHalfword;
6612 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006613 case DataType::Type::kInt16:
6614 load_type = kLoadSignedHalfword;
6615 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006616 case DataType::Type::kInt32:
6617 case DataType::Type::kFloat32:
6618 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006619 load_type = kLoadWord;
6620 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006621 case DataType::Type::kInt64:
6622 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006623 load_type = kLoadDoubleword;
6624 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006625 case DataType::Type::kUint32:
6626 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006627 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006628 LOG(FATAL) << "Unreachable type " << type;
6629 UNREACHABLE();
6630 }
6631
6632 if (is_volatile && load_type == kLoadDoubleword) {
6633 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006634 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006635 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006636 __ LoadFromOffset(kLoadWord,
6637 ZERO,
6638 locations->GetTemp(0).AsRegister<Register>(),
6639 0,
6640 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006641 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006642 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006643 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006644 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006645 if (dst_loc.IsFpuRegister()) {
6646 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006647 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006648 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006649 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006650 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006651 __ StoreToOffset(kStoreWord,
6652 locations->GetTemp(1).AsRegister<Register>(),
6653 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006654 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006655 __ StoreToOffset(kStoreWord,
6656 locations->GetTemp(2).AsRegister<Register>(),
6657 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006658 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006659 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006660 }
6661 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006662 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006663 // /* HeapReference<Object> */ dst = *(obj + offset)
6664 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006665 Location temp_loc =
6666 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006667 // Note that a potential implicit null check is handled in this
6668 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6669 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6670 dst_loc,
6671 obj,
6672 offset,
6673 temp_loc,
6674 /* needs_null_check */ true);
6675 if (is_volatile) {
6676 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6677 }
6678 } else {
6679 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6680 if (is_volatile) {
6681 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6682 }
6683 // If read barriers are enabled, emit read barriers other than
6684 // Baker's using a slow path (and also unpoison the loaded
6685 // reference, if heap poisoning is enabled).
6686 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6687 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006688 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006689 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006690 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006691 DCHECK(dst_loc.IsRegisterPair());
6692 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006693 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006694 DCHECK(dst_loc.IsRegister());
6695 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006696 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006697 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006698 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006699 DCHECK(dst_loc.IsFpuRegister());
6700 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006701 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006702 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006703 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006704 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006705 }
6706 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006707 }
6708
Alexey Frunze15958152017-02-09 19:08:30 -08006709 // Memory barriers, in the case of references, are handled in the
6710 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006711 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006712 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6713 }
6714}
6715
6716void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006717 DataType::Type field_type = field_info.GetFieldType();
6718 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006719 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006720 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006721 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006722
6723 locations->SetInAt(0, Location::RequiresRegister());
6724 if (generate_volatile) {
6725 InvokeRuntimeCallingConvention calling_convention;
6726 // need A0 to hold base + offset
6727 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006728 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006729 locations->SetInAt(1, Location::RegisterPairLocation(
6730 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6731 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006732 // Use Location::Any() to prevent situations when running out of available fp registers.
6733 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006734 // Pass FP parameters in core registers.
6735 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6736 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6737 }
6738 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006739 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006740 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006741 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006742 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006743 }
6744 }
6745}
6746
6747void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6748 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006749 uint32_t dex_pc,
6750 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006751 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006752 LocationSummary* locations = instruction->GetLocations();
6753 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006754 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006755 StoreOperandType store_type = kStoreByte;
6756 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006757 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006758 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006759 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006760
6761 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006762 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006763 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006764 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006765 store_type = kStoreByte;
6766 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006767 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006768 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006769 store_type = kStoreHalfword;
6770 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006771 case DataType::Type::kInt32:
6772 case DataType::Type::kFloat32:
6773 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006774 store_type = kStoreWord;
6775 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006776 case DataType::Type::kInt64:
6777 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006778 store_type = kStoreDoubleword;
6779 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006780 case DataType::Type::kUint32:
6781 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006782 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006783 LOG(FATAL) << "Unreachable type " << type;
6784 UNREACHABLE();
6785 }
6786
6787 if (is_volatile) {
6788 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6789 }
6790
6791 if (is_volatile && store_type == kStoreDoubleword) {
6792 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006793 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006794 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006795 __ LoadFromOffset(kLoadWord,
6796 ZERO,
6797 locations->GetTemp(0).AsRegister<Register>(),
6798 0,
6799 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006800 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006801 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006802 if (value_location.IsFpuRegister()) {
6803 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6804 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006805 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006806 value_location.AsFpuRegister<FRegister>());
6807 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006808 __ LoadFromOffset(kLoadWord,
6809 locations->GetTemp(1).AsRegister<Register>(),
6810 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006811 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006812 __ LoadFromOffset(kLoadWord,
6813 locations->GetTemp(2).AsRegister<Register>(),
6814 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006815 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006816 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006817 DCHECK(value_location.IsConstant());
6818 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6819 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006820 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6821 locations->GetTemp(1).AsRegister<Register>(),
6822 value);
6823 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006824 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006825 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006826 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6827 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006828 if (value_location.IsConstant()) {
6829 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6830 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006831 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006832 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006833 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006834 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006835 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006836 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006837 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006838 if (kPoisonHeapReferences && needs_write_barrier) {
6839 // Note that in the case where `value` is a null reference,
6840 // we do not enter this block, as a null reference does not
6841 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006842 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006843 __ PoisonHeapReference(TMP, src);
6844 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6845 } else {
6846 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6847 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006848 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006849 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006850 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006851 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006852 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006853 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006854 }
6855 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006856 }
6857
Alexey Frunzec061de12017-02-14 13:27:23 -08006858 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006859 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006860 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006861 }
6862
6863 if (is_volatile) {
6864 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6865 }
6866}
6867
6868void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6869 HandleFieldGet(instruction, instruction->GetFieldInfo());
6870}
6871
6872void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6873 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6874}
6875
6876void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6877 HandleFieldSet(instruction, instruction->GetFieldInfo());
6878}
6879
6880void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006881 HandleFieldSet(instruction,
6882 instruction->GetFieldInfo(),
6883 instruction->GetDexPc(),
6884 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006885}
6886
Alexey Frunze15958152017-02-09 19:08:30 -08006887void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6888 HInstruction* instruction,
6889 Location out,
6890 uint32_t offset,
6891 Location maybe_temp,
6892 ReadBarrierOption read_barrier_option) {
6893 Register out_reg = out.AsRegister<Register>();
6894 if (read_barrier_option == kWithReadBarrier) {
6895 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006896 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6897 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6898 }
Alexey Frunze15958152017-02-09 19:08:30 -08006899 if (kUseBakerReadBarrier) {
6900 // Load with fast path based Baker's read barrier.
6901 // /* HeapReference<Object> */ out = *(out + offset)
6902 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6903 out,
6904 out_reg,
6905 offset,
6906 maybe_temp,
6907 /* needs_null_check */ false);
6908 } else {
6909 // Load with slow path based read barrier.
6910 // Save the value of `out` into `maybe_temp` before overwriting it
6911 // in the following move operation, as we will need it for the
6912 // read barrier below.
6913 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6914 // /* HeapReference<Object> */ out = *(out + offset)
6915 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6916 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6917 }
6918 } else {
6919 // Plain load with no read barrier.
6920 // /* HeapReference<Object> */ out = *(out + offset)
6921 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6922 __ MaybeUnpoisonHeapReference(out_reg);
6923 }
6924}
6925
6926void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6927 HInstruction* instruction,
6928 Location out,
6929 Location obj,
6930 uint32_t offset,
6931 Location maybe_temp,
6932 ReadBarrierOption read_barrier_option) {
6933 Register out_reg = out.AsRegister<Register>();
6934 Register obj_reg = obj.AsRegister<Register>();
6935 if (read_barrier_option == kWithReadBarrier) {
6936 CHECK(kEmitCompilerReadBarrier);
6937 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006938 if (!kBakerReadBarrierThunksEnableForFields) {
6939 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6940 }
Alexey Frunze15958152017-02-09 19:08:30 -08006941 // Load with fast path based Baker's read barrier.
6942 // /* HeapReference<Object> */ out = *(obj + offset)
6943 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6944 out,
6945 obj_reg,
6946 offset,
6947 maybe_temp,
6948 /* needs_null_check */ false);
6949 } else {
6950 // Load with slow path based read barrier.
6951 // /* HeapReference<Object> */ out = *(obj + offset)
6952 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6953 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6954 }
6955 } else {
6956 // Plain load with no read barrier.
6957 // /* HeapReference<Object> */ out = *(obj + offset)
6958 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6959 __ MaybeUnpoisonHeapReference(out_reg);
6960 }
6961}
6962
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006963static inline int GetBakerMarkThunkNumber(Register reg) {
6964 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6965 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6966 return reg - V0;
6967 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6968 return 14 + (reg - S2);
6969 } else if (reg == FP) { // One more.
6970 return 20;
6971 }
6972 LOG(FATAL) << "Unexpected register " << reg;
6973 UNREACHABLE();
6974}
6975
6976static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6977 int num = GetBakerMarkThunkNumber(reg) +
6978 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6979 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6980}
6981
6982static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6983 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6984 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6985}
6986
Alexey Frunze15958152017-02-09 19:08:30 -08006987void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6988 Location root,
6989 Register obj,
6990 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006991 ReadBarrierOption read_barrier_option,
6992 MipsLabel* label_low) {
6993 bool reordering;
6994 if (label_low != nullptr) {
6995 DCHECK_EQ(offset, 0x5678u);
6996 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006997 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006998 if (read_barrier_option == kWithReadBarrier) {
6999 DCHECK(kEmitCompilerReadBarrier);
7000 if (kUseBakerReadBarrier) {
7001 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7002 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007003 if (kBakerReadBarrierThunksEnableForGcRoots) {
7004 // Note that we do not actually check the value of `GetIsGcMarking()`
7005 // to decide whether to mark the loaded GC root or not. Instead, we
7006 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7007 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7008 // vice versa.
7009 //
7010 // We use thunks for the slow path. That thunk checks the reference
7011 // and jumps to the entrypoint if needed.
7012 //
7013 // temp = Thread::Current()->pReadBarrierMarkReg00
7014 // // AKA &art_quick_read_barrier_mark_introspection.
7015 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7016 // if (temp != nullptr) {
7017 // temp = &gc_root_thunk<root_reg>
7018 // root = temp(root)
7019 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007020
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007021 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7022 const int32_t entry_point_offset =
7023 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7024 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
7025 int16_t offset_low = Low16Bits(offset);
7026 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
7027 // extension in lw.
7028 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7029 Register base = short_offset ? obj : TMP;
7030 // Loading the entrypoint does not require a load acquire since it is only changed when
7031 // threads are suspended or running a checkpoint.
7032 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7033 reordering = __ SetReorder(false);
7034 if (!short_offset) {
7035 DCHECK(!label_low);
7036 __ AddUpper(base, obj, offset_high);
7037 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007038 MipsLabel skip_call;
7039 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007040 if (label_low != nullptr) {
7041 DCHECK(short_offset);
7042 __ Bind(label_low);
7043 }
7044 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7045 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
7046 // in delay slot.
7047 if (isR6) {
7048 __ Jialc(T9, thunk_disp);
7049 } else {
7050 __ Addiu(T9, T9, thunk_disp);
7051 __ Jalr(T9);
7052 __ Nop();
7053 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007054 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007055 __ SetReorder(reordering);
7056 } else {
7057 // Note that we do not actually check the value of `GetIsGcMarking()`
7058 // to decide whether to mark the loaded GC root or not. Instead, we
7059 // load into `temp` (T9) the read barrier mark entry point corresponding
7060 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
7061 // is false, and vice versa.
7062 //
7063 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7064 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7065 // if (temp != null) {
7066 // root = temp(root)
7067 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007068
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007069 if (label_low != nullptr) {
7070 reordering = __ SetReorder(false);
7071 __ Bind(label_low);
7072 }
7073 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7074 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7075 if (label_low != nullptr) {
7076 __ SetReorder(reordering);
7077 }
7078 static_assert(
7079 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7080 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7081 "have different sizes.");
7082 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7083 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7084 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08007085
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007086 // Slow path marking the GC root `root`.
7087 Location temp = Location::RegisterLocation(T9);
7088 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007089 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007090 instruction,
7091 root,
7092 /*entrypoint*/ temp);
7093 codegen_->AddSlowPath(slow_path);
7094
7095 const int32_t entry_point_offset =
7096 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
7097 // Loading the entrypoint does not require a load acquire since it is only changed when
7098 // threads are suspended or running a checkpoint.
7099 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7100 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
7101 __ Bind(slow_path->GetExitLabel());
7102 }
Alexey Frunze15958152017-02-09 19:08:30 -08007103 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007104 if (label_low != nullptr) {
7105 reordering = __ SetReorder(false);
7106 __ Bind(label_low);
7107 }
Alexey Frunze15958152017-02-09 19:08:30 -08007108 // GC root loaded through a slow path for read barriers other
7109 // than Baker's.
7110 // /* GcRoot<mirror::Object>* */ root = obj + offset
7111 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007112 if (label_low != nullptr) {
7113 __ SetReorder(reordering);
7114 }
Alexey Frunze15958152017-02-09 19:08:30 -08007115 // /* mirror::Object* */ root = root->Read()
7116 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7117 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007118 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007119 if (label_low != nullptr) {
7120 reordering = __ SetReorder(false);
7121 __ Bind(label_low);
7122 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007123 // Plain GC root load with no read barrier.
7124 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7125 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7126 // Note that GC roots are not affected by heap poisoning, thus we
7127 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007128 if (label_low != nullptr) {
7129 __ SetReorder(reordering);
7130 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007131 }
7132}
7133
Alexey Frunze15958152017-02-09 19:08:30 -08007134void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7135 Location ref,
7136 Register obj,
7137 uint32_t offset,
7138 Location temp,
7139 bool needs_null_check) {
7140 DCHECK(kEmitCompilerReadBarrier);
7141 DCHECK(kUseBakerReadBarrier);
7142
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007143 if (kBakerReadBarrierThunksEnableForFields) {
7144 // Note that we do not actually check the value of `GetIsGcMarking()`
7145 // to decide whether to mark the loaded reference or not. Instead, we
7146 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7147 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7148 // vice versa.
7149 //
7150 // We use thunks for the slow path. That thunk checks the reference
7151 // and jumps to the entrypoint if needed. If the holder is not gray,
7152 // it issues a load-load memory barrier and returns to the original
7153 // reference load.
7154 //
7155 // temp = Thread::Current()->pReadBarrierMarkReg00
7156 // // AKA &art_quick_read_barrier_mark_introspection.
7157 // if (temp != nullptr) {
7158 // temp = &field_array_thunk<holder_reg>
7159 // temp()
7160 // }
7161 // not_gray_return_address:
7162 // // If the offset is too large to fit into the lw instruction, we
7163 // // use an adjusted base register (TMP) here. This register
7164 // // receives bits 16 ... 31 of the offset before the thunk invocation
7165 // // and the thunk benefits from it.
7166 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7167 // gray_return_address:
7168
7169 DCHECK(temp.IsInvalid());
7170 bool isR6 = GetInstructionSetFeatures().IsR6();
7171 int16_t offset_low = Low16Bits(offset);
7172 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7173 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7174 bool reordering = __ SetReorder(false);
7175 const int32_t entry_point_offset =
7176 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7177 // There may have or may have not been a null check if the field offset is smaller than
7178 // the page size.
7179 // There must've been a null check in case it's actually a load from an array.
7180 // We will, however, perform an explicit null check in the thunk as it's easier to
7181 // do it than not.
7182 if (instruction->IsArrayGet()) {
7183 DCHECK(!needs_null_check);
7184 }
7185 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7186 // Loading the entrypoint does not require a load acquire since it is only changed when
7187 // threads are suspended or running a checkpoint.
7188 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7189 Register ref_reg = ref.AsRegister<Register>();
7190 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007191 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007192 if (short_offset) {
7193 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007194 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007195 __ Nop(); // In forbidden slot.
7196 __ Jialc(T9, thunk_disp);
7197 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007198 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007199 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7200 __ Jalr(T9);
7201 __ Nop(); // In delay slot.
7202 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007203 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007204 } else {
7205 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007206 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007207 __ Aui(base, obj, offset_high); // In delay slot.
7208 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007209 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007210 } else {
7211 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007212 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007213 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7214 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007215 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007216 __ Addu(base, base, obj); // In delay slot.
7217 }
7218 }
7219 // /* HeapReference<Object> */ ref = *(obj + offset)
7220 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7221 if (needs_null_check) {
7222 MaybeRecordImplicitNullCheck(instruction);
7223 }
7224 __ MaybeUnpoisonHeapReference(ref_reg);
7225 __ SetReorder(reordering);
7226 return;
7227 }
7228
Alexey Frunze15958152017-02-09 19:08:30 -08007229 // /* HeapReference<Object> */ ref = *(obj + offset)
7230 Location no_index = Location::NoLocation();
7231 ScaleFactor no_scale_factor = TIMES_1;
7232 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7233 ref,
7234 obj,
7235 offset,
7236 no_index,
7237 no_scale_factor,
7238 temp,
7239 needs_null_check);
7240}
7241
7242void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7243 Location ref,
7244 Register obj,
7245 uint32_t data_offset,
7246 Location index,
7247 Location temp,
7248 bool needs_null_check) {
7249 DCHECK(kEmitCompilerReadBarrier);
7250 DCHECK(kUseBakerReadBarrier);
7251
7252 static_assert(
7253 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7254 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007255 ScaleFactor scale_factor = TIMES_4;
7256
7257 if (kBakerReadBarrierThunksEnableForArrays) {
7258 // Note that we do not actually check the value of `GetIsGcMarking()`
7259 // to decide whether to mark the loaded reference or not. Instead, we
7260 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7261 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7262 // vice versa.
7263 //
7264 // We use thunks for the slow path. That thunk checks the reference
7265 // and jumps to the entrypoint if needed. If the holder is not gray,
7266 // it issues a load-load memory barrier and returns to the original
7267 // reference load.
7268 //
7269 // temp = Thread::Current()->pReadBarrierMarkReg00
7270 // // AKA &art_quick_read_barrier_mark_introspection.
7271 // if (temp != nullptr) {
7272 // temp = &field_array_thunk<holder_reg>
7273 // temp()
7274 // }
7275 // not_gray_return_address:
7276 // // The element address is pre-calculated in the TMP register before the
7277 // // thunk invocation and the thunk benefits from it.
7278 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7279 // gray_return_address:
7280
7281 DCHECK(temp.IsInvalid());
7282 DCHECK(index.IsValid());
7283 bool reordering = __ SetReorder(false);
7284 const int32_t entry_point_offset =
7285 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7286 // We will not do the explicit null check in the thunk as some form of a null check
7287 // must've been done earlier.
7288 DCHECK(!needs_null_check);
7289 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7290 // Loading the entrypoint does not require a load acquire since it is only changed when
7291 // threads are suspended or running a checkpoint.
7292 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7293 Register ref_reg = ref.AsRegister<Register>();
7294 Register index_reg = index.IsRegisterPair()
7295 ? index.AsRegisterPairLow<Register>()
7296 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007297 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007298 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007299 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007300 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7301 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007302 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007303 } else {
7304 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007305 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007306 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7307 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007308 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007309 __ Addu(TMP, TMP, obj); // In delay slot.
7310 }
7311 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7312 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7313 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7314 __ MaybeUnpoisonHeapReference(ref_reg);
7315 __ SetReorder(reordering);
7316 return;
7317 }
7318
Alexey Frunze15958152017-02-09 19:08:30 -08007319 // /* HeapReference<Object> */ ref =
7320 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007321 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7322 ref,
7323 obj,
7324 data_offset,
7325 index,
7326 scale_factor,
7327 temp,
7328 needs_null_check);
7329}
7330
7331void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7332 Location ref,
7333 Register obj,
7334 uint32_t offset,
7335 Location index,
7336 ScaleFactor scale_factor,
7337 Location temp,
7338 bool needs_null_check,
7339 bool always_update_field) {
7340 DCHECK(kEmitCompilerReadBarrier);
7341 DCHECK(kUseBakerReadBarrier);
7342
7343 // In slow path based read barriers, the read barrier call is
7344 // inserted after the original load. However, in fast path based
7345 // Baker's read barriers, we need to perform the load of
7346 // mirror::Object::monitor_ *before* the original reference load.
7347 // This load-load ordering is required by the read barrier.
7348 // The fast path/slow path (for Baker's algorithm) should look like:
7349 //
7350 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7351 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7352 // HeapReference<Object> ref = *src; // Original reference load.
7353 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7354 // if (is_gray) {
7355 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7356 // }
7357 //
7358 // Note: the original implementation in ReadBarrier::Barrier is
7359 // slightly more complex as it performs additional checks that we do
7360 // not do here for performance reasons.
7361
7362 Register ref_reg = ref.AsRegister<Register>();
7363 Register temp_reg = temp.AsRegister<Register>();
7364 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7365
7366 // /* int32_t */ monitor = obj->monitor_
7367 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7368 if (needs_null_check) {
7369 MaybeRecordImplicitNullCheck(instruction);
7370 }
7371 // /* LockWord */ lock_word = LockWord(monitor)
7372 static_assert(sizeof(LockWord) == sizeof(int32_t),
7373 "art::LockWord and int32_t have different sizes.");
7374
7375 __ Sync(0); // Barrier to prevent load-load reordering.
7376
7377 // The actual reference load.
7378 if (index.IsValid()) {
7379 // Load types involving an "index": ArrayGet,
7380 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7381 // intrinsics.
7382 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7383 if (index.IsConstant()) {
7384 size_t computed_offset =
7385 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7386 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7387 } else {
7388 // Handle the special case of the
7389 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7390 // intrinsics, which use a register pair as index ("long
7391 // offset"), of which only the low part contains data.
7392 Register index_reg = index.IsRegisterPair()
7393 ? index.AsRegisterPairLow<Register>()
7394 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007395 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007396 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7397 }
7398 } else {
7399 // /* HeapReference<Object> */ ref = *(obj + offset)
7400 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7401 }
7402
7403 // Object* ref = ref_addr->AsMirrorPtr()
7404 __ MaybeUnpoisonHeapReference(ref_reg);
7405
7406 // Slow path marking the object `ref` when it is gray.
7407 SlowPathCodeMIPS* slow_path;
7408 if (always_update_field) {
7409 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7410 // of the form `obj + field_offset`, where `obj` is a register and
7411 // `field_offset` is a register pair (of which only the lower half
7412 // is used). Thus `offset` and `scale_factor` above are expected
7413 // to be null in this code path.
7414 DCHECK_EQ(offset, 0u);
7415 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007416 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007417 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7418 ref,
7419 obj,
7420 /* field_offset */ index,
7421 temp_reg);
7422 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007423 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007424 }
7425 AddSlowPath(slow_path);
7426
7427 // if (rb_state == ReadBarrier::GrayState())
7428 // ref = ReadBarrier::Mark(ref);
7429 // Given the numeric representation, it's enough to check the low bit of the
7430 // rb_state. We do that by shifting the bit into the sign bit (31) and
7431 // performing a branch on less than zero.
7432 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7433 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7434 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7435 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7436 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7437 __ Bind(slow_path->GetExitLabel());
7438}
7439
7440void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7441 Location out,
7442 Location ref,
7443 Location obj,
7444 uint32_t offset,
7445 Location index) {
7446 DCHECK(kEmitCompilerReadBarrier);
7447
7448 // Insert a slow path based read barrier *after* the reference load.
7449 //
7450 // If heap poisoning is enabled, the unpoisoning of the loaded
7451 // reference will be carried out by the runtime within the slow
7452 // path.
7453 //
7454 // Note that `ref` currently does not get unpoisoned (when heap
7455 // poisoning is enabled), which is alright as the `ref` argument is
7456 // not used by the artReadBarrierSlow entry point.
7457 //
7458 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007459 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007460 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7461 AddSlowPath(slow_path);
7462
7463 __ B(slow_path->GetEntryLabel());
7464 __ Bind(slow_path->GetExitLabel());
7465}
7466
7467void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7468 Location out,
7469 Location ref,
7470 Location obj,
7471 uint32_t offset,
7472 Location index) {
7473 if (kEmitCompilerReadBarrier) {
7474 // Baker's read barriers shall be handled by the fast path
7475 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7476 DCHECK(!kUseBakerReadBarrier);
7477 // If heap poisoning is enabled, unpoisoning will be taken care of
7478 // by the runtime within the slow path.
7479 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7480 } else if (kPoisonHeapReferences) {
7481 __ UnpoisonHeapReference(out.AsRegister<Register>());
7482 }
7483}
7484
7485void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7486 Location out,
7487 Location root) {
7488 DCHECK(kEmitCompilerReadBarrier);
7489
7490 // Insert a slow path based read barrier *after* the GC root load.
7491 //
7492 // Note that GC roots are not affected by heap poisoning, so we do
7493 // not need to do anything special for this here.
7494 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007495 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007496 AddSlowPath(slow_path);
7497
7498 __ B(slow_path->GetEntryLabel());
7499 __ Bind(slow_path->GetExitLabel());
7500}
7501
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007502void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007503 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7504 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007505 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007506 switch (type_check_kind) {
7507 case TypeCheckKind::kExactCheck:
7508 case TypeCheckKind::kAbstractClassCheck:
7509 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007510 case TypeCheckKind::kArrayObjectCheck: {
7511 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7512 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7513 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007514 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007515 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007516 case TypeCheckKind::kArrayCheck:
7517 case TypeCheckKind::kUnresolvedCheck:
7518 case TypeCheckKind::kInterfaceCheck:
7519 call_kind = LocationSummary::kCallOnSlowPath;
7520 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007521 case TypeCheckKind::kBitstringCheck:
7522 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007523 }
7524
Vladimir Markoca6fff82017-10-03 14:49:14 +01007525 LocationSummary* locations =
7526 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007527 if (baker_read_barrier_slow_path) {
7528 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7529 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007530 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007531 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7532 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7533 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7534 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7535 } else {
7536 locations->SetInAt(1, Location::RequiresRegister());
7537 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007538 // The output does overlap inputs.
7539 // Note that TypeCheckSlowPathMIPS uses this register too.
7540 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007541 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007542}
7543
7544void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007545 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007546 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007547 Location obj_loc = locations->InAt(0);
7548 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko175e7862018-03-27 09:03:13 +00007549 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08007550 Location out_loc = locations->Out();
7551 Register out = out_loc.AsRegister<Register>();
7552 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7553 DCHECK_LE(num_temps, 1u);
7554 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007555 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7556 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7557 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7558 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007559 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007560 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007561
7562 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007563 // Avoid this check if we know `obj` is not null.
7564 if (instruction->MustDoNullCheck()) {
7565 __ Move(out, ZERO);
7566 __ Beqz(obj, &done);
7567 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007568
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007569 switch (type_check_kind) {
7570 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007571 ReadBarrierOption read_barrier_option =
7572 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007573 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007574 GenerateReferenceLoadTwoRegisters(instruction,
7575 out_loc,
7576 obj_loc,
7577 class_offset,
7578 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007579 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007580 // Classes must be equal for the instanceof to succeed.
Vladimir Marko175e7862018-03-27 09:03:13 +00007581 __ Xor(out, out, cls.AsRegister<Register>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007582 __ Sltiu(out, out, 1);
7583 break;
7584 }
7585
7586 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007587 ReadBarrierOption read_barrier_option =
7588 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007589 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007590 GenerateReferenceLoadTwoRegisters(instruction,
7591 out_loc,
7592 obj_loc,
7593 class_offset,
7594 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007595 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007596 // If the class is abstract, we eagerly fetch the super class of the
7597 // object to avoid doing a comparison we know will fail.
7598 MipsLabel loop;
7599 __ Bind(&loop);
7600 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007601 GenerateReferenceLoadOneRegister(instruction,
7602 out_loc,
7603 super_offset,
7604 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007605 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007606 // If `out` is null, we use it for the result, and jump to `done`.
7607 __ Beqz(out, &done);
Vladimir Marko175e7862018-03-27 09:03:13 +00007608 __ Bne(out, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007609 __ LoadConst32(out, 1);
7610 break;
7611 }
7612
7613 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007614 ReadBarrierOption read_barrier_option =
7615 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007616 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007617 GenerateReferenceLoadTwoRegisters(instruction,
7618 out_loc,
7619 obj_loc,
7620 class_offset,
7621 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007622 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007623 // Walk over the class hierarchy to find a match.
7624 MipsLabel loop, success;
7625 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00007626 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007627 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007628 GenerateReferenceLoadOneRegister(instruction,
7629 out_loc,
7630 super_offset,
7631 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007632 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007633 __ Bnez(out, &loop);
7634 // If `out` is null, we use it for the result, and jump to `done`.
7635 __ B(&done);
7636 __ Bind(&success);
7637 __ LoadConst32(out, 1);
7638 break;
7639 }
7640
7641 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007642 ReadBarrierOption read_barrier_option =
7643 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007644 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007645 GenerateReferenceLoadTwoRegisters(instruction,
7646 out_loc,
7647 obj_loc,
7648 class_offset,
7649 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007650 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007651 // Do an exact check.
7652 MipsLabel success;
Vladimir Marko175e7862018-03-27 09:03:13 +00007653 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007654 // Otherwise, we need to check that the object's class is a non-primitive array.
7655 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007656 GenerateReferenceLoadOneRegister(instruction,
7657 out_loc,
7658 component_offset,
7659 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007660 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007661 // If `out` is null, we use it for the result, and jump to `done`.
7662 __ Beqz(out, &done);
7663 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7664 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7665 __ Sltiu(out, out, 1);
7666 __ B(&done);
7667 __ Bind(&success);
7668 __ LoadConst32(out, 1);
7669 break;
7670 }
7671
7672 case TypeCheckKind::kArrayCheck: {
7673 // No read barrier since the slow path will retry upon failure.
7674 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007675 GenerateReferenceLoadTwoRegisters(instruction,
7676 out_loc,
7677 obj_loc,
7678 class_offset,
7679 maybe_temp_loc,
7680 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007681 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007682 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7683 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007684 codegen_->AddSlowPath(slow_path);
Vladimir Marko175e7862018-03-27 09:03:13 +00007685 __ Bne(out, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007686 __ LoadConst32(out, 1);
7687 break;
7688 }
7689
7690 case TypeCheckKind::kUnresolvedCheck:
7691 case TypeCheckKind::kInterfaceCheck: {
7692 // Note that we indeed only call on slow path, but we always go
7693 // into the slow path for the unresolved and interface check
7694 // cases.
7695 //
7696 // We cannot directly call the InstanceofNonTrivial runtime
7697 // entry point without resorting to a type checking slow path
7698 // here (i.e. by calling InvokeRuntime directly), as it would
7699 // require to assign fixed registers for the inputs of this
7700 // HInstanceOf instruction (following the runtime calling
7701 // convention), which might be cluttered by the potential first
7702 // read barrier emission at the beginning of this method.
7703 //
7704 // TODO: Introduce a new runtime entry point taking the object
7705 // to test (instead of its class) as argument, and let it deal
7706 // with the read barrier issues. This will let us refactor this
7707 // case of the `switch` code as it was previously (with a direct
7708 // call to the runtime not using a type checking slow path).
7709 // This should also be beneficial for the other cases above.
7710 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007711 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7712 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007713 codegen_->AddSlowPath(slow_path);
7714 __ B(slow_path->GetEntryLabel());
7715 break;
7716 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007717
7718 case TypeCheckKind::kBitstringCheck: {
7719 // /* HeapReference<Class> */ temp = obj->klass_
7720 GenerateReferenceLoadTwoRegisters(instruction,
7721 out_loc,
7722 obj_loc,
7723 class_offset,
7724 maybe_temp_loc,
7725 kWithoutReadBarrier);
7726
7727 GenerateBitstringTypeCheckCompare(instruction, out);
7728 __ Sltiu(out, out, 1);
7729 break;
7730 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007731 }
7732
7733 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007734
7735 if (slow_path != nullptr) {
7736 __ Bind(slow_path->GetExitLabel());
7737 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007738}
7739
7740void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007741 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007742 locations->SetOut(Location::ConstantLocation(constant));
7743}
7744
7745void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7746 // Will be generated at use site.
7747}
7748
7749void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007750 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007751 locations->SetOut(Location::ConstantLocation(constant));
7752}
7753
7754void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7755 // Will be generated at use site.
7756}
7757
7758void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7759 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7760 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7761}
7762
7763void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7764 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007765 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007766 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007767 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007768}
7769
7770void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7771 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7772 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007773 Location receiver = invoke->GetLocations()->InAt(0);
7774 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007775 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007776
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007777 // temp = object->GetClass();
7778 if (receiver.IsStackSlot()) {
7779 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7780 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7781 } else {
7782 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7783 }
7784 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007785 // Instead of simply (possibly) unpoisoning `temp` here, we should
7786 // emit a read barrier for the previous class reference load.
7787 // However this is not required in practice, as this is an
7788 // intermediate/temporary reference and because the current
7789 // concurrent copying collector keeps the from-space memory
7790 // intact/accessible until the end of the marking phase (the
7791 // concurrent copying collector may not in the future).
7792 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007793 __ LoadFromOffset(kLoadWord, temp, temp,
7794 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7795 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007796 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007797 // temp = temp->GetImtEntryAt(method_offset);
7798 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7799 // T9 = temp->GetEntryPoint();
7800 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
Lena Djokic3177e102018-02-28 11:32:40 +01007801 // Set the hidden argument.
7802 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7803 invoke->GetDexMethodIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007804 // T9();
7805 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007806 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007807 DCHECK(!codegen_->IsLeafMethod());
7808 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7809}
7810
7811void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007812 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7813 if (intrinsic.TryDispatch(invoke)) {
7814 return;
7815 }
7816
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007817 HandleInvoke(invoke);
7818}
7819
7820void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007821 // Explicit clinit checks triggered by static invokes must have been pruned by
7822 // art::PrepareForRegisterAllocation.
7823 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007824
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007825 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007826 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7827 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007828
Chris Larsen701566a2015-10-27 15:29:13 -07007829 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7830 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007831 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7832 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7833 }
Chris Larsen701566a2015-10-27 15:29:13 -07007834 return;
7835 }
7836
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007837 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007838
7839 // Add the extra input register if either the dex cache array base register
7840 // or the PC-relative base register for accessing literals is needed.
7841 if (has_extra_input) {
7842 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7843 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007844}
7845
Orion Hodsonac141392017-01-13 11:53:47 +00007846void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7847 HandleInvoke(invoke);
7848}
7849
7850void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7851 codegen_->GenerateInvokePolymorphicCall(invoke);
7852}
7853
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007854void LocationsBuilderMIPS::VisitInvokeCustom(HInvokeCustom* invoke) {
7855 HandleInvoke(invoke);
7856}
7857
7858void InstructionCodeGeneratorMIPS::VisitInvokeCustom(HInvokeCustom* invoke) {
7859 codegen_->GenerateInvokeCustomCall(invoke);
7860}
7861
Chris Larsen701566a2015-10-27 15:29:13 -07007862static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007863 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007864 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7865 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007866 return true;
7867 }
7868 return false;
7869}
7870
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007871HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007872 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007873 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007874 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007875 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007876 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007877 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007878 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007879 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007880 case HLoadString::LoadKind::kJitTableAddress:
7881 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007882 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007883 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007884 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007885 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007886 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007887}
7888
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007889HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7890 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007891 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007892 case HLoadClass::LoadKind::kInvalid:
7893 LOG(FATAL) << "UNREACHABLE";
7894 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007895 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007896 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007897 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007898 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007899 case HLoadClass::LoadKind::kBssEntry:
7900 DCHECK(!Runtime::Current()->UseJitCompilation());
7901 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007902 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007903 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007904 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007905 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007906 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007907 break;
7908 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007909 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007910}
7911
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007912Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7913 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007914 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007915 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007916 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7917 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7918 if (!invoke->GetLocations()->Intrinsified()) {
7919 return location.AsRegister<Register>();
7920 }
7921 // For intrinsics we allow any location, so it may be on the stack.
7922 if (!location.IsRegister()) {
7923 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7924 return temp;
7925 }
7926 // For register locations, check if the register was saved. If so, get it from the stack.
7927 // Note: There is a chance that the register was saved but not overwritten, so we could
7928 // save one load. However, since this is just an intrinsic slow path we prefer this
7929 // simple and more robust approach rather that trying to determine if that's the case.
7930 SlowPathCode* slow_path = GetCurrentSlowPath();
7931 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7932 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7933 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7934 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7935 return temp;
7936 }
7937 return location.AsRegister<Register>();
7938}
7939
Vladimir Markodc151b22015-10-15 18:02:30 +01007940HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7941 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007942 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007943 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007944}
7945
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007946void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7947 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007948 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007949 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007950 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7951 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007952 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007953 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7954 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007955 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7956 : ZERO;
7957
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007958 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007959 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007960 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007961 uint32_t offset =
7962 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007963 __ LoadFromOffset(kLoadWord,
7964 temp.AsRegister<Register>(),
7965 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007966 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007967 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007968 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007969 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007970 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007971 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007972 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7973 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007974 PcRelativePatchInfo* info_high = NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007975 PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007976 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007977 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007978 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7979 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007980 break;
7981 }
Vladimir Markob066d432018-01-03 13:14:37 +00007982 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007983 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00007984 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
7985 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
7986 Register temp_reg = temp.AsRegister<Register>();
7987 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7988 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
7989 break;
7990 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007991 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007992 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007993 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007994 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7995 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007996 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007997 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7998 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007999 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008000 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008001 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
8002 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8003 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008004 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8005 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8006 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008007 }
8008 }
8009
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008010 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008011 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008012 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008013 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008014 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8015 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01008016 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008017 T9,
8018 callee_method.AsRegister<Register>(),
8019 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07008020 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008021 // T9()
8022 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008023 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008024 break;
8025 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008026 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8027
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008028 DCHECK(!IsLeafMethod());
8029}
8030
8031void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00008032 // Explicit clinit checks triggered by static invokes must have been pruned by
8033 // art::PrepareForRegisterAllocation.
8034 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008035
8036 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8037 return;
8038 }
8039
8040 LocationSummary* locations = invoke->GetLocations();
8041 codegen_->GenerateStaticOrDirectCall(invoke,
8042 locations->HasTemps()
8043 ? locations->GetTemp(0)
8044 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008045}
8046
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008047void CodeGeneratorMIPS::GenerateVirtualCall(
8048 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02008049 // Use the calling convention instead of the location of the receiver, as
8050 // intrinsics may have put the receiver in a different register. In the intrinsics
8051 // slow path, the arguments have been moved to the right place, so here we are
8052 // guaranteed that the receiver is the first register of the calling convention.
8053 InvokeDexCallingConvention calling_convention;
8054 Register receiver = calling_convention.GetRegisterAt(0);
8055
Chris Larsen3acee732015-11-18 13:31:08 -08008056 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008057 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8058 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
8059 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07008060 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008061
8062 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02008063 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08008064 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08008065 // Instead of simply (possibly) unpoisoning `temp` here, we should
8066 // emit a read barrier for the previous class reference load.
8067 // However this is not required in practice, as this is an
8068 // intermediate/temporary reference and because the current
8069 // concurrent copying collector keeps the from-space memory
8070 // intact/accessible until the end of the marking phase (the
8071 // concurrent copying collector may not in the future).
8072 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008073 // temp = temp->GetMethodAt(method_offset);
8074 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8075 // T9 = temp->GetEntryPoint();
8076 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
8077 // T9();
8078 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008079 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008080 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08008081}
8082
8083void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
8084 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8085 return;
8086 }
8087
8088 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008089 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008090}
8091
8092void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00008093 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008094 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008095 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008096 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8097 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008098 return;
8099 }
Vladimir Marko41559982017-01-06 14:04:23 +00008100 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008101 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008102 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08008103 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
8104 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07008105 ? LocationSummary::kCallOnSlowPath
8106 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008107 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008108 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
8109 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
8110 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008111 switch (load_kind) {
8112 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008113 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008114 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008115 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008116 case HLoadClass::LoadKind::kJitBootImageAddress:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008117 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008118 break;
8119 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008120 if (has_irreducible_loops) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008121 if (load_kind != HLoadClass::LoadKind::kJitBootImageAddress) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008122 codegen_->ClobberRA();
8123 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008124 break;
8125 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008126 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008127 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008128 locations->SetInAt(0, Location::RequiresRegister());
8129 break;
8130 default:
8131 break;
8132 }
8133 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008134 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
8135 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8136 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008137 RegisterSet caller_saves = RegisterSet::Empty();
8138 InvokeRuntimeCallingConvention calling_convention;
8139 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8140 locations->SetCustomSlowPathCallerSaves(caller_saves);
8141 } else {
8142 // For non-Baker read barriers we have a temp-clobbering call.
8143 }
8144 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008145}
8146
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008147// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8148// move.
8149void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00008150 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008151 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00008152 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01008153 return;
8154 }
Vladimir Marko41559982017-01-06 14:04:23 +00008155 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008156
Vladimir Marko41559982017-01-06 14:04:23 +00008157 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008158 Location out_loc = locations->Out();
8159 Register out = out_loc.AsRegister<Register>();
8160 Register base_or_current_method_reg;
8161 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008162 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008163 switch (load_kind) {
8164 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008165 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008166 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008167 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008168 case HLoadClass::LoadKind::kJitBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008169 base_or_current_method_reg =
8170 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008171 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008172 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008173 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008174 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8175 break;
8176 default:
8177 base_or_current_method_reg = ZERO;
8178 break;
8179 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008180
Alexey Frunze15958152017-02-09 19:08:30 -08008181 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8182 ? kWithoutReadBarrier
8183 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008184 bool generate_null_check = false;
8185 switch (load_kind) {
8186 case HLoadClass::LoadKind::kReferrersClass: {
8187 DCHECK(!cls->CanCallRuntime());
8188 DCHECK(!cls->MustGenerateClinitCheck());
8189 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8190 GenerateGcRootFieldLoad(cls,
8191 out_loc,
8192 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008193 ArtMethod::DeclaringClassOffset().Int32Value(),
8194 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008195 break;
8196 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008197 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008198 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008199 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008200 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008201 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008202 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008203 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008204 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8205 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008206 base_or_current_method_reg);
8207 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008208 break;
8209 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008210 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008211 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008212 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008213 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008214 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008215 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008216 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008217 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8218 out,
8219 base_or_current_method_reg);
8220 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008221 break;
8222 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008223 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008224 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8225 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008226 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8227 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008228 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008229 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008230 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008231 GenerateGcRootFieldLoad(cls,
8232 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008233 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008234 /* placeholder */ 0x5678,
8235 read_barrier_option,
8236 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008237 generate_null_check = true;
8238 break;
8239 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008240 case HLoadClass::LoadKind::kJitBootImageAddress: {
8241 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
8242 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
8243 DCHECK_NE(address, 0u);
8244 if (isR6 || !has_irreducible_loops) {
8245 __ LoadLiteral(out,
8246 base_or_current_method_reg,
8247 codegen_->DeduplicateBootImageAddressLiteral(address));
8248 } else {
8249 __ LoadConst32(out, address);
8250 }
8251 break;
8252 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008253 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008254 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8255 cls->GetTypeIndex(),
8256 cls->GetClass());
8257 bool reordering = __ SetReorder(false);
8258 __ Bind(&info->high_label);
8259 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008260 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008261 GenerateGcRootFieldLoad(cls,
8262 out_loc,
8263 out,
8264 /* placeholder */ 0x5678,
8265 read_barrier_option,
8266 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008267 break;
8268 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008269 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008270 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008271 LOG(FATAL) << "UNREACHABLE";
8272 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008273 }
8274
8275 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8276 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01008277 SlowPathCodeMIPS* slow_path =
8278 new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(cls, cls);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008279 codegen_->AddSlowPath(slow_path);
8280 if (generate_null_check) {
8281 __ Beqz(out, slow_path->GetEntryLabel());
8282 }
8283 if (cls->MustGenerateClinitCheck()) {
8284 GenerateClassInitializationCheck(slow_path, out);
8285 } else {
8286 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008287 }
8288 }
8289}
8290
Orion Hodsondbaa5c72018-05-10 08:22:46 +01008291void LocationsBuilderMIPS::VisitLoadMethodHandle(HLoadMethodHandle* load) {
8292 InvokeRuntimeCallingConvention calling_convention;
8293 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8294 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, loc, loc);
8295}
8296
8297void InstructionCodeGeneratorMIPS::VisitLoadMethodHandle(HLoadMethodHandle* load) {
8298 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
8299}
8300
Orion Hodson18259d72018-04-12 11:18:23 +01008301void LocationsBuilderMIPS::VisitLoadMethodType(HLoadMethodType* load) {
8302 InvokeRuntimeCallingConvention calling_convention;
8303 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8304 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, loc, loc);
8305}
8306
8307void InstructionCodeGeneratorMIPS::VisitLoadMethodType(HLoadMethodType* load) {
8308 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
8309}
8310
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008311static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008312 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008313}
8314
8315void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8316 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008317 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008318 locations->SetOut(Location::RequiresRegister());
8319}
8320
8321void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8322 Register out = load->GetLocations()->Out().AsRegister<Register>();
8323 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8324}
8325
8326void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008327 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008328}
8329
8330void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8331 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8332}
8333
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008334void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008335 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008336 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008337 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008338 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008339 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008340 switch (load_kind) {
8341 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008342 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008343 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008344 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008345 case HLoadString::LoadKind::kJitBootImageAddress:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008346 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008347 break;
8348 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008349 if (has_irreducible_loops) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008350 if (load_kind != HLoadString::LoadKind::kJitBootImageAddress) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008351 codegen_->ClobberRA();
8352 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008353 break;
8354 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008355 FALLTHROUGH_INTENDED;
8356 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008357 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008358 locations->SetInAt(0, Location::RequiresRegister());
8359 break;
8360 default:
8361 break;
8362 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008363 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008364 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008365 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008366 } else {
8367 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008368 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8369 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8370 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008371 RegisterSet caller_saves = RegisterSet::Empty();
8372 InvokeRuntimeCallingConvention calling_convention;
8373 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8374 locations->SetCustomSlowPathCallerSaves(caller_saves);
8375 } else {
8376 // For non-Baker read barriers we have a temp-clobbering call.
8377 }
8378 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008379 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008380}
8381
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008382// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8383// move.
8384void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008385 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008386 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008387 Location out_loc = locations->Out();
8388 Register out = out_loc.AsRegister<Register>();
8389 Register base_or_current_method_reg;
8390 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008391 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008392 switch (load_kind) {
8393 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008394 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008395 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008396 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008397 case HLoadString::LoadKind::kJitBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008398 base_or_current_method_reg =
8399 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008400 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008401 default:
8402 base_or_current_method_reg = ZERO;
8403 break;
8404 }
8405
8406 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008407 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008408 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008409 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008410 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008411 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008412 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008413 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8414 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008415 base_or_current_method_reg);
8416 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008417 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008418 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008419 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008420 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008421 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008422 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008423 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008424 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008425 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008426 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8427 out,
8428 base_or_current_method_reg);
8429 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8430 return;
8431 }
8432 case HLoadString::LoadKind::kBssEntry: {
8433 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8434 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8435 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8436 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8437 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008438 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008439 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008440 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008441 GenerateGcRootFieldLoad(load,
8442 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008443 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008444 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008445 kCompilerReadBarrierOption,
8446 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008447 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008448 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008449 codegen_->AddSlowPath(slow_path);
8450 __ Beqz(out, slow_path->GetEntryLabel());
8451 __ Bind(slow_path->GetExitLabel());
8452 return;
8453 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008454 case HLoadString::LoadKind::kJitBootImageAddress: {
8455 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
8456 DCHECK_NE(address, 0u);
8457 if (isR6 || !has_irreducible_loops) {
8458 __ LoadLiteral(out,
8459 base_or_current_method_reg,
8460 codegen_->DeduplicateBootImageAddressLiteral(address));
8461 } else {
8462 __ LoadConst32(out, address);
8463 }
8464 return;
8465 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008466 case HLoadString::LoadKind::kJitTableAddress: {
8467 CodeGeneratorMIPS::JitPatchInfo* info =
8468 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8469 load->GetStringIndex(),
8470 load->GetString());
8471 bool reordering = __ SetReorder(false);
8472 __ Bind(&info->high_label);
8473 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008474 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008475 GenerateGcRootFieldLoad(load,
8476 out_loc,
8477 out,
8478 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008479 kCompilerReadBarrierOption,
8480 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008481 return;
8482 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008483 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008484 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008485 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008486
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008487 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008488 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008489 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008490 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008491 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008492 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8493 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008494}
8495
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008496void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008497 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008498 locations->SetOut(Location::ConstantLocation(constant));
8499}
8500
8501void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8502 // Will be generated at use site.
8503}
8504
8505void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008506 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8507 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008508 InvokeRuntimeCallingConvention calling_convention;
8509 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8510}
8511
8512void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8513 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008514 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008515 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8516 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008517 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008518 }
8519 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8520}
8521
8522void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8523 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008524 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008525 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008526 case DataType::Type::kInt32:
8527 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008528 locations->SetInAt(0, Location::RequiresRegister());
8529 locations->SetInAt(1, Location::RequiresRegister());
8530 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8531 break;
8532
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008533 case DataType::Type::kFloat32:
8534 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008535 locations->SetInAt(0, Location::RequiresFpuRegister());
8536 locations->SetInAt(1, Location::RequiresFpuRegister());
8537 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8538 break;
8539
8540 default:
8541 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8542 }
8543}
8544
8545void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008546 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008547 LocationSummary* locations = instruction->GetLocations();
8548 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8549
8550 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008551 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008552 Register dst = locations->Out().AsRegister<Register>();
8553 Register lhs = locations->InAt(0).AsRegister<Register>();
8554 Register rhs = locations->InAt(1).AsRegister<Register>();
8555
8556 if (isR6) {
8557 __ MulR6(dst, lhs, rhs);
8558 } else {
8559 __ MulR2(dst, lhs, rhs);
8560 }
8561 break;
8562 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008563 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008564 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8565 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8566 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8567 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8568 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8569 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8570
8571 // Extra checks to protect caused by the existance of A1_A2.
8572 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8573 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8574 DCHECK_NE(dst_high, lhs_low);
8575 DCHECK_NE(dst_high, rhs_low);
8576
8577 // A_B * C_D
8578 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8579 // dst_lo: [ low(B*D) ]
8580 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8581
8582 if (isR6) {
8583 __ MulR6(TMP, lhs_high, rhs_low);
8584 __ MulR6(dst_high, lhs_low, rhs_high);
8585 __ Addu(dst_high, dst_high, TMP);
8586 __ MuhuR6(TMP, lhs_low, rhs_low);
8587 __ Addu(dst_high, dst_high, TMP);
8588 __ MulR6(dst_low, lhs_low, rhs_low);
8589 } else {
8590 __ MulR2(TMP, lhs_high, rhs_low);
8591 __ MulR2(dst_high, lhs_low, rhs_high);
8592 __ Addu(dst_high, dst_high, TMP);
8593 __ MultuR2(lhs_low, rhs_low);
8594 __ Mfhi(TMP);
8595 __ Addu(dst_high, dst_high, TMP);
8596 __ Mflo(dst_low);
8597 }
8598 break;
8599 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008600 case DataType::Type::kFloat32:
8601 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008602 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8603 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8604 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008605 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008606 __ MulS(dst, lhs, rhs);
8607 } else {
8608 __ MulD(dst, lhs, rhs);
8609 }
8610 break;
8611 }
8612 default:
8613 LOG(FATAL) << "Unexpected mul type " << type;
8614 }
8615}
8616
8617void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8618 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008619 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008620 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008621 case DataType::Type::kInt32:
8622 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008623 locations->SetInAt(0, Location::RequiresRegister());
8624 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8625 break;
8626
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008627 case DataType::Type::kFloat32:
8628 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008629 locations->SetInAt(0, Location::RequiresFpuRegister());
8630 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8631 break;
8632
8633 default:
8634 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8635 }
8636}
8637
8638void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008639 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008640 LocationSummary* locations = instruction->GetLocations();
8641
8642 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008643 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008644 Register dst = locations->Out().AsRegister<Register>();
8645 Register src = locations->InAt(0).AsRegister<Register>();
8646 __ Subu(dst, ZERO, src);
8647 break;
8648 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008649 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008650 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8651 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8652 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8653 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8654 __ Subu(dst_low, ZERO, src_low);
8655 __ Sltu(TMP, ZERO, dst_low);
8656 __ Subu(dst_high, ZERO, src_high);
8657 __ Subu(dst_high, dst_high, TMP);
8658 break;
8659 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008660 case DataType::Type::kFloat32:
8661 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008662 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8663 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008664 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008665 __ NegS(dst, src);
8666 } else {
8667 __ NegD(dst, src);
8668 }
8669 break;
8670 }
8671 default:
8672 LOG(FATAL) << "Unexpected neg type " << type;
8673 }
8674}
8675
8676void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008677 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8678 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008679 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008680 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008681 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8682 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008683}
8684
8685void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008686 // Note: if heap poisoning is enabled, the entry point takes care
8687 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008688 QuickEntrypointEnum entrypoint =
8689 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8690 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008691 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008692 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008693}
8694
8695void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008696 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8697 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008698 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07008699 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008700 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008701}
8702
8703void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07008704 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
8705 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008706}
8707
8708void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008709 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008710 locations->SetInAt(0, Location::RequiresRegister());
8711 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8712}
8713
8714void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008715 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008716 LocationSummary* locations = instruction->GetLocations();
8717
8718 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008719 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008720 Register dst = locations->Out().AsRegister<Register>();
8721 Register src = locations->InAt(0).AsRegister<Register>();
8722 __ Nor(dst, src, ZERO);
8723 break;
8724 }
8725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008726 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008727 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8728 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8729 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8730 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8731 __ Nor(dst_high, src_high, ZERO);
8732 __ Nor(dst_low, src_low, ZERO);
8733 break;
8734 }
8735
8736 default:
8737 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8738 }
8739}
8740
8741void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008742 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008743 locations->SetInAt(0, Location::RequiresRegister());
8744 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8745}
8746
8747void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8748 LocationSummary* locations = instruction->GetLocations();
8749 __ Xori(locations->Out().AsRegister<Register>(),
8750 locations->InAt(0).AsRegister<Register>(),
8751 1);
8752}
8753
8754void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008755 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8756 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008757}
8758
Calin Juravle2ae48182016-03-16 14:05:09 +00008759void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8760 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008761 return;
8762 }
8763 Location obj = instruction->GetLocations()->InAt(0);
8764
8765 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008766 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008767}
8768
Calin Juravle2ae48182016-03-16 14:05:09 +00008769void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008770 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008771 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008772
8773 Location obj = instruction->GetLocations()->InAt(0);
8774
8775 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8776}
8777
8778void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008779 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008780}
8781
8782void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8783 HandleBinaryOp(instruction);
8784}
8785
8786void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8787 HandleBinaryOp(instruction);
8788}
8789
8790void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8791 LOG(FATAL) << "Unreachable";
8792}
8793
8794void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008795 if (instruction->GetNext()->IsSuspendCheck() &&
8796 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8797 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8798 // The back edge will generate the suspend check.
8799 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8800 }
8801
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008802 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8803}
8804
8805void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008806 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008807 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8808 if (location.IsStackSlot()) {
8809 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8810 } else if (location.IsDoubleStackSlot()) {
8811 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8812 }
8813 locations->SetOut(location);
8814}
8815
8816void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8817 ATTRIBUTE_UNUSED) {
8818 // Nothing to do, the parameter is already at its location.
8819}
8820
8821void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8822 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008823 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008824 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8825}
8826
8827void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8828 ATTRIBUTE_UNUSED) {
8829 // Nothing to do, the method is already at its location.
8830}
8831
8832void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008833 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008834 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008835 locations->SetInAt(i, Location::Any());
8836 }
8837 locations->SetOut(Location::Any());
8838}
8839
8840void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8841 LOG(FATAL) << "Unreachable";
8842}
8843
8844void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008845 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008846 bool call_rem;
8847 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8848 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8849 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8850 } else {
8851 call_rem = (type != DataType::Type::kInt32);
8852 }
8853 LocationSummary::CallKind call_kind = call_rem
8854 ? LocationSummary::kCallOnMainOnly
8855 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008856 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008857
8858 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008859 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008860 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008861 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008862 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8863 break;
8864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008865 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008866 if (call_rem) {
8867 InvokeRuntimeCallingConvention calling_convention;
8868 locations->SetInAt(0, Location::RegisterPairLocation(
8869 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8870 locations->SetInAt(1, Location::RegisterPairLocation(
8871 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8872 locations->SetOut(calling_convention.GetReturnLocation(type));
8873 } else {
8874 locations->SetInAt(0, Location::RequiresRegister());
8875 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8876 locations->SetOut(Location::RequiresRegister());
8877 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008878 break;
8879 }
8880
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008881 case DataType::Type::kFloat32:
8882 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008883 InvokeRuntimeCallingConvention calling_convention;
8884 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8885 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8886 locations->SetOut(calling_convention.GetReturnLocation(type));
8887 break;
8888 }
8889
8890 default:
8891 LOG(FATAL) << "Unexpected rem type " << type;
8892 }
8893}
8894
8895void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008896 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008897 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008898
8899 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008900 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008901 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008902 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008903 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008904 if (locations->InAt(1).IsConstant()) {
8905 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8906 if (imm == 0) {
8907 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8908 } else if (imm == 1 || imm == -1) {
8909 DivRemOneOrMinusOne(instruction);
8910 } else {
8911 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8912 DivRemByPowerOfTwo(instruction);
8913 }
8914 } else {
8915 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8916 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8917 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008918 break;
8919 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008920 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008921 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008922 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008923 break;
8924 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008925 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008926 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008927 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008928 break;
8929 }
8930 default:
8931 LOG(FATAL) << "Unexpected rem type " << type;
8932 }
8933}
8934
Aart Bik1f8d51b2018-02-15 10:42:37 -08008935static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
8936 LocationSummary* locations = new (allocator) LocationSummary(minmax);
8937 switch (minmax->GetResultType()) {
8938 case DataType::Type::kInt32:
8939 case DataType::Type::kInt64:
8940 locations->SetInAt(0, Location::RequiresRegister());
8941 locations->SetInAt(1, Location::RequiresRegister());
8942 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8943 break;
8944 case DataType::Type::kFloat32:
8945 case DataType::Type::kFloat64:
8946 locations->SetInAt(0, Location::RequiresFpuRegister());
8947 locations->SetInAt(1, Location::RequiresFpuRegister());
8948 locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap);
8949 break;
8950 default:
8951 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
8952 }
8953}
8954
Aart Bik351df3e2018-03-07 11:54:57 -08008955void InstructionCodeGeneratorMIPS::GenerateMinMaxInt(LocationSummary* locations,
8956 bool is_min,
8957 bool isR6,
8958 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08008959 if (isR6) {
8960 // Some architectures, such as ARM and MIPS (prior to r6), have a
8961 // conditional move instruction which only changes the target
8962 // (output) register if the condition is true (MIPS prior to r6 had
8963 // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions
8964 // always change the target (output) register. If the condition is
8965 // true the output register gets the contents of the "rs" register;
8966 // otherwise, the output register is set to zero. One consequence
8967 // of this is that to implement something like "rd = c==0 ? rs : rt"
8968 // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions.
8969 // After executing this pair of instructions one of the output
8970 // registers from the pair will necessarily contain zero. Then the
8971 // code ORs the output registers from the SELEQZ/SELNEZ instructions
8972 // to get the final result.
8973 //
8974 // The initial test to see if the output register is same as the
8975 // first input register is needed to make sure that value in the
8976 // first input register isn't clobbered before we've finished
8977 // computing the output value. The logic in the corresponding else
8978 // clause performs the same task but makes sure the second input
8979 // register isn't clobbered in the event that it's the same register
8980 // as the output register; the else clause also handles the case
8981 // where the output register is distinct from both the first, and the
8982 // second input registers.
8983 if (type == DataType::Type::kInt64) {
8984 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8985 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8986 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
8987 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
8988 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8989 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8990
8991 MipsLabel compare_done;
8992
8993 if (a_lo == b_lo) {
8994 if (out_lo != a_lo) {
8995 __ Move(out_lo, a_lo);
8996 __ Move(out_hi, a_hi);
8997 }
8998 } else {
8999 __ Slt(TMP, b_hi, a_hi);
9000 __ Bne(b_hi, a_hi, &compare_done);
9001
9002 __ Sltu(TMP, b_lo, a_lo);
9003
9004 __ Bind(&compare_done);
9005
9006 if (is_min) {
9007 __ Seleqz(AT, a_lo, TMP);
9008 __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo
9009 // because at this point we're
9010 // done using a_lo/b_lo.
9011 } else {
9012 __ Selnez(AT, a_lo, TMP);
9013 __ Seleqz(out_lo, b_lo, TMP); // ditto
9014 }
9015 __ Or(out_lo, out_lo, AT);
9016 if (is_min) {
9017 __ Seleqz(AT, a_hi, TMP);
9018 __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
9019 } else {
9020 __ Selnez(AT, a_hi, TMP);
9021 __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
9022 }
9023 __ Or(out_hi, out_hi, AT);
9024 }
9025 } else {
9026 DCHECK_EQ(type, DataType::Type::kInt32);
9027 Register a = locations->InAt(0).AsRegister<Register>();
9028 Register b = locations->InAt(1).AsRegister<Register>();
9029 Register out = locations->Out().AsRegister<Register>();
9030
9031 if (a == b) {
9032 if (out != a) {
9033 __ Move(out, a);
9034 }
9035 } else {
9036 __ Slt(AT, b, a);
9037 if (is_min) {
9038 __ Seleqz(TMP, a, AT);
9039 __ Selnez(AT, b, AT);
9040 } else {
9041 __ Selnez(TMP, a, AT);
9042 __ Seleqz(AT, b, AT);
9043 }
9044 __ Or(out, TMP, AT);
9045 }
9046 }
9047 } else { // !isR6
9048 if (type == DataType::Type::kInt64) {
9049 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9050 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9051 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
9052 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
9053 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9054 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9055
9056 MipsLabel compare_done;
9057
9058 if (a_lo == b_lo) {
9059 if (out_lo != a_lo) {
9060 __ Move(out_lo, a_lo);
9061 __ Move(out_hi, a_hi);
9062 }
9063 } else {
9064 __ Slt(TMP, a_hi, b_hi);
9065 __ Bne(a_hi, b_hi, &compare_done);
9066
9067 __ Sltu(TMP, a_lo, b_lo);
9068
9069 __ Bind(&compare_done);
9070
9071 if (is_min) {
9072 if (out_lo != a_lo) {
9073 __ Movn(out_hi, a_hi, TMP);
9074 __ Movn(out_lo, a_lo, TMP);
9075 }
9076 if (out_lo != b_lo) {
9077 __ Movz(out_hi, b_hi, TMP);
9078 __ Movz(out_lo, b_lo, TMP);
9079 }
9080 } else {
9081 if (out_lo != a_lo) {
9082 __ Movz(out_hi, a_hi, TMP);
9083 __ Movz(out_lo, a_lo, TMP);
9084 }
9085 if (out_lo != b_lo) {
9086 __ Movn(out_hi, b_hi, TMP);
9087 __ Movn(out_lo, b_lo, TMP);
9088 }
9089 }
9090 }
9091 } else {
9092 DCHECK_EQ(type, DataType::Type::kInt32);
9093 Register a = locations->InAt(0).AsRegister<Register>();
9094 Register b = locations->InAt(1).AsRegister<Register>();
9095 Register out = locations->Out().AsRegister<Register>();
9096
9097 if (a == b) {
9098 if (out != a) {
9099 __ Move(out, a);
9100 }
9101 } else {
9102 __ Slt(AT, a, b);
9103 if (is_min) {
9104 if (out != a) {
9105 __ Movn(out, a, AT);
9106 }
9107 if (out != b) {
9108 __ Movz(out, b, AT);
9109 }
9110 } else {
9111 if (out != a) {
9112 __ Movz(out, a, AT);
9113 }
9114 if (out != b) {
9115 __ Movn(out, b, AT);
9116 }
9117 }
9118 }
9119 }
9120 }
9121}
9122
9123void InstructionCodeGeneratorMIPS::GenerateMinMaxFP(LocationSummary* locations,
9124 bool is_min,
9125 bool isR6,
9126 DataType::Type type) {
9127 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9128 FRegister a = locations->InAt(0).AsFpuRegister<FRegister>();
9129 FRegister b = locations->InAt(1).AsFpuRegister<FRegister>();
9130
9131 if (isR6) {
9132 MipsLabel noNaNs;
9133 MipsLabel done;
9134 FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
9135
9136 // When Java computes min/max it prefers a NaN to a number; the
9137 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
9138 // the inputs is a NaN and the other is a valid number, the MIPS
9139 // instruction will return the number; Java wants the NaN value
9140 // returned. This is why there is extra logic preceding the use of
9141 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
9142 // NaN, return the NaN, otherwise return the min/max.
9143 if (type == DataType::Type::kFloat64) {
9144 __ CmpUnD(FTMP, a, b);
9145 __ Bc1eqz(FTMP, &noNaNs);
9146
9147 // One of the inputs is a NaN
9148 __ CmpEqD(ftmp, a, a);
9149 // If a == a then b is the NaN, otherwise a is the NaN.
9150 __ SelD(ftmp, a, b);
9151
9152 if (ftmp != out) {
9153 __ MovD(out, ftmp);
9154 }
9155
9156 __ B(&done);
9157
9158 __ Bind(&noNaNs);
9159
9160 if (is_min) {
9161 __ MinD(out, a, b);
9162 } else {
9163 __ MaxD(out, a, b);
9164 }
9165 } else {
9166 DCHECK_EQ(type, DataType::Type::kFloat32);
9167 __ CmpUnS(FTMP, a, b);
9168 __ Bc1eqz(FTMP, &noNaNs);
9169
9170 // One of the inputs is a NaN
9171 __ CmpEqS(ftmp, a, a);
9172 // If a == a then b is the NaN, otherwise a is the NaN.
9173 __ SelS(ftmp, a, b);
9174
9175 if (ftmp != out) {
9176 __ MovS(out, ftmp);
9177 }
9178
9179 __ B(&done);
9180
9181 __ Bind(&noNaNs);
9182
9183 if (is_min) {
9184 __ MinS(out, a, b);
9185 } else {
9186 __ MaxS(out, a, b);
9187 }
9188 }
9189
9190 __ Bind(&done);
9191
9192 } else { // !isR6
9193 MipsLabel ordered;
9194 MipsLabel compare;
9195 MipsLabel select;
9196 MipsLabel done;
9197
9198 if (type == DataType::Type::kFloat64) {
9199 __ CunD(a, b);
9200 } else {
9201 DCHECK_EQ(type, DataType::Type::kFloat32);
9202 __ CunS(a, b);
9203 }
9204 __ Bc1f(&ordered);
9205
9206 // a or b (or both) is a NaN. Return one, which is a NaN.
9207 if (type == DataType::Type::kFloat64) {
9208 __ CeqD(b, b);
9209 } else {
9210 __ CeqS(b, b);
9211 }
9212 __ B(&select);
9213
9214 __ Bind(&ordered);
9215
9216 // Neither is a NaN.
9217 // a == b? (-0.0 compares equal with +0.0)
9218 // If equal, handle zeroes, else compare further.
9219 if (type == DataType::Type::kFloat64) {
9220 __ CeqD(a, b);
9221 } else {
9222 __ CeqS(a, b);
9223 }
9224 __ Bc1f(&compare);
9225
9226 // a == b either bit for bit or one is -0.0 and the other is +0.0.
9227 if (type == DataType::Type::kFloat64) {
9228 __ MoveFromFpuHigh(TMP, a);
9229 __ MoveFromFpuHigh(AT, b);
9230 } else {
9231 __ Mfc1(TMP, a);
9232 __ Mfc1(AT, b);
9233 }
9234
9235 if (is_min) {
9236 // -0.0 prevails over +0.0.
9237 __ Or(TMP, TMP, AT);
9238 } else {
9239 // +0.0 prevails over -0.0.
9240 __ And(TMP, TMP, AT);
9241 }
9242
9243 if (type == DataType::Type::kFloat64) {
9244 __ Mfc1(AT, a);
9245 __ Mtc1(AT, out);
9246 __ MoveToFpuHigh(TMP, out);
9247 } else {
9248 __ Mtc1(TMP, out);
9249 }
9250 __ B(&done);
9251
9252 __ Bind(&compare);
9253
9254 if (type == DataType::Type::kFloat64) {
9255 if (is_min) {
9256 // return (a <= b) ? a : b;
9257 __ ColeD(a, b);
9258 } else {
9259 // return (a >= b) ? a : b;
9260 __ ColeD(b, a); // b <= a
9261 }
9262 } else {
9263 if (is_min) {
9264 // return (a <= b) ? a : b;
9265 __ ColeS(a, b);
9266 } else {
9267 // return (a >= b) ? a : b;
9268 __ ColeS(b, a); // b <= a
9269 }
9270 }
9271
9272 __ Bind(&select);
9273
9274 if (type == DataType::Type::kFloat64) {
9275 __ MovtD(out, a);
9276 __ MovfD(out, b);
9277 } else {
9278 __ MovtS(out, a);
9279 __ MovfS(out, b);
9280 }
9281
9282 __ Bind(&done);
9283 }
9284}
9285
Aart Bik351df3e2018-03-07 11:54:57 -08009286void InstructionCodeGeneratorMIPS::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
9287 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9288 DataType::Type type = minmax->GetResultType();
9289 switch (type) {
9290 case DataType::Type::kInt32:
9291 case DataType::Type::kInt64:
9292 GenerateMinMaxInt(minmax->GetLocations(), is_min, isR6, type);
9293 break;
9294 case DataType::Type::kFloat32:
9295 case DataType::Type::kFloat64:
9296 GenerateMinMaxFP(minmax->GetLocations(), is_min, isR6, type);
9297 break;
9298 default:
9299 LOG(FATAL) << "Unexpected type for HMinMax " << type;
9300 }
9301}
9302
Aart Bik1f8d51b2018-02-15 10:42:37 -08009303void LocationsBuilderMIPS::VisitMin(HMin* min) {
9304 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
9305}
9306
9307void InstructionCodeGeneratorMIPS::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08009308 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009309}
9310
9311void LocationsBuilderMIPS::VisitMax(HMax* max) {
9312 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
9313}
9314
9315void InstructionCodeGeneratorMIPS::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08009316 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009317}
9318
Aart Bik3dad3412018-02-28 12:01:46 -08009319void LocationsBuilderMIPS::VisitAbs(HAbs* abs) {
9320 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
9321 switch (abs->GetResultType()) {
9322 case DataType::Type::kInt32:
9323 case DataType::Type::kInt64:
9324 locations->SetInAt(0, Location::RequiresRegister());
9325 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9326 break;
9327 case DataType::Type::kFloat32:
9328 case DataType::Type::kFloat64:
9329 locations->SetInAt(0, Location::RequiresFpuRegister());
9330 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9331 break;
9332 default:
9333 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9334 }
9335}
9336
9337void InstructionCodeGeneratorMIPS::GenerateAbsFP(LocationSummary* locations,
9338 DataType::Type type,
9339 bool isR2OrNewer,
9340 bool isR6) {
9341 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
9342 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9343
9344 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
9345 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
9346 // (signaling NaN may become quiet though).
9347 //
9348 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
9349 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
9350 // affected by this instruction.
9351 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
9352 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
9353 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
9354 if (isR6) {
9355 if (type == DataType::Type::kFloat64) {
9356 __ AbsD(out, in);
9357 } else {
9358 DCHECK_EQ(type, DataType::Type::kFloat32);
9359 __ AbsS(out, in);
9360 }
9361 } else {
9362 if (type == DataType::Type::kFloat64) {
9363 if (in != out) {
9364 __ MovD(out, in);
9365 }
9366 __ MoveFromFpuHigh(TMP, in);
9367 // ins instruction is not available for R1.
9368 if (isR2OrNewer) {
9369 __ Ins(TMP, ZERO, 31, 1);
9370 } else {
9371 __ Sll(TMP, TMP, 1);
9372 __ Srl(TMP, TMP, 1);
9373 }
9374 __ MoveToFpuHigh(TMP, out);
9375 } else {
9376 DCHECK_EQ(type, DataType::Type::kFloat32);
9377 __ Mfc1(TMP, in);
9378 // ins instruction is not available for R1.
9379 if (isR2OrNewer) {
9380 __ Ins(TMP, ZERO, 31, 1);
9381 } else {
9382 __ Sll(TMP, TMP, 1);
9383 __ Srl(TMP, TMP, 1);
9384 }
9385 __ Mtc1(TMP, out);
9386 }
9387 }
9388}
9389
9390void InstructionCodeGeneratorMIPS::VisitAbs(HAbs* abs) {
9391 LocationSummary* locations = abs->GetLocations();
9392 bool isR2OrNewer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
9393 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9394 switch (abs->GetResultType()) {
9395 case DataType::Type::kInt32: {
9396 Register in = locations->InAt(0).AsRegister<Register>();
9397 Register out = locations->Out().AsRegister<Register>();
9398 __ Sra(AT, in, 31);
9399 __ Xor(out, in, AT);
9400 __ Subu(out, out, AT);
9401 break;
9402 }
9403 case DataType::Type::kInt64: {
9404 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9405 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9406 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9407 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9408 // The comments in this section show the analogous operations which would
9409 // be performed if we had 64-bit registers "in", and "out".
9410 // __ Dsra32(AT, in, 31);
9411 __ Sra(AT, in_hi, 31);
9412 // __ Xor(out, in, AT);
9413 __ Xor(TMP, in_lo, AT);
9414 __ Xor(out_hi, in_hi, AT);
9415 // __ Dsubu(out, out, AT);
9416 __ Subu(out_lo, TMP, AT);
9417 __ Sltu(TMP, out_lo, TMP);
9418 __ Addu(out_hi, out_hi, TMP);
9419 break;
9420 }
9421 case DataType::Type::kFloat32:
9422 case DataType::Type::kFloat64:
9423 GenerateAbsFP(locations, abs->GetResultType(), isR2OrNewer, isR6);
9424 break;
9425 default:
9426 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9427 }
9428}
9429
Igor Murashkind01745e2017-04-05 16:40:31 -07009430void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
9431 constructor_fence->SetLocations(nullptr);
9432}
9433
9434void InstructionCodeGeneratorMIPS::VisitConstructorFence(
9435 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
9436 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
9437}
9438
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009439void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9440 memory_barrier->SetLocations(nullptr);
9441}
9442
9443void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9444 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
9445}
9446
9447void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009448 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009449 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009450 locations->SetInAt(0, MipsReturnLocation(return_type));
9451}
9452
9453void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
9454 codegen_->GenerateFrameExit();
9455}
9456
9457void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
9458 ret->SetLocations(nullptr);
9459}
9460
9461void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
9462 codegen_->GenerateFrameExit();
9463}
9464
Alexey Frunze92d90602015-12-18 18:16:36 -08009465void LocationsBuilderMIPS::VisitRor(HRor* ror) {
9466 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009467}
9468
Alexey Frunze92d90602015-12-18 18:16:36 -08009469void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
9470 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009471}
9472
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009473void LocationsBuilderMIPS::VisitShl(HShl* shl) {
9474 HandleShift(shl);
9475}
9476
9477void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
9478 HandleShift(shl);
9479}
9480
9481void LocationsBuilderMIPS::VisitShr(HShr* shr) {
9482 HandleShift(shr);
9483}
9484
9485void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
9486 HandleShift(shr);
9487}
9488
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009489void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
9490 HandleBinaryOp(instruction);
9491}
9492
9493void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
9494 HandleBinaryOp(instruction);
9495}
9496
9497void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9498 HandleFieldGet(instruction, instruction->GetFieldInfo());
9499}
9500
9501void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9502 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
9503}
9504
9505void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
9506 HandleFieldSet(instruction, instruction->GetFieldInfo());
9507}
9508
9509void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01009510 HandleFieldSet(instruction,
9511 instruction->GetFieldInfo(),
9512 instruction->GetDexPc(),
9513 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009514}
9515
9516void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
9517 HUnresolvedInstanceFieldGet* instruction) {
9518 FieldAccessCallingConventionMIPS calling_convention;
9519 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9520 instruction->GetFieldType(),
9521 calling_convention);
9522}
9523
9524void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
9525 HUnresolvedInstanceFieldGet* instruction) {
9526 FieldAccessCallingConventionMIPS calling_convention;
9527 codegen_->GenerateUnresolvedFieldAccess(instruction,
9528 instruction->GetFieldType(),
9529 instruction->GetFieldIndex(),
9530 instruction->GetDexPc(),
9531 calling_convention);
9532}
9533
9534void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
9535 HUnresolvedInstanceFieldSet* instruction) {
9536 FieldAccessCallingConventionMIPS calling_convention;
9537 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9538 instruction->GetFieldType(),
9539 calling_convention);
9540}
9541
9542void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
9543 HUnresolvedInstanceFieldSet* instruction) {
9544 FieldAccessCallingConventionMIPS calling_convention;
9545 codegen_->GenerateUnresolvedFieldAccess(instruction,
9546 instruction->GetFieldType(),
9547 instruction->GetFieldIndex(),
9548 instruction->GetDexPc(),
9549 calling_convention);
9550}
9551
9552void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
9553 HUnresolvedStaticFieldGet* instruction) {
9554 FieldAccessCallingConventionMIPS calling_convention;
9555 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9556 instruction->GetFieldType(),
9557 calling_convention);
9558}
9559
9560void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
9561 HUnresolvedStaticFieldGet* instruction) {
9562 FieldAccessCallingConventionMIPS calling_convention;
9563 codegen_->GenerateUnresolvedFieldAccess(instruction,
9564 instruction->GetFieldType(),
9565 instruction->GetFieldIndex(),
9566 instruction->GetDexPc(),
9567 calling_convention);
9568}
9569
9570void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
9571 HUnresolvedStaticFieldSet* instruction) {
9572 FieldAccessCallingConventionMIPS calling_convention;
9573 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9574 instruction->GetFieldType(),
9575 calling_convention);
9576}
9577
9578void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9579 HUnresolvedStaticFieldSet* instruction) {
9580 FieldAccessCallingConventionMIPS calling_convention;
9581 codegen_->GenerateUnresolvedFieldAccess(instruction,
9582 instruction->GetFieldType(),
9583 instruction->GetFieldIndex(),
9584 instruction->GetDexPc(),
9585 calling_convention);
9586}
9587
9588void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009589 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9590 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009591 // In suspend check slow path, usually there are no caller-save registers at all.
9592 // If SIMD instructions are present, however, we force spilling all live SIMD
9593 // registers in full width (since the runtime only saves/restores lower part).
9594 locations->SetCustomSlowPathCallerSaves(
9595 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009596}
9597
9598void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9599 HBasicBlock* block = instruction->GetBlock();
9600 if (block->GetLoopInformation() != nullptr) {
9601 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9602 // The back edge will generate the suspend check.
9603 return;
9604 }
9605 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9606 // The goto will generate the suspend check.
9607 return;
9608 }
9609 GenerateSuspendCheck(instruction, nullptr);
9610}
9611
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009612void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009613 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9614 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009615 InvokeRuntimeCallingConvention calling_convention;
9616 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9617}
9618
9619void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009620 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009621 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9622}
9623
9624void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009625 DataType::Type input_type = conversion->GetInputType();
9626 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009627 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9628 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009629 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009631 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9632 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009633 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9634 }
9635
9636 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009637 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009638 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9639 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009640 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009641 }
9642
Vladimir Markoca6fff82017-10-03 14:49:14 +01009643 LocationSummary* locations =
9644 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009645
9646 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009647 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009648 locations->SetInAt(0, Location::RequiresFpuRegister());
9649 } else {
9650 locations->SetInAt(0, Location::RequiresRegister());
9651 }
9652
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009653 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009654 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9655 } else {
9656 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9657 }
9658 } else {
9659 InvokeRuntimeCallingConvention calling_convention;
9660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009661 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009662 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9663 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009664 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009665 locations->SetInAt(0, Location::RegisterPairLocation(
9666 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9667 }
9668
9669 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9670 }
9671}
9672
9673void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9674 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009675 DataType::Type result_type = conversion->GetResultType();
9676 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009677 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009678 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009679
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009680 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9681 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009683 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009684 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9685 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9686 Register src = locations->InAt(0).AsRegister<Register>();
9687
Alexey Frunzea871ef12016-06-27 15:20:11 -07009688 if (dst_low != src) {
9689 __ Move(dst_low, src);
9690 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009691 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009692 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009693 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009694 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009695 ? locations->InAt(0).AsRegisterPairLow<Register>()
9696 : locations->InAt(0).AsRegister<Register>();
9697
9698 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009699 case DataType::Type::kUint8:
9700 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009701 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009702 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009703 if (has_sign_extension) {
9704 __ Seb(dst, src);
9705 } else {
9706 __ Sll(dst, src, 24);
9707 __ Sra(dst, dst, 24);
9708 }
9709 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009710 case DataType::Type::kUint16:
9711 __ Andi(dst, src, 0xFFFF);
9712 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009713 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009714 if (has_sign_extension) {
9715 __ Seh(dst, src);
9716 } else {
9717 __ Sll(dst, src, 16);
9718 __ Sra(dst, dst, 16);
9719 }
9720 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009721 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009722 if (dst != src) {
9723 __ Move(dst, src);
9724 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009725 break;
9726
9727 default:
9728 LOG(FATAL) << "Unexpected type conversion from " << input_type
9729 << " to " << result_type;
9730 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009731 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9732 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009733 if (isR6) {
9734 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9735 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9736 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9737 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9738 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9739 __ Mtc1(src_low, FTMP);
9740 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009741 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009742 __ Cvtsl(dst, FTMP);
9743 } else {
9744 __ Cvtdl(dst, FTMP);
9745 }
9746 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009747 QuickEntrypointEnum entrypoint =
9748 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009749 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009750 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009751 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9752 } else {
9753 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9754 }
9755 }
9756 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009757 Register src = locations->InAt(0).AsRegister<Register>();
9758 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9759 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009760 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009761 __ Cvtsw(dst, FTMP);
9762 } else {
9763 __ Cvtdw(dst, FTMP);
9764 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009765 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009766 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9767 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009768
9769 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9770 // value of the output type if the input is outside of the range after the truncation or
9771 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9772 // results. This matches the desired float/double-to-int/long conversion exactly.
9773 //
9774 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9775 // value when the input is either a NaN or is outside of the range of the output type
9776 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9777 // the same result.
9778 //
9779 // The code takes care of the different behaviors by first comparing the input to the
9780 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9781 // If the input is greater than or equal to the minimum, it procedes to the truncate
9782 // instruction, which will handle such an input the same way irrespective of NAN2008.
9783 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9784 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009785 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009786 if (isR6) {
9787 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9788 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9789 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9790 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9791 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009793 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009794 __ TruncLS(FTMP, src);
9795 } else {
9796 __ TruncLD(FTMP, src);
9797 }
9798 __ Mfc1(dst_low, FTMP);
9799 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009800 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009801 QuickEntrypointEnum entrypoint =
9802 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009803 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009804 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009805 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9806 } else {
9807 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9808 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009809 }
9810 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009811 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9812 Register dst = locations->Out().AsRegister<Register>();
9813 MipsLabel truncate;
9814 MipsLabel done;
9815
Lena Djokicf4e23a82017-05-09 15:43:45 +02009816 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009817 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009818 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9819 __ LoadConst32(TMP, min_val);
9820 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009821 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009822 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9823 __ LoadConst32(TMP, High32Bits(min_val));
9824 __ Mtc1(ZERO, FTMP);
9825 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009826 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009827
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009828 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009829 __ ColeS(0, FTMP, src);
9830 } else {
9831 __ ColeD(0, FTMP, src);
9832 }
9833 __ Bc1t(0, &truncate);
9834
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009835 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009836 __ CeqS(0, src, src);
9837 } else {
9838 __ CeqD(0, src, src);
9839 }
9840 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9841 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009842
9843 __ B(&done);
9844
9845 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009846 }
9847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009848 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009849 __ TruncWS(FTMP, src);
9850 } else {
9851 __ TruncWD(FTMP, src);
9852 }
9853 __ Mfc1(dst, FTMP);
9854
Lena Djokicf4e23a82017-05-09 15:43:45 +02009855 if (!isR6) {
9856 __ Bind(&done);
9857 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009858 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009859 } else if (DataType::IsFloatingPointType(result_type) &&
9860 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009861 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9862 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009863 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009864 __ Cvtsd(dst, src);
9865 } else {
9866 __ Cvtds(dst, src);
9867 }
9868 } else {
9869 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9870 << " to " << result_type;
9871 }
9872}
9873
9874void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9875 HandleShift(ushr);
9876}
9877
9878void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9879 HandleShift(ushr);
9880}
9881
9882void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9883 HandleBinaryOp(instruction);
9884}
9885
9886void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9887 HandleBinaryOp(instruction);
9888}
9889
9890void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9891 // Nothing to do, this should be removed during prepare for register allocator.
9892 LOG(FATAL) << "Unreachable";
9893}
9894
9895void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9896 // Nothing to do, this should be removed during prepare for register allocator.
9897 LOG(FATAL) << "Unreachable";
9898}
9899
9900void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009901 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009902}
9903
9904void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009905 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009906}
9907
9908void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009909 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009910}
9911
9912void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009913 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009914}
9915
9916void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009917 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009918}
9919
9920void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009921 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009922}
9923
9924void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009925 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009926}
9927
9928void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009929 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009930}
9931
9932void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009933 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009934}
9935
9936void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009937 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009938}
9939
9940void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009941 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009942}
9943
9944void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009945 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009946}
9947
9948void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009949 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009950}
9951
9952void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009953 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009954}
9955
9956void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009957 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009958}
9959
9960void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009961 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009962}
9963
9964void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009965 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009966}
9967
9968void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009969 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009970}
9971
9972void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009973 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009974}
9975
9976void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009977 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009978}
9979
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009980void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9981 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009982 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009983 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009984 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9985 uint32_t num_entries = switch_instr->GetNumEntries();
9986 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9987 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9988 // instruction to simulate PC-relative addressing when accessing the jump table.
9989 // NAL clobbers RA. Make sure RA is preserved.
9990 codegen_->ClobberRA();
9991 }
9992 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009993}
9994
Alexey Frunze96b66822016-09-10 02:32:44 -07009995void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9996 int32_t lower_bound,
9997 uint32_t num_entries,
9998 HBasicBlock* switch_block,
9999 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010000 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010001 Register temp_reg = TMP;
10002 __ Addiu32(temp_reg, value_reg, -lower_bound);
10003 // Jump to default if index is negative
10004 // Note: We don't check the case that index is positive while value < lower_bound, because in
10005 // this case, index >= num_entries must be true. So that we can save one branch instruction.
10006 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
10007
Alexey Frunze96b66822016-09-10 02:32:44 -070010008 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010009 // Jump to successors[0] if value == lower_bound.
10010 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
10011 int32_t last_index = 0;
10012 for (; num_entries - last_index > 2; last_index += 2) {
10013 __ Addiu(temp_reg, temp_reg, -2);
10014 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
10015 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
10016 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
10017 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
10018 }
10019 if (num_entries - last_index == 2) {
10020 // The last missing case_value.
10021 __ Addiu(temp_reg, temp_reg, -1);
10022 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010023 }
10024
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010025 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -070010026 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010027 __ B(codegen_->GetLabelOf(default_block));
10028 }
10029}
10030
Alexey Frunze96b66822016-09-10 02:32:44 -070010031void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
10032 Register constant_area,
10033 int32_t lower_bound,
10034 uint32_t num_entries,
10035 HBasicBlock* switch_block,
10036 HBasicBlock* default_block) {
10037 // Create a jump table.
10038 std::vector<MipsLabel*> labels(num_entries);
10039 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
10040 for (uint32_t i = 0; i < num_entries; i++) {
10041 labels[i] = codegen_->GetLabelOf(successors[i]);
10042 }
10043 JumpTable* table = __ CreateJumpTable(std::move(labels));
10044
10045 // Is the value in range?
10046 __ Addiu32(TMP, value_reg, -lower_bound);
10047 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
10048 __ Sltiu(AT, TMP, num_entries);
10049 __ Beqz(AT, codegen_->GetLabelOf(default_block));
10050 } else {
10051 __ LoadConst32(AT, num_entries);
10052 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
10053 }
10054
10055 // We are in the range of the table.
10056 // Load the target address from the jump table, indexing by the value.
10057 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -070010058 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -070010059 __ Lw(TMP, TMP, 0);
10060 // Compute the absolute target address by adding the table start address
10061 // (the table contains offsets to targets relative to its start).
10062 __ Addu(TMP, TMP, AT);
10063 // And jump.
10064 __ Jr(TMP);
10065 __ NopIfNoReordering();
10066}
10067
10068void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
10069 int32_t lower_bound = switch_instr->GetStartValue();
10070 uint32_t num_entries = switch_instr->GetNumEntries();
10071 LocationSummary* locations = switch_instr->GetLocations();
10072 Register value_reg = locations->InAt(0).AsRegister<Register>();
10073 HBasicBlock* switch_block = switch_instr->GetBlock();
10074 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10075
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010076 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -070010077 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010078 //
10079 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
10080 // to access the jump table and it is implemented by changing HPackedSwitch to
10081 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
10082 // VisitMipsPackedSwitch()).
10083 //
10084 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
10085 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
10086 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -070010087 GenTableBasedPackedSwitch(value_reg,
10088 ZERO,
10089 lower_bound,
10090 num_entries,
10091 switch_block,
10092 default_block);
10093 } else {
10094 GenPackedSwitchWithCompares(value_reg,
10095 lower_bound,
10096 num_entries,
10097 switch_block,
10098 default_block);
10099 }
10100}
10101
10102void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10103 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010104 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -070010105 locations->SetInAt(0, Location::RequiresRegister());
10106 // Constant area pointer (HMipsComputeBaseMethodAddress).
10107 locations->SetInAt(1, Location::RequiresRegister());
10108}
10109
10110void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10111 int32_t lower_bound = switch_instr->GetStartValue();
10112 uint32_t num_entries = switch_instr->GetNumEntries();
10113 LocationSummary* locations = switch_instr->GetLocations();
10114 Register value_reg = locations->InAt(0).AsRegister<Register>();
10115 Register constant_area = locations->InAt(1).AsRegister<Register>();
10116 HBasicBlock* switch_block = switch_instr->GetBlock();
10117 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10118
10119 // This is an R2-only path. HPackedSwitch has been changed to
10120 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
10121 // required to address the jump table relative to PC.
10122 GenTableBasedPackedSwitch(value_reg,
10123 constant_area,
10124 lower_bound,
10125 num_entries,
10126 switch_block,
10127 default_block);
10128}
10129
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010130void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
10131 HMipsComputeBaseMethodAddress* insn) {
10132 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010133 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010134 locations->SetOut(Location::RequiresRegister());
10135}
10136
10137void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
10138 HMipsComputeBaseMethodAddress* insn) {
10139 LocationSummary* locations = insn->GetLocations();
10140 Register reg = locations->Out().AsRegister<Register>();
10141
10142 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
10143
10144 // Generate a dummy PC-relative call to obtain PC.
10145 __ Nal();
10146 // Grab the return address off RA.
10147 __ Move(reg, RA);
10148
10149 // Remember this offset (the obtained PC value) for later use with constant area.
10150 __ BindPcRelBaseLabel();
10151}
10152
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010153void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10154 // The trampoline uses the same calling convention as dex calling conventions,
10155 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
10156 // the method_idx.
10157 HandleInvoke(invoke);
10158}
10159
10160void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10161 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
10162}
10163
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010164void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10165 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010166 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010167 locations->SetInAt(0, Location::RequiresRegister());
10168 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010169}
10170
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010171void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10172 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +000010173 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010174 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010175 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010176 __ LoadFromOffset(kLoadWord,
10177 locations->Out().AsRegister<Register>(),
10178 locations->InAt(0).AsRegister<Register>(),
10179 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010180 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010181 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +000010182 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000010183 __ LoadFromOffset(kLoadWord,
10184 locations->Out().AsRegister<Register>(),
10185 locations->InAt(0).AsRegister<Register>(),
10186 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010187 __ LoadFromOffset(kLoadWord,
10188 locations->Out().AsRegister<Register>(),
10189 locations->Out().AsRegister<Register>(),
10190 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010191 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010192}
10193
xueliang.zhonge0eb4832017-10-30 13:43:14 +000010194void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10195 ATTRIBUTE_UNUSED) {
10196 LOG(FATAL) << "Unreachable";
10197}
10198
10199void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10200 ATTRIBUTE_UNUSED) {
10201 LOG(FATAL) << "Unreachable";
10202}
10203
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010204#undef __
10205#undef QUICK_ENTRY_POINT
10206
10207} // namespace mips
10208} // namespace art