blob: 0ed5756b532c7e52835826980cf30d074ebf12f1 [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
Vladimir Marko3232dbb2018-07-25 15:42:46 +0100163static RegisterSet OneRegInReferenceOutSaveEverythingCallerSaves() {
164 InvokeRuntimeCallingConvention calling_convention;
165 RegisterSet caller_saves = RegisterSet::Empty();
166 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
167 // The reference is returned in the same register. This differs from the standard return location.
168 return caller_saves;
169}
170
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100171// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
172#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700173#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200174
175class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
176 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000177 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200178
179 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
180 LocationSummary* locations = instruction_->GetLocations();
181 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
182 __ Bind(GetEntryLabel());
183 if (instruction_->CanThrowIntoCatchBlock()) {
184 // Live registers will be restored in the catch block if caught.
185 SaveLiveRegisters(codegen, instruction_->GetLocations());
186 }
187 // We're moving two locations to locations that could overlap, so we need a parallel
188 // move resolver.
189 InvokeRuntimeCallingConvention calling_convention;
190 codegen->EmitParallelMoves(locations->InAt(0),
191 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100192 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193 locations->InAt(1),
194 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100195 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100196 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
197 ? kQuickThrowStringBounds
198 : kQuickThrowArrayBounds;
199 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100200 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200201 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
202 }
203
204 bool IsFatal() const OVERRIDE { return true; }
205
206 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
207
208 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200209 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
210};
211
212class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
213 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000214 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200215
216 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
217 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
218 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100219 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200220 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
221 }
222
223 bool IsFatal() const OVERRIDE { return true; }
224
225 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
226
227 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200228 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
229};
230
231class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
232 public:
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100233 LoadClassSlowPathMIPS(HLoadClass* cls, HInstruction* at)
234 : SlowPathCodeMIPS(at), cls_(cls) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200235 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100236 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200237 }
238
239 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000240 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700241 Location out = locations->Out();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100242 const uint32_t dex_pc = instruction_->GetDexPc();
243 bool must_resolve_type = instruction_->IsLoadClass() && cls_->MustResolveTypeOnSlowPath();
244 bool must_do_clinit = instruction_->IsClinitCheck() || cls_->MustGenerateClinitCheck();
245
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200246 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200247 __ Bind(GetEntryLabel());
248 SaveLiveRegisters(codegen, locations);
249
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100250 InvokeRuntimeCallingConvention calling_convention;
251 if (must_resolve_type) {
252 DCHECK(IsSameDexFile(cls_->GetDexFile(), mips_codegen->GetGraph()->GetDexFile()));
253 dex::TypeIndex type_index = cls_->GetTypeIndex();
254 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Vladimir Marko9d479252018-07-24 11:35:20 +0100255 mips_codegen->InvokeRuntime(kQuickResolveType, instruction_, dex_pc, this);
256 CheckEntrypointTypes<kQuickResolveType, void*, uint32_t>();
Vladimir Markoa9f303c2018-07-20 16:43:56 +0100257 // If we also must_do_clinit, the resolved type is now in the correct register.
258 } else {
259 DCHECK(must_do_clinit);
260 Location source = instruction_->IsLoadClass() ? out : locations->InAt(0);
261 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
262 source,
263 cls_->GetType());
264 }
265 if (must_do_clinit) {
266 mips_codegen->InvokeRuntime(kQuickInitializeStaticStorage, instruction_, dex_pc, this);
267 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200268 }
269
270 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200271 if (out.IsValid()) {
272 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100273 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700274 mips_codegen->MoveLocation(out,
275 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
276 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200277 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200278 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700279
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200280 __ B(GetExitLabel());
281 }
282
283 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
284
285 private:
286 // The class this slow path will load.
287 HLoadClass* const cls_;
288
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200289 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
290};
291
292class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
293 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000294 explicit LoadStringSlowPathMIPS(HLoadString* instruction)
295 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200296
297 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700298 DCHECK(instruction_->IsLoadString());
299 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200300 LocationSummary* locations = instruction_->GetLocations();
301 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000302 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200303 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700304 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200305 __ Bind(GetEntryLabel());
306 SaveLiveRegisters(codegen, locations);
307
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000308 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100309 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200310 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100312 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200313 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700314 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200315 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000317
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200318 __ B(GetExitLabel());
319 }
320
321 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
322
323 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200324 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
325};
326
327class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
328 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000329 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330
331 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
332 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
333 __ Bind(GetEntryLabel());
334 if (instruction_->CanThrowIntoCatchBlock()) {
335 // Live registers will be restored in the catch block if caught.
336 SaveLiveRegisters(codegen, instruction_->GetLocations());
337 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100338 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200339 instruction_,
340 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100341 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200342 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
343 }
344
345 bool IsFatal() const OVERRIDE { return true; }
346
347 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
348
349 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200350 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
351};
352
353class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
354 public:
355 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000356 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200357
358 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200359 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200360 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
361 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200362 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100363 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200364 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200365 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200366 if (successor_ == nullptr) {
367 __ B(GetReturnLabel());
368 } else {
369 __ B(mips_codegen->GetLabelOf(successor_));
370 }
371 }
372
373 MipsLabel* GetReturnLabel() {
374 DCHECK(successor_ == nullptr);
375 return &return_label_;
376 }
377
378 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
379
Chris Larsena2045912017-11-02 12:39:54 -0700380 HBasicBlock* GetSuccessor() const {
381 return successor_;
382 }
383
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200384 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200385 // If not null, the block to branch to after the suspend check.
386 HBasicBlock* const successor_;
387
388 // If `successor_` is null, the label to branch to after the suspend check.
389 MipsLabel return_label_;
390
391 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
392};
393
394class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
395 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800396 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
397 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200398
399 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
400 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200401 uint32_t dex_pc = instruction_->GetDexPc();
402 DCHECK(instruction_->IsCheckCast()
403 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
404 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
405
406 __ Bind(GetEntryLabel());
Alexey Frunzedfc30af2018-01-24 16:25:10 -0800407 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800408 SaveLiveRegisters(codegen, locations);
409 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410
411 // We're moving two locations to locations that could overlap, so we need a parallel
412 // move resolver.
413 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800414 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200415 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100416 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800417 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200418 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100419 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100421 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800422 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100423 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200424 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
425 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200426 } else {
427 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800428 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
429 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200430 }
431
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800432 if (!is_fatal_) {
433 RestoreLiveRegisters(codegen, locations);
434 __ B(GetExitLabel());
435 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200436 }
437
438 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
439
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800440 bool IsFatal() const OVERRIDE { return is_fatal_; }
441
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200442 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800443 const bool is_fatal_;
444
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200445 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
446};
447
448class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
449 public:
Aart Bik42249c32016-01-07 15:33:50 -0800450 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000451 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200452
453 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800454 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100456 LocationSummary* locations = instruction_->GetLocations();
457 SaveLiveRegisters(codegen, locations);
458 InvokeRuntimeCallingConvention calling_convention;
459 __ LoadConst32(calling_convention.GetRegisterAt(0),
460 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100461 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100462 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200463 }
464
465 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
466
467 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200468 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
469};
470
Alexey Frunze15958152017-02-09 19:08:30 -0800471class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
472 public:
473 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
474
475 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
476 LocationSummary* locations = instruction_->GetLocations();
477 __ Bind(GetEntryLabel());
478 SaveLiveRegisters(codegen, locations);
479
480 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100481 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800482 parallel_move.AddMove(
483 locations->InAt(0),
484 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100485 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800486 nullptr);
487 parallel_move.AddMove(
488 locations->InAt(1),
489 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100490 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800491 nullptr);
492 parallel_move.AddMove(
493 locations->InAt(2),
494 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100495 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800496 nullptr);
497 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
498
499 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
500 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
501 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
502 RestoreLiveRegisters(codegen, locations);
503 __ B(GetExitLabel());
504 }
505
506 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
507
508 private:
509 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
510};
511
512// Slow path marking an object reference `ref` during a read
513// barrier. The field `obj.field` in the object `obj` holding this
514// reference does not get updated by this slow path after marking (see
515// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
516//
517// This means that after the execution of this slow path, `ref` will
518// always be up-to-date, but `obj.field` may not; i.e., after the
519// flip, `ref` will be a to-space reference, but `obj.field` will
520// probably still be a from-space reference (unless it gets updated by
521// another thread, or if another thread installed another object
522// reference (different from `ref`) in `obj.field`).
523//
524// If `entrypoint` is a valid location it is assumed to already be
525// holding the entrypoint. The case where the entrypoint is passed in
526// is for the GcRoot read barrier.
527class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
528 public:
529 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
530 Location ref,
531 Location entrypoint = Location::NoLocation())
532 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
533 DCHECK(kEmitCompilerReadBarrier);
534 }
535
536 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
537
538 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
539 LocationSummary* locations = instruction_->GetLocations();
540 Register ref_reg = ref_.AsRegister<Register>();
541 DCHECK(locations->CanCall());
542 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
543 DCHECK(instruction_->IsInstanceFieldGet() ||
544 instruction_->IsStaticFieldGet() ||
545 instruction_->IsArrayGet() ||
546 instruction_->IsArraySet() ||
547 instruction_->IsLoadClass() ||
548 instruction_->IsLoadString() ||
549 instruction_->IsInstanceOf() ||
550 instruction_->IsCheckCast() ||
551 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
552 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
553 << "Unexpected instruction in read barrier marking slow path: "
554 << instruction_->DebugName();
555
556 __ Bind(GetEntryLabel());
557 // No need to save live registers; it's taken care of by the
558 // entrypoint. Also, there is no need to update the stack mask,
559 // as this runtime call will not trigger a garbage collection.
560 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
561 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
562 (S2 <= ref_reg && ref_reg <= S7) ||
563 (ref_reg == FP)) << ref_reg;
564 // "Compact" slow path, saving two moves.
565 //
566 // Instead of using the standard runtime calling convention (input
567 // and output in A0 and V0 respectively):
568 //
569 // A0 <- ref
570 // V0 <- ReadBarrierMark(A0)
571 // ref <- V0
572 //
573 // we just use rX (the register containing `ref`) as input and output
574 // of a dedicated entrypoint:
575 //
576 // rX <- ReadBarrierMarkRegX(rX)
577 //
578 if (entrypoint_.IsValid()) {
579 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
580 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
581 __ Jalr(entrypoint_.AsRegister<Register>());
582 __ NopIfNoReordering();
583 } else {
584 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100585 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800586 // This runtime call does not require a stack map.
587 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
588 instruction_,
589 this,
590 /* direct */ false);
591 }
592 __ B(GetExitLabel());
593 }
594
595 private:
596 // The location (register) of the marked object reference.
597 const Location ref_;
598
599 // The location of the entrypoint if already loaded.
600 const Location entrypoint_;
601
602 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
603};
604
605// Slow path marking an object reference `ref` during a read barrier,
606// and if needed, atomically updating the field `obj.field` in the
607// object `obj` holding this reference after marking (contrary to
608// ReadBarrierMarkSlowPathMIPS above, which never tries to update
609// `obj.field`).
610//
611// This means that after the execution of this slow path, both `ref`
612// and `obj.field` will be up-to-date; i.e., after the flip, both will
613// hold the same to-space reference (unless another thread installed
614// another object reference (different from `ref`) in `obj.field`).
615class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
616 public:
617 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
618 Location ref,
619 Register obj,
620 Location field_offset,
621 Register temp1)
622 : SlowPathCodeMIPS(instruction),
623 ref_(ref),
624 obj_(obj),
625 field_offset_(field_offset),
626 temp1_(temp1) {
627 DCHECK(kEmitCompilerReadBarrier);
628 }
629
630 const char* GetDescription() const OVERRIDE {
631 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
632 }
633
634 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
635 LocationSummary* locations = instruction_->GetLocations();
636 Register ref_reg = ref_.AsRegister<Register>();
637 DCHECK(locations->CanCall());
638 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
639 // This slow path is only used by the UnsafeCASObject intrinsic.
640 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
641 << "Unexpected instruction in read barrier marking and field updating slow path: "
642 << instruction_->DebugName();
643 DCHECK(instruction_->GetLocations()->Intrinsified());
644 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
645 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
646
647 __ Bind(GetEntryLabel());
648
649 // Save the old reference.
650 // Note that we cannot use AT or TMP to save the old reference, as those
651 // are used by the code that follows, but we need the old reference after
652 // the call to the ReadBarrierMarkRegX entry point.
653 DCHECK_NE(temp1_, AT);
654 DCHECK_NE(temp1_, TMP);
655 __ Move(temp1_, ref_reg);
656
657 // No need to save live registers; it's taken care of by the
658 // entrypoint. Also, there is no need to update the stack mask,
659 // as this runtime call will not trigger a garbage collection.
660 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
661 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
662 (S2 <= ref_reg && ref_reg <= S7) ||
663 (ref_reg == FP)) << ref_reg;
664 // "Compact" slow path, saving two moves.
665 //
666 // Instead of using the standard runtime calling convention (input
667 // and output in A0 and V0 respectively):
668 //
669 // A0 <- ref
670 // V0 <- ReadBarrierMark(A0)
671 // ref <- V0
672 //
673 // we just use rX (the register containing `ref`) as input and output
674 // of a dedicated entrypoint:
675 //
676 // rX <- ReadBarrierMarkRegX(rX)
677 //
678 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100679 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800680 // This runtime call does not require a stack map.
681 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
682 instruction_,
683 this,
684 /* direct */ false);
685
686 // If the new reference is different from the old reference,
687 // update the field in the holder (`*(obj_ + field_offset_)`).
688 //
689 // Note that this field could also hold a different object, if
690 // another thread had concurrently changed it. In that case, the
691 // the compare-and-set (CAS) loop below would abort, leaving the
692 // field as-is.
693 MipsLabel done;
694 __ Beq(temp1_, ref_reg, &done);
695
696 // Update the the holder's field atomically. This may fail if
697 // mutator updates before us, but it's OK. This is achieved
698 // using a strong compare-and-set (CAS) operation with relaxed
699 // memory synchronization ordering, where the expected value is
700 // the old reference and the desired value is the new reference.
701
702 // Convenience aliases.
703 Register base = obj_;
704 // The UnsafeCASObject intrinsic uses a register pair as field
705 // offset ("long offset"), of which only the low part contains
706 // data.
707 Register offset = field_offset_.AsRegisterPairLow<Register>();
708 Register expected = temp1_;
709 Register value = ref_reg;
710 Register tmp_ptr = TMP; // Pointer to actual memory.
711 Register tmp = AT; // Value in memory.
712
713 __ Addu(tmp_ptr, base, offset);
714
715 if (kPoisonHeapReferences) {
716 __ PoisonHeapReference(expected);
717 // Do not poison `value` if it is the same register as
718 // `expected`, which has just been poisoned.
719 if (value != expected) {
720 __ PoisonHeapReference(value);
721 }
722 }
723
724 // do {
725 // tmp = [r_ptr] - expected;
726 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
727
728 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
729 MipsLabel loop_head, exit_loop;
730 __ Bind(&loop_head);
731 if (is_r6) {
732 __ LlR6(tmp, tmp_ptr);
733 } else {
734 __ LlR2(tmp, tmp_ptr);
735 }
736 __ Bne(tmp, expected, &exit_loop);
737 __ Move(tmp, value);
738 if (is_r6) {
739 __ ScR6(tmp, tmp_ptr);
740 } else {
741 __ ScR2(tmp, tmp_ptr);
742 }
743 __ Beqz(tmp, &loop_head);
744 __ Bind(&exit_loop);
745
746 if (kPoisonHeapReferences) {
747 __ UnpoisonHeapReference(expected);
748 // Do not unpoison `value` if it is the same register as
749 // `expected`, which has just been unpoisoned.
750 if (value != expected) {
751 __ UnpoisonHeapReference(value);
752 }
753 }
754
755 __ Bind(&done);
756 __ B(GetExitLabel());
757 }
758
759 private:
760 // The location (register) of the marked object reference.
761 const Location ref_;
762 // The register containing the object holding the marked object reference field.
763 const Register obj_;
764 // The location of the offset of the marked reference field within `obj_`.
765 Location field_offset_;
766
767 const Register temp1_;
768
769 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
770};
771
772// Slow path generating a read barrier for a heap reference.
773class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
774 public:
775 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
776 Location out,
777 Location ref,
778 Location obj,
779 uint32_t offset,
780 Location index)
781 : SlowPathCodeMIPS(instruction),
782 out_(out),
783 ref_(ref),
784 obj_(obj),
785 offset_(offset),
786 index_(index) {
787 DCHECK(kEmitCompilerReadBarrier);
788 // If `obj` is equal to `out` or `ref`, it means the initial object
789 // has been overwritten by (or after) the heap object reference load
790 // to be instrumented, e.g.:
791 //
792 // __ LoadFromOffset(kLoadWord, out, out, offset);
793 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
794 //
795 // In that case, we have lost the information about the original
796 // object, and the emitted read barrier cannot work properly.
797 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
798 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
799 }
800
801 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
802 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
803 LocationSummary* locations = instruction_->GetLocations();
804 Register reg_out = out_.AsRegister<Register>();
805 DCHECK(locations->CanCall());
806 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
807 DCHECK(instruction_->IsInstanceFieldGet() ||
808 instruction_->IsStaticFieldGet() ||
809 instruction_->IsArrayGet() ||
810 instruction_->IsInstanceOf() ||
811 instruction_->IsCheckCast() ||
812 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
813 << "Unexpected instruction in read barrier for heap reference slow path: "
814 << instruction_->DebugName();
815
816 __ Bind(GetEntryLabel());
817 SaveLiveRegisters(codegen, locations);
818
819 // We may have to change the index's value, but as `index_` is a
820 // constant member (like other "inputs" of this slow path),
821 // introduce a copy of it, `index`.
822 Location index = index_;
823 if (index_.IsValid()) {
824 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
825 if (instruction_->IsArrayGet()) {
826 // Compute the actual memory offset and store it in `index`.
827 Register index_reg = index_.AsRegister<Register>();
828 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
829 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
830 // We are about to change the value of `index_reg` (see the
831 // calls to art::mips::MipsAssembler::Sll and
832 // art::mips::MipsAssembler::Addiu32 below), but it has
833 // not been saved by the previous call to
834 // art::SlowPathCode::SaveLiveRegisters, as it is a
835 // callee-save register --
836 // art::SlowPathCode::SaveLiveRegisters does not consider
837 // callee-save registers, as it has been designed with the
838 // assumption that callee-save registers are supposed to be
839 // handled by the called function. So, as a callee-save
840 // register, `index_reg` _would_ eventually be saved onto
841 // the stack, but it would be too late: we would have
842 // changed its value earlier. Therefore, we manually save
843 // it here into another freely available register,
844 // `free_reg`, chosen of course among the caller-save
845 // registers (as a callee-save `free_reg` register would
846 // exhibit the same problem).
847 //
848 // Note we could have requested a temporary register from
849 // the register allocator instead; but we prefer not to, as
850 // this is a slow path, and we know we can find a
851 // caller-save register that is available.
852 Register free_reg = FindAvailableCallerSaveRegister(codegen);
853 __ Move(free_reg, index_reg);
854 index_reg = free_reg;
855 index = Location::RegisterLocation(index_reg);
856 } else {
857 // The initial register stored in `index_` has already been
858 // saved in the call to art::SlowPathCode::SaveLiveRegisters
859 // (as it is not a callee-save register), so we can freely
860 // use it.
861 }
862 // Shifting the index value contained in `index_reg` by the scale
863 // factor (2) cannot overflow in practice, as the runtime is
864 // unable to allocate object arrays with a size larger than
865 // 2^26 - 1 (that is, 2^28 - 4 bytes).
866 __ Sll(index_reg, index_reg, TIMES_4);
867 static_assert(
868 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
869 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
870 __ Addiu32(index_reg, index_reg, offset_);
871 } else {
872 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
873 // intrinsics, `index_` is not shifted by a scale factor of 2
874 // (as in the case of ArrayGet), as it is actually an offset
875 // to an object field within an object.
876 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
877 DCHECK(instruction_->GetLocations()->Intrinsified());
878 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
879 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
880 << instruction_->AsInvoke()->GetIntrinsic();
881 DCHECK_EQ(offset_, 0U);
882 DCHECK(index_.IsRegisterPair());
883 // UnsafeGet's offset location is a register pair, the low
884 // part contains the correct offset.
885 index = index_.ToLow();
886 }
887 }
888
889 // We're moving two or three locations to locations that could
890 // overlap, so we need a parallel move resolver.
891 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100892 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800893 parallel_move.AddMove(ref_,
894 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100895 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800896 nullptr);
897 parallel_move.AddMove(obj_,
898 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100899 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800900 nullptr);
901 if (index.IsValid()) {
902 parallel_move.AddMove(index,
903 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100904 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800905 nullptr);
906 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
907 } else {
908 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
909 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
910 }
911 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
912 instruction_,
913 instruction_->GetDexPc(),
914 this);
915 CheckEntrypointTypes<
916 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200917 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100918 calling_convention.GetReturnLocation(DataType::Type::kReference),
919 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800920
921 RestoreLiveRegisters(codegen, locations);
922 __ B(GetExitLabel());
923 }
924
925 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
926
927 private:
928 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
929 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
930 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
931 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
932 if (i != ref &&
933 i != obj &&
934 !codegen->IsCoreCalleeSaveRegister(i) &&
935 !codegen->IsBlockedCoreRegister(i)) {
936 return static_cast<Register>(i);
937 }
938 }
939 // We shall never fail to find a free caller-save register, as
940 // there are more than two core caller-save registers on MIPS
941 // (meaning it is possible to find one which is different from
942 // `ref` and `obj`).
943 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
944 LOG(FATAL) << "Could not find a free caller-save register";
945 UNREACHABLE();
946 }
947
948 const Location out_;
949 const Location ref_;
950 const Location obj_;
951 const uint32_t offset_;
952 // An additional location containing an index to an array.
953 // Only used for HArrayGet and the UnsafeGetObject &
954 // UnsafeGetObjectVolatile intrinsics.
955 const Location index_;
956
957 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
958};
959
960// Slow path generating a read barrier for a GC root.
961class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
962 public:
963 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
964 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
965 DCHECK(kEmitCompilerReadBarrier);
966 }
967
968 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
969 LocationSummary* locations = instruction_->GetLocations();
970 Register reg_out = out_.AsRegister<Register>();
971 DCHECK(locations->CanCall());
972 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
973 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
974 << "Unexpected instruction in read barrier for GC root slow path: "
975 << instruction_->DebugName();
976
977 __ Bind(GetEntryLabel());
978 SaveLiveRegisters(codegen, locations);
979
980 InvokeRuntimeCallingConvention calling_convention;
981 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +0200982 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
983 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100984 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800985 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
986 instruction_,
987 instruction_->GetDexPc(),
988 this);
989 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +0200990 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100991 calling_convention.GetReturnLocation(DataType::Type::kReference),
992 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800993
994 RestoreLiveRegisters(codegen, locations);
995 __ B(GetExitLabel());
996 }
997
998 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
999
1000 private:
1001 const Location out_;
1002 const Location root_;
1003
1004 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1005};
1006
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001007CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001008 const CompilerOptions& compiler_options,
1009 OptimizingCompilerStats* stats)
1010 : CodeGenerator(graph,
1011 kNumberOfCoreRegisters,
1012 kNumberOfFRegisters,
1013 kNumberOfRegisterPairs,
1014 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1015 arraysize(kCoreCalleeSaves)),
1016 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1017 arraysize(kFpuCalleeSaves)),
1018 compiler_options,
1019 stats),
1020 block_labels_(nullptr),
1021 location_builder_(graph, this),
1022 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001023 move_resolver_(graph->GetAllocator(), this),
Vladimir Markoa0431112018-06-25 09:32:54 +01001024 assembler_(graph->GetAllocator(),
1025 compiler_options.GetInstructionSetFeatures()->AsMipsInstructionSetFeatures()),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001026 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001027 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001028 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001029 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001030 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001031 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001032 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001033 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001034 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001035 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1036 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001037 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001038 // Save RA (containing the return address) to mimic Quick.
1039 AddAllocatedRegister(Location::RegisterLocation(RA));
1040}
1041
1042#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001043// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1044#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001045#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001046
1047void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1048 // Ensure that we fix up branches.
1049 __ FinalizeCode();
1050
1051 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001052 StackMapStream* stack_map_stream = GetStackMapStream();
1053 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
David Srbeckyd02b23f2018-05-29 23:27:22 +01001054 uint32_t old_position = stack_map_stream->GetStackMapNativePcOffset(i);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001055 uint32_t new_position = __ GetAdjustedPosition(old_position);
1056 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001057 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001058 }
1059
1060 // Adjust pc offsets for the disassembly information.
1061 if (disasm_info_ != nullptr) {
1062 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1063 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1064 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1065 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1066 it.second.start = __ GetAdjustedPosition(it.second.start);
1067 it.second.end = __ GetAdjustedPosition(it.second.end);
1068 }
1069 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1070 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1071 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1072 }
1073 }
1074
1075 CodeGenerator::Finalize(allocator);
1076}
1077
1078MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1079 return codegen_->GetAssembler();
1080}
1081
1082void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1083 DCHECK_LT(index, moves_.size());
1084 MoveOperands* move = moves_[index];
1085 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1086}
1087
1088void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1089 DCHECK_LT(index, moves_.size());
1090 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001091 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001092 Location loc1 = move->GetDestination();
1093 Location loc2 = move->GetSource();
1094
1095 DCHECK(!loc1.IsConstant());
1096 DCHECK(!loc2.IsConstant());
1097
1098 if (loc1.Equals(loc2)) {
1099 return;
1100 }
1101
1102 if (loc1.IsRegister() && loc2.IsRegister()) {
1103 // Swap 2 GPRs.
1104 Register r1 = loc1.AsRegister<Register>();
1105 Register r2 = loc2.AsRegister<Register>();
1106 __ Move(TMP, r2);
1107 __ Move(r2, r1);
1108 __ Move(r1, TMP);
1109 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001110 if (codegen_->GetGraph()->HasSIMD()) {
1111 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1112 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1113 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001114 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001115 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1116 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1117 if (type == DataType::Type::kFloat32) {
1118 __ MovS(FTMP, f2);
1119 __ MovS(f2, f1);
1120 __ MovS(f1, FTMP);
1121 } else {
1122 DCHECK_EQ(type, DataType::Type::kFloat64);
1123 __ MovD(FTMP, f2);
1124 __ MovD(f2, f1);
1125 __ MovD(f1, FTMP);
1126 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001127 }
1128 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1129 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1130 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001131 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001132 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1133 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001134 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001135 __ Move(TMP, r2);
1136 __ Mfc1(r2, f1);
1137 __ Mtc1(TMP, f1);
1138 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1139 // Swap 2 GPR register pairs.
1140 Register r1 = loc1.AsRegisterPairLow<Register>();
1141 Register r2 = loc2.AsRegisterPairLow<Register>();
1142 __ Move(TMP, r2);
1143 __ Move(r2, r1);
1144 __ Move(r1, TMP);
1145 r1 = loc1.AsRegisterPairHigh<Register>();
1146 r2 = loc2.AsRegisterPairHigh<Register>();
1147 __ Move(TMP, r2);
1148 __ Move(r2, r1);
1149 __ Move(r1, TMP);
1150 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1151 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1152 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001153 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001154 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1155 : loc2.AsFpuRegister<FRegister>();
1156 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1157 : loc2.AsRegisterPairLow<Register>();
1158 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1159 : loc2.AsRegisterPairHigh<Register>();
1160 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1161 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1162 // unpredictable and the following mfch1 will fail.
1163 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001164 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001165 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001166 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001167 __ Move(r2_l, TMP);
1168 __ Move(r2_h, AT);
1169 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1170 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1171 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1172 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001173 } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) {
1174 ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
David Brazdilcc0f3112016-01-28 17:14:52 +00001175 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1176 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001177 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1178 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001179 __ Move(TMP, reg);
1180 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1181 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1182 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1183 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1184 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1185 : loc2.AsRegisterPairLow<Register>();
1186 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1187 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001188 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001189 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1190 : loc2.GetHighStackIndex(kMipsWordSize);
1191 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001192 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001193 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001194 __ Move(TMP, reg_h);
1195 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1196 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001197 } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) ||
1198 (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) {
1199 Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2;
1200 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1201 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(fp_loc));
1202 __ LoadQFromOffset(fp_loc.AsFpuRegister<FRegister>(), SP, offset);
1203 __ StoreQToOffset(FTMP, SP, offset);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001204 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1205 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1206 : loc2.AsFpuRegister<FRegister>();
1207 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001208 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001209 __ MovS(FTMP, reg);
1210 __ LoadSFromOffset(reg, SP, offset);
1211 __ StoreSToOffset(FTMP, SP, offset);
1212 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001213 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001214 __ MovD(FTMP, reg);
1215 __ LoadDFromOffset(reg, SP, offset);
1216 __ StoreDToOffset(FTMP, SP, offset);
1217 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001218 } else {
1219 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1220 }
1221}
1222
1223void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1224 __ Pop(static_cast<Register>(reg));
1225}
1226
1227void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1228 __ Push(static_cast<Register>(reg));
1229}
1230
1231void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1232 // Allocate a scratch register other than TMP, if available.
1233 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1234 // automatically unspilled when the scratch scope object is destroyed).
1235 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1236 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001237 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001238 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1239 __ LoadFromOffset(kLoadWord,
1240 Register(ensure_scratch.GetRegister()),
1241 SP,
1242 index1 + stack_offset);
1243 __ LoadFromOffset(kLoadWord,
1244 TMP,
1245 SP,
1246 index2 + stack_offset);
1247 __ StoreToOffset(kStoreWord,
1248 Register(ensure_scratch.GetRegister()),
1249 SP,
1250 index2 + stack_offset);
1251 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1252 }
1253}
1254
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001255void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) {
1256 __ LoadQFromOffset(FTMP, SP, index1);
1257 __ LoadQFromOffset(FTMP2, SP, index2);
1258 __ StoreQToOffset(FTMP, SP, index2);
1259 __ StoreQToOffset(FTMP2, SP, index1);
1260}
1261
Alexey Frunze73296a72016-06-03 22:51:46 -07001262void CodeGeneratorMIPS::ComputeSpillMask() {
1263 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1264 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1265 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1266 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1267 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1268 // within the stack frame.
1269 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1270 core_spill_mask_ |= (1 << ZERO);
1271 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001272}
1273
1274bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001275 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001276 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1277 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1278 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001279 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001280}
1281
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001282static dwarf::Reg DWARFReg(Register reg) {
1283 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1284}
1285
1286// TODO: mapping of floating-point registers to DWARF.
1287
1288void CodeGeneratorMIPS::GenerateFrameEntry() {
1289 __ Bind(&frame_entry_label_);
1290
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001291 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01001292 __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
1293 __ Addiu(TMP, TMP, 1);
1294 __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001295 }
1296
Vladimir Marko33bff252017-11-01 14:35:42 +00001297 bool do_overflow_check =
1298 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001299
1300 if (do_overflow_check) {
1301 __ LoadFromOffset(kLoadWord,
1302 ZERO,
1303 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001304 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001305 RecordPcInfo(nullptr, 0);
1306 }
1307
1308 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001309 CHECK_EQ(fpu_spill_mask_, 0u);
1310 CHECK_EQ(core_spill_mask_, 1u << RA);
1311 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001312 return;
1313 }
1314
1315 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001316 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1317 LOG(FATAL) << "Stack frame larger than "
1318 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001319 }
1320
1321 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001322
Alexey Frunze73296a72016-06-03 22:51:46 -07001323 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001324 __ IncreaseFrameSize(ofs);
1325
Alexey Frunze73296a72016-06-03 22:51:46 -07001326 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1327 Register reg = static_cast<Register>(MostSignificantBit(mask));
1328 mask ^= 1u << reg;
1329 ofs -= kMipsWordSize;
1330 // The ZERO register is only included for alignment.
1331 if (reg != ZERO) {
1332 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001333 __ cfi().RelOffset(DWARFReg(reg), ofs);
1334 }
1335 }
1336
Alexey Frunze73296a72016-06-03 22:51:46 -07001337 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1338 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1339 mask ^= 1u << reg;
1340 ofs -= kMipsDoublewordSize;
1341 __ StoreDToOffset(reg, SP, ofs);
1342 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001343 }
1344
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001345 // Save the current method if we need it. Note that we do not
1346 // do this in HCurrentMethod, as the instruction might have been removed
1347 // in the SSA graph.
1348 if (RequiresCurrentMethod()) {
1349 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1350 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001351
1352 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1353 // Initialize should deoptimize flag to 0.
1354 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1355 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001356}
1357
1358void CodeGeneratorMIPS::GenerateFrameExit() {
1359 __ cfi().RememberState();
1360
1361 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001362 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001363
Alexey Frunze73296a72016-06-03 22:51:46 -07001364 // For better instruction scheduling restore RA before other registers.
1365 uint32_t ofs = GetFrameSize();
1366 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1367 Register reg = static_cast<Register>(MostSignificantBit(mask));
1368 mask ^= 1u << reg;
1369 ofs -= kMipsWordSize;
1370 // The ZERO register is only included for alignment.
1371 if (reg != ZERO) {
1372 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001373 __ cfi().Restore(DWARFReg(reg));
1374 }
1375 }
1376
Alexey Frunze73296a72016-06-03 22:51:46 -07001377 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1378 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1379 mask ^= 1u << reg;
1380 ofs -= kMipsDoublewordSize;
1381 __ LoadDFromOffset(reg, SP, ofs);
1382 // TODO: __ cfi().Restore(DWARFReg(reg));
1383 }
1384
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001385 size_t frame_size = GetFrameSize();
1386 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1387 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1388 bool reordering = __ SetReorder(false);
1389 if (exchange) {
1390 __ Jr(RA);
1391 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1392 } else {
1393 __ DecreaseFrameSize(frame_size);
1394 __ Jr(RA);
1395 __ Nop(); // In delay slot.
1396 }
1397 __ SetReorder(reordering);
1398 } else {
1399 __ Jr(RA);
1400 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001401 }
1402
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001403 __ cfi().RestoreState();
1404 __ cfi().DefCFAOffset(GetFrameSize());
1405}
1406
1407void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1408 __ Bind(GetLabelOf(block));
1409}
1410
Lena Djokicca8c2952017-05-29 11:31:46 +02001411VectorRegister VectorRegisterFrom(Location location) {
1412 DCHECK(location.IsFpuRegister());
1413 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1414}
1415
Lena Djokic8098da92017-06-28 12:07:50 +02001416void CodeGeneratorMIPS::MoveLocation(Location destination,
1417 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001418 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001419 if (source.Equals(destination)) {
1420 return;
1421 }
1422
Lena Djokic8098da92017-06-28 12:07:50 +02001423 if (source.IsConstant()) {
1424 MoveConstant(destination, source.GetConstant());
1425 } else {
1426 if (destination.IsRegister()) {
1427 if (source.IsRegister()) {
1428 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1429 } else if (source.IsFpuRegister()) {
1430 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1431 } else {
1432 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001433 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001434 }
1435 } else if (destination.IsRegisterPair()) {
1436 if (source.IsRegisterPair()) {
1437 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1438 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1439 } else if (source.IsFpuRegister()) {
1440 Register dst_high = destination.AsRegisterPairHigh<Register>();
1441 Register dst_low = destination.AsRegisterPairLow<Register>();
1442 FRegister src = source.AsFpuRegister<FRegister>();
1443 __ Mfc1(dst_low, src);
1444 __ MoveFromFpuHigh(dst_high, src);
1445 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001446 DCHECK(source.IsDoubleStackSlot())
1447 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001448 int32_t off = source.GetStackIndex();
1449 Register r = destination.AsRegisterPairLow<Register>();
1450 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1451 }
1452 } else if (destination.IsFpuRegister()) {
1453 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001454 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001455 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1456 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001457 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001458 FRegister dst = destination.AsFpuRegister<FRegister>();
1459 Register src_high = source.AsRegisterPairHigh<Register>();
1460 Register src_low = source.AsRegisterPairLow<Register>();
1461 __ Mtc1(src_low, dst);
1462 __ MoveToFpuHigh(src_high, dst);
1463 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001464 if (GetGraph()->HasSIMD()) {
1465 __ MoveV(VectorRegisterFrom(destination),
1466 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001467 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001468 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001469 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1470 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001471 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001472 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1473 }
Lena Djokic8098da92017-06-28 12:07:50 +02001474 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001475 } else if (source.IsSIMDStackSlot()) {
1476 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001477 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001478 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001479 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1480 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001481 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001482 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1483 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1484 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001485 } else if (destination.IsSIMDStackSlot()) {
1486 if (source.IsFpuRegister()) {
1487 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1488 } else {
1489 DCHECK(source.IsSIMDStackSlot());
1490 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1491 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1492 }
Lena Djokic8098da92017-06-28 12:07:50 +02001493 } else if (destination.IsDoubleStackSlot()) {
1494 int32_t dst_offset = destination.GetStackIndex();
1495 if (source.IsRegisterPair()) {
1496 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1497 } else if (source.IsFpuRegister()) {
1498 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1499 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001500 DCHECK(source.IsDoubleStackSlot())
1501 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001502 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1503 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1504 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1505 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1506 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001507 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001508 DCHECK(destination.IsStackSlot()) << destination;
1509 int32_t dst_offset = destination.GetStackIndex();
1510 if (source.IsRegister()) {
1511 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1512 } else if (source.IsFpuRegister()) {
1513 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1514 } else {
1515 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1516 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1517 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1518 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001519 }
1520 }
1521}
1522
1523void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1524 if (c->IsIntConstant() || c->IsNullConstant()) {
1525 // Move 32 bit constant.
1526 int32_t value = GetInt32ValueOf(c);
1527 if (destination.IsRegister()) {
1528 Register dst = destination.AsRegister<Register>();
1529 __ LoadConst32(dst, value);
1530 } else {
1531 DCHECK(destination.IsStackSlot())
1532 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001533 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001534 }
1535 } else if (c->IsLongConstant()) {
1536 // Move 64 bit constant.
1537 int64_t value = GetInt64ValueOf(c);
1538 if (destination.IsRegisterPair()) {
1539 Register r_h = destination.AsRegisterPairHigh<Register>();
1540 Register r_l = destination.AsRegisterPairLow<Register>();
1541 __ LoadConst64(r_h, r_l, value);
1542 } else {
1543 DCHECK(destination.IsDoubleStackSlot())
1544 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001545 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001546 }
1547 } else if (c->IsFloatConstant()) {
1548 // Move 32 bit float constant.
1549 int32_t value = GetInt32ValueOf(c);
1550 if (destination.IsFpuRegister()) {
1551 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1552 } else {
1553 DCHECK(destination.IsStackSlot())
1554 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001555 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001556 }
1557 } else {
1558 // Move 64 bit double constant.
1559 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1560 int64_t value = GetInt64ValueOf(c);
1561 if (destination.IsFpuRegister()) {
1562 FRegister fd = destination.AsFpuRegister<FRegister>();
1563 __ LoadDConst64(fd, value, TMP);
1564 } else {
1565 DCHECK(destination.IsDoubleStackSlot())
1566 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001567 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001568 }
1569 }
1570}
1571
1572void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1573 DCHECK(destination.IsRegister());
1574 Register dst = destination.AsRegister<Register>();
1575 __ LoadConst32(dst, value);
1576}
1577
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001578void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1579 if (location.IsRegister()) {
1580 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001581 } else if (location.IsRegisterPair()) {
1582 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1583 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001584 } else {
1585 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1586 }
1587}
1588
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001589template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001590inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1591 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001592 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001593 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001594 const DexFile* dex_file = info.target_dex_file;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001595 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001596 DCHECK(info.label.IsBound());
1597 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001598 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1599 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001600 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1601 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1602 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001603 : __ GetPcRelBaseLabelLocation();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001604 linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001605 }
1606}
1607
Vladimir Marko6fd16062018-06-26 11:02:04 +01001608template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
1609linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
1610 const DexFile* target_dex_file,
1611 uint32_t pc_insn_offset,
1612 uint32_t boot_image_offset) {
1613 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
1614 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00001615}
1616
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001617void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001618 DCHECK(linker_patches->empty());
1619 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001620 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001621 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001622 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001623 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001624 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01001625 string_bss_entry_patches_.size() +
1626 boot_image_intrinsic_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001627 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001628 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001629 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001630 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001631 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001632 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001633 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001634 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001635 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
1636 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001637 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001638 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00001639 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001640 DCHECK(boot_image_type_patches_.empty());
1641 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001642 DCHECK(boot_image_intrinsic_patches_.empty());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001643 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001644 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1645 method_bss_entry_patches_, linker_patches);
1646 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1647 type_bss_entry_patches_, linker_patches);
1648 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1649 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001650 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001651}
1652
Vladimir Marko6fd16062018-06-26 11:02:04 +01001653CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageIntrinsicPatch(
1654 uint32_t intrinsic_data,
1655 const PcRelativePatchInfo* info_high) {
1656 return NewPcRelativePatch(
1657 /* dex_file */ nullptr, intrinsic_data, info_high, &boot_image_intrinsic_patches_);
1658}
1659
Vladimir Markob066d432018-01-03 13:14:37 +00001660CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageRelRoPatch(
1661 uint32_t boot_image_offset,
1662 const PcRelativePatchInfo* info_high) {
1663 return NewPcRelativePatch(
1664 /* dex_file */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_);
1665}
1666
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001667CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001668 MethodReference target_method,
1669 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001670 return NewPcRelativePatch(
1671 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001672}
1673
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001674CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001675 MethodReference target_method,
1676 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001677 return NewPcRelativePatch(
1678 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001679}
1680
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001681CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001682 const DexFile& dex_file,
1683 dex::TypeIndex type_index,
1684 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001685 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001686}
1687
Vladimir Marko1998cd02017-01-13 13:02:58 +00001688CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001689 const DexFile& dex_file,
1690 dex::TypeIndex type_index,
1691 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001692 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001693}
1694
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001695CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001696 const DexFile& dex_file,
1697 dex::StringIndex string_index,
1698 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001699 return NewPcRelativePatch(
1700 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001701}
1702
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001703CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1704 const DexFile& dex_file,
1705 dex::StringIndex string_index,
1706 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001707 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001708}
1709
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001710CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001711 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001712 uint32_t offset_or_index,
1713 const PcRelativePatchInfo* info_high,
1714 ArenaDeque<PcRelativePatchInfo>* patches) {
1715 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001716 return &patches->back();
1717}
1718
Alexey Frunze06a46c42016-07-19 15:00:40 -07001719Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1720 return map->GetOrCreate(
1721 value,
1722 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1723}
1724
Alexey Frunze06a46c42016-07-19 15:00:40 -07001725Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001726 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001727}
1728
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001729void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001730 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001731 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001732 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001733 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001734 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001735 if (GetInstructionSetFeatures().IsR6()) {
1736 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001737 __ Bind(&info_high->label);
1738 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001739 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001740 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001741 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001742 } else {
1743 // If base is ZERO, emit NAL to obtain the actual base.
1744 if (base == ZERO) {
1745 // Generate a dummy PC-relative call to obtain PC.
1746 __ Nal();
1747 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001748 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001749 __ Lui(out, /* placeholder */ 0x1234);
1750 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1751 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1752 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001753 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001754 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001755 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001756 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001757 __ Addu(out, out, (base == ZERO) ? RA : base);
1758 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001759 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001760 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001761}
1762
Vladimir Marko6fd16062018-06-26 11:02:04 +01001763void CodeGeneratorMIPS::LoadBootImageAddress(Register reg, uint32_t boot_image_reference) {
1764 if (GetCompilerOptions().IsBootImage()) {
1765 PcRelativePatchInfo* info_high = NewBootImageIntrinsicPatch(boot_image_reference);
1766 PcRelativePatchInfo* info_low = NewBootImageIntrinsicPatch(boot_image_reference, info_high);
1767 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, /* base */ ZERO);
1768 __ Addiu(reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001769 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001770 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_reference);
1771 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_reference, info_high);
Vladimir Markoeebb8212018-06-05 14:57:24 +01001772 EmitPcRelativeAddressPlaceholderHigh(info_high, reg, /* base */ ZERO);
1773 __ Lw(reg, reg, /* placeholder */ 0x5678, &info_low->label);
1774 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001775 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01001776 gc::Heap* heap = Runtime::Current()->GetHeap();
1777 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001778 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01001779 __ LoadConst32(reg, dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address)));
1780 }
1781}
1782
Vladimir Marko6fd16062018-06-26 11:02:04 +01001783void CodeGeneratorMIPS::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
1784 uint32_t boot_image_offset) {
1785 DCHECK(invoke->IsStatic());
1786 InvokeRuntimeCallingConvention calling_convention;
1787 Register argument = calling_convention.GetRegisterAt(0);
1788 if (GetCompilerOptions().IsBootImage()) {
1789 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
1790 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
1791 MethodReference target_method = invoke->GetTargetMethod();
1792 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
1793 PcRelativePatchInfo* info_high = NewBootImageTypePatch(*target_method.dex_file, type_idx);
1794 PcRelativePatchInfo* info_low =
1795 NewBootImageTypePatch(*target_method.dex_file, type_idx, info_high);
1796 EmitPcRelativeAddressPlaceholderHigh(info_high, argument, /* base */ ZERO);
1797 __ Addiu(argument, argument, /* placeholder */ 0x5678, &info_low->label);
1798 } else {
1799 LoadBootImageAddress(argument, boot_image_offset);
1800 }
1801 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
1802 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
1803}
1804
Alexey Frunze627c1a02017-01-30 19:28:14 -08001805CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1806 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001807 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001808 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001809 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1810 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001811 return &jit_string_patches_.back();
1812}
1813
1814CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1815 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001816 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001817 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001818 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1819 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001820 return &jit_class_patches_.back();
1821}
1822
1823void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1824 const uint8_t* roots_data,
1825 const CodeGeneratorMIPS::JitPatchInfo& info,
1826 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001827 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1828 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001829 uintptr_t address =
1830 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1831 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1832 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001833 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1834 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1835 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1836 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001837 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001838 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1839 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001840 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001841 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001842 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1843 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001844 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001845 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1846 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001847}
1848
1849void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1850 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001851 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1852 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001853 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001854 }
1855 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001856 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1857 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001858 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001859 }
1860}
1861
Goran Jakovljevice114da22016-12-26 14:21:43 +01001862void CodeGeneratorMIPS::MarkGCCard(Register object,
1863 Register value,
1864 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001865 MipsLabel done;
1866 Register card = AT;
1867 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001868 if (value_can_be_null) {
1869 __ Beqz(value, &done);
1870 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 __ LoadFromOffset(kLoadWord,
1872 card,
1873 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001874 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001875 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1876 __ Addu(temp, card, temp);
1877 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001878 if (value_can_be_null) {
1879 __ Bind(&done);
1880 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001881}
1882
David Brazdil58282f42016-01-14 12:45:10 +00001883void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001884 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1885 blocked_core_registers_[ZERO] = true;
1886 blocked_core_registers_[K0] = true;
1887 blocked_core_registers_[K1] = true;
1888 blocked_core_registers_[GP] = true;
1889 blocked_core_registers_[SP] = true;
1890 blocked_core_registers_[RA] = true;
1891
1892 // AT and TMP(T8) are used as temporary/scratch registers
1893 // (similar to how AT is used by MIPS assemblers).
1894 blocked_core_registers_[AT] = true;
1895 blocked_core_registers_[TMP] = true;
1896 blocked_fpu_registers_[FTMP] = true;
1897
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001898 if (GetInstructionSetFeatures().HasMsa()) {
1899 // To be used just for MSA instructions.
1900 blocked_fpu_registers_[FTMP2] = true;
1901 }
1902
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001903 // Reserve suspend and thread registers.
1904 blocked_core_registers_[S0] = true;
1905 blocked_core_registers_[TR] = true;
1906
1907 // Reserve T9 for function calls
1908 blocked_core_registers_[T9] = true;
1909
1910 // Reserve odd-numbered FPU registers.
1911 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1912 blocked_fpu_registers_[i] = true;
1913 }
1914
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001915 if (GetGraph()->IsDebuggable()) {
1916 // Stubs do not save callee-save floating point registers. If the graph
1917 // is debuggable, we need to deal with these registers differently. For
1918 // now, just block them.
1919 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1920 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1921 }
1922 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001923}
1924
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001925size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1926 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1927 return kMipsWordSize;
1928}
1929
1930size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1931 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1932 return kMipsWordSize;
1933}
1934
1935size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001936 if (GetGraph()->HasSIMD()) {
1937 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1938 } else {
1939 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1940 }
1941 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942}
1943
1944size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001945 if (GetGraph()->HasSIMD()) {
1946 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1947 } else {
1948 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1949 }
1950 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951}
1952
1953void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001954 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001955}
1956
1957void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001958 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959}
1960
Vladimir Markoa0431112018-06-25 09:32:54 +01001961const MipsInstructionSetFeatures& CodeGeneratorMIPS::GetInstructionSetFeatures() const {
1962 return *GetCompilerOptions().GetInstructionSetFeatures()->AsMipsInstructionSetFeatures();
1963}
1964
Serban Constantinescufca16662016-07-14 09:21:59 +01001965constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1966
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001967void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1968 HInstruction* instruction,
1969 uint32_t dex_pc,
1970 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001971 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001972 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1973 IsDirectEntrypoint(entrypoint));
1974 if (EntrypointRequiresStackMap(entrypoint)) {
1975 RecordPcInfo(instruction, dex_pc, slow_path);
1976 }
1977}
1978
1979void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1980 HInstruction* instruction,
1981 SlowPathCode* slow_path,
1982 bool direct) {
1983 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1984 GenerateInvokeRuntime(entry_point_offset, direct);
1985}
1986
1987void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001988 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001989 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001990 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001991 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001992 // Reserve argument space on stack (for $a0-$a3) for
1993 // entrypoints that directly reference native implementations.
1994 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001995 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001996 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001997 } else {
1998 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001999 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07002000 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002001}
2002
2003void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
2004 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00002005 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
2006 const size_t status_byte_offset =
2007 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2008 constexpr uint32_t shifted_initialized_value =
2009 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
2010
2011 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01002012 __ Sltiu(TMP, TMP, shifted_initialized_value);
2013 __ Bnez(TMP, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
2015 __ Sync(0);
2016 __ Bind(slow_path->GetExitLabel());
2017}
2018
Vladimir Marko175e7862018-03-27 09:03:13 +00002019void InstructionCodeGeneratorMIPS::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
2020 Register temp) {
2021 uint32_t path_to_root = check->GetBitstringPathToRoot();
2022 uint32_t mask = check->GetBitstringMask();
2023 DCHECK(IsPowerOfTwo(mask + 1));
2024 size_t mask_bits = WhichPowerOf2(mask + 1);
2025
2026 if (mask_bits == 16u) {
2027 // Load only the bitstring part of the status word.
2028 __ LoadFromOffset(
2029 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
2030 // Compare the bitstring bits using XOR.
2031 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
2032 } else {
2033 // /* uint32_t */ temp = temp->status_
2034 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
2035 // Compare the bitstring bits using XOR.
2036 if (IsUint<16>(path_to_root)) {
2037 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
2038 } else {
2039 __ LoadConst32(TMP, path_to_root);
2040 __ Xor(temp, temp, TMP);
2041 }
2042 // Shift out bits that do not contribute to the comparison.
2043 __ Sll(temp, temp, 32 - mask_bits);
2044 }
2045}
2046
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002047void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
2048 __ Sync(0); // Only stype 0 is supported.
2049}
2050
2051void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
2052 HBasicBlock* successor) {
2053 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07002054 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
2055
2056 if (slow_path == nullptr) {
2057 slow_path =
2058 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
2059 instruction->SetSlowPath(slow_path);
2060 codegen_->AddSlowPath(slow_path);
2061 if (successor != nullptr) {
2062 DCHECK(successor->IsLoopHeader());
2063 }
2064 } else {
2065 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2066 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002067
2068 __ LoadFromOffset(kLoadUnsignedHalfword,
2069 TMP,
2070 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002071 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002072 if (successor == nullptr) {
2073 __ Bnez(TMP, slow_path->GetEntryLabel());
2074 __ Bind(slow_path->GetReturnLabel());
2075 } else {
2076 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2077 __ B(slow_path->GetEntryLabel());
2078 // slow_path will return to GetLabelOf(successor).
2079 }
2080}
2081
2082InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2083 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002084 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002085 assembler_(codegen->GetAssembler()),
2086 codegen_(codegen) {}
2087
2088void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2089 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002090 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002091 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01002092 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002093 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002094 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002095 locations->SetInAt(0, Location::RequiresRegister());
2096 HInstruction* right = instruction->InputAt(1);
2097 bool can_use_imm = false;
2098 if (right->IsConstant()) {
2099 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2100 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2101 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002102 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002103 DCHECK(instruction->IsSub() || instruction->IsAdd());
2104 if (instruction->IsSub()) {
2105 imm = -imm;
2106 }
2107 if (isR6) {
2108 bool single_use = right->GetUses().HasExactlyOneElement();
2109 int16_t imm_high = High16Bits(imm);
2110 int16_t imm_low = Low16Bits(imm);
2111 if (imm_low < 0) {
2112 imm_high += 1;
2113 }
2114 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2115 } else {
2116 can_use_imm = IsInt<16>(imm);
2117 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002118 }
2119 }
2120 if (can_use_imm)
2121 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2122 else
2123 locations->SetInAt(1, Location::RequiresRegister());
2124 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2125 break;
2126 }
2127
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002128 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002129 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002130 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2131 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002132 break;
2133 }
2134
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002135 case DataType::Type::kFloat32:
2136 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002137 DCHECK(instruction->IsAdd() || instruction->IsSub());
2138 locations->SetInAt(0, Location::RequiresFpuRegister());
2139 locations->SetInAt(1, Location::RequiresFpuRegister());
2140 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2141 break;
2142
2143 default:
2144 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2145 }
2146}
2147
2148void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002149 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002150 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002151 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002152
2153 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002154 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002155 Register dst = locations->Out().AsRegister<Register>();
2156 Register lhs = locations->InAt(0).AsRegister<Register>();
2157 Location rhs_location = locations->InAt(1);
2158
2159 Register rhs_reg = ZERO;
2160 int32_t rhs_imm = 0;
2161 bool use_imm = rhs_location.IsConstant();
2162 if (use_imm) {
2163 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2164 } else {
2165 rhs_reg = rhs_location.AsRegister<Register>();
2166 }
2167
2168 if (instruction->IsAnd()) {
2169 if (use_imm)
2170 __ Andi(dst, lhs, rhs_imm);
2171 else
2172 __ And(dst, lhs, rhs_reg);
2173 } else if (instruction->IsOr()) {
2174 if (use_imm)
2175 __ Ori(dst, lhs, rhs_imm);
2176 else
2177 __ Or(dst, lhs, rhs_reg);
2178 } else if (instruction->IsXor()) {
2179 if (use_imm)
2180 __ Xori(dst, lhs, rhs_imm);
2181 else
2182 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002183 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002184 DCHECK(instruction->IsAdd() || instruction->IsSub());
2185 if (use_imm) {
2186 if (instruction->IsSub()) {
2187 rhs_imm = -rhs_imm;
2188 }
2189 if (IsInt<16>(rhs_imm)) {
2190 __ Addiu(dst, lhs, rhs_imm);
2191 } else {
2192 DCHECK(isR6);
2193 int16_t rhs_imm_high = High16Bits(rhs_imm);
2194 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2195 if (rhs_imm_low < 0) {
2196 rhs_imm_high += 1;
2197 }
2198 __ Aui(dst, lhs, rhs_imm_high);
2199 if (rhs_imm_low != 0) {
2200 __ Addiu(dst, dst, rhs_imm_low);
2201 }
2202 }
2203 } else if (instruction->IsAdd()) {
2204 __ Addu(dst, lhs, rhs_reg);
2205 } else {
2206 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002207 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002208 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002209 }
2210 break;
2211 }
2212
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002213 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002214 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2215 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2216 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2217 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002218 Location rhs_location = locations->InAt(1);
2219 bool use_imm = rhs_location.IsConstant();
2220 if (!use_imm) {
2221 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2222 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2223 if (instruction->IsAnd()) {
2224 __ And(dst_low, lhs_low, rhs_low);
2225 __ And(dst_high, lhs_high, rhs_high);
2226 } else if (instruction->IsOr()) {
2227 __ Or(dst_low, lhs_low, rhs_low);
2228 __ Or(dst_high, lhs_high, rhs_high);
2229 } else if (instruction->IsXor()) {
2230 __ Xor(dst_low, lhs_low, rhs_low);
2231 __ Xor(dst_high, lhs_high, rhs_high);
2232 } else if (instruction->IsAdd()) {
2233 if (lhs_low == rhs_low) {
2234 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2235 __ Slt(TMP, lhs_low, ZERO);
2236 __ Addu(dst_low, lhs_low, rhs_low);
2237 } else {
2238 __ Addu(dst_low, lhs_low, rhs_low);
2239 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2240 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2241 }
2242 __ Addu(dst_high, lhs_high, rhs_high);
2243 __ Addu(dst_high, dst_high, TMP);
2244 } else {
2245 DCHECK(instruction->IsSub());
2246 __ Sltu(TMP, lhs_low, rhs_low);
2247 __ Subu(dst_low, lhs_low, rhs_low);
2248 __ Subu(dst_high, lhs_high, rhs_high);
2249 __ Subu(dst_high, dst_high, TMP);
2250 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002251 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002252 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2253 if (instruction->IsOr()) {
2254 uint32_t low = Low32Bits(value);
2255 uint32_t high = High32Bits(value);
2256 if (IsUint<16>(low)) {
2257 if (dst_low != lhs_low || low != 0) {
2258 __ Ori(dst_low, lhs_low, low);
2259 }
2260 } else {
2261 __ LoadConst32(TMP, low);
2262 __ Or(dst_low, lhs_low, TMP);
2263 }
2264 if (IsUint<16>(high)) {
2265 if (dst_high != lhs_high || high != 0) {
2266 __ Ori(dst_high, lhs_high, high);
2267 }
2268 } else {
2269 if (high != low) {
2270 __ LoadConst32(TMP, high);
2271 }
2272 __ Or(dst_high, lhs_high, TMP);
2273 }
2274 } else if (instruction->IsXor()) {
2275 uint32_t low = Low32Bits(value);
2276 uint32_t high = High32Bits(value);
2277 if (IsUint<16>(low)) {
2278 if (dst_low != lhs_low || low != 0) {
2279 __ Xori(dst_low, lhs_low, low);
2280 }
2281 } else {
2282 __ LoadConst32(TMP, low);
2283 __ Xor(dst_low, lhs_low, TMP);
2284 }
2285 if (IsUint<16>(high)) {
2286 if (dst_high != lhs_high || high != 0) {
2287 __ Xori(dst_high, lhs_high, high);
2288 }
2289 } else {
2290 if (high != low) {
2291 __ LoadConst32(TMP, high);
2292 }
2293 __ Xor(dst_high, lhs_high, TMP);
2294 }
2295 } else if (instruction->IsAnd()) {
2296 uint32_t low = Low32Bits(value);
2297 uint32_t high = High32Bits(value);
2298 if (IsUint<16>(low)) {
2299 __ Andi(dst_low, lhs_low, low);
2300 } else if (low != 0xFFFFFFFF) {
2301 __ LoadConst32(TMP, low);
2302 __ And(dst_low, lhs_low, TMP);
2303 } else if (dst_low != lhs_low) {
2304 __ Move(dst_low, lhs_low);
2305 }
2306 if (IsUint<16>(high)) {
2307 __ Andi(dst_high, lhs_high, high);
2308 } else if (high != 0xFFFFFFFF) {
2309 if (high != low) {
2310 __ LoadConst32(TMP, high);
2311 }
2312 __ And(dst_high, lhs_high, TMP);
2313 } else if (dst_high != lhs_high) {
2314 __ Move(dst_high, lhs_high);
2315 }
2316 } else {
2317 if (instruction->IsSub()) {
2318 value = -value;
2319 } else {
2320 DCHECK(instruction->IsAdd());
2321 }
2322 int32_t low = Low32Bits(value);
2323 int32_t high = High32Bits(value);
2324 if (IsInt<16>(low)) {
2325 if (dst_low != lhs_low || low != 0) {
2326 __ Addiu(dst_low, lhs_low, low);
2327 }
2328 if (low != 0) {
2329 __ Sltiu(AT, dst_low, low);
2330 }
2331 } else {
2332 __ LoadConst32(TMP, low);
2333 __ Addu(dst_low, lhs_low, TMP);
2334 __ Sltu(AT, dst_low, TMP);
2335 }
2336 if (IsInt<16>(high)) {
2337 if (dst_high != lhs_high || high != 0) {
2338 __ Addiu(dst_high, lhs_high, high);
2339 }
2340 } else {
2341 if (high != low) {
2342 __ LoadConst32(TMP, high);
2343 }
2344 __ Addu(dst_high, lhs_high, TMP);
2345 }
2346 if (low != 0) {
2347 __ Addu(dst_high, dst_high, AT);
2348 }
2349 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002350 }
2351 break;
2352 }
2353
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 case DataType::Type::kFloat32:
2355 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002356 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2357 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2358 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2359 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002360 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002361 __ AddS(dst, lhs, rhs);
2362 } else {
2363 __ AddD(dst, lhs, rhs);
2364 }
2365 } else {
2366 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 __ SubS(dst, lhs, rhs);
2369 } else {
2370 __ SubD(dst, lhs, rhs);
2371 }
2372 }
2373 break;
2374 }
2375
2376 default:
2377 LOG(FATAL) << "Unexpected binary operation type " << type;
2378 }
2379}
2380
2381void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002382 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002383
Vladimir Markoca6fff82017-10-03 14:49:14 +01002384 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002386 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002387 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002388 locations->SetInAt(0, Location::RequiresRegister());
2389 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2390 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2391 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002392 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002393 locations->SetInAt(0, Location::RequiresRegister());
2394 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2395 locations->SetOut(Location::RequiresRegister());
2396 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002397 default:
2398 LOG(FATAL) << "Unexpected shift type " << type;
2399 }
2400}
2401
2402static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2403
2404void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002405 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002406 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002407 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002408
2409 Location rhs_location = locations->InAt(1);
2410 bool use_imm = rhs_location.IsConstant();
2411 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2412 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002413 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002414 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002415 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002416 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2417 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418
2419 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002420 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 Register dst = locations->Out().AsRegister<Register>();
2422 Register lhs = locations->InAt(0).AsRegister<Register>();
2423 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002424 if (shift_value == 0) {
2425 if (dst != lhs) {
2426 __ Move(dst, lhs);
2427 }
2428 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429 __ Sll(dst, lhs, shift_value);
2430 } else if (instr->IsShr()) {
2431 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002432 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002433 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002434 } else {
2435 if (has_ins_rotr) {
2436 __ Rotr(dst, lhs, shift_value);
2437 } else {
2438 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2439 __ Srl(dst, lhs, shift_value);
2440 __ Or(dst, dst, TMP);
2441 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002442 }
2443 } else {
2444 if (instr->IsShl()) {
2445 __ Sllv(dst, lhs, rhs_reg);
2446 } else if (instr->IsShr()) {
2447 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002448 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002449 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002450 } else {
2451 if (has_ins_rotr) {
2452 __ Rotrv(dst, lhs, rhs_reg);
2453 } else {
2454 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002455 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2456 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2457 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2458 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2459 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002460 __ Sllv(TMP, lhs, TMP);
2461 __ Srlv(dst, lhs, rhs_reg);
2462 __ Or(dst, dst, TMP);
2463 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002464 }
2465 }
2466 break;
2467 }
2468
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002469 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002470 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2471 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2472 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2473 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2474 if (use_imm) {
2475 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002476 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002477 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002478 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002479 if (instr->IsShl()) {
2480 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2481 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2482 __ Sll(dst_low, lhs_low, shift_value);
2483 } else if (instr->IsShr()) {
2484 __ Srl(dst_low, lhs_low, shift_value);
2485 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2486 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002487 } else if (instr->IsUShr()) {
2488 __ Srl(dst_low, lhs_low, shift_value);
2489 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2490 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002491 } else {
2492 __ Srl(dst_low, lhs_low, shift_value);
2493 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2494 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002495 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002496 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002497 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002498 if (instr->IsShl()) {
2499 __ Sll(dst_low, lhs_low, shift_value);
2500 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2501 __ Sll(dst_high, lhs_high, shift_value);
2502 __ Or(dst_high, dst_high, TMP);
2503 } else if (instr->IsShr()) {
2504 __ Sra(dst_high, lhs_high, shift_value);
2505 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2506 __ Srl(dst_low, lhs_low, shift_value);
2507 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002508 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002509 __ Srl(dst_high, lhs_high, shift_value);
2510 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2511 __ Srl(dst_low, lhs_low, shift_value);
2512 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002513 } else {
2514 __ Srl(TMP, lhs_low, shift_value);
2515 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2516 __ Or(dst_low, dst_low, TMP);
2517 __ Srl(TMP, lhs_high, shift_value);
2518 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2519 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002520 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002521 }
2522 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002523 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002524 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002525 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002526 __ Move(dst_low, ZERO);
2527 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002528 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002529 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002530 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002531 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002532 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002533 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002534 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002535 // 64-bit rotation by 32 is just a swap.
2536 __ Move(dst_low, lhs_high);
2537 __ Move(dst_high, lhs_low);
2538 } else {
2539 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002540 __ Srl(dst_low, lhs_high, shift_value_high);
2541 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2542 __ Srl(dst_high, lhs_low, shift_value_high);
2543 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002544 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002545 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2546 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002547 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002548 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2549 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002550 __ Or(dst_high, dst_high, TMP);
2551 }
2552 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002553 }
2554 }
2555 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002556 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002557 MipsLabel done;
2558 if (instr->IsShl()) {
2559 __ Sllv(dst_low, lhs_low, rhs_reg);
2560 __ Nor(AT, ZERO, rhs_reg);
2561 __ Srl(TMP, lhs_low, 1);
2562 __ Srlv(TMP, TMP, AT);
2563 __ Sllv(dst_high, lhs_high, rhs_reg);
2564 __ Or(dst_high, dst_high, TMP);
2565 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002566 if (isR6) {
2567 __ Beqzc(TMP, &done, /* is_bare */ true);
2568 __ Move(dst_high, dst_low);
2569 __ Move(dst_low, ZERO);
2570 } else {
2571 __ Movn(dst_high, dst_low, TMP);
2572 __ Movn(dst_low, ZERO, TMP);
2573 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002574 } else if (instr->IsShr()) {
2575 __ Srav(dst_high, lhs_high, rhs_reg);
2576 __ Nor(AT, ZERO, rhs_reg);
2577 __ Sll(TMP, lhs_high, 1);
2578 __ Sllv(TMP, TMP, AT);
2579 __ Srlv(dst_low, lhs_low, rhs_reg);
2580 __ Or(dst_low, dst_low, TMP);
2581 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002582 if (isR6) {
2583 __ Beqzc(TMP, &done, /* is_bare */ true);
2584 __ Move(dst_low, dst_high);
2585 __ Sra(dst_high, dst_high, 31);
2586 } else {
2587 __ Sra(AT, dst_high, 31);
2588 __ Movn(dst_low, dst_high, TMP);
2589 __ Movn(dst_high, AT, TMP);
2590 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002591 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002592 __ Srlv(dst_high, lhs_high, rhs_reg);
2593 __ Nor(AT, ZERO, rhs_reg);
2594 __ Sll(TMP, lhs_high, 1);
2595 __ Sllv(TMP, TMP, AT);
2596 __ Srlv(dst_low, lhs_low, rhs_reg);
2597 __ Or(dst_low, dst_low, TMP);
2598 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002599 if (isR6) {
2600 __ Beqzc(TMP, &done, /* is_bare */ true);
2601 __ Move(dst_low, dst_high);
2602 __ Move(dst_high, ZERO);
2603 } else {
2604 __ Movn(dst_low, dst_high, TMP);
2605 __ Movn(dst_high, ZERO, TMP);
2606 }
2607 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002608 __ Nor(AT, ZERO, rhs_reg);
2609 __ Srlv(TMP, lhs_low, rhs_reg);
2610 __ Sll(dst_low, lhs_high, 1);
2611 __ Sllv(dst_low, dst_low, AT);
2612 __ Or(dst_low, dst_low, TMP);
2613 __ Srlv(TMP, lhs_high, rhs_reg);
2614 __ Sll(dst_high, lhs_low, 1);
2615 __ Sllv(dst_high, dst_high, AT);
2616 __ Or(dst_high, dst_high, TMP);
2617 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002618 if (isR6) {
2619 __ Beqzc(TMP, &done, /* is_bare */ true);
2620 __ Move(TMP, dst_high);
2621 __ Move(dst_high, dst_low);
2622 __ Move(dst_low, TMP);
2623 } else {
2624 __ Movn(AT, dst_high, TMP);
2625 __ Movn(dst_high, dst_low, TMP);
2626 __ Movn(dst_low, AT, TMP);
2627 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 }
2629 __ Bind(&done);
2630 }
2631 break;
2632 }
2633
2634 default:
2635 LOG(FATAL) << "Unexpected shift operation type " << type;
2636 }
2637}
2638
2639void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2640 HandleBinaryOp(instruction);
2641}
2642
2643void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2644 HandleBinaryOp(instruction);
2645}
2646
2647void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2648 HandleBinaryOp(instruction);
2649}
2650
2651void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2652 HandleBinaryOp(instruction);
2653}
2654
2655void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002656 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002657 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002658 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002659 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002660 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2661 object_array_get_with_read_barrier
2662 ? LocationSummary::kCallOnSlowPath
2663 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002664 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2665 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2666 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002667 locations->SetInAt(0, Location::RequiresRegister());
2668 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002669 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002670 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2671 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002672 // The output overlaps in the case of an object array get with
2673 // read barriers enabled: we do not want the move to overwrite the
2674 // array's location, as we need it to emit the read barrier.
2675 locations->SetOut(Location::RequiresRegister(),
2676 object_array_get_with_read_barrier
2677 ? Location::kOutputOverlap
2678 : Location::kNoOutputOverlap);
2679 }
2680 // We need a temporary register for the read barrier marking slow
2681 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2682 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002683 bool temp_needed = instruction->GetIndex()->IsConstant()
2684 ? !kBakerReadBarrierThunksEnableForFields
2685 : !kBakerReadBarrierThunksEnableForArrays;
2686 if (temp_needed) {
2687 locations->AddTemp(Location::RequiresRegister());
2688 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002689 }
2690}
2691
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002692static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2693 auto null_checker = [codegen, instruction]() {
2694 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002695 };
2696 return null_checker;
2697}
2698
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2700 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002701 Location obj_loc = locations->InAt(0);
2702 Register obj = obj_loc.AsRegister<Register>();
2703 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002704 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002705 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002706 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002707
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002708 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002709 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2710 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002711 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002712 case DataType::Type::kBool:
2713 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002714 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002715 if (index.IsConstant()) {
2716 size_t offset =
2717 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002718 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002719 } else {
2720 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002721 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002722 }
2723 break;
2724 }
2725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002726 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002727 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002728 if (index.IsConstant()) {
2729 size_t offset =
2730 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002731 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002732 } else {
2733 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002734 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002735 }
2736 break;
2737 }
2738
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002739 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002740 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002741 if (maybe_compressed_char_at) {
2742 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2743 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2744 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2745 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2746 "Expecting 0=compressed, 1=uncompressed");
2747 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002748 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002749 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2750 if (maybe_compressed_char_at) {
2751 MipsLabel uncompressed_load, done;
2752 __ Bnez(TMP, &uncompressed_load);
2753 __ LoadFromOffset(kLoadUnsignedByte,
2754 out,
2755 obj,
2756 data_offset + (const_index << TIMES_1));
2757 __ B(&done);
2758 __ Bind(&uncompressed_load);
2759 __ LoadFromOffset(kLoadUnsignedHalfword,
2760 out,
2761 obj,
2762 data_offset + (const_index << TIMES_2));
2763 __ Bind(&done);
2764 } else {
2765 __ LoadFromOffset(kLoadUnsignedHalfword,
2766 out,
2767 obj,
2768 data_offset + (const_index << TIMES_2),
2769 null_checker);
2770 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002771 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002772 Register index_reg = index.AsRegister<Register>();
2773 if (maybe_compressed_char_at) {
2774 MipsLabel uncompressed_load, done;
2775 __ Bnez(TMP, &uncompressed_load);
2776 __ Addu(TMP, obj, index_reg);
2777 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2778 __ B(&done);
2779 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002780 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002781 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2782 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002783 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2784 __ Addu(TMP, index_reg, obj);
2785 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002786 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002787 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002788 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2789 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002790 }
2791 break;
2792 }
2793
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002794 case DataType::Type::kInt16: {
2795 Register out = out_loc.AsRegister<Register>();
2796 if (index.IsConstant()) {
2797 size_t offset =
2798 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2799 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002800 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2801 __ Addu(TMP, index.AsRegister<Register>(), obj);
2802 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002803 } else {
2804 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2805 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2806 }
2807 break;
2808 }
2809
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002810 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002811 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002812 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002813 if (index.IsConstant()) {
2814 size_t offset =
2815 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002816 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002817 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2818 __ Addu(TMP, index.AsRegister<Register>(), obj);
2819 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002821 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002822 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002823 }
2824 break;
2825 }
2826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002828 static_assert(
2829 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2830 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2831 // /* HeapReference<Object> */ out =
2832 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2833 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002834 bool temp_needed = index.IsConstant()
2835 ? !kBakerReadBarrierThunksEnableForFields
2836 : !kBakerReadBarrierThunksEnableForArrays;
2837 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002838 // Note that a potential implicit null check is handled in this
2839 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002840 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2841 if (index.IsConstant()) {
2842 // Array load with a constant index can be treated as a field load.
2843 size_t offset =
2844 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2845 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2846 out_loc,
2847 obj,
2848 offset,
2849 temp,
2850 /* needs_null_check */ false);
2851 } else {
2852 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2853 out_loc,
2854 obj,
2855 data_offset,
2856 index,
2857 temp,
2858 /* needs_null_check */ false);
2859 }
Alexey Frunze15958152017-02-09 19:08:30 -08002860 } else {
2861 Register out = out_loc.AsRegister<Register>();
2862 if (index.IsConstant()) {
2863 size_t offset =
2864 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2865 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2866 // If read barriers are enabled, emit read barriers other than
2867 // Baker's using a slow path (and also unpoison the loaded
2868 // reference, if heap poisoning is enabled).
2869 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2870 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002871 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002872 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2873 // If read barriers are enabled, emit read barriers other than
2874 // Baker's using a slow path (and also unpoison the loaded
2875 // reference, if heap poisoning is enabled).
2876 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2877 out_loc,
2878 out_loc,
2879 obj_loc,
2880 data_offset,
2881 index);
2882 }
2883 }
2884 break;
2885 }
2886
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002887 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002888 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002889 if (index.IsConstant()) {
2890 size_t offset =
2891 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002892 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002893 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2894 __ Addu(TMP, index.AsRegister<Register>(), obj);
2895 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002896 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002897 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002898 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002899 }
2900 break;
2901 }
2902
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002903 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002904 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002905 if (index.IsConstant()) {
2906 size_t offset =
2907 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002908 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002909 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2910 __ Addu(TMP, index.AsRegister<Register>(), obj);
2911 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002912 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002913 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002914 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002915 }
2916 break;
2917 }
2918
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002919 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002920 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002921 if (index.IsConstant()) {
2922 size_t offset =
2923 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002924 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002925 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2926 __ Addu(TMP, index.AsRegister<Register>(), obj);
2927 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002928 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002929 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002930 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002931 }
2932 break;
2933 }
2934
Aart Bik66c158e2018-01-31 12:55:04 -08002935 case DataType::Type::kUint32:
2936 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002937 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002938 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2939 UNREACHABLE();
2940 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002941}
2942
2943void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002944 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002945 locations->SetInAt(0, Location::RequiresRegister());
2946 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2947}
2948
2949void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2950 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002951 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002952 Register obj = locations->InAt(0).AsRegister<Register>();
2953 Register out = locations->Out().AsRegister<Register>();
2954 __ LoadFromOffset(kLoadWord, out, obj, offset);
2955 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002956 // Mask out compression flag from String's array length.
2957 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2958 __ Srl(out, out, 1u);
2959 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002960}
2961
Alexey Frunzef58b2482016-09-02 22:14:06 -07002962Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2963 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2964 ? Location::ConstantLocation(instruction->AsConstant())
2965 : Location::RequiresRegister();
2966}
2967
2968Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2969 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2970 // We can store a non-zero float or double constant without first loading it into the FPU,
2971 // but we should only prefer this if the constant has a single use.
2972 if (instruction->IsConstant() &&
2973 (instruction->AsConstant()->IsZeroBitPattern() ||
2974 instruction->GetUses().HasExactlyOneElement())) {
2975 return Location::ConstantLocation(instruction->AsConstant());
2976 // Otherwise fall through and require an FPU register for the constant.
2977 }
2978 return Location::RequiresFpuRegister();
2979}
2980
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002981void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002982 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002983
2984 bool needs_write_barrier =
2985 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2986 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2987
Vladimir Markoca6fff82017-10-03 14:49:14 +01002988 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002989 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002990 may_need_runtime_call_for_type_check ?
2991 LocationSummary::kCallOnSlowPath :
2992 LocationSummary::kNoCall);
2993
2994 locations->SetInAt(0, Location::RequiresRegister());
2995 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002996 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002997 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002998 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002999 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
3000 }
3001 if (needs_write_barrier) {
3002 // Temporary register for the write barrier.
3003 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003004 }
3005}
3006
3007void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
3008 LocationSummary* locations = instruction->GetLocations();
3009 Register obj = locations->InAt(0).AsRegister<Register>();
3010 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003011 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08003013 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003014 bool needs_write_barrier =
3015 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003016 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003017 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003018
3019 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003020 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003021 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003022 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003023 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003024 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003025 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003026 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003027 __ Addu(base_reg, obj, index.AsRegister<Register>());
3028 }
3029 if (value_location.IsConstant()) {
3030 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3031 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
3032 } else {
3033 Register value = value_location.AsRegister<Register>();
3034 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003035 }
3036 break;
3037 }
3038
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003039 case DataType::Type::kUint16:
3040 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003041 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003042 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003043 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02003044 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3045 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003046 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003047 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003048 }
3049 if (value_location.IsConstant()) {
3050 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3051 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
3052 } else {
3053 Register value = value_location.AsRegister<Register>();
3054 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003055 }
3056 break;
3057 }
3058
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003059 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08003060 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3061 if (index.IsConstant()) {
3062 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003063 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3064 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08003065 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003066 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003067 }
3068 if (value_location.IsConstant()) {
3069 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3070 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3071 } else {
3072 Register value = value_location.AsRegister<Register>();
3073 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3074 }
3075 break;
3076 }
3077
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003078 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08003079 if (value_location.IsConstant()) {
3080 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003081 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003082 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003083 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003084 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003085 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003086 }
Alexey Frunze15958152017-02-09 19:08:30 -08003087 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3088 DCHECK_EQ(value, 0);
3089 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3090 DCHECK(!needs_write_barrier);
3091 DCHECK(!may_need_runtime_call_for_type_check);
3092 break;
3093 }
3094
3095 DCHECK(needs_write_barrier);
3096 Register value = value_location.AsRegister<Register>();
3097 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3098 Register temp2 = TMP; // Doesn't need to survive slow path.
3099 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3100 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3101 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3102 MipsLabel done;
3103 SlowPathCodeMIPS* slow_path = nullptr;
3104
3105 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003106 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003107 codegen_->AddSlowPath(slow_path);
3108 if (instruction->GetValueCanBeNull()) {
3109 MipsLabel non_zero;
3110 __ Bnez(value, &non_zero);
3111 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3112 if (index.IsConstant()) {
3113 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003114 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3115 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003116 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003117 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003118 }
Alexey Frunze15958152017-02-09 19:08:30 -08003119 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3120 __ B(&done);
3121 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003122 }
Alexey Frunze15958152017-02-09 19:08:30 -08003123
3124 // Note that when read barriers are enabled, the type checks
3125 // are performed without read barriers. This is fine, even in
3126 // the case where a class object is in the from-space after
3127 // the flip, as a comparison involving such a type would not
3128 // produce a false positive; it may of course produce a false
3129 // negative, in which case we would take the ArraySet slow
3130 // path.
3131
3132 // /* HeapReference<Class> */ temp1 = obj->klass_
3133 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3134 __ MaybeUnpoisonHeapReference(temp1);
3135
3136 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3137 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3138 // /* HeapReference<Class> */ temp2 = value->klass_
3139 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3140 // If heap poisoning is enabled, no need to unpoison `temp1`
3141 // nor `temp2`, as we are comparing two poisoned references.
3142
3143 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3144 MipsLabel do_put;
3145 __ Beq(temp1, temp2, &do_put);
3146 // If heap poisoning is enabled, the `temp1` reference has
3147 // not been unpoisoned yet; unpoison it now.
3148 __ MaybeUnpoisonHeapReference(temp1);
3149
3150 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3151 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3152 // If heap poisoning is enabled, no need to unpoison
3153 // `temp1`, as we are comparing against null below.
3154 __ Bnez(temp1, slow_path->GetEntryLabel());
3155 __ Bind(&do_put);
3156 } else {
3157 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3158 }
3159 }
3160
3161 Register source = value;
3162 if (kPoisonHeapReferences) {
3163 // Note that in the case where `value` is a null reference,
3164 // we do not enter this block, as a null reference does not
3165 // need poisoning.
3166 __ Move(temp1, value);
3167 __ PoisonHeapReference(temp1);
3168 source = temp1;
3169 }
3170
3171 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3172 if (index.IsConstant()) {
3173 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003174 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003175 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003176 }
3177 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3178
3179 if (!may_need_runtime_call_for_type_check) {
3180 codegen_->MaybeRecordImplicitNullCheck(instruction);
3181 }
3182
3183 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3184
3185 if (done.IsLinked()) {
3186 __ Bind(&done);
3187 }
3188
3189 if (slow_path != nullptr) {
3190 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003191 }
3192 break;
3193 }
3194
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003195 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003196 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003197 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003198 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003199 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3200 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003201 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003202 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003203 }
3204 if (value_location.IsConstant()) {
3205 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3206 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3207 } else {
3208 Register value = value_location.AsRegisterPairLow<Register>();
3209 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003210 }
3211 break;
3212 }
3213
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003214 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003215 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003216 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003217 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003218 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3219 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003220 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003221 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003222 }
3223 if (value_location.IsConstant()) {
3224 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3225 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3226 } else {
3227 FRegister value = value_location.AsFpuRegister<FRegister>();
3228 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003229 }
3230 break;
3231 }
3232
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003233 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003234 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003235 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003236 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003237 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3238 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003239 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003240 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003241 }
3242 if (value_location.IsConstant()) {
3243 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3244 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3245 } else {
3246 FRegister value = value_location.AsFpuRegister<FRegister>();
3247 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003248 }
3249 break;
3250 }
3251
Aart Bik66c158e2018-01-31 12:55:04 -08003252 case DataType::Type::kUint32:
3253 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003254 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003255 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3256 UNREACHABLE();
3257 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003258}
3259
Lena Djokica2901602017-09-21 13:50:52 +02003260void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3261 HIntermediateArrayAddressIndex* instruction) {
3262 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003263 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003264
3265 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3266
3267 locations->SetInAt(0, Location::RequiresRegister());
3268 locations->SetInAt(1, Location::ConstantLocation(shift));
3269 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3270}
3271
3272void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3273 HIntermediateArrayAddressIndex* instruction) {
3274 LocationSummary* locations = instruction->GetLocations();
3275 Register index_reg = locations->InAt(0).AsRegister<Register>();
3276 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3277 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3278}
3279
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003280void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003281 RegisterSet caller_saves = RegisterSet::Empty();
3282 InvokeRuntimeCallingConvention calling_convention;
3283 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3284 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3285 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003286
3287 HInstruction* index = instruction->InputAt(0);
3288 HInstruction* length = instruction->InputAt(1);
3289
3290 bool const_index = false;
3291 bool const_length = false;
3292
3293 if (index->IsConstant()) {
3294 if (length->IsConstant()) {
3295 const_index = true;
3296 const_length = true;
3297 } else {
3298 int32_t index_value = index->AsIntConstant()->GetValue();
3299 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3300 const_index = true;
3301 }
3302 }
3303 } else if (length->IsConstant()) {
3304 int32_t length_value = length->AsIntConstant()->GetValue();
3305 if (IsUint<15>(length_value)) {
3306 const_length = true;
3307 }
3308 }
3309
3310 locations->SetInAt(0, const_index
3311 ? Location::ConstantLocation(index->AsConstant())
3312 : Location::RequiresRegister());
3313 locations->SetInAt(1, const_length
3314 ? Location::ConstantLocation(length->AsConstant())
3315 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003316}
3317
3318void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3319 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003320 Location index_loc = locations->InAt(0);
3321 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003322
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003323 if (length_loc.IsConstant()) {
3324 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3325 if (index_loc.IsConstant()) {
3326 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3327 if (index < 0 || index >= length) {
3328 BoundsCheckSlowPathMIPS* slow_path =
3329 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3330 codegen_->AddSlowPath(slow_path);
3331 __ B(slow_path->GetEntryLabel());
3332 } else {
3333 // Nothing to be done.
3334 }
3335 return;
3336 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003337
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003338 BoundsCheckSlowPathMIPS* slow_path =
3339 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3340 codegen_->AddSlowPath(slow_path);
3341 Register index = index_loc.AsRegister<Register>();
3342 if (length == 0) {
3343 __ B(slow_path->GetEntryLabel());
3344 } else if (length == 1) {
3345 __ Bnez(index, slow_path->GetEntryLabel());
3346 } else {
3347 DCHECK(IsUint<15>(length)) << length;
3348 __ Sltiu(TMP, index, length);
3349 __ Beqz(TMP, slow_path->GetEntryLabel());
3350 }
3351 } else {
3352 Register length = length_loc.AsRegister<Register>();
3353 BoundsCheckSlowPathMIPS* slow_path =
3354 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3355 codegen_->AddSlowPath(slow_path);
3356 if (index_loc.IsConstant()) {
3357 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3358 if (index < 0) {
3359 __ B(slow_path->GetEntryLabel());
3360 } else if (index == 0) {
3361 __ Blez(length, slow_path->GetEntryLabel());
3362 } else {
3363 DCHECK(IsInt<16>(index + 1)) << index;
3364 __ Sltiu(TMP, length, index + 1);
3365 __ Bnez(TMP, slow_path->GetEntryLabel());
3366 }
3367 } else {
3368 Register index = index_loc.AsRegister<Register>();
3369 __ Bgeu(index, length, slow_path->GetEntryLabel());
3370 }
3371 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003372}
3373
Alexey Frunze15958152017-02-09 19:08:30 -08003374// Temp is used for read barrier.
3375static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3376 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003377 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003378 (kUseBakerReadBarrier ||
3379 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3380 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3381 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3382 return 1;
3383 }
3384 return 0;
3385}
3386
3387// Extra temp is used for read barrier.
3388static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3389 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3390}
3391
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003392void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003393 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003394 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003395 LocationSummary* locations =
3396 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003397 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003398 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3399 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3400 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3401 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3402 } else {
3403 locations->SetInAt(1, Location::RequiresRegister());
3404 }
Alexey Frunze15958152017-02-09 19:08:30 -08003405 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003406}
3407
3408void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003409 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003410 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003411 Location obj_loc = locations->InAt(0);
3412 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko175e7862018-03-27 09:03:13 +00003413 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08003414 Location temp_loc = locations->GetTemp(0);
3415 Register temp = temp_loc.AsRegister<Register>();
3416 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3417 DCHECK_LE(num_temps, 2u);
3418 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003419 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3420 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3421 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3422 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3423 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3424 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3425 const uint32_t object_array_data_offset =
3426 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3427 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003428
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003429 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003430 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003431 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3432 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003433 codegen_->AddSlowPath(slow_path);
3434
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003435 // Avoid this check if we know `obj` is not null.
3436 if (instruction->MustDoNullCheck()) {
3437 __ Beqz(obj, &done);
3438 }
3439
3440 switch (type_check_kind) {
3441 case TypeCheckKind::kExactCheck:
3442 case TypeCheckKind::kArrayCheck: {
3443 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003444 GenerateReferenceLoadTwoRegisters(instruction,
3445 temp_loc,
3446 obj_loc,
3447 class_offset,
3448 maybe_temp2_loc,
3449 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003450 // Jump to slow path for throwing the exception or doing a
3451 // more involved array check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003452 __ Bne(temp, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003453 break;
3454 }
3455
3456 case TypeCheckKind::kAbstractClassCheck: {
3457 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003458 GenerateReferenceLoadTwoRegisters(instruction,
3459 temp_loc,
3460 obj_loc,
3461 class_offset,
3462 maybe_temp2_loc,
3463 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003464 // If the class is abstract, we eagerly fetch the super class of the
3465 // object to avoid doing a comparison we know will fail.
3466 MipsLabel loop;
3467 __ Bind(&loop);
3468 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003469 GenerateReferenceLoadOneRegister(instruction,
3470 temp_loc,
3471 super_offset,
3472 maybe_temp2_loc,
3473 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003474 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3475 // exception.
3476 __ Beqz(temp, slow_path->GetEntryLabel());
3477 // Otherwise, compare the classes.
Vladimir Marko175e7862018-03-27 09:03:13 +00003478 __ Bne(temp, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003479 break;
3480 }
3481
3482 case TypeCheckKind::kClassHierarchyCheck: {
3483 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003484 GenerateReferenceLoadTwoRegisters(instruction,
3485 temp_loc,
3486 obj_loc,
3487 class_offset,
3488 maybe_temp2_loc,
3489 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003490 // Walk over the class hierarchy to find a match.
3491 MipsLabel loop;
3492 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00003493 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003494 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003495 GenerateReferenceLoadOneRegister(instruction,
3496 temp_loc,
3497 super_offset,
3498 maybe_temp2_loc,
3499 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003500 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3501 // exception. Otherwise, jump to the beginning of the loop.
3502 __ Bnez(temp, &loop);
3503 __ B(slow_path->GetEntryLabel());
3504 break;
3505 }
3506
3507 case TypeCheckKind::kArrayObjectCheck: {
3508 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003509 GenerateReferenceLoadTwoRegisters(instruction,
3510 temp_loc,
3511 obj_loc,
3512 class_offset,
3513 maybe_temp2_loc,
3514 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003515 // Do an exact check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003516 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003517 // Otherwise, we need to check that the object's class is a non-primitive array.
3518 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003519 GenerateReferenceLoadOneRegister(instruction,
3520 temp_loc,
3521 component_offset,
3522 maybe_temp2_loc,
3523 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003524 // If the component type is null, jump to the slow path to throw the exception.
3525 __ Beqz(temp, slow_path->GetEntryLabel());
3526 // Otherwise, the object is indeed an array, further check that this component
3527 // type is not a primitive type.
3528 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3529 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3530 __ Bnez(temp, slow_path->GetEntryLabel());
3531 break;
3532 }
3533
3534 case TypeCheckKind::kUnresolvedCheck:
3535 // We always go into the type check slow path for the unresolved check case.
3536 // We cannot directly call the CheckCast runtime entry point
3537 // without resorting to a type checking slow path here (i.e. by
3538 // calling InvokeRuntime directly), as it would require to
3539 // assign fixed registers for the inputs of this HInstanceOf
3540 // instruction (following the runtime calling convention), which
3541 // might be cluttered by the potential first read barrier
3542 // emission at the beginning of this method.
3543 __ B(slow_path->GetEntryLabel());
3544 break;
3545
3546 case TypeCheckKind::kInterfaceCheck: {
3547 // Avoid read barriers to improve performance of the fast path. We can not get false
3548 // positives by doing this.
3549 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003550 GenerateReferenceLoadTwoRegisters(instruction,
3551 temp_loc,
3552 obj_loc,
3553 class_offset,
3554 maybe_temp2_loc,
3555 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003556 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003557 GenerateReferenceLoadTwoRegisters(instruction,
3558 temp_loc,
3559 temp_loc,
3560 iftable_offset,
3561 maybe_temp2_loc,
3562 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003563 // Iftable is never null.
3564 __ Lw(TMP, temp, array_length_offset);
3565 // Loop through the iftable and check if any class matches.
3566 MipsLabel loop;
3567 __ Bind(&loop);
3568 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3569 __ Beqz(TMP, slow_path->GetEntryLabel());
3570 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3571 __ MaybeUnpoisonHeapReference(AT);
3572 // Go to next interface.
3573 __ Addiu(TMP, TMP, -2);
3574 // Compare the classes and continue the loop if they do not match.
Vladimir Marko175e7862018-03-27 09:03:13 +00003575 __ Bne(AT, cls.AsRegister<Register>(), &loop);
3576 break;
3577 }
3578
3579 case TypeCheckKind::kBitstringCheck: {
3580 // /* HeapReference<Class> */ temp = obj->klass_
3581 GenerateReferenceLoadTwoRegisters(instruction,
3582 temp_loc,
3583 obj_loc,
3584 class_offset,
3585 maybe_temp2_loc,
3586 kWithoutReadBarrier);
3587
3588 GenerateBitstringTypeCheckCompare(instruction, temp);
3589 __ Bnez(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003590 break;
3591 }
3592 }
3593
3594 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003595 __ Bind(slow_path->GetExitLabel());
3596}
3597
3598void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3599 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003600 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003601 locations->SetInAt(0, Location::RequiresRegister());
3602 if (check->HasUses()) {
3603 locations->SetOut(Location::SameAsFirstInput());
3604 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01003605 // Rely on the type initialization to save everything we need.
3606 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003607}
3608
3609void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3610 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01003611 SlowPathCodeMIPS* slow_path =
3612 new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(check->GetLoadClass(), check);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003613 codegen_->AddSlowPath(slow_path);
3614 GenerateClassInitializationCheck(slow_path,
3615 check->GetLocations()->InAt(0).AsRegister<Register>());
3616}
3617
3618void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003619 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003620
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003621 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003622 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003623
3624 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003626 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003627 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003628 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003629 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003630 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003631 locations->SetInAt(0, Location::RequiresRegister());
3632 locations->SetInAt(1, Location::RequiresRegister());
3633 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3634 break;
3635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003636 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003637 locations->SetInAt(0, Location::RequiresRegister());
3638 locations->SetInAt(1, Location::RequiresRegister());
3639 // Output overlaps because it is written before doing the low comparison.
3640 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3641 break;
3642
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003643 case DataType::Type::kFloat32:
3644 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003645 locations->SetInAt(0, Location::RequiresFpuRegister());
3646 locations->SetInAt(1, Location::RequiresFpuRegister());
3647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003648 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003649
3650 default:
3651 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3652 }
3653}
3654
3655void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3656 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003657 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003658 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003659 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003660
3661 // 0 if: left == right
3662 // 1 if: left > right
3663 // -1 if: left < right
3664 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003665 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003666 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003667 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003668 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003669 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003670 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003671 Register lhs = locations->InAt(0).AsRegister<Register>();
3672 Register rhs = locations->InAt(1).AsRegister<Register>();
3673 __ Slt(TMP, lhs, rhs);
3674 __ Slt(res, rhs, lhs);
3675 __ Subu(res, res, TMP);
3676 break;
3677 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003678 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003679 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003680 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3681 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3682 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3683 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3684 // TODO: more efficient (direct) comparison with a constant.
3685 __ Slt(TMP, lhs_high, rhs_high);
3686 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3687 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3688 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3689 __ Sltu(TMP, lhs_low, rhs_low);
3690 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3691 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3692 __ Bind(&done);
3693 break;
3694 }
3695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003697 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003698 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3699 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3700 MipsLabel done;
3701 if (isR6) {
3702 __ CmpEqS(FTMP, lhs, rhs);
3703 __ LoadConst32(res, 0);
3704 __ Bc1nez(FTMP, &done);
3705 if (gt_bias) {
3706 __ CmpLtS(FTMP, lhs, rhs);
3707 __ LoadConst32(res, -1);
3708 __ Bc1nez(FTMP, &done);
3709 __ LoadConst32(res, 1);
3710 } else {
3711 __ CmpLtS(FTMP, rhs, lhs);
3712 __ LoadConst32(res, 1);
3713 __ Bc1nez(FTMP, &done);
3714 __ LoadConst32(res, -1);
3715 }
3716 } else {
3717 if (gt_bias) {
3718 __ ColtS(0, lhs, rhs);
3719 __ LoadConst32(res, -1);
3720 __ Bc1t(0, &done);
3721 __ CeqS(0, lhs, rhs);
3722 __ LoadConst32(res, 1);
3723 __ Movt(res, ZERO, 0);
3724 } else {
3725 __ ColtS(0, rhs, lhs);
3726 __ LoadConst32(res, 1);
3727 __ Bc1t(0, &done);
3728 __ CeqS(0, lhs, rhs);
3729 __ LoadConst32(res, -1);
3730 __ Movt(res, ZERO, 0);
3731 }
3732 }
3733 __ Bind(&done);
3734 break;
3735 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003736 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003737 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003738 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3739 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3740 MipsLabel done;
3741 if (isR6) {
3742 __ CmpEqD(FTMP, lhs, rhs);
3743 __ LoadConst32(res, 0);
3744 __ Bc1nez(FTMP, &done);
3745 if (gt_bias) {
3746 __ CmpLtD(FTMP, lhs, rhs);
3747 __ LoadConst32(res, -1);
3748 __ Bc1nez(FTMP, &done);
3749 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003750 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003751 __ CmpLtD(FTMP, rhs, lhs);
3752 __ LoadConst32(res, 1);
3753 __ Bc1nez(FTMP, &done);
3754 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003755 }
3756 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003757 if (gt_bias) {
3758 __ ColtD(0, lhs, rhs);
3759 __ LoadConst32(res, -1);
3760 __ Bc1t(0, &done);
3761 __ CeqD(0, lhs, rhs);
3762 __ LoadConst32(res, 1);
3763 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003764 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003765 __ ColtD(0, rhs, lhs);
3766 __ LoadConst32(res, 1);
3767 __ Bc1t(0, &done);
3768 __ CeqD(0, lhs, rhs);
3769 __ LoadConst32(res, -1);
3770 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003771 }
3772 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003773 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003774 break;
3775 }
3776
3777 default:
3778 LOG(FATAL) << "Unimplemented compare type " << in_type;
3779 }
3780}
3781
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003782void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003783 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003784 switch (instruction->InputAt(0)->GetType()) {
3785 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003786 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003787 locations->SetInAt(0, Location::RequiresRegister());
3788 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3789 break;
3790
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003791 case DataType::Type::kFloat32:
3792 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003793 locations->SetInAt(0, Location::RequiresFpuRegister());
3794 locations->SetInAt(1, Location::RequiresFpuRegister());
3795 break;
3796 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003797 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3799 }
3800}
3801
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003802void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003803 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003804 return;
3805 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003806
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003807 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003808 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003809
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003810 switch (type) {
3811 default:
3812 // Integer case.
3813 GenerateIntCompare(instruction->GetCondition(), locations);
3814 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003815
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003816 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003817 GenerateLongCompare(instruction->GetCondition(), locations);
3818 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003819
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003820 case DataType::Type::kFloat32:
3821 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003822 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3823 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003824 }
3825}
3826
Alexey Frunze7e99e052015-11-24 19:28:01 -08003827void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3828 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003829
3830 LocationSummary* locations = instruction->GetLocations();
3831 Location second = locations->InAt(1);
3832 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003833 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003834 DCHECK(imm == 1 || imm == -1);
3835
Lena Djokic4b8025c2017-12-21 16:15:50 +01003836 if (instruction->GetResultType() == DataType::Type::kInt32) {
3837 Register out = locations->Out().AsRegister<Register>();
3838 Register dividend = locations->InAt(0).AsRegister<Register>();
3839
3840 if (instruction->IsRem()) {
3841 __ Move(out, ZERO);
3842 } else {
3843 if (imm == -1) {
3844 __ Subu(out, ZERO, dividend);
3845 } else if (out != dividend) {
3846 __ Move(out, dividend);
3847 }
3848 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003849 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003850 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3851 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3852 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3853 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3854 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3855
3856 if (instruction->IsRem()) {
3857 __ Move(out_high, ZERO);
3858 __ Move(out_low, ZERO);
3859 } else {
3860 if (imm == -1) {
3861 __ Subu(out_low, ZERO, in_low);
3862 __ Sltu(AT, ZERO, out_low);
3863 __ Subu(out_high, ZERO, in_high);
3864 __ Subu(out_high, out_high, AT);
3865 } else {
3866 __ Move(out_low, in_low);
3867 __ Move(out_high, in_high);
3868 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003869 }
3870 }
3871}
3872
3873void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3874 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003875
3876 LocationSummary* locations = instruction->GetLocations();
3877 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003878 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3879 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003880 DCHECK(second.IsConstant());
3881
Lena Djokic4b8025c2017-12-21 16:15:50 +01003882 if (instruction->GetResultType() == DataType::Type::kInt32) {
3883 Register out = locations->Out().AsRegister<Register>();
3884 Register dividend = locations->InAt(0).AsRegister<Register>();
3885 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3886 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3887 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003888
Lena Djokic4b8025c2017-12-21 16:15:50 +01003889 if (instruction->IsDiv()) {
3890 if (ctz_imm == 1) {
3891 // Fast path for division by +/-2, which is very common.
3892 __ Srl(TMP, dividend, 31);
3893 } else {
3894 __ Sra(TMP, dividend, 31);
3895 __ Srl(TMP, TMP, 32 - ctz_imm);
3896 }
3897 __ Addu(out, dividend, TMP);
3898 __ Sra(out, out, ctz_imm);
3899 if (imm < 0) {
3900 __ Subu(out, ZERO, out);
3901 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003902 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003903 if (ctz_imm == 1) {
3904 // Fast path for modulo +/-2, which is very common.
3905 __ Sra(TMP, dividend, 31);
3906 __ Subu(out, dividend, TMP);
3907 __ Andi(out, out, 1);
3908 __ Addu(out, out, TMP);
3909 } else {
3910 __ Sra(TMP, dividend, 31);
3911 __ Srl(TMP, TMP, 32 - ctz_imm);
3912 __ Addu(out, dividend, TMP);
3913 if (IsUint<16>(abs_imm - 1)) {
3914 __ Andi(out, out, abs_imm - 1);
3915 } else {
3916 if (is_r2_or_newer) {
3917 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3918 } else {
3919 __ Sll(out, out, 32 - ctz_imm);
3920 __ Srl(out, out, 32 - ctz_imm);
3921 }
3922 }
3923 __ Subu(out, out, TMP);
3924 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003925 }
3926 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003927 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3928 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3929 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3930 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3931 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3932 int64_t imm = Int64FromConstant(second.GetConstant());
3933 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3934 int ctz_imm = CTZ(abs_imm);
3935
3936 if (instruction->IsDiv()) {
3937 if (ctz_imm < 32) {
3938 if (ctz_imm == 1) {
3939 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003940 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003941 __ Sra(AT, in_high, 31);
3942 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003943 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003944 __ Addu(AT, AT, in_low);
3945 __ Sltu(TMP, AT, in_low);
3946 __ Addu(out_high, in_high, TMP);
3947 __ Srl(out_low, AT, ctz_imm);
3948 if (is_r2_or_newer) {
3949 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3950 __ Sra(out_high, out_high, ctz_imm);
3951 } else {
3952 __ Sll(AT, out_high, 32 - ctz_imm);
3953 __ Sra(out_high, out_high, ctz_imm);
3954 __ Or(out_low, out_low, AT);
3955 }
3956 if (imm < 0) {
3957 __ Subu(out_low, ZERO, out_low);
3958 __ Sltu(AT, ZERO, out_low);
3959 __ Subu(out_high, ZERO, out_high);
3960 __ Subu(out_high, out_high, AT);
3961 }
3962 } else if (ctz_imm == 32) {
3963 __ Sra(AT, in_high, 31);
3964 __ Addu(AT, AT, in_low);
3965 __ Sltu(AT, AT, in_low);
3966 __ Addu(out_low, in_high, AT);
3967 if (imm < 0) {
3968 __ Srl(TMP, out_low, 31);
3969 __ Subu(out_low, ZERO, out_low);
3970 __ Sltu(AT, ZERO, out_low);
3971 __ Subu(out_high, TMP, AT);
3972 } else {
3973 __ Sra(out_high, out_low, 31);
3974 }
3975 } else if (ctz_imm < 63) {
3976 __ Sra(AT, in_high, 31);
3977 __ Srl(TMP, AT, 64 - ctz_imm);
3978 __ Addu(AT, AT, in_low);
3979 __ Sltu(AT, AT, in_low);
3980 __ Addu(out_low, in_high, AT);
3981 __ Addu(out_low, out_low, TMP);
3982 __ Sra(out_low, out_low, ctz_imm - 32);
3983 if (imm < 0) {
3984 __ Subu(out_low, ZERO, out_low);
3985 }
3986 __ Sra(out_high, out_low, 31);
3987 } else {
3988 DCHECK_LT(imm, 0);
3989 if (is_r6) {
3990 __ Aui(AT, in_high, 0x8000);
3991 } else {
3992 __ Lui(AT, 0x8000);
3993 __ Xor(AT, AT, in_high);
3994 }
3995 __ Or(AT, AT, in_low);
3996 __ Sltiu(out_low, AT, 1);
3997 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003998 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003999 } else {
4000 if ((ctz_imm == 1) && !is_r6) {
4001 __ Andi(AT, in_low, 1);
4002 __ Sll(TMP, in_low, 31);
4003 __ And(TMP, in_high, TMP);
4004 __ Sra(out_high, TMP, 31);
4005 __ Or(out_low, out_high, AT);
4006 } else if (ctz_imm < 32) {
4007 __ Sra(AT, in_high, 31);
4008 if (ctz_imm <= 16) {
4009 __ Andi(out_low, in_low, abs_imm - 1);
4010 } else if (is_r2_or_newer) {
4011 __ Ext(out_low, in_low, 0, ctz_imm);
4012 } else {
4013 __ Sll(out_low, in_low, 32 - ctz_imm);
4014 __ Srl(out_low, out_low, 32 - ctz_imm);
4015 }
4016 if (is_r6) {
4017 __ Selnez(out_high, AT, out_low);
4018 } else {
4019 __ Movz(AT, ZERO, out_low);
4020 __ Move(out_high, AT);
4021 }
4022 if (is_r2_or_newer) {
4023 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
4024 } else {
4025 __ Sll(AT, out_high, ctz_imm);
4026 __ Or(out_low, out_low, AT);
4027 }
4028 } else if (ctz_imm == 32) {
4029 __ Sra(AT, in_high, 31);
4030 __ Move(out_low, in_low);
4031 if (is_r6) {
4032 __ Selnez(out_high, AT, out_low);
4033 } else {
4034 __ Movz(AT, ZERO, out_low);
4035 __ Move(out_high, AT);
4036 }
4037 } else if (ctz_imm < 63) {
4038 __ Sra(AT, in_high, 31);
4039 __ Move(TMP, in_low);
4040 if (ctz_imm - 32 <= 16) {
4041 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
4042 } else if (is_r2_or_newer) {
4043 __ Ext(out_high, in_high, 0, ctz_imm - 32);
4044 } else {
4045 __ Sll(out_high, in_high, 64 - ctz_imm);
4046 __ Srl(out_high, out_high, 64 - ctz_imm);
4047 }
4048 __ Move(out_low, TMP);
4049 __ Or(TMP, TMP, out_high);
4050 if (is_r6) {
4051 __ Selnez(AT, AT, TMP);
4052 } else {
4053 __ Movz(AT, ZERO, TMP);
4054 }
4055 if (is_r2_or_newer) {
4056 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
4057 } else {
4058 __ Sll(AT, AT, ctz_imm - 32);
4059 __ Or(out_high, out_high, AT);
4060 }
4061 } else {
4062 if (is_r6) {
4063 __ Aui(AT, in_high, 0x8000);
4064 } else {
4065 __ Lui(AT, 0x8000);
4066 __ Xor(AT, AT, in_high);
4067 }
4068 __ Or(AT, AT, in_low);
4069 __ Sltiu(AT, AT, 1);
4070 __ Sll(AT, AT, 31);
4071 __ Move(out_low, in_low);
4072 __ Xor(out_high, in_high, AT);
4073 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08004074 }
4075 }
4076}
4077
4078void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4079 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004080 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004081
4082 LocationSummary* locations = instruction->GetLocations();
4083 Location second = locations->InAt(1);
4084 DCHECK(second.IsConstant());
4085
4086 Register out = locations->Out().AsRegister<Register>();
4087 Register dividend = locations->InAt(0).AsRegister<Register>();
4088 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4089
4090 int64_t magic;
4091 int shift;
4092 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4093
4094 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4095
4096 __ LoadConst32(TMP, magic);
4097 if (isR6) {
4098 __ MuhR6(TMP, dividend, TMP);
4099 } else {
4100 __ MultR2(dividend, TMP);
4101 __ Mfhi(TMP);
4102 }
4103 if (imm > 0 && magic < 0) {
4104 __ Addu(TMP, TMP, dividend);
4105 } else if (imm < 0 && magic > 0) {
4106 __ Subu(TMP, TMP, dividend);
4107 }
4108
4109 if (shift != 0) {
4110 __ Sra(TMP, TMP, shift);
4111 }
4112
4113 if (instruction->IsDiv()) {
4114 __ Sra(out, TMP, 31);
4115 __ Subu(out, TMP, out);
4116 } else {
4117 __ Sra(AT, TMP, 31);
4118 __ Subu(AT, TMP, AT);
4119 __ LoadConst32(TMP, imm);
4120 if (isR6) {
4121 __ MulR6(TMP, AT, TMP);
4122 } else {
4123 __ MulR2(TMP, AT, TMP);
4124 }
4125 __ Subu(out, dividend, TMP);
4126 }
4127}
4128
4129void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4130 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004131 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004132
4133 LocationSummary* locations = instruction->GetLocations();
4134 Register out = locations->Out().AsRegister<Register>();
4135 Location second = locations->InAt(1);
4136
4137 if (second.IsConstant()) {
4138 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4139 if (imm == 0) {
4140 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4141 } else if (imm == 1 || imm == -1) {
4142 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004143 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004144 DivRemByPowerOfTwo(instruction);
4145 } else {
4146 DCHECK(imm <= -2 || imm >= 2);
4147 GenerateDivRemWithAnyConstant(instruction);
4148 }
4149 } else {
4150 Register dividend = locations->InAt(0).AsRegister<Register>();
4151 Register divisor = second.AsRegister<Register>();
4152 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4153 if (instruction->IsDiv()) {
4154 if (isR6) {
4155 __ DivR6(out, dividend, divisor);
4156 } else {
4157 __ DivR2(out, dividend, divisor);
4158 }
4159 } else {
4160 if (isR6) {
4161 __ ModR6(out, dividend, divisor);
4162 } else {
4163 __ ModR2(out, dividend, divisor);
4164 }
4165 }
4166 }
4167}
4168
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004169void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004170 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004171 bool call_long_div = false;
4172 if (type == DataType::Type::kInt64) {
4173 if (div->InputAt(1)->IsConstant()) {
4174 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4175 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4176 } else {
4177 call_long_div = true;
4178 }
4179 }
4180 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004181 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004182 : LocationSummary::kNoCall;
4183
Vladimir Markoca6fff82017-10-03 14:49:14 +01004184 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004185
4186 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004187 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004188 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004189 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004190 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4191 break;
4192
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004193 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004194 if (call_long_div) {
4195 InvokeRuntimeCallingConvention calling_convention;
4196 locations->SetInAt(0, Location::RegisterPairLocation(
4197 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4198 locations->SetInAt(1, Location::RegisterPairLocation(
4199 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4200 locations->SetOut(calling_convention.GetReturnLocation(type));
4201 } else {
4202 locations->SetInAt(0, Location::RequiresRegister());
4203 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4204 locations->SetOut(Location::RequiresRegister());
4205 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004206 break;
4207 }
4208
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004209 case DataType::Type::kFloat32:
4210 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004211 locations->SetInAt(0, Location::RequiresFpuRegister());
4212 locations->SetInAt(1, Location::RequiresFpuRegister());
4213 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4214 break;
4215
4216 default:
4217 LOG(FATAL) << "Unexpected div type " << type;
4218 }
4219}
4220
4221void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004222 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004223 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004224
4225 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004226 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004227 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004228 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004229 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004230 if (locations->InAt(1).IsConstant()) {
4231 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4232 if (imm == 0) {
4233 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4234 } else if (imm == 1 || imm == -1) {
4235 DivRemOneOrMinusOne(instruction);
4236 } else {
4237 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4238 DivRemByPowerOfTwo(instruction);
4239 }
4240 } else {
4241 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4242 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4243 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004244 break;
4245 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004246 case DataType::Type::kFloat32:
4247 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004248 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4249 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4250 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004251 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004252 __ DivS(dst, lhs, rhs);
4253 } else {
4254 __ DivD(dst, lhs, rhs);
4255 }
4256 break;
4257 }
4258 default:
4259 LOG(FATAL) << "Unexpected div type " << type;
4260 }
4261}
4262
4263void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004264 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004265 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004266}
4267
4268void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004269 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004270 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004271 codegen_->AddSlowPath(slow_path);
4272 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004273 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004274
4275 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004276 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004277 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004278 case DataType::Type::kInt8:
4279 case DataType::Type::kUint16:
4280 case DataType::Type::kInt16:
4281 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004282 if (value.IsConstant()) {
4283 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4284 __ B(slow_path->GetEntryLabel());
4285 } else {
4286 // A division by a non-null constant is valid. We don't need to perform
4287 // any check, so simply fall through.
4288 }
4289 } else {
4290 DCHECK(value.IsRegister()) << value;
4291 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4292 }
4293 break;
4294 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004295 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004296 if (value.IsConstant()) {
4297 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4298 __ B(slow_path->GetEntryLabel());
4299 } else {
4300 // A division by a non-null constant is valid. We don't need to perform
4301 // any check, so simply fall through.
4302 }
4303 } else {
4304 DCHECK(value.IsRegisterPair()) << value;
4305 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4306 __ Beqz(TMP, slow_path->GetEntryLabel());
4307 }
4308 break;
4309 }
4310 default:
4311 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4312 }
4313}
4314
4315void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4316 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004317 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004318 locations->SetOut(Location::ConstantLocation(constant));
4319}
4320
4321void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4322 // Will be generated at use site.
4323}
4324
4325void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4326 exit->SetLocations(nullptr);
4327}
4328
4329void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4330}
4331
4332void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4333 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004334 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004335 locations->SetOut(Location::ConstantLocation(constant));
4336}
4337
4338void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4339 // Will be generated at use site.
4340}
4341
4342void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4343 got->SetLocations(nullptr);
4344}
4345
4346void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004347 if (successor->IsExitBlock()) {
4348 DCHECK(got->GetPrevious()->AlwaysThrows());
4349 return; // no code needed
4350 }
4351
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004352 HBasicBlock* block = got->GetBlock();
4353 HInstruction* previous = got->GetPrevious();
4354 HLoopInformation* info = block->GetLoopInformation();
4355
4356 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004357 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4358 __ Lw(AT, SP, kCurrentMethodStackOffset);
4359 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4360 __ Addiu(TMP, TMP, 1);
4361 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4362 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004363 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4364 return;
4365 }
4366 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4367 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4368 }
4369 if (!codegen_->GoesToNextBlock(block, successor)) {
4370 __ B(codegen_->GetLabelOf(successor));
4371 }
4372}
4373
4374void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4375 HandleGoto(got, got->GetSuccessor());
4376}
4377
4378void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4379 try_boundary->SetLocations(nullptr);
4380}
4381
4382void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4383 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4384 if (!successor->IsExitBlock()) {
4385 HandleGoto(try_boundary, successor);
4386 }
4387}
4388
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004389void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4390 LocationSummary* locations) {
4391 Register dst = locations->Out().AsRegister<Register>();
4392 Register lhs = locations->InAt(0).AsRegister<Register>();
4393 Location rhs_location = locations->InAt(1);
4394 Register rhs_reg = ZERO;
4395 int64_t rhs_imm = 0;
4396 bool use_imm = rhs_location.IsConstant();
4397 if (use_imm) {
4398 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4399 } else {
4400 rhs_reg = rhs_location.AsRegister<Register>();
4401 }
4402
4403 switch (cond) {
4404 case kCondEQ:
4405 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004406 if (use_imm && IsInt<16>(-rhs_imm)) {
4407 if (rhs_imm == 0) {
4408 if (cond == kCondEQ) {
4409 __ Sltiu(dst, lhs, 1);
4410 } else {
4411 __ Sltu(dst, ZERO, lhs);
4412 }
4413 } else {
4414 __ Addiu(dst, lhs, -rhs_imm);
4415 if (cond == kCondEQ) {
4416 __ Sltiu(dst, dst, 1);
4417 } else {
4418 __ Sltu(dst, ZERO, dst);
4419 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004420 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004421 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004422 if (use_imm && IsUint<16>(rhs_imm)) {
4423 __ Xori(dst, lhs, rhs_imm);
4424 } else {
4425 if (use_imm) {
4426 rhs_reg = TMP;
4427 __ LoadConst32(rhs_reg, rhs_imm);
4428 }
4429 __ Xor(dst, lhs, rhs_reg);
4430 }
4431 if (cond == kCondEQ) {
4432 __ Sltiu(dst, dst, 1);
4433 } else {
4434 __ Sltu(dst, ZERO, dst);
4435 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004436 }
4437 break;
4438
4439 case kCondLT:
4440 case kCondGE:
4441 if (use_imm && IsInt<16>(rhs_imm)) {
4442 __ Slti(dst, lhs, rhs_imm);
4443 } else {
4444 if (use_imm) {
4445 rhs_reg = TMP;
4446 __ LoadConst32(rhs_reg, rhs_imm);
4447 }
4448 __ Slt(dst, lhs, rhs_reg);
4449 }
4450 if (cond == kCondGE) {
4451 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4452 // only the slt instruction but no sge.
4453 __ Xori(dst, dst, 1);
4454 }
4455 break;
4456
4457 case kCondLE:
4458 case kCondGT:
4459 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4460 // Simulate lhs <= rhs via lhs < rhs + 1.
4461 __ Slti(dst, lhs, rhs_imm + 1);
4462 if (cond == kCondGT) {
4463 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4464 // only the slti instruction but no sgti.
4465 __ Xori(dst, dst, 1);
4466 }
4467 } else {
4468 if (use_imm) {
4469 rhs_reg = TMP;
4470 __ LoadConst32(rhs_reg, rhs_imm);
4471 }
4472 __ Slt(dst, rhs_reg, lhs);
4473 if (cond == kCondLE) {
4474 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4475 // only the slt instruction but no sle.
4476 __ Xori(dst, dst, 1);
4477 }
4478 }
4479 break;
4480
4481 case kCondB:
4482 case kCondAE:
4483 if (use_imm && IsInt<16>(rhs_imm)) {
4484 // Sltiu sign-extends its 16-bit immediate operand before
4485 // the comparison and thus lets us compare directly with
4486 // unsigned values in the ranges [0, 0x7fff] and
4487 // [0xffff8000, 0xffffffff].
4488 __ Sltiu(dst, lhs, rhs_imm);
4489 } else {
4490 if (use_imm) {
4491 rhs_reg = TMP;
4492 __ LoadConst32(rhs_reg, rhs_imm);
4493 }
4494 __ Sltu(dst, lhs, rhs_reg);
4495 }
4496 if (cond == kCondAE) {
4497 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4498 // only the sltu instruction but no sgeu.
4499 __ Xori(dst, dst, 1);
4500 }
4501 break;
4502
4503 case kCondBE:
4504 case kCondA:
4505 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4506 // Simulate lhs <= rhs via lhs < rhs + 1.
4507 // Note that this only works if rhs + 1 does not overflow
4508 // to 0, hence the check above.
4509 // Sltiu sign-extends its 16-bit immediate operand before
4510 // the comparison and thus lets us compare directly with
4511 // unsigned values in the ranges [0, 0x7fff] and
4512 // [0xffff8000, 0xffffffff].
4513 __ Sltiu(dst, lhs, rhs_imm + 1);
4514 if (cond == kCondA) {
4515 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4516 // only the sltiu instruction but no sgtiu.
4517 __ Xori(dst, dst, 1);
4518 }
4519 } else {
4520 if (use_imm) {
4521 rhs_reg = TMP;
4522 __ LoadConst32(rhs_reg, rhs_imm);
4523 }
4524 __ Sltu(dst, rhs_reg, lhs);
4525 if (cond == kCondBE) {
4526 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4527 // only the sltu instruction but no sleu.
4528 __ Xori(dst, dst, 1);
4529 }
4530 }
4531 break;
4532 }
4533}
4534
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004535bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4536 LocationSummary* input_locations,
4537 Register dst) {
4538 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4539 Location rhs_location = input_locations->InAt(1);
4540 Register rhs_reg = ZERO;
4541 int64_t rhs_imm = 0;
4542 bool use_imm = rhs_location.IsConstant();
4543 if (use_imm) {
4544 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4545 } else {
4546 rhs_reg = rhs_location.AsRegister<Register>();
4547 }
4548
4549 switch (cond) {
4550 case kCondEQ:
4551 case kCondNE:
4552 if (use_imm && IsInt<16>(-rhs_imm)) {
4553 __ Addiu(dst, lhs, -rhs_imm);
4554 } else if (use_imm && IsUint<16>(rhs_imm)) {
4555 __ Xori(dst, lhs, rhs_imm);
4556 } else {
4557 if (use_imm) {
4558 rhs_reg = TMP;
4559 __ LoadConst32(rhs_reg, rhs_imm);
4560 }
4561 __ Xor(dst, lhs, rhs_reg);
4562 }
4563 return (cond == kCondEQ);
4564
4565 case kCondLT:
4566 case kCondGE:
4567 if (use_imm && IsInt<16>(rhs_imm)) {
4568 __ Slti(dst, lhs, rhs_imm);
4569 } else {
4570 if (use_imm) {
4571 rhs_reg = TMP;
4572 __ LoadConst32(rhs_reg, rhs_imm);
4573 }
4574 __ Slt(dst, lhs, rhs_reg);
4575 }
4576 return (cond == kCondGE);
4577
4578 case kCondLE:
4579 case kCondGT:
4580 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4581 // Simulate lhs <= rhs via lhs < rhs + 1.
4582 __ Slti(dst, lhs, rhs_imm + 1);
4583 return (cond == kCondGT);
4584 } else {
4585 if (use_imm) {
4586 rhs_reg = TMP;
4587 __ LoadConst32(rhs_reg, rhs_imm);
4588 }
4589 __ Slt(dst, rhs_reg, lhs);
4590 return (cond == kCondLE);
4591 }
4592
4593 case kCondB:
4594 case kCondAE:
4595 if (use_imm && IsInt<16>(rhs_imm)) {
4596 // Sltiu sign-extends its 16-bit immediate operand before
4597 // the comparison and thus lets us compare directly with
4598 // unsigned values in the ranges [0, 0x7fff] and
4599 // [0xffff8000, 0xffffffff].
4600 __ Sltiu(dst, lhs, rhs_imm);
4601 } else {
4602 if (use_imm) {
4603 rhs_reg = TMP;
4604 __ LoadConst32(rhs_reg, rhs_imm);
4605 }
4606 __ Sltu(dst, lhs, rhs_reg);
4607 }
4608 return (cond == kCondAE);
4609
4610 case kCondBE:
4611 case kCondA:
4612 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4613 // Simulate lhs <= rhs via lhs < rhs + 1.
4614 // Note that this only works if rhs + 1 does not overflow
4615 // to 0, hence the check above.
4616 // Sltiu sign-extends its 16-bit immediate operand before
4617 // the comparison and thus lets us compare directly with
4618 // unsigned values in the ranges [0, 0x7fff] and
4619 // [0xffff8000, 0xffffffff].
4620 __ Sltiu(dst, lhs, rhs_imm + 1);
4621 return (cond == kCondA);
4622 } else {
4623 if (use_imm) {
4624 rhs_reg = TMP;
4625 __ LoadConst32(rhs_reg, rhs_imm);
4626 }
4627 __ Sltu(dst, rhs_reg, lhs);
4628 return (cond == kCondBE);
4629 }
4630 }
4631}
4632
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004633void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4634 LocationSummary* locations,
4635 MipsLabel* label) {
4636 Register lhs = locations->InAt(0).AsRegister<Register>();
4637 Location rhs_location = locations->InAt(1);
4638 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004639 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004640 bool use_imm = rhs_location.IsConstant();
4641 if (use_imm) {
4642 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4643 } else {
4644 rhs_reg = rhs_location.AsRegister<Register>();
4645 }
4646
4647 if (use_imm && rhs_imm == 0) {
4648 switch (cond) {
4649 case kCondEQ:
4650 case kCondBE: // <= 0 if zero
4651 __ Beqz(lhs, label);
4652 break;
4653 case kCondNE:
4654 case kCondA: // > 0 if non-zero
4655 __ Bnez(lhs, label);
4656 break;
4657 case kCondLT:
4658 __ Bltz(lhs, label);
4659 break;
4660 case kCondGE:
4661 __ Bgez(lhs, label);
4662 break;
4663 case kCondLE:
4664 __ Blez(lhs, label);
4665 break;
4666 case kCondGT:
4667 __ Bgtz(lhs, label);
4668 break;
4669 case kCondB: // always false
4670 break;
4671 case kCondAE: // always true
4672 __ B(label);
4673 break;
4674 }
4675 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004676 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4677 if (isR6 || !use_imm) {
4678 if (use_imm) {
4679 rhs_reg = TMP;
4680 __ LoadConst32(rhs_reg, rhs_imm);
4681 }
4682 switch (cond) {
4683 case kCondEQ:
4684 __ Beq(lhs, rhs_reg, label);
4685 break;
4686 case kCondNE:
4687 __ Bne(lhs, rhs_reg, label);
4688 break;
4689 case kCondLT:
4690 __ Blt(lhs, rhs_reg, label);
4691 break;
4692 case kCondGE:
4693 __ Bge(lhs, rhs_reg, label);
4694 break;
4695 case kCondLE:
4696 __ Bge(rhs_reg, lhs, label);
4697 break;
4698 case kCondGT:
4699 __ Blt(rhs_reg, lhs, label);
4700 break;
4701 case kCondB:
4702 __ Bltu(lhs, rhs_reg, label);
4703 break;
4704 case kCondAE:
4705 __ Bgeu(lhs, rhs_reg, label);
4706 break;
4707 case kCondBE:
4708 __ Bgeu(rhs_reg, lhs, label);
4709 break;
4710 case kCondA:
4711 __ Bltu(rhs_reg, lhs, label);
4712 break;
4713 }
4714 } else {
4715 // Special cases for more efficient comparison with constants on R2.
4716 switch (cond) {
4717 case kCondEQ:
4718 __ LoadConst32(TMP, rhs_imm);
4719 __ Beq(lhs, TMP, label);
4720 break;
4721 case kCondNE:
4722 __ LoadConst32(TMP, rhs_imm);
4723 __ Bne(lhs, TMP, label);
4724 break;
4725 case kCondLT:
4726 if (IsInt<16>(rhs_imm)) {
4727 __ Slti(TMP, lhs, rhs_imm);
4728 __ Bnez(TMP, label);
4729 } else {
4730 __ LoadConst32(TMP, rhs_imm);
4731 __ Blt(lhs, TMP, label);
4732 }
4733 break;
4734 case kCondGE:
4735 if (IsInt<16>(rhs_imm)) {
4736 __ Slti(TMP, lhs, rhs_imm);
4737 __ Beqz(TMP, label);
4738 } else {
4739 __ LoadConst32(TMP, rhs_imm);
4740 __ Bge(lhs, TMP, label);
4741 }
4742 break;
4743 case kCondLE:
4744 if (IsInt<16>(rhs_imm + 1)) {
4745 // Simulate lhs <= rhs via lhs < rhs + 1.
4746 __ Slti(TMP, lhs, rhs_imm + 1);
4747 __ Bnez(TMP, label);
4748 } else {
4749 __ LoadConst32(TMP, rhs_imm);
4750 __ Bge(TMP, lhs, label);
4751 }
4752 break;
4753 case kCondGT:
4754 if (IsInt<16>(rhs_imm + 1)) {
4755 // Simulate lhs > rhs via !(lhs < rhs + 1).
4756 __ Slti(TMP, lhs, rhs_imm + 1);
4757 __ Beqz(TMP, label);
4758 } else {
4759 __ LoadConst32(TMP, rhs_imm);
4760 __ Blt(TMP, lhs, label);
4761 }
4762 break;
4763 case kCondB:
4764 if (IsInt<16>(rhs_imm)) {
4765 __ Sltiu(TMP, lhs, rhs_imm);
4766 __ Bnez(TMP, label);
4767 } else {
4768 __ LoadConst32(TMP, rhs_imm);
4769 __ Bltu(lhs, TMP, label);
4770 }
4771 break;
4772 case kCondAE:
4773 if (IsInt<16>(rhs_imm)) {
4774 __ Sltiu(TMP, lhs, rhs_imm);
4775 __ Beqz(TMP, label);
4776 } else {
4777 __ LoadConst32(TMP, rhs_imm);
4778 __ Bgeu(lhs, TMP, label);
4779 }
4780 break;
4781 case kCondBE:
4782 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4783 // Simulate lhs <= rhs via lhs < rhs + 1.
4784 // Note that this only works if rhs + 1 does not overflow
4785 // to 0, hence the check above.
4786 __ Sltiu(TMP, lhs, rhs_imm + 1);
4787 __ Bnez(TMP, label);
4788 } else {
4789 __ LoadConst32(TMP, rhs_imm);
4790 __ Bgeu(TMP, lhs, label);
4791 }
4792 break;
4793 case kCondA:
4794 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4795 // Simulate lhs > rhs via !(lhs < rhs + 1).
4796 // Note that this only works if rhs + 1 does not overflow
4797 // to 0, hence the check above.
4798 __ Sltiu(TMP, lhs, rhs_imm + 1);
4799 __ Beqz(TMP, label);
4800 } else {
4801 __ LoadConst32(TMP, rhs_imm);
4802 __ Bltu(TMP, lhs, label);
4803 }
4804 break;
4805 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004806 }
4807 }
4808}
4809
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004810void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4811 LocationSummary* locations) {
4812 Register dst = locations->Out().AsRegister<Register>();
4813 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4814 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4815 Location rhs_location = locations->InAt(1);
4816 Register rhs_high = ZERO;
4817 Register rhs_low = ZERO;
4818 int64_t imm = 0;
4819 uint32_t imm_high = 0;
4820 uint32_t imm_low = 0;
4821 bool use_imm = rhs_location.IsConstant();
4822 if (use_imm) {
4823 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4824 imm_high = High32Bits(imm);
4825 imm_low = Low32Bits(imm);
4826 } else {
4827 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4828 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4829 }
4830 if (use_imm && imm == 0) {
4831 switch (cond) {
4832 case kCondEQ:
4833 case kCondBE: // <= 0 if zero
4834 __ Or(dst, lhs_high, lhs_low);
4835 __ Sltiu(dst, dst, 1);
4836 break;
4837 case kCondNE:
4838 case kCondA: // > 0 if non-zero
4839 __ Or(dst, lhs_high, lhs_low);
4840 __ Sltu(dst, ZERO, dst);
4841 break;
4842 case kCondLT:
4843 __ Slt(dst, lhs_high, ZERO);
4844 break;
4845 case kCondGE:
4846 __ Slt(dst, lhs_high, ZERO);
4847 __ Xori(dst, dst, 1);
4848 break;
4849 case kCondLE:
4850 __ Or(TMP, lhs_high, lhs_low);
4851 __ Sra(AT, lhs_high, 31);
4852 __ Sltu(dst, AT, TMP);
4853 __ Xori(dst, dst, 1);
4854 break;
4855 case kCondGT:
4856 __ Or(TMP, lhs_high, lhs_low);
4857 __ Sra(AT, lhs_high, 31);
4858 __ Sltu(dst, AT, TMP);
4859 break;
4860 case kCondB: // always false
4861 __ Andi(dst, dst, 0);
4862 break;
4863 case kCondAE: // always true
4864 __ Ori(dst, ZERO, 1);
4865 break;
4866 }
4867 } else if (use_imm) {
4868 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4869 switch (cond) {
4870 case kCondEQ:
4871 __ LoadConst32(TMP, imm_high);
4872 __ Xor(TMP, TMP, lhs_high);
4873 __ LoadConst32(AT, imm_low);
4874 __ Xor(AT, AT, lhs_low);
4875 __ Or(dst, TMP, AT);
4876 __ Sltiu(dst, dst, 1);
4877 break;
4878 case kCondNE:
4879 __ LoadConst32(TMP, imm_high);
4880 __ Xor(TMP, TMP, lhs_high);
4881 __ LoadConst32(AT, imm_low);
4882 __ Xor(AT, AT, lhs_low);
4883 __ Or(dst, TMP, AT);
4884 __ Sltu(dst, ZERO, dst);
4885 break;
4886 case kCondLT:
4887 case kCondGE:
4888 if (dst == lhs_low) {
4889 __ LoadConst32(TMP, imm_low);
4890 __ Sltu(dst, lhs_low, TMP);
4891 }
4892 __ LoadConst32(TMP, imm_high);
4893 __ Slt(AT, lhs_high, TMP);
4894 __ Slt(TMP, TMP, lhs_high);
4895 if (dst != lhs_low) {
4896 __ LoadConst32(dst, imm_low);
4897 __ Sltu(dst, lhs_low, dst);
4898 }
4899 __ Slt(dst, TMP, dst);
4900 __ Or(dst, dst, AT);
4901 if (cond == kCondGE) {
4902 __ Xori(dst, dst, 1);
4903 }
4904 break;
4905 case kCondGT:
4906 case kCondLE:
4907 if (dst == lhs_low) {
4908 __ LoadConst32(TMP, imm_low);
4909 __ Sltu(dst, TMP, lhs_low);
4910 }
4911 __ LoadConst32(TMP, imm_high);
4912 __ Slt(AT, TMP, lhs_high);
4913 __ Slt(TMP, lhs_high, TMP);
4914 if (dst != lhs_low) {
4915 __ LoadConst32(dst, imm_low);
4916 __ Sltu(dst, dst, lhs_low);
4917 }
4918 __ Slt(dst, TMP, dst);
4919 __ Or(dst, dst, AT);
4920 if (cond == kCondLE) {
4921 __ Xori(dst, dst, 1);
4922 }
4923 break;
4924 case kCondB:
4925 case kCondAE:
4926 if (dst == lhs_low) {
4927 __ LoadConst32(TMP, imm_low);
4928 __ Sltu(dst, lhs_low, TMP);
4929 }
4930 __ LoadConst32(TMP, imm_high);
4931 __ Sltu(AT, lhs_high, TMP);
4932 __ Sltu(TMP, TMP, lhs_high);
4933 if (dst != lhs_low) {
4934 __ LoadConst32(dst, imm_low);
4935 __ Sltu(dst, lhs_low, dst);
4936 }
4937 __ Slt(dst, TMP, dst);
4938 __ Or(dst, dst, AT);
4939 if (cond == kCondAE) {
4940 __ Xori(dst, dst, 1);
4941 }
4942 break;
4943 case kCondA:
4944 case kCondBE:
4945 if (dst == lhs_low) {
4946 __ LoadConst32(TMP, imm_low);
4947 __ Sltu(dst, TMP, lhs_low);
4948 }
4949 __ LoadConst32(TMP, imm_high);
4950 __ Sltu(AT, TMP, lhs_high);
4951 __ Sltu(TMP, lhs_high, TMP);
4952 if (dst != lhs_low) {
4953 __ LoadConst32(dst, imm_low);
4954 __ Sltu(dst, dst, lhs_low);
4955 }
4956 __ Slt(dst, TMP, dst);
4957 __ Or(dst, dst, AT);
4958 if (cond == kCondBE) {
4959 __ Xori(dst, dst, 1);
4960 }
4961 break;
4962 }
4963 } else {
4964 switch (cond) {
4965 case kCondEQ:
4966 __ Xor(TMP, lhs_high, rhs_high);
4967 __ Xor(AT, lhs_low, rhs_low);
4968 __ Or(dst, TMP, AT);
4969 __ Sltiu(dst, dst, 1);
4970 break;
4971 case kCondNE:
4972 __ Xor(TMP, lhs_high, rhs_high);
4973 __ Xor(AT, lhs_low, rhs_low);
4974 __ Or(dst, TMP, AT);
4975 __ Sltu(dst, ZERO, dst);
4976 break;
4977 case kCondLT:
4978 case kCondGE:
4979 __ Slt(TMP, rhs_high, lhs_high);
4980 __ Sltu(AT, lhs_low, rhs_low);
4981 __ Slt(TMP, TMP, AT);
4982 __ Slt(AT, lhs_high, rhs_high);
4983 __ Or(dst, AT, TMP);
4984 if (cond == kCondGE) {
4985 __ Xori(dst, dst, 1);
4986 }
4987 break;
4988 case kCondGT:
4989 case kCondLE:
4990 __ Slt(TMP, lhs_high, rhs_high);
4991 __ Sltu(AT, rhs_low, lhs_low);
4992 __ Slt(TMP, TMP, AT);
4993 __ Slt(AT, rhs_high, lhs_high);
4994 __ Or(dst, AT, TMP);
4995 if (cond == kCondLE) {
4996 __ Xori(dst, dst, 1);
4997 }
4998 break;
4999 case kCondB:
5000 case kCondAE:
5001 __ Sltu(TMP, rhs_high, lhs_high);
5002 __ Sltu(AT, lhs_low, rhs_low);
5003 __ Slt(TMP, TMP, AT);
5004 __ Sltu(AT, lhs_high, rhs_high);
5005 __ Or(dst, AT, TMP);
5006 if (cond == kCondAE) {
5007 __ Xori(dst, dst, 1);
5008 }
5009 break;
5010 case kCondA:
5011 case kCondBE:
5012 __ Sltu(TMP, lhs_high, rhs_high);
5013 __ Sltu(AT, rhs_low, lhs_low);
5014 __ Slt(TMP, TMP, AT);
5015 __ Sltu(AT, rhs_high, lhs_high);
5016 __ Or(dst, AT, TMP);
5017 if (cond == kCondBE) {
5018 __ Xori(dst, dst, 1);
5019 }
5020 break;
5021 }
5022 }
5023}
5024
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005025void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
5026 LocationSummary* locations,
5027 MipsLabel* label) {
5028 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5029 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5030 Location rhs_location = locations->InAt(1);
5031 Register rhs_high = ZERO;
5032 Register rhs_low = ZERO;
5033 int64_t imm = 0;
5034 uint32_t imm_high = 0;
5035 uint32_t imm_low = 0;
5036 bool use_imm = rhs_location.IsConstant();
5037 if (use_imm) {
5038 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
5039 imm_high = High32Bits(imm);
5040 imm_low = Low32Bits(imm);
5041 } else {
5042 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
5043 rhs_low = rhs_location.AsRegisterPairLow<Register>();
5044 }
5045
5046 if (use_imm && imm == 0) {
5047 switch (cond) {
5048 case kCondEQ:
5049 case kCondBE: // <= 0 if zero
5050 __ Or(TMP, lhs_high, lhs_low);
5051 __ Beqz(TMP, label);
5052 break;
5053 case kCondNE:
5054 case kCondA: // > 0 if non-zero
5055 __ Or(TMP, lhs_high, lhs_low);
5056 __ Bnez(TMP, label);
5057 break;
5058 case kCondLT:
5059 __ Bltz(lhs_high, label);
5060 break;
5061 case kCondGE:
5062 __ Bgez(lhs_high, label);
5063 break;
5064 case kCondLE:
5065 __ Or(TMP, lhs_high, lhs_low);
5066 __ Sra(AT, lhs_high, 31);
5067 __ Bgeu(AT, TMP, label);
5068 break;
5069 case kCondGT:
5070 __ Or(TMP, lhs_high, lhs_low);
5071 __ Sra(AT, lhs_high, 31);
5072 __ Bltu(AT, TMP, label);
5073 break;
5074 case kCondB: // always false
5075 break;
5076 case kCondAE: // always true
5077 __ B(label);
5078 break;
5079 }
5080 } else if (use_imm) {
5081 // TODO: more efficient comparison with constants without loading them into TMP/AT.
5082 switch (cond) {
5083 case kCondEQ:
5084 __ LoadConst32(TMP, imm_high);
5085 __ Xor(TMP, TMP, lhs_high);
5086 __ LoadConst32(AT, imm_low);
5087 __ Xor(AT, AT, lhs_low);
5088 __ Or(TMP, TMP, AT);
5089 __ Beqz(TMP, label);
5090 break;
5091 case kCondNE:
5092 __ LoadConst32(TMP, imm_high);
5093 __ Xor(TMP, TMP, lhs_high);
5094 __ LoadConst32(AT, imm_low);
5095 __ Xor(AT, AT, lhs_low);
5096 __ Or(TMP, TMP, AT);
5097 __ Bnez(TMP, label);
5098 break;
5099 case kCondLT:
5100 __ LoadConst32(TMP, imm_high);
5101 __ Blt(lhs_high, TMP, label);
5102 __ Slt(TMP, TMP, lhs_high);
5103 __ LoadConst32(AT, imm_low);
5104 __ Sltu(AT, lhs_low, AT);
5105 __ Blt(TMP, AT, label);
5106 break;
5107 case kCondGE:
5108 __ LoadConst32(TMP, imm_high);
5109 __ Blt(TMP, lhs_high, label);
5110 __ Slt(TMP, lhs_high, TMP);
5111 __ LoadConst32(AT, imm_low);
5112 __ Sltu(AT, lhs_low, AT);
5113 __ Or(TMP, TMP, AT);
5114 __ Beqz(TMP, label);
5115 break;
5116 case kCondLE:
5117 __ LoadConst32(TMP, imm_high);
5118 __ Blt(lhs_high, TMP, label);
5119 __ Slt(TMP, TMP, lhs_high);
5120 __ LoadConst32(AT, imm_low);
5121 __ Sltu(AT, AT, lhs_low);
5122 __ Or(TMP, TMP, AT);
5123 __ Beqz(TMP, label);
5124 break;
5125 case kCondGT:
5126 __ LoadConst32(TMP, imm_high);
5127 __ Blt(TMP, lhs_high, label);
5128 __ Slt(TMP, lhs_high, TMP);
5129 __ LoadConst32(AT, imm_low);
5130 __ Sltu(AT, AT, lhs_low);
5131 __ Blt(TMP, AT, label);
5132 break;
5133 case kCondB:
5134 __ LoadConst32(TMP, imm_high);
5135 __ Bltu(lhs_high, TMP, label);
5136 __ Sltu(TMP, TMP, lhs_high);
5137 __ LoadConst32(AT, imm_low);
5138 __ Sltu(AT, lhs_low, AT);
5139 __ Blt(TMP, AT, label);
5140 break;
5141 case kCondAE:
5142 __ LoadConst32(TMP, imm_high);
5143 __ Bltu(TMP, lhs_high, label);
5144 __ Sltu(TMP, lhs_high, TMP);
5145 __ LoadConst32(AT, imm_low);
5146 __ Sltu(AT, lhs_low, AT);
5147 __ Or(TMP, TMP, AT);
5148 __ Beqz(TMP, label);
5149 break;
5150 case kCondBE:
5151 __ LoadConst32(TMP, imm_high);
5152 __ Bltu(lhs_high, TMP, label);
5153 __ Sltu(TMP, TMP, lhs_high);
5154 __ LoadConst32(AT, imm_low);
5155 __ Sltu(AT, AT, lhs_low);
5156 __ Or(TMP, TMP, AT);
5157 __ Beqz(TMP, label);
5158 break;
5159 case kCondA:
5160 __ LoadConst32(TMP, imm_high);
5161 __ Bltu(TMP, lhs_high, label);
5162 __ Sltu(TMP, lhs_high, TMP);
5163 __ LoadConst32(AT, imm_low);
5164 __ Sltu(AT, AT, lhs_low);
5165 __ Blt(TMP, AT, label);
5166 break;
5167 }
5168 } else {
5169 switch (cond) {
5170 case kCondEQ:
5171 __ Xor(TMP, lhs_high, rhs_high);
5172 __ Xor(AT, lhs_low, rhs_low);
5173 __ Or(TMP, TMP, AT);
5174 __ Beqz(TMP, label);
5175 break;
5176 case kCondNE:
5177 __ Xor(TMP, lhs_high, rhs_high);
5178 __ Xor(AT, lhs_low, rhs_low);
5179 __ Or(TMP, TMP, AT);
5180 __ Bnez(TMP, label);
5181 break;
5182 case kCondLT:
5183 __ Blt(lhs_high, rhs_high, label);
5184 __ Slt(TMP, rhs_high, lhs_high);
5185 __ Sltu(AT, lhs_low, rhs_low);
5186 __ Blt(TMP, AT, label);
5187 break;
5188 case kCondGE:
5189 __ Blt(rhs_high, lhs_high, label);
5190 __ Slt(TMP, lhs_high, rhs_high);
5191 __ Sltu(AT, lhs_low, rhs_low);
5192 __ Or(TMP, TMP, AT);
5193 __ Beqz(TMP, label);
5194 break;
5195 case kCondLE:
5196 __ Blt(lhs_high, rhs_high, label);
5197 __ Slt(TMP, rhs_high, lhs_high);
5198 __ Sltu(AT, rhs_low, lhs_low);
5199 __ Or(TMP, TMP, AT);
5200 __ Beqz(TMP, label);
5201 break;
5202 case kCondGT:
5203 __ Blt(rhs_high, lhs_high, label);
5204 __ Slt(TMP, lhs_high, rhs_high);
5205 __ Sltu(AT, rhs_low, lhs_low);
5206 __ Blt(TMP, AT, label);
5207 break;
5208 case kCondB:
5209 __ Bltu(lhs_high, rhs_high, label);
5210 __ Sltu(TMP, rhs_high, lhs_high);
5211 __ Sltu(AT, lhs_low, rhs_low);
5212 __ Blt(TMP, AT, label);
5213 break;
5214 case kCondAE:
5215 __ Bltu(rhs_high, lhs_high, label);
5216 __ Sltu(TMP, lhs_high, rhs_high);
5217 __ Sltu(AT, lhs_low, rhs_low);
5218 __ Or(TMP, TMP, AT);
5219 __ Beqz(TMP, label);
5220 break;
5221 case kCondBE:
5222 __ Bltu(lhs_high, rhs_high, label);
5223 __ Sltu(TMP, rhs_high, lhs_high);
5224 __ Sltu(AT, rhs_low, lhs_low);
5225 __ Or(TMP, TMP, AT);
5226 __ Beqz(TMP, label);
5227 break;
5228 case kCondA:
5229 __ Bltu(rhs_high, lhs_high, label);
5230 __ Sltu(TMP, lhs_high, rhs_high);
5231 __ Sltu(AT, rhs_low, lhs_low);
5232 __ Blt(TMP, AT, label);
5233 break;
5234 }
5235 }
5236}
5237
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005238void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5239 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005240 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005241 LocationSummary* locations) {
5242 Register dst = locations->Out().AsRegister<Register>();
5243 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5244 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5245 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005246 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005247 if (isR6) {
5248 switch (cond) {
5249 case kCondEQ:
5250 __ CmpEqS(FTMP, lhs, rhs);
5251 __ Mfc1(dst, FTMP);
5252 __ Andi(dst, dst, 1);
5253 break;
5254 case kCondNE:
5255 __ CmpEqS(FTMP, lhs, rhs);
5256 __ Mfc1(dst, FTMP);
5257 __ Addiu(dst, dst, 1);
5258 break;
5259 case kCondLT:
5260 if (gt_bias) {
5261 __ CmpLtS(FTMP, lhs, rhs);
5262 } else {
5263 __ CmpUltS(FTMP, lhs, rhs);
5264 }
5265 __ Mfc1(dst, FTMP);
5266 __ Andi(dst, dst, 1);
5267 break;
5268 case kCondLE:
5269 if (gt_bias) {
5270 __ CmpLeS(FTMP, lhs, rhs);
5271 } else {
5272 __ CmpUleS(FTMP, lhs, rhs);
5273 }
5274 __ Mfc1(dst, FTMP);
5275 __ Andi(dst, dst, 1);
5276 break;
5277 case kCondGT:
5278 if (gt_bias) {
5279 __ CmpUltS(FTMP, rhs, lhs);
5280 } else {
5281 __ CmpLtS(FTMP, rhs, lhs);
5282 }
5283 __ Mfc1(dst, FTMP);
5284 __ Andi(dst, dst, 1);
5285 break;
5286 case kCondGE:
5287 if (gt_bias) {
5288 __ CmpUleS(FTMP, rhs, lhs);
5289 } else {
5290 __ CmpLeS(FTMP, rhs, lhs);
5291 }
5292 __ Mfc1(dst, FTMP);
5293 __ Andi(dst, dst, 1);
5294 break;
5295 default:
5296 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5297 UNREACHABLE();
5298 }
5299 } else {
5300 switch (cond) {
5301 case kCondEQ:
5302 __ CeqS(0, lhs, rhs);
5303 __ LoadConst32(dst, 1);
5304 __ Movf(dst, ZERO, 0);
5305 break;
5306 case kCondNE:
5307 __ CeqS(0, lhs, rhs);
5308 __ LoadConst32(dst, 1);
5309 __ Movt(dst, ZERO, 0);
5310 break;
5311 case kCondLT:
5312 if (gt_bias) {
5313 __ ColtS(0, lhs, rhs);
5314 } else {
5315 __ CultS(0, lhs, rhs);
5316 }
5317 __ LoadConst32(dst, 1);
5318 __ Movf(dst, ZERO, 0);
5319 break;
5320 case kCondLE:
5321 if (gt_bias) {
5322 __ ColeS(0, lhs, rhs);
5323 } else {
5324 __ CuleS(0, lhs, rhs);
5325 }
5326 __ LoadConst32(dst, 1);
5327 __ Movf(dst, ZERO, 0);
5328 break;
5329 case kCondGT:
5330 if (gt_bias) {
5331 __ CultS(0, rhs, lhs);
5332 } else {
5333 __ ColtS(0, rhs, lhs);
5334 }
5335 __ LoadConst32(dst, 1);
5336 __ Movf(dst, ZERO, 0);
5337 break;
5338 case kCondGE:
5339 if (gt_bias) {
5340 __ CuleS(0, rhs, lhs);
5341 } else {
5342 __ ColeS(0, rhs, lhs);
5343 }
5344 __ LoadConst32(dst, 1);
5345 __ Movf(dst, ZERO, 0);
5346 break;
5347 default:
5348 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5349 UNREACHABLE();
5350 }
5351 }
5352 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005353 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005354 if (isR6) {
5355 switch (cond) {
5356 case kCondEQ:
5357 __ CmpEqD(FTMP, lhs, rhs);
5358 __ Mfc1(dst, FTMP);
5359 __ Andi(dst, dst, 1);
5360 break;
5361 case kCondNE:
5362 __ CmpEqD(FTMP, lhs, rhs);
5363 __ Mfc1(dst, FTMP);
5364 __ Addiu(dst, dst, 1);
5365 break;
5366 case kCondLT:
5367 if (gt_bias) {
5368 __ CmpLtD(FTMP, lhs, rhs);
5369 } else {
5370 __ CmpUltD(FTMP, lhs, rhs);
5371 }
5372 __ Mfc1(dst, FTMP);
5373 __ Andi(dst, dst, 1);
5374 break;
5375 case kCondLE:
5376 if (gt_bias) {
5377 __ CmpLeD(FTMP, lhs, rhs);
5378 } else {
5379 __ CmpUleD(FTMP, lhs, rhs);
5380 }
5381 __ Mfc1(dst, FTMP);
5382 __ Andi(dst, dst, 1);
5383 break;
5384 case kCondGT:
5385 if (gt_bias) {
5386 __ CmpUltD(FTMP, rhs, lhs);
5387 } else {
5388 __ CmpLtD(FTMP, rhs, lhs);
5389 }
5390 __ Mfc1(dst, FTMP);
5391 __ Andi(dst, dst, 1);
5392 break;
5393 case kCondGE:
5394 if (gt_bias) {
5395 __ CmpUleD(FTMP, rhs, lhs);
5396 } else {
5397 __ CmpLeD(FTMP, rhs, lhs);
5398 }
5399 __ Mfc1(dst, FTMP);
5400 __ Andi(dst, dst, 1);
5401 break;
5402 default:
5403 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5404 UNREACHABLE();
5405 }
5406 } else {
5407 switch (cond) {
5408 case kCondEQ:
5409 __ CeqD(0, lhs, rhs);
5410 __ LoadConst32(dst, 1);
5411 __ Movf(dst, ZERO, 0);
5412 break;
5413 case kCondNE:
5414 __ CeqD(0, lhs, rhs);
5415 __ LoadConst32(dst, 1);
5416 __ Movt(dst, ZERO, 0);
5417 break;
5418 case kCondLT:
5419 if (gt_bias) {
5420 __ ColtD(0, lhs, rhs);
5421 } else {
5422 __ CultD(0, lhs, rhs);
5423 }
5424 __ LoadConst32(dst, 1);
5425 __ Movf(dst, ZERO, 0);
5426 break;
5427 case kCondLE:
5428 if (gt_bias) {
5429 __ ColeD(0, lhs, rhs);
5430 } else {
5431 __ CuleD(0, lhs, rhs);
5432 }
5433 __ LoadConst32(dst, 1);
5434 __ Movf(dst, ZERO, 0);
5435 break;
5436 case kCondGT:
5437 if (gt_bias) {
5438 __ CultD(0, rhs, lhs);
5439 } else {
5440 __ ColtD(0, rhs, lhs);
5441 }
5442 __ LoadConst32(dst, 1);
5443 __ Movf(dst, ZERO, 0);
5444 break;
5445 case kCondGE:
5446 if (gt_bias) {
5447 __ CuleD(0, rhs, lhs);
5448 } else {
5449 __ ColeD(0, rhs, lhs);
5450 }
5451 __ LoadConst32(dst, 1);
5452 __ Movf(dst, ZERO, 0);
5453 break;
5454 default:
5455 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5456 UNREACHABLE();
5457 }
5458 }
5459 }
5460}
5461
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005462bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5463 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005464 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005465 LocationSummary* input_locations,
5466 int cc) {
5467 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5468 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5469 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005470 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005471 switch (cond) {
5472 case kCondEQ:
5473 __ CeqS(cc, lhs, rhs);
5474 return false;
5475 case kCondNE:
5476 __ CeqS(cc, lhs, rhs);
5477 return true;
5478 case kCondLT:
5479 if (gt_bias) {
5480 __ ColtS(cc, lhs, rhs);
5481 } else {
5482 __ CultS(cc, lhs, rhs);
5483 }
5484 return false;
5485 case kCondLE:
5486 if (gt_bias) {
5487 __ ColeS(cc, lhs, rhs);
5488 } else {
5489 __ CuleS(cc, lhs, rhs);
5490 }
5491 return false;
5492 case kCondGT:
5493 if (gt_bias) {
5494 __ CultS(cc, rhs, lhs);
5495 } else {
5496 __ ColtS(cc, rhs, lhs);
5497 }
5498 return false;
5499 case kCondGE:
5500 if (gt_bias) {
5501 __ CuleS(cc, rhs, lhs);
5502 } else {
5503 __ ColeS(cc, rhs, lhs);
5504 }
5505 return false;
5506 default:
5507 LOG(FATAL) << "Unexpected non-floating-point condition";
5508 UNREACHABLE();
5509 }
5510 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005511 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005512 switch (cond) {
5513 case kCondEQ:
5514 __ CeqD(cc, lhs, rhs);
5515 return false;
5516 case kCondNE:
5517 __ CeqD(cc, lhs, rhs);
5518 return true;
5519 case kCondLT:
5520 if (gt_bias) {
5521 __ ColtD(cc, lhs, rhs);
5522 } else {
5523 __ CultD(cc, lhs, rhs);
5524 }
5525 return false;
5526 case kCondLE:
5527 if (gt_bias) {
5528 __ ColeD(cc, lhs, rhs);
5529 } else {
5530 __ CuleD(cc, lhs, rhs);
5531 }
5532 return false;
5533 case kCondGT:
5534 if (gt_bias) {
5535 __ CultD(cc, rhs, lhs);
5536 } else {
5537 __ ColtD(cc, rhs, lhs);
5538 }
5539 return false;
5540 case kCondGE:
5541 if (gt_bias) {
5542 __ CuleD(cc, rhs, lhs);
5543 } else {
5544 __ ColeD(cc, rhs, lhs);
5545 }
5546 return false;
5547 default:
5548 LOG(FATAL) << "Unexpected non-floating-point condition";
5549 UNREACHABLE();
5550 }
5551 }
5552}
5553
5554bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5555 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005556 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005557 LocationSummary* input_locations,
5558 FRegister dst) {
5559 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5560 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5561 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005562 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005563 switch (cond) {
5564 case kCondEQ:
5565 __ CmpEqS(dst, lhs, rhs);
5566 return false;
5567 case kCondNE:
5568 __ CmpEqS(dst, lhs, rhs);
5569 return true;
5570 case kCondLT:
5571 if (gt_bias) {
5572 __ CmpLtS(dst, lhs, rhs);
5573 } else {
5574 __ CmpUltS(dst, lhs, rhs);
5575 }
5576 return false;
5577 case kCondLE:
5578 if (gt_bias) {
5579 __ CmpLeS(dst, lhs, rhs);
5580 } else {
5581 __ CmpUleS(dst, lhs, rhs);
5582 }
5583 return false;
5584 case kCondGT:
5585 if (gt_bias) {
5586 __ CmpUltS(dst, rhs, lhs);
5587 } else {
5588 __ CmpLtS(dst, rhs, lhs);
5589 }
5590 return false;
5591 case kCondGE:
5592 if (gt_bias) {
5593 __ CmpUleS(dst, rhs, lhs);
5594 } else {
5595 __ CmpLeS(dst, rhs, lhs);
5596 }
5597 return false;
5598 default:
5599 LOG(FATAL) << "Unexpected non-floating-point condition";
5600 UNREACHABLE();
5601 }
5602 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005603 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005604 switch (cond) {
5605 case kCondEQ:
5606 __ CmpEqD(dst, lhs, rhs);
5607 return false;
5608 case kCondNE:
5609 __ CmpEqD(dst, lhs, rhs);
5610 return true;
5611 case kCondLT:
5612 if (gt_bias) {
5613 __ CmpLtD(dst, lhs, rhs);
5614 } else {
5615 __ CmpUltD(dst, lhs, rhs);
5616 }
5617 return false;
5618 case kCondLE:
5619 if (gt_bias) {
5620 __ CmpLeD(dst, lhs, rhs);
5621 } else {
5622 __ CmpUleD(dst, lhs, rhs);
5623 }
5624 return false;
5625 case kCondGT:
5626 if (gt_bias) {
5627 __ CmpUltD(dst, rhs, lhs);
5628 } else {
5629 __ CmpLtD(dst, rhs, lhs);
5630 }
5631 return false;
5632 case kCondGE:
5633 if (gt_bias) {
5634 __ CmpUleD(dst, rhs, lhs);
5635 } else {
5636 __ CmpLeD(dst, rhs, lhs);
5637 }
5638 return false;
5639 default:
5640 LOG(FATAL) << "Unexpected non-floating-point condition";
5641 UNREACHABLE();
5642 }
5643 }
5644}
5645
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005646void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5647 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005648 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005649 LocationSummary* locations,
5650 MipsLabel* label) {
5651 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5652 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5653 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005654 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005655 if (isR6) {
5656 switch (cond) {
5657 case kCondEQ:
5658 __ CmpEqS(FTMP, lhs, rhs);
5659 __ Bc1nez(FTMP, label);
5660 break;
5661 case kCondNE:
5662 __ CmpEqS(FTMP, lhs, rhs);
5663 __ Bc1eqz(FTMP, label);
5664 break;
5665 case kCondLT:
5666 if (gt_bias) {
5667 __ CmpLtS(FTMP, lhs, rhs);
5668 } else {
5669 __ CmpUltS(FTMP, lhs, rhs);
5670 }
5671 __ Bc1nez(FTMP, label);
5672 break;
5673 case kCondLE:
5674 if (gt_bias) {
5675 __ CmpLeS(FTMP, lhs, rhs);
5676 } else {
5677 __ CmpUleS(FTMP, lhs, rhs);
5678 }
5679 __ Bc1nez(FTMP, label);
5680 break;
5681 case kCondGT:
5682 if (gt_bias) {
5683 __ CmpUltS(FTMP, rhs, lhs);
5684 } else {
5685 __ CmpLtS(FTMP, rhs, lhs);
5686 }
5687 __ Bc1nez(FTMP, label);
5688 break;
5689 case kCondGE:
5690 if (gt_bias) {
5691 __ CmpUleS(FTMP, rhs, lhs);
5692 } else {
5693 __ CmpLeS(FTMP, rhs, lhs);
5694 }
5695 __ Bc1nez(FTMP, label);
5696 break;
5697 default:
5698 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005699 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005700 }
5701 } else {
5702 switch (cond) {
5703 case kCondEQ:
5704 __ CeqS(0, lhs, rhs);
5705 __ Bc1t(0, label);
5706 break;
5707 case kCondNE:
5708 __ CeqS(0, lhs, rhs);
5709 __ Bc1f(0, label);
5710 break;
5711 case kCondLT:
5712 if (gt_bias) {
5713 __ ColtS(0, lhs, rhs);
5714 } else {
5715 __ CultS(0, lhs, rhs);
5716 }
5717 __ Bc1t(0, label);
5718 break;
5719 case kCondLE:
5720 if (gt_bias) {
5721 __ ColeS(0, lhs, rhs);
5722 } else {
5723 __ CuleS(0, lhs, rhs);
5724 }
5725 __ Bc1t(0, label);
5726 break;
5727 case kCondGT:
5728 if (gt_bias) {
5729 __ CultS(0, rhs, lhs);
5730 } else {
5731 __ ColtS(0, rhs, lhs);
5732 }
5733 __ Bc1t(0, label);
5734 break;
5735 case kCondGE:
5736 if (gt_bias) {
5737 __ CuleS(0, rhs, lhs);
5738 } else {
5739 __ ColeS(0, rhs, lhs);
5740 }
5741 __ Bc1t(0, label);
5742 break;
5743 default:
5744 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005745 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005746 }
5747 }
5748 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005749 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005750 if (isR6) {
5751 switch (cond) {
5752 case kCondEQ:
5753 __ CmpEqD(FTMP, lhs, rhs);
5754 __ Bc1nez(FTMP, label);
5755 break;
5756 case kCondNE:
5757 __ CmpEqD(FTMP, lhs, rhs);
5758 __ Bc1eqz(FTMP, label);
5759 break;
5760 case kCondLT:
5761 if (gt_bias) {
5762 __ CmpLtD(FTMP, lhs, rhs);
5763 } else {
5764 __ CmpUltD(FTMP, lhs, rhs);
5765 }
5766 __ Bc1nez(FTMP, label);
5767 break;
5768 case kCondLE:
5769 if (gt_bias) {
5770 __ CmpLeD(FTMP, lhs, rhs);
5771 } else {
5772 __ CmpUleD(FTMP, lhs, rhs);
5773 }
5774 __ Bc1nez(FTMP, label);
5775 break;
5776 case kCondGT:
5777 if (gt_bias) {
5778 __ CmpUltD(FTMP, rhs, lhs);
5779 } else {
5780 __ CmpLtD(FTMP, rhs, lhs);
5781 }
5782 __ Bc1nez(FTMP, label);
5783 break;
5784 case kCondGE:
5785 if (gt_bias) {
5786 __ CmpUleD(FTMP, rhs, lhs);
5787 } else {
5788 __ CmpLeD(FTMP, rhs, lhs);
5789 }
5790 __ Bc1nez(FTMP, label);
5791 break;
5792 default:
5793 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005794 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005795 }
5796 } else {
5797 switch (cond) {
5798 case kCondEQ:
5799 __ CeqD(0, lhs, rhs);
5800 __ Bc1t(0, label);
5801 break;
5802 case kCondNE:
5803 __ CeqD(0, lhs, rhs);
5804 __ Bc1f(0, label);
5805 break;
5806 case kCondLT:
5807 if (gt_bias) {
5808 __ ColtD(0, lhs, rhs);
5809 } else {
5810 __ CultD(0, lhs, rhs);
5811 }
5812 __ Bc1t(0, label);
5813 break;
5814 case kCondLE:
5815 if (gt_bias) {
5816 __ ColeD(0, lhs, rhs);
5817 } else {
5818 __ CuleD(0, lhs, rhs);
5819 }
5820 __ Bc1t(0, label);
5821 break;
5822 case kCondGT:
5823 if (gt_bias) {
5824 __ CultD(0, rhs, lhs);
5825 } else {
5826 __ ColtD(0, rhs, lhs);
5827 }
5828 __ Bc1t(0, label);
5829 break;
5830 case kCondGE:
5831 if (gt_bias) {
5832 __ CuleD(0, rhs, lhs);
5833 } else {
5834 __ ColeD(0, rhs, lhs);
5835 }
5836 __ Bc1t(0, label);
5837 break;
5838 default:
5839 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005840 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005841 }
5842 }
5843 }
5844}
5845
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005846void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005847 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005848 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005849 MipsLabel* false_target) {
5850 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005851
David Brazdil0debae72015-11-12 18:37:00 +00005852 if (true_target == nullptr && false_target == nullptr) {
5853 // Nothing to do. The code always falls through.
5854 return;
5855 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005856 // Constant condition, statically compared against "true" (integer value 1).
5857 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005858 if (true_target != nullptr) {
5859 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005860 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005861 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005862 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005863 if (false_target != nullptr) {
5864 __ B(false_target);
5865 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005866 }
David Brazdil0debae72015-11-12 18:37:00 +00005867 return;
5868 }
5869
5870 // The following code generates these patterns:
5871 // (1) true_target == nullptr && false_target != nullptr
5872 // - opposite condition true => branch to false_target
5873 // (2) true_target != nullptr && false_target == nullptr
5874 // - condition true => branch to true_target
5875 // (3) true_target != nullptr && false_target != nullptr
5876 // - condition true => branch to true_target
5877 // - branch to false_target
5878 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005879 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005880 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005881 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005882 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005883 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5884 } else {
5885 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5886 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005887 } else {
5888 // The condition instruction has not been materialized, use its inputs as
5889 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005890 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005891 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005892 LocationSummary* locations = cond->GetLocations();
5893 IfCondition if_cond = condition->GetCondition();
5894 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005895
David Brazdil0debae72015-11-12 18:37:00 +00005896 if (true_target == nullptr) {
5897 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005898 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005899 }
5900
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005901 switch (type) {
5902 default:
5903 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5904 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005905 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005906 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5907 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005908 case DataType::Type::kFloat32:
5909 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005910 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5911 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005912 }
5913 }
David Brazdil0debae72015-11-12 18:37:00 +00005914
5915 // If neither branch falls through (case 3), the conditional branch to `true_target`
5916 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5917 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005918 __ B(false_target);
5919 }
5920}
5921
5922void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005923 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005924 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005925 locations->SetInAt(0, Location::RequiresRegister());
5926 }
5927}
5928
5929void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005930 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5931 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5932 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5933 nullptr : codegen_->GetLabelOf(true_successor);
5934 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5935 nullptr : codegen_->GetLabelOf(false_successor);
5936 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005937}
5938
5939void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005940 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005941 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005942 InvokeRuntimeCallingConvention calling_convention;
5943 RegisterSet caller_saves = RegisterSet::Empty();
5944 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5945 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005946 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005947 locations->SetInAt(0, Location::RequiresRegister());
5948 }
5949}
5950
5951void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005952 SlowPathCodeMIPS* slow_path =
5953 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005954 GenerateTestAndBranch(deoptimize,
5955 /* condition_input_index */ 0,
5956 slow_path->GetEntryLabel(),
5957 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005958}
5959
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005960// This function returns true if a conditional move can be generated for HSelect.
5961// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5962// branches and regular moves.
5963//
5964// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5965//
5966// While determining feasibility of a conditional move and setting inputs/outputs
5967// are two distinct tasks, this function does both because they share quite a bit
5968// of common logic.
5969static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5970 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5971 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5972 HCondition* condition = cond->AsCondition();
5973
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005974 DataType::Type cond_type =
5975 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5976 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005977
5978 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5979 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5980 bool is_true_value_zero_constant =
5981 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5982 bool is_false_value_zero_constant =
5983 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5984
5985 bool can_move_conditionally = false;
5986 bool use_const_for_false_in = false;
5987 bool use_const_for_true_in = false;
5988
5989 if (!cond->IsConstant()) {
5990 switch (cond_type) {
5991 default:
5992 switch (dst_type) {
5993 default:
5994 // Moving int on int condition.
5995 if (is_r6) {
5996 if (is_true_value_zero_constant) {
5997 // seleqz out_reg, false_reg, cond_reg
5998 can_move_conditionally = true;
5999 use_const_for_true_in = true;
6000 } else if (is_false_value_zero_constant) {
6001 // selnez out_reg, true_reg, cond_reg
6002 can_move_conditionally = true;
6003 use_const_for_false_in = true;
6004 } else if (materialized) {
6005 // Not materializing unmaterialized int conditions
6006 // to keep the instruction count low.
6007 // selnez AT, true_reg, cond_reg
6008 // seleqz TMP, false_reg, cond_reg
6009 // or out_reg, AT, TMP
6010 can_move_conditionally = true;
6011 }
6012 } else {
6013 // movn out_reg, true_reg/ZERO, cond_reg
6014 can_move_conditionally = true;
6015 use_const_for_true_in = is_true_value_zero_constant;
6016 }
6017 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006018 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006019 // Moving long on int condition.
6020 if (is_r6) {
6021 if (is_true_value_zero_constant) {
6022 // seleqz out_reg_lo, false_reg_lo, cond_reg
6023 // seleqz out_reg_hi, false_reg_hi, cond_reg
6024 can_move_conditionally = true;
6025 use_const_for_true_in = true;
6026 } else if (is_false_value_zero_constant) {
6027 // selnez out_reg_lo, true_reg_lo, cond_reg
6028 // selnez out_reg_hi, true_reg_hi, cond_reg
6029 can_move_conditionally = true;
6030 use_const_for_false_in = true;
6031 }
6032 // Other long conditional moves would generate 6+ instructions,
6033 // which is too many.
6034 } else {
6035 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
6036 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
6037 can_move_conditionally = true;
6038 use_const_for_true_in = is_true_value_zero_constant;
6039 }
6040 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006041 case DataType::Type::kFloat32:
6042 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006043 // Moving float/double on int condition.
6044 if (is_r6) {
6045 if (materialized) {
6046 // Not materializing unmaterialized int conditions
6047 // to keep the instruction count low.
6048 can_move_conditionally = true;
6049 if (is_true_value_zero_constant) {
6050 // sltu TMP, ZERO, cond_reg
6051 // mtc1 TMP, temp_cond_reg
6052 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6053 use_const_for_true_in = true;
6054 } else if (is_false_value_zero_constant) {
6055 // sltu TMP, ZERO, cond_reg
6056 // mtc1 TMP, temp_cond_reg
6057 // selnez.fmt out_reg, true_reg, temp_cond_reg
6058 use_const_for_false_in = true;
6059 } else {
6060 // sltu TMP, ZERO, cond_reg
6061 // mtc1 TMP, temp_cond_reg
6062 // sel.fmt temp_cond_reg, false_reg, true_reg
6063 // mov.fmt out_reg, temp_cond_reg
6064 }
6065 }
6066 } else {
6067 // movn.fmt out_reg, true_reg, cond_reg
6068 can_move_conditionally = true;
6069 }
6070 break;
6071 }
6072 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006073 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006074 // We don't materialize long comparison now
6075 // and use conditional branches instead.
6076 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006077 case DataType::Type::kFloat32:
6078 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006079 switch (dst_type) {
6080 default:
6081 // Moving int on float/double condition.
6082 if (is_r6) {
6083 if (is_true_value_zero_constant) {
6084 // mfc1 TMP, temp_cond_reg
6085 // seleqz out_reg, false_reg, TMP
6086 can_move_conditionally = true;
6087 use_const_for_true_in = true;
6088 } else if (is_false_value_zero_constant) {
6089 // mfc1 TMP, temp_cond_reg
6090 // selnez out_reg, true_reg, TMP
6091 can_move_conditionally = true;
6092 use_const_for_false_in = true;
6093 } else {
6094 // mfc1 TMP, temp_cond_reg
6095 // selnez AT, true_reg, TMP
6096 // seleqz TMP, false_reg, TMP
6097 // or out_reg, AT, TMP
6098 can_move_conditionally = true;
6099 }
6100 } else {
6101 // movt out_reg, true_reg/ZERO, cc
6102 can_move_conditionally = true;
6103 use_const_for_true_in = is_true_value_zero_constant;
6104 }
6105 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006106 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006107 // Moving long on float/double condition.
6108 if (is_r6) {
6109 if (is_true_value_zero_constant) {
6110 // mfc1 TMP, temp_cond_reg
6111 // seleqz out_reg_lo, false_reg_lo, TMP
6112 // seleqz out_reg_hi, false_reg_hi, TMP
6113 can_move_conditionally = true;
6114 use_const_for_true_in = true;
6115 } else if (is_false_value_zero_constant) {
6116 // mfc1 TMP, temp_cond_reg
6117 // selnez out_reg_lo, true_reg_lo, TMP
6118 // selnez out_reg_hi, true_reg_hi, TMP
6119 can_move_conditionally = true;
6120 use_const_for_false_in = true;
6121 }
6122 // Other long conditional moves would generate 6+ instructions,
6123 // which is too many.
6124 } else {
6125 // movt out_reg_lo, true_reg_lo/ZERO, cc
6126 // movt out_reg_hi, true_reg_hi/ZERO, cc
6127 can_move_conditionally = true;
6128 use_const_for_true_in = is_true_value_zero_constant;
6129 }
6130 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006131 case DataType::Type::kFloat32:
6132 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006133 // Moving float/double on float/double condition.
6134 if (is_r6) {
6135 can_move_conditionally = true;
6136 if (is_true_value_zero_constant) {
6137 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6138 use_const_for_true_in = true;
6139 } else if (is_false_value_zero_constant) {
6140 // selnez.fmt out_reg, true_reg, temp_cond_reg
6141 use_const_for_false_in = true;
6142 } else {
6143 // sel.fmt temp_cond_reg, false_reg, true_reg
6144 // mov.fmt out_reg, temp_cond_reg
6145 }
6146 } else {
6147 // movt.fmt out_reg, true_reg, cc
6148 can_move_conditionally = true;
6149 }
6150 break;
6151 }
6152 break;
6153 }
6154 }
6155
6156 if (can_move_conditionally) {
6157 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6158 } else {
6159 DCHECK(!use_const_for_false_in);
6160 DCHECK(!use_const_for_true_in);
6161 }
6162
6163 if (locations_to_set != nullptr) {
6164 if (use_const_for_false_in) {
6165 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6166 } else {
6167 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006168 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006169 ? Location::RequiresFpuRegister()
6170 : Location::RequiresRegister());
6171 }
6172 if (use_const_for_true_in) {
6173 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6174 } else {
6175 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006176 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006177 ? Location::RequiresFpuRegister()
6178 : Location::RequiresRegister());
6179 }
6180 if (materialized) {
6181 locations_to_set->SetInAt(2, Location::RequiresRegister());
6182 }
6183 // On R6 we don't require the output to be the same as the
6184 // first input for conditional moves unlike on R2.
6185 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6186 if (is_out_same_as_first_in) {
6187 locations_to_set->SetOut(Location::SameAsFirstInput());
6188 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006189 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006190 ? Location::RequiresFpuRegister()
6191 : Location::RequiresRegister());
6192 }
6193 }
6194
6195 return can_move_conditionally;
6196}
6197
6198void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6199 LocationSummary* locations = select->GetLocations();
6200 Location dst = locations->Out();
6201 Location src = locations->InAt(1);
6202 Register src_reg = ZERO;
6203 Register src_reg_high = ZERO;
6204 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6205 Register cond_reg = TMP;
6206 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006207 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006208 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006209 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006210
6211 if (IsBooleanValueOrMaterializedCondition(cond)) {
6212 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6213 } else {
6214 HCondition* condition = cond->AsCondition();
6215 LocationSummary* cond_locations = cond->GetLocations();
6216 IfCondition if_cond = condition->GetCondition();
6217 cond_type = condition->InputAt(0)->GetType();
6218 switch (cond_type) {
6219 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006221 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6222 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006223 case DataType::Type::kFloat32:
6224 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006225 cond_inverted = MaterializeFpCompareR2(if_cond,
6226 condition->IsGtBias(),
6227 cond_type,
6228 cond_locations,
6229 cond_cc);
6230 break;
6231 }
6232 }
6233
6234 DCHECK(dst.Equals(locations->InAt(0)));
6235 if (src.IsRegister()) {
6236 src_reg = src.AsRegister<Register>();
6237 } else if (src.IsRegisterPair()) {
6238 src_reg = src.AsRegisterPairLow<Register>();
6239 src_reg_high = src.AsRegisterPairHigh<Register>();
6240 } else if (src.IsConstant()) {
6241 DCHECK(src.GetConstant()->IsZeroBitPattern());
6242 }
6243
6244 switch (cond_type) {
6245 default:
6246 switch (dst_type) {
6247 default:
6248 if (cond_inverted) {
6249 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6250 } else {
6251 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6252 }
6253 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006254 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006255 if (cond_inverted) {
6256 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6257 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6258 } else {
6259 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6260 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6261 }
6262 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006263 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006264 if (cond_inverted) {
6265 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6266 } else {
6267 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6268 }
6269 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006270 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006271 if (cond_inverted) {
6272 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6273 } else {
6274 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6275 }
6276 break;
6277 }
6278 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006279 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006280 LOG(FATAL) << "Unreachable";
6281 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006282 case DataType::Type::kFloat32:
6283 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006284 switch (dst_type) {
6285 default:
6286 if (cond_inverted) {
6287 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6288 } else {
6289 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6290 }
6291 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006292 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006293 if (cond_inverted) {
6294 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6295 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6296 } else {
6297 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6298 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6299 }
6300 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006302 if (cond_inverted) {
6303 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6304 } else {
6305 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6306 }
6307 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006308 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006309 if (cond_inverted) {
6310 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6311 } else {
6312 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6313 }
6314 break;
6315 }
6316 break;
6317 }
6318}
6319
6320void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6321 LocationSummary* locations = select->GetLocations();
6322 Location dst = locations->Out();
6323 Location false_src = locations->InAt(0);
6324 Location true_src = locations->InAt(1);
6325 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6326 Register cond_reg = TMP;
6327 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006328 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006329 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006330 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006331
6332 if (IsBooleanValueOrMaterializedCondition(cond)) {
6333 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6334 } else {
6335 HCondition* condition = cond->AsCondition();
6336 LocationSummary* cond_locations = cond->GetLocations();
6337 IfCondition if_cond = condition->GetCondition();
6338 cond_type = condition->InputAt(0)->GetType();
6339 switch (cond_type) {
6340 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006341 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006342 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6343 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006344 case DataType::Type::kFloat32:
6345 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006346 cond_inverted = MaterializeFpCompareR6(if_cond,
6347 condition->IsGtBias(),
6348 cond_type,
6349 cond_locations,
6350 fcond_reg);
6351 break;
6352 }
6353 }
6354
6355 if (true_src.IsConstant()) {
6356 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6357 }
6358 if (false_src.IsConstant()) {
6359 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6360 }
6361
6362 switch (dst_type) {
6363 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006364 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006365 __ Mfc1(cond_reg, fcond_reg);
6366 }
6367 if (true_src.IsConstant()) {
6368 if (cond_inverted) {
6369 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6370 } else {
6371 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6372 }
6373 } else if (false_src.IsConstant()) {
6374 if (cond_inverted) {
6375 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6376 } else {
6377 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6378 }
6379 } else {
6380 DCHECK_NE(cond_reg, AT);
6381 if (cond_inverted) {
6382 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6383 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6384 } else {
6385 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6386 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6387 }
6388 __ Or(dst.AsRegister<Register>(), AT, TMP);
6389 }
6390 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006391 case DataType::Type::kInt64: {
6392 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006393 __ Mfc1(cond_reg, fcond_reg);
6394 }
6395 Register dst_lo = dst.AsRegisterPairLow<Register>();
6396 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6397 if (true_src.IsConstant()) {
6398 Register src_lo = false_src.AsRegisterPairLow<Register>();
6399 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6400 if (cond_inverted) {
6401 __ Selnez(dst_lo, src_lo, cond_reg);
6402 __ Selnez(dst_hi, src_hi, cond_reg);
6403 } else {
6404 __ Seleqz(dst_lo, src_lo, cond_reg);
6405 __ Seleqz(dst_hi, src_hi, cond_reg);
6406 }
6407 } else {
6408 DCHECK(false_src.IsConstant());
6409 Register src_lo = true_src.AsRegisterPairLow<Register>();
6410 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6411 if (cond_inverted) {
6412 __ Seleqz(dst_lo, src_lo, cond_reg);
6413 __ Seleqz(dst_hi, src_hi, cond_reg);
6414 } else {
6415 __ Selnez(dst_lo, src_lo, cond_reg);
6416 __ Selnez(dst_hi, src_hi, cond_reg);
6417 }
6418 }
6419 break;
6420 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006421 case DataType::Type::kFloat32: {
6422 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006423 // sel*.fmt tests bit 0 of the condition register, account for that.
6424 __ Sltu(TMP, ZERO, cond_reg);
6425 __ Mtc1(TMP, fcond_reg);
6426 }
6427 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6428 if (true_src.IsConstant()) {
6429 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6430 if (cond_inverted) {
6431 __ SelnezS(dst_reg, src_reg, fcond_reg);
6432 } else {
6433 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6434 }
6435 } else if (false_src.IsConstant()) {
6436 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6437 if (cond_inverted) {
6438 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6439 } else {
6440 __ SelnezS(dst_reg, src_reg, fcond_reg);
6441 }
6442 } else {
6443 if (cond_inverted) {
6444 __ SelS(fcond_reg,
6445 true_src.AsFpuRegister<FRegister>(),
6446 false_src.AsFpuRegister<FRegister>());
6447 } else {
6448 __ SelS(fcond_reg,
6449 false_src.AsFpuRegister<FRegister>(),
6450 true_src.AsFpuRegister<FRegister>());
6451 }
6452 __ MovS(dst_reg, fcond_reg);
6453 }
6454 break;
6455 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006456 case DataType::Type::kFloat64: {
6457 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006458 // sel*.fmt tests bit 0 of the condition register, account for that.
6459 __ Sltu(TMP, ZERO, cond_reg);
6460 __ Mtc1(TMP, fcond_reg);
6461 }
6462 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6463 if (true_src.IsConstant()) {
6464 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6465 if (cond_inverted) {
6466 __ SelnezD(dst_reg, src_reg, fcond_reg);
6467 } else {
6468 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6469 }
6470 } else if (false_src.IsConstant()) {
6471 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6472 if (cond_inverted) {
6473 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6474 } else {
6475 __ SelnezD(dst_reg, src_reg, fcond_reg);
6476 }
6477 } else {
6478 if (cond_inverted) {
6479 __ SelD(fcond_reg,
6480 true_src.AsFpuRegister<FRegister>(),
6481 false_src.AsFpuRegister<FRegister>());
6482 } else {
6483 __ SelD(fcond_reg,
6484 false_src.AsFpuRegister<FRegister>(),
6485 true_src.AsFpuRegister<FRegister>());
6486 }
6487 __ MovD(dst_reg, fcond_reg);
6488 }
6489 break;
6490 }
6491 }
6492}
6493
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006494void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006495 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006496 LocationSummary(flag, LocationSummary::kNoCall);
6497 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006498}
6499
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006500void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6501 __ LoadFromOffset(kLoadWord,
6502 flag->GetLocations()->Out().AsRegister<Register>(),
6503 SP,
6504 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006505}
6506
David Brazdil74eb1b22015-12-14 11:44:01 +00006507void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006508 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006509 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006510}
6511
6512void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006513 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6514 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6515 if (is_r6) {
6516 GenConditionalMoveR6(select);
6517 } else {
6518 GenConditionalMoveR2(select);
6519 }
6520 } else {
6521 LocationSummary* locations = select->GetLocations();
6522 MipsLabel false_target;
6523 GenerateTestAndBranch(select,
6524 /* condition_input_index */ 2,
6525 /* true_target */ nullptr,
6526 &false_target);
6527 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6528 __ Bind(&false_target);
6529 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006530}
6531
David Srbecky0cf44932015-12-09 14:09:59 +00006532void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006533 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006534}
6535
David Srbeckyd28f4a02016-03-14 17:14:24 +00006536void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6537 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006538}
6539
6540void CodeGeneratorMIPS::GenerateNop() {
6541 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006542}
6543
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006544void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006545 DataType::Type field_type = field_info.GetFieldType();
6546 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006547 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006548 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006549 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006550 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006551 instruction,
6552 generate_volatile
6553 ? LocationSummary::kCallOnMainOnly
6554 : (object_field_get_with_read_barrier
6555 ? LocationSummary::kCallOnSlowPath
6556 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006557
Alexey Frunzec61c0762017-04-10 13:54:23 -07006558 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6559 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6560 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006561 locations->SetInAt(0, Location::RequiresRegister());
6562 if (generate_volatile) {
6563 InvokeRuntimeCallingConvention calling_convention;
6564 // need A0 to hold base + offset
6565 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006566 if (field_type == DataType::Type::kInt64) {
6567 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006568 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006569 // Use Location::Any() to prevent situations when running out of available fp registers.
6570 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006571 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006572 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006573 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6574 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6575 }
6576 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006577 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006578 locations->SetOut(Location::RequiresFpuRegister());
6579 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006580 // The output overlaps in the case of an object field get with
6581 // read barriers enabled: we do not want the move to overwrite the
6582 // object's location, as we need it to emit the read barrier.
6583 locations->SetOut(Location::RequiresRegister(),
6584 object_field_get_with_read_barrier
6585 ? Location::kOutputOverlap
6586 : Location::kNoOutputOverlap);
6587 }
6588 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6589 // We need a temporary register for the read barrier marking slow
6590 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006591 if (!kBakerReadBarrierThunksEnableForFields) {
6592 locations->AddTemp(Location::RequiresRegister());
6593 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006594 }
6595 }
6596}
6597
6598void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6599 const FieldInfo& field_info,
6600 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006601 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6602 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006603 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006604 Location obj_loc = locations->InAt(0);
6605 Register obj = obj_loc.AsRegister<Register>();
6606 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006607 LoadOperandType load_type = kLoadUnsignedByte;
6608 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006609 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006610 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006611
6612 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006613 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006614 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006615 load_type = kLoadUnsignedByte;
6616 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006617 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006618 load_type = kLoadSignedByte;
6619 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006620 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006621 load_type = kLoadUnsignedHalfword;
6622 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006623 case DataType::Type::kInt16:
6624 load_type = kLoadSignedHalfword;
6625 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006626 case DataType::Type::kInt32:
6627 case DataType::Type::kFloat32:
6628 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006629 load_type = kLoadWord;
6630 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006631 case DataType::Type::kInt64:
6632 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006633 load_type = kLoadDoubleword;
6634 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006635 case DataType::Type::kUint32:
6636 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006637 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006638 LOG(FATAL) << "Unreachable type " << type;
6639 UNREACHABLE();
6640 }
6641
6642 if (is_volatile && load_type == kLoadDoubleword) {
6643 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006644 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006645 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006646 __ LoadFromOffset(kLoadWord,
6647 ZERO,
6648 locations->GetTemp(0).AsRegister<Register>(),
6649 0,
6650 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006651 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006652 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006653 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006654 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006655 if (dst_loc.IsFpuRegister()) {
6656 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006657 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006658 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006659 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006660 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006661 __ StoreToOffset(kStoreWord,
6662 locations->GetTemp(1).AsRegister<Register>(),
6663 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006664 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006665 __ StoreToOffset(kStoreWord,
6666 locations->GetTemp(2).AsRegister<Register>(),
6667 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006668 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006669 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006670 }
6671 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006672 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006673 // /* HeapReference<Object> */ dst = *(obj + offset)
6674 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006675 Location temp_loc =
6676 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006677 // Note that a potential implicit null check is handled in this
6678 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6679 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6680 dst_loc,
6681 obj,
6682 offset,
6683 temp_loc,
6684 /* needs_null_check */ true);
6685 if (is_volatile) {
6686 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6687 }
6688 } else {
6689 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6690 if (is_volatile) {
6691 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6692 }
6693 // If read barriers are enabled, emit read barriers other than
6694 // Baker's using a slow path (and also unpoison the loaded
6695 // reference, if heap poisoning is enabled).
6696 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6697 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006698 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006699 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006700 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006701 DCHECK(dst_loc.IsRegisterPair());
6702 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006703 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006704 DCHECK(dst_loc.IsRegister());
6705 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006706 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006707 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006708 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006709 DCHECK(dst_loc.IsFpuRegister());
6710 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006711 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006712 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006713 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006714 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006715 }
6716 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006717 }
6718
Alexey Frunze15958152017-02-09 19:08:30 -08006719 // Memory barriers, in the case of references, are handled in the
6720 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006721 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006722 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6723 }
6724}
6725
6726void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 DataType::Type field_type = field_info.GetFieldType();
6728 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006729 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006730 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006731 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006732
6733 locations->SetInAt(0, Location::RequiresRegister());
6734 if (generate_volatile) {
6735 InvokeRuntimeCallingConvention calling_convention;
6736 // need A0 to hold base + offset
6737 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006738 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006739 locations->SetInAt(1, Location::RegisterPairLocation(
6740 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6741 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006742 // Use Location::Any() to prevent situations when running out of available fp registers.
6743 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006744 // Pass FP parameters in core registers.
6745 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6746 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6747 }
6748 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006749 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006750 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006751 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006752 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006753 }
6754 }
6755}
6756
6757void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6758 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006759 uint32_t dex_pc,
6760 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006761 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006762 LocationSummary* locations = instruction->GetLocations();
6763 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006764 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006765 StoreOperandType store_type = kStoreByte;
6766 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006767 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006768 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006769 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006770
6771 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006772 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006773 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006774 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006775 store_type = kStoreByte;
6776 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006777 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006778 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006779 store_type = kStoreHalfword;
6780 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006781 case DataType::Type::kInt32:
6782 case DataType::Type::kFloat32:
6783 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006784 store_type = kStoreWord;
6785 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006786 case DataType::Type::kInt64:
6787 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006788 store_type = kStoreDoubleword;
6789 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006790 case DataType::Type::kUint32:
6791 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006792 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006793 LOG(FATAL) << "Unreachable type " << type;
6794 UNREACHABLE();
6795 }
6796
6797 if (is_volatile) {
6798 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6799 }
6800
6801 if (is_volatile && store_type == kStoreDoubleword) {
6802 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006803 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006804 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006805 __ LoadFromOffset(kLoadWord,
6806 ZERO,
6807 locations->GetTemp(0).AsRegister<Register>(),
6808 0,
6809 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006810 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006811 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006812 if (value_location.IsFpuRegister()) {
6813 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6814 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006815 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006816 value_location.AsFpuRegister<FRegister>());
6817 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006818 __ LoadFromOffset(kLoadWord,
6819 locations->GetTemp(1).AsRegister<Register>(),
6820 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006821 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006822 __ LoadFromOffset(kLoadWord,
6823 locations->GetTemp(2).AsRegister<Register>(),
6824 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006825 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006826 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006827 DCHECK(value_location.IsConstant());
6828 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6829 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006830 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6831 locations->GetTemp(1).AsRegister<Register>(),
6832 value);
6833 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006834 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006835 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006836 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6837 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006838 if (value_location.IsConstant()) {
6839 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6840 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006841 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006842 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006843 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006844 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006845 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006846 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006847 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006848 if (kPoisonHeapReferences && needs_write_barrier) {
6849 // Note that in the case where `value` is a null reference,
6850 // we do not enter this block, as a null reference does not
6851 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006852 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006853 __ PoisonHeapReference(TMP, src);
6854 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6855 } else {
6856 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6857 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006858 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006859 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006860 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006861 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006862 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006863 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006864 }
6865 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006866 }
6867
Alexey Frunzec061de12017-02-14 13:27:23 -08006868 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006869 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006870 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006871 }
6872
6873 if (is_volatile) {
6874 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6875 }
6876}
6877
6878void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6879 HandleFieldGet(instruction, instruction->GetFieldInfo());
6880}
6881
6882void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6883 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6884}
6885
6886void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6887 HandleFieldSet(instruction, instruction->GetFieldInfo());
6888}
6889
6890void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006891 HandleFieldSet(instruction,
6892 instruction->GetFieldInfo(),
6893 instruction->GetDexPc(),
6894 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006895}
6896
Alexey Frunze15958152017-02-09 19:08:30 -08006897void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6898 HInstruction* instruction,
6899 Location out,
6900 uint32_t offset,
6901 Location maybe_temp,
6902 ReadBarrierOption read_barrier_option) {
6903 Register out_reg = out.AsRegister<Register>();
6904 if (read_barrier_option == kWithReadBarrier) {
6905 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006906 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6907 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6908 }
Alexey Frunze15958152017-02-09 19:08:30 -08006909 if (kUseBakerReadBarrier) {
6910 // Load with fast path based Baker's read barrier.
6911 // /* HeapReference<Object> */ out = *(out + offset)
6912 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6913 out,
6914 out_reg,
6915 offset,
6916 maybe_temp,
6917 /* needs_null_check */ false);
6918 } else {
6919 // Load with slow path based read barrier.
6920 // Save the value of `out` into `maybe_temp` before overwriting it
6921 // in the following move operation, as we will need it for the
6922 // read barrier below.
6923 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6924 // /* HeapReference<Object> */ out = *(out + offset)
6925 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6926 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6927 }
6928 } else {
6929 // Plain load with no read barrier.
6930 // /* HeapReference<Object> */ out = *(out + offset)
6931 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6932 __ MaybeUnpoisonHeapReference(out_reg);
6933 }
6934}
6935
6936void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6937 HInstruction* instruction,
6938 Location out,
6939 Location obj,
6940 uint32_t offset,
6941 Location maybe_temp,
6942 ReadBarrierOption read_barrier_option) {
6943 Register out_reg = out.AsRegister<Register>();
6944 Register obj_reg = obj.AsRegister<Register>();
6945 if (read_barrier_option == kWithReadBarrier) {
6946 CHECK(kEmitCompilerReadBarrier);
6947 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006948 if (!kBakerReadBarrierThunksEnableForFields) {
6949 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6950 }
Alexey Frunze15958152017-02-09 19:08:30 -08006951 // Load with fast path based Baker's read barrier.
6952 // /* HeapReference<Object> */ out = *(obj + offset)
6953 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6954 out,
6955 obj_reg,
6956 offset,
6957 maybe_temp,
6958 /* needs_null_check */ false);
6959 } else {
6960 // Load with slow path based read barrier.
6961 // /* HeapReference<Object> */ out = *(obj + offset)
6962 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6963 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6964 }
6965 } else {
6966 // Plain load with no read barrier.
6967 // /* HeapReference<Object> */ out = *(obj + offset)
6968 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6969 __ MaybeUnpoisonHeapReference(out_reg);
6970 }
6971}
6972
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006973static inline int GetBakerMarkThunkNumber(Register reg) {
6974 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6975 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6976 return reg - V0;
6977 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6978 return 14 + (reg - S2);
6979 } else if (reg == FP) { // One more.
6980 return 20;
6981 }
6982 LOG(FATAL) << "Unexpected register " << reg;
6983 UNREACHABLE();
6984}
6985
6986static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6987 int num = GetBakerMarkThunkNumber(reg) +
6988 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6989 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6990}
6991
6992static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6993 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6994 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6995}
6996
Alexey Frunze15958152017-02-09 19:08:30 -08006997void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6998 Location root,
6999 Register obj,
7000 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007001 ReadBarrierOption read_barrier_option,
7002 MipsLabel* label_low) {
7003 bool reordering;
7004 if (label_low != nullptr) {
7005 DCHECK_EQ(offset, 0x5678u);
7006 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007007 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007008 if (read_barrier_option == kWithReadBarrier) {
7009 DCHECK(kEmitCompilerReadBarrier);
7010 if (kUseBakerReadBarrier) {
7011 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7012 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007013 if (kBakerReadBarrierThunksEnableForGcRoots) {
7014 // Note that we do not actually check the value of `GetIsGcMarking()`
7015 // to decide whether to mark the loaded GC root or not. Instead, we
7016 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7017 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7018 // vice versa.
7019 //
7020 // We use thunks for the slow path. That thunk checks the reference
7021 // and jumps to the entrypoint if needed.
7022 //
7023 // temp = Thread::Current()->pReadBarrierMarkReg00
7024 // // AKA &art_quick_read_barrier_mark_introspection.
7025 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7026 // if (temp != nullptr) {
7027 // temp = &gc_root_thunk<root_reg>
7028 // root = temp(root)
7029 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007030
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007031 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7032 const int32_t entry_point_offset =
7033 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7034 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
7035 int16_t offset_low = Low16Bits(offset);
7036 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
7037 // extension in lw.
7038 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7039 Register base = short_offset ? obj : TMP;
7040 // Loading the entrypoint does not require a load acquire since it is only changed when
7041 // threads are suspended or running a checkpoint.
7042 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7043 reordering = __ SetReorder(false);
7044 if (!short_offset) {
7045 DCHECK(!label_low);
7046 __ AddUpper(base, obj, offset_high);
7047 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007048 MipsLabel skip_call;
7049 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007050 if (label_low != nullptr) {
7051 DCHECK(short_offset);
7052 __ Bind(label_low);
7053 }
7054 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7055 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
7056 // in delay slot.
7057 if (isR6) {
7058 __ Jialc(T9, thunk_disp);
7059 } else {
7060 __ Addiu(T9, T9, thunk_disp);
7061 __ Jalr(T9);
7062 __ Nop();
7063 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007064 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007065 __ SetReorder(reordering);
7066 } else {
7067 // Note that we do not actually check the value of `GetIsGcMarking()`
7068 // to decide whether to mark the loaded GC root or not. Instead, we
7069 // load into `temp` (T9) the read barrier mark entry point corresponding
7070 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
7071 // is false, and vice versa.
7072 //
7073 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7074 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7075 // if (temp != null) {
7076 // root = temp(root)
7077 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007078
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007079 if (label_low != nullptr) {
7080 reordering = __ SetReorder(false);
7081 __ Bind(label_low);
7082 }
7083 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7084 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7085 if (label_low != nullptr) {
7086 __ SetReorder(reordering);
7087 }
7088 static_assert(
7089 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7090 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7091 "have different sizes.");
7092 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7093 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7094 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08007095
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007096 // Slow path marking the GC root `root`.
7097 Location temp = Location::RegisterLocation(T9);
7098 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007099 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007100 instruction,
7101 root,
7102 /*entrypoint*/ temp);
7103 codegen_->AddSlowPath(slow_path);
7104
7105 const int32_t entry_point_offset =
7106 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
7107 // Loading the entrypoint does not require a load acquire since it is only changed when
7108 // threads are suspended or running a checkpoint.
7109 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7110 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
7111 __ Bind(slow_path->GetExitLabel());
7112 }
Alexey Frunze15958152017-02-09 19:08:30 -08007113 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007114 if (label_low != nullptr) {
7115 reordering = __ SetReorder(false);
7116 __ Bind(label_low);
7117 }
Alexey Frunze15958152017-02-09 19:08:30 -08007118 // GC root loaded through a slow path for read barriers other
7119 // than Baker's.
7120 // /* GcRoot<mirror::Object>* */ root = obj + offset
7121 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007122 if (label_low != nullptr) {
7123 __ SetReorder(reordering);
7124 }
Alexey Frunze15958152017-02-09 19:08:30 -08007125 // /* mirror::Object* */ root = root->Read()
7126 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7127 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007128 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007129 if (label_low != nullptr) {
7130 reordering = __ SetReorder(false);
7131 __ Bind(label_low);
7132 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007133 // Plain GC root load with no read barrier.
7134 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7135 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7136 // Note that GC roots are not affected by heap poisoning, thus we
7137 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007138 if (label_low != nullptr) {
7139 __ SetReorder(reordering);
7140 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007141 }
7142}
7143
Alexey Frunze15958152017-02-09 19:08:30 -08007144void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7145 Location ref,
7146 Register obj,
7147 uint32_t offset,
7148 Location temp,
7149 bool needs_null_check) {
7150 DCHECK(kEmitCompilerReadBarrier);
7151 DCHECK(kUseBakerReadBarrier);
7152
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007153 if (kBakerReadBarrierThunksEnableForFields) {
7154 // Note that we do not actually check the value of `GetIsGcMarking()`
7155 // to decide whether to mark the loaded reference or not. Instead, we
7156 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7157 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7158 // vice versa.
7159 //
7160 // We use thunks for the slow path. That thunk checks the reference
7161 // and jumps to the entrypoint if needed. If the holder is not gray,
7162 // it issues a load-load memory barrier and returns to the original
7163 // reference load.
7164 //
7165 // temp = Thread::Current()->pReadBarrierMarkReg00
7166 // // AKA &art_quick_read_barrier_mark_introspection.
7167 // if (temp != nullptr) {
7168 // temp = &field_array_thunk<holder_reg>
7169 // temp()
7170 // }
7171 // not_gray_return_address:
7172 // // If the offset is too large to fit into the lw instruction, we
7173 // // use an adjusted base register (TMP) here. This register
7174 // // receives bits 16 ... 31 of the offset before the thunk invocation
7175 // // and the thunk benefits from it.
7176 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7177 // gray_return_address:
7178
7179 DCHECK(temp.IsInvalid());
7180 bool isR6 = GetInstructionSetFeatures().IsR6();
7181 int16_t offset_low = Low16Bits(offset);
7182 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7183 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7184 bool reordering = __ SetReorder(false);
7185 const int32_t entry_point_offset =
7186 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7187 // There may have or may have not been a null check if the field offset is smaller than
7188 // the page size.
7189 // There must've been a null check in case it's actually a load from an array.
7190 // We will, however, perform an explicit null check in the thunk as it's easier to
7191 // do it than not.
7192 if (instruction->IsArrayGet()) {
7193 DCHECK(!needs_null_check);
7194 }
7195 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7196 // Loading the entrypoint does not require a load acquire since it is only changed when
7197 // threads are suspended or running a checkpoint.
7198 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7199 Register ref_reg = ref.AsRegister<Register>();
7200 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007201 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007202 if (short_offset) {
7203 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007204 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007205 __ Nop(); // In forbidden slot.
7206 __ Jialc(T9, thunk_disp);
7207 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007208 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007209 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7210 __ Jalr(T9);
7211 __ Nop(); // In delay slot.
7212 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007213 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007214 } else {
7215 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007216 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007217 __ Aui(base, obj, offset_high); // In delay slot.
7218 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007219 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007220 } else {
7221 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007222 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007223 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7224 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007225 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007226 __ Addu(base, base, obj); // In delay slot.
7227 }
7228 }
7229 // /* HeapReference<Object> */ ref = *(obj + offset)
7230 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7231 if (needs_null_check) {
7232 MaybeRecordImplicitNullCheck(instruction);
7233 }
7234 __ MaybeUnpoisonHeapReference(ref_reg);
7235 __ SetReorder(reordering);
7236 return;
7237 }
7238
Alexey Frunze15958152017-02-09 19:08:30 -08007239 // /* HeapReference<Object> */ ref = *(obj + offset)
7240 Location no_index = Location::NoLocation();
7241 ScaleFactor no_scale_factor = TIMES_1;
7242 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7243 ref,
7244 obj,
7245 offset,
7246 no_index,
7247 no_scale_factor,
7248 temp,
7249 needs_null_check);
7250}
7251
7252void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7253 Location ref,
7254 Register obj,
7255 uint32_t data_offset,
7256 Location index,
7257 Location temp,
7258 bool needs_null_check) {
7259 DCHECK(kEmitCompilerReadBarrier);
7260 DCHECK(kUseBakerReadBarrier);
7261
7262 static_assert(
7263 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7264 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007265 ScaleFactor scale_factor = TIMES_4;
7266
7267 if (kBakerReadBarrierThunksEnableForArrays) {
7268 // Note that we do not actually check the value of `GetIsGcMarking()`
7269 // to decide whether to mark the loaded reference or not. Instead, we
7270 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7271 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7272 // vice versa.
7273 //
7274 // We use thunks for the slow path. That thunk checks the reference
7275 // and jumps to the entrypoint if needed. If the holder is not gray,
7276 // it issues a load-load memory barrier and returns to the original
7277 // reference load.
7278 //
7279 // temp = Thread::Current()->pReadBarrierMarkReg00
7280 // // AKA &art_quick_read_barrier_mark_introspection.
7281 // if (temp != nullptr) {
7282 // temp = &field_array_thunk<holder_reg>
7283 // temp()
7284 // }
7285 // not_gray_return_address:
7286 // // The element address is pre-calculated in the TMP register before the
7287 // // thunk invocation and the thunk benefits from it.
7288 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7289 // gray_return_address:
7290
7291 DCHECK(temp.IsInvalid());
7292 DCHECK(index.IsValid());
7293 bool reordering = __ SetReorder(false);
7294 const int32_t entry_point_offset =
7295 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7296 // We will not do the explicit null check in the thunk as some form of a null check
7297 // must've been done earlier.
7298 DCHECK(!needs_null_check);
7299 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7300 // Loading the entrypoint does not require a load acquire since it is only changed when
7301 // threads are suspended or running a checkpoint.
7302 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7303 Register ref_reg = ref.AsRegister<Register>();
7304 Register index_reg = index.IsRegisterPair()
7305 ? index.AsRegisterPairLow<Register>()
7306 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007307 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007308 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007309 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007310 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7311 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007312 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007313 } else {
7314 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007315 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007316 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7317 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007318 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007319 __ Addu(TMP, TMP, obj); // In delay slot.
7320 }
7321 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7322 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7323 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7324 __ MaybeUnpoisonHeapReference(ref_reg);
7325 __ SetReorder(reordering);
7326 return;
7327 }
7328
Alexey Frunze15958152017-02-09 19:08:30 -08007329 // /* HeapReference<Object> */ ref =
7330 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007331 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7332 ref,
7333 obj,
7334 data_offset,
7335 index,
7336 scale_factor,
7337 temp,
7338 needs_null_check);
7339}
7340
7341void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7342 Location ref,
7343 Register obj,
7344 uint32_t offset,
7345 Location index,
7346 ScaleFactor scale_factor,
7347 Location temp,
7348 bool needs_null_check,
7349 bool always_update_field) {
7350 DCHECK(kEmitCompilerReadBarrier);
7351 DCHECK(kUseBakerReadBarrier);
7352
7353 // In slow path based read barriers, the read barrier call is
7354 // inserted after the original load. However, in fast path based
7355 // Baker's read barriers, we need to perform the load of
7356 // mirror::Object::monitor_ *before* the original reference load.
7357 // This load-load ordering is required by the read barrier.
7358 // The fast path/slow path (for Baker's algorithm) should look like:
7359 //
7360 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7361 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7362 // HeapReference<Object> ref = *src; // Original reference load.
7363 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7364 // if (is_gray) {
7365 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7366 // }
7367 //
7368 // Note: the original implementation in ReadBarrier::Barrier is
7369 // slightly more complex as it performs additional checks that we do
7370 // not do here for performance reasons.
7371
7372 Register ref_reg = ref.AsRegister<Register>();
7373 Register temp_reg = temp.AsRegister<Register>();
7374 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7375
7376 // /* int32_t */ monitor = obj->monitor_
7377 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7378 if (needs_null_check) {
7379 MaybeRecordImplicitNullCheck(instruction);
7380 }
7381 // /* LockWord */ lock_word = LockWord(monitor)
7382 static_assert(sizeof(LockWord) == sizeof(int32_t),
7383 "art::LockWord and int32_t have different sizes.");
7384
7385 __ Sync(0); // Barrier to prevent load-load reordering.
7386
7387 // The actual reference load.
7388 if (index.IsValid()) {
7389 // Load types involving an "index": ArrayGet,
7390 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7391 // intrinsics.
7392 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7393 if (index.IsConstant()) {
7394 size_t computed_offset =
7395 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7396 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7397 } else {
7398 // Handle the special case of the
7399 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7400 // intrinsics, which use a register pair as index ("long
7401 // offset"), of which only the low part contains data.
7402 Register index_reg = index.IsRegisterPair()
7403 ? index.AsRegisterPairLow<Register>()
7404 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007405 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007406 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7407 }
7408 } else {
7409 // /* HeapReference<Object> */ ref = *(obj + offset)
7410 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7411 }
7412
7413 // Object* ref = ref_addr->AsMirrorPtr()
7414 __ MaybeUnpoisonHeapReference(ref_reg);
7415
7416 // Slow path marking the object `ref` when it is gray.
7417 SlowPathCodeMIPS* slow_path;
7418 if (always_update_field) {
7419 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7420 // of the form `obj + field_offset`, where `obj` is a register and
7421 // `field_offset` is a register pair (of which only the lower half
7422 // is used). Thus `offset` and `scale_factor` above are expected
7423 // to be null in this code path.
7424 DCHECK_EQ(offset, 0u);
7425 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007426 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007427 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7428 ref,
7429 obj,
7430 /* field_offset */ index,
7431 temp_reg);
7432 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007433 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007434 }
7435 AddSlowPath(slow_path);
7436
7437 // if (rb_state == ReadBarrier::GrayState())
7438 // ref = ReadBarrier::Mark(ref);
7439 // Given the numeric representation, it's enough to check the low bit of the
7440 // rb_state. We do that by shifting the bit into the sign bit (31) and
7441 // performing a branch on less than zero.
7442 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7443 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7444 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7445 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7446 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7447 __ Bind(slow_path->GetExitLabel());
7448}
7449
7450void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7451 Location out,
7452 Location ref,
7453 Location obj,
7454 uint32_t offset,
7455 Location index) {
7456 DCHECK(kEmitCompilerReadBarrier);
7457
7458 // Insert a slow path based read barrier *after* the reference load.
7459 //
7460 // If heap poisoning is enabled, the unpoisoning of the loaded
7461 // reference will be carried out by the runtime within the slow
7462 // path.
7463 //
7464 // Note that `ref` currently does not get unpoisoned (when heap
7465 // poisoning is enabled), which is alright as the `ref` argument is
7466 // not used by the artReadBarrierSlow entry point.
7467 //
7468 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007469 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007470 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7471 AddSlowPath(slow_path);
7472
7473 __ B(slow_path->GetEntryLabel());
7474 __ Bind(slow_path->GetExitLabel());
7475}
7476
7477void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7478 Location out,
7479 Location ref,
7480 Location obj,
7481 uint32_t offset,
7482 Location index) {
7483 if (kEmitCompilerReadBarrier) {
7484 // Baker's read barriers shall be handled by the fast path
7485 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7486 DCHECK(!kUseBakerReadBarrier);
7487 // If heap poisoning is enabled, unpoisoning will be taken care of
7488 // by the runtime within the slow path.
7489 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7490 } else if (kPoisonHeapReferences) {
7491 __ UnpoisonHeapReference(out.AsRegister<Register>());
7492 }
7493}
7494
7495void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7496 Location out,
7497 Location root) {
7498 DCHECK(kEmitCompilerReadBarrier);
7499
7500 // Insert a slow path based read barrier *after* the GC root load.
7501 //
7502 // Note that GC roots are not affected by heap poisoning, so we do
7503 // not need to do anything special for this here.
7504 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007505 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007506 AddSlowPath(slow_path);
7507
7508 __ B(slow_path->GetEntryLabel());
7509 __ Bind(slow_path->GetExitLabel());
7510}
7511
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007512void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007513 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7514 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007515 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007516 switch (type_check_kind) {
7517 case TypeCheckKind::kExactCheck:
7518 case TypeCheckKind::kAbstractClassCheck:
7519 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007520 case TypeCheckKind::kArrayObjectCheck: {
7521 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7522 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7523 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007524 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007525 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007526 case TypeCheckKind::kArrayCheck:
7527 case TypeCheckKind::kUnresolvedCheck:
7528 case TypeCheckKind::kInterfaceCheck:
7529 call_kind = LocationSummary::kCallOnSlowPath;
7530 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007531 case TypeCheckKind::kBitstringCheck:
7532 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007533 }
7534
Vladimir Markoca6fff82017-10-03 14:49:14 +01007535 LocationSummary* locations =
7536 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007537 if (baker_read_barrier_slow_path) {
7538 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7539 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007540 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007541 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7542 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7543 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7544 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7545 } else {
7546 locations->SetInAt(1, Location::RequiresRegister());
7547 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007548 // The output does overlap inputs.
7549 // Note that TypeCheckSlowPathMIPS uses this register too.
7550 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007551 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007552}
7553
7554void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007555 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007556 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007557 Location obj_loc = locations->InAt(0);
7558 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko175e7862018-03-27 09:03:13 +00007559 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08007560 Location out_loc = locations->Out();
7561 Register out = out_loc.AsRegister<Register>();
7562 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7563 DCHECK_LE(num_temps, 1u);
7564 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007565 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7566 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7567 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7568 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007569 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007570 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007571
7572 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007573 // Avoid this check if we know `obj` is not null.
7574 if (instruction->MustDoNullCheck()) {
7575 __ Move(out, ZERO);
7576 __ Beqz(obj, &done);
7577 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007578
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007579 switch (type_check_kind) {
7580 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007581 ReadBarrierOption read_barrier_option =
7582 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007583 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007584 GenerateReferenceLoadTwoRegisters(instruction,
7585 out_loc,
7586 obj_loc,
7587 class_offset,
7588 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007589 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007590 // Classes must be equal for the instanceof to succeed.
Vladimir Marko175e7862018-03-27 09:03:13 +00007591 __ Xor(out, out, cls.AsRegister<Register>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007592 __ Sltiu(out, out, 1);
7593 break;
7594 }
7595
7596 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007597 ReadBarrierOption read_barrier_option =
7598 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007599 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007600 GenerateReferenceLoadTwoRegisters(instruction,
7601 out_loc,
7602 obj_loc,
7603 class_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 the class is abstract, we eagerly fetch the super class of the
7607 // object to avoid doing a comparison we know will fail.
7608 MipsLabel loop;
7609 __ Bind(&loop);
7610 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007611 GenerateReferenceLoadOneRegister(instruction,
7612 out_loc,
7613 super_offset,
7614 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007615 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007616 // If `out` is null, we use it for the result, and jump to `done`.
7617 __ Beqz(out, &done);
Vladimir Marko175e7862018-03-27 09:03:13 +00007618 __ Bne(out, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007619 __ LoadConst32(out, 1);
7620 break;
7621 }
7622
7623 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007624 ReadBarrierOption read_barrier_option =
7625 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007626 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007627 GenerateReferenceLoadTwoRegisters(instruction,
7628 out_loc,
7629 obj_loc,
7630 class_offset,
7631 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007632 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007633 // Walk over the class hierarchy to find a match.
7634 MipsLabel loop, success;
7635 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00007636 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007637 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007638 GenerateReferenceLoadOneRegister(instruction,
7639 out_loc,
7640 super_offset,
7641 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007642 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007643 __ Bnez(out, &loop);
7644 // If `out` is null, we use it for the result, and jump to `done`.
7645 __ B(&done);
7646 __ Bind(&success);
7647 __ LoadConst32(out, 1);
7648 break;
7649 }
7650
7651 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007652 ReadBarrierOption read_barrier_option =
7653 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007654 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007655 GenerateReferenceLoadTwoRegisters(instruction,
7656 out_loc,
7657 obj_loc,
7658 class_offset,
7659 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007660 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007661 // Do an exact check.
7662 MipsLabel success;
Vladimir Marko175e7862018-03-27 09:03:13 +00007663 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007664 // Otherwise, we need to check that the object's class is a non-primitive array.
7665 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007666 GenerateReferenceLoadOneRegister(instruction,
7667 out_loc,
7668 component_offset,
7669 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007670 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007671 // If `out` is null, we use it for the result, and jump to `done`.
7672 __ Beqz(out, &done);
7673 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7674 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7675 __ Sltiu(out, out, 1);
7676 __ B(&done);
7677 __ Bind(&success);
7678 __ LoadConst32(out, 1);
7679 break;
7680 }
7681
7682 case TypeCheckKind::kArrayCheck: {
7683 // No read barrier since the slow path will retry upon failure.
7684 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007685 GenerateReferenceLoadTwoRegisters(instruction,
7686 out_loc,
7687 obj_loc,
7688 class_offset,
7689 maybe_temp_loc,
7690 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007691 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007692 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7693 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007694 codegen_->AddSlowPath(slow_path);
Vladimir Marko175e7862018-03-27 09:03:13 +00007695 __ Bne(out, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007696 __ LoadConst32(out, 1);
7697 break;
7698 }
7699
7700 case TypeCheckKind::kUnresolvedCheck:
7701 case TypeCheckKind::kInterfaceCheck: {
7702 // Note that we indeed only call on slow path, but we always go
7703 // into the slow path for the unresolved and interface check
7704 // cases.
7705 //
7706 // We cannot directly call the InstanceofNonTrivial runtime
7707 // entry point without resorting to a type checking slow path
7708 // here (i.e. by calling InvokeRuntime directly), as it would
7709 // require to assign fixed registers for the inputs of this
7710 // HInstanceOf instruction (following the runtime calling
7711 // convention), which might be cluttered by the potential first
7712 // read barrier emission at the beginning of this method.
7713 //
7714 // TODO: Introduce a new runtime entry point taking the object
7715 // to test (instead of its class) as argument, and let it deal
7716 // with the read barrier issues. This will let us refactor this
7717 // case of the `switch` code as it was previously (with a direct
7718 // call to the runtime not using a type checking slow path).
7719 // This should also be beneficial for the other cases above.
7720 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007721 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7722 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007723 codegen_->AddSlowPath(slow_path);
7724 __ B(slow_path->GetEntryLabel());
7725 break;
7726 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007727
7728 case TypeCheckKind::kBitstringCheck: {
7729 // /* HeapReference<Class> */ temp = obj->klass_
7730 GenerateReferenceLoadTwoRegisters(instruction,
7731 out_loc,
7732 obj_loc,
7733 class_offset,
7734 maybe_temp_loc,
7735 kWithoutReadBarrier);
7736
7737 GenerateBitstringTypeCheckCompare(instruction, out);
7738 __ Sltiu(out, out, 1);
7739 break;
7740 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007741 }
7742
7743 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007744
7745 if (slow_path != nullptr) {
7746 __ Bind(slow_path->GetExitLabel());
7747 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007748}
7749
7750void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007751 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007752 locations->SetOut(Location::ConstantLocation(constant));
7753}
7754
7755void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7756 // Will be generated at use site.
7757}
7758
7759void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007760 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007761 locations->SetOut(Location::ConstantLocation(constant));
7762}
7763
7764void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7765 // Will be generated at use site.
7766}
7767
7768void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7769 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7770 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7771}
7772
7773void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7774 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007775 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007776 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007777 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007778}
7779
7780void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7781 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7782 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007783 Location receiver = invoke->GetLocations()->InAt(0);
7784 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007785 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007786
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007787 // temp = object->GetClass();
7788 if (receiver.IsStackSlot()) {
7789 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7790 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7791 } else {
7792 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7793 }
7794 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007795 // Instead of simply (possibly) unpoisoning `temp` here, we should
7796 // emit a read barrier for the previous class reference load.
7797 // However this is not required in practice, as this is an
7798 // intermediate/temporary reference and because the current
7799 // concurrent copying collector keeps the from-space memory
7800 // intact/accessible until the end of the marking phase (the
7801 // concurrent copying collector may not in the future).
7802 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007803 __ LoadFromOffset(kLoadWord, temp, temp,
7804 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7805 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007806 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007807 // temp = temp->GetImtEntryAt(method_offset);
7808 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7809 // T9 = temp->GetEntryPoint();
7810 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
Lena Djokic3177e102018-02-28 11:32:40 +01007811 // Set the hidden argument.
7812 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7813 invoke->GetDexMethodIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007814 // T9();
7815 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007816 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007817 DCHECK(!codegen_->IsLeafMethod());
7818 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7819}
7820
7821void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007822 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7823 if (intrinsic.TryDispatch(invoke)) {
7824 return;
7825 }
7826
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007827 HandleInvoke(invoke);
7828}
7829
7830void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007831 // Explicit clinit checks triggered by static invokes must have been pruned by
7832 // art::PrepareForRegisterAllocation.
7833 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007834
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007835 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007836 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7837 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007838
Chris Larsen701566a2015-10-27 15:29:13 -07007839 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7840 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007841 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7842 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7843 }
Chris Larsen701566a2015-10-27 15:29:13 -07007844 return;
7845 }
7846
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007847 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007848
7849 // Add the extra input register if either the dex cache array base register
7850 // or the PC-relative base register for accessing literals is needed.
7851 if (has_extra_input) {
7852 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7853 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007854}
7855
Orion Hodsonac141392017-01-13 11:53:47 +00007856void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7857 HandleInvoke(invoke);
7858}
7859
7860void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7861 codegen_->GenerateInvokePolymorphicCall(invoke);
7862}
7863
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007864void LocationsBuilderMIPS::VisitInvokeCustom(HInvokeCustom* invoke) {
7865 HandleInvoke(invoke);
7866}
7867
7868void InstructionCodeGeneratorMIPS::VisitInvokeCustom(HInvokeCustom* invoke) {
7869 codegen_->GenerateInvokeCustomCall(invoke);
7870}
7871
Chris Larsen701566a2015-10-27 15:29:13 -07007872static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007873 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007874 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7875 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007876 return true;
7877 }
7878 return false;
7879}
7880
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007881HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007882 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007883 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007884 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007885 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007886 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007887 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007888 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007889 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007890 case HLoadString::LoadKind::kJitTableAddress:
7891 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007892 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007893 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007894 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007895 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007896 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007897}
7898
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007899HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7900 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007901 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007902 case HLoadClass::LoadKind::kInvalid:
7903 LOG(FATAL) << "UNREACHABLE";
7904 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007905 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007906 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007907 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007908 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007909 case HLoadClass::LoadKind::kBssEntry:
7910 DCHECK(!Runtime::Current()->UseJitCompilation());
7911 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007912 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007913 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007914 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007915 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007916 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007917 break;
7918 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007919 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007920}
7921
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007922Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7923 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007924 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007925 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007926 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7927 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7928 if (!invoke->GetLocations()->Intrinsified()) {
7929 return location.AsRegister<Register>();
7930 }
7931 // For intrinsics we allow any location, so it may be on the stack.
7932 if (!location.IsRegister()) {
7933 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7934 return temp;
7935 }
7936 // For register locations, check if the register was saved. If so, get it from the stack.
7937 // Note: There is a chance that the register was saved but not overwritten, so we could
7938 // save one load. However, since this is just an intrinsic slow path we prefer this
7939 // simple and more robust approach rather that trying to determine if that's the case.
7940 SlowPathCode* slow_path = GetCurrentSlowPath();
7941 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7942 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7943 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7944 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7945 return temp;
7946 }
7947 return location.AsRegister<Register>();
7948}
7949
Vladimir Markodc151b22015-10-15 18:02:30 +01007950HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7951 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007952 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007953 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007954}
7955
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007956void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7957 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007958 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007959 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007960 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7961 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007962 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007963 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7964 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007965 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7966 : ZERO;
7967
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007968 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007969 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007970 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007971 uint32_t offset =
7972 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007973 __ LoadFromOffset(kLoadWord,
7974 temp.AsRegister<Register>(),
7975 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007976 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007977 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007978 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007979 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007980 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007981 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007982 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7983 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007984 PcRelativePatchInfo* info_high = NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007985 PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007986 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007987 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007988 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7989 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007990 break;
7991 }
Vladimir Markob066d432018-01-03 13:14:37 +00007992 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007993 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00007994 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
7995 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
7996 Register temp_reg = temp.AsRegister<Register>();
7997 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7998 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
7999 break;
8000 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008001 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008002 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008003 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008004 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
8005 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008006 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008007 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
8008 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008009 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008010 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008011 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
8012 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8013 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008014 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8015 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8016 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008017 }
8018 }
8019
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008020 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008021 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008022 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008023 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008024 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8025 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01008026 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008027 T9,
8028 callee_method.AsRegister<Register>(),
8029 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07008030 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008031 // T9()
8032 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008033 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008034 break;
8035 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008036 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8037
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008038 DCHECK(!IsLeafMethod());
8039}
8040
8041void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00008042 // Explicit clinit checks triggered by static invokes must have been pruned by
8043 // art::PrepareForRegisterAllocation.
8044 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008045
8046 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8047 return;
8048 }
8049
8050 LocationSummary* locations = invoke->GetLocations();
8051 codegen_->GenerateStaticOrDirectCall(invoke,
8052 locations->HasTemps()
8053 ? locations->GetTemp(0)
8054 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008055}
8056
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008057void CodeGeneratorMIPS::GenerateVirtualCall(
8058 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02008059 // Use the calling convention instead of the location of the receiver, as
8060 // intrinsics may have put the receiver in a different register. In the intrinsics
8061 // slow path, the arguments have been moved to the right place, so here we are
8062 // guaranteed that the receiver is the first register of the calling convention.
8063 InvokeDexCallingConvention calling_convention;
8064 Register receiver = calling_convention.GetRegisterAt(0);
8065
Chris Larsen3acee732015-11-18 13:31:08 -08008066 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008067 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8068 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
8069 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07008070 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008071
8072 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02008073 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08008074 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08008075 // Instead of simply (possibly) unpoisoning `temp` here, we should
8076 // emit a read barrier for the previous class reference load.
8077 // However this is not required in practice, as this is an
8078 // intermediate/temporary reference and because the current
8079 // concurrent copying collector keeps the from-space memory
8080 // intact/accessible until the end of the marking phase (the
8081 // concurrent copying collector may not in the future).
8082 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008083 // temp = temp->GetMethodAt(method_offset);
8084 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8085 // T9 = temp->GetEntryPoint();
8086 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
8087 // T9();
8088 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008089 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008090 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08008091}
8092
8093void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
8094 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8095 return;
8096 }
8097
8098 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008099 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008100}
8101
8102void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00008103 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008104 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008105 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008106 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8107 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008108 return;
8109 }
Vladimir Marko41559982017-01-06 14:04:23 +00008110 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008111 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008112 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08008113 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
8114 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07008115 ? LocationSummary::kCallOnSlowPath
8116 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008117 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008118 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
8119 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
8120 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008121 switch (load_kind) {
8122 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008123 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008124 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008125 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008126 case HLoadClass::LoadKind::kJitBootImageAddress:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008127 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008128 break;
8129 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008130 if (has_irreducible_loops) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008131 if (load_kind != HLoadClass::LoadKind::kJitBootImageAddress) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008132 codegen_->ClobberRA();
8133 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008134 break;
8135 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008136 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008137 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008138 locations->SetInAt(0, Location::RequiresRegister());
8139 break;
8140 default:
8141 break;
8142 }
8143 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008144 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
8145 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8146 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01008147 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008148 } else {
8149 // For non-Baker read barriers we have a temp-clobbering call.
8150 }
8151 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008152}
8153
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008154// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8155// move.
8156void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00008157 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008158 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00008159 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01008160 return;
8161 }
Vladimir Marko41559982017-01-06 14:04:23 +00008162 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008163
Vladimir Marko41559982017-01-06 14:04:23 +00008164 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008165 Location out_loc = locations->Out();
8166 Register out = out_loc.AsRegister<Register>();
8167 Register base_or_current_method_reg;
8168 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008169 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008170 switch (load_kind) {
8171 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008172 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008173 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008174 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008175 case HLoadClass::LoadKind::kJitBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008176 base_or_current_method_reg =
8177 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008178 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008179 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008180 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008181 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8182 break;
8183 default:
8184 base_or_current_method_reg = ZERO;
8185 break;
8186 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008187
Alexey Frunze15958152017-02-09 19:08:30 -08008188 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8189 ? kWithoutReadBarrier
8190 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008191 bool generate_null_check = false;
8192 switch (load_kind) {
8193 case HLoadClass::LoadKind::kReferrersClass: {
8194 DCHECK(!cls->CanCallRuntime());
8195 DCHECK(!cls->MustGenerateClinitCheck());
8196 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8197 GenerateGcRootFieldLoad(cls,
8198 out_loc,
8199 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008200 ArtMethod::DeclaringClassOffset().Int32Value(),
8201 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008202 break;
8203 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008204 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008205 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008206 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008207 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008208 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008209 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008210 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008211 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8212 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008213 base_or_current_method_reg);
8214 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008215 break;
8216 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008217 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008218 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008219 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008220 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008221 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008222 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008223 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008224 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8225 out,
8226 base_or_current_method_reg);
8227 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008228 break;
8229 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008230 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008231 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8232 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008233 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8234 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008235 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008236 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008237 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008238 GenerateGcRootFieldLoad(cls,
8239 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008240 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008241 /* placeholder */ 0x5678,
8242 read_barrier_option,
8243 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008244 generate_null_check = true;
8245 break;
8246 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008247 case HLoadClass::LoadKind::kJitBootImageAddress: {
8248 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
8249 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
8250 DCHECK_NE(address, 0u);
8251 if (isR6 || !has_irreducible_loops) {
8252 __ LoadLiteral(out,
8253 base_or_current_method_reg,
8254 codegen_->DeduplicateBootImageAddressLiteral(address));
8255 } else {
8256 __ LoadConst32(out, address);
8257 }
8258 break;
8259 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008260 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008261 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8262 cls->GetTypeIndex(),
8263 cls->GetClass());
8264 bool reordering = __ SetReorder(false);
8265 __ Bind(&info->high_label);
8266 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008267 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008268 GenerateGcRootFieldLoad(cls,
8269 out_loc,
8270 out,
8271 /* placeholder */ 0x5678,
8272 read_barrier_option,
8273 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008274 break;
8275 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008276 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008277 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008278 LOG(FATAL) << "UNREACHABLE";
8279 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008280 }
8281
8282 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8283 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01008284 SlowPathCodeMIPS* slow_path =
8285 new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(cls, cls);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008286 codegen_->AddSlowPath(slow_path);
8287 if (generate_null_check) {
8288 __ Beqz(out, slow_path->GetEntryLabel());
8289 }
8290 if (cls->MustGenerateClinitCheck()) {
8291 GenerateClassInitializationCheck(slow_path, out);
8292 } else {
8293 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008294 }
8295 }
8296}
8297
Orion Hodsondbaa5c72018-05-10 08:22:46 +01008298void LocationsBuilderMIPS::VisitLoadMethodHandle(HLoadMethodHandle* load) {
8299 InvokeRuntimeCallingConvention calling_convention;
8300 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8301 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, loc, loc);
8302}
8303
8304void InstructionCodeGeneratorMIPS::VisitLoadMethodHandle(HLoadMethodHandle* load) {
8305 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
8306}
8307
Orion Hodson18259d72018-04-12 11:18:23 +01008308void LocationsBuilderMIPS::VisitLoadMethodType(HLoadMethodType* load) {
8309 InvokeRuntimeCallingConvention calling_convention;
8310 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8311 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, loc, loc);
8312}
8313
8314void InstructionCodeGeneratorMIPS::VisitLoadMethodType(HLoadMethodType* load) {
8315 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
8316}
8317
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008318static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008319 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008320}
8321
8322void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8323 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008324 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008325 locations->SetOut(Location::RequiresRegister());
8326}
8327
8328void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8329 Register out = load->GetLocations()->Out().AsRegister<Register>();
8330 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8331}
8332
8333void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008334 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008335}
8336
8337void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8338 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8339}
8340
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008341void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008342 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008343 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008344 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008345 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008346 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008347 switch (load_kind) {
8348 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008349 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008350 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008351 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008352 case HLoadString::LoadKind::kJitBootImageAddress:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008353 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008354 break;
8355 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008356 if (has_irreducible_loops) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008357 if (load_kind != HLoadString::LoadKind::kJitBootImageAddress) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008358 codegen_->ClobberRA();
8359 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008360 break;
8361 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008362 FALLTHROUGH_INTENDED;
8363 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008364 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008365 locations->SetInAt(0, Location::RequiresRegister());
8366 break;
8367 default:
8368 break;
8369 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008370 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008371 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008372 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008373 } else {
8374 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008375 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8376 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8377 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01008378 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008379 } else {
8380 // For non-Baker read barriers we have a temp-clobbering call.
8381 }
8382 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008383 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008384}
8385
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008386// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8387// move.
8388void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008389 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008390 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008391 Location out_loc = locations->Out();
8392 Register out = out_loc.AsRegister<Register>();
8393 Register base_or_current_method_reg;
8394 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008395 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008396 switch (load_kind) {
8397 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008398 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008399 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008400 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008401 case HLoadString::LoadKind::kJitBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008402 base_or_current_method_reg =
8403 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008404 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008405 default:
8406 base_or_current_method_reg = ZERO;
8407 break;
8408 }
8409
8410 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008411 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008412 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008413 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008414 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008415 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008416 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008417 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8418 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008419 base_or_current_method_reg);
8420 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008421 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008422 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008423 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008424 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008425 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008426 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008427 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008428 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008429 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008430 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8431 out,
8432 base_or_current_method_reg);
8433 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8434 return;
8435 }
8436 case HLoadString::LoadKind::kBssEntry: {
8437 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8438 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8439 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8440 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8441 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008442 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008443 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008444 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008445 GenerateGcRootFieldLoad(load,
8446 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008447 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008448 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008449 kCompilerReadBarrierOption,
8450 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008451 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008452 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008453 codegen_->AddSlowPath(slow_path);
8454 __ Beqz(out, slow_path->GetEntryLabel());
8455 __ Bind(slow_path->GetExitLabel());
8456 return;
8457 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008458 case HLoadString::LoadKind::kJitBootImageAddress: {
8459 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
8460 DCHECK_NE(address, 0u);
8461 if (isR6 || !has_irreducible_loops) {
8462 __ LoadLiteral(out,
8463 base_or_current_method_reg,
8464 codegen_->DeduplicateBootImageAddressLiteral(address));
8465 } else {
8466 __ LoadConst32(out, address);
8467 }
8468 return;
8469 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008470 case HLoadString::LoadKind::kJitTableAddress: {
8471 CodeGeneratorMIPS::JitPatchInfo* info =
8472 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8473 load->GetStringIndex(),
8474 load->GetString());
8475 bool reordering = __ SetReorder(false);
8476 __ Bind(&info->high_label);
8477 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008478 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008479 GenerateGcRootFieldLoad(load,
8480 out_loc,
8481 out,
8482 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008483 kCompilerReadBarrierOption,
8484 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008485 return;
8486 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008487 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008488 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008489 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008490
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008491 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008492 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008493 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008494 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008495 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008496 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8497 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008498}
8499
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008500void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008501 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008502 locations->SetOut(Location::ConstantLocation(constant));
8503}
8504
8505void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8506 // Will be generated at use site.
8507}
8508
8509void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008510 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8511 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008512 InvokeRuntimeCallingConvention calling_convention;
8513 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8514}
8515
8516void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8517 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008518 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008519 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8520 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008521 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008522 }
8523 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8524}
8525
8526void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8527 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008528 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008529 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008530 case DataType::Type::kInt32:
8531 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008532 locations->SetInAt(0, Location::RequiresRegister());
8533 locations->SetInAt(1, Location::RequiresRegister());
8534 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8535 break;
8536
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008537 case DataType::Type::kFloat32:
8538 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008539 locations->SetInAt(0, Location::RequiresFpuRegister());
8540 locations->SetInAt(1, Location::RequiresFpuRegister());
8541 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8542 break;
8543
8544 default:
8545 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8546 }
8547}
8548
8549void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008550 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008551 LocationSummary* locations = instruction->GetLocations();
8552 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8553
8554 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008555 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008556 Register dst = locations->Out().AsRegister<Register>();
8557 Register lhs = locations->InAt(0).AsRegister<Register>();
8558 Register rhs = locations->InAt(1).AsRegister<Register>();
8559
8560 if (isR6) {
8561 __ MulR6(dst, lhs, rhs);
8562 } else {
8563 __ MulR2(dst, lhs, rhs);
8564 }
8565 break;
8566 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008567 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008568 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8569 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8570 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8571 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8572 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8573 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8574
8575 // Extra checks to protect caused by the existance of A1_A2.
8576 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8577 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8578 DCHECK_NE(dst_high, lhs_low);
8579 DCHECK_NE(dst_high, rhs_low);
8580
8581 // A_B * C_D
8582 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8583 // dst_lo: [ low(B*D) ]
8584 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8585
8586 if (isR6) {
8587 __ MulR6(TMP, lhs_high, rhs_low);
8588 __ MulR6(dst_high, lhs_low, rhs_high);
8589 __ Addu(dst_high, dst_high, TMP);
8590 __ MuhuR6(TMP, lhs_low, rhs_low);
8591 __ Addu(dst_high, dst_high, TMP);
8592 __ MulR6(dst_low, lhs_low, rhs_low);
8593 } else {
8594 __ MulR2(TMP, lhs_high, rhs_low);
8595 __ MulR2(dst_high, lhs_low, rhs_high);
8596 __ Addu(dst_high, dst_high, TMP);
8597 __ MultuR2(lhs_low, rhs_low);
8598 __ Mfhi(TMP);
8599 __ Addu(dst_high, dst_high, TMP);
8600 __ Mflo(dst_low);
8601 }
8602 break;
8603 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008604 case DataType::Type::kFloat32:
8605 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008606 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8607 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8608 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008609 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008610 __ MulS(dst, lhs, rhs);
8611 } else {
8612 __ MulD(dst, lhs, rhs);
8613 }
8614 break;
8615 }
8616 default:
8617 LOG(FATAL) << "Unexpected mul type " << type;
8618 }
8619}
8620
8621void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8622 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008623 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008624 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008625 case DataType::Type::kInt32:
8626 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008627 locations->SetInAt(0, Location::RequiresRegister());
8628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8629 break;
8630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008631 case DataType::Type::kFloat32:
8632 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008633 locations->SetInAt(0, Location::RequiresFpuRegister());
8634 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8635 break;
8636
8637 default:
8638 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8639 }
8640}
8641
8642void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008643 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008644 LocationSummary* locations = instruction->GetLocations();
8645
8646 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008647 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008648 Register dst = locations->Out().AsRegister<Register>();
8649 Register src = locations->InAt(0).AsRegister<Register>();
8650 __ Subu(dst, ZERO, src);
8651 break;
8652 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008653 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008654 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8655 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8656 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8657 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8658 __ Subu(dst_low, ZERO, src_low);
8659 __ Sltu(TMP, ZERO, dst_low);
8660 __ Subu(dst_high, ZERO, src_high);
8661 __ Subu(dst_high, dst_high, TMP);
8662 break;
8663 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008664 case DataType::Type::kFloat32:
8665 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008666 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8667 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008668 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008669 __ NegS(dst, src);
8670 } else {
8671 __ NegD(dst, src);
8672 }
8673 break;
8674 }
8675 default:
8676 LOG(FATAL) << "Unexpected neg type " << type;
8677 }
8678}
8679
8680void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008681 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8682 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008683 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008684 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008685 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8686 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008687}
8688
8689void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008690 // Note: if heap poisoning is enabled, the entry point takes care
8691 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008692 QuickEntrypointEnum entrypoint =
8693 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8694 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008695 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008696 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008697}
8698
8699void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008700 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8701 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008702 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07008703 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008704 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008705}
8706
8707void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07008708 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
8709 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008710}
8711
8712void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008713 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008714 locations->SetInAt(0, Location::RequiresRegister());
8715 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8716}
8717
8718void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008719 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008720 LocationSummary* locations = instruction->GetLocations();
8721
8722 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008723 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008724 Register dst = locations->Out().AsRegister<Register>();
8725 Register src = locations->InAt(0).AsRegister<Register>();
8726 __ Nor(dst, src, ZERO);
8727 break;
8728 }
8729
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008730 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008731 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8732 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8733 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8734 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8735 __ Nor(dst_high, src_high, ZERO);
8736 __ Nor(dst_low, src_low, ZERO);
8737 break;
8738 }
8739
8740 default:
8741 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8742 }
8743}
8744
8745void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008746 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008747 locations->SetInAt(0, Location::RequiresRegister());
8748 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8749}
8750
8751void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8752 LocationSummary* locations = instruction->GetLocations();
8753 __ Xori(locations->Out().AsRegister<Register>(),
8754 locations->InAt(0).AsRegister<Register>(),
8755 1);
8756}
8757
8758void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008759 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8760 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008761}
8762
Calin Juravle2ae48182016-03-16 14:05:09 +00008763void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8764 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008765 return;
8766 }
8767 Location obj = instruction->GetLocations()->InAt(0);
8768
8769 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008770 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008771}
8772
Calin Juravle2ae48182016-03-16 14:05:09 +00008773void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008774 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008775 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008776
8777 Location obj = instruction->GetLocations()->InAt(0);
8778
8779 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8780}
8781
8782void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008783 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008784}
8785
8786void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8787 HandleBinaryOp(instruction);
8788}
8789
8790void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8791 HandleBinaryOp(instruction);
8792}
8793
8794void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8795 LOG(FATAL) << "Unreachable";
8796}
8797
8798void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008799 if (instruction->GetNext()->IsSuspendCheck() &&
8800 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8801 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8802 // The back edge will generate the suspend check.
8803 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8804 }
8805
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008806 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8807}
8808
8809void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008810 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008811 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8812 if (location.IsStackSlot()) {
8813 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8814 } else if (location.IsDoubleStackSlot()) {
8815 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8816 }
8817 locations->SetOut(location);
8818}
8819
8820void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8821 ATTRIBUTE_UNUSED) {
8822 // Nothing to do, the parameter is already at its location.
8823}
8824
8825void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8826 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008827 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008828 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8829}
8830
8831void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8832 ATTRIBUTE_UNUSED) {
8833 // Nothing to do, the method is already at its location.
8834}
8835
8836void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008837 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008838 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008839 locations->SetInAt(i, Location::Any());
8840 }
8841 locations->SetOut(Location::Any());
8842}
8843
8844void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8845 LOG(FATAL) << "Unreachable";
8846}
8847
8848void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008849 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008850 bool call_rem;
8851 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8852 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8853 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8854 } else {
8855 call_rem = (type != DataType::Type::kInt32);
8856 }
8857 LocationSummary::CallKind call_kind = call_rem
8858 ? LocationSummary::kCallOnMainOnly
8859 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008860 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008861
8862 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008863 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008864 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008865 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008866 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8867 break;
8868
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008869 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008870 if (call_rem) {
8871 InvokeRuntimeCallingConvention calling_convention;
8872 locations->SetInAt(0, Location::RegisterPairLocation(
8873 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8874 locations->SetInAt(1, Location::RegisterPairLocation(
8875 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8876 locations->SetOut(calling_convention.GetReturnLocation(type));
8877 } else {
8878 locations->SetInAt(0, Location::RequiresRegister());
8879 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8880 locations->SetOut(Location::RequiresRegister());
8881 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008882 break;
8883 }
8884
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008885 case DataType::Type::kFloat32:
8886 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008887 InvokeRuntimeCallingConvention calling_convention;
8888 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8889 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8890 locations->SetOut(calling_convention.GetReturnLocation(type));
8891 break;
8892 }
8893
8894 default:
8895 LOG(FATAL) << "Unexpected rem type " << type;
8896 }
8897}
8898
8899void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008900 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008901 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008902
8903 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008904 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008905 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008906 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008907 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008908 if (locations->InAt(1).IsConstant()) {
8909 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8910 if (imm == 0) {
8911 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8912 } else if (imm == 1 || imm == -1) {
8913 DivRemOneOrMinusOne(instruction);
8914 } else {
8915 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8916 DivRemByPowerOfTwo(instruction);
8917 }
8918 } else {
8919 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8920 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8921 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008922 break;
8923 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008924 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008925 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008926 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008927 break;
8928 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008929 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008930 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008931 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008932 break;
8933 }
8934 default:
8935 LOG(FATAL) << "Unexpected rem type " << type;
8936 }
8937}
8938
Aart Bik1f8d51b2018-02-15 10:42:37 -08008939static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
8940 LocationSummary* locations = new (allocator) LocationSummary(minmax);
8941 switch (minmax->GetResultType()) {
8942 case DataType::Type::kInt32:
8943 case DataType::Type::kInt64:
8944 locations->SetInAt(0, Location::RequiresRegister());
8945 locations->SetInAt(1, Location::RequiresRegister());
8946 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8947 break;
8948 case DataType::Type::kFloat32:
8949 case DataType::Type::kFloat64:
8950 locations->SetInAt(0, Location::RequiresFpuRegister());
8951 locations->SetInAt(1, Location::RequiresFpuRegister());
8952 locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap);
8953 break;
8954 default:
8955 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
8956 }
8957}
8958
Aart Bik351df3e2018-03-07 11:54:57 -08008959void InstructionCodeGeneratorMIPS::GenerateMinMaxInt(LocationSummary* locations,
8960 bool is_min,
8961 bool isR6,
8962 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08008963 if (isR6) {
8964 // Some architectures, such as ARM and MIPS (prior to r6), have a
8965 // conditional move instruction which only changes the target
8966 // (output) register if the condition is true (MIPS prior to r6 had
8967 // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions
8968 // always change the target (output) register. If the condition is
8969 // true the output register gets the contents of the "rs" register;
8970 // otherwise, the output register is set to zero. One consequence
8971 // of this is that to implement something like "rd = c==0 ? rs : rt"
8972 // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions.
8973 // After executing this pair of instructions one of the output
8974 // registers from the pair will necessarily contain zero. Then the
8975 // code ORs the output registers from the SELEQZ/SELNEZ instructions
8976 // to get the final result.
8977 //
8978 // The initial test to see if the output register is same as the
8979 // first input register is needed to make sure that value in the
8980 // first input register isn't clobbered before we've finished
8981 // computing the output value. The logic in the corresponding else
8982 // clause performs the same task but makes sure the second input
8983 // register isn't clobbered in the event that it's the same register
8984 // as the output register; the else clause also handles the case
8985 // where the output register is distinct from both the first, and the
8986 // second input registers.
8987 if (type == DataType::Type::kInt64) {
8988 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8989 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8990 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
8991 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
8992 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8993 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8994
8995 MipsLabel compare_done;
8996
8997 if (a_lo == b_lo) {
8998 if (out_lo != a_lo) {
8999 __ Move(out_lo, a_lo);
9000 __ Move(out_hi, a_hi);
9001 }
9002 } else {
9003 __ Slt(TMP, b_hi, a_hi);
9004 __ Bne(b_hi, a_hi, &compare_done);
9005
9006 __ Sltu(TMP, b_lo, a_lo);
9007
9008 __ Bind(&compare_done);
9009
9010 if (is_min) {
9011 __ Seleqz(AT, a_lo, TMP);
9012 __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo
9013 // because at this point we're
9014 // done using a_lo/b_lo.
9015 } else {
9016 __ Selnez(AT, a_lo, TMP);
9017 __ Seleqz(out_lo, b_lo, TMP); // ditto
9018 }
9019 __ Or(out_lo, out_lo, AT);
9020 if (is_min) {
9021 __ Seleqz(AT, a_hi, TMP);
9022 __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
9023 } else {
9024 __ Selnez(AT, a_hi, TMP);
9025 __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
9026 }
9027 __ Or(out_hi, out_hi, AT);
9028 }
9029 } else {
9030 DCHECK_EQ(type, DataType::Type::kInt32);
9031 Register a = locations->InAt(0).AsRegister<Register>();
9032 Register b = locations->InAt(1).AsRegister<Register>();
9033 Register out = locations->Out().AsRegister<Register>();
9034
9035 if (a == b) {
9036 if (out != a) {
9037 __ Move(out, a);
9038 }
9039 } else {
9040 __ Slt(AT, b, a);
9041 if (is_min) {
9042 __ Seleqz(TMP, a, AT);
9043 __ Selnez(AT, b, AT);
9044 } else {
9045 __ Selnez(TMP, a, AT);
9046 __ Seleqz(AT, b, AT);
9047 }
9048 __ Or(out, TMP, AT);
9049 }
9050 }
9051 } else { // !isR6
9052 if (type == DataType::Type::kInt64) {
9053 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9054 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9055 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
9056 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
9057 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9058 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9059
9060 MipsLabel compare_done;
9061
9062 if (a_lo == b_lo) {
9063 if (out_lo != a_lo) {
9064 __ Move(out_lo, a_lo);
9065 __ Move(out_hi, a_hi);
9066 }
9067 } else {
9068 __ Slt(TMP, a_hi, b_hi);
9069 __ Bne(a_hi, b_hi, &compare_done);
9070
9071 __ Sltu(TMP, a_lo, b_lo);
9072
9073 __ Bind(&compare_done);
9074
9075 if (is_min) {
9076 if (out_lo != a_lo) {
9077 __ Movn(out_hi, a_hi, TMP);
9078 __ Movn(out_lo, a_lo, TMP);
9079 }
9080 if (out_lo != b_lo) {
9081 __ Movz(out_hi, b_hi, TMP);
9082 __ Movz(out_lo, b_lo, TMP);
9083 }
9084 } else {
9085 if (out_lo != a_lo) {
9086 __ Movz(out_hi, a_hi, TMP);
9087 __ Movz(out_lo, a_lo, TMP);
9088 }
9089 if (out_lo != b_lo) {
9090 __ Movn(out_hi, b_hi, TMP);
9091 __ Movn(out_lo, b_lo, TMP);
9092 }
9093 }
9094 }
9095 } else {
9096 DCHECK_EQ(type, DataType::Type::kInt32);
9097 Register a = locations->InAt(0).AsRegister<Register>();
9098 Register b = locations->InAt(1).AsRegister<Register>();
9099 Register out = locations->Out().AsRegister<Register>();
9100
9101 if (a == b) {
9102 if (out != a) {
9103 __ Move(out, a);
9104 }
9105 } else {
9106 __ Slt(AT, a, b);
9107 if (is_min) {
9108 if (out != a) {
9109 __ Movn(out, a, AT);
9110 }
9111 if (out != b) {
9112 __ Movz(out, b, AT);
9113 }
9114 } else {
9115 if (out != a) {
9116 __ Movz(out, a, AT);
9117 }
9118 if (out != b) {
9119 __ Movn(out, b, AT);
9120 }
9121 }
9122 }
9123 }
9124 }
9125}
9126
9127void InstructionCodeGeneratorMIPS::GenerateMinMaxFP(LocationSummary* locations,
9128 bool is_min,
9129 bool isR6,
9130 DataType::Type type) {
9131 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9132 FRegister a = locations->InAt(0).AsFpuRegister<FRegister>();
9133 FRegister b = locations->InAt(1).AsFpuRegister<FRegister>();
9134
9135 if (isR6) {
9136 MipsLabel noNaNs;
9137 MipsLabel done;
9138 FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
9139
9140 // When Java computes min/max it prefers a NaN to a number; the
9141 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
9142 // the inputs is a NaN and the other is a valid number, the MIPS
9143 // instruction will return the number; Java wants the NaN value
9144 // returned. This is why there is extra logic preceding the use of
9145 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
9146 // NaN, return the NaN, otherwise return the min/max.
9147 if (type == DataType::Type::kFloat64) {
9148 __ CmpUnD(FTMP, a, b);
9149 __ Bc1eqz(FTMP, &noNaNs);
9150
9151 // One of the inputs is a NaN
9152 __ CmpEqD(ftmp, a, a);
9153 // If a == a then b is the NaN, otherwise a is the NaN.
9154 __ SelD(ftmp, a, b);
9155
9156 if (ftmp != out) {
9157 __ MovD(out, ftmp);
9158 }
9159
9160 __ B(&done);
9161
9162 __ Bind(&noNaNs);
9163
9164 if (is_min) {
9165 __ MinD(out, a, b);
9166 } else {
9167 __ MaxD(out, a, b);
9168 }
9169 } else {
9170 DCHECK_EQ(type, DataType::Type::kFloat32);
9171 __ CmpUnS(FTMP, a, b);
9172 __ Bc1eqz(FTMP, &noNaNs);
9173
9174 // One of the inputs is a NaN
9175 __ CmpEqS(ftmp, a, a);
9176 // If a == a then b is the NaN, otherwise a is the NaN.
9177 __ SelS(ftmp, a, b);
9178
9179 if (ftmp != out) {
9180 __ MovS(out, ftmp);
9181 }
9182
9183 __ B(&done);
9184
9185 __ Bind(&noNaNs);
9186
9187 if (is_min) {
9188 __ MinS(out, a, b);
9189 } else {
9190 __ MaxS(out, a, b);
9191 }
9192 }
9193
9194 __ Bind(&done);
9195
9196 } else { // !isR6
9197 MipsLabel ordered;
9198 MipsLabel compare;
9199 MipsLabel select;
9200 MipsLabel done;
9201
9202 if (type == DataType::Type::kFloat64) {
9203 __ CunD(a, b);
9204 } else {
9205 DCHECK_EQ(type, DataType::Type::kFloat32);
9206 __ CunS(a, b);
9207 }
9208 __ Bc1f(&ordered);
9209
9210 // a or b (or both) is a NaN. Return one, which is a NaN.
9211 if (type == DataType::Type::kFloat64) {
9212 __ CeqD(b, b);
9213 } else {
9214 __ CeqS(b, b);
9215 }
9216 __ B(&select);
9217
9218 __ Bind(&ordered);
9219
9220 // Neither is a NaN.
9221 // a == b? (-0.0 compares equal with +0.0)
9222 // If equal, handle zeroes, else compare further.
9223 if (type == DataType::Type::kFloat64) {
9224 __ CeqD(a, b);
9225 } else {
9226 __ CeqS(a, b);
9227 }
9228 __ Bc1f(&compare);
9229
9230 // a == b either bit for bit or one is -0.0 and the other is +0.0.
9231 if (type == DataType::Type::kFloat64) {
9232 __ MoveFromFpuHigh(TMP, a);
9233 __ MoveFromFpuHigh(AT, b);
9234 } else {
9235 __ Mfc1(TMP, a);
9236 __ Mfc1(AT, b);
9237 }
9238
9239 if (is_min) {
9240 // -0.0 prevails over +0.0.
9241 __ Or(TMP, TMP, AT);
9242 } else {
9243 // +0.0 prevails over -0.0.
9244 __ And(TMP, TMP, AT);
9245 }
9246
9247 if (type == DataType::Type::kFloat64) {
9248 __ Mfc1(AT, a);
9249 __ Mtc1(AT, out);
9250 __ MoveToFpuHigh(TMP, out);
9251 } else {
9252 __ Mtc1(TMP, out);
9253 }
9254 __ B(&done);
9255
9256 __ Bind(&compare);
9257
9258 if (type == DataType::Type::kFloat64) {
9259 if (is_min) {
9260 // return (a <= b) ? a : b;
9261 __ ColeD(a, b);
9262 } else {
9263 // return (a >= b) ? a : b;
9264 __ ColeD(b, a); // b <= a
9265 }
9266 } else {
9267 if (is_min) {
9268 // return (a <= b) ? a : b;
9269 __ ColeS(a, b);
9270 } else {
9271 // return (a >= b) ? a : b;
9272 __ ColeS(b, a); // b <= a
9273 }
9274 }
9275
9276 __ Bind(&select);
9277
9278 if (type == DataType::Type::kFloat64) {
9279 __ MovtD(out, a);
9280 __ MovfD(out, b);
9281 } else {
9282 __ MovtS(out, a);
9283 __ MovfS(out, b);
9284 }
9285
9286 __ Bind(&done);
9287 }
9288}
9289
Aart Bik351df3e2018-03-07 11:54:57 -08009290void InstructionCodeGeneratorMIPS::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
9291 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9292 DataType::Type type = minmax->GetResultType();
9293 switch (type) {
9294 case DataType::Type::kInt32:
9295 case DataType::Type::kInt64:
9296 GenerateMinMaxInt(minmax->GetLocations(), is_min, isR6, type);
9297 break;
9298 case DataType::Type::kFloat32:
9299 case DataType::Type::kFloat64:
9300 GenerateMinMaxFP(minmax->GetLocations(), is_min, isR6, type);
9301 break;
9302 default:
9303 LOG(FATAL) << "Unexpected type for HMinMax " << type;
9304 }
9305}
9306
Aart Bik1f8d51b2018-02-15 10:42:37 -08009307void LocationsBuilderMIPS::VisitMin(HMin* min) {
9308 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
9309}
9310
9311void InstructionCodeGeneratorMIPS::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08009312 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009313}
9314
9315void LocationsBuilderMIPS::VisitMax(HMax* max) {
9316 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
9317}
9318
9319void InstructionCodeGeneratorMIPS::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08009320 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009321}
9322
Aart Bik3dad3412018-02-28 12:01:46 -08009323void LocationsBuilderMIPS::VisitAbs(HAbs* abs) {
9324 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
9325 switch (abs->GetResultType()) {
9326 case DataType::Type::kInt32:
9327 case DataType::Type::kInt64:
9328 locations->SetInAt(0, Location::RequiresRegister());
9329 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9330 break;
9331 case DataType::Type::kFloat32:
9332 case DataType::Type::kFloat64:
9333 locations->SetInAt(0, Location::RequiresFpuRegister());
9334 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9335 break;
9336 default:
9337 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9338 }
9339}
9340
9341void InstructionCodeGeneratorMIPS::GenerateAbsFP(LocationSummary* locations,
9342 DataType::Type type,
9343 bool isR2OrNewer,
9344 bool isR6) {
9345 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
9346 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9347
9348 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
9349 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
9350 // (signaling NaN may become quiet though).
9351 //
9352 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
9353 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
9354 // affected by this instruction.
9355 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
9356 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
9357 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
9358 if (isR6) {
9359 if (type == DataType::Type::kFloat64) {
9360 __ AbsD(out, in);
9361 } else {
9362 DCHECK_EQ(type, DataType::Type::kFloat32);
9363 __ AbsS(out, in);
9364 }
9365 } else {
9366 if (type == DataType::Type::kFloat64) {
9367 if (in != out) {
9368 __ MovD(out, in);
9369 }
9370 __ MoveFromFpuHigh(TMP, in);
9371 // ins instruction is not available for R1.
9372 if (isR2OrNewer) {
9373 __ Ins(TMP, ZERO, 31, 1);
9374 } else {
9375 __ Sll(TMP, TMP, 1);
9376 __ Srl(TMP, TMP, 1);
9377 }
9378 __ MoveToFpuHigh(TMP, out);
9379 } else {
9380 DCHECK_EQ(type, DataType::Type::kFloat32);
9381 __ Mfc1(TMP, in);
9382 // ins instruction is not available for R1.
9383 if (isR2OrNewer) {
9384 __ Ins(TMP, ZERO, 31, 1);
9385 } else {
9386 __ Sll(TMP, TMP, 1);
9387 __ Srl(TMP, TMP, 1);
9388 }
9389 __ Mtc1(TMP, out);
9390 }
9391 }
9392}
9393
9394void InstructionCodeGeneratorMIPS::VisitAbs(HAbs* abs) {
9395 LocationSummary* locations = abs->GetLocations();
9396 bool isR2OrNewer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
9397 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9398 switch (abs->GetResultType()) {
9399 case DataType::Type::kInt32: {
9400 Register in = locations->InAt(0).AsRegister<Register>();
9401 Register out = locations->Out().AsRegister<Register>();
9402 __ Sra(AT, in, 31);
9403 __ Xor(out, in, AT);
9404 __ Subu(out, out, AT);
9405 break;
9406 }
9407 case DataType::Type::kInt64: {
9408 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9409 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9410 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9411 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9412 // The comments in this section show the analogous operations which would
9413 // be performed if we had 64-bit registers "in", and "out".
9414 // __ Dsra32(AT, in, 31);
9415 __ Sra(AT, in_hi, 31);
9416 // __ Xor(out, in, AT);
9417 __ Xor(TMP, in_lo, AT);
9418 __ Xor(out_hi, in_hi, AT);
9419 // __ Dsubu(out, out, AT);
9420 __ Subu(out_lo, TMP, AT);
9421 __ Sltu(TMP, out_lo, TMP);
9422 __ Addu(out_hi, out_hi, TMP);
9423 break;
9424 }
9425 case DataType::Type::kFloat32:
9426 case DataType::Type::kFloat64:
9427 GenerateAbsFP(locations, abs->GetResultType(), isR2OrNewer, isR6);
9428 break;
9429 default:
9430 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9431 }
9432}
9433
Igor Murashkind01745e2017-04-05 16:40:31 -07009434void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
9435 constructor_fence->SetLocations(nullptr);
9436}
9437
9438void InstructionCodeGeneratorMIPS::VisitConstructorFence(
9439 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
9440 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
9441}
9442
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009443void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9444 memory_barrier->SetLocations(nullptr);
9445}
9446
9447void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9448 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
9449}
9450
9451void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009452 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009453 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009454 locations->SetInAt(0, MipsReturnLocation(return_type));
9455}
9456
9457void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
9458 codegen_->GenerateFrameExit();
9459}
9460
9461void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
9462 ret->SetLocations(nullptr);
9463}
9464
9465void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
9466 codegen_->GenerateFrameExit();
9467}
9468
Alexey Frunze92d90602015-12-18 18:16:36 -08009469void LocationsBuilderMIPS::VisitRor(HRor* ror) {
9470 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009471}
9472
Alexey Frunze92d90602015-12-18 18:16:36 -08009473void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
9474 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009475}
9476
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009477void LocationsBuilderMIPS::VisitShl(HShl* shl) {
9478 HandleShift(shl);
9479}
9480
9481void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
9482 HandleShift(shl);
9483}
9484
9485void LocationsBuilderMIPS::VisitShr(HShr* shr) {
9486 HandleShift(shr);
9487}
9488
9489void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
9490 HandleShift(shr);
9491}
9492
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009493void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
9494 HandleBinaryOp(instruction);
9495}
9496
9497void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
9498 HandleBinaryOp(instruction);
9499}
9500
9501void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9502 HandleFieldGet(instruction, instruction->GetFieldInfo());
9503}
9504
9505void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9506 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
9507}
9508
9509void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
9510 HandleFieldSet(instruction, instruction->GetFieldInfo());
9511}
9512
9513void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01009514 HandleFieldSet(instruction,
9515 instruction->GetFieldInfo(),
9516 instruction->GetDexPc(),
9517 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009518}
9519
9520void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
9521 HUnresolvedInstanceFieldGet* instruction) {
9522 FieldAccessCallingConventionMIPS calling_convention;
9523 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9524 instruction->GetFieldType(),
9525 calling_convention);
9526}
9527
9528void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
9529 HUnresolvedInstanceFieldGet* instruction) {
9530 FieldAccessCallingConventionMIPS calling_convention;
9531 codegen_->GenerateUnresolvedFieldAccess(instruction,
9532 instruction->GetFieldType(),
9533 instruction->GetFieldIndex(),
9534 instruction->GetDexPc(),
9535 calling_convention);
9536}
9537
9538void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
9539 HUnresolvedInstanceFieldSet* instruction) {
9540 FieldAccessCallingConventionMIPS calling_convention;
9541 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9542 instruction->GetFieldType(),
9543 calling_convention);
9544}
9545
9546void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
9547 HUnresolvedInstanceFieldSet* instruction) {
9548 FieldAccessCallingConventionMIPS calling_convention;
9549 codegen_->GenerateUnresolvedFieldAccess(instruction,
9550 instruction->GetFieldType(),
9551 instruction->GetFieldIndex(),
9552 instruction->GetDexPc(),
9553 calling_convention);
9554}
9555
9556void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
9557 HUnresolvedStaticFieldGet* instruction) {
9558 FieldAccessCallingConventionMIPS calling_convention;
9559 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9560 instruction->GetFieldType(),
9561 calling_convention);
9562}
9563
9564void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
9565 HUnresolvedStaticFieldGet* instruction) {
9566 FieldAccessCallingConventionMIPS calling_convention;
9567 codegen_->GenerateUnresolvedFieldAccess(instruction,
9568 instruction->GetFieldType(),
9569 instruction->GetFieldIndex(),
9570 instruction->GetDexPc(),
9571 calling_convention);
9572}
9573
9574void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
9575 HUnresolvedStaticFieldSet* instruction) {
9576 FieldAccessCallingConventionMIPS calling_convention;
9577 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9578 instruction->GetFieldType(),
9579 calling_convention);
9580}
9581
9582void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9583 HUnresolvedStaticFieldSet* instruction) {
9584 FieldAccessCallingConventionMIPS calling_convention;
9585 codegen_->GenerateUnresolvedFieldAccess(instruction,
9586 instruction->GetFieldType(),
9587 instruction->GetFieldIndex(),
9588 instruction->GetDexPc(),
9589 calling_convention);
9590}
9591
9592void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009593 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9594 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009595 // In suspend check slow path, usually there are no caller-save registers at all.
9596 // If SIMD instructions are present, however, we force spilling all live SIMD
9597 // registers in full width (since the runtime only saves/restores lower part).
9598 locations->SetCustomSlowPathCallerSaves(
9599 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009600}
9601
9602void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9603 HBasicBlock* block = instruction->GetBlock();
9604 if (block->GetLoopInformation() != nullptr) {
9605 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9606 // The back edge will generate the suspend check.
9607 return;
9608 }
9609 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9610 // The goto will generate the suspend check.
9611 return;
9612 }
9613 GenerateSuspendCheck(instruction, nullptr);
9614}
9615
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009616void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009617 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9618 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009619 InvokeRuntimeCallingConvention calling_convention;
9620 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9621}
9622
9623void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009624 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009625 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9626}
9627
9628void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009629 DataType::Type input_type = conversion->GetInputType();
9630 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009631 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9632 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009633 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009634
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009635 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9636 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009637 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9638 }
9639
9640 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009641 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009642 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9643 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009644 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009645 }
9646
Vladimir Markoca6fff82017-10-03 14:49:14 +01009647 LocationSummary* locations =
9648 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009649
9650 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009651 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009652 locations->SetInAt(0, Location::RequiresFpuRegister());
9653 } else {
9654 locations->SetInAt(0, Location::RequiresRegister());
9655 }
9656
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009657 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009658 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9659 } else {
9660 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9661 }
9662 } else {
9663 InvokeRuntimeCallingConvention calling_convention;
9664
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009665 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009666 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9667 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009668 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009669 locations->SetInAt(0, Location::RegisterPairLocation(
9670 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9671 }
9672
9673 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9674 }
9675}
9676
9677void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9678 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009679 DataType::Type result_type = conversion->GetResultType();
9680 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009681 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009682 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009683
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009684 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9685 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009686
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009687 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009688 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9689 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9690 Register src = locations->InAt(0).AsRegister<Register>();
9691
Alexey Frunzea871ef12016-06-27 15:20:11 -07009692 if (dst_low != src) {
9693 __ Move(dst_low, src);
9694 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009695 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009696 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009697 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009698 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009699 ? locations->InAt(0).AsRegisterPairLow<Register>()
9700 : locations->InAt(0).AsRegister<Register>();
9701
9702 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009703 case DataType::Type::kUint8:
9704 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009705 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009706 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009707 if (has_sign_extension) {
9708 __ Seb(dst, src);
9709 } else {
9710 __ Sll(dst, src, 24);
9711 __ Sra(dst, dst, 24);
9712 }
9713 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009714 case DataType::Type::kUint16:
9715 __ Andi(dst, src, 0xFFFF);
9716 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009717 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009718 if (has_sign_extension) {
9719 __ Seh(dst, src);
9720 } else {
9721 __ Sll(dst, src, 16);
9722 __ Sra(dst, dst, 16);
9723 }
9724 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009725 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009726 if (dst != src) {
9727 __ Move(dst, src);
9728 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009729 break;
9730
9731 default:
9732 LOG(FATAL) << "Unexpected type conversion from " << input_type
9733 << " to " << result_type;
9734 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009735 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9736 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009737 if (isR6) {
9738 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9739 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9740 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9741 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9742 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9743 __ Mtc1(src_low, FTMP);
9744 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009745 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009746 __ Cvtsl(dst, FTMP);
9747 } else {
9748 __ Cvtdl(dst, FTMP);
9749 }
9750 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009751 QuickEntrypointEnum entrypoint =
9752 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009753 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009754 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009755 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9756 } else {
9757 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9758 }
9759 }
9760 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009761 Register src = locations->InAt(0).AsRegister<Register>();
9762 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9763 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009764 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009765 __ Cvtsw(dst, FTMP);
9766 } else {
9767 __ Cvtdw(dst, FTMP);
9768 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009769 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009770 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9771 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009772
9773 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9774 // value of the output type if the input is outside of the range after the truncation or
9775 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9776 // results. This matches the desired float/double-to-int/long conversion exactly.
9777 //
9778 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9779 // value when the input is either a NaN or is outside of the range of the output type
9780 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9781 // the same result.
9782 //
9783 // The code takes care of the different behaviors by first comparing the input to the
9784 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9785 // If the input is greater than or equal to the minimum, it procedes to the truncate
9786 // instruction, which will handle such an input the same way irrespective of NAN2008.
9787 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9788 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009789 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009790 if (isR6) {
9791 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9792 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9793 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9794 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9795 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009796
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009797 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009798 __ TruncLS(FTMP, src);
9799 } else {
9800 __ TruncLD(FTMP, src);
9801 }
9802 __ Mfc1(dst_low, FTMP);
9803 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009804 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009805 QuickEntrypointEnum entrypoint =
9806 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009807 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009808 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009809 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9810 } else {
9811 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9812 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009813 }
9814 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009815 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9816 Register dst = locations->Out().AsRegister<Register>();
9817 MipsLabel truncate;
9818 MipsLabel done;
9819
Lena Djokicf4e23a82017-05-09 15:43:45 +02009820 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009821 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009822 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9823 __ LoadConst32(TMP, min_val);
9824 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009825 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009826 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9827 __ LoadConst32(TMP, High32Bits(min_val));
9828 __ Mtc1(ZERO, FTMP);
9829 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009830 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009831
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009832 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009833 __ ColeS(0, FTMP, src);
9834 } else {
9835 __ ColeD(0, FTMP, src);
9836 }
9837 __ Bc1t(0, &truncate);
9838
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009839 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009840 __ CeqS(0, src, src);
9841 } else {
9842 __ CeqD(0, src, src);
9843 }
9844 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9845 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009846
9847 __ B(&done);
9848
9849 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009850 }
9851
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009852 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009853 __ TruncWS(FTMP, src);
9854 } else {
9855 __ TruncWD(FTMP, src);
9856 }
9857 __ Mfc1(dst, FTMP);
9858
Lena Djokicf4e23a82017-05-09 15:43:45 +02009859 if (!isR6) {
9860 __ Bind(&done);
9861 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009862 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009863 } else if (DataType::IsFloatingPointType(result_type) &&
9864 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009865 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9866 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009867 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009868 __ Cvtsd(dst, src);
9869 } else {
9870 __ Cvtds(dst, src);
9871 }
9872 } else {
9873 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9874 << " to " << result_type;
9875 }
9876}
9877
9878void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9879 HandleShift(ushr);
9880}
9881
9882void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9883 HandleShift(ushr);
9884}
9885
9886void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9887 HandleBinaryOp(instruction);
9888}
9889
9890void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9891 HandleBinaryOp(instruction);
9892}
9893
9894void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9895 // Nothing to do, this should be removed during prepare for register allocator.
9896 LOG(FATAL) << "Unreachable";
9897}
9898
9899void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9900 // Nothing to do, this should be removed during prepare for register allocator.
9901 LOG(FATAL) << "Unreachable";
9902}
9903
9904void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009905 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009906}
9907
9908void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009909 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009910}
9911
9912void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009913 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009914}
9915
9916void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009917 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009918}
9919
9920void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009921 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009922}
9923
9924void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009925 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009926}
9927
9928void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009929 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009930}
9931
9932void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009933 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009934}
9935
9936void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009937 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009938}
9939
9940void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009941 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009942}
9943
9944void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009945 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009946}
9947
9948void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009949 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009950}
9951
9952void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009953 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009954}
9955
9956void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009957 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009958}
9959
9960void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009961 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009962}
9963
9964void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009965 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009966}
9967
9968void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009969 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009970}
9971
9972void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009973 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009974}
9975
9976void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009977 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009978}
9979
9980void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009981 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009982}
9983
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009984void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9985 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009986 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009987 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009988 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9989 uint32_t num_entries = switch_instr->GetNumEntries();
9990 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9991 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9992 // instruction to simulate PC-relative addressing when accessing the jump table.
9993 // NAL clobbers RA. Make sure RA is preserved.
9994 codegen_->ClobberRA();
9995 }
9996 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009997}
9998
Alexey Frunze96b66822016-09-10 02:32:44 -07009999void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
10000 int32_t lower_bound,
10001 uint32_t num_entries,
10002 HBasicBlock* switch_block,
10003 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010004 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010005 Register temp_reg = TMP;
10006 __ Addiu32(temp_reg, value_reg, -lower_bound);
10007 // Jump to default if index is negative
10008 // Note: We don't check the case that index is positive while value < lower_bound, because in
10009 // this case, index >= num_entries must be true. So that we can save one branch instruction.
10010 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
10011
Alexey Frunze96b66822016-09-10 02:32:44 -070010012 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010013 // Jump to successors[0] if value == lower_bound.
10014 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
10015 int32_t last_index = 0;
10016 for (; num_entries - last_index > 2; last_index += 2) {
10017 __ Addiu(temp_reg, temp_reg, -2);
10018 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
10019 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
10020 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
10021 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
10022 }
10023 if (num_entries - last_index == 2) {
10024 // The last missing case_value.
10025 __ Addiu(temp_reg, temp_reg, -1);
10026 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010027 }
10028
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010029 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -070010030 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010031 __ B(codegen_->GetLabelOf(default_block));
10032 }
10033}
10034
Alexey Frunze96b66822016-09-10 02:32:44 -070010035void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
10036 Register constant_area,
10037 int32_t lower_bound,
10038 uint32_t num_entries,
10039 HBasicBlock* switch_block,
10040 HBasicBlock* default_block) {
10041 // Create a jump table.
10042 std::vector<MipsLabel*> labels(num_entries);
10043 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
10044 for (uint32_t i = 0; i < num_entries; i++) {
10045 labels[i] = codegen_->GetLabelOf(successors[i]);
10046 }
10047 JumpTable* table = __ CreateJumpTable(std::move(labels));
10048
10049 // Is the value in range?
10050 __ Addiu32(TMP, value_reg, -lower_bound);
10051 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
10052 __ Sltiu(AT, TMP, num_entries);
10053 __ Beqz(AT, codegen_->GetLabelOf(default_block));
10054 } else {
10055 __ LoadConst32(AT, num_entries);
10056 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
10057 }
10058
10059 // We are in the range of the table.
10060 // Load the target address from the jump table, indexing by the value.
10061 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -070010062 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -070010063 __ Lw(TMP, TMP, 0);
10064 // Compute the absolute target address by adding the table start address
10065 // (the table contains offsets to targets relative to its start).
10066 __ Addu(TMP, TMP, AT);
10067 // And jump.
10068 __ Jr(TMP);
10069 __ NopIfNoReordering();
10070}
10071
10072void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
10073 int32_t lower_bound = switch_instr->GetStartValue();
10074 uint32_t num_entries = switch_instr->GetNumEntries();
10075 LocationSummary* locations = switch_instr->GetLocations();
10076 Register value_reg = locations->InAt(0).AsRegister<Register>();
10077 HBasicBlock* switch_block = switch_instr->GetBlock();
10078 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10079
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010080 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -070010081 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010082 //
10083 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
10084 // to access the jump table and it is implemented by changing HPackedSwitch to
10085 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
10086 // VisitMipsPackedSwitch()).
10087 //
10088 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
10089 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
10090 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -070010091 GenTableBasedPackedSwitch(value_reg,
10092 ZERO,
10093 lower_bound,
10094 num_entries,
10095 switch_block,
10096 default_block);
10097 } else {
10098 GenPackedSwitchWithCompares(value_reg,
10099 lower_bound,
10100 num_entries,
10101 switch_block,
10102 default_block);
10103 }
10104}
10105
10106void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10107 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010108 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -070010109 locations->SetInAt(0, Location::RequiresRegister());
10110 // Constant area pointer (HMipsComputeBaseMethodAddress).
10111 locations->SetInAt(1, Location::RequiresRegister());
10112}
10113
10114void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10115 int32_t lower_bound = switch_instr->GetStartValue();
10116 uint32_t num_entries = switch_instr->GetNumEntries();
10117 LocationSummary* locations = switch_instr->GetLocations();
10118 Register value_reg = locations->InAt(0).AsRegister<Register>();
10119 Register constant_area = locations->InAt(1).AsRegister<Register>();
10120 HBasicBlock* switch_block = switch_instr->GetBlock();
10121 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10122
10123 // This is an R2-only path. HPackedSwitch has been changed to
10124 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
10125 // required to address the jump table relative to PC.
10126 GenTableBasedPackedSwitch(value_reg,
10127 constant_area,
10128 lower_bound,
10129 num_entries,
10130 switch_block,
10131 default_block);
10132}
10133
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010134void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
10135 HMipsComputeBaseMethodAddress* insn) {
10136 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010137 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010138 locations->SetOut(Location::RequiresRegister());
10139}
10140
10141void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
10142 HMipsComputeBaseMethodAddress* insn) {
10143 LocationSummary* locations = insn->GetLocations();
10144 Register reg = locations->Out().AsRegister<Register>();
10145
10146 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
10147
10148 // Generate a dummy PC-relative call to obtain PC.
10149 __ Nal();
10150 // Grab the return address off RA.
10151 __ Move(reg, RA);
10152
10153 // Remember this offset (the obtained PC value) for later use with constant area.
10154 __ BindPcRelBaseLabel();
10155}
10156
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010157void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10158 // The trampoline uses the same calling convention as dex calling conventions,
10159 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
10160 // the method_idx.
10161 HandleInvoke(invoke);
10162}
10163
10164void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10165 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
10166}
10167
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010168void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10169 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010170 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010171 locations->SetInAt(0, Location::RequiresRegister());
10172 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010173}
10174
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010175void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10176 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +000010177 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010178 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010179 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010180 __ LoadFromOffset(kLoadWord,
10181 locations->Out().AsRegister<Register>(),
10182 locations->InAt(0).AsRegister<Register>(),
10183 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010184 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010185 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +000010186 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000010187 __ LoadFromOffset(kLoadWord,
10188 locations->Out().AsRegister<Register>(),
10189 locations->InAt(0).AsRegister<Register>(),
10190 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010191 __ LoadFromOffset(kLoadWord,
10192 locations->Out().AsRegister<Register>(),
10193 locations->Out().AsRegister<Register>(),
10194 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010195 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010196}
10197
xueliang.zhonge0eb4832017-10-30 13:43:14 +000010198void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10199 ATTRIBUTE_UNUSED) {
10200 LOG(FATAL) << "Unreachable";
10201}
10202
10203void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10204 ATTRIBUTE_UNUSED) {
10205 LOG(FATAL) << "Unreachable";
10206}
10207
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010208#undef __
10209#undef QUICK_ENTRY_POINT
10210
10211} // namespace mips
10212} // namespace art