blob: dc657b6fb11d1927f2a5e6c9b6ac54a8e9a27985 [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
Vladimir Marko0a516052019-10-14 13:00:44 +000043namespace art {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020044namespace 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;
Elliott Hughesc1896c92018-11-29 11:33:18 -0800150 UNREACHABLE();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200151 }
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100179 void EmitNativeCode(CodeGenerator* codegen) override {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200180 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100204 bool IsFatal() const override { return true; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200205
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100206 const char* GetDescription() const override { return "BoundsCheckSlowPathMIPS"; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100216 void EmitNativeCode(CodeGenerator* codegen) override {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200217 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100223 bool IsFatal() const override { return true; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200224
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100225 const char* GetDescription() const override { return "DivZeroCheckSlowPathMIPS"; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200226
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100239 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100283 const char* GetDescription() const override { return "LoadClassSlowPathMIPS"; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200284
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100297 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100321 const char* GetDescription() const override { return "LoadStringSlowPathMIPS"; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200322
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100331 void EmitNativeCode(CodeGenerator* codegen) override {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200332 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100345 bool IsFatal() const override { return true; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200346
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100347 const char* GetDescription() const override { return "NullCheckSlowPathMIPS"; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200348
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100358 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100378 const char* GetDescription() const override { return "SuspendCheckSlowPathMIPS"; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200379
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100399 void EmitNativeCode(CodeGenerator* codegen) override {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200400 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100438 const char* GetDescription() const override { return "TypeCheckSlowPathMIPS"; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200439
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100440 bool IsFatal() const override { return is_fatal_; }
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800441
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100453 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100465 const char* GetDescription() const override { return "DeoptimizationSlowPathMIPS"; }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200466
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100475 void EmitNativeCode(CodeGenerator* codegen) override {
Alexey Frunze15958152017-02-09 19:08:30 -0800476 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100506 const char* GetDescription() const override { return "ArraySetSlowPathMIPS"; }
Alexey Frunze15958152017-02-09 19:08:30 -0800507
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100536 const char* GetDescription() const override { return "ReadBarrierMarkSlowPathMIPS"; }
Alexey Frunze15958152017-02-09 19:08:30 -0800537
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100538 void EmitNativeCode(CodeGenerator* codegen) override {
Alexey Frunze15958152017-02-09 19:08:30 -0800539 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800590 /* direct= */ false);
Alexey Frunze15958152017-02-09 19:08:30 -0800591 }
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100630 const char* GetDescription() const override {
Alexey Frunze15958152017-02-09 19:08:30 -0800631 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
632 }
633
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100634 void EmitNativeCode(CodeGenerator* codegen) override {
Alexey Frunze15958152017-02-09 19:08:30 -0800635 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,
Andreas Gampe3db70682018-12-26 15:12:03 -0800684 /* direct= */ false);
Alexey Frunze15958152017-02-09 19:08:30 -0800685
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100801 void EmitNativeCode(CodeGenerator* codegen) override {
Alexey Frunze15958152017-02-09 19:08:30 -0800802 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100925 const char* GetDescription() const override { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
Alexey Frunze15958152017-02-09 19:08:30 -0800926
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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100968 void EmitNativeCode(CodeGenerator* codegen) override {
Alexey Frunze15958152017-02-09 19:08:30 -0800969 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
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100998 const char* GetDescription() const override { return "ReadBarrierForRootSlowPathMIPS"; }
Alexey Frunze15958152017-02-09 19:08:30 -0800999
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 Marko2d06e022019-07-08 15:45:19 +01001034 boot_image_other_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()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001170 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot= */ false);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001171 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08001172 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() +
Vladimir Marko2d06e022019-07-08 15:45:19 +01001626 boot_image_other_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001627 linker_patches->reserve(size);
Vladimir Marko44ca0752019-07-29 10:18:25 +01001628 if (GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension()) {
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 Marko65979462017-05-19 17:25:12 +01001635 } else {
Vladimir Marko2d06e022019-07-08 15:45:19 +01001636 DCHECK(boot_image_method_patches_.empty());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001637 DCHECK(boot_image_type_patches_.empty());
1638 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko2d06e022019-07-08 15:45:19 +01001639 }
1640 if (GetCompilerOptions().IsBootImage()) {
1641 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
1642 boot_image_other_patches_, linker_patches);
1643 } else {
1644 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
1645 boot_image_other_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001646 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001647 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1648 method_bss_entry_patches_, linker_patches);
1649 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1650 type_bss_entry_patches_, linker_patches);
1651 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1652 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001653 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001654}
1655
Vladimir Marko6fd16062018-06-26 11:02:04 +01001656CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageIntrinsicPatch(
1657 uint32_t intrinsic_data,
1658 const PcRelativePatchInfo* info_high) {
1659 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01001660 /* dex_file= */ nullptr, intrinsic_data, info_high, &boot_image_other_patches_);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001661}
1662
Vladimir Markob066d432018-01-03 13:14:37 +00001663CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageRelRoPatch(
1664 uint32_t boot_image_offset,
1665 const PcRelativePatchInfo* info_high) {
1666 return NewPcRelativePatch(
Vladimir Marko2d06e022019-07-08 15:45:19 +01001667 /* dex_file= */ nullptr, boot_image_offset, info_high, &boot_image_other_patches_);
Vladimir Markob066d432018-01-03 13:14:37 +00001668}
1669
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001670CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001671 MethodReference target_method,
1672 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001673 return NewPcRelativePatch(
1674 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001675}
1676
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001677CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001678 MethodReference target_method,
1679 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001680 return NewPcRelativePatch(
1681 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001682}
1683
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001684CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001685 const DexFile& dex_file,
1686 dex::TypeIndex type_index,
1687 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001688 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001689}
1690
Vladimir Marko1998cd02017-01-13 13:02:58 +00001691CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001692 const DexFile& dex_file,
1693 dex::TypeIndex type_index,
1694 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001695 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001696}
1697
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001698CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001699 const DexFile& dex_file,
1700 dex::StringIndex string_index,
1701 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001702 return NewPcRelativePatch(
1703 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001704}
1705
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001706CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1707 const DexFile& dex_file,
1708 dex::StringIndex string_index,
1709 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001710 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001711}
1712
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001713CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001714 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001715 uint32_t offset_or_index,
1716 const PcRelativePatchInfo* info_high,
1717 ArenaDeque<PcRelativePatchInfo>* patches) {
1718 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001719 return &patches->back();
1720}
1721
Alexey Frunze06a46c42016-07-19 15:00:40 -07001722Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1723 return map->GetOrCreate(
1724 value,
1725 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1726}
1727
Alexey Frunze06a46c42016-07-19 15:00:40 -07001728Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001729 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001730}
1731
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001732void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001733 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001734 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001735 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001736 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001737 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001738 if (GetInstructionSetFeatures().IsR6()) {
1739 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001740 __ Bind(&info_high->label);
1741 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001742 // Add the high half of a 32-bit offset to PC.
Andreas Gampe3db70682018-12-26 15:12:03 -08001743 __ Auipc(out, /* imm16= */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001744 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001745 } else {
1746 // If base is ZERO, emit NAL to obtain the actual base.
1747 if (base == ZERO) {
1748 // Generate a dummy PC-relative call to obtain PC.
1749 __ Nal();
1750 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001751 __ Bind(&info_high->label);
Andreas Gampe3db70682018-12-26 15:12:03 -08001752 __ Lui(out, /* imm16= */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001753 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1754 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1755 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001756 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001757 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001758 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001759 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001760 __ Addu(out, out, (base == ZERO) ? RA : base);
1761 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001762 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001763 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001764}
1765
Vladimir Marko6fd16062018-06-26 11:02:04 +01001766void CodeGeneratorMIPS::LoadBootImageAddress(Register reg, uint32_t boot_image_reference) {
1767 if (GetCompilerOptions().IsBootImage()) {
1768 PcRelativePatchInfo* info_high = NewBootImageIntrinsicPatch(boot_image_reference);
1769 PcRelativePatchInfo* info_low = NewBootImageIntrinsicPatch(boot_image_reference, info_high);
Andreas Gampe3db70682018-12-26 15:12:03 -08001770 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, /* base= */ ZERO);
1771 __ Addiu(reg, TMP, /* imm16= */ 0x5678, &info_low->label);
Vladimir Markoa2da9b92018-10-10 14:21:55 +01001772 } else if (GetCompilerOptions().GetCompilePic()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001773 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_reference);
1774 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_reference, info_high);
Andreas Gampe3db70682018-12-26 15:12:03 -08001775 EmitPcRelativeAddressPlaceholderHigh(info_high, reg, /* base= */ ZERO);
1776 __ Lw(reg, reg, /* imm16= */ 0x5678, &info_low->label);
Vladimir Markoeebb8212018-06-05 14:57:24 +01001777 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001778 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01001779 gc::Heap* heap = Runtime::Current()->GetHeap();
1780 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001781 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01001782 __ LoadConst32(reg, dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address)));
1783 }
1784}
1785
Vladimir Marko6fd16062018-06-26 11:02:04 +01001786void CodeGeneratorMIPS::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
1787 uint32_t boot_image_offset) {
1788 DCHECK(invoke->IsStatic());
1789 InvokeRuntimeCallingConvention calling_convention;
1790 Register argument = calling_convention.GetRegisterAt(0);
1791 if (GetCompilerOptions().IsBootImage()) {
1792 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
1793 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
1794 MethodReference target_method = invoke->GetTargetMethod();
1795 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
1796 PcRelativePatchInfo* info_high = NewBootImageTypePatch(*target_method.dex_file, type_idx);
1797 PcRelativePatchInfo* info_low =
1798 NewBootImageTypePatch(*target_method.dex_file, type_idx, info_high);
Andreas Gampe3db70682018-12-26 15:12:03 -08001799 EmitPcRelativeAddressPlaceholderHigh(info_high, argument, /* base= */ ZERO);
1800 __ Addiu(argument, argument, /* imm16= */ 0x5678, &info_low->label);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001801 } else {
1802 LoadBootImageAddress(argument, boot_image_offset);
1803 }
1804 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
1805 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
1806}
1807
Alexey Frunze627c1a02017-01-30 19:28:14 -08001808CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1809 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001810 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001811 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001812 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1813 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001814 return &jit_string_patches_.back();
1815}
1816
1817CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1818 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001819 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001820 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001821 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1822 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001823 return &jit_class_patches_.back();
1824}
1825
1826void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1827 const uint8_t* roots_data,
1828 const CodeGeneratorMIPS::JitPatchInfo& info,
1829 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001830 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1831 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001832 uintptr_t address =
1833 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1834 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1835 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001836 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1837 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1838 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1839 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001840 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001841 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1842 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001843 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001844 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001845 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1846 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001847 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001848 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1849 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001850}
1851
1852void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1853 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001854 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1855 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001856 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001857 }
1858 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001859 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1860 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001861 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001862 }
1863}
1864
Goran Jakovljevice114da22016-12-26 14:21:43 +01001865void CodeGeneratorMIPS::MarkGCCard(Register object,
1866 Register value,
1867 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001868 MipsLabel done;
1869 Register card = AT;
1870 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001871 if (value_can_be_null) {
1872 __ Beqz(value, &done);
1873 }
Roland Levillainc73f0522018-08-14 15:16:50 +01001874 // Load the address of the card table into `card`.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001875 __ LoadFromOffset(kLoadWord,
1876 card,
1877 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001878 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Roland Levillainc73f0522018-08-14 15:16:50 +01001879 // Calculate the address of the card corresponding to `object`.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1881 __ Addu(temp, card, temp);
Roland Levillainc73f0522018-08-14 15:16:50 +01001882 // Write the `art::gc::accounting::CardTable::kCardDirty` value into the
1883 // `object`'s card.
1884 //
1885 // Register `card` contains the address of the card table. Note that the card
1886 // table's base is biased during its creation so that it always starts at an
1887 // address whose least-significant byte is equal to `kCardDirty` (see
1888 // art::gc::accounting::CardTable::Create). Therefore the SB instruction
1889 // below writes the `kCardDirty` (byte) value into the `object`'s card
1890 // (located at `card + object >> kCardShift`).
1891 //
1892 // This dual use of the value in register `card` (1. to calculate the location
1893 // of the card to mark; and 2. to load the `kCardDirty` value) saves a load
1894 // (no need to explicitly load `kCardDirty` as an immediate value).
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001895 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001896 if (value_can_be_null) {
1897 __ Bind(&done);
1898 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001899}
1900
David Brazdil58282f42016-01-14 12:45:10 +00001901void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001902 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1903 blocked_core_registers_[ZERO] = true;
1904 blocked_core_registers_[K0] = true;
1905 blocked_core_registers_[K1] = true;
1906 blocked_core_registers_[GP] = true;
1907 blocked_core_registers_[SP] = true;
1908 blocked_core_registers_[RA] = true;
1909
1910 // AT and TMP(T8) are used as temporary/scratch registers
1911 // (similar to how AT is used by MIPS assemblers).
1912 blocked_core_registers_[AT] = true;
1913 blocked_core_registers_[TMP] = true;
1914 blocked_fpu_registers_[FTMP] = true;
1915
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001916 if (GetInstructionSetFeatures().HasMsa()) {
1917 // To be used just for MSA instructions.
1918 blocked_fpu_registers_[FTMP2] = true;
1919 }
1920
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921 // Reserve suspend and thread registers.
1922 blocked_core_registers_[S0] = true;
1923 blocked_core_registers_[TR] = true;
1924
1925 // Reserve T9 for function calls
1926 blocked_core_registers_[T9] = true;
1927
1928 // Reserve odd-numbered FPU registers.
1929 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1930 blocked_fpu_registers_[i] = true;
1931 }
1932
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001933 if (GetGraph()->IsDebuggable()) {
1934 // Stubs do not save callee-save floating point registers. If the graph
1935 // is debuggable, we need to deal with these registers differently. For
1936 // now, just block them.
1937 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1938 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1939 }
1940 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001941}
1942
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001943size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1944 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1945 return kMipsWordSize;
1946}
1947
1948size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1949 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1950 return kMipsWordSize;
1951}
1952
1953size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001954 if (GetGraph()->HasSIMD()) {
1955 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1956 } else {
1957 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1958 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001959 return GetSlowPathFPWidth();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001960}
1961
1962size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001963 if (GetGraph()->HasSIMD()) {
1964 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1965 } else {
1966 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1967 }
Artem Serov6a0b6572019-07-26 20:38:37 +01001968 return GetSlowPathFPWidth();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001969}
1970
1971void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001972 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973}
1974
1975void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001976 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001977}
1978
Vladimir Markoa0431112018-06-25 09:32:54 +01001979const MipsInstructionSetFeatures& CodeGeneratorMIPS::GetInstructionSetFeatures() const {
1980 return *GetCompilerOptions().GetInstructionSetFeatures()->AsMipsInstructionSetFeatures();
1981}
1982
Serban Constantinescufca16662016-07-14 09:21:59 +01001983constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1984
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001985void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1986 HInstruction* instruction,
1987 uint32_t dex_pc,
1988 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001989 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001990 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1991 IsDirectEntrypoint(entrypoint));
1992 if (EntrypointRequiresStackMap(entrypoint)) {
1993 RecordPcInfo(instruction, dex_pc, slow_path);
1994 }
1995}
1996
1997void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1998 HInstruction* instruction,
1999 SlowPathCode* slow_path,
2000 bool direct) {
2001 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
2002 GenerateInvokeRuntime(entry_point_offset, direct);
2003}
2004
2005void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07002006 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08002007 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002008 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08002009 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002010 // Reserve argument space on stack (for $a0-$a3) for
2011 // entrypoints that directly reference native implementations.
2012 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002013 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002015 } else {
2016 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002017 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07002018 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002019}
2020
2021void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
2022 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00002023 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
2024 const size_t status_byte_offset =
2025 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2026 constexpr uint32_t shifted_initialized_value =
2027 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
2028
2029 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01002030 __ Sltiu(TMP, TMP, shifted_initialized_value);
2031 __ Bnez(TMP, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002032 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
2033 __ Sync(0);
2034 __ Bind(slow_path->GetExitLabel());
2035}
2036
Vladimir Marko175e7862018-03-27 09:03:13 +00002037void InstructionCodeGeneratorMIPS::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
2038 Register temp) {
2039 uint32_t path_to_root = check->GetBitstringPathToRoot();
2040 uint32_t mask = check->GetBitstringMask();
2041 DCHECK(IsPowerOfTwo(mask + 1));
2042 size_t mask_bits = WhichPowerOf2(mask + 1);
2043
2044 if (mask_bits == 16u) {
2045 // Load only the bitstring part of the status word.
2046 __ LoadFromOffset(
2047 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
2048 // Compare the bitstring bits using XOR.
2049 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
2050 } else {
2051 // /* uint32_t */ temp = temp->status_
2052 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
2053 // Compare the bitstring bits using XOR.
2054 if (IsUint<16>(path_to_root)) {
2055 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
2056 } else {
2057 __ LoadConst32(TMP, path_to_root);
2058 __ Xor(temp, temp, TMP);
2059 }
2060 // Shift out bits that do not contribute to the comparison.
2061 __ Sll(temp, temp, 32 - mask_bits);
2062 }
2063}
2064
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002065void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
2066 __ Sync(0); // Only stype 0 is supported.
2067}
2068
2069void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
2070 HBasicBlock* successor) {
2071 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07002072 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
2073
2074 if (slow_path == nullptr) {
2075 slow_path =
2076 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
2077 instruction->SetSlowPath(slow_path);
2078 codegen_->AddSlowPath(slow_path);
2079 if (successor != nullptr) {
2080 DCHECK(successor->IsLoopHeader());
2081 }
2082 } else {
2083 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2084 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002085
2086 __ LoadFromOffset(kLoadUnsignedHalfword,
2087 TMP,
2088 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002089 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002090 if (successor == nullptr) {
2091 __ Bnez(TMP, slow_path->GetEntryLabel());
2092 __ Bind(slow_path->GetReturnLabel());
2093 } else {
2094 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2095 __ B(slow_path->GetEntryLabel());
2096 // slow_path will return to GetLabelOf(successor).
2097 }
2098}
2099
2100InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2101 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002102 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002103 assembler_(codegen->GetAssembler()),
2104 codegen_(codegen) {}
2105
2106void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2107 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002108 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002109 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01002110 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002111 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002112 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002113 locations->SetInAt(0, Location::RequiresRegister());
2114 HInstruction* right = instruction->InputAt(1);
2115 bool can_use_imm = false;
2116 if (right->IsConstant()) {
2117 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2118 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2119 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002120 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002121 DCHECK(instruction->IsSub() || instruction->IsAdd());
2122 if (instruction->IsSub()) {
2123 imm = -imm;
2124 }
2125 if (isR6) {
2126 bool single_use = right->GetUses().HasExactlyOneElement();
2127 int16_t imm_high = High16Bits(imm);
2128 int16_t imm_low = Low16Bits(imm);
2129 if (imm_low < 0) {
2130 imm_high += 1;
2131 }
2132 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2133 } else {
2134 can_use_imm = IsInt<16>(imm);
2135 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002136 }
2137 }
2138 if (can_use_imm)
2139 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2140 else
2141 locations->SetInAt(1, Location::RequiresRegister());
2142 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2143 break;
2144 }
2145
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002146 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002147 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002148 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2149 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002150 break;
2151 }
2152
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002153 case DataType::Type::kFloat32:
2154 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002155 DCHECK(instruction->IsAdd() || instruction->IsSub());
2156 locations->SetInAt(0, Location::RequiresFpuRegister());
2157 locations->SetInAt(1, Location::RequiresFpuRegister());
2158 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2159 break;
2160
2161 default:
2162 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2163 }
2164}
2165
2166void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002167 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002168 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002169 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002170
2171 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002172 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002173 Register dst = locations->Out().AsRegister<Register>();
2174 Register lhs = locations->InAt(0).AsRegister<Register>();
2175 Location rhs_location = locations->InAt(1);
2176
2177 Register rhs_reg = ZERO;
2178 int32_t rhs_imm = 0;
2179 bool use_imm = rhs_location.IsConstant();
2180 if (use_imm) {
2181 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2182 } else {
2183 rhs_reg = rhs_location.AsRegister<Register>();
2184 }
2185
2186 if (instruction->IsAnd()) {
2187 if (use_imm)
2188 __ Andi(dst, lhs, rhs_imm);
2189 else
2190 __ And(dst, lhs, rhs_reg);
2191 } else if (instruction->IsOr()) {
2192 if (use_imm)
2193 __ Ori(dst, lhs, rhs_imm);
2194 else
2195 __ Or(dst, lhs, rhs_reg);
2196 } else if (instruction->IsXor()) {
2197 if (use_imm)
2198 __ Xori(dst, lhs, rhs_imm);
2199 else
2200 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002201 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002202 DCHECK(instruction->IsAdd() || instruction->IsSub());
2203 if (use_imm) {
2204 if (instruction->IsSub()) {
2205 rhs_imm = -rhs_imm;
2206 }
2207 if (IsInt<16>(rhs_imm)) {
2208 __ Addiu(dst, lhs, rhs_imm);
2209 } else {
2210 DCHECK(isR6);
2211 int16_t rhs_imm_high = High16Bits(rhs_imm);
2212 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2213 if (rhs_imm_low < 0) {
2214 rhs_imm_high += 1;
2215 }
2216 __ Aui(dst, lhs, rhs_imm_high);
2217 if (rhs_imm_low != 0) {
2218 __ Addiu(dst, dst, rhs_imm_low);
2219 }
2220 }
2221 } else if (instruction->IsAdd()) {
2222 __ Addu(dst, lhs, rhs_reg);
2223 } else {
2224 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002225 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002226 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002227 }
2228 break;
2229 }
2230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002231 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002232 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2233 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2234 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2235 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002236 Location rhs_location = locations->InAt(1);
2237 bool use_imm = rhs_location.IsConstant();
2238 if (!use_imm) {
2239 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2240 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2241 if (instruction->IsAnd()) {
2242 __ And(dst_low, lhs_low, rhs_low);
2243 __ And(dst_high, lhs_high, rhs_high);
2244 } else if (instruction->IsOr()) {
2245 __ Or(dst_low, lhs_low, rhs_low);
2246 __ Or(dst_high, lhs_high, rhs_high);
2247 } else if (instruction->IsXor()) {
2248 __ Xor(dst_low, lhs_low, rhs_low);
2249 __ Xor(dst_high, lhs_high, rhs_high);
2250 } else if (instruction->IsAdd()) {
2251 if (lhs_low == rhs_low) {
2252 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2253 __ Slt(TMP, lhs_low, ZERO);
2254 __ Addu(dst_low, lhs_low, rhs_low);
2255 } else {
2256 __ Addu(dst_low, lhs_low, rhs_low);
2257 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2258 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2259 }
2260 __ Addu(dst_high, lhs_high, rhs_high);
2261 __ Addu(dst_high, dst_high, TMP);
2262 } else {
2263 DCHECK(instruction->IsSub());
2264 __ Sltu(TMP, lhs_low, rhs_low);
2265 __ Subu(dst_low, lhs_low, rhs_low);
2266 __ Subu(dst_high, lhs_high, rhs_high);
2267 __ Subu(dst_high, dst_high, TMP);
2268 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002269 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002270 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2271 if (instruction->IsOr()) {
2272 uint32_t low = Low32Bits(value);
2273 uint32_t high = High32Bits(value);
2274 if (IsUint<16>(low)) {
2275 if (dst_low != lhs_low || low != 0) {
2276 __ Ori(dst_low, lhs_low, low);
2277 }
2278 } else {
2279 __ LoadConst32(TMP, low);
2280 __ Or(dst_low, lhs_low, TMP);
2281 }
2282 if (IsUint<16>(high)) {
2283 if (dst_high != lhs_high || high != 0) {
2284 __ Ori(dst_high, lhs_high, high);
2285 }
2286 } else {
2287 if (high != low) {
2288 __ LoadConst32(TMP, high);
2289 }
2290 __ Or(dst_high, lhs_high, TMP);
2291 }
2292 } else if (instruction->IsXor()) {
2293 uint32_t low = Low32Bits(value);
2294 uint32_t high = High32Bits(value);
2295 if (IsUint<16>(low)) {
2296 if (dst_low != lhs_low || low != 0) {
2297 __ Xori(dst_low, lhs_low, low);
2298 }
2299 } else {
2300 __ LoadConst32(TMP, low);
2301 __ Xor(dst_low, lhs_low, TMP);
2302 }
2303 if (IsUint<16>(high)) {
2304 if (dst_high != lhs_high || high != 0) {
2305 __ Xori(dst_high, lhs_high, high);
2306 }
2307 } else {
2308 if (high != low) {
2309 __ LoadConst32(TMP, high);
2310 }
2311 __ Xor(dst_high, lhs_high, TMP);
2312 }
2313 } else if (instruction->IsAnd()) {
2314 uint32_t low = Low32Bits(value);
2315 uint32_t high = High32Bits(value);
2316 if (IsUint<16>(low)) {
2317 __ Andi(dst_low, lhs_low, low);
2318 } else if (low != 0xFFFFFFFF) {
2319 __ LoadConst32(TMP, low);
2320 __ And(dst_low, lhs_low, TMP);
2321 } else if (dst_low != lhs_low) {
2322 __ Move(dst_low, lhs_low);
2323 }
2324 if (IsUint<16>(high)) {
2325 __ Andi(dst_high, lhs_high, high);
2326 } else if (high != 0xFFFFFFFF) {
2327 if (high != low) {
2328 __ LoadConst32(TMP, high);
2329 }
2330 __ And(dst_high, lhs_high, TMP);
2331 } else if (dst_high != lhs_high) {
2332 __ Move(dst_high, lhs_high);
2333 }
2334 } else {
2335 if (instruction->IsSub()) {
2336 value = -value;
2337 } else {
2338 DCHECK(instruction->IsAdd());
2339 }
2340 int32_t low = Low32Bits(value);
2341 int32_t high = High32Bits(value);
2342 if (IsInt<16>(low)) {
2343 if (dst_low != lhs_low || low != 0) {
2344 __ Addiu(dst_low, lhs_low, low);
2345 }
2346 if (low != 0) {
2347 __ Sltiu(AT, dst_low, low);
2348 }
2349 } else {
2350 __ LoadConst32(TMP, low);
2351 __ Addu(dst_low, lhs_low, TMP);
2352 __ Sltu(AT, dst_low, TMP);
2353 }
2354 if (IsInt<16>(high)) {
2355 if (dst_high != lhs_high || high != 0) {
2356 __ Addiu(dst_high, lhs_high, high);
2357 }
2358 } else {
2359 if (high != low) {
2360 __ LoadConst32(TMP, high);
2361 }
2362 __ Addu(dst_high, lhs_high, TMP);
2363 }
2364 if (low != 0) {
2365 __ Addu(dst_high, dst_high, AT);
2366 }
2367 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002368 }
2369 break;
2370 }
2371
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002372 case DataType::Type::kFloat32:
2373 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002374 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2375 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2376 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2377 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002378 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002379 __ AddS(dst, lhs, rhs);
2380 } else {
2381 __ AddD(dst, lhs, rhs);
2382 }
2383 } else {
2384 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002385 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002386 __ SubS(dst, lhs, rhs);
2387 } else {
2388 __ SubD(dst, lhs, rhs);
2389 }
2390 }
2391 break;
2392 }
2393
2394 default:
2395 LOG(FATAL) << "Unexpected binary operation type " << type;
2396 }
2397}
2398
2399void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002400 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002401
Vladimir Markoca6fff82017-10-03 14:49:14 +01002402 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002404 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002405 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002406 locations->SetInAt(0, Location::RequiresRegister());
2407 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2409 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002410 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002411 locations->SetInAt(0, Location::RequiresRegister());
2412 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2413 locations->SetOut(Location::RequiresRegister());
2414 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002415 default:
2416 LOG(FATAL) << "Unexpected shift type " << type;
2417 }
2418}
2419
2420static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2421
2422void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002423 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002424 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002425 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426
2427 Location rhs_location = locations->InAt(1);
2428 bool use_imm = rhs_location.IsConstant();
2429 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2430 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002431 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002432 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002433 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002434 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2435 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002436
2437 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002438 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002439 Register dst = locations->Out().AsRegister<Register>();
2440 Register lhs = locations->InAt(0).AsRegister<Register>();
2441 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002442 if (shift_value == 0) {
2443 if (dst != lhs) {
2444 __ Move(dst, lhs);
2445 }
2446 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002447 __ Sll(dst, lhs, shift_value);
2448 } else if (instr->IsShr()) {
2449 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002450 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002451 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002452 } else {
2453 if (has_ins_rotr) {
2454 __ Rotr(dst, lhs, shift_value);
2455 } else {
2456 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2457 __ Srl(dst, lhs, shift_value);
2458 __ Or(dst, dst, TMP);
2459 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002460 }
2461 } else {
2462 if (instr->IsShl()) {
2463 __ Sllv(dst, lhs, rhs_reg);
2464 } else if (instr->IsShr()) {
2465 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002466 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002467 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002468 } else {
2469 if (has_ins_rotr) {
2470 __ Rotrv(dst, lhs, rhs_reg);
2471 } else {
2472 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002473 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2474 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2475 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2476 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2477 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002478 __ Sllv(TMP, lhs, TMP);
2479 __ Srlv(dst, lhs, rhs_reg);
2480 __ Or(dst, dst, TMP);
2481 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002482 }
2483 }
2484 break;
2485 }
2486
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002487 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002488 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2489 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2490 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2491 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2492 if (use_imm) {
2493 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002494 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002495 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002496 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002497 if (instr->IsShl()) {
2498 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2499 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2500 __ Sll(dst_low, lhs_low, shift_value);
2501 } else if (instr->IsShr()) {
2502 __ Srl(dst_low, lhs_low, shift_value);
2503 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2504 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002505 } else if (instr->IsUShr()) {
2506 __ Srl(dst_low, lhs_low, shift_value);
2507 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2508 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002509 } else {
2510 __ Srl(dst_low, lhs_low, shift_value);
2511 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2512 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002513 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002514 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002515 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002516 if (instr->IsShl()) {
2517 __ Sll(dst_low, lhs_low, shift_value);
2518 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2519 __ Sll(dst_high, lhs_high, shift_value);
2520 __ Or(dst_high, dst_high, TMP);
2521 } else if (instr->IsShr()) {
2522 __ Sra(dst_high, lhs_high, shift_value);
2523 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2524 __ Srl(dst_low, lhs_low, shift_value);
2525 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002526 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002527 __ Srl(dst_high, lhs_high, shift_value);
2528 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2529 __ Srl(dst_low, lhs_low, shift_value);
2530 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002531 } else {
2532 __ Srl(TMP, lhs_low, shift_value);
2533 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2534 __ Or(dst_low, dst_low, TMP);
2535 __ Srl(TMP, lhs_high, shift_value);
2536 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2537 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002538 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002539 }
2540 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002541 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002542 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002543 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002544 __ Move(dst_low, ZERO);
2545 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002546 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002547 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002548 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002549 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002550 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002551 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002552 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002553 // 64-bit rotation by 32 is just a swap.
2554 __ Move(dst_low, lhs_high);
2555 __ Move(dst_high, lhs_low);
2556 } else {
2557 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002558 __ Srl(dst_low, lhs_high, shift_value_high);
2559 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2560 __ Srl(dst_high, lhs_low, shift_value_high);
2561 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002562 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002563 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2564 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002565 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002566 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2567 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002568 __ Or(dst_high, dst_high, TMP);
2569 }
2570 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002571 }
2572 }
2573 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002574 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002575 MipsLabel done;
2576 if (instr->IsShl()) {
2577 __ Sllv(dst_low, lhs_low, rhs_reg);
2578 __ Nor(AT, ZERO, rhs_reg);
2579 __ Srl(TMP, lhs_low, 1);
2580 __ Srlv(TMP, TMP, AT);
2581 __ Sllv(dst_high, lhs_high, rhs_reg);
2582 __ Or(dst_high, dst_high, TMP);
2583 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002584 if (isR6) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002585 __ Beqzc(TMP, &done, /* is_bare= */ true);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002586 __ Move(dst_high, dst_low);
2587 __ Move(dst_low, ZERO);
2588 } else {
2589 __ Movn(dst_high, dst_low, TMP);
2590 __ Movn(dst_low, ZERO, TMP);
2591 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002592 } else if (instr->IsShr()) {
2593 __ Srav(dst_high, lhs_high, rhs_reg);
2594 __ Nor(AT, ZERO, rhs_reg);
2595 __ Sll(TMP, lhs_high, 1);
2596 __ Sllv(TMP, TMP, AT);
2597 __ Srlv(dst_low, lhs_low, rhs_reg);
2598 __ Or(dst_low, dst_low, TMP);
2599 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002600 if (isR6) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002601 __ Beqzc(TMP, &done, /* is_bare= */ true);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002602 __ Move(dst_low, dst_high);
2603 __ Sra(dst_high, dst_high, 31);
2604 } else {
2605 __ Sra(AT, dst_high, 31);
2606 __ Movn(dst_low, dst_high, TMP);
2607 __ Movn(dst_high, AT, TMP);
2608 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002609 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002610 __ Srlv(dst_high, lhs_high, rhs_reg);
2611 __ Nor(AT, ZERO, rhs_reg);
2612 __ Sll(TMP, lhs_high, 1);
2613 __ Sllv(TMP, TMP, AT);
2614 __ Srlv(dst_low, lhs_low, rhs_reg);
2615 __ Or(dst_low, dst_low, TMP);
2616 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002617 if (isR6) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002618 __ Beqzc(TMP, &done, /* is_bare= */ true);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002619 __ Move(dst_low, dst_high);
2620 __ Move(dst_high, ZERO);
2621 } else {
2622 __ Movn(dst_low, dst_high, TMP);
2623 __ Movn(dst_high, ZERO, TMP);
2624 }
2625 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002626 __ Nor(AT, ZERO, rhs_reg);
2627 __ Srlv(TMP, lhs_low, rhs_reg);
2628 __ Sll(dst_low, lhs_high, 1);
2629 __ Sllv(dst_low, dst_low, AT);
2630 __ Or(dst_low, dst_low, TMP);
2631 __ Srlv(TMP, lhs_high, rhs_reg);
2632 __ Sll(dst_high, lhs_low, 1);
2633 __ Sllv(dst_high, dst_high, AT);
2634 __ Or(dst_high, dst_high, TMP);
2635 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002636 if (isR6) {
Andreas Gampe3db70682018-12-26 15:12:03 -08002637 __ Beqzc(TMP, &done, /* is_bare= */ true);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002638 __ Move(TMP, dst_high);
2639 __ Move(dst_high, dst_low);
2640 __ Move(dst_low, TMP);
2641 } else {
2642 __ Movn(AT, dst_high, TMP);
2643 __ Movn(dst_high, dst_low, TMP);
2644 __ Movn(dst_low, AT, TMP);
2645 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002646 }
2647 __ Bind(&done);
2648 }
2649 break;
2650 }
2651
2652 default:
2653 LOG(FATAL) << "Unexpected shift operation type " << type;
2654 }
2655}
2656
2657void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2658 HandleBinaryOp(instruction);
2659}
2660
2661void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2662 HandleBinaryOp(instruction);
2663}
2664
2665void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2666 HandleBinaryOp(instruction);
2667}
2668
2669void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2670 HandleBinaryOp(instruction);
2671}
2672
2673void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002674 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002675 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002676 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002677 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002678 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2679 object_array_get_with_read_barrier
2680 ? LocationSummary::kCallOnSlowPath
2681 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002682 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2683 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2684 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002685 locations->SetInAt(0, Location::RequiresRegister());
2686 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002687 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002688 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2689 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002690 // The output overlaps in the case of an object array get with
2691 // read barriers enabled: we do not want the move to overwrite the
2692 // array's location, as we need it to emit the read barrier.
2693 locations->SetOut(Location::RequiresRegister(),
2694 object_array_get_with_read_barrier
2695 ? Location::kOutputOverlap
2696 : Location::kNoOutputOverlap);
2697 }
2698 // We need a temporary register for the read barrier marking slow
2699 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2700 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002701 bool temp_needed = instruction->GetIndex()->IsConstant()
2702 ? !kBakerReadBarrierThunksEnableForFields
2703 : !kBakerReadBarrierThunksEnableForArrays;
2704 if (temp_needed) {
2705 locations->AddTemp(Location::RequiresRegister());
2706 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002707 }
2708}
2709
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002710static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2711 auto null_checker = [codegen, instruction]() {
2712 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002713 };
2714 return null_checker;
2715}
2716
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002717void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2718 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002719 Location obj_loc = locations->InAt(0);
2720 Register obj = obj_loc.AsRegister<Register>();
2721 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002722 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002723 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002724 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002726 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002727 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2728 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002729 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002730 case DataType::Type::kBool:
2731 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002732 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002733 if (index.IsConstant()) {
2734 size_t offset =
2735 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002736 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002737 } else {
2738 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002739 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002740 }
2741 break;
2742 }
2743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002745 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002746 if (index.IsConstant()) {
2747 size_t offset =
2748 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002749 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002750 } else {
2751 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002752 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002753 }
2754 break;
2755 }
2756
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002757 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002758 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002759 if (maybe_compressed_char_at) {
2760 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2761 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2762 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2763 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2764 "Expecting 0=compressed, 1=uncompressed");
2765 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002766 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002767 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2768 if (maybe_compressed_char_at) {
2769 MipsLabel uncompressed_load, done;
2770 __ Bnez(TMP, &uncompressed_load);
2771 __ LoadFromOffset(kLoadUnsignedByte,
2772 out,
2773 obj,
2774 data_offset + (const_index << TIMES_1));
2775 __ B(&done);
2776 __ Bind(&uncompressed_load);
2777 __ LoadFromOffset(kLoadUnsignedHalfword,
2778 out,
2779 obj,
2780 data_offset + (const_index << TIMES_2));
2781 __ Bind(&done);
2782 } else {
2783 __ LoadFromOffset(kLoadUnsignedHalfword,
2784 out,
2785 obj,
2786 data_offset + (const_index << TIMES_2),
2787 null_checker);
2788 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002789 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002790 Register index_reg = index.AsRegister<Register>();
2791 if (maybe_compressed_char_at) {
2792 MipsLabel uncompressed_load, done;
2793 __ Bnez(TMP, &uncompressed_load);
2794 __ Addu(TMP, obj, index_reg);
2795 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2796 __ B(&done);
2797 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002798 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002799 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2800 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002801 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2802 __ Addu(TMP, index_reg, obj);
2803 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002804 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002805 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002806 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2807 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002808 }
2809 break;
2810 }
2811
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002812 case DataType::Type::kInt16: {
2813 Register out = out_loc.AsRegister<Register>();
2814 if (index.IsConstant()) {
2815 size_t offset =
2816 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2817 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002818 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2819 __ Addu(TMP, index.AsRegister<Register>(), obj);
2820 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002821 } else {
2822 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2823 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2824 }
2825 break;
2826 }
2827
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002828 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002829 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002830 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002831 if (index.IsConstant()) {
2832 size_t offset =
2833 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002834 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002835 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2836 __ Addu(TMP, index.AsRegister<Register>(), obj);
2837 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002838 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002839 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002840 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002841 }
2842 break;
2843 }
2844
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002845 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002846 static_assert(
2847 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2848 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2849 // /* HeapReference<Object> */ out =
2850 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2851 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002852 bool temp_needed = index.IsConstant()
2853 ? !kBakerReadBarrierThunksEnableForFields
2854 : !kBakerReadBarrierThunksEnableForArrays;
2855 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002856 // Note that a potential implicit null check is handled in this
2857 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002858 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2859 if (index.IsConstant()) {
2860 // Array load with a constant index can be treated as a field load.
2861 size_t offset =
2862 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2863 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2864 out_loc,
2865 obj,
2866 offset,
2867 temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002868 /* needs_null_check= */ false);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002869 } else {
2870 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2871 out_loc,
2872 obj,
2873 data_offset,
2874 index,
2875 temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08002876 /* needs_null_check= */ false);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002877 }
Alexey Frunze15958152017-02-09 19:08:30 -08002878 } else {
2879 Register out = out_loc.AsRegister<Register>();
2880 if (index.IsConstant()) {
2881 size_t offset =
2882 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2883 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2884 // If read barriers are enabled, emit read barriers other than
2885 // Baker's using a slow path (and also unpoison the loaded
2886 // reference, if heap poisoning is enabled).
2887 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2888 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002889 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002890 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2891 // If read barriers are enabled, emit read barriers other than
2892 // Baker's using a slow path (and also unpoison the loaded
2893 // reference, if heap poisoning is enabled).
2894 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2895 out_loc,
2896 out_loc,
2897 obj_loc,
2898 data_offset,
2899 index);
2900 }
2901 }
2902 break;
2903 }
2904
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002905 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002906 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002907 if (index.IsConstant()) {
2908 size_t offset =
2909 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002910 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002911 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2912 __ Addu(TMP, index.AsRegister<Register>(), obj);
2913 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002914 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002915 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002916 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002917 }
2918 break;
2919 }
2920
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002921 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002922 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002923 if (index.IsConstant()) {
2924 size_t offset =
2925 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002926 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002927 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2928 __ Addu(TMP, index.AsRegister<Register>(), obj);
2929 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002930 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002931 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002932 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002933 }
2934 break;
2935 }
2936
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002937 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002938 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002939 if (index.IsConstant()) {
2940 size_t offset =
2941 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002942 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002943 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2944 __ Addu(TMP, index.AsRegister<Register>(), obj);
2945 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002946 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002947 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002948 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002949 }
2950 break;
2951 }
2952
Aart Bik66c158e2018-01-31 12:55:04 -08002953 case DataType::Type::kUint32:
2954 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002955 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002956 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2957 UNREACHABLE();
2958 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002959}
2960
2961void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002962 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002963 locations->SetInAt(0, Location::RequiresRegister());
2964 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2965}
2966
2967void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2968 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002969 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002970 Register obj = locations->InAt(0).AsRegister<Register>();
2971 Register out = locations->Out().AsRegister<Register>();
2972 __ LoadFromOffset(kLoadWord, out, obj, offset);
2973 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002974 // Mask out compression flag from String's array length.
2975 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2976 __ Srl(out, out, 1u);
2977 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002978}
2979
Alexey Frunzef58b2482016-09-02 22:14:06 -07002980Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2981 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2982 ? Location::ConstantLocation(instruction->AsConstant())
2983 : Location::RequiresRegister();
2984}
2985
2986Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2987 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2988 // We can store a non-zero float or double constant without first loading it into the FPU,
2989 // but we should only prefer this if the constant has a single use.
2990 if (instruction->IsConstant() &&
2991 (instruction->AsConstant()->IsZeroBitPattern() ||
2992 instruction->GetUses().HasExactlyOneElement())) {
2993 return Location::ConstantLocation(instruction->AsConstant());
2994 // Otherwise fall through and require an FPU register for the constant.
2995 }
2996 return Location::RequiresFpuRegister();
2997}
2998
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002999void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003000 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08003001
3002 bool needs_write_barrier =
3003 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
3004 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
3005
Vladimir Markoca6fff82017-10-03 14:49:14 +01003006 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003007 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08003008 may_need_runtime_call_for_type_check ?
3009 LocationSummary::kCallOnSlowPath :
3010 LocationSummary::kNoCall);
3011
3012 locations->SetInAt(0, Location::RequiresRegister());
3013 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003014 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08003015 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003016 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08003017 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
3018 }
3019 if (needs_write_barrier) {
3020 // Temporary register for the write barrier.
3021 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003022 }
3023}
3024
3025void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
3026 LocationSummary* locations = instruction->GetLocations();
3027 Register obj = locations->InAt(0).AsRegister<Register>();
3028 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003029 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003030 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08003031 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003032 bool needs_write_barrier =
3033 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003034 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003035 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003036
3037 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003038 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003039 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003040 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003041 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_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_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003044 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003045 __ Addu(base_reg, obj, index.AsRegister<Register>());
3046 }
3047 if (value_location.IsConstant()) {
3048 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3049 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
3050 } else {
3051 Register value = value_location.AsRegister<Register>();
3052 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003053 }
3054 break;
3055 }
3056
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003057 case DataType::Type::kUint16:
3058 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003059 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003060 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003061 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02003062 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3063 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003064 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003065 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003066 }
3067 if (value_location.IsConstant()) {
3068 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3069 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
3070 } else {
3071 Register value = value_location.AsRegister<Register>();
3072 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003073 }
3074 break;
3075 }
3076
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003077 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08003078 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3079 if (index.IsConstant()) {
3080 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003081 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3082 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08003083 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003084 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003085 }
3086 if (value_location.IsConstant()) {
3087 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3088 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3089 } else {
3090 Register value = value_location.AsRegister<Register>();
3091 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3092 }
3093 break;
3094 }
3095
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003096 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08003097 if (value_location.IsConstant()) {
3098 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003099 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003100 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003101 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003102 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003103 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003104 }
Alexey Frunze15958152017-02-09 19:08:30 -08003105 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3106 DCHECK_EQ(value, 0);
3107 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3108 DCHECK(!needs_write_barrier);
3109 DCHECK(!may_need_runtime_call_for_type_check);
3110 break;
3111 }
3112
3113 DCHECK(needs_write_barrier);
3114 Register value = value_location.AsRegister<Register>();
3115 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3116 Register temp2 = TMP; // Doesn't need to survive slow path.
3117 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3118 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3119 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3120 MipsLabel done;
3121 SlowPathCodeMIPS* slow_path = nullptr;
3122
3123 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003124 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003125 codegen_->AddSlowPath(slow_path);
3126 if (instruction->GetValueCanBeNull()) {
3127 MipsLabel non_zero;
3128 __ Bnez(value, &non_zero);
3129 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3130 if (index.IsConstant()) {
3131 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003132 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3133 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003134 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003135 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003136 }
Alexey Frunze15958152017-02-09 19:08:30 -08003137 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3138 __ B(&done);
3139 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003140 }
Alexey Frunze15958152017-02-09 19:08:30 -08003141
3142 // Note that when read barriers are enabled, the type checks
3143 // are performed without read barriers. This is fine, even in
3144 // the case where a class object is in the from-space after
3145 // the flip, as a comparison involving such a type would not
3146 // produce a false positive; it may of course produce a false
3147 // negative, in which case we would take the ArraySet slow
3148 // path.
3149
3150 // /* HeapReference<Class> */ temp1 = obj->klass_
3151 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3152 __ MaybeUnpoisonHeapReference(temp1);
3153
3154 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3155 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3156 // /* HeapReference<Class> */ temp2 = value->klass_
3157 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3158 // If heap poisoning is enabled, no need to unpoison `temp1`
3159 // nor `temp2`, as we are comparing two poisoned references.
3160
3161 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3162 MipsLabel do_put;
3163 __ Beq(temp1, temp2, &do_put);
3164 // If heap poisoning is enabled, the `temp1` reference has
3165 // not been unpoisoned yet; unpoison it now.
3166 __ MaybeUnpoisonHeapReference(temp1);
3167
3168 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3169 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3170 // If heap poisoning is enabled, no need to unpoison
3171 // `temp1`, as we are comparing against null below.
3172 __ Bnez(temp1, slow_path->GetEntryLabel());
3173 __ Bind(&do_put);
3174 } else {
3175 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3176 }
3177 }
3178
3179 Register source = value;
3180 if (kPoisonHeapReferences) {
3181 // Note that in the case where `value` is a null reference,
3182 // we do not enter this block, as a null reference does not
3183 // need poisoning.
3184 __ Move(temp1, value);
3185 __ PoisonHeapReference(temp1);
3186 source = temp1;
3187 }
3188
3189 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3190 if (index.IsConstant()) {
3191 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003192 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003193 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003194 }
3195 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3196
3197 if (!may_need_runtime_call_for_type_check) {
3198 codegen_->MaybeRecordImplicitNullCheck(instruction);
3199 }
3200
3201 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3202
3203 if (done.IsLinked()) {
3204 __ Bind(&done);
3205 }
3206
3207 if (slow_path != nullptr) {
3208 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003209 }
3210 break;
3211 }
3212
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003213 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003214 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003215 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003216 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003217 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3218 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003219 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003220 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003221 }
3222 if (value_location.IsConstant()) {
3223 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3224 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3225 } else {
3226 Register value = value_location.AsRegisterPairLow<Register>();
3227 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003228 }
3229 break;
3230 }
3231
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003232 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003233 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003234 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003235 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003236 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3237 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003238 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003239 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003240 }
3241 if (value_location.IsConstant()) {
3242 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3243 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3244 } else {
3245 FRegister value = value_location.AsFpuRegister<FRegister>();
3246 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003247 }
3248 break;
3249 }
3250
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003251 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003252 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003253 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003254 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003255 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3256 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003257 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003258 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003259 }
3260 if (value_location.IsConstant()) {
3261 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3262 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3263 } else {
3264 FRegister value = value_location.AsFpuRegister<FRegister>();
3265 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003266 }
3267 break;
3268 }
3269
Aart Bik66c158e2018-01-31 12:55:04 -08003270 case DataType::Type::kUint32:
3271 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003272 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003273 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3274 UNREACHABLE();
3275 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003276}
3277
Lena Djokica2901602017-09-21 13:50:52 +02003278void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3279 HIntermediateArrayAddressIndex* instruction) {
3280 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003281 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003282
3283 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3284
3285 locations->SetInAt(0, Location::RequiresRegister());
3286 locations->SetInAt(1, Location::ConstantLocation(shift));
3287 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3288}
3289
3290void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3291 HIntermediateArrayAddressIndex* instruction) {
3292 LocationSummary* locations = instruction->GetLocations();
3293 Register index_reg = locations->InAt(0).AsRegister<Register>();
3294 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3295 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3296}
3297
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003298void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003299 RegisterSet caller_saves = RegisterSet::Empty();
3300 InvokeRuntimeCallingConvention calling_convention;
3301 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3302 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3303 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003304
3305 HInstruction* index = instruction->InputAt(0);
3306 HInstruction* length = instruction->InputAt(1);
3307
3308 bool const_index = false;
3309 bool const_length = false;
3310
3311 if (index->IsConstant()) {
3312 if (length->IsConstant()) {
3313 const_index = true;
3314 const_length = true;
3315 } else {
3316 int32_t index_value = index->AsIntConstant()->GetValue();
3317 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3318 const_index = true;
3319 }
3320 }
3321 } else if (length->IsConstant()) {
3322 int32_t length_value = length->AsIntConstant()->GetValue();
3323 if (IsUint<15>(length_value)) {
3324 const_length = true;
3325 }
3326 }
3327
3328 locations->SetInAt(0, const_index
3329 ? Location::ConstantLocation(index->AsConstant())
3330 : Location::RequiresRegister());
3331 locations->SetInAt(1, const_length
3332 ? Location::ConstantLocation(length->AsConstant())
3333 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003334}
3335
3336void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3337 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003338 Location index_loc = locations->InAt(0);
3339 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003340
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003341 if (length_loc.IsConstant()) {
3342 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3343 if (index_loc.IsConstant()) {
3344 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3345 if (index < 0 || index >= length) {
3346 BoundsCheckSlowPathMIPS* slow_path =
3347 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3348 codegen_->AddSlowPath(slow_path);
3349 __ B(slow_path->GetEntryLabel());
3350 } else {
3351 // Nothing to be done.
3352 }
3353 return;
3354 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003355
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003356 BoundsCheckSlowPathMIPS* slow_path =
3357 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3358 codegen_->AddSlowPath(slow_path);
3359 Register index = index_loc.AsRegister<Register>();
3360 if (length == 0) {
3361 __ B(slow_path->GetEntryLabel());
3362 } else if (length == 1) {
3363 __ Bnez(index, slow_path->GetEntryLabel());
3364 } else {
3365 DCHECK(IsUint<15>(length)) << length;
3366 __ Sltiu(TMP, index, length);
3367 __ Beqz(TMP, slow_path->GetEntryLabel());
3368 }
3369 } else {
3370 Register length = length_loc.AsRegister<Register>();
3371 BoundsCheckSlowPathMIPS* slow_path =
3372 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3373 codegen_->AddSlowPath(slow_path);
3374 if (index_loc.IsConstant()) {
3375 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3376 if (index < 0) {
3377 __ B(slow_path->GetEntryLabel());
3378 } else if (index == 0) {
3379 __ Blez(length, slow_path->GetEntryLabel());
3380 } else {
3381 DCHECK(IsInt<16>(index + 1)) << index;
3382 __ Sltiu(TMP, length, index + 1);
3383 __ Bnez(TMP, slow_path->GetEntryLabel());
3384 }
3385 } else {
3386 Register index = index_loc.AsRegister<Register>();
3387 __ Bgeu(index, length, slow_path->GetEntryLabel());
3388 }
3389 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003390}
3391
Alexey Frunze15958152017-02-09 19:08:30 -08003392// Temp is used for read barrier.
3393static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3394 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003395 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003396 (kUseBakerReadBarrier ||
3397 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3398 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3399 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3400 return 1;
3401 }
3402 return 0;
3403}
3404
3405// Extra temp is used for read barrier.
3406static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3407 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3408}
3409
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003410void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003411 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003412 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003413 LocationSummary* locations =
3414 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003415 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003416 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3417 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3418 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3419 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3420 } else {
3421 locations->SetInAt(1, Location::RequiresRegister());
3422 }
Alexey Frunze15958152017-02-09 19:08:30 -08003423 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003424}
3425
3426void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003427 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003428 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003429 Location obj_loc = locations->InAt(0);
3430 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko175e7862018-03-27 09:03:13 +00003431 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08003432 Location temp_loc = locations->GetTemp(0);
3433 Register temp = temp_loc.AsRegister<Register>();
3434 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3435 DCHECK_LE(num_temps, 2u);
3436 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003437 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3438 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3439 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3440 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3441 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3442 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3443 const uint32_t object_array_data_offset =
3444 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3445 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003446
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003447 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003448 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003449 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3450 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003451 codegen_->AddSlowPath(slow_path);
3452
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003453 // Avoid this check if we know `obj` is not null.
3454 if (instruction->MustDoNullCheck()) {
3455 __ Beqz(obj, &done);
3456 }
3457
3458 switch (type_check_kind) {
3459 case TypeCheckKind::kExactCheck:
3460 case TypeCheckKind::kArrayCheck: {
3461 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003462 GenerateReferenceLoadTwoRegisters(instruction,
3463 temp_loc,
3464 obj_loc,
3465 class_offset,
3466 maybe_temp2_loc,
3467 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003468 // Jump to slow path for throwing the exception or doing a
3469 // more involved array check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003470 __ Bne(temp, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003471 break;
3472 }
3473
3474 case TypeCheckKind::kAbstractClassCheck: {
3475 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003476 GenerateReferenceLoadTwoRegisters(instruction,
3477 temp_loc,
3478 obj_loc,
3479 class_offset,
3480 maybe_temp2_loc,
3481 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003482 // If the class is abstract, we eagerly fetch the super class of the
3483 // object to avoid doing a comparison we know will fail.
3484 MipsLabel loop;
3485 __ Bind(&loop);
3486 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003487 GenerateReferenceLoadOneRegister(instruction,
3488 temp_loc,
3489 super_offset,
3490 maybe_temp2_loc,
3491 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003492 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3493 // exception.
3494 __ Beqz(temp, slow_path->GetEntryLabel());
3495 // Otherwise, compare the classes.
Vladimir Marko175e7862018-03-27 09:03:13 +00003496 __ Bne(temp, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003497 break;
3498 }
3499
3500 case TypeCheckKind::kClassHierarchyCheck: {
3501 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003502 GenerateReferenceLoadTwoRegisters(instruction,
3503 temp_loc,
3504 obj_loc,
3505 class_offset,
3506 maybe_temp2_loc,
3507 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003508 // Walk over the class hierarchy to find a match.
3509 MipsLabel loop;
3510 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00003511 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003512 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003513 GenerateReferenceLoadOneRegister(instruction,
3514 temp_loc,
3515 super_offset,
3516 maybe_temp2_loc,
3517 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003518 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3519 // exception. Otherwise, jump to the beginning of the loop.
3520 __ Bnez(temp, &loop);
3521 __ B(slow_path->GetEntryLabel());
3522 break;
3523 }
3524
3525 case TypeCheckKind::kArrayObjectCheck: {
3526 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003527 GenerateReferenceLoadTwoRegisters(instruction,
3528 temp_loc,
3529 obj_loc,
3530 class_offset,
3531 maybe_temp2_loc,
3532 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003533 // Do an exact check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003534 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003535 // Otherwise, we need to check that the object's class is a non-primitive array.
3536 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003537 GenerateReferenceLoadOneRegister(instruction,
3538 temp_loc,
3539 component_offset,
3540 maybe_temp2_loc,
3541 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003542 // If the component type is null, jump to the slow path to throw the exception.
3543 __ Beqz(temp, slow_path->GetEntryLabel());
3544 // Otherwise, the object is indeed an array, further check that this component
3545 // type is not a primitive type.
3546 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3547 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3548 __ Bnez(temp, slow_path->GetEntryLabel());
3549 break;
3550 }
3551
3552 case TypeCheckKind::kUnresolvedCheck:
3553 // We always go into the type check slow path for the unresolved check case.
3554 // We cannot directly call the CheckCast runtime entry point
3555 // without resorting to a type checking slow path here (i.e. by
3556 // calling InvokeRuntime directly), as it would require to
3557 // assign fixed registers for the inputs of this HInstanceOf
3558 // instruction (following the runtime calling convention), which
3559 // might be cluttered by the potential first read barrier
3560 // emission at the beginning of this method.
3561 __ B(slow_path->GetEntryLabel());
3562 break;
3563
3564 case TypeCheckKind::kInterfaceCheck: {
3565 // Avoid read barriers to improve performance of the fast path. We can not get false
3566 // positives by doing this.
3567 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003568 GenerateReferenceLoadTwoRegisters(instruction,
3569 temp_loc,
3570 obj_loc,
3571 class_offset,
3572 maybe_temp2_loc,
3573 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003574 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003575 GenerateReferenceLoadTwoRegisters(instruction,
3576 temp_loc,
3577 temp_loc,
3578 iftable_offset,
3579 maybe_temp2_loc,
3580 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003581 // Iftable is never null.
3582 __ Lw(TMP, temp, array_length_offset);
3583 // Loop through the iftable and check if any class matches.
3584 MipsLabel loop;
3585 __ Bind(&loop);
3586 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3587 __ Beqz(TMP, slow_path->GetEntryLabel());
3588 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3589 __ MaybeUnpoisonHeapReference(AT);
3590 // Go to next interface.
3591 __ Addiu(TMP, TMP, -2);
3592 // Compare the classes and continue the loop if they do not match.
Vladimir Marko175e7862018-03-27 09:03:13 +00003593 __ Bne(AT, cls.AsRegister<Register>(), &loop);
3594 break;
3595 }
3596
3597 case TypeCheckKind::kBitstringCheck: {
3598 // /* HeapReference<Class> */ temp = obj->klass_
3599 GenerateReferenceLoadTwoRegisters(instruction,
3600 temp_loc,
3601 obj_loc,
3602 class_offset,
3603 maybe_temp2_loc,
3604 kWithoutReadBarrier);
3605
3606 GenerateBitstringTypeCheckCompare(instruction, temp);
3607 __ Bnez(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003608 break;
3609 }
3610 }
3611
3612 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003613 __ Bind(slow_path->GetExitLabel());
3614}
3615
3616void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3617 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003618 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003619 locations->SetInAt(0, Location::RequiresRegister());
3620 if (check->HasUses()) {
3621 locations->SetOut(Location::SameAsFirstInput());
3622 }
Vladimir Marko3232dbb2018-07-25 15:42:46 +01003623 // Rely on the type initialization to save everything we need.
3624 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003625}
3626
3627void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3628 // We assume the class is not null.
Vladimir Markoa9f303c2018-07-20 16:43:56 +01003629 SlowPathCodeMIPS* slow_path =
3630 new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(check->GetLoadClass(), check);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003631 codegen_->AddSlowPath(slow_path);
3632 GenerateClassInitializationCheck(slow_path,
3633 check->GetLocations()->InAt(0).AsRegister<Register>());
3634}
3635
3636void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003637 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003638
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003639 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003640 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003641
3642 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003643 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003644 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003645 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003646 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003647 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003648 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003649 locations->SetInAt(0, Location::RequiresRegister());
3650 locations->SetInAt(1, Location::RequiresRegister());
3651 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3652 break;
3653
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003654 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003655 locations->SetInAt(0, Location::RequiresRegister());
3656 locations->SetInAt(1, Location::RequiresRegister());
3657 // Output overlaps because it is written before doing the low comparison.
3658 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3659 break;
3660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003661 case DataType::Type::kFloat32:
3662 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003663 locations->SetInAt(0, Location::RequiresFpuRegister());
3664 locations->SetInAt(1, Location::RequiresFpuRegister());
3665 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003666 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003667
3668 default:
3669 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3670 }
3671}
3672
3673void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3674 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003675 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003676 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003677 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003678
3679 // 0 if: left == right
3680 // 1 if: left > right
3681 // -1 if: left < right
3682 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003683 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003684 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003685 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003686 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003687 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003688 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003689 Register lhs = locations->InAt(0).AsRegister<Register>();
3690 Register rhs = locations->InAt(1).AsRegister<Register>();
3691 __ Slt(TMP, lhs, rhs);
3692 __ Slt(res, rhs, lhs);
3693 __ Subu(res, res, TMP);
3694 break;
3695 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003697 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003698 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3699 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3700 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3701 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3702 // TODO: more efficient (direct) comparison with a constant.
3703 __ Slt(TMP, lhs_high, rhs_high);
3704 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3705 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3706 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3707 __ Sltu(TMP, lhs_low, rhs_low);
3708 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3709 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3710 __ Bind(&done);
3711 break;
3712 }
3713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003714 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003715 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003716 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3717 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3718 MipsLabel done;
3719 if (isR6) {
3720 __ CmpEqS(FTMP, lhs, rhs);
3721 __ LoadConst32(res, 0);
3722 __ Bc1nez(FTMP, &done);
3723 if (gt_bias) {
3724 __ CmpLtS(FTMP, lhs, rhs);
3725 __ LoadConst32(res, -1);
3726 __ Bc1nez(FTMP, &done);
3727 __ LoadConst32(res, 1);
3728 } else {
3729 __ CmpLtS(FTMP, rhs, lhs);
3730 __ LoadConst32(res, 1);
3731 __ Bc1nez(FTMP, &done);
3732 __ LoadConst32(res, -1);
3733 }
3734 } else {
3735 if (gt_bias) {
3736 __ ColtS(0, lhs, rhs);
3737 __ LoadConst32(res, -1);
3738 __ Bc1t(0, &done);
3739 __ CeqS(0, lhs, rhs);
3740 __ LoadConst32(res, 1);
3741 __ Movt(res, ZERO, 0);
3742 } else {
3743 __ ColtS(0, rhs, lhs);
3744 __ LoadConst32(res, 1);
3745 __ Bc1t(0, &done);
3746 __ CeqS(0, lhs, rhs);
3747 __ LoadConst32(res, -1);
3748 __ Movt(res, ZERO, 0);
3749 }
3750 }
3751 __ Bind(&done);
3752 break;
3753 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003754 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003755 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003756 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3757 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3758 MipsLabel done;
3759 if (isR6) {
3760 __ CmpEqD(FTMP, lhs, rhs);
3761 __ LoadConst32(res, 0);
3762 __ Bc1nez(FTMP, &done);
3763 if (gt_bias) {
3764 __ CmpLtD(FTMP, lhs, rhs);
3765 __ LoadConst32(res, -1);
3766 __ Bc1nez(FTMP, &done);
3767 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003768 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003769 __ CmpLtD(FTMP, rhs, lhs);
3770 __ LoadConst32(res, 1);
3771 __ Bc1nez(FTMP, &done);
3772 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003773 }
3774 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003775 if (gt_bias) {
3776 __ ColtD(0, lhs, rhs);
3777 __ LoadConst32(res, -1);
3778 __ Bc1t(0, &done);
3779 __ CeqD(0, lhs, rhs);
3780 __ LoadConst32(res, 1);
3781 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003782 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003783 __ ColtD(0, rhs, lhs);
3784 __ LoadConst32(res, 1);
3785 __ Bc1t(0, &done);
3786 __ CeqD(0, lhs, rhs);
3787 __ LoadConst32(res, -1);
3788 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003789 }
3790 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003791 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003792 break;
3793 }
3794
3795 default:
3796 LOG(FATAL) << "Unimplemented compare type " << in_type;
3797 }
3798}
3799
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003800void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003801 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003802 switch (instruction->InputAt(0)->GetType()) {
3803 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003804 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003805 locations->SetInAt(0, Location::RequiresRegister());
3806 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3807 break;
3808
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003809 case DataType::Type::kFloat32:
3810 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003811 locations->SetInAt(0, Location::RequiresFpuRegister());
3812 locations->SetInAt(1, Location::RequiresFpuRegister());
3813 break;
3814 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003815 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003816 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3817 }
3818}
3819
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003820void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003821 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003822 return;
3823 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003824
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003825 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003826 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003827
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003828 switch (type) {
3829 default:
3830 // Integer case.
3831 GenerateIntCompare(instruction->GetCondition(), locations);
3832 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003833
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003834 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003835 GenerateLongCompare(instruction->GetCondition(), locations);
3836 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003837
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003838 case DataType::Type::kFloat32:
3839 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003840 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3841 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003842 }
3843}
3844
Alexey Frunze7e99e052015-11-24 19:28:01 -08003845void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3846 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003847
3848 LocationSummary* locations = instruction->GetLocations();
3849 Location second = locations->InAt(1);
3850 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003851 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003852 DCHECK(imm == 1 || imm == -1);
3853
Lena Djokic4b8025c2017-12-21 16:15:50 +01003854 if (instruction->GetResultType() == DataType::Type::kInt32) {
3855 Register out = locations->Out().AsRegister<Register>();
3856 Register dividend = locations->InAt(0).AsRegister<Register>();
3857
3858 if (instruction->IsRem()) {
3859 __ Move(out, ZERO);
3860 } else {
3861 if (imm == -1) {
3862 __ Subu(out, ZERO, dividend);
3863 } else if (out != dividend) {
3864 __ Move(out, dividend);
3865 }
3866 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003867 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003868 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3869 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3870 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3871 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3872 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3873
3874 if (instruction->IsRem()) {
3875 __ Move(out_high, ZERO);
3876 __ Move(out_low, ZERO);
3877 } else {
3878 if (imm == -1) {
3879 __ Subu(out_low, ZERO, in_low);
3880 __ Sltu(AT, ZERO, out_low);
3881 __ Subu(out_high, ZERO, in_high);
3882 __ Subu(out_high, out_high, AT);
3883 } else {
3884 __ Move(out_low, in_low);
3885 __ Move(out_high, in_high);
3886 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003887 }
3888 }
3889}
3890
3891void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3892 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003893
3894 LocationSummary* locations = instruction->GetLocations();
3895 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003896 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3897 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003898 DCHECK(second.IsConstant());
3899
Lena Djokic4b8025c2017-12-21 16:15:50 +01003900 if (instruction->GetResultType() == DataType::Type::kInt32) {
3901 Register out = locations->Out().AsRegister<Register>();
3902 Register dividend = locations->InAt(0).AsRegister<Register>();
3903 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3904 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3905 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003906
Lena Djokic4b8025c2017-12-21 16:15:50 +01003907 if (instruction->IsDiv()) {
3908 if (ctz_imm == 1) {
3909 // Fast path for division by +/-2, which is very common.
3910 __ Srl(TMP, dividend, 31);
3911 } else {
3912 __ Sra(TMP, dividend, 31);
3913 __ Srl(TMP, TMP, 32 - ctz_imm);
3914 }
3915 __ Addu(out, dividend, TMP);
3916 __ Sra(out, out, ctz_imm);
3917 if (imm < 0) {
3918 __ Subu(out, ZERO, out);
3919 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003920 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003921 if (ctz_imm == 1) {
3922 // Fast path for modulo +/-2, which is very common.
3923 __ Sra(TMP, dividend, 31);
3924 __ Subu(out, dividend, TMP);
3925 __ Andi(out, out, 1);
3926 __ Addu(out, out, TMP);
3927 } else {
3928 __ Sra(TMP, dividend, 31);
3929 __ Srl(TMP, TMP, 32 - ctz_imm);
3930 __ Addu(out, dividend, TMP);
3931 if (IsUint<16>(abs_imm - 1)) {
3932 __ Andi(out, out, abs_imm - 1);
3933 } else {
3934 if (is_r2_or_newer) {
3935 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3936 } else {
3937 __ Sll(out, out, 32 - ctz_imm);
3938 __ Srl(out, out, 32 - ctz_imm);
3939 }
3940 }
3941 __ Subu(out, out, TMP);
3942 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003943 }
3944 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003945 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3946 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3947 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3948 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3949 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3950 int64_t imm = Int64FromConstant(second.GetConstant());
3951 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3952 int ctz_imm = CTZ(abs_imm);
3953
3954 if (instruction->IsDiv()) {
3955 if (ctz_imm < 32) {
3956 if (ctz_imm == 1) {
3957 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003958 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003959 __ Sra(AT, in_high, 31);
3960 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003961 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003962 __ Addu(AT, AT, in_low);
3963 __ Sltu(TMP, AT, in_low);
3964 __ Addu(out_high, in_high, TMP);
3965 __ Srl(out_low, AT, ctz_imm);
3966 if (is_r2_or_newer) {
3967 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3968 __ Sra(out_high, out_high, ctz_imm);
3969 } else {
3970 __ Sll(AT, out_high, 32 - ctz_imm);
3971 __ Sra(out_high, out_high, ctz_imm);
3972 __ Or(out_low, out_low, AT);
3973 }
3974 if (imm < 0) {
3975 __ Subu(out_low, ZERO, out_low);
3976 __ Sltu(AT, ZERO, out_low);
3977 __ Subu(out_high, ZERO, out_high);
3978 __ Subu(out_high, out_high, AT);
3979 }
3980 } else if (ctz_imm == 32) {
3981 __ Sra(AT, in_high, 31);
3982 __ Addu(AT, AT, in_low);
3983 __ Sltu(AT, AT, in_low);
3984 __ Addu(out_low, in_high, AT);
3985 if (imm < 0) {
3986 __ Srl(TMP, out_low, 31);
3987 __ Subu(out_low, ZERO, out_low);
3988 __ Sltu(AT, ZERO, out_low);
3989 __ Subu(out_high, TMP, AT);
3990 } else {
3991 __ Sra(out_high, out_low, 31);
3992 }
3993 } else if (ctz_imm < 63) {
3994 __ Sra(AT, in_high, 31);
3995 __ Srl(TMP, AT, 64 - ctz_imm);
3996 __ Addu(AT, AT, in_low);
3997 __ Sltu(AT, AT, in_low);
3998 __ Addu(out_low, in_high, AT);
3999 __ Addu(out_low, out_low, TMP);
4000 __ Sra(out_low, out_low, ctz_imm - 32);
4001 if (imm < 0) {
4002 __ Subu(out_low, ZERO, out_low);
4003 }
4004 __ Sra(out_high, out_low, 31);
4005 } else {
4006 DCHECK_LT(imm, 0);
4007 if (is_r6) {
4008 __ Aui(AT, in_high, 0x8000);
4009 } else {
4010 __ Lui(AT, 0x8000);
4011 __ Xor(AT, AT, in_high);
4012 }
4013 __ Or(AT, AT, in_low);
4014 __ Sltiu(out_low, AT, 1);
4015 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004016 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01004017 } else {
4018 if ((ctz_imm == 1) && !is_r6) {
4019 __ Andi(AT, in_low, 1);
4020 __ Sll(TMP, in_low, 31);
4021 __ And(TMP, in_high, TMP);
4022 __ Sra(out_high, TMP, 31);
4023 __ Or(out_low, out_high, AT);
4024 } else if (ctz_imm < 32) {
4025 __ Sra(AT, in_high, 31);
4026 if (ctz_imm <= 16) {
4027 __ Andi(out_low, in_low, abs_imm - 1);
4028 } else if (is_r2_or_newer) {
4029 __ Ext(out_low, in_low, 0, ctz_imm);
4030 } else {
4031 __ Sll(out_low, in_low, 32 - ctz_imm);
4032 __ Srl(out_low, out_low, 32 - ctz_imm);
4033 }
4034 if (is_r6) {
4035 __ Selnez(out_high, AT, out_low);
4036 } else {
4037 __ Movz(AT, ZERO, out_low);
4038 __ Move(out_high, AT);
4039 }
4040 if (is_r2_or_newer) {
4041 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
4042 } else {
4043 __ Sll(AT, out_high, ctz_imm);
4044 __ Or(out_low, out_low, AT);
4045 }
4046 } else if (ctz_imm == 32) {
4047 __ Sra(AT, in_high, 31);
4048 __ Move(out_low, in_low);
4049 if (is_r6) {
4050 __ Selnez(out_high, AT, out_low);
4051 } else {
4052 __ Movz(AT, ZERO, out_low);
4053 __ Move(out_high, AT);
4054 }
4055 } else if (ctz_imm < 63) {
4056 __ Sra(AT, in_high, 31);
4057 __ Move(TMP, in_low);
4058 if (ctz_imm - 32 <= 16) {
4059 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
4060 } else if (is_r2_or_newer) {
4061 __ Ext(out_high, in_high, 0, ctz_imm - 32);
4062 } else {
4063 __ Sll(out_high, in_high, 64 - ctz_imm);
4064 __ Srl(out_high, out_high, 64 - ctz_imm);
4065 }
4066 __ Move(out_low, TMP);
4067 __ Or(TMP, TMP, out_high);
4068 if (is_r6) {
4069 __ Selnez(AT, AT, TMP);
4070 } else {
4071 __ Movz(AT, ZERO, TMP);
4072 }
4073 if (is_r2_or_newer) {
4074 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
4075 } else {
4076 __ Sll(AT, AT, ctz_imm - 32);
4077 __ Or(out_high, out_high, AT);
4078 }
4079 } else {
4080 if (is_r6) {
4081 __ Aui(AT, in_high, 0x8000);
4082 } else {
4083 __ Lui(AT, 0x8000);
4084 __ Xor(AT, AT, in_high);
4085 }
4086 __ Or(AT, AT, in_low);
4087 __ Sltiu(AT, AT, 1);
4088 __ Sll(AT, AT, 31);
4089 __ Move(out_low, in_low);
4090 __ Xor(out_high, in_high, AT);
4091 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08004092 }
4093 }
4094}
4095
4096void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4097 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004098 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004099
4100 LocationSummary* locations = instruction->GetLocations();
4101 Location second = locations->InAt(1);
4102 DCHECK(second.IsConstant());
4103
4104 Register out = locations->Out().AsRegister<Register>();
4105 Register dividend = locations->InAt(0).AsRegister<Register>();
4106 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4107
4108 int64_t magic;
4109 int shift;
Andreas Gampe3db70682018-12-26 15:12:03 -08004110 CalculateMagicAndShiftForDivRem(imm, false /* is_long= */, &magic, &shift);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004111
4112 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4113
4114 __ LoadConst32(TMP, magic);
4115 if (isR6) {
4116 __ MuhR6(TMP, dividend, TMP);
4117 } else {
4118 __ MultR2(dividend, TMP);
4119 __ Mfhi(TMP);
4120 }
4121 if (imm > 0 && magic < 0) {
4122 __ Addu(TMP, TMP, dividend);
4123 } else if (imm < 0 && magic > 0) {
4124 __ Subu(TMP, TMP, dividend);
4125 }
4126
4127 if (shift != 0) {
4128 __ Sra(TMP, TMP, shift);
4129 }
4130
4131 if (instruction->IsDiv()) {
4132 __ Sra(out, TMP, 31);
4133 __ Subu(out, TMP, out);
4134 } else {
4135 __ Sra(AT, TMP, 31);
4136 __ Subu(AT, TMP, AT);
4137 __ LoadConst32(TMP, imm);
4138 if (isR6) {
4139 __ MulR6(TMP, AT, TMP);
4140 } else {
4141 __ MulR2(TMP, AT, TMP);
4142 }
4143 __ Subu(out, dividend, TMP);
4144 }
4145}
4146
4147void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4148 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004150
4151 LocationSummary* locations = instruction->GetLocations();
4152 Register out = locations->Out().AsRegister<Register>();
4153 Location second = locations->InAt(1);
4154
4155 if (second.IsConstant()) {
4156 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4157 if (imm == 0) {
4158 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4159 } else if (imm == 1 || imm == -1) {
4160 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004161 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004162 DivRemByPowerOfTwo(instruction);
4163 } else {
4164 DCHECK(imm <= -2 || imm >= 2);
4165 GenerateDivRemWithAnyConstant(instruction);
4166 }
4167 } else {
4168 Register dividend = locations->InAt(0).AsRegister<Register>();
4169 Register divisor = second.AsRegister<Register>();
4170 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4171 if (instruction->IsDiv()) {
4172 if (isR6) {
4173 __ DivR6(out, dividend, divisor);
4174 } else {
4175 __ DivR2(out, dividend, divisor);
4176 }
4177 } else {
4178 if (isR6) {
4179 __ ModR6(out, dividend, divisor);
4180 } else {
4181 __ ModR2(out, dividend, divisor);
4182 }
4183 }
4184 }
4185}
4186
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004187void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004188 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004189 bool call_long_div = false;
4190 if (type == DataType::Type::kInt64) {
4191 if (div->InputAt(1)->IsConstant()) {
4192 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4193 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4194 } else {
4195 call_long_div = true;
4196 }
4197 }
4198 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004199 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004200 : LocationSummary::kNoCall;
4201
Vladimir Markoca6fff82017-10-03 14:49:14 +01004202 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004203
4204 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004205 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004206 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004207 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004208 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4209 break;
4210
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004211 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004212 if (call_long_div) {
4213 InvokeRuntimeCallingConvention calling_convention;
4214 locations->SetInAt(0, Location::RegisterPairLocation(
4215 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4216 locations->SetInAt(1, Location::RegisterPairLocation(
4217 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4218 locations->SetOut(calling_convention.GetReturnLocation(type));
4219 } else {
4220 locations->SetInAt(0, Location::RequiresRegister());
4221 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4222 locations->SetOut(Location::RequiresRegister());
4223 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004224 break;
4225 }
4226
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004227 case DataType::Type::kFloat32:
4228 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004229 locations->SetInAt(0, Location::RequiresFpuRegister());
4230 locations->SetInAt(1, Location::RequiresFpuRegister());
4231 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4232 break;
4233
4234 default:
4235 LOG(FATAL) << "Unexpected div type " << type;
4236 }
4237}
4238
4239void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004240 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004241 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004242
4243 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004244 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004245 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004246 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004247 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004248 if (locations->InAt(1).IsConstant()) {
4249 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4250 if (imm == 0) {
4251 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4252 } else if (imm == 1 || imm == -1) {
4253 DivRemOneOrMinusOne(instruction);
4254 } else {
4255 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4256 DivRemByPowerOfTwo(instruction);
4257 }
4258 } else {
4259 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4260 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4261 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004262 break;
4263 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004264 case DataType::Type::kFloat32:
4265 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004266 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4267 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4268 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004269 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004270 __ DivS(dst, lhs, rhs);
4271 } else {
4272 __ DivD(dst, lhs, rhs);
4273 }
4274 break;
4275 }
4276 default:
4277 LOG(FATAL) << "Unexpected div type " << type;
4278 }
4279}
4280
4281void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004282 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004283 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004284}
4285
4286void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004287 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004288 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004289 codegen_->AddSlowPath(slow_path);
4290 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004291 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004292
4293 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004294 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004295 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004296 case DataType::Type::kInt8:
4297 case DataType::Type::kUint16:
4298 case DataType::Type::kInt16:
4299 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004300 if (value.IsConstant()) {
4301 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4302 __ B(slow_path->GetEntryLabel());
4303 } else {
4304 // A division by a non-null constant is valid. We don't need to perform
4305 // any check, so simply fall through.
4306 }
4307 } else {
4308 DCHECK(value.IsRegister()) << value;
4309 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4310 }
4311 break;
4312 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004313 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004314 if (value.IsConstant()) {
4315 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4316 __ B(slow_path->GetEntryLabel());
4317 } else {
4318 // A division by a non-null constant is valid. We don't need to perform
4319 // any check, so simply fall through.
4320 }
4321 } else {
4322 DCHECK(value.IsRegisterPair()) << value;
4323 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4324 __ Beqz(TMP, slow_path->GetEntryLabel());
4325 }
4326 break;
4327 }
4328 default:
4329 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4330 }
4331}
4332
4333void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4334 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004335 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004336 locations->SetOut(Location::ConstantLocation(constant));
4337}
4338
4339void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4340 // Will be generated at use site.
4341}
4342
4343void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4344 exit->SetLocations(nullptr);
4345}
4346
4347void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4348}
4349
4350void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4351 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004352 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004353 locations->SetOut(Location::ConstantLocation(constant));
4354}
4355
4356void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4357 // Will be generated at use site.
4358}
4359
4360void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4361 got->SetLocations(nullptr);
4362}
4363
4364void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004365 if (successor->IsExitBlock()) {
4366 DCHECK(got->GetPrevious()->AlwaysThrows());
4367 return; // no code needed
4368 }
4369
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004370 HBasicBlock* block = got->GetBlock();
4371 HInstruction* previous = got->GetPrevious();
4372 HLoopInformation* info = block->GetLoopInformation();
4373
4374 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004375 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4376 __ Lw(AT, SP, kCurrentMethodStackOffset);
4377 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4378 __ Addiu(TMP, TMP, 1);
4379 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4380 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004381 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4382 return;
4383 }
4384 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4385 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4386 }
4387 if (!codegen_->GoesToNextBlock(block, successor)) {
4388 __ B(codegen_->GetLabelOf(successor));
4389 }
4390}
4391
4392void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4393 HandleGoto(got, got->GetSuccessor());
4394}
4395
4396void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4397 try_boundary->SetLocations(nullptr);
4398}
4399
4400void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4401 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4402 if (!successor->IsExitBlock()) {
4403 HandleGoto(try_boundary, successor);
4404 }
4405}
4406
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004407void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4408 LocationSummary* locations) {
4409 Register dst = locations->Out().AsRegister<Register>();
4410 Register lhs = locations->InAt(0).AsRegister<Register>();
4411 Location rhs_location = locations->InAt(1);
4412 Register rhs_reg = ZERO;
4413 int64_t rhs_imm = 0;
4414 bool use_imm = rhs_location.IsConstant();
4415 if (use_imm) {
4416 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4417 } else {
4418 rhs_reg = rhs_location.AsRegister<Register>();
4419 }
4420
4421 switch (cond) {
4422 case kCondEQ:
4423 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004424 if (use_imm && IsInt<16>(-rhs_imm)) {
4425 if (rhs_imm == 0) {
4426 if (cond == kCondEQ) {
4427 __ Sltiu(dst, lhs, 1);
4428 } else {
4429 __ Sltu(dst, ZERO, lhs);
4430 }
4431 } else {
4432 __ Addiu(dst, lhs, -rhs_imm);
4433 if (cond == kCondEQ) {
4434 __ Sltiu(dst, dst, 1);
4435 } else {
4436 __ Sltu(dst, ZERO, dst);
4437 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004438 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004439 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004440 if (use_imm && IsUint<16>(rhs_imm)) {
4441 __ Xori(dst, lhs, rhs_imm);
4442 } else {
4443 if (use_imm) {
4444 rhs_reg = TMP;
4445 __ LoadConst32(rhs_reg, rhs_imm);
4446 }
4447 __ Xor(dst, lhs, rhs_reg);
4448 }
4449 if (cond == kCondEQ) {
4450 __ Sltiu(dst, dst, 1);
4451 } else {
4452 __ Sltu(dst, ZERO, dst);
4453 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004454 }
4455 break;
4456
4457 case kCondLT:
4458 case kCondGE:
4459 if (use_imm && IsInt<16>(rhs_imm)) {
4460 __ Slti(dst, lhs, rhs_imm);
4461 } else {
4462 if (use_imm) {
4463 rhs_reg = TMP;
4464 __ LoadConst32(rhs_reg, rhs_imm);
4465 }
4466 __ Slt(dst, lhs, rhs_reg);
4467 }
4468 if (cond == kCondGE) {
4469 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4470 // only the slt instruction but no sge.
4471 __ Xori(dst, dst, 1);
4472 }
4473 break;
4474
4475 case kCondLE:
4476 case kCondGT:
4477 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4478 // Simulate lhs <= rhs via lhs < rhs + 1.
4479 __ Slti(dst, lhs, rhs_imm + 1);
4480 if (cond == kCondGT) {
4481 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4482 // only the slti instruction but no sgti.
4483 __ Xori(dst, dst, 1);
4484 }
4485 } else {
4486 if (use_imm) {
4487 rhs_reg = TMP;
4488 __ LoadConst32(rhs_reg, rhs_imm);
4489 }
4490 __ Slt(dst, rhs_reg, lhs);
4491 if (cond == kCondLE) {
4492 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4493 // only the slt instruction but no sle.
4494 __ Xori(dst, dst, 1);
4495 }
4496 }
4497 break;
4498
4499 case kCondB:
4500 case kCondAE:
4501 if (use_imm && IsInt<16>(rhs_imm)) {
4502 // Sltiu sign-extends its 16-bit immediate operand before
4503 // the comparison and thus lets us compare directly with
4504 // unsigned values in the ranges [0, 0x7fff] and
4505 // [0xffff8000, 0xffffffff].
4506 __ Sltiu(dst, lhs, rhs_imm);
4507 } else {
4508 if (use_imm) {
4509 rhs_reg = TMP;
4510 __ LoadConst32(rhs_reg, rhs_imm);
4511 }
4512 __ Sltu(dst, lhs, rhs_reg);
4513 }
4514 if (cond == kCondAE) {
4515 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4516 // only the sltu instruction but no sgeu.
4517 __ Xori(dst, dst, 1);
4518 }
4519 break;
4520
4521 case kCondBE:
4522 case kCondA:
4523 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4524 // Simulate lhs <= rhs via lhs < rhs + 1.
4525 // Note that this only works if rhs + 1 does not overflow
4526 // to 0, hence the check above.
4527 // Sltiu sign-extends its 16-bit immediate operand before
4528 // the comparison and thus lets us compare directly with
4529 // unsigned values in the ranges [0, 0x7fff] and
4530 // [0xffff8000, 0xffffffff].
4531 __ Sltiu(dst, lhs, rhs_imm + 1);
4532 if (cond == kCondA) {
4533 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4534 // only the sltiu instruction but no sgtiu.
4535 __ Xori(dst, dst, 1);
4536 }
4537 } else {
4538 if (use_imm) {
4539 rhs_reg = TMP;
4540 __ LoadConst32(rhs_reg, rhs_imm);
4541 }
4542 __ Sltu(dst, rhs_reg, lhs);
4543 if (cond == kCondBE) {
4544 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4545 // only the sltu instruction but no sleu.
4546 __ Xori(dst, dst, 1);
4547 }
4548 }
4549 break;
4550 }
4551}
4552
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004553bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4554 LocationSummary* input_locations,
4555 Register dst) {
4556 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4557 Location rhs_location = input_locations->InAt(1);
4558 Register rhs_reg = ZERO;
4559 int64_t rhs_imm = 0;
4560 bool use_imm = rhs_location.IsConstant();
4561 if (use_imm) {
4562 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4563 } else {
4564 rhs_reg = rhs_location.AsRegister<Register>();
4565 }
4566
4567 switch (cond) {
4568 case kCondEQ:
4569 case kCondNE:
4570 if (use_imm && IsInt<16>(-rhs_imm)) {
4571 __ Addiu(dst, lhs, -rhs_imm);
4572 } else if (use_imm && IsUint<16>(rhs_imm)) {
4573 __ Xori(dst, lhs, rhs_imm);
4574 } else {
4575 if (use_imm) {
4576 rhs_reg = TMP;
4577 __ LoadConst32(rhs_reg, rhs_imm);
4578 }
4579 __ Xor(dst, lhs, rhs_reg);
4580 }
4581 return (cond == kCondEQ);
4582
4583 case kCondLT:
4584 case kCondGE:
4585 if (use_imm && IsInt<16>(rhs_imm)) {
4586 __ Slti(dst, lhs, rhs_imm);
4587 } else {
4588 if (use_imm) {
4589 rhs_reg = TMP;
4590 __ LoadConst32(rhs_reg, rhs_imm);
4591 }
4592 __ Slt(dst, lhs, rhs_reg);
4593 }
4594 return (cond == kCondGE);
4595
4596 case kCondLE:
4597 case kCondGT:
4598 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4599 // Simulate lhs <= rhs via lhs < rhs + 1.
4600 __ Slti(dst, lhs, rhs_imm + 1);
4601 return (cond == kCondGT);
4602 } else {
4603 if (use_imm) {
4604 rhs_reg = TMP;
4605 __ LoadConst32(rhs_reg, rhs_imm);
4606 }
4607 __ Slt(dst, rhs_reg, lhs);
4608 return (cond == kCondLE);
4609 }
4610
4611 case kCondB:
4612 case kCondAE:
4613 if (use_imm && IsInt<16>(rhs_imm)) {
4614 // Sltiu sign-extends its 16-bit immediate operand before
4615 // the comparison and thus lets us compare directly with
4616 // unsigned values in the ranges [0, 0x7fff] and
4617 // [0xffff8000, 0xffffffff].
4618 __ Sltiu(dst, lhs, rhs_imm);
4619 } else {
4620 if (use_imm) {
4621 rhs_reg = TMP;
4622 __ LoadConst32(rhs_reg, rhs_imm);
4623 }
4624 __ Sltu(dst, lhs, rhs_reg);
4625 }
4626 return (cond == kCondAE);
4627
4628 case kCondBE:
4629 case kCondA:
4630 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4631 // Simulate lhs <= rhs via lhs < rhs + 1.
4632 // Note that this only works if rhs + 1 does not overflow
4633 // to 0, hence the check above.
4634 // Sltiu sign-extends its 16-bit immediate operand before
4635 // the comparison and thus lets us compare directly with
4636 // unsigned values in the ranges [0, 0x7fff] and
4637 // [0xffff8000, 0xffffffff].
4638 __ Sltiu(dst, lhs, rhs_imm + 1);
4639 return (cond == kCondA);
4640 } else {
4641 if (use_imm) {
4642 rhs_reg = TMP;
4643 __ LoadConst32(rhs_reg, rhs_imm);
4644 }
4645 __ Sltu(dst, rhs_reg, lhs);
4646 return (cond == kCondBE);
4647 }
4648 }
4649}
4650
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004651void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4652 LocationSummary* locations,
4653 MipsLabel* label) {
4654 Register lhs = locations->InAt(0).AsRegister<Register>();
4655 Location rhs_location = locations->InAt(1);
4656 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004657 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004658 bool use_imm = rhs_location.IsConstant();
4659 if (use_imm) {
4660 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4661 } else {
4662 rhs_reg = rhs_location.AsRegister<Register>();
4663 }
4664
4665 if (use_imm && rhs_imm == 0) {
4666 switch (cond) {
4667 case kCondEQ:
4668 case kCondBE: // <= 0 if zero
4669 __ Beqz(lhs, label);
4670 break;
4671 case kCondNE:
4672 case kCondA: // > 0 if non-zero
4673 __ Bnez(lhs, label);
4674 break;
4675 case kCondLT:
4676 __ Bltz(lhs, label);
4677 break;
4678 case kCondGE:
4679 __ Bgez(lhs, label);
4680 break;
4681 case kCondLE:
4682 __ Blez(lhs, label);
4683 break;
4684 case kCondGT:
4685 __ Bgtz(lhs, label);
4686 break;
4687 case kCondB: // always false
4688 break;
4689 case kCondAE: // always true
4690 __ B(label);
4691 break;
4692 }
4693 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004694 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4695 if (isR6 || !use_imm) {
4696 if (use_imm) {
4697 rhs_reg = TMP;
4698 __ LoadConst32(rhs_reg, rhs_imm);
4699 }
4700 switch (cond) {
4701 case kCondEQ:
4702 __ Beq(lhs, rhs_reg, label);
4703 break;
4704 case kCondNE:
4705 __ Bne(lhs, rhs_reg, label);
4706 break;
4707 case kCondLT:
4708 __ Blt(lhs, rhs_reg, label);
4709 break;
4710 case kCondGE:
4711 __ Bge(lhs, rhs_reg, label);
4712 break;
4713 case kCondLE:
4714 __ Bge(rhs_reg, lhs, label);
4715 break;
4716 case kCondGT:
4717 __ Blt(rhs_reg, lhs, label);
4718 break;
4719 case kCondB:
4720 __ Bltu(lhs, rhs_reg, label);
4721 break;
4722 case kCondAE:
4723 __ Bgeu(lhs, rhs_reg, label);
4724 break;
4725 case kCondBE:
4726 __ Bgeu(rhs_reg, lhs, label);
4727 break;
4728 case kCondA:
4729 __ Bltu(rhs_reg, lhs, label);
4730 break;
4731 }
4732 } else {
4733 // Special cases for more efficient comparison with constants on R2.
4734 switch (cond) {
4735 case kCondEQ:
4736 __ LoadConst32(TMP, rhs_imm);
4737 __ Beq(lhs, TMP, label);
4738 break;
4739 case kCondNE:
4740 __ LoadConst32(TMP, rhs_imm);
4741 __ Bne(lhs, TMP, label);
4742 break;
4743 case kCondLT:
4744 if (IsInt<16>(rhs_imm)) {
4745 __ Slti(TMP, lhs, rhs_imm);
4746 __ Bnez(TMP, label);
4747 } else {
4748 __ LoadConst32(TMP, rhs_imm);
4749 __ Blt(lhs, TMP, label);
4750 }
4751 break;
4752 case kCondGE:
4753 if (IsInt<16>(rhs_imm)) {
4754 __ Slti(TMP, lhs, rhs_imm);
4755 __ Beqz(TMP, label);
4756 } else {
4757 __ LoadConst32(TMP, rhs_imm);
4758 __ Bge(lhs, TMP, label);
4759 }
4760 break;
4761 case kCondLE:
4762 if (IsInt<16>(rhs_imm + 1)) {
4763 // Simulate lhs <= rhs via lhs < rhs + 1.
4764 __ Slti(TMP, lhs, rhs_imm + 1);
4765 __ Bnez(TMP, label);
4766 } else {
4767 __ LoadConst32(TMP, rhs_imm);
4768 __ Bge(TMP, lhs, label);
4769 }
4770 break;
4771 case kCondGT:
4772 if (IsInt<16>(rhs_imm + 1)) {
4773 // Simulate lhs > rhs via !(lhs < rhs + 1).
4774 __ Slti(TMP, lhs, rhs_imm + 1);
4775 __ Beqz(TMP, label);
4776 } else {
4777 __ LoadConst32(TMP, rhs_imm);
4778 __ Blt(TMP, lhs, label);
4779 }
4780 break;
4781 case kCondB:
4782 if (IsInt<16>(rhs_imm)) {
4783 __ Sltiu(TMP, lhs, rhs_imm);
4784 __ Bnez(TMP, label);
4785 } else {
4786 __ LoadConst32(TMP, rhs_imm);
4787 __ Bltu(lhs, TMP, label);
4788 }
4789 break;
4790 case kCondAE:
4791 if (IsInt<16>(rhs_imm)) {
4792 __ Sltiu(TMP, lhs, rhs_imm);
4793 __ Beqz(TMP, label);
4794 } else {
4795 __ LoadConst32(TMP, rhs_imm);
4796 __ Bgeu(lhs, TMP, label);
4797 }
4798 break;
4799 case kCondBE:
4800 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4801 // Simulate lhs <= rhs via lhs < rhs + 1.
4802 // Note that this only works if rhs + 1 does not overflow
4803 // to 0, hence the check above.
4804 __ Sltiu(TMP, lhs, rhs_imm + 1);
4805 __ Bnez(TMP, label);
4806 } else {
4807 __ LoadConst32(TMP, rhs_imm);
4808 __ Bgeu(TMP, lhs, label);
4809 }
4810 break;
4811 case kCondA:
4812 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4813 // Simulate lhs > rhs via !(lhs < rhs + 1).
4814 // Note that this only works if rhs + 1 does not overflow
4815 // to 0, hence the check above.
4816 __ Sltiu(TMP, lhs, rhs_imm + 1);
4817 __ Beqz(TMP, label);
4818 } else {
4819 __ LoadConst32(TMP, rhs_imm);
4820 __ Bltu(TMP, lhs, label);
4821 }
4822 break;
4823 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004824 }
4825 }
4826}
4827
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004828void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4829 LocationSummary* locations) {
4830 Register dst = locations->Out().AsRegister<Register>();
4831 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4832 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4833 Location rhs_location = locations->InAt(1);
4834 Register rhs_high = ZERO;
4835 Register rhs_low = ZERO;
4836 int64_t imm = 0;
4837 uint32_t imm_high = 0;
4838 uint32_t imm_low = 0;
4839 bool use_imm = rhs_location.IsConstant();
4840 if (use_imm) {
4841 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4842 imm_high = High32Bits(imm);
4843 imm_low = Low32Bits(imm);
4844 } else {
4845 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4846 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4847 }
4848 if (use_imm && imm == 0) {
4849 switch (cond) {
4850 case kCondEQ:
4851 case kCondBE: // <= 0 if zero
4852 __ Or(dst, lhs_high, lhs_low);
4853 __ Sltiu(dst, dst, 1);
4854 break;
4855 case kCondNE:
4856 case kCondA: // > 0 if non-zero
4857 __ Or(dst, lhs_high, lhs_low);
4858 __ Sltu(dst, ZERO, dst);
4859 break;
4860 case kCondLT:
4861 __ Slt(dst, lhs_high, ZERO);
4862 break;
4863 case kCondGE:
4864 __ Slt(dst, lhs_high, ZERO);
4865 __ Xori(dst, dst, 1);
4866 break;
4867 case kCondLE:
4868 __ Or(TMP, lhs_high, lhs_low);
4869 __ Sra(AT, lhs_high, 31);
4870 __ Sltu(dst, AT, TMP);
4871 __ Xori(dst, dst, 1);
4872 break;
4873 case kCondGT:
4874 __ Or(TMP, lhs_high, lhs_low);
4875 __ Sra(AT, lhs_high, 31);
4876 __ Sltu(dst, AT, TMP);
4877 break;
4878 case kCondB: // always false
4879 __ Andi(dst, dst, 0);
4880 break;
4881 case kCondAE: // always true
4882 __ Ori(dst, ZERO, 1);
4883 break;
4884 }
4885 } else if (use_imm) {
4886 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4887 switch (cond) {
4888 case kCondEQ:
4889 __ LoadConst32(TMP, imm_high);
4890 __ Xor(TMP, TMP, lhs_high);
4891 __ LoadConst32(AT, imm_low);
4892 __ Xor(AT, AT, lhs_low);
4893 __ Or(dst, TMP, AT);
4894 __ Sltiu(dst, dst, 1);
4895 break;
4896 case kCondNE:
4897 __ LoadConst32(TMP, imm_high);
4898 __ Xor(TMP, TMP, lhs_high);
4899 __ LoadConst32(AT, imm_low);
4900 __ Xor(AT, AT, lhs_low);
4901 __ Or(dst, TMP, AT);
4902 __ Sltu(dst, ZERO, dst);
4903 break;
4904 case kCondLT:
4905 case kCondGE:
4906 if (dst == lhs_low) {
4907 __ LoadConst32(TMP, imm_low);
4908 __ Sltu(dst, lhs_low, TMP);
4909 }
4910 __ LoadConst32(TMP, imm_high);
4911 __ Slt(AT, lhs_high, TMP);
4912 __ Slt(TMP, TMP, lhs_high);
4913 if (dst != lhs_low) {
4914 __ LoadConst32(dst, imm_low);
4915 __ Sltu(dst, lhs_low, dst);
4916 }
4917 __ Slt(dst, TMP, dst);
4918 __ Or(dst, dst, AT);
4919 if (cond == kCondGE) {
4920 __ Xori(dst, dst, 1);
4921 }
4922 break;
4923 case kCondGT:
4924 case kCondLE:
4925 if (dst == lhs_low) {
4926 __ LoadConst32(TMP, imm_low);
4927 __ Sltu(dst, TMP, lhs_low);
4928 }
4929 __ LoadConst32(TMP, imm_high);
4930 __ Slt(AT, TMP, lhs_high);
4931 __ Slt(TMP, lhs_high, TMP);
4932 if (dst != lhs_low) {
4933 __ LoadConst32(dst, imm_low);
4934 __ Sltu(dst, dst, lhs_low);
4935 }
4936 __ Slt(dst, TMP, dst);
4937 __ Or(dst, dst, AT);
4938 if (cond == kCondLE) {
4939 __ Xori(dst, dst, 1);
4940 }
4941 break;
4942 case kCondB:
4943 case kCondAE:
4944 if (dst == lhs_low) {
4945 __ LoadConst32(TMP, imm_low);
4946 __ Sltu(dst, lhs_low, TMP);
4947 }
4948 __ LoadConst32(TMP, imm_high);
4949 __ Sltu(AT, lhs_high, TMP);
4950 __ Sltu(TMP, TMP, lhs_high);
4951 if (dst != lhs_low) {
4952 __ LoadConst32(dst, imm_low);
4953 __ Sltu(dst, lhs_low, dst);
4954 }
4955 __ Slt(dst, TMP, dst);
4956 __ Or(dst, dst, AT);
4957 if (cond == kCondAE) {
4958 __ Xori(dst, dst, 1);
4959 }
4960 break;
4961 case kCondA:
4962 case kCondBE:
4963 if (dst == lhs_low) {
4964 __ LoadConst32(TMP, imm_low);
4965 __ Sltu(dst, TMP, lhs_low);
4966 }
4967 __ LoadConst32(TMP, imm_high);
4968 __ Sltu(AT, TMP, lhs_high);
4969 __ Sltu(TMP, lhs_high, TMP);
4970 if (dst != lhs_low) {
4971 __ LoadConst32(dst, imm_low);
4972 __ Sltu(dst, dst, lhs_low);
4973 }
4974 __ Slt(dst, TMP, dst);
4975 __ Or(dst, dst, AT);
4976 if (cond == kCondBE) {
4977 __ Xori(dst, dst, 1);
4978 }
4979 break;
4980 }
4981 } else {
4982 switch (cond) {
4983 case kCondEQ:
4984 __ Xor(TMP, lhs_high, rhs_high);
4985 __ Xor(AT, lhs_low, rhs_low);
4986 __ Or(dst, TMP, AT);
4987 __ Sltiu(dst, dst, 1);
4988 break;
4989 case kCondNE:
4990 __ Xor(TMP, lhs_high, rhs_high);
4991 __ Xor(AT, lhs_low, rhs_low);
4992 __ Or(dst, TMP, AT);
4993 __ Sltu(dst, ZERO, dst);
4994 break;
4995 case kCondLT:
4996 case kCondGE:
4997 __ Slt(TMP, rhs_high, lhs_high);
4998 __ Sltu(AT, lhs_low, rhs_low);
4999 __ Slt(TMP, TMP, AT);
5000 __ Slt(AT, lhs_high, rhs_high);
5001 __ Or(dst, AT, TMP);
5002 if (cond == kCondGE) {
5003 __ Xori(dst, dst, 1);
5004 }
5005 break;
5006 case kCondGT:
5007 case kCondLE:
5008 __ Slt(TMP, lhs_high, rhs_high);
5009 __ Sltu(AT, rhs_low, lhs_low);
5010 __ Slt(TMP, TMP, AT);
5011 __ Slt(AT, rhs_high, lhs_high);
5012 __ Or(dst, AT, TMP);
5013 if (cond == kCondLE) {
5014 __ Xori(dst, dst, 1);
5015 }
5016 break;
5017 case kCondB:
5018 case kCondAE:
5019 __ Sltu(TMP, rhs_high, lhs_high);
5020 __ Sltu(AT, lhs_low, rhs_low);
5021 __ Slt(TMP, TMP, AT);
5022 __ Sltu(AT, lhs_high, rhs_high);
5023 __ Or(dst, AT, TMP);
5024 if (cond == kCondAE) {
5025 __ Xori(dst, dst, 1);
5026 }
5027 break;
5028 case kCondA:
5029 case kCondBE:
5030 __ Sltu(TMP, lhs_high, rhs_high);
5031 __ Sltu(AT, rhs_low, lhs_low);
5032 __ Slt(TMP, TMP, AT);
5033 __ Sltu(AT, rhs_high, lhs_high);
5034 __ Or(dst, AT, TMP);
5035 if (cond == kCondBE) {
5036 __ Xori(dst, dst, 1);
5037 }
5038 break;
5039 }
5040 }
5041}
5042
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005043void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
5044 LocationSummary* locations,
5045 MipsLabel* label) {
5046 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5047 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5048 Location rhs_location = locations->InAt(1);
5049 Register rhs_high = ZERO;
5050 Register rhs_low = ZERO;
5051 int64_t imm = 0;
5052 uint32_t imm_high = 0;
5053 uint32_t imm_low = 0;
5054 bool use_imm = rhs_location.IsConstant();
5055 if (use_imm) {
5056 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
5057 imm_high = High32Bits(imm);
5058 imm_low = Low32Bits(imm);
5059 } else {
5060 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
5061 rhs_low = rhs_location.AsRegisterPairLow<Register>();
5062 }
5063
5064 if (use_imm && imm == 0) {
5065 switch (cond) {
5066 case kCondEQ:
5067 case kCondBE: // <= 0 if zero
5068 __ Or(TMP, lhs_high, lhs_low);
5069 __ Beqz(TMP, label);
5070 break;
5071 case kCondNE:
5072 case kCondA: // > 0 if non-zero
5073 __ Or(TMP, lhs_high, lhs_low);
5074 __ Bnez(TMP, label);
5075 break;
5076 case kCondLT:
5077 __ Bltz(lhs_high, label);
5078 break;
5079 case kCondGE:
5080 __ Bgez(lhs_high, label);
5081 break;
5082 case kCondLE:
5083 __ Or(TMP, lhs_high, lhs_low);
5084 __ Sra(AT, lhs_high, 31);
5085 __ Bgeu(AT, TMP, label);
5086 break;
5087 case kCondGT:
5088 __ Or(TMP, lhs_high, lhs_low);
5089 __ Sra(AT, lhs_high, 31);
5090 __ Bltu(AT, TMP, label);
5091 break;
5092 case kCondB: // always false
5093 break;
5094 case kCondAE: // always true
5095 __ B(label);
5096 break;
5097 }
5098 } else if (use_imm) {
5099 // TODO: more efficient comparison with constants without loading them into TMP/AT.
5100 switch (cond) {
5101 case kCondEQ:
5102 __ LoadConst32(TMP, imm_high);
5103 __ Xor(TMP, TMP, lhs_high);
5104 __ LoadConst32(AT, imm_low);
5105 __ Xor(AT, AT, lhs_low);
5106 __ Or(TMP, TMP, AT);
5107 __ Beqz(TMP, label);
5108 break;
5109 case kCondNE:
5110 __ LoadConst32(TMP, imm_high);
5111 __ Xor(TMP, TMP, lhs_high);
5112 __ LoadConst32(AT, imm_low);
5113 __ Xor(AT, AT, lhs_low);
5114 __ Or(TMP, TMP, AT);
5115 __ Bnez(TMP, label);
5116 break;
5117 case kCondLT:
5118 __ LoadConst32(TMP, imm_high);
5119 __ Blt(lhs_high, TMP, label);
5120 __ Slt(TMP, TMP, lhs_high);
5121 __ LoadConst32(AT, imm_low);
5122 __ Sltu(AT, lhs_low, AT);
5123 __ Blt(TMP, AT, label);
5124 break;
5125 case kCondGE:
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, lhs_low, AT);
5131 __ Or(TMP, TMP, AT);
5132 __ Beqz(TMP, label);
5133 break;
5134 case kCondLE:
5135 __ LoadConst32(TMP, imm_high);
5136 __ Blt(lhs_high, TMP, label);
5137 __ Slt(TMP, TMP, lhs_high);
5138 __ LoadConst32(AT, imm_low);
5139 __ Sltu(AT, AT, lhs_low);
5140 __ Or(TMP, TMP, AT);
5141 __ Beqz(TMP, label);
5142 break;
5143 case kCondGT:
5144 __ LoadConst32(TMP, imm_high);
5145 __ Blt(TMP, lhs_high, label);
5146 __ Slt(TMP, lhs_high, TMP);
5147 __ LoadConst32(AT, imm_low);
5148 __ Sltu(AT, AT, lhs_low);
5149 __ Blt(TMP, AT, label);
5150 break;
5151 case kCondB:
5152 __ LoadConst32(TMP, imm_high);
5153 __ Bltu(lhs_high, TMP, label);
5154 __ Sltu(TMP, TMP, lhs_high);
5155 __ LoadConst32(AT, imm_low);
5156 __ Sltu(AT, lhs_low, AT);
5157 __ Blt(TMP, AT, label);
5158 break;
5159 case kCondAE:
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, lhs_low, AT);
5165 __ Or(TMP, TMP, AT);
5166 __ Beqz(TMP, label);
5167 break;
5168 case kCondBE:
5169 __ LoadConst32(TMP, imm_high);
5170 __ Bltu(lhs_high, TMP, label);
5171 __ Sltu(TMP, TMP, lhs_high);
5172 __ LoadConst32(AT, imm_low);
5173 __ Sltu(AT, AT, lhs_low);
5174 __ Or(TMP, TMP, AT);
5175 __ Beqz(TMP, label);
5176 break;
5177 case kCondA:
5178 __ LoadConst32(TMP, imm_high);
5179 __ Bltu(TMP, lhs_high, label);
5180 __ Sltu(TMP, lhs_high, TMP);
5181 __ LoadConst32(AT, imm_low);
5182 __ Sltu(AT, AT, lhs_low);
5183 __ Blt(TMP, AT, label);
5184 break;
5185 }
5186 } else {
5187 switch (cond) {
5188 case kCondEQ:
5189 __ Xor(TMP, lhs_high, rhs_high);
5190 __ Xor(AT, lhs_low, rhs_low);
5191 __ Or(TMP, TMP, AT);
5192 __ Beqz(TMP, label);
5193 break;
5194 case kCondNE:
5195 __ Xor(TMP, lhs_high, rhs_high);
5196 __ Xor(AT, lhs_low, rhs_low);
5197 __ Or(TMP, TMP, AT);
5198 __ Bnez(TMP, label);
5199 break;
5200 case kCondLT:
5201 __ Blt(lhs_high, rhs_high, label);
5202 __ Slt(TMP, rhs_high, lhs_high);
5203 __ Sltu(AT, lhs_low, rhs_low);
5204 __ Blt(TMP, AT, label);
5205 break;
5206 case kCondGE:
5207 __ Blt(rhs_high, lhs_high, label);
5208 __ Slt(TMP, lhs_high, rhs_high);
5209 __ Sltu(AT, lhs_low, rhs_low);
5210 __ Or(TMP, TMP, AT);
5211 __ Beqz(TMP, label);
5212 break;
5213 case kCondLE:
5214 __ Blt(lhs_high, rhs_high, label);
5215 __ Slt(TMP, rhs_high, lhs_high);
5216 __ Sltu(AT, rhs_low, lhs_low);
5217 __ Or(TMP, TMP, AT);
5218 __ Beqz(TMP, label);
5219 break;
5220 case kCondGT:
5221 __ Blt(rhs_high, lhs_high, label);
5222 __ Slt(TMP, lhs_high, rhs_high);
5223 __ Sltu(AT, rhs_low, lhs_low);
5224 __ Blt(TMP, AT, label);
5225 break;
5226 case kCondB:
5227 __ Bltu(lhs_high, rhs_high, label);
5228 __ Sltu(TMP, rhs_high, lhs_high);
5229 __ Sltu(AT, lhs_low, rhs_low);
5230 __ Blt(TMP, AT, label);
5231 break;
5232 case kCondAE:
5233 __ Bltu(rhs_high, lhs_high, label);
5234 __ Sltu(TMP, lhs_high, rhs_high);
5235 __ Sltu(AT, lhs_low, rhs_low);
5236 __ Or(TMP, TMP, AT);
5237 __ Beqz(TMP, label);
5238 break;
5239 case kCondBE:
5240 __ Bltu(lhs_high, rhs_high, label);
5241 __ Sltu(TMP, rhs_high, lhs_high);
5242 __ Sltu(AT, rhs_low, lhs_low);
5243 __ Or(TMP, TMP, AT);
5244 __ Beqz(TMP, label);
5245 break;
5246 case kCondA:
5247 __ Bltu(rhs_high, lhs_high, label);
5248 __ Sltu(TMP, lhs_high, rhs_high);
5249 __ Sltu(AT, rhs_low, lhs_low);
5250 __ Blt(TMP, AT, label);
5251 break;
5252 }
5253 }
5254}
5255
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005256void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5257 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005258 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005259 LocationSummary* locations) {
5260 Register dst = locations->Out().AsRegister<Register>();
5261 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5262 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5263 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005264 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005265 if (isR6) {
5266 switch (cond) {
5267 case kCondEQ:
5268 __ CmpEqS(FTMP, lhs, rhs);
5269 __ Mfc1(dst, FTMP);
5270 __ Andi(dst, dst, 1);
5271 break;
5272 case kCondNE:
5273 __ CmpEqS(FTMP, lhs, rhs);
5274 __ Mfc1(dst, FTMP);
5275 __ Addiu(dst, dst, 1);
5276 break;
5277 case kCondLT:
5278 if (gt_bias) {
5279 __ CmpLtS(FTMP, lhs, rhs);
5280 } else {
5281 __ CmpUltS(FTMP, lhs, rhs);
5282 }
5283 __ Mfc1(dst, FTMP);
5284 __ Andi(dst, dst, 1);
5285 break;
5286 case kCondLE:
5287 if (gt_bias) {
5288 __ CmpLeS(FTMP, lhs, rhs);
5289 } else {
5290 __ CmpUleS(FTMP, lhs, rhs);
5291 }
5292 __ Mfc1(dst, FTMP);
5293 __ Andi(dst, dst, 1);
5294 break;
5295 case kCondGT:
5296 if (gt_bias) {
5297 __ CmpUltS(FTMP, rhs, lhs);
5298 } else {
5299 __ CmpLtS(FTMP, rhs, lhs);
5300 }
5301 __ Mfc1(dst, FTMP);
5302 __ Andi(dst, dst, 1);
5303 break;
5304 case kCondGE:
5305 if (gt_bias) {
5306 __ CmpUleS(FTMP, rhs, lhs);
5307 } else {
5308 __ CmpLeS(FTMP, rhs, lhs);
5309 }
5310 __ Mfc1(dst, FTMP);
5311 __ Andi(dst, dst, 1);
5312 break;
5313 default:
5314 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5315 UNREACHABLE();
5316 }
5317 } else {
5318 switch (cond) {
5319 case kCondEQ:
5320 __ CeqS(0, lhs, rhs);
5321 __ LoadConst32(dst, 1);
5322 __ Movf(dst, ZERO, 0);
5323 break;
5324 case kCondNE:
5325 __ CeqS(0, lhs, rhs);
5326 __ LoadConst32(dst, 1);
5327 __ Movt(dst, ZERO, 0);
5328 break;
5329 case kCondLT:
5330 if (gt_bias) {
5331 __ ColtS(0, lhs, rhs);
5332 } else {
5333 __ CultS(0, lhs, rhs);
5334 }
5335 __ LoadConst32(dst, 1);
5336 __ Movf(dst, ZERO, 0);
5337 break;
5338 case kCondLE:
5339 if (gt_bias) {
5340 __ ColeS(0, lhs, rhs);
5341 } else {
5342 __ CuleS(0, lhs, rhs);
5343 }
5344 __ LoadConst32(dst, 1);
5345 __ Movf(dst, ZERO, 0);
5346 break;
5347 case kCondGT:
5348 if (gt_bias) {
5349 __ CultS(0, rhs, lhs);
5350 } else {
5351 __ ColtS(0, rhs, lhs);
5352 }
5353 __ LoadConst32(dst, 1);
5354 __ Movf(dst, ZERO, 0);
5355 break;
5356 case kCondGE:
5357 if (gt_bias) {
5358 __ CuleS(0, rhs, lhs);
5359 } else {
5360 __ ColeS(0, rhs, lhs);
5361 }
5362 __ LoadConst32(dst, 1);
5363 __ Movf(dst, ZERO, 0);
5364 break;
5365 default:
5366 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5367 UNREACHABLE();
5368 }
5369 }
5370 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005371 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005372 if (isR6) {
5373 switch (cond) {
5374 case kCondEQ:
5375 __ CmpEqD(FTMP, lhs, rhs);
5376 __ Mfc1(dst, FTMP);
5377 __ Andi(dst, dst, 1);
5378 break;
5379 case kCondNE:
5380 __ CmpEqD(FTMP, lhs, rhs);
5381 __ Mfc1(dst, FTMP);
5382 __ Addiu(dst, dst, 1);
5383 break;
5384 case kCondLT:
5385 if (gt_bias) {
5386 __ CmpLtD(FTMP, lhs, rhs);
5387 } else {
5388 __ CmpUltD(FTMP, lhs, rhs);
5389 }
5390 __ Mfc1(dst, FTMP);
5391 __ Andi(dst, dst, 1);
5392 break;
5393 case kCondLE:
5394 if (gt_bias) {
5395 __ CmpLeD(FTMP, lhs, rhs);
5396 } else {
5397 __ CmpUleD(FTMP, lhs, rhs);
5398 }
5399 __ Mfc1(dst, FTMP);
5400 __ Andi(dst, dst, 1);
5401 break;
5402 case kCondGT:
5403 if (gt_bias) {
5404 __ CmpUltD(FTMP, rhs, lhs);
5405 } else {
5406 __ CmpLtD(FTMP, rhs, lhs);
5407 }
5408 __ Mfc1(dst, FTMP);
5409 __ Andi(dst, dst, 1);
5410 break;
5411 case kCondGE:
5412 if (gt_bias) {
5413 __ CmpUleD(FTMP, rhs, lhs);
5414 } else {
5415 __ CmpLeD(FTMP, rhs, lhs);
5416 }
5417 __ Mfc1(dst, FTMP);
5418 __ Andi(dst, dst, 1);
5419 break;
5420 default:
5421 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5422 UNREACHABLE();
5423 }
5424 } else {
5425 switch (cond) {
5426 case kCondEQ:
5427 __ CeqD(0, lhs, rhs);
5428 __ LoadConst32(dst, 1);
5429 __ Movf(dst, ZERO, 0);
5430 break;
5431 case kCondNE:
5432 __ CeqD(0, lhs, rhs);
5433 __ LoadConst32(dst, 1);
5434 __ Movt(dst, ZERO, 0);
5435 break;
5436 case kCondLT:
5437 if (gt_bias) {
5438 __ ColtD(0, lhs, rhs);
5439 } else {
5440 __ CultD(0, lhs, rhs);
5441 }
5442 __ LoadConst32(dst, 1);
5443 __ Movf(dst, ZERO, 0);
5444 break;
5445 case kCondLE:
5446 if (gt_bias) {
5447 __ ColeD(0, lhs, rhs);
5448 } else {
5449 __ CuleD(0, lhs, rhs);
5450 }
5451 __ LoadConst32(dst, 1);
5452 __ Movf(dst, ZERO, 0);
5453 break;
5454 case kCondGT:
5455 if (gt_bias) {
5456 __ CultD(0, rhs, lhs);
5457 } else {
5458 __ ColtD(0, rhs, lhs);
5459 }
5460 __ LoadConst32(dst, 1);
5461 __ Movf(dst, ZERO, 0);
5462 break;
5463 case kCondGE:
5464 if (gt_bias) {
5465 __ CuleD(0, rhs, lhs);
5466 } else {
5467 __ ColeD(0, rhs, lhs);
5468 }
5469 __ LoadConst32(dst, 1);
5470 __ Movf(dst, ZERO, 0);
5471 break;
5472 default:
5473 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5474 UNREACHABLE();
5475 }
5476 }
5477 }
5478}
5479
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005480bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5481 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005482 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005483 LocationSummary* input_locations,
5484 int cc) {
5485 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5486 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5487 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005488 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005489 switch (cond) {
5490 case kCondEQ:
5491 __ CeqS(cc, lhs, rhs);
5492 return false;
5493 case kCondNE:
5494 __ CeqS(cc, lhs, rhs);
5495 return true;
5496 case kCondLT:
5497 if (gt_bias) {
5498 __ ColtS(cc, lhs, rhs);
5499 } else {
5500 __ CultS(cc, lhs, rhs);
5501 }
5502 return false;
5503 case kCondLE:
5504 if (gt_bias) {
5505 __ ColeS(cc, lhs, rhs);
5506 } else {
5507 __ CuleS(cc, lhs, rhs);
5508 }
5509 return false;
5510 case kCondGT:
5511 if (gt_bias) {
5512 __ CultS(cc, rhs, lhs);
5513 } else {
5514 __ ColtS(cc, rhs, lhs);
5515 }
5516 return false;
5517 case kCondGE:
5518 if (gt_bias) {
5519 __ CuleS(cc, rhs, lhs);
5520 } else {
5521 __ ColeS(cc, rhs, lhs);
5522 }
5523 return false;
5524 default:
5525 LOG(FATAL) << "Unexpected non-floating-point condition";
5526 UNREACHABLE();
5527 }
5528 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005529 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005530 switch (cond) {
5531 case kCondEQ:
5532 __ CeqD(cc, lhs, rhs);
5533 return false;
5534 case kCondNE:
5535 __ CeqD(cc, lhs, rhs);
5536 return true;
5537 case kCondLT:
5538 if (gt_bias) {
5539 __ ColtD(cc, lhs, rhs);
5540 } else {
5541 __ CultD(cc, lhs, rhs);
5542 }
5543 return false;
5544 case kCondLE:
5545 if (gt_bias) {
5546 __ ColeD(cc, lhs, rhs);
5547 } else {
5548 __ CuleD(cc, lhs, rhs);
5549 }
5550 return false;
5551 case kCondGT:
5552 if (gt_bias) {
5553 __ CultD(cc, rhs, lhs);
5554 } else {
5555 __ ColtD(cc, rhs, lhs);
5556 }
5557 return false;
5558 case kCondGE:
5559 if (gt_bias) {
5560 __ CuleD(cc, rhs, lhs);
5561 } else {
5562 __ ColeD(cc, rhs, lhs);
5563 }
5564 return false;
5565 default:
5566 LOG(FATAL) << "Unexpected non-floating-point condition";
5567 UNREACHABLE();
5568 }
5569 }
5570}
5571
5572bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5573 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005574 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005575 LocationSummary* input_locations,
5576 FRegister dst) {
5577 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5578 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5579 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005580 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005581 switch (cond) {
5582 case kCondEQ:
5583 __ CmpEqS(dst, lhs, rhs);
5584 return false;
5585 case kCondNE:
5586 __ CmpEqS(dst, lhs, rhs);
5587 return true;
5588 case kCondLT:
5589 if (gt_bias) {
5590 __ CmpLtS(dst, lhs, rhs);
5591 } else {
5592 __ CmpUltS(dst, lhs, rhs);
5593 }
5594 return false;
5595 case kCondLE:
5596 if (gt_bias) {
5597 __ CmpLeS(dst, lhs, rhs);
5598 } else {
5599 __ CmpUleS(dst, lhs, rhs);
5600 }
5601 return false;
5602 case kCondGT:
5603 if (gt_bias) {
5604 __ CmpUltS(dst, rhs, lhs);
5605 } else {
5606 __ CmpLtS(dst, rhs, lhs);
5607 }
5608 return false;
5609 case kCondGE:
5610 if (gt_bias) {
5611 __ CmpUleS(dst, rhs, lhs);
5612 } else {
5613 __ CmpLeS(dst, rhs, lhs);
5614 }
5615 return false;
5616 default:
5617 LOG(FATAL) << "Unexpected non-floating-point condition";
5618 UNREACHABLE();
5619 }
5620 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005621 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005622 switch (cond) {
5623 case kCondEQ:
5624 __ CmpEqD(dst, lhs, rhs);
5625 return false;
5626 case kCondNE:
5627 __ CmpEqD(dst, lhs, rhs);
5628 return true;
5629 case kCondLT:
5630 if (gt_bias) {
5631 __ CmpLtD(dst, lhs, rhs);
5632 } else {
5633 __ CmpUltD(dst, lhs, rhs);
5634 }
5635 return false;
5636 case kCondLE:
5637 if (gt_bias) {
5638 __ CmpLeD(dst, lhs, rhs);
5639 } else {
5640 __ CmpUleD(dst, lhs, rhs);
5641 }
5642 return false;
5643 case kCondGT:
5644 if (gt_bias) {
5645 __ CmpUltD(dst, rhs, lhs);
5646 } else {
5647 __ CmpLtD(dst, rhs, lhs);
5648 }
5649 return false;
5650 case kCondGE:
5651 if (gt_bias) {
5652 __ CmpUleD(dst, rhs, lhs);
5653 } else {
5654 __ CmpLeD(dst, rhs, lhs);
5655 }
5656 return false;
5657 default:
5658 LOG(FATAL) << "Unexpected non-floating-point condition";
5659 UNREACHABLE();
5660 }
5661 }
5662}
5663
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005664void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5665 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005666 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005667 LocationSummary* locations,
5668 MipsLabel* label) {
5669 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5670 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5671 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005672 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005673 if (isR6) {
5674 switch (cond) {
5675 case kCondEQ:
5676 __ CmpEqS(FTMP, lhs, rhs);
5677 __ Bc1nez(FTMP, label);
5678 break;
5679 case kCondNE:
5680 __ CmpEqS(FTMP, lhs, rhs);
5681 __ Bc1eqz(FTMP, label);
5682 break;
5683 case kCondLT:
5684 if (gt_bias) {
5685 __ CmpLtS(FTMP, lhs, rhs);
5686 } else {
5687 __ CmpUltS(FTMP, lhs, rhs);
5688 }
5689 __ Bc1nez(FTMP, label);
5690 break;
5691 case kCondLE:
5692 if (gt_bias) {
5693 __ CmpLeS(FTMP, lhs, rhs);
5694 } else {
5695 __ CmpUleS(FTMP, lhs, rhs);
5696 }
5697 __ Bc1nez(FTMP, label);
5698 break;
5699 case kCondGT:
5700 if (gt_bias) {
5701 __ CmpUltS(FTMP, rhs, lhs);
5702 } else {
5703 __ CmpLtS(FTMP, rhs, lhs);
5704 }
5705 __ Bc1nez(FTMP, label);
5706 break;
5707 case kCondGE:
5708 if (gt_bias) {
5709 __ CmpUleS(FTMP, rhs, lhs);
5710 } else {
5711 __ CmpLeS(FTMP, rhs, lhs);
5712 }
5713 __ Bc1nez(FTMP, label);
5714 break;
5715 default:
5716 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005717 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005718 }
5719 } else {
5720 switch (cond) {
5721 case kCondEQ:
5722 __ CeqS(0, lhs, rhs);
5723 __ Bc1t(0, label);
5724 break;
5725 case kCondNE:
5726 __ CeqS(0, lhs, rhs);
5727 __ Bc1f(0, label);
5728 break;
5729 case kCondLT:
5730 if (gt_bias) {
5731 __ ColtS(0, lhs, rhs);
5732 } else {
5733 __ CultS(0, lhs, rhs);
5734 }
5735 __ Bc1t(0, label);
5736 break;
5737 case kCondLE:
5738 if (gt_bias) {
5739 __ ColeS(0, lhs, rhs);
5740 } else {
5741 __ CuleS(0, lhs, rhs);
5742 }
5743 __ Bc1t(0, label);
5744 break;
5745 case kCondGT:
5746 if (gt_bias) {
5747 __ CultS(0, rhs, lhs);
5748 } else {
5749 __ ColtS(0, rhs, lhs);
5750 }
5751 __ Bc1t(0, label);
5752 break;
5753 case kCondGE:
5754 if (gt_bias) {
5755 __ CuleS(0, rhs, lhs);
5756 } else {
5757 __ ColeS(0, rhs, lhs);
5758 }
5759 __ Bc1t(0, label);
5760 break;
5761 default:
5762 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005763 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005764 }
5765 }
5766 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005767 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005768 if (isR6) {
5769 switch (cond) {
5770 case kCondEQ:
5771 __ CmpEqD(FTMP, lhs, rhs);
5772 __ Bc1nez(FTMP, label);
5773 break;
5774 case kCondNE:
5775 __ CmpEqD(FTMP, lhs, rhs);
5776 __ Bc1eqz(FTMP, label);
5777 break;
5778 case kCondLT:
5779 if (gt_bias) {
5780 __ CmpLtD(FTMP, lhs, rhs);
5781 } else {
5782 __ CmpUltD(FTMP, lhs, rhs);
5783 }
5784 __ Bc1nez(FTMP, label);
5785 break;
5786 case kCondLE:
5787 if (gt_bias) {
5788 __ CmpLeD(FTMP, lhs, rhs);
5789 } else {
5790 __ CmpUleD(FTMP, lhs, rhs);
5791 }
5792 __ Bc1nez(FTMP, label);
5793 break;
5794 case kCondGT:
5795 if (gt_bias) {
5796 __ CmpUltD(FTMP, rhs, lhs);
5797 } else {
5798 __ CmpLtD(FTMP, rhs, lhs);
5799 }
5800 __ Bc1nez(FTMP, label);
5801 break;
5802 case kCondGE:
5803 if (gt_bias) {
5804 __ CmpUleD(FTMP, rhs, lhs);
5805 } else {
5806 __ CmpLeD(FTMP, rhs, lhs);
5807 }
5808 __ Bc1nez(FTMP, label);
5809 break;
5810 default:
5811 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005812 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005813 }
5814 } else {
5815 switch (cond) {
5816 case kCondEQ:
5817 __ CeqD(0, lhs, rhs);
5818 __ Bc1t(0, label);
5819 break;
5820 case kCondNE:
5821 __ CeqD(0, lhs, rhs);
5822 __ Bc1f(0, label);
5823 break;
5824 case kCondLT:
5825 if (gt_bias) {
5826 __ ColtD(0, lhs, rhs);
5827 } else {
5828 __ CultD(0, lhs, rhs);
5829 }
5830 __ Bc1t(0, label);
5831 break;
5832 case kCondLE:
5833 if (gt_bias) {
5834 __ ColeD(0, lhs, rhs);
5835 } else {
5836 __ CuleD(0, lhs, rhs);
5837 }
5838 __ Bc1t(0, label);
5839 break;
5840 case kCondGT:
5841 if (gt_bias) {
5842 __ CultD(0, rhs, lhs);
5843 } else {
5844 __ ColtD(0, rhs, lhs);
5845 }
5846 __ Bc1t(0, label);
5847 break;
5848 case kCondGE:
5849 if (gt_bias) {
5850 __ CuleD(0, rhs, lhs);
5851 } else {
5852 __ ColeD(0, rhs, lhs);
5853 }
5854 __ Bc1t(0, label);
5855 break;
5856 default:
5857 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005858 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005859 }
5860 }
5861 }
5862}
5863
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005864void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005865 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005866 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005867 MipsLabel* false_target) {
5868 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005869
David Brazdil0debae72015-11-12 18:37:00 +00005870 if (true_target == nullptr && false_target == nullptr) {
5871 // Nothing to do. The code always falls through.
5872 return;
5873 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005874 // Constant condition, statically compared against "true" (integer value 1).
5875 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005876 if (true_target != nullptr) {
5877 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005878 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005879 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005880 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005881 if (false_target != nullptr) {
5882 __ B(false_target);
5883 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005884 }
David Brazdil0debae72015-11-12 18:37:00 +00005885 return;
5886 }
5887
5888 // The following code generates these patterns:
5889 // (1) true_target == nullptr && false_target != nullptr
5890 // - opposite condition true => branch to false_target
5891 // (2) true_target != nullptr && false_target == nullptr
5892 // - condition true => branch to true_target
5893 // (3) true_target != nullptr && false_target != nullptr
5894 // - condition true => branch to true_target
5895 // - branch to false_target
5896 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005897 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005898 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005899 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005900 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005901 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5902 } else {
5903 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5904 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005905 } else {
5906 // The condition instruction has not been materialized, use its inputs as
5907 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005908 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005909 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005910 LocationSummary* locations = cond->GetLocations();
5911 IfCondition if_cond = condition->GetCondition();
5912 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005913
David Brazdil0debae72015-11-12 18:37:00 +00005914 if (true_target == nullptr) {
5915 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005916 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005917 }
5918
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005919 switch (type) {
5920 default:
5921 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5922 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005923 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005924 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5925 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005926 case DataType::Type::kFloat32:
5927 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005928 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5929 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005930 }
5931 }
David Brazdil0debae72015-11-12 18:37:00 +00005932
5933 // If neither branch falls through (case 3), the conditional branch to `true_target`
5934 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5935 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005936 __ B(false_target);
5937 }
5938}
5939
5940void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005941 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005942 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005943 locations->SetInAt(0, Location::RequiresRegister());
5944 }
5945}
5946
5947void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005948 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5949 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5950 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5951 nullptr : codegen_->GetLabelOf(true_successor);
5952 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5953 nullptr : codegen_->GetLabelOf(false_successor);
Andreas Gampe3db70682018-12-26 15:12:03 -08005954 GenerateTestAndBranch(if_instr, /* condition_input_index= */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005955}
5956
5957void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005958 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005959 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005960 InvokeRuntimeCallingConvention calling_convention;
5961 RegisterSet caller_saves = RegisterSet::Empty();
5962 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5963 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005964 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005965 locations->SetInAt(0, Location::RequiresRegister());
5966 }
5967}
5968
5969void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005970 SlowPathCodeMIPS* slow_path =
5971 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005972 GenerateTestAndBranch(deoptimize,
Andreas Gampe3db70682018-12-26 15:12:03 -08005973 /* condition_input_index= */ 0,
David Brazdil0debae72015-11-12 18:37:00 +00005974 slow_path->GetEntryLabel(),
Andreas Gampe3db70682018-12-26 15:12:03 -08005975 /* false_target= */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005976}
5977
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005978// This function returns true if a conditional move can be generated for HSelect.
5979// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5980// branches and regular moves.
5981//
5982// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5983//
5984// While determining feasibility of a conditional move and setting inputs/outputs
5985// are two distinct tasks, this function does both because they share quite a bit
5986// of common logic.
5987static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5988 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
Andreas Gampe3db70682018-12-26 15:12:03 -08005989 HInstruction* cond = select->InputAt(/* i= */ 2);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005990 HCondition* condition = cond->AsCondition();
5991
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005992 DataType::Type cond_type =
5993 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5994 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005995
5996 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5997 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5998 bool is_true_value_zero_constant =
5999 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
6000 bool is_false_value_zero_constant =
6001 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
6002
6003 bool can_move_conditionally = false;
6004 bool use_const_for_false_in = false;
6005 bool use_const_for_true_in = false;
6006
6007 if (!cond->IsConstant()) {
6008 switch (cond_type) {
6009 default:
6010 switch (dst_type) {
6011 default:
6012 // Moving int on int condition.
6013 if (is_r6) {
6014 if (is_true_value_zero_constant) {
6015 // seleqz out_reg, false_reg, cond_reg
6016 can_move_conditionally = true;
6017 use_const_for_true_in = true;
6018 } else if (is_false_value_zero_constant) {
6019 // selnez out_reg, true_reg, cond_reg
6020 can_move_conditionally = true;
6021 use_const_for_false_in = true;
6022 } else if (materialized) {
6023 // Not materializing unmaterialized int conditions
6024 // to keep the instruction count low.
6025 // selnez AT, true_reg, cond_reg
6026 // seleqz TMP, false_reg, cond_reg
6027 // or out_reg, AT, TMP
6028 can_move_conditionally = true;
6029 }
6030 } else {
6031 // movn out_reg, true_reg/ZERO, cond_reg
6032 can_move_conditionally = true;
6033 use_const_for_true_in = is_true_value_zero_constant;
6034 }
6035 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006036 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006037 // Moving long on int condition.
6038 if (is_r6) {
6039 if (is_true_value_zero_constant) {
6040 // seleqz out_reg_lo, false_reg_lo, cond_reg
6041 // seleqz out_reg_hi, false_reg_hi, cond_reg
6042 can_move_conditionally = true;
6043 use_const_for_true_in = true;
6044 } else if (is_false_value_zero_constant) {
6045 // selnez out_reg_lo, true_reg_lo, cond_reg
6046 // selnez out_reg_hi, true_reg_hi, cond_reg
6047 can_move_conditionally = true;
6048 use_const_for_false_in = true;
6049 }
6050 // Other long conditional moves would generate 6+ instructions,
6051 // which is too many.
6052 } else {
6053 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
6054 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
6055 can_move_conditionally = true;
6056 use_const_for_true_in = is_true_value_zero_constant;
6057 }
6058 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006059 case DataType::Type::kFloat32:
6060 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006061 // Moving float/double on int condition.
6062 if (is_r6) {
6063 if (materialized) {
6064 // Not materializing unmaterialized int conditions
6065 // to keep the instruction count low.
6066 can_move_conditionally = true;
6067 if (is_true_value_zero_constant) {
6068 // sltu TMP, ZERO, cond_reg
6069 // mtc1 TMP, temp_cond_reg
6070 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6071 use_const_for_true_in = true;
6072 } else if (is_false_value_zero_constant) {
6073 // sltu TMP, ZERO, cond_reg
6074 // mtc1 TMP, temp_cond_reg
6075 // selnez.fmt out_reg, true_reg, temp_cond_reg
6076 use_const_for_false_in = true;
6077 } else {
6078 // sltu TMP, ZERO, cond_reg
6079 // mtc1 TMP, temp_cond_reg
6080 // sel.fmt temp_cond_reg, false_reg, true_reg
6081 // mov.fmt out_reg, temp_cond_reg
6082 }
6083 }
6084 } else {
6085 // movn.fmt out_reg, true_reg, cond_reg
6086 can_move_conditionally = true;
6087 }
6088 break;
6089 }
6090 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006091 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006092 // We don't materialize long comparison now
6093 // and use conditional branches instead.
6094 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006095 case DataType::Type::kFloat32:
6096 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006097 switch (dst_type) {
6098 default:
6099 // Moving int on float/double condition.
6100 if (is_r6) {
6101 if (is_true_value_zero_constant) {
6102 // mfc1 TMP, temp_cond_reg
6103 // seleqz out_reg, false_reg, TMP
6104 can_move_conditionally = true;
6105 use_const_for_true_in = true;
6106 } else if (is_false_value_zero_constant) {
6107 // mfc1 TMP, temp_cond_reg
6108 // selnez out_reg, true_reg, TMP
6109 can_move_conditionally = true;
6110 use_const_for_false_in = true;
6111 } else {
6112 // mfc1 TMP, temp_cond_reg
6113 // selnez AT, true_reg, TMP
6114 // seleqz TMP, false_reg, TMP
6115 // or out_reg, AT, TMP
6116 can_move_conditionally = true;
6117 }
6118 } else {
6119 // movt out_reg, true_reg/ZERO, cc
6120 can_move_conditionally = true;
6121 use_const_for_true_in = is_true_value_zero_constant;
6122 }
6123 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006124 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006125 // Moving long on float/double condition.
6126 if (is_r6) {
6127 if (is_true_value_zero_constant) {
6128 // mfc1 TMP, temp_cond_reg
6129 // seleqz out_reg_lo, false_reg_lo, TMP
6130 // seleqz out_reg_hi, false_reg_hi, TMP
6131 can_move_conditionally = true;
6132 use_const_for_true_in = true;
6133 } else if (is_false_value_zero_constant) {
6134 // mfc1 TMP, temp_cond_reg
6135 // selnez out_reg_lo, true_reg_lo, TMP
6136 // selnez out_reg_hi, true_reg_hi, TMP
6137 can_move_conditionally = true;
6138 use_const_for_false_in = true;
6139 }
6140 // Other long conditional moves would generate 6+ instructions,
6141 // which is too many.
6142 } else {
6143 // movt out_reg_lo, true_reg_lo/ZERO, cc
6144 // movt out_reg_hi, true_reg_hi/ZERO, cc
6145 can_move_conditionally = true;
6146 use_const_for_true_in = is_true_value_zero_constant;
6147 }
6148 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006149 case DataType::Type::kFloat32:
6150 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006151 // Moving float/double on float/double condition.
6152 if (is_r6) {
6153 can_move_conditionally = true;
6154 if (is_true_value_zero_constant) {
6155 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6156 use_const_for_true_in = true;
6157 } else if (is_false_value_zero_constant) {
6158 // selnez.fmt out_reg, true_reg, temp_cond_reg
6159 use_const_for_false_in = true;
6160 } else {
6161 // sel.fmt temp_cond_reg, false_reg, true_reg
6162 // mov.fmt out_reg, temp_cond_reg
6163 }
6164 } else {
6165 // movt.fmt out_reg, true_reg, cc
6166 can_move_conditionally = true;
6167 }
6168 break;
6169 }
6170 break;
6171 }
6172 }
6173
6174 if (can_move_conditionally) {
6175 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6176 } else {
6177 DCHECK(!use_const_for_false_in);
6178 DCHECK(!use_const_for_true_in);
6179 }
6180
6181 if (locations_to_set != nullptr) {
6182 if (use_const_for_false_in) {
6183 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6184 } else {
6185 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006186 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006187 ? Location::RequiresFpuRegister()
6188 : Location::RequiresRegister());
6189 }
6190 if (use_const_for_true_in) {
6191 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6192 } else {
6193 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006194 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006195 ? Location::RequiresFpuRegister()
6196 : Location::RequiresRegister());
6197 }
6198 if (materialized) {
6199 locations_to_set->SetInAt(2, Location::RequiresRegister());
6200 }
6201 // On R6 we don't require the output to be the same as the
6202 // first input for conditional moves unlike on R2.
6203 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6204 if (is_out_same_as_first_in) {
6205 locations_to_set->SetOut(Location::SameAsFirstInput());
6206 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006207 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006208 ? Location::RequiresFpuRegister()
6209 : Location::RequiresRegister());
6210 }
6211 }
6212
6213 return can_move_conditionally;
6214}
6215
6216void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6217 LocationSummary* locations = select->GetLocations();
6218 Location dst = locations->Out();
6219 Location src = locations->InAt(1);
6220 Register src_reg = ZERO;
6221 Register src_reg_high = ZERO;
Andreas Gampe3db70682018-12-26 15:12:03 -08006222 HInstruction* cond = select->InputAt(/* i= */ 2);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006223 Register cond_reg = TMP;
6224 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006225 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006226 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006227 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006228
6229 if (IsBooleanValueOrMaterializedCondition(cond)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08006230 cond_reg = locations->InAt(/* at= */ 2).AsRegister<Register>();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006231 } else {
6232 HCondition* condition = cond->AsCondition();
6233 LocationSummary* cond_locations = cond->GetLocations();
6234 IfCondition if_cond = condition->GetCondition();
6235 cond_type = condition->InputAt(0)->GetType();
6236 switch (cond_type) {
6237 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006238 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006239 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6240 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006241 case DataType::Type::kFloat32:
6242 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006243 cond_inverted = MaterializeFpCompareR2(if_cond,
6244 condition->IsGtBias(),
6245 cond_type,
6246 cond_locations,
6247 cond_cc);
6248 break;
6249 }
6250 }
6251
6252 DCHECK(dst.Equals(locations->InAt(0)));
6253 if (src.IsRegister()) {
6254 src_reg = src.AsRegister<Register>();
6255 } else if (src.IsRegisterPair()) {
6256 src_reg = src.AsRegisterPairLow<Register>();
6257 src_reg_high = src.AsRegisterPairHigh<Register>();
6258 } else if (src.IsConstant()) {
6259 DCHECK(src.GetConstant()->IsZeroBitPattern());
6260 }
6261
6262 switch (cond_type) {
6263 default:
6264 switch (dst_type) {
6265 default:
6266 if (cond_inverted) {
6267 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6268 } else {
6269 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6270 }
6271 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006272 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006273 if (cond_inverted) {
6274 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6275 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6276 } else {
6277 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6278 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6279 }
6280 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006281 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006282 if (cond_inverted) {
6283 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6284 } else {
6285 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6286 }
6287 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006288 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006289 if (cond_inverted) {
6290 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6291 } else {
6292 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6293 }
6294 break;
6295 }
6296 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006297 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006298 LOG(FATAL) << "Unreachable";
6299 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006300 case DataType::Type::kFloat32:
6301 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006302 switch (dst_type) {
6303 default:
6304 if (cond_inverted) {
6305 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6306 } else {
6307 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6308 }
6309 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006310 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006311 if (cond_inverted) {
6312 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6313 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6314 } else {
6315 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6316 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6317 }
6318 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006319 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006320 if (cond_inverted) {
6321 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6322 } else {
6323 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6324 }
6325 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006326 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006327 if (cond_inverted) {
6328 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6329 } else {
6330 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6331 }
6332 break;
6333 }
6334 break;
6335 }
6336}
6337
6338void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6339 LocationSummary* locations = select->GetLocations();
6340 Location dst = locations->Out();
6341 Location false_src = locations->InAt(0);
6342 Location true_src = locations->InAt(1);
Andreas Gampe3db70682018-12-26 15:12:03 -08006343 HInstruction* cond = select->InputAt(/* i= */ 2);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006344 Register cond_reg = TMP;
6345 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006346 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006347 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006348 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006349
6350 if (IsBooleanValueOrMaterializedCondition(cond)) {
Andreas Gampe3db70682018-12-26 15:12:03 -08006351 cond_reg = locations->InAt(/* at= */ 2).AsRegister<Register>();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006352 } else {
6353 HCondition* condition = cond->AsCondition();
6354 LocationSummary* cond_locations = cond->GetLocations();
6355 IfCondition if_cond = condition->GetCondition();
6356 cond_type = condition->InputAt(0)->GetType();
6357 switch (cond_type) {
6358 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006359 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006360 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6361 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006362 case DataType::Type::kFloat32:
6363 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006364 cond_inverted = MaterializeFpCompareR6(if_cond,
6365 condition->IsGtBias(),
6366 cond_type,
6367 cond_locations,
6368 fcond_reg);
6369 break;
6370 }
6371 }
6372
6373 if (true_src.IsConstant()) {
6374 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6375 }
6376 if (false_src.IsConstant()) {
6377 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6378 }
6379
6380 switch (dst_type) {
6381 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006382 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006383 __ Mfc1(cond_reg, fcond_reg);
6384 }
6385 if (true_src.IsConstant()) {
6386 if (cond_inverted) {
6387 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6388 } else {
6389 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6390 }
6391 } else if (false_src.IsConstant()) {
6392 if (cond_inverted) {
6393 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6394 } else {
6395 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6396 }
6397 } else {
6398 DCHECK_NE(cond_reg, AT);
6399 if (cond_inverted) {
6400 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6401 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6402 } else {
6403 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6404 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6405 }
6406 __ Or(dst.AsRegister<Register>(), AT, TMP);
6407 }
6408 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006409 case DataType::Type::kInt64: {
6410 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006411 __ Mfc1(cond_reg, fcond_reg);
6412 }
6413 Register dst_lo = dst.AsRegisterPairLow<Register>();
6414 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6415 if (true_src.IsConstant()) {
6416 Register src_lo = false_src.AsRegisterPairLow<Register>();
6417 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6418 if (cond_inverted) {
6419 __ Selnez(dst_lo, src_lo, cond_reg);
6420 __ Selnez(dst_hi, src_hi, cond_reg);
6421 } else {
6422 __ Seleqz(dst_lo, src_lo, cond_reg);
6423 __ Seleqz(dst_hi, src_hi, cond_reg);
6424 }
6425 } else {
6426 DCHECK(false_src.IsConstant());
6427 Register src_lo = true_src.AsRegisterPairLow<Register>();
6428 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6429 if (cond_inverted) {
6430 __ Seleqz(dst_lo, src_lo, cond_reg);
6431 __ Seleqz(dst_hi, src_hi, cond_reg);
6432 } else {
6433 __ Selnez(dst_lo, src_lo, cond_reg);
6434 __ Selnez(dst_hi, src_hi, cond_reg);
6435 }
6436 }
6437 break;
6438 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006439 case DataType::Type::kFloat32: {
6440 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006441 // sel*.fmt tests bit 0 of the condition register, account for that.
6442 __ Sltu(TMP, ZERO, cond_reg);
6443 __ Mtc1(TMP, fcond_reg);
6444 }
6445 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6446 if (true_src.IsConstant()) {
6447 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6448 if (cond_inverted) {
6449 __ SelnezS(dst_reg, src_reg, fcond_reg);
6450 } else {
6451 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6452 }
6453 } else if (false_src.IsConstant()) {
6454 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6455 if (cond_inverted) {
6456 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6457 } else {
6458 __ SelnezS(dst_reg, src_reg, fcond_reg);
6459 }
6460 } else {
6461 if (cond_inverted) {
6462 __ SelS(fcond_reg,
6463 true_src.AsFpuRegister<FRegister>(),
6464 false_src.AsFpuRegister<FRegister>());
6465 } else {
6466 __ SelS(fcond_reg,
6467 false_src.AsFpuRegister<FRegister>(),
6468 true_src.AsFpuRegister<FRegister>());
6469 }
6470 __ MovS(dst_reg, fcond_reg);
6471 }
6472 break;
6473 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006474 case DataType::Type::kFloat64: {
6475 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006476 // sel*.fmt tests bit 0 of the condition register, account for that.
6477 __ Sltu(TMP, ZERO, cond_reg);
6478 __ Mtc1(TMP, fcond_reg);
6479 }
6480 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6481 if (true_src.IsConstant()) {
6482 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6483 if (cond_inverted) {
6484 __ SelnezD(dst_reg, src_reg, fcond_reg);
6485 } else {
6486 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6487 }
6488 } else if (false_src.IsConstant()) {
6489 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6490 if (cond_inverted) {
6491 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6492 } else {
6493 __ SelnezD(dst_reg, src_reg, fcond_reg);
6494 }
6495 } else {
6496 if (cond_inverted) {
6497 __ SelD(fcond_reg,
6498 true_src.AsFpuRegister<FRegister>(),
6499 false_src.AsFpuRegister<FRegister>());
6500 } else {
6501 __ SelD(fcond_reg,
6502 false_src.AsFpuRegister<FRegister>(),
6503 true_src.AsFpuRegister<FRegister>());
6504 }
6505 __ MovD(dst_reg, fcond_reg);
6506 }
6507 break;
6508 }
6509 }
6510}
6511
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006512void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006513 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006514 LocationSummary(flag, LocationSummary::kNoCall);
6515 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006516}
6517
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006518void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6519 __ LoadFromOffset(kLoadWord,
6520 flag->GetLocations()->Out().AsRegister<Register>(),
6521 SP,
6522 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006523}
6524
David Brazdil74eb1b22015-12-14 11:44:01 +00006525void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006526 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006527 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006528}
6529
6530void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006531 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Andreas Gampe3db70682018-12-26 15:12:03 -08006532 if (CanMoveConditionally(select, is_r6, /* locations_to_set= */ nullptr)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006533 if (is_r6) {
6534 GenConditionalMoveR6(select);
6535 } else {
6536 GenConditionalMoveR2(select);
6537 }
6538 } else {
6539 LocationSummary* locations = select->GetLocations();
6540 MipsLabel false_target;
6541 GenerateTestAndBranch(select,
Andreas Gampe3db70682018-12-26 15:12:03 -08006542 /* condition_input_index= */ 2,
6543 /* true_target= */ nullptr,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006544 &false_target);
6545 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6546 __ Bind(&false_target);
6547 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006548}
6549
David Srbecky0cf44932015-12-09 14:09:59 +00006550void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006551 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006552}
6553
David Srbeckyd28f4a02016-03-14 17:14:24 +00006554void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6555 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006556}
6557
6558void CodeGeneratorMIPS::GenerateNop() {
6559 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006560}
6561
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006562void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006563 DataType::Type field_type = field_info.GetFieldType();
6564 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006565 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006566 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006567 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006568 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006569 instruction,
6570 generate_volatile
6571 ? LocationSummary::kCallOnMainOnly
6572 : (object_field_get_with_read_barrier
6573 ? LocationSummary::kCallOnSlowPath
6574 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006575
Alexey Frunzec61c0762017-04-10 13:54:23 -07006576 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6577 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6578 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006579 locations->SetInAt(0, Location::RequiresRegister());
6580 if (generate_volatile) {
6581 InvokeRuntimeCallingConvention calling_convention;
6582 // need A0 to hold base + offset
6583 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006584 if (field_type == DataType::Type::kInt64) {
6585 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006586 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006587 // Use Location::Any() to prevent situations when running out of available fp registers.
6588 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006589 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006590 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006591 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6592 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6593 }
6594 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006595 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006596 locations->SetOut(Location::RequiresFpuRegister());
6597 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006598 // The output overlaps in the case of an object field get with
6599 // read barriers enabled: we do not want the move to overwrite the
6600 // object's location, as we need it to emit the read barrier.
6601 locations->SetOut(Location::RequiresRegister(),
6602 object_field_get_with_read_barrier
6603 ? Location::kOutputOverlap
6604 : Location::kNoOutputOverlap);
6605 }
6606 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6607 // We need a temporary register for the read barrier marking slow
6608 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006609 if (!kBakerReadBarrierThunksEnableForFields) {
6610 locations->AddTemp(Location::RequiresRegister());
6611 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006612 }
6613 }
6614}
6615
6616void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6617 const FieldInfo& field_info,
6618 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006619 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6620 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006621 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006622 Location obj_loc = locations->InAt(0);
6623 Register obj = obj_loc.AsRegister<Register>();
6624 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006625 LoadOperandType load_type = kLoadUnsignedByte;
6626 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006627 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006628 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006629
6630 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006631 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006632 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006633 load_type = kLoadUnsignedByte;
6634 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006635 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006636 load_type = kLoadSignedByte;
6637 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006638 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006639 load_type = kLoadUnsignedHalfword;
6640 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006641 case DataType::Type::kInt16:
6642 load_type = kLoadSignedHalfword;
6643 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006644 case DataType::Type::kInt32:
6645 case DataType::Type::kFloat32:
6646 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006647 load_type = kLoadWord;
6648 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006649 case DataType::Type::kInt64:
6650 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006651 load_type = kLoadDoubleword;
6652 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006653 case DataType::Type::kUint32:
6654 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006655 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006656 LOG(FATAL) << "Unreachable type " << type;
6657 UNREACHABLE();
6658 }
6659
6660 if (is_volatile && load_type == kLoadDoubleword) {
6661 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006662 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006663 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006664 __ LoadFromOffset(kLoadWord,
6665 ZERO,
6666 locations->GetTemp(0).AsRegister<Register>(),
6667 0,
6668 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006669 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006670 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006671 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006672 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006673 if (dst_loc.IsFpuRegister()) {
6674 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006675 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006676 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006677 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006678 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006679 __ StoreToOffset(kStoreWord,
6680 locations->GetTemp(1).AsRegister<Register>(),
6681 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006682 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006683 __ StoreToOffset(kStoreWord,
6684 locations->GetTemp(2).AsRegister<Register>(),
6685 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006686 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006687 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006688 }
6689 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006690 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006691 // /* HeapReference<Object> */ dst = *(obj + offset)
6692 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006693 Location temp_loc =
6694 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006695 // Note that a potential implicit null check is handled in this
6696 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6697 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6698 dst_loc,
6699 obj,
6700 offset,
6701 temp_loc,
Andreas Gampe3db70682018-12-26 15:12:03 -08006702 /* needs_null_check= */ true);
Alexey Frunze15958152017-02-09 19:08:30 -08006703 if (is_volatile) {
6704 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6705 }
6706 } else {
6707 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6708 if (is_volatile) {
6709 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6710 }
6711 // If read barriers are enabled, emit read barriers other than
6712 // Baker's using a slow path (and also unpoison the loaded
6713 // reference, if heap poisoning is enabled).
6714 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6715 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006716 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006717 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006718 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006719 DCHECK(dst_loc.IsRegisterPair());
6720 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006721 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006722 DCHECK(dst_loc.IsRegister());
6723 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006725 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006726 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006727 DCHECK(dst_loc.IsFpuRegister());
6728 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006729 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006730 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006731 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006732 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006733 }
6734 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006735 }
6736
Alexey Frunze15958152017-02-09 19:08:30 -08006737 // Memory barriers, in the case of references, are handled in the
6738 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006739 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006740 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6741 }
6742}
6743
6744void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006745 DataType::Type field_type = field_info.GetFieldType();
6746 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006747 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006748 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006749 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006750
6751 locations->SetInAt(0, Location::RequiresRegister());
6752 if (generate_volatile) {
6753 InvokeRuntimeCallingConvention calling_convention;
6754 // need A0 to hold base + offset
6755 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006756 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006757 locations->SetInAt(1, Location::RegisterPairLocation(
6758 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6759 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006760 // Use Location::Any() to prevent situations when running out of available fp registers.
6761 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006762 // Pass FP parameters in core registers.
6763 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6764 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6765 }
6766 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006767 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006768 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006769 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006770 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006771 }
6772 }
6773}
6774
6775void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6776 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006777 uint32_t dex_pc,
6778 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006779 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006780 LocationSummary* locations = instruction->GetLocations();
6781 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006782 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006783 StoreOperandType store_type = kStoreByte;
6784 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006785 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006786 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006787 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006788
6789 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006790 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006791 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006792 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006793 store_type = kStoreByte;
6794 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006795 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006796 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006797 store_type = kStoreHalfword;
6798 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006799 case DataType::Type::kInt32:
6800 case DataType::Type::kFloat32:
6801 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006802 store_type = kStoreWord;
6803 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006804 case DataType::Type::kInt64:
6805 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006806 store_type = kStoreDoubleword;
6807 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006808 case DataType::Type::kUint32:
6809 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006810 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006811 LOG(FATAL) << "Unreachable type " << type;
6812 UNREACHABLE();
6813 }
6814
6815 if (is_volatile) {
6816 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6817 }
6818
6819 if (is_volatile && store_type == kStoreDoubleword) {
6820 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006821 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006822 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006823 __ LoadFromOffset(kLoadWord,
6824 ZERO,
6825 locations->GetTemp(0).AsRegister<Register>(),
6826 0,
6827 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006828 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006829 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006830 if (value_location.IsFpuRegister()) {
6831 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6832 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006833 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006834 value_location.AsFpuRegister<FRegister>());
6835 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006836 __ LoadFromOffset(kLoadWord,
6837 locations->GetTemp(1).AsRegister<Register>(),
6838 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006839 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006840 __ LoadFromOffset(kLoadWord,
6841 locations->GetTemp(2).AsRegister<Register>(),
6842 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006843 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006844 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006845 DCHECK(value_location.IsConstant());
6846 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6847 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006848 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6849 locations->GetTemp(1).AsRegister<Register>(),
6850 value);
6851 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006852 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006853 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006854 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6855 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006856 if (value_location.IsConstant()) {
6857 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6858 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006859 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006860 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006861 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006862 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006863 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006864 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006865 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006866 if (kPoisonHeapReferences && needs_write_barrier) {
6867 // Note that in the case where `value` is a null reference,
6868 // we do not enter this block, as a null reference does not
6869 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006870 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006871 __ PoisonHeapReference(TMP, src);
6872 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6873 } else {
6874 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6875 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006876 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006877 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006878 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006879 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006880 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006881 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006882 }
6883 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006884 }
6885
Alexey Frunzec061de12017-02-14 13:27:23 -08006886 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006887 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006888 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006889 }
6890
6891 if (is_volatile) {
6892 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6893 }
6894}
6895
6896void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6897 HandleFieldGet(instruction, instruction->GetFieldInfo());
6898}
6899
6900void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6901 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6902}
6903
6904void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6905 HandleFieldSet(instruction, instruction->GetFieldInfo());
6906}
6907
6908void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006909 HandleFieldSet(instruction,
6910 instruction->GetFieldInfo(),
6911 instruction->GetDexPc(),
6912 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006913}
6914
Alexey Frunze15958152017-02-09 19:08:30 -08006915void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6916 HInstruction* instruction,
6917 Location out,
6918 uint32_t offset,
6919 Location maybe_temp,
6920 ReadBarrierOption read_barrier_option) {
6921 Register out_reg = out.AsRegister<Register>();
6922 if (read_barrier_option == kWithReadBarrier) {
6923 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006924 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6925 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6926 }
Alexey Frunze15958152017-02-09 19:08:30 -08006927 if (kUseBakerReadBarrier) {
6928 // Load with fast path based Baker's read barrier.
6929 // /* HeapReference<Object> */ out = *(out + offset)
6930 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6931 out,
6932 out_reg,
6933 offset,
6934 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006935 /* needs_null_check= */ false);
Alexey Frunze15958152017-02-09 19:08:30 -08006936 } else {
6937 // Load with slow path based read barrier.
6938 // Save the value of `out` into `maybe_temp` before overwriting it
6939 // in the following move operation, as we will need it for the
6940 // read barrier below.
6941 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6942 // /* HeapReference<Object> */ out = *(out + offset)
6943 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6944 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6945 }
6946 } else {
6947 // Plain load with no read barrier.
6948 // /* HeapReference<Object> */ out = *(out + offset)
6949 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6950 __ MaybeUnpoisonHeapReference(out_reg);
6951 }
6952}
6953
6954void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6955 HInstruction* instruction,
6956 Location out,
6957 Location obj,
6958 uint32_t offset,
6959 Location maybe_temp,
6960 ReadBarrierOption read_barrier_option) {
6961 Register out_reg = out.AsRegister<Register>();
6962 Register obj_reg = obj.AsRegister<Register>();
6963 if (read_barrier_option == kWithReadBarrier) {
6964 CHECK(kEmitCompilerReadBarrier);
6965 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006966 if (!kBakerReadBarrierThunksEnableForFields) {
6967 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6968 }
Alexey Frunze15958152017-02-09 19:08:30 -08006969 // Load with fast path based Baker's read barrier.
6970 // /* HeapReference<Object> */ out = *(obj + offset)
6971 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6972 out,
6973 obj_reg,
6974 offset,
6975 maybe_temp,
Andreas Gampe3db70682018-12-26 15:12:03 -08006976 /* needs_null_check= */ false);
Alexey Frunze15958152017-02-09 19:08:30 -08006977 } else {
6978 // Load with slow path based read barrier.
6979 // /* HeapReference<Object> */ out = *(obj + offset)
6980 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6981 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6982 }
6983 } else {
6984 // Plain load with no read barrier.
6985 // /* HeapReference<Object> */ out = *(obj + offset)
6986 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6987 __ MaybeUnpoisonHeapReference(out_reg);
6988 }
6989}
6990
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006991static inline int GetBakerMarkThunkNumber(Register reg) {
6992 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6993 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6994 return reg - V0;
6995 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6996 return 14 + (reg - S2);
6997 } else if (reg == FP) { // One more.
6998 return 20;
6999 }
7000 LOG(FATAL) << "Unexpected register " << reg;
7001 UNREACHABLE();
7002}
7003
7004static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
7005 int num = GetBakerMarkThunkNumber(reg) +
7006 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
7007 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
7008}
7009
7010static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
7011 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
7012 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
7013}
7014
Alexey Frunze15958152017-02-09 19:08:30 -08007015void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
7016 Location root,
7017 Register obj,
7018 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007019 ReadBarrierOption read_barrier_option,
7020 MipsLabel* label_low) {
7021 bool reordering;
7022 if (label_low != nullptr) {
7023 DCHECK_EQ(offset, 0x5678u);
7024 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007025 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007026 if (read_barrier_option == kWithReadBarrier) {
7027 DCHECK(kEmitCompilerReadBarrier);
7028 if (kUseBakerReadBarrier) {
7029 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7030 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007031 if (kBakerReadBarrierThunksEnableForGcRoots) {
7032 // Note that we do not actually check the value of `GetIsGcMarking()`
7033 // to decide whether to mark the loaded GC root or not. Instead, we
7034 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7035 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7036 // vice versa.
7037 //
7038 // We use thunks for the slow path. That thunk checks the reference
7039 // and jumps to the entrypoint if needed.
7040 //
7041 // temp = Thread::Current()->pReadBarrierMarkReg00
7042 // // AKA &art_quick_read_barrier_mark_introspection.
7043 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7044 // if (temp != nullptr) {
7045 // temp = &gc_root_thunk<root_reg>
7046 // root = temp(root)
7047 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007048
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007049 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7050 const int32_t entry_point_offset =
7051 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7052 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
7053 int16_t offset_low = Low16Bits(offset);
7054 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
7055 // extension in lw.
7056 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7057 Register base = short_offset ? obj : TMP;
7058 // Loading the entrypoint does not require a load acquire since it is only changed when
7059 // threads are suspended or running a checkpoint.
7060 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7061 reordering = __ SetReorder(false);
7062 if (!short_offset) {
7063 DCHECK(!label_low);
7064 __ AddUpper(base, obj, offset_high);
7065 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007066 MipsLabel skip_call;
Andreas Gampe3db70682018-12-26 15:12:03 -08007067 __ Beqz(T9, &skip_call, /* is_bare= */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007068 if (label_low != nullptr) {
7069 DCHECK(short_offset);
7070 __ Bind(label_low);
7071 }
7072 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7073 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
7074 // in delay slot.
7075 if (isR6) {
7076 __ Jialc(T9, thunk_disp);
7077 } else {
7078 __ Addiu(T9, T9, thunk_disp);
7079 __ Jalr(T9);
7080 __ Nop();
7081 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007082 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007083 __ SetReorder(reordering);
7084 } else {
7085 // Note that we do not actually check the value of `GetIsGcMarking()`
7086 // to decide whether to mark the loaded GC root or not. Instead, we
7087 // load into `temp` (T9) the read barrier mark entry point corresponding
7088 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
7089 // is false, and vice versa.
7090 //
7091 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7092 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7093 // if (temp != null) {
7094 // root = temp(root)
7095 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007096
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007097 if (label_low != nullptr) {
7098 reordering = __ SetReorder(false);
7099 __ Bind(label_low);
7100 }
7101 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7102 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7103 if (label_low != nullptr) {
7104 __ SetReorder(reordering);
7105 }
7106 static_assert(
7107 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7108 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7109 "have different sizes.");
7110 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7111 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7112 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08007113
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007114 // Slow path marking the GC root `root`.
7115 Location temp = Location::RegisterLocation(T9);
7116 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007117 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007118 instruction,
7119 root,
7120 /*entrypoint*/ temp);
7121 codegen_->AddSlowPath(slow_path);
7122
7123 const int32_t entry_point_offset =
7124 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
7125 // Loading the entrypoint does not require a load acquire since it is only changed when
7126 // threads are suspended or running a checkpoint.
7127 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7128 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
7129 __ Bind(slow_path->GetExitLabel());
7130 }
Alexey Frunze15958152017-02-09 19:08:30 -08007131 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007132 if (label_low != nullptr) {
7133 reordering = __ SetReorder(false);
7134 __ Bind(label_low);
7135 }
Alexey Frunze15958152017-02-09 19:08:30 -08007136 // GC root loaded through a slow path for read barriers other
7137 // than Baker's.
7138 // /* GcRoot<mirror::Object>* */ root = obj + offset
7139 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007140 if (label_low != nullptr) {
7141 __ SetReorder(reordering);
7142 }
Alexey Frunze15958152017-02-09 19:08:30 -08007143 // /* mirror::Object* */ root = root->Read()
7144 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7145 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007146 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007147 if (label_low != nullptr) {
7148 reordering = __ SetReorder(false);
7149 __ Bind(label_low);
7150 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007151 // Plain GC root load with no read barrier.
7152 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7153 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7154 // Note that GC roots are not affected by heap poisoning, thus we
7155 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007156 if (label_low != nullptr) {
7157 __ SetReorder(reordering);
7158 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007159 }
7160}
7161
Alexey Frunze15958152017-02-09 19:08:30 -08007162void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7163 Location ref,
7164 Register obj,
7165 uint32_t offset,
7166 Location temp,
7167 bool needs_null_check) {
7168 DCHECK(kEmitCompilerReadBarrier);
7169 DCHECK(kUseBakerReadBarrier);
7170
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007171 if (kBakerReadBarrierThunksEnableForFields) {
7172 // Note that we do not actually check the value of `GetIsGcMarking()`
7173 // to decide whether to mark the loaded reference or not. Instead, we
7174 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7175 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7176 // vice versa.
7177 //
7178 // We use thunks for the slow path. That thunk checks the reference
7179 // and jumps to the entrypoint if needed. If the holder is not gray,
7180 // it issues a load-load memory barrier and returns to the original
7181 // reference load.
7182 //
7183 // temp = Thread::Current()->pReadBarrierMarkReg00
7184 // // AKA &art_quick_read_barrier_mark_introspection.
7185 // if (temp != nullptr) {
7186 // temp = &field_array_thunk<holder_reg>
7187 // temp()
7188 // }
7189 // not_gray_return_address:
7190 // // If the offset is too large to fit into the lw instruction, we
7191 // // use an adjusted base register (TMP) here. This register
7192 // // receives bits 16 ... 31 of the offset before the thunk invocation
7193 // // and the thunk benefits from it.
7194 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7195 // gray_return_address:
7196
7197 DCHECK(temp.IsInvalid());
7198 bool isR6 = GetInstructionSetFeatures().IsR6();
7199 int16_t offset_low = Low16Bits(offset);
7200 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7201 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7202 bool reordering = __ SetReorder(false);
7203 const int32_t entry_point_offset =
7204 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7205 // There may have or may have not been a null check if the field offset is smaller than
7206 // the page size.
7207 // There must've been a null check in case it's actually a load from an array.
7208 // We will, however, perform an explicit null check in the thunk as it's easier to
7209 // do it than not.
7210 if (instruction->IsArrayGet()) {
7211 DCHECK(!needs_null_check);
7212 }
7213 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7214 // Loading the entrypoint does not require a load acquire since it is only changed when
7215 // threads are suspended or running a checkpoint.
7216 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7217 Register ref_reg = ref.AsRegister<Register>();
7218 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007219 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007220 if (short_offset) {
7221 if (isR6) {
Andreas Gampe3db70682018-12-26 15:12:03 -08007222 __ Beqzc(T9, &skip_call, /* is_bare= */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007223 __ Nop(); // In forbidden slot.
7224 __ Jialc(T9, thunk_disp);
7225 } else {
Andreas Gampe3db70682018-12-26 15:12:03 -08007226 __ Beqz(T9, &skip_call, /* is_bare= */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007227 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7228 __ Jalr(T9);
7229 __ Nop(); // In delay slot.
7230 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007231 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007232 } else {
7233 if (isR6) {
Andreas Gampe3db70682018-12-26 15:12:03 -08007234 __ Beqz(T9, &skip_call, /* is_bare= */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007235 __ Aui(base, obj, offset_high); // In delay slot.
7236 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007237 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007238 } else {
7239 __ Lui(base, offset_high);
Andreas Gampe3db70682018-12-26 15:12:03 -08007240 __ Beqz(T9, &skip_call, /* is_bare= */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007241 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7242 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007243 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007244 __ Addu(base, base, obj); // In delay slot.
7245 }
7246 }
7247 // /* HeapReference<Object> */ ref = *(obj + offset)
7248 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7249 if (needs_null_check) {
7250 MaybeRecordImplicitNullCheck(instruction);
7251 }
7252 __ MaybeUnpoisonHeapReference(ref_reg);
7253 __ SetReorder(reordering);
7254 return;
7255 }
7256
Alexey Frunze15958152017-02-09 19:08:30 -08007257 // /* HeapReference<Object> */ ref = *(obj + offset)
7258 Location no_index = Location::NoLocation();
7259 ScaleFactor no_scale_factor = TIMES_1;
7260 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7261 ref,
7262 obj,
7263 offset,
7264 no_index,
7265 no_scale_factor,
7266 temp,
7267 needs_null_check);
7268}
7269
7270void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7271 Location ref,
7272 Register obj,
7273 uint32_t data_offset,
7274 Location index,
7275 Location temp,
7276 bool needs_null_check) {
7277 DCHECK(kEmitCompilerReadBarrier);
7278 DCHECK(kUseBakerReadBarrier);
7279
7280 static_assert(
7281 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7282 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007283 ScaleFactor scale_factor = TIMES_4;
7284
7285 if (kBakerReadBarrierThunksEnableForArrays) {
7286 // Note that we do not actually check the value of `GetIsGcMarking()`
7287 // to decide whether to mark the loaded reference or not. Instead, we
7288 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7289 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7290 // vice versa.
7291 //
7292 // We use thunks for the slow path. That thunk checks the reference
7293 // and jumps to the entrypoint if needed. If the holder is not gray,
7294 // it issues a load-load memory barrier and returns to the original
7295 // reference load.
7296 //
7297 // temp = Thread::Current()->pReadBarrierMarkReg00
7298 // // AKA &art_quick_read_barrier_mark_introspection.
7299 // if (temp != nullptr) {
7300 // temp = &field_array_thunk<holder_reg>
7301 // temp()
7302 // }
7303 // not_gray_return_address:
7304 // // The element address is pre-calculated in the TMP register before the
7305 // // thunk invocation and the thunk benefits from it.
7306 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7307 // gray_return_address:
7308
7309 DCHECK(temp.IsInvalid());
7310 DCHECK(index.IsValid());
7311 bool reordering = __ SetReorder(false);
7312 const int32_t entry_point_offset =
7313 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7314 // We will not do the explicit null check in the thunk as some form of a null check
7315 // must've been done earlier.
7316 DCHECK(!needs_null_check);
Andreas Gampe3db70682018-12-26 15:12:03 -08007317 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset= */ false);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007318 // Loading the entrypoint does not require a load acquire since it is only changed when
7319 // threads are suspended or running a checkpoint.
7320 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7321 Register ref_reg = ref.AsRegister<Register>();
7322 Register index_reg = index.IsRegisterPair()
7323 ? index.AsRegisterPairLow<Register>()
7324 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007325 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007326 if (GetInstructionSetFeatures().IsR6()) {
Andreas Gampe3db70682018-12-26 15:12:03 -08007327 __ Beqz(T9, &skip_call, /* is_bare= */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007328 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7329 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007330 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007331 } else {
7332 __ Sll(TMP, index_reg, scale_factor);
Andreas Gampe3db70682018-12-26 15:12:03 -08007333 __ Beqz(T9, &skip_call, /* is_bare= */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007334 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7335 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007336 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007337 __ Addu(TMP, TMP, obj); // In delay slot.
7338 }
7339 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7340 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7341 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7342 __ MaybeUnpoisonHeapReference(ref_reg);
7343 __ SetReorder(reordering);
7344 return;
7345 }
7346
Alexey Frunze15958152017-02-09 19:08:30 -08007347 // /* HeapReference<Object> */ ref =
7348 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007349 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7350 ref,
7351 obj,
7352 data_offset,
7353 index,
7354 scale_factor,
7355 temp,
7356 needs_null_check);
7357}
7358
7359void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7360 Location ref,
7361 Register obj,
7362 uint32_t offset,
7363 Location index,
7364 ScaleFactor scale_factor,
7365 Location temp,
7366 bool needs_null_check,
7367 bool always_update_field) {
7368 DCHECK(kEmitCompilerReadBarrier);
7369 DCHECK(kUseBakerReadBarrier);
7370
7371 // In slow path based read barriers, the read barrier call is
7372 // inserted after the original load. However, in fast path based
7373 // Baker's read barriers, we need to perform the load of
7374 // mirror::Object::monitor_ *before* the original reference load.
7375 // This load-load ordering is required by the read barrier.
7376 // The fast path/slow path (for Baker's algorithm) should look like:
7377 //
7378 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7379 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7380 // HeapReference<Object> ref = *src; // Original reference load.
7381 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7382 // if (is_gray) {
7383 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7384 // }
7385 //
7386 // Note: the original implementation in ReadBarrier::Barrier is
7387 // slightly more complex as it performs additional checks that we do
7388 // not do here for performance reasons.
7389
7390 Register ref_reg = ref.AsRegister<Register>();
7391 Register temp_reg = temp.AsRegister<Register>();
7392 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7393
7394 // /* int32_t */ monitor = obj->monitor_
7395 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7396 if (needs_null_check) {
7397 MaybeRecordImplicitNullCheck(instruction);
7398 }
7399 // /* LockWord */ lock_word = LockWord(monitor)
7400 static_assert(sizeof(LockWord) == sizeof(int32_t),
7401 "art::LockWord and int32_t have different sizes.");
7402
7403 __ Sync(0); // Barrier to prevent load-load reordering.
7404
7405 // The actual reference load.
7406 if (index.IsValid()) {
7407 // Load types involving an "index": ArrayGet,
7408 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7409 // intrinsics.
7410 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7411 if (index.IsConstant()) {
7412 size_t computed_offset =
7413 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7414 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7415 } else {
7416 // Handle the special case of the
7417 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7418 // intrinsics, which use a register pair as index ("long
7419 // offset"), of which only the low part contains data.
7420 Register index_reg = index.IsRegisterPair()
7421 ? index.AsRegisterPairLow<Register>()
7422 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007423 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007424 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7425 }
7426 } else {
7427 // /* HeapReference<Object> */ ref = *(obj + offset)
7428 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7429 }
7430
7431 // Object* ref = ref_addr->AsMirrorPtr()
7432 __ MaybeUnpoisonHeapReference(ref_reg);
7433
7434 // Slow path marking the object `ref` when it is gray.
7435 SlowPathCodeMIPS* slow_path;
7436 if (always_update_field) {
7437 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7438 // of the form `obj + field_offset`, where `obj` is a register and
7439 // `field_offset` is a register pair (of which only the lower half
7440 // is used). Thus `offset` and `scale_factor` above are expected
7441 // to be null in this code path.
7442 DCHECK_EQ(offset, 0u);
7443 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007444 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007445 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7446 ref,
7447 obj,
Andreas Gampe3db70682018-12-26 15:12:03 -08007448 /* field_offset= */ index,
Alexey Frunze15958152017-02-09 19:08:30 -08007449 temp_reg);
7450 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007451 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007452 }
7453 AddSlowPath(slow_path);
7454
7455 // if (rb_state == ReadBarrier::GrayState())
7456 // ref = ReadBarrier::Mark(ref);
7457 // Given the numeric representation, it's enough to check the low bit of the
7458 // rb_state. We do that by shifting the bit into the sign bit (31) and
7459 // performing a branch on less than zero.
Roland Levillain14e5a292018-06-28 12:00:56 +01007460 static_assert(ReadBarrier::NonGrayState() == 0, "Expecting non-gray to have value 0");
Alexey Frunze15958152017-02-09 19:08:30 -08007461 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7462 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7463 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7464 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7465 __ Bind(slow_path->GetExitLabel());
7466}
7467
7468void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7469 Location out,
7470 Location ref,
7471 Location obj,
7472 uint32_t offset,
7473 Location index) {
7474 DCHECK(kEmitCompilerReadBarrier);
7475
7476 // Insert a slow path based read barrier *after* the reference load.
7477 //
7478 // If heap poisoning is enabled, the unpoisoning of the loaded
7479 // reference will be carried out by the runtime within the slow
7480 // path.
7481 //
7482 // Note that `ref` currently does not get unpoisoned (when heap
7483 // poisoning is enabled), which is alright as the `ref` argument is
7484 // not used by the artReadBarrierSlow entry point.
7485 //
7486 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007487 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007488 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7489 AddSlowPath(slow_path);
7490
7491 __ B(slow_path->GetEntryLabel());
7492 __ Bind(slow_path->GetExitLabel());
7493}
7494
7495void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7496 Location out,
7497 Location ref,
7498 Location obj,
7499 uint32_t offset,
7500 Location index) {
7501 if (kEmitCompilerReadBarrier) {
7502 // Baker's read barriers shall be handled by the fast path
7503 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7504 DCHECK(!kUseBakerReadBarrier);
7505 // If heap poisoning is enabled, unpoisoning will be taken care of
7506 // by the runtime within the slow path.
7507 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7508 } else if (kPoisonHeapReferences) {
7509 __ UnpoisonHeapReference(out.AsRegister<Register>());
7510 }
7511}
7512
7513void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7514 Location out,
7515 Location root) {
7516 DCHECK(kEmitCompilerReadBarrier);
7517
7518 // Insert a slow path based read barrier *after* the GC root load.
7519 //
7520 // Note that GC roots are not affected by heap poisoning, so we do
7521 // not need to do anything special for this here.
7522 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007523 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007524 AddSlowPath(slow_path);
7525
7526 __ B(slow_path->GetEntryLabel());
7527 __ Bind(slow_path->GetExitLabel());
7528}
7529
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007530void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007531 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7532 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007533 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007534 switch (type_check_kind) {
7535 case TypeCheckKind::kExactCheck:
7536 case TypeCheckKind::kAbstractClassCheck:
7537 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007538 case TypeCheckKind::kArrayObjectCheck: {
7539 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7540 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7541 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007542 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007543 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007544 case TypeCheckKind::kArrayCheck:
7545 case TypeCheckKind::kUnresolvedCheck:
7546 case TypeCheckKind::kInterfaceCheck:
7547 call_kind = LocationSummary::kCallOnSlowPath;
7548 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007549 case TypeCheckKind::kBitstringCheck:
7550 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007551 }
7552
Vladimir Markoca6fff82017-10-03 14:49:14 +01007553 LocationSummary* locations =
7554 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007555 if (baker_read_barrier_slow_path) {
7556 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7557 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007558 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007559 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7560 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7561 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7562 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7563 } else {
7564 locations->SetInAt(1, Location::RequiresRegister());
7565 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007566 // The output does overlap inputs.
7567 // Note that TypeCheckSlowPathMIPS uses this register too.
7568 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007569 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007570}
7571
7572void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007573 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007574 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007575 Location obj_loc = locations->InAt(0);
7576 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko175e7862018-03-27 09:03:13 +00007577 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08007578 Location out_loc = locations->Out();
7579 Register out = out_loc.AsRegister<Register>();
7580 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7581 DCHECK_LE(num_temps, 1u);
7582 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007583 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7584 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7585 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7586 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007587 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007588 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007589
7590 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007591 // Avoid this check if we know `obj` is not null.
7592 if (instruction->MustDoNullCheck()) {
7593 __ Move(out, ZERO);
7594 __ Beqz(obj, &done);
7595 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007596
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007597 switch (type_check_kind) {
7598 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007599 ReadBarrierOption read_barrier_option =
7600 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007601 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007602 GenerateReferenceLoadTwoRegisters(instruction,
7603 out_loc,
7604 obj_loc,
7605 class_offset,
7606 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007607 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007608 // Classes must be equal for the instanceof to succeed.
Vladimir Marko175e7862018-03-27 09:03:13 +00007609 __ Xor(out, out, cls.AsRegister<Register>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007610 __ Sltiu(out, out, 1);
7611 break;
7612 }
7613
7614 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007615 ReadBarrierOption read_barrier_option =
7616 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007617 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007618 GenerateReferenceLoadTwoRegisters(instruction,
7619 out_loc,
7620 obj_loc,
7621 class_offset,
7622 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007623 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007624 // If the class is abstract, we eagerly fetch the super class of the
7625 // object to avoid doing a comparison we know will fail.
7626 MipsLabel loop;
7627 __ Bind(&loop);
7628 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007629 GenerateReferenceLoadOneRegister(instruction,
7630 out_loc,
7631 super_offset,
7632 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007633 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007634 // If `out` is null, we use it for the result, and jump to `done`.
7635 __ Beqz(out, &done);
Vladimir Marko175e7862018-03-27 09:03:13 +00007636 __ Bne(out, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007637 __ LoadConst32(out, 1);
7638 break;
7639 }
7640
7641 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007642 ReadBarrierOption read_barrier_option =
7643 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007644 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007645 GenerateReferenceLoadTwoRegisters(instruction,
7646 out_loc,
7647 obj_loc,
7648 class_offset,
7649 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007650 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007651 // Walk over the class hierarchy to find a match.
7652 MipsLabel loop, success;
7653 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00007654 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007655 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007656 GenerateReferenceLoadOneRegister(instruction,
7657 out_loc,
7658 super_offset,
7659 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007660 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007661 __ Bnez(out, &loop);
7662 // If `out` is null, we use it for the result, and jump to `done`.
7663 __ B(&done);
7664 __ Bind(&success);
7665 __ LoadConst32(out, 1);
7666 break;
7667 }
7668
7669 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007670 ReadBarrierOption read_barrier_option =
7671 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007672 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007673 GenerateReferenceLoadTwoRegisters(instruction,
7674 out_loc,
7675 obj_loc,
7676 class_offset,
7677 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007678 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007679 // Do an exact check.
7680 MipsLabel success;
Vladimir Marko175e7862018-03-27 09:03:13 +00007681 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007682 // Otherwise, we need to check that the object's class is a non-primitive array.
7683 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007684 GenerateReferenceLoadOneRegister(instruction,
7685 out_loc,
7686 component_offset,
7687 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007688 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007689 // If `out` is null, we use it for the result, and jump to `done`.
7690 __ Beqz(out, &done);
7691 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7692 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7693 __ Sltiu(out, out, 1);
7694 __ B(&done);
7695 __ Bind(&success);
7696 __ LoadConst32(out, 1);
7697 break;
7698 }
7699
7700 case TypeCheckKind::kArrayCheck: {
7701 // No read barrier since the slow path will retry upon failure.
7702 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007703 GenerateReferenceLoadTwoRegisters(instruction,
7704 out_loc,
7705 obj_loc,
7706 class_offset,
7707 maybe_temp_loc,
7708 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007709 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007710 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
Andreas Gampe3db70682018-12-26 15:12:03 -08007711 instruction, /* is_fatal= */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007712 codegen_->AddSlowPath(slow_path);
Vladimir Marko175e7862018-03-27 09:03:13 +00007713 __ Bne(out, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007714 __ LoadConst32(out, 1);
7715 break;
7716 }
7717
7718 case TypeCheckKind::kUnresolvedCheck:
7719 case TypeCheckKind::kInterfaceCheck: {
7720 // Note that we indeed only call on slow path, but we always go
7721 // into the slow path for the unresolved and interface check
7722 // cases.
7723 //
7724 // We cannot directly call the InstanceofNonTrivial runtime
7725 // entry point without resorting to a type checking slow path
7726 // here (i.e. by calling InvokeRuntime directly), as it would
7727 // require to assign fixed registers for the inputs of this
7728 // HInstanceOf instruction (following the runtime calling
7729 // convention), which might be cluttered by the potential first
7730 // read barrier emission at the beginning of this method.
7731 //
7732 // TODO: Introduce a new runtime entry point taking the object
7733 // to test (instead of its class) as argument, and let it deal
7734 // with the read barrier issues. This will let us refactor this
7735 // case of the `switch` code as it was previously (with a direct
7736 // call to the runtime not using a type checking slow path).
7737 // This should also be beneficial for the other cases above.
7738 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007739 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
Andreas Gampe3db70682018-12-26 15:12:03 -08007740 instruction, /* is_fatal= */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007741 codegen_->AddSlowPath(slow_path);
7742 __ B(slow_path->GetEntryLabel());
7743 break;
7744 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007745
7746 case TypeCheckKind::kBitstringCheck: {
7747 // /* HeapReference<Class> */ temp = obj->klass_
7748 GenerateReferenceLoadTwoRegisters(instruction,
7749 out_loc,
7750 obj_loc,
7751 class_offset,
7752 maybe_temp_loc,
7753 kWithoutReadBarrier);
7754
7755 GenerateBitstringTypeCheckCompare(instruction, out);
7756 __ Sltiu(out, out, 1);
7757 break;
7758 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007759 }
7760
7761 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007762
7763 if (slow_path != nullptr) {
7764 __ Bind(slow_path->GetExitLabel());
7765 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007766}
7767
7768void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007769 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007770 locations->SetOut(Location::ConstantLocation(constant));
7771}
7772
7773void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7774 // Will be generated at use site.
7775}
7776
7777void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007778 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007779 locations->SetOut(Location::ConstantLocation(constant));
7780}
7781
7782void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7783 // Will be generated at use site.
7784}
7785
7786void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7787 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7788 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7789}
7790
7791void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7792 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007793 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007794 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007795 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007796}
7797
7798void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7799 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7800 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007801 Location receiver = invoke->GetLocations()->InAt(0);
7802 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007803 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007804
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007805 // temp = object->GetClass();
7806 if (receiver.IsStackSlot()) {
7807 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7808 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7809 } else {
7810 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7811 }
7812 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007813 // Instead of simply (possibly) unpoisoning `temp` here, we should
7814 // emit a read barrier for the previous class reference load.
7815 // However this is not required in practice, as this is an
7816 // intermediate/temporary reference and because the current
7817 // concurrent copying collector keeps the from-space memory
7818 // intact/accessible until the end of the marking phase (the
7819 // concurrent copying collector may not in the future).
7820 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007821 __ LoadFromOffset(kLoadWord, temp, temp,
7822 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7823 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007824 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007825 // temp = temp->GetImtEntryAt(method_offset);
7826 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7827 // T9 = temp->GetEntryPoint();
7828 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
Lena Djokic3177e102018-02-28 11:32:40 +01007829 // Set the hidden argument.
7830 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7831 invoke->GetDexMethodIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007832 // T9();
7833 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007834 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007835 DCHECK(!codegen_->IsLeafMethod());
7836 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7837}
7838
7839void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007840 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7841 if (intrinsic.TryDispatch(invoke)) {
7842 return;
7843 }
7844
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007845 HandleInvoke(invoke);
7846}
7847
7848void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007849 // Explicit clinit checks triggered by static invokes must have been pruned by
7850 // art::PrepareForRegisterAllocation.
7851 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007852
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007853 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007854 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7855 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007856
Chris Larsen701566a2015-10-27 15:29:13 -07007857 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7858 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007859 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7860 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7861 }
Chris Larsen701566a2015-10-27 15:29:13 -07007862 return;
7863 }
7864
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007865 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007866
7867 // Add the extra input register if either the dex cache array base register
7868 // or the PC-relative base register for accessing literals is needed.
7869 if (has_extra_input) {
7870 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7871 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007872}
7873
Orion Hodsonac141392017-01-13 11:53:47 +00007874void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7875 HandleInvoke(invoke);
7876}
7877
7878void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7879 codegen_->GenerateInvokePolymorphicCall(invoke);
7880}
7881
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007882void LocationsBuilderMIPS::VisitInvokeCustom(HInvokeCustom* invoke) {
7883 HandleInvoke(invoke);
7884}
7885
7886void InstructionCodeGeneratorMIPS::VisitInvokeCustom(HInvokeCustom* invoke) {
7887 codegen_->GenerateInvokeCustomCall(invoke);
7888}
7889
Chris Larsen701566a2015-10-27 15:29:13 -07007890static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007891 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007892 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7893 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007894 return true;
7895 }
7896 return false;
7897}
7898
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007899HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007900 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007901 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007902 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007903 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007904 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007905 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007906 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007907 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007908 case HLoadString::LoadKind::kJitTableAddress:
7909 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007910 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007911 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007912 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007913 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007914 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007915}
7916
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007917HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7918 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007919 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007920 case HLoadClass::LoadKind::kInvalid:
7921 LOG(FATAL) << "UNREACHABLE";
7922 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007923 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007924 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007925 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007926 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007927 case HLoadClass::LoadKind::kBssEntry:
7928 DCHECK(!Runtime::Current()->UseJitCompilation());
7929 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007930 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007931 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007932 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007933 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007934 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007935 break;
7936 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007937 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007938}
7939
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007940Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7941 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007942 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007943 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007944 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7945 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7946 if (!invoke->GetLocations()->Intrinsified()) {
7947 return location.AsRegister<Register>();
7948 }
7949 // For intrinsics we allow any location, so it may be on the stack.
7950 if (!location.IsRegister()) {
7951 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7952 return temp;
7953 }
7954 // For register locations, check if the register was saved. If so, get it from the stack.
7955 // Note: There is a chance that the register was saved but not overwritten, so we could
7956 // save one load. However, since this is just an intrinsic slow path we prefer this
7957 // simple and more robust approach rather that trying to determine if that's the case.
7958 SlowPathCode* slow_path = GetCurrentSlowPath();
7959 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7960 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7961 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7962 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7963 return temp;
7964 }
7965 return location.AsRegister<Register>();
7966}
7967
Vladimir Markodc151b22015-10-15 18:02:30 +01007968HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7969 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffraybdb2ecc2018-09-18 14:33:55 +01007970 ArtMethod* method ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007971 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007972}
7973
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007974void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7975 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007976 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007977 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007978 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7979 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007980 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007981 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7982 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007983 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7984 : ZERO;
7985
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007986 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007987 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007988 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007989 uint32_t offset =
7990 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007991 __ LoadFromOffset(kLoadWord,
7992 temp.AsRegister<Register>(),
7993 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007994 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007995 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007996 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007997 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007998 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007999 break;
Vladimir Marko65979462017-05-19 17:25:12 +01008000 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01008001 DCHECK(GetCompilerOptions().IsBootImage() || GetCompilerOptions().IsBootImageExtension());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008002 PcRelativePatchInfo* info_high = NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008003 PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008004 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01008005 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008006 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08008007 __ Addiu(temp_reg, TMP, /* imm16= */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01008008 break;
8009 }
Vladimir Markob066d432018-01-03 13:14:37 +00008010 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008011 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00008012 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
8013 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
8014 Register temp_reg = temp.AsRegister<Register>();
8015 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08008016 __ Lw(temp_reg, TMP, /* imm16= */ 0x5678, &info_low->label);
Vladimir Markob066d432018-01-03 13:14:37 +00008017 break;
8018 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008019 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008020 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008021 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008022 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
8023 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008024 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008025 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08008026 __ Lw(temp_reg, TMP, /* imm16= */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008027 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008028 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008029 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
8030 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8031 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008032 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8033 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8034 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008035 }
8036 }
8037
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008038 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008039 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008040 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008041 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008042 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8043 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01008044 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008045 T9,
8046 callee_method.AsRegister<Register>(),
8047 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07008048 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008049 // T9()
8050 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008051 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008052 break;
8053 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008054 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8055
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008056 DCHECK(!IsLeafMethod());
8057}
8058
8059void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00008060 // Explicit clinit checks triggered by static invokes must have been pruned by
8061 // art::PrepareForRegisterAllocation.
8062 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008063
8064 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8065 return;
8066 }
8067
8068 LocationSummary* locations = invoke->GetLocations();
8069 codegen_->GenerateStaticOrDirectCall(invoke,
8070 locations->HasTemps()
8071 ? locations->GetTemp(0)
8072 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008073}
8074
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008075void CodeGeneratorMIPS::GenerateVirtualCall(
8076 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02008077 // Use the calling convention instead of the location of the receiver, as
8078 // intrinsics may have put the receiver in a different register. In the intrinsics
8079 // slow path, the arguments have been moved to the right place, so here we are
8080 // guaranteed that the receiver is the first register of the calling convention.
8081 InvokeDexCallingConvention calling_convention;
8082 Register receiver = calling_convention.GetRegisterAt(0);
8083
Chris Larsen3acee732015-11-18 13:31:08 -08008084 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008085 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8086 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
8087 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07008088 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008089
8090 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02008091 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08008092 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08008093 // Instead of simply (possibly) unpoisoning `temp` here, we should
8094 // emit a read barrier for the previous class reference load.
8095 // However this is not required in practice, as this is an
8096 // intermediate/temporary reference and because the current
8097 // concurrent copying collector keeps the from-space memory
8098 // intact/accessible until the end of the marking phase (the
8099 // concurrent copying collector may not in the future).
8100 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008101 // temp = temp->GetMethodAt(method_offset);
8102 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8103 // T9 = temp->GetEntryPoint();
8104 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
8105 // T9();
8106 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008107 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008108 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08008109}
8110
8111void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
8112 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8113 return;
8114 }
8115
8116 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008117 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008118}
8119
8120void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00008121 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008122 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008123 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008124 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8125 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008126 return;
8127 }
Vladimir Marko41559982017-01-06 14:04:23 +00008128 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008129 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008130 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08008131 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
8132 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07008133 ? LocationSummary::kCallOnSlowPath
8134 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008135 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008136 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
8137 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
8138 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008139 switch (load_kind) {
8140 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008141 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008142 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008143 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008144 case HLoadClass::LoadKind::kJitBootImageAddress:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008145 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008146 break;
8147 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008148 if (has_irreducible_loops) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008149 if (load_kind != HLoadClass::LoadKind::kJitBootImageAddress) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008150 codegen_->ClobberRA();
8151 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008152 break;
8153 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008154 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008155 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008156 locations->SetInAt(0, Location::RequiresRegister());
8157 break;
8158 default:
8159 break;
8160 }
8161 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008162 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
8163 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8164 // Rely on the type resolution or initialization and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01008165 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008166 } else {
8167 // For non-Baker read barriers we have a temp-clobbering call.
8168 }
8169 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008170}
8171
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008172// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8173// move.
8174void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00008175 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008176 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00008177 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01008178 return;
8179 }
Vladimir Marko41559982017-01-06 14:04:23 +00008180 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008181
Vladimir Marko41559982017-01-06 14:04:23 +00008182 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008183 Location out_loc = locations->Out();
8184 Register out = out_loc.AsRegister<Register>();
8185 Register base_or_current_method_reg;
8186 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008187 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008188 switch (load_kind) {
8189 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008190 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008191 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008192 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008193 case HLoadClass::LoadKind::kJitBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008194 base_or_current_method_reg =
8195 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008196 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008197 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008198 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008199 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8200 break;
8201 default:
8202 base_or_current_method_reg = ZERO;
8203 break;
8204 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008205
Alexey Frunze15958152017-02-09 19:08:30 -08008206 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8207 ? kWithoutReadBarrier
8208 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008209 bool generate_null_check = false;
8210 switch (load_kind) {
8211 case HLoadClass::LoadKind::kReferrersClass: {
8212 DCHECK(!cls->CanCallRuntime());
8213 DCHECK(!cls->MustGenerateClinitCheck());
8214 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8215 GenerateGcRootFieldLoad(cls,
8216 out_loc,
8217 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008218 ArtMethod::DeclaringClassOffset().Int32Value(),
8219 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008220 break;
8221 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008222 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01008223 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
8224 codegen_->GetCompilerOptions().IsBootImageExtension());
Alexey Frunze15958152017-02-09 19:08:30 -08008225 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008226 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008227 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008228 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008229 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008230 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8231 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008232 base_or_current_method_reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08008233 __ Addiu(out, out, /* imm16= */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008234 break;
8235 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008236 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008237 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008238 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008239 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008240 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008241 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008242 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008243 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8244 out,
8245 base_or_current_method_reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08008246 __ Lw(out, out, /* imm16= */ 0x5678, &info_low->label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008247 break;
8248 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008249 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008250 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8251 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008252 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8253 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008254 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008255 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008256 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008257 GenerateGcRootFieldLoad(cls,
8258 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008259 out,
Andreas Gampe3db70682018-12-26 15:12:03 -08008260 /* offset= */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008261 read_barrier_option,
8262 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008263 generate_null_check = true;
8264 break;
8265 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008266 case HLoadClass::LoadKind::kJitBootImageAddress: {
8267 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
8268 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
8269 DCHECK_NE(address, 0u);
8270 if (isR6 || !has_irreducible_loops) {
8271 __ LoadLiteral(out,
8272 base_or_current_method_reg,
8273 codegen_->DeduplicateBootImageAddressLiteral(address));
8274 } else {
8275 __ LoadConst32(out, address);
8276 }
8277 break;
8278 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008279 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008280 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8281 cls->GetTypeIndex(),
8282 cls->GetClass());
8283 bool reordering = __ SetReorder(false);
8284 __ Bind(&info->high_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08008285 __ Lui(out, /* imm16= */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008286 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008287 GenerateGcRootFieldLoad(cls,
8288 out_loc,
8289 out,
Andreas Gampe3db70682018-12-26 15:12:03 -08008290 /* offset= */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008291 read_barrier_option,
8292 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008293 break;
8294 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008295 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008296 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008297 LOG(FATAL) << "UNREACHABLE";
8298 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008299 }
8300
8301 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8302 DCHECK(cls->CanCallRuntime());
Vladimir Markoa9f303c2018-07-20 16:43:56 +01008303 SlowPathCodeMIPS* slow_path =
8304 new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(cls, cls);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008305 codegen_->AddSlowPath(slow_path);
8306 if (generate_null_check) {
8307 __ Beqz(out, slow_path->GetEntryLabel());
8308 }
8309 if (cls->MustGenerateClinitCheck()) {
8310 GenerateClassInitializationCheck(slow_path, out);
8311 } else {
8312 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008313 }
8314 }
8315}
8316
Orion Hodsondbaa5c72018-05-10 08:22:46 +01008317void LocationsBuilderMIPS::VisitLoadMethodHandle(HLoadMethodHandle* load) {
8318 InvokeRuntimeCallingConvention calling_convention;
8319 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8320 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, loc, loc);
8321}
8322
8323void InstructionCodeGeneratorMIPS::VisitLoadMethodHandle(HLoadMethodHandle* load) {
8324 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
8325}
8326
Orion Hodson18259d72018-04-12 11:18:23 +01008327void LocationsBuilderMIPS::VisitLoadMethodType(HLoadMethodType* load) {
8328 InvokeRuntimeCallingConvention calling_convention;
8329 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8330 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, loc, loc);
8331}
8332
8333void InstructionCodeGeneratorMIPS::VisitLoadMethodType(HLoadMethodType* load) {
8334 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
8335}
8336
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008337static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008338 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008339}
8340
8341void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8342 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008343 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008344 locations->SetOut(Location::RequiresRegister());
8345}
8346
8347void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8348 Register out = load->GetLocations()->Out().AsRegister<Register>();
8349 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8350}
8351
8352void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008353 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008354}
8355
8356void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8357 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8358}
8359
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008360void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008361 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008362 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008363 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008364 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008365 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008366 switch (load_kind) {
8367 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008368 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008369 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008370 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008371 case HLoadString::LoadKind::kJitBootImageAddress:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008372 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008373 break;
8374 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008375 if (has_irreducible_loops) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008376 if (load_kind != HLoadString::LoadKind::kJitBootImageAddress) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008377 codegen_->ClobberRA();
8378 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008379 break;
8380 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008381 FALLTHROUGH_INTENDED;
8382 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008383 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008384 locations->SetInAt(0, Location::RequiresRegister());
8385 break;
8386 default:
8387 break;
8388 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008389 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008390 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008391 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008392 } else {
8393 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008394 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8395 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8396 // Rely on the pResolveString and marking to save everything we need.
Vladimir Marko3232dbb2018-07-25 15:42:46 +01008397 locations->SetCustomSlowPathCallerSaves(OneRegInReferenceOutSaveEverythingCallerSaves());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008398 } else {
8399 // For non-Baker read barriers we have a temp-clobbering call.
8400 }
8401 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008402 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008403}
8404
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008405// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8406// move.
8407void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008408 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008409 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008410 Location out_loc = locations->Out();
8411 Register out = out_loc.AsRegister<Register>();
8412 Register base_or_current_method_reg;
8413 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008414 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008415 switch (load_kind) {
8416 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008417 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008418 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008419 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008420 case HLoadString::LoadKind::kJitBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008421 base_or_current_method_reg =
8422 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008423 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008424 default:
8425 base_or_current_method_reg = ZERO;
8426 break;
8427 }
8428
8429 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008430 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko44ca0752019-07-29 10:18:25 +01008431 DCHECK(codegen_->GetCompilerOptions().IsBootImage() ||
8432 codegen_->GetCompilerOptions().IsBootImageExtension());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008433 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008434 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008435 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008436 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008437 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8438 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008439 base_or_current_method_reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08008440 __ Addiu(out, out, /* imm16= */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008441 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008442 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008443 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008444 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008445 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008446 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008447 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008448 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008449 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008450 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8451 out,
8452 base_or_current_method_reg);
Andreas Gampe3db70682018-12-26 15:12:03 -08008453 __ Lw(out, out, /* imm16= */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008454 return;
8455 }
8456 case HLoadString::LoadKind::kBssEntry: {
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008457 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8458 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8459 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8460 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008461 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008462 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008463 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008464 GenerateGcRootFieldLoad(load,
8465 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008466 out,
Andreas Gampe3db70682018-12-26 15:12:03 -08008467 /* offset= */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008468 kCompilerReadBarrierOption,
8469 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008470 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008471 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008472 codegen_->AddSlowPath(slow_path);
8473 __ Beqz(out, slow_path->GetEntryLabel());
8474 __ Bind(slow_path->GetExitLabel());
8475 return;
8476 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008477 case HLoadString::LoadKind::kJitBootImageAddress: {
8478 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
8479 DCHECK_NE(address, 0u);
8480 if (isR6 || !has_irreducible_loops) {
8481 __ LoadLiteral(out,
8482 base_or_current_method_reg,
8483 codegen_->DeduplicateBootImageAddressLiteral(address));
8484 } else {
8485 __ LoadConst32(out, address);
8486 }
8487 return;
8488 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008489 case HLoadString::LoadKind::kJitTableAddress: {
8490 CodeGeneratorMIPS::JitPatchInfo* info =
8491 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8492 load->GetStringIndex(),
8493 load->GetString());
8494 bool reordering = __ SetReorder(false);
8495 __ Bind(&info->high_label);
Andreas Gampe3db70682018-12-26 15:12:03 -08008496 __ Lui(out, /* imm16= */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008497 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008498 GenerateGcRootFieldLoad(load,
8499 out_loc,
8500 out,
Andreas Gampe3db70682018-12-26 15:12:03 -08008501 /* offset= */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008502 kCompilerReadBarrierOption,
8503 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008504 return;
8505 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008506 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008507 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008508 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008509
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008510 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008511 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008512 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008513 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008514 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008515 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8516 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008517}
8518
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008519void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008520 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008521 locations->SetOut(Location::ConstantLocation(constant));
8522}
8523
8524void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8525 // Will be generated at use site.
8526}
8527
8528void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008529 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8530 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008531 InvokeRuntimeCallingConvention calling_convention;
8532 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8533}
8534
8535void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8536 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008537 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008538 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8539 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008540 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008541 }
8542 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8543}
8544
8545void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8546 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008547 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008548 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008549 case DataType::Type::kInt32:
8550 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008551 locations->SetInAt(0, Location::RequiresRegister());
8552 locations->SetInAt(1, Location::RequiresRegister());
8553 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8554 break;
8555
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008556 case DataType::Type::kFloat32:
8557 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008558 locations->SetInAt(0, Location::RequiresFpuRegister());
8559 locations->SetInAt(1, Location::RequiresFpuRegister());
8560 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8561 break;
8562
8563 default:
8564 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8565 }
8566}
8567
8568void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008569 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008570 LocationSummary* locations = instruction->GetLocations();
8571 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8572
8573 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008574 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008575 Register dst = locations->Out().AsRegister<Register>();
8576 Register lhs = locations->InAt(0).AsRegister<Register>();
8577 Register rhs = locations->InAt(1).AsRegister<Register>();
8578
8579 if (isR6) {
8580 __ MulR6(dst, lhs, rhs);
8581 } else {
8582 __ MulR2(dst, lhs, rhs);
8583 }
8584 break;
8585 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008586 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008587 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8588 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8589 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8590 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8591 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8592 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8593
8594 // Extra checks to protect caused by the existance of A1_A2.
8595 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8596 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8597 DCHECK_NE(dst_high, lhs_low);
8598 DCHECK_NE(dst_high, rhs_low);
8599
8600 // A_B * C_D
8601 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8602 // dst_lo: [ low(B*D) ]
8603 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8604
8605 if (isR6) {
8606 __ MulR6(TMP, lhs_high, rhs_low);
8607 __ MulR6(dst_high, lhs_low, rhs_high);
8608 __ Addu(dst_high, dst_high, TMP);
8609 __ MuhuR6(TMP, lhs_low, rhs_low);
8610 __ Addu(dst_high, dst_high, TMP);
8611 __ MulR6(dst_low, lhs_low, rhs_low);
8612 } else {
8613 __ MulR2(TMP, lhs_high, rhs_low);
8614 __ MulR2(dst_high, lhs_low, rhs_high);
8615 __ Addu(dst_high, dst_high, TMP);
8616 __ MultuR2(lhs_low, rhs_low);
8617 __ Mfhi(TMP);
8618 __ Addu(dst_high, dst_high, TMP);
8619 __ Mflo(dst_low);
8620 }
8621 break;
8622 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008623 case DataType::Type::kFloat32:
8624 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008625 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8626 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8627 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008628 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008629 __ MulS(dst, lhs, rhs);
8630 } else {
8631 __ MulD(dst, lhs, rhs);
8632 }
8633 break;
8634 }
8635 default:
8636 LOG(FATAL) << "Unexpected mul type " << type;
8637 }
8638}
8639
8640void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8641 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008642 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008643 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008644 case DataType::Type::kInt32:
8645 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008646 locations->SetInAt(0, Location::RequiresRegister());
8647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8648 break;
8649
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008650 case DataType::Type::kFloat32:
8651 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008652 locations->SetInAt(0, Location::RequiresFpuRegister());
8653 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8654 break;
8655
8656 default:
8657 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8658 }
8659}
8660
8661void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008662 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008663 LocationSummary* locations = instruction->GetLocations();
8664
8665 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008666 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008667 Register dst = locations->Out().AsRegister<Register>();
8668 Register src = locations->InAt(0).AsRegister<Register>();
8669 __ Subu(dst, ZERO, src);
8670 break;
8671 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008672 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008673 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8674 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8675 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8676 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8677 __ Subu(dst_low, ZERO, src_low);
8678 __ Sltu(TMP, ZERO, dst_low);
8679 __ Subu(dst_high, ZERO, src_high);
8680 __ Subu(dst_high, dst_high, TMP);
8681 break;
8682 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008683 case DataType::Type::kFloat32:
8684 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008685 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8686 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008687 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008688 __ NegS(dst, src);
8689 } else {
8690 __ NegD(dst, src);
8691 }
8692 break;
8693 }
8694 default:
8695 LOG(FATAL) << "Unexpected neg type " << type;
8696 }
8697}
8698
8699void LocationsBuilderMIPS::VisitNewArray(HNewArray* 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;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008703 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008704 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8705 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008706}
8707
8708void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markob5461632018-10-15 14:24:21 +01008709 // Note: if heap poisoning is enabled, the entry point takes care of poisoning the reference.
8710 QuickEntrypointEnum entrypoint = CodeGenerator::GetArrayAllocationEntrypoint(instruction);
Goran Jakovljevic854df412017-06-27 14:41:39 +02008711 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008712 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008713 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008714}
8715
8716void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008717 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8718 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008719 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07008720 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008721 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008722}
8723
8724void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07008725 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
8726 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008727}
8728
8729void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008730 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008731 locations->SetInAt(0, Location::RequiresRegister());
8732 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8733}
8734
8735void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008736 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008737 LocationSummary* locations = instruction->GetLocations();
8738
8739 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008740 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008741 Register dst = locations->Out().AsRegister<Register>();
8742 Register src = locations->InAt(0).AsRegister<Register>();
8743 __ Nor(dst, src, ZERO);
8744 break;
8745 }
8746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008747 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008748 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8749 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8750 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8751 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8752 __ Nor(dst_high, src_high, ZERO);
8753 __ Nor(dst_low, src_low, ZERO);
8754 break;
8755 }
8756
8757 default:
8758 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8759 }
8760}
8761
8762void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008763 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008764 locations->SetInAt(0, Location::RequiresRegister());
8765 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8766}
8767
8768void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8769 LocationSummary* locations = instruction->GetLocations();
8770 __ Xori(locations->Out().AsRegister<Register>(),
8771 locations->InAt(0).AsRegister<Register>(),
8772 1);
8773}
8774
8775void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008776 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8777 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008778}
8779
Calin Juravle2ae48182016-03-16 14:05:09 +00008780void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8781 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008782 return;
8783 }
8784 Location obj = instruction->GetLocations()->InAt(0);
8785
8786 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008787 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008788}
8789
Calin Juravle2ae48182016-03-16 14:05:09 +00008790void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008791 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008792 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008793
8794 Location obj = instruction->GetLocations()->InAt(0);
8795
8796 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8797}
8798
8799void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008800 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008801}
8802
8803void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8804 HandleBinaryOp(instruction);
8805}
8806
8807void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8808 HandleBinaryOp(instruction);
8809}
8810
8811void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8812 LOG(FATAL) << "Unreachable";
8813}
8814
8815void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008816 if (instruction->GetNext()->IsSuspendCheck() &&
8817 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8818 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8819 // The back edge will generate the suspend check.
8820 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8821 }
8822
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008823 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8824}
8825
8826void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008827 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008828 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8829 if (location.IsStackSlot()) {
8830 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8831 } else if (location.IsDoubleStackSlot()) {
8832 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8833 }
8834 locations->SetOut(location);
8835}
8836
8837void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8838 ATTRIBUTE_UNUSED) {
8839 // Nothing to do, the parameter is already at its location.
8840}
8841
8842void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8843 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008844 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008845 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8846}
8847
8848void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8849 ATTRIBUTE_UNUSED) {
8850 // Nothing to do, the method is already at its location.
8851}
8852
8853void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008854 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008855 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008856 locations->SetInAt(i, Location::Any());
8857 }
8858 locations->SetOut(Location::Any());
8859}
8860
8861void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8862 LOG(FATAL) << "Unreachable";
8863}
8864
8865void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008866 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008867 bool call_rem;
8868 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8869 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8870 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8871 } else {
8872 call_rem = (type != DataType::Type::kInt32);
8873 }
8874 LocationSummary::CallKind call_kind = call_rem
8875 ? LocationSummary::kCallOnMainOnly
8876 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008877 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008878
8879 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008880 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008881 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008882 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008883 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8884 break;
8885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008886 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008887 if (call_rem) {
8888 InvokeRuntimeCallingConvention calling_convention;
8889 locations->SetInAt(0, Location::RegisterPairLocation(
8890 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8891 locations->SetInAt(1, Location::RegisterPairLocation(
8892 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8893 locations->SetOut(calling_convention.GetReturnLocation(type));
8894 } else {
8895 locations->SetInAt(0, Location::RequiresRegister());
8896 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8897 locations->SetOut(Location::RequiresRegister());
8898 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008899 break;
8900 }
8901
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008902 case DataType::Type::kFloat32:
8903 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008904 InvokeRuntimeCallingConvention calling_convention;
8905 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8906 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8907 locations->SetOut(calling_convention.GetReturnLocation(type));
8908 break;
8909 }
8910
8911 default:
8912 LOG(FATAL) << "Unexpected rem type " << type;
8913 }
8914}
8915
8916void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008917 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008918 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008919
8920 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008921 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008922 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008923 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008924 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008925 if (locations->InAt(1).IsConstant()) {
8926 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8927 if (imm == 0) {
8928 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8929 } else if (imm == 1 || imm == -1) {
8930 DivRemOneOrMinusOne(instruction);
8931 } else {
8932 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8933 DivRemByPowerOfTwo(instruction);
8934 }
8935 } else {
8936 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8937 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8938 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008939 break;
8940 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008941 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008942 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008943 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008944 break;
8945 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008946 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008947 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008948 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008949 break;
8950 }
8951 default:
8952 LOG(FATAL) << "Unexpected rem type " << type;
8953 }
8954}
8955
Aart Bik1f8d51b2018-02-15 10:42:37 -08008956static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
8957 LocationSummary* locations = new (allocator) LocationSummary(minmax);
8958 switch (minmax->GetResultType()) {
8959 case DataType::Type::kInt32:
8960 case DataType::Type::kInt64:
8961 locations->SetInAt(0, Location::RequiresRegister());
8962 locations->SetInAt(1, Location::RequiresRegister());
8963 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8964 break;
8965 case DataType::Type::kFloat32:
8966 case DataType::Type::kFloat64:
8967 locations->SetInAt(0, Location::RequiresFpuRegister());
8968 locations->SetInAt(1, Location::RequiresFpuRegister());
8969 locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap);
8970 break;
8971 default:
8972 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
8973 }
8974}
8975
Aart Bik351df3e2018-03-07 11:54:57 -08008976void InstructionCodeGeneratorMIPS::GenerateMinMaxInt(LocationSummary* locations,
8977 bool is_min,
8978 bool isR6,
8979 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08008980 if (isR6) {
8981 // Some architectures, such as ARM and MIPS (prior to r6), have a
8982 // conditional move instruction which only changes the target
8983 // (output) register if the condition is true (MIPS prior to r6 had
8984 // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions
8985 // always change the target (output) register. If the condition is
8986 // true the output register gets the contents of the "rs" register;
8987 // otherwise, the output register is set to zero. One consequence
8988 // of this is that to implement something like "rd = c==0 ? rs : rt"
8989 // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions.
8990 // After executing this pair of instructions one of the output
8991 // registers from the pair will necessarily contain zero. Then the
8992 // code ORs the output registers from the SELEQZ/SELNEZ instructions
8993 // to get the final result.
8994 //
8995 // The initial test to see if the output register is same as the
8996 // first input register is needed to make sure that value in the
8997 // first input register isn't clobbered before we've finished
8998 // computing the output value. The logic in the corresponding else
8999 // clause performs the same task but makes sure the second input
9000 // register isn't clobbered in the event that it's the same register
9001 // as the output register; the else clause also handles the case
9002 // where the output register is distinct from both the first, and the
9003 // second input registers.
9004 if (type == DataType::Type::kInt64) {
9005 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9006 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9007 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
9008 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
9009 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9010 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9011
9012 MipsLabel compare_done;
9013
9014 if (a_lo == b_lo) {
9015 if (out_lo != a_lo) {
9016 __ Move(out_lo, a_lo);
9017 __ Move(out_hi, a_hi);
9018 }
9019 } else {
9020 __ Slt(TMP, b_hi, a_hi);
9021 __ Bne(b_hi, a_hi, &compare_done);
9022
9023 __ Sltu(TMP, b_lo, a_lo);
9024
9025 __ Bind(&compare_done);
9026
9027 if (is_min) {
9028 __ Seleqz(AT, a_lo, TMP);
9029 __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo
9030 // because at this point we're
9031 // done using a_lo/b_lo.
9032 } else {
9033 __ Selnez(AT, a_lo, TMP);
9034 __ Seleqz(out_lo, b_lo, TMP); // ditto
9035 }
9036 __ Or(out_lo, out_lo, AT);
9037 if (is_min) {
9038 __ Seleqz(AT, a_hi, TMP);
9039 __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
9040 } else {
9041 __ Selnez(AT, a_hi, TMP);
9042 __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
9043 }
9044 __ Or(out_hi, out_hi, AT);
9045 }
9046 } else {
9047 DCHECK_EQ(type, DataType::Type::kInt32);
9048 Register a = locations->InAt(0).AsRegister<Register>();
9049 Register b = locations->InAt(1).AsRegister<Register>();
9050 Register out = locations->Out().AsRegister<Register>();
9051
9052 if (a == b) {
9053 if (out != a) {
9054 __ Move(out, a);
9055 }
9056 } else {
9057 __ Slt(AT, b, a);
9058 if (is_min) {
9059 __ Seleqz(TMP, a, AT);
9060 __ Selnez(AT, b, AT);
9061 } else {
9062 __ Selnez(TMP, a, AT);
9063 __ Seleqz(AT, b, AT);
9064 }
9065 __ Or(out, TMP, AT);
9066 }
9067 }
9068 } else { // !isR6
9069 if (type == DataType::Type::kInt64) {
9070 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9071 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9072 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
9073 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
9074 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9075 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9076
9077 MipsLabel compare_done;
9078
9079 if (a_lo == b_lo) {
9080 if (out_lo != a_lo) {
9081 __ Move(out_lo, a_lo);
9082 __ Move(out_hi, a_hi);
9083 }
9084 } else {
9085 __ Slt(TMP, a_hi, b_hi);
9086 __ Bne(a_hi, b_hi, &compare_done);
9087
9088 __ Sltu(TMP, a_lo, b_lo);
9089
9090 __ Bind(&compare_done);
9091
9092 if (is_min) {
9093 if (out_lo != a_lo) {
9094 __ Movn(out_hi, a_hi, TMP);
9095 __ Movn(out_lo, a_lo, TMP);
9096 }
9097 if (out_lo != b_lo) {
9098 __ Movz(out_hi, b_hi, TMP);
9099 __ Movz(out_lo, b_lo, TMP);
9100 }
9101 } else {
9102 if (out_lo != a_lo) {
9103 __ Movz(out_hi, a_hi, TMP);
9104 __ Movz(out_lo, a_lo, TMP);
9105 }
9106 if (out_lo != b_lo) {
9107 __ Movn(out_hi, b_hi, TMP);
9108 __ Movn(out_lo, b_lo, TMP);
9109 }
9110 }
9111 }
9112 } else {
9113 DCHECK_EQ(type, DataType::Type::kInt32);
9114 Register a = locations->InAt(0).AsRegister<Register>();
9115 Register b = locations->InAt(1).AsRegister<Register>();
9116 Register out = locations->Out().AsRegister<Register>();
9117
9118 if (a == b) {
9119 if (out != a) {
9120 __ Move(out, a);
9121 }
9122 } else {
9123 __ Slt(AT, a, b);
9124 if (is_min) {
9125 if (out != a) {
9126 __ Movn(out, a, AT);
9127 }
9128 if (out != b) {
9129 __ Movz(out, b, AT);
9130 }
9131 } else {
9132 if (out != a) {
9133 __ Movz(out, a, AT);
9134 }
9135 if (out != b) {
9136 __ Movn(out, b, AT);
9137 }
9138 }
9139 }
9140 }
9141 }
9142}
9143
9144void InstructionCodeGeneratorMIPS::GenerateMinMaxFP(LocationSummary* locations,
9145 bool is_min,
9146 bool isR6,
9147 DataType::Type type) {
9148 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9149 FRegister a = locations->InAt(0).AsFpuRegister<FRegister>();
9150 FRegister b = locations->InAt(1).AsFpuRegister<FRegister>();
9151
9152 if (isR6) {
9153 MipsLabel noNaNs;
9154 MipsLabel done;
9155 FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
9156
9157 // When Java computes min/max it prefers a NaN to a number; the
9158 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
9159 // the inputs is a NaN and the other is a valid number, the MIPS
9160 // instruction will return the number; Java wants the NaN value
9161 // returned. This is why there is extra logic preceding the use of
9162 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
9163 // NaN, return the NaN, otherwise return the min/max.
9164 if (type == DataType::Type::kFloat64) {
9165 __ CmpUnD(FTMP, a, b);
9166 __ Bc1eqz(FTMP, &noNaNs);
9167
9168 // One of the inputs is a NaN
9169 __ CmpEqD(ftmp, a, a);
9170 // If a == a then b is the NaN, otherwise a is the NaN.
9171 __ SelD(ftmp, a, b);
9172
9173 if (ftmp != out) {
9174 __ MovD(out, ftmp);
9175 }
9176
9177 __ B(&done);
9178
9179 __ Bind(&noNaNs);
9180
9181 if (is_min) {
9182 __ MinD(out, a, b);
9183 } else {
9184 __ MaxD(out, a, b);
9185 }
9186 } else {
9187 DCHECK_EQ(type, DataType::Type::kFloat32);
9188 __ CmpUnS(FTMP, a, b);
9189 __ Bc1eqz(FTMP, &noNaNs);
9190
9191 // One of the inputs is a NaN
9192 __ CmpEqS(ftmp, a, a);
9193 // If a == a then b is the NaN, otherwise a is the NaN.
9194 __ SelS(ftmp, a, b);
9195
9196 if (ftmp != out) {
9197 __ MovS(out, ftmp);
9198 }
9199
9200 __ B(&done);
9201
9202 __ Bind(&noNaNs);
9203
9204 if (is_min) {
9205 __ MinS(out, a, b);
9206 } else {
9207 __ MaxS(out, a, b);
9208 }
9209 }
9210
9211 __ Bind(&done);
9212
9213 } else { // !isR6
9214 MipsLabel ordered;
9215 MipsLabel compare;
9216 MipsLabel select;
9217 MipsLabel done;
9218
9219 if (type == DataType::Type::kFloat64) {
9220 __ CunD(a, b);
9221 } else {
9222 DCHECK_EQ(type, DataType::Type::kFloat32);
9223 __ CunS(a, b);
9224 }
9225 __ Bc1f(&ordered);
9226
9227 // a or b (or both) is a NaN. Return one, which is a NaN.
9228 if (type == DataType::Type::kFloat64) {
9229 __ CeqD(b, b);
9230 } else {
9231 __ CeqS(b, b);
9232 }
9233 __ B(&select);
9234
9235 __ Bind(&ordered);
9236
9237 // Neither is a NaN.
9238 // a == b? (-0.0 compares equal with +0.0)
9239 // If equal, handle zeroes, else compare further.
9240 if (type == DataType::Type::kFloat64) {
9241 __ CeqD(a, b);
9242 } else {
9243 __ CeqS(a, b);
9244 }
9245 __ Bc1f(&compare);
9246
9247 // a == b either bit for bit or one is -0.0 and the other is +0.0.
9248 if (type == DataType::Type::kFloat64) {
9249 __ MoveFromFpuHigh(TMP, a);
9250 __ MoveFromFpuHigh(AT, b);
9251 } else {
9252 __ Mfc1(TMP, a);
9253 __ Mfc1(AT, b);
9254 }
9255
9256 if (is_min) {
9257 // -0.0 prevails over +0.0.
9258 __ Or(TMP, TMP, AT);
9259 } else {
9260 // +0.0 prevails over -0.0.
9261 __ And(TMP, TMP, AT);
9262 }
9263
9264 if (type == DataType::Type::kFloat64) {
9265 __ Mfc1(AT, a);
9266 __ Mtc1(AT, out);
9267 __ MoveToFpuHigh(TMP, out);
9268 } else {
9269 __ Mtc1(TMP, out);
9270 }
9271 __ B(&done);
9272
9273 __ Bind(&compare);
9274
9275 if (type == DataType::Type::kFloat64) {
9276 if (is_min) {
9277 // return (a <= b) ? a : b;
9278 __ ColeD(a, b);
9279 } else {
9280 // return (a >= b) ? a : b;
9281 __ ColeD(b, a); // b <= a
9282 }
9283 } else {
9284 if (is_min) {
9285 // return (a <= b) ? a : b;
9286 __ ColeS(a, b);
9287 } else {
9288 // return (a >= b) ? a : b;
9289 __ ColeS(b, a); // b <= a
9290 }
9291 }
9292
9293 __ Bind(&select);
9294
9295 if (type == DataType::Type::kFloat64) {
9296 __ MovtD(out, a);
9297 __ MovfD(out, b);
9298 } else {
9299 __ MovtS(out, a);
9300 __ MovfS(out, b);
9301 }
9302
9303 __ Bind(&done);
9304 }
9305}
9306
Aart Bik351df3e2018-03-07 11:54:57 -08009307void InstructionCodeGeneratorMIPS::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
9308 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9309 DataType::Type type = minmax->GetResultType();
9310 switch (type) {
9311 case DataType::Type::kInt32:
9312 case DataType::Type::kInt64:
9313 GenerateMinMaxInt(minmax->GetLocations(), is_min, isR6, type);
9314 break;
9315 case DataType::Type::kFloat32:
9316 case DataType::Type::kFloat64:
9317 GenerateMinMaxFP(minmax->GetLocations(), is_min, isR6, type);
9318 break;
9319 default:
9320 LOG(FATAL) << "Unexpected type for HMinMax " << type;
9321 }
9322}
9323
Aart Bik1f8d51b2018-02-15 10:42:37 -08009324void LocationsBuilderMIPS::VisitMin(HMin* min) {
9325 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
9326}
9327
9328void InstructionCodeGeneratorMIPS::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08009329 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009330}
9331
9332void LocationsBuilderMIPS::VisitMax(HMax* max) {
9333 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
9334}
9335
9336void InstructionCodeGeneratorMIPS::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08009337 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009338}
9339
Aart Bik3dad3412018-02-28 12:01:46 -08009340void LocationsBuilderMIPS::VisitAbs(HAbs* abs) {
9341 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
9342 switch (abs->GetResultType()) {
9343 case DataType::Type::kInt32:
9344 case DataType::Type::kInt64:
9345 locations->SetInAt(0, Location::RequiresRegister());
9346 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9347 break;
9348 case DataType::Type::kFloat32:
9349 case DataType::Type::kFloat64:
9350 locations->SetInAt(0, Location::RequiresFpuRegister());
9351 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9352 break;
9353 default:
9354 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9355 }
9356}
9357
9358void InstructionCodeGeneratorMIPS::GenerateAbsFP(LocationSummary* locations,
9359 DataType::Type type,
9360 bool isR2OrNewer,
9361 bool isR6) {
9362 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
9363 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9364
9365 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
9366 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
9367 // (signaling NaN may become quiet though).
9368 //
9369 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
9370 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
9371 // affected by this instruction.
9372 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
9373 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
9374 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
9375 if (isR6) {
9376 if (type == DataType::Type::kFloat64) {
9377 __ AbsD(out, in);
9378 } else {
9379 DCHECK_EQ(type, DataType::Type::kFloat32);
9380 __ AbsS(out, in);
9381 }
9382 } else {
9383 if (type == DataType::Type::kFloat64) {
9384 if (in != out) {
9385 __ MovD(out, in);
9386 }
9387 __ MoveFromFpuHigh(TMP, in);
9388 // ins instruction is not available for R1.
9389 if (isR2OrNewer) {
9390 __ Ins(TMP, ZERO, 31, 1);
9391 } else {
9392 __ Sll(TMP, TMP, 1);
9393 __ Srl(TMP, TMP, 1);
9394 }
9395 __ MoveToFpuHigh(TMP, out);
9396 } else {
9397 DCHECK_EQ(type, DataType::Type::kFloat32);
9398 __ Mfc1(TMP, in);
9399 // ins instruction is not available for R1.
9400 if (isR2OrNewer) {
9401 __ Ins(TMP, ZERO, 31, 1);
9402 } else {
9403 __ Sll(TMP, TMP, 1);
9404 __ Srl(TMP, TMP, 1);
9405 }
9406 __ Mtc1(TMP, out);
9407 }
9408 }
9409}
9410
9411void InstructionCodeGeneratorMIPS::VisitAbs(HAbs* abs) {
9412 LocationSummary* locations = abs->GetLocations();
9413 bool isR2OrNewer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
9414 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9415 switch (abs->GetResultType()) {
9416 case DataType::Type::kInt32: {
9417 Register in = locations->InAt(0).AsRegister<Register>();
9418 Register out = locations->Out().AsRegister<Register>();
9419 __ Sra(AT, in, 31);
9420 __ Xor(out, in, AT);
9421 __ Subu(out, out, AT);
9422 break;
9423 }
9424 case DataType::Type::kInt64: {
9425 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9426 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9427 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9428 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9429 // The comments in this section show the analogous operations which would
9430 // be performed if we had 64-bit registers "in", and "out".
9431 // __ Dsra32(AT, in, 31);
9432 __ Sra(AT, in_hi, 31);
9433 // __ Xor(out, in, AT);
9434 __ Xor(TMP, in_lo, AT);
9435 __ Xor(out_hi, in_hi, AT);
9436 // __ Dsubu(out, out, AT);
9437 __ Subu(out_lo, TMP, AT);
9438 __ Sltu(TMP, out_lo, TMP);
9439 __ Addu(out_hi, out_hi, TMP);
9440 break;
9441 }
9442 case DataType::Type::kFloat32:
9443 case DataType::Type::kFloat64:
9444 GenerateAbsFP(locations, abs->GetResultType(), isR2OrNewer, isR6);
9445 break;
9446 default:
9447 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9448 }
9449}
9450
Igor Murashkind01745e2017-04-05 16:40:31 -07009451void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
9452 constructor_fence->SetLocations(nullptr);
9453}
9454
9455void InstructionCodeGeneratorMIPS::VisitConstructorFence(
9456 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
9457 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
9458}
9459
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009460void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9461 memory_barrier->SetLocations(nullptr);
9462}
9463
9464void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9465 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
9466}
9467
9468void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009469 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009470 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009471 locations->SetInAt(0, MipsReturnLocation(return_type));
9472}
9473
9474void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
9475 codegen_->GenerateFrameExit();
9476}
9477
9478void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
9479 ret->SetLocations(nullptr);
9480}
9481
9482void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
9483 codegen_->GenerateFrameExit();
9484}
9485
Alexey Frunze92d90602015-12-18 18:16:36 -08009486void LocationsBuilderMIPS::VisitRor(HRor* ror) {
9487 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009488}
9489
Alexey Frunze92d90602015-12-18 18:16:36 -08009490void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
9491 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009492}
9493
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009494void LocationsBuilderMIPS::VisitShl(HShl* shl) {
9495 HandleShift(shl);
9496}
9497
9498void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
9499 HandleShift(shl);
9500}
9501
9502void LocationsBuilderMIPS::VisitShr(HShr* shr) {
9503 HandleShift(shr);
9504}
9505
9506void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
9507 HandleShift(shr);
9508}
9509
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009510void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
9511 HandleBinaryOp(instruction);
9512}
9513
9514void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
9515 HandleBinaryOp(instruction);
9516}
9517
9518void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9519 HandleFieldGet(instruction, instruction->GetFieldInfo());
9520}
9521
9522void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9523 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
9524}
9525
9526void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
9527 HandleFieldSet(instruction, instruction->GetFieldInfo());
9528}
9529
9530void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01009531 HandleFieldSet(instruction,
9532 instruction->GetFieldInfo(),
9533 instruction->GetDexPc(),
9534 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009535}
9536
Vladimir Marko552a1342017-10-31 10:56:47 +00009537void LocationsBuilderMIPS::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
9538 codegen_->CreateStringBuilderAppendLocations(instruction, Location::RegisterLocation(V0));
9539}
9540
9541void InstructionCodeGeneratorMIPS::VisitStringBuilderAppend(HStringBuilderAppend* instruction) {
9542 __ LoadConst32(A0, instruction->GetFormat()->GetValue());
9543 codegen_->InvokeRuntime(kQuickStringBuilderAppend, instruction, instruction->GetDexPc());
9544}
9545
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009546void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
9547 HUnresolvedInstanceFieldGet* instruction) {
9548 FieldAccessCallingConventionMIPS calling_convention;
9549 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9550 instruction->GetFieldType(),
9551 calling_convention);
9552}
9553
9554void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
9555 HUnresolvedInstanceFieldGet* instruction) {
9556 FieldAccessCallingConventionMIPS calling_convention;
9557 codegen_->GenerateUnresolvedFieldAccess(instruction,
9558 instruction->GetFieldType(),
9559 instruction->GetFieldIndex(),
9560 instruction->GetDexPc(),
9561 calling_convention);
9562}
9563
9564void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
9565 HUnresolvedInstanceFieldSet* instruction) {
9566 FieldAccessCallingConventionMIPS calling_convention;
9567 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9568 instruction->GetFieldType(),
9569 calling_convention);
9570}
9571
9572void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
9573 HUnresolvedInstanceFieldSet* instruction) {
9574 FieldAccessCallingConventionMIPS calling_convention;
9575 codegen_->GenerateUnresolvedFieldAccess(instruction,
9576 instruction->GetFieldType(),
9577 instruction->GetFieldIndex(),
9578 instruction->GetDexPc(),
9579 calling_convention);
9580}
9581
9582void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
9583 HUnresolvedStaticFieldGet* instruction) {
9584 FieldAccessCallingConventionMIPS calling_convention;
9585 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9586 instruction->GetFieldType(),
9587 calling_convention);
9588}
9589
9590void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
9591 HUnresolvedStaticFieldGet* instruction) {
9592 FieldAccessCallingConventionMIPS calling_convention;
9593 codegen_->GenerateUnresolvedFieldAccess(instruction,
9594 instruction->GetFieldType(),
9595 instruction->GetFieldIndex(),
9596 instruction->GetDexPc(),
9597 calling_convention);
9598}
9599
9600void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
9601 HUnresolvedStaticFieldSet* instruction) {
9602 FieldAccessCallingConventionMIPS calling_convention;
9603 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9604 instruction->GetFieldType(),
9605 calling_convention);
9606}
9607
9608void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9609 HUnresolvedStaticFieldSet* instruction) {
9610 FieldAccessCallingConventionMIPS calling_convention;
9611 codegen_->GenerateUnresolvedFieldAccess(instruction,
9612 instruction->GetFieldType(),
9613 instruction->GetFieldIndex(),
9614 instruction->GetDexPc(),
9615 calling_convention);
9616}
9617
9618void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009619 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9620 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009621 // In suspend check slow path, usually there are no caller-save registers at all.
9622 // If SIMD instructions are present, however, we force spilling all live SIMD
9623 // registers in full width (since the runtime only saves/restores lower part).
9624 locations->SetCustomSlowPathCallerSaves(
9625 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009626}
9627
9628void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9629 HBasicBlock* block = instruction->GetBlock();
9630 if (block->GetLoopInformation() != nullptr) {
9631 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9632 // The back edge will generate the suspend check.
9633 return;
9634 }
9635 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9636 // The goto will generate the suspend check.
9637 return;
9638 }
9639 GenerateSuspendCheck(instruction, nullptr);
9640}
9641
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009642void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009643 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9644 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009645 InvokeRuntimeCallingConvention calling_convention;
9646 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9647}
9648
9649void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009650 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009651 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9652}
9653
9654void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009655 DataType::Type input_type = conversion->GetInputType();
9656 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009657 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9658 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009659 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009661 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9662 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009663 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9664 }
9665
9666 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009667 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009668 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9669 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009670 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009671 }
9672
Vladimir Markoca6fff82017-10-03 14:49:14 +01009673 LocationSummary* locations =
9674 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009675
9676 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009677 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009678 locations->SetInAt(0, Location::RequiresFpuRegister());
9679 } else {
9680 locations->SetInAt(0, Location::RequiresRegister());
9681 }
9682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009683 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009684 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9685 } else {
9686 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9687 }
9688 } else {
9689 InvokeRuntimeCallingConvention calling_convention;
9690
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009691 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009692 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9693 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009694 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009695 locations->SetInAt(0, Location::RegisterPairLocation(
9696 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9697 }
9698
9699 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9700 }
9701}
9702
9703void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9704 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009705 DataType::Type result_type = conversion->GetResultType();
9706 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009707 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009708 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009709
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009710 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9711 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009712
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009713 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009714 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9715 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9716 Register src = locations->InAt(0).AsRegister<Register>();
9717
Alexey Frunzea871ef12016-06-27 15:20:11 -07009718 if (dst_low != src) {
9719 __ Move(dst_low, src);
9720 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009721 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009722 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009723 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009724 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009725 ? locations->InAt(0).AsRegisterPairLow<Register>()
9726 : locations->InAt(0).AsRegister<Register>();
9727
9728 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009729 case DataType::Type::kUint8:
9730 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009731 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009732 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009733 if (has_sign_extension) {
9734 __ Seb(dst, src);
9735 } else {
9736 __ Sll(dst, src, 24);
9737 __ Sra(dst, dst, 24);
9738 }
9739 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009740 case DataType::Type::kUint16:
9741 __ Andi(dst, src, 0xFFFF);
9742 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009743 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009744 if (has_sign_extension) {
9745 __ Seh(dst, src);
9746 } else {
9747 __ Sll(dst, src, 16);
9748 __ Sra(dst, dst, 16);
9749 }
9750 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009751 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009752 if (dst != src) {
9753 __ Move(dst, src);
9754 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009755 break;
9756
9757 default:
9758 LOG(FATAL) << "Unexpected type conversion from " << input_type
9759 << " to " << result_type;
9760 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009761 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9762 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009763 if (isR6) {
9764 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9765 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9766 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9767 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9768 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9769 __ Mtc1(src_low, FTMP);
9770 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009771 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009772 __ Cvtsl(dst, FTMP);
9773 } else {
9774 __ Cvtdl(dst, FTMP);
9775 }
9776 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009777 QuickEntrypointEnum entrypoint =
9778 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009779 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009780 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009781 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9782 } else {
9783 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9784 }
9785 }
9786 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009787 Register src = locations->InAt(0).AsRegister<Register>();
9788 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9789 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009790 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009791 __ Cvtsw(dst, FTMP);
9792 } else {
9793 __ Cvtdw(dst, FTMP);
9794 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009795 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009796 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9797 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009798
9799 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9800 // value of the output type if the input is outside of the range after the truncation or
9801 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9802 // results. This matches the desired float/double-to-int/long conversion exactly.
9803 //
9804 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9805 // value when the input is either a NaN or is outside of the range of the output type
9806 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9807 // the same result.
9808 //
9809 // The code takes care of the different behaviors by first comparing the input to the
9810 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9811 // If the input is greater than or equal to the minimum, it procedes to the truncate
9812 // instruction, which will handle such an input the same way irrespective of NAN2008.
9813 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9814 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009815 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009816 if (isR6) {
9817 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9818 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9819 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9820 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9821 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009822
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009823 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009824 __ TruncLS(FTMP, src);
9825 } else {
9826 __ TruncLD(FTMP, src);
9827 }
9828 __ Mfc1(dst_low, FTMP);
9829 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009830 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009831 QuickEntrypointEnum entrypoint =
9832 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009833 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009834 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009835 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9836 } else {
9837 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9838 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009839 }
9840 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009841 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9842 Register dst = locations->Out().AsRegister<Register>();
9843 MipsLabel truncate;
9844 MipsLabel done;
9845
Lena Djokicf4e23a82017-05-09 15:43:45 +02009846 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009847 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009848 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9849 __ LoadConst32(TMP, min_val);
9850 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009851 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009852 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9853 __ LoadConst32(TMP, High32Bits(min_val));
9854 __ Mtc1(ZERO, FTMP);
9855 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009856 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009857
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009858 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009859 __ ColeS(0, FTMP, src);
9860 } else {
9861 __ ColeD(0, FTMP, src);
9862 }
9863 __ Bc1t(0, &truncate);
9864
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009865 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009866 __ CeqS(0, src, src);
9867 } else {
9868 __ CeqD(0, src, src);
9869 }
9870 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9871 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009872
9873 __ B(&done);
9874
9875 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009876 }
9877
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009878 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009879 __ TruncWS(FTMP, src);
9880 } else {
9881 __ TruncWD(FTMP, src);
9882 }
9883 __ Mfc1(dst, FTMP);
9884
Lena Djokicf4e23a82017-05-09 15:43:45 +02009885 if (!isR6) {
9886 __ Bind(&done);
9887 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009888 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009889 } else if (DataType::IsFloatingPointType(result_type) &&
9890 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009891 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9892 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009893 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009894 __ Cvtsd(dst, src);
9895 } else {
9896 __ Cvtds(dst, src);
9897 }
9898 } else {
9899 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9900 << " to " << result_type;
9901 }
9902}
9903
9904void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9905 HandleShift(ushr);
9906}
9907
9908void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9909 HandleShift(ushr);
9910}
9911
9912void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9913 HandleBinaryOp(instruction);
9914}
9915
9916void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9917 HandleBinaryOp(instruction);
9918}
9919
9920void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9921 // Nothing to do, this should be removed during prepare for register allocator.
9922 LOG(FATAL) << "Unreachable";
9923}
9924
9925void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9926 // Nothing to do, this should be removed during prepare for register allocator.
9927 LOG(FATAL) << "Unreachable";
9928}
9929
9930void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009931 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009932}
9933
9934void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009935 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009936}
9937
9938void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009939 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009940}
9941
9942void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009943 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009944}
9945
9946void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009947 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009948}
9949
9950void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009951 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009952}
9953
9954void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009955 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009956}
9957
9958void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009959 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009960}
9961
9962void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009963 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009964}
9965
9966void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009967 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009968}
9969
9970void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009971 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009972}
9973
9974void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009975 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009976}
9977
9978void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009979 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009980}
9981
9982void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009983 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009984}
9985
9986void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009987 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009988}
9989
9990void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009991 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009992}
9993
9994void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009995 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009996}
9997
9998void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009999 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010000}
10001
10002void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +000010003 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010004}
10005
10006void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +000010007 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010008}
10009
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010010void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
10011 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010012 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010013 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010014 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
10015 uint32_t num_entries = switch_instr->GetNumEntries();
10016 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
10017 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
10018 // instruction to simulate PC-relative addressing when accessing the jump table.
10019 // NAL clobbers RA. Make sure RA is preserved.
10020 codegen_->ClobberRA();
10021 }
10022 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010023}
10024
Alexey Frunze96b66822016-09-10 02:32:44 -070010025void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
10026 int32_t lower_bound,
10027 uint32_t num_entries,
10028 HBasicBlock* switch_block,
10029 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010030 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010031 Register temp_reg = TMP;
10032 __ Addiu32(temp_reg, value_reg, -lower_bound);
10033 // Jump to default if index is negative
10034 // Note: We don't check the case that index is positive while value < lower_bound, because in
10035 // this case, index >= num_entries must be true. So that we can save one branch instruction.
10036 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
10037
Alexey Frunze96b66822016-09-10 02:32:44 -070010038 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010039 // Jump to successors[0] if value == lower_bound.
10040 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
10041 int32_t last_index = 0;
10042 for (; num_entries - last_index > 2; last_index += 2) {
10043 __ Addiu(temp_reg, temp_reg, -2);
10044 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
10045 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
10046 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
10047 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
10048 }
10049 if (num_entries - last_index == 2) {
10050 // The last missing case_value.
10051 __ Addiu(temp_reg, temp_reg, -1);
10052 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010053 }
10054
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010055 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -070010056 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010057 __ B(codegen_->GetLabelOf(default_block));
10058 }
10059}
10060
Alexey Frunze96b66822016-09-10 02:32:44 -070010061void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
10062 Register constant_area,
10063 int32_t lower_bound,
10064 uint32_t num_entries,
10065 HBasicBlock* switch_block,
10066 HBasicBlock* default_block) {
10067 // Create a jump table.
10068 std::vector<MipsLabel*> labels(num_entries);
10069 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
10070 for (uint32_t i = 0; i < num_entries; i++) {
10071 labels[i] = codegen_->GetLabelOf(successors[i]);
10072 }
10073 JumpTable* table = __ CreateJumpTable(std::move(labels));
10074
10075 // Is the value in range?
10076 __ Addiu32(TMP, value_reg, -lower_bound);
10077 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
10078 __ Sltiu(AT, TMP, num_entries);
10079 __ Beqz(AT, codegen_->GetLabelOf(default_block));
10080 } else {
10081 __ LoadConst32(AT, num_entries);
10082 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
10083 }
10084
10085 // We are in the range of the table.
10086 // Load the target address from the jump table, indexing by the value.
10087 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -070010088 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -070010089 __ Lw(TMP, TMP, 0);
10090 // Compute the absolute target address by adding the table start address
10091 // (the table contains offsets to targets relative to its start).
10092 __ Addu(TMP, TMP, AT);
10093 // And jump.
10094 __ Jr(TMP);
10095 __ NopIfNoReordering();
10096}
10097
10098void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
10099 int32_t lower_bound = switch_instr->GetStartValue();
10100 uint32_t num_entries = switch_instr->GetNumEntries();
10101 LocationSummary* locations = switch_instr->GetLocations();
10102 Register value_reg = locations->InAt(0).AsRegister<Register>();
10103 HBasicBlock* switch_block = switch_instr->GetBlock();
10104 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10105
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010106 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -070010107 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010108 //
10109 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
10110 // to access the jump table and it is implemented by changing HPackedSwitch to
10111 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
10112 // VisitMipsPackedSwitch()).
10113 //
10114 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
10115 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
10116 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -070010117 GenTableBasedPackedSwitch(value_reg,
10118 ZERO,
10119 lower_bound,
10120 num_entries,
10121 switch_block,
10122 default_block);
10123 } else {
10124 GenPackedSwitchWithCompares(value_reg,
10125 lower_bound,
10126 num_entries,
10127 switch_block,
10128 default_block);
10129 }
10130}
10131
10132void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10133 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010134 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -070010135 locations->SetInAt(0, Location::RequiresRegister());
10136 // Constant area pointer (HMipsComputeBaseMethodAddress).
10137 locations->SetInAt(1, Location::RequiresRegister());
10138}
10139
10140void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10141 int32_t lower_bound = switch_instr->GetStartValue();
10142 uint32_t num_entries = switch_instr->GetNumEntries();
10143 LocationSummary* locations = switch_instr->GetLocations();
10144 Register value_reg = locations->InAt(0).AsRegister<Register>();
10145 Register constant_area = locations->InAt(1).AsRegister<Register>();
10146 HBasicBlock* switch_block = switch_instr->GetBlock();
10147 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10148
10149 // This is an R2-only path. HPackedSwitch has been changed to
10150 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
10151 // required to address the jump table relative to PC.
10152 GenTableBasedPackedSwitch(value_reg,
10153 constant_area,
10154 lower_bound,
10155 num_entries,
10156 switch_block,
10157 default_block);
10158}
10159
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010160void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
10161 HMipsComputeBaseMethodAddress* insn) {
10162 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010163 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010164 locations->SetOut(Location::RequiresRegister());
10165}
10166
10167void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
10168 HMipsComputeBaseMethodAddress* insn) {
10169 LocationSummary* locations = insn->GetLocations();
10170 Register reg = locations->Out().AsRegister<Register>();
10171
10172 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
10173
10174 // Generate a dummy PC-relative call to obtain PC.
10175 __ Nal();
10176 // Grab the return address off RA.
10177 __ Move(reg, RA);
10178
10179 // Remember this offset (the obtained PC value) for later use with constant area.
10180 __ BindPcRelBaseLabel();
10181}
10182
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010183void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10184 // The trampoline uses the same calling convention as dex calling conventions,
10185 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
10186 // the method_idx.
10187 HandleInvoke(invoke);
10188}
10189
10190void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10191 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
10192}
10193
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010194void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10195 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010196 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010197 locations->SetInAt(0, Location::RequiresRegister());
10198 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010199}
10200
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010201void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10202 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +000010203 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010204 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010205 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010206 __ LoadFromOffset(kLoadWord,
10207 locations->Out().AsRegister<Register>(),
10208 locations->InAt(0).AsRegister<Register>(),
10209 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010210 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010211 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +000010212 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000010213 __ LoadFromOffset(kLoadWord,
10214 locations->Out().AsRegister<Register>(),
10215 locations->InAt(0).AsRegister<Register>(),
10216 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010217 __ LoadFromOffset(kLoadWord,
10218 locations->Out().AsRegister<Register>(),
10219 locations->Out().AsRegister<Register>(),
10220 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010221 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010222}
10223
xueliang.zhonge0eb4832017-10-30 13:43:14 +000010224void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10225 ATTRIBUTE_UNUSED) {
10226 LOG(FATAL) << "Unreachable";
10227}
10228
10229void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10230 ATTRIBUTE_UNUSED) {
10231 LOG(FATAL) << "Unreachable";
10232}
10233
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010234#undef __
10235#undef QUICK_ENTRY_POINT
10236
10237} // namespace mips
10238} // namespace art