blob: 8c38824d12f9cb28c2542a89d5ac27dc9a54173e [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Vladimir Markoeebb8212018-06-05 14:57:24 +010029#include "gc/space/image_space.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070030#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020031#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070032#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010033#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020034#include "mirror/array-inl.h"
35#include "mirror/class-inl.h"
36#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010037#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020038#include "thread.h"
39#include "utils/assembler.h"
40#include "utils/mips/assembler_mips.h"
41#include "utils/stack_checks.h"
42
43namespace art {
44namespace mips {
45
46static constexpr int kCurrentMethodStackOffset = 0;
47static constexpr Register kMethodRegisterArgument = A0;
48
Alexey Frunze4147fcc2017-06-17 19:57:27 -070049// Flags controlling the use of thunks for Baker read barriers.
50constexpr bool kBakerReadBarrierThunksEnableForFields = true;
51constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
52constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
53
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010054Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020055 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010056 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010057 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010058 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010059 case DataType::Type::kInt8:
60 case DataType::Type::kUint16:
61 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -080062 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010063 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020064 return Location::RegisterLocation(V0);
65
Aart Bik66c158e2018-01-31 12:55:04 -080066 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020068 return Location::RegisterPairLocation(V0, V1);
69
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010070 case DataType::Type::kFloat32:
71 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020072 return Location::FpuRegisterLocation(F0);
73
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010074 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020075 return Location();
76 }
77 UNREACHABLE();
78}
79
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010080Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020081 return MipsReturnLocation(type);
82}
83
84Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
85 return Location::RegisterLocation(kMethodRegisterArgument);
86}
87
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010088Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020089 Location next_location;
90
91 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010092 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010093 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010094 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010095 case DataType::Type::kInt8:
96 case DataType::Type::kUint16:
97 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010098 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020099 uint32_t gp_index = gp_index_++;
100 if (gp_index < calling_convention.GetNumberOfRegisters()) {
101 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
102 } else {
103 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
104 next_location = Location::StackSlot(stack_offset);
105 }
106 break;
107 }
108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100109 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200110 uint32_t gp_index = gp_index_;
111 gp_index_ += 2;
112 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800113 Register reg = calling_convention.GetRegisterAt(gp_index);
114 if (reg == A1 || reg == A3) {
115 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200116 gp_index++;
117 }
118 Register low_even = calling_convention.GetRegisterAt(gp_index);
119 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
120 DCHECK_EQ(low_even + 1, high_odd);
121 next_location = Location::RegisterPairLocation(low_even, high_odd);
122 } else {
123 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
124 next_location = Location::DoubleStackSlot(stack_offset);
125 }
126 break;
127 }
128
129 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
130 // will take up the even/odd pair, while floats are stored in even regs only.
131 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100132 case DataType::Type::kFloat32:
133 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200134 uint32_t float_index = float_index_++;
135 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
136 next_location = Location::FpuRegisterLocation(
137 calling_convention.GetFpuRegisterAt(float_index));
138 } else {
139 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100140 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
141 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200142 }
143 break;
144 }
145
Aart Bik66c158e2018-01-31 12:55:04 -0800146 case DataType::Type::kUint32:
147 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200149 LOG(FATAL) << "Unexpected parameter type " << type;
150 break;
151 }
152
153 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100154 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200155
156 return next_location;
157}
158
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100159Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200160 return MipsReturnLocation(type);
161}
162
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100163// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
164#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700165#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200166
167class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
168 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000169 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200170
171 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
172 LocationSummary* locations = instruction_->GetLocations();
173 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
174 __ Bind(GetEntryLabel());
175 if (instruction_->CanThrowIntoCatchBlock()) {
176 // Live registers will be restored in the catch block if caught.
177 SaveLiveRegisters(codegen, instruction_->GetLocations());
178 }
179 // We're moving two locations to locations that could overlap, so we need a parallel
180 // move resolver.
181 InvokeRuntimeCallingConvention calling_convention;
182 codegen->EmitParallelMoves(locations->InAt(0),
183 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100184 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200185 locations->InAt(1),
186 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100187 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100188 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
189 ? kQuickThrowStringBounds
190 : kQuickThrowArrayBounds;
191 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100192 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
194 }
195
196 bool IsFatal() const OVERRIDE { return true; }
197
198 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
199
200 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200201 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
202};
203
204class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
205 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000206 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200207
208 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
209 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
210 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100211 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200212 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
213 }
214
215 bool IsFatal() const OVERRIDE { return true; }
216
217 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
218
219 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200220 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
221};
222
223class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
224 public:
225 LoadClassSlowPathMIPS(HLoadClass* cls,
226 HInstruction* at,
227 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000228 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700229 : SlowPathCodeMIPS(at),
230 cls_(cls),
231 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000232 do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200233 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
234 }
235
236 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000237 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700238 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200239 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700240 InvokeRuntimeCallingConvention calling_convention;
241 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200242 __ Bind(GetEntryLabel());
243 SaveLiveRegisters(codegen, locations);
244
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000245 dex::TypeIndex type_index = cls_->GetTypeIndex();
246 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100247 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
248 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000249 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200250 if (do_clinit_) {
251 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
252 } else {
253 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
254 }
255
256 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200257 if (out.IsValid()) {
258 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100259 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700260 mips_codegen->MoveLocation(out,
261 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
262 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200263 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200264 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700265
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200266 __ B(GetExitLabel());
267 }
268
269 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
270
271 private:
272 // The class this slow path will load.
273 HLoadClass* const cls_;
274
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200275 // The dex PC of `at_`.
276 const uint32_t dex_pc_;
277
278 // Whether to initialize the class.
279 const bool do_clinit_;
280
281 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
282};
283
284class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
285 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000286 explicit LoadStringSlowPathMIPS(HLoadString* instruction)
287 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200288
289 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700290 DCHECK(instruction_->IsLoadString());
291 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200292 LocationSummary* locations = instruction_->GetLocations();
293 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000294 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200295 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700296 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200297 __ Bind(GetEntryLabel());
298 SaveLiveRegisters(codegen, locations);
299
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000300 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100301 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200302 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700303
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100304 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200305 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700306 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200308 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000309
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200310 __ B(GetExitLabel());
311 }
312
313 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
314
315 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
317};
318
319class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
320 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000321 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200322
323 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
324 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
325 __ Bind(GetEntryLabel());
326 if (instruction_->CanThrowIntoCatchBlock()) {
327 // Live registers will be restored in the catch block if caught.
328 SaveLiveRegisters(codegen, instruction_->GetLocations());
329 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100330 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200331 instruction_,
332 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100333 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200334 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
335 }
336
337 bool IsFatal() const OVERRIDE { return true; }
338
339 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
340
341 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200342 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
343};
344
345class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
346 public:
347 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000348 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200349
350 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200351 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200352 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
353 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200354 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100355 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200356 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200357 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200358 if (successor_ == nullptr) {
359 __ B(GetReturnLabel());
360 } else {
361 __ B(mips_codegen->GetLabelOf(successor_));
362 }
363 }
364
365 MipsLabel* GetReturnLabel() {
366 DCHECK(successor_ == nullptr);
367 return &return_label_;
368 }
369
370 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
371
Chris Larsena2045912017-11-02 12:39:54 -0700372 HBasicBlock* GetSuccessor() const {
373 return successor_;
374 }
375
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200376 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200377 // If not null, the block to branch to after the suspend check.
378 HBasicBlock* const successor_;
379
380 // If `successor_` is null, the label to branch to after the suspend check.
381 MipsLabel return_label_;
382
383 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
384};
385
386class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
387 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800388 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
389 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200390
391 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
392 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200393 uint32_t dex_pc = instruction_->GetDexPc();
394 DCHECK(instruction_->IsCheckCast()
395 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
396 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
397
398 __ Bind(GetEntryLabel());
Alexey Frunzedfc30af2018-01-24 16:25:10 -0800399 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800400 SaveLiveRegisters(codegen, locations);
401 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200402
403 // We're moving two locations to locations that could overlap, so we need a parallel
404 // move resolver.
405 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800406 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200407 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100408 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800409 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100411 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200412 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100413 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800414 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100415 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200416 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
417 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200418 } else {
419 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800420 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
421 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200422 }
423
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800424 if (!is_fatal_) {
425 RestoreLiveRegisters(codegen, locations);
426 __ B(GetExitLabel());
427 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200428 }
429
430 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
431
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800432 bool IsFatal() const OVERRIDE { return is_fatal_; }
433
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800435 const bool is_fatal_;
436
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200437 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
438};
439
440class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
441 public:
Aart Bik42249c32016-01-07 15:33:50 -0800442 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000443 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200444
445 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800446 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200447 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100448 LocationSummary* locations = instruction_->GetLocations();
449 SaveLiveRegisters(codegen, locations);
450 InvokeRuntimeCallingConvention calling_convention;
451 __ LoadConst32(calling_convention.GetRegisterAt(0),
452 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100453 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100454 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200455 }
456
457 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
458
459 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200460 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
461};
462
Alexey Frunze15958152017-02-09 19:08:30 -0800463class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
464 public:
465 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
466
467 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
468 LocationSummary* locations = instruction_->GetLocations();
469 __ Bind(GetEntryLabel());
470 SaveLiveRegisters(codegen, locations);
471
472 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100473 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800474 parallel_move.AddMove(
475 locations->InAt(0),
476 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100477 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800478 nullptr);
479 parallel_move.AddMove(
480 locations->InAt(1),
481 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100482 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800483 nullptr);
484 parallel_move.AddMove(
485 locations->InAt(2),
486 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100487 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800488 nullptr);
489 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
490
491 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
492 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
493 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
494 RestoreLiveRegisters(codegen, locations);
495 __ B(GetExitLabel());
496 }
497
498 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
499
500 private:
501 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
502};
503
504// Slow path marking an object reference `ref` during a read
505// barrier. The field `obj.field` in the object `obj` holding this
506// reference does not get updated by this slow path after marking (see
507// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
508//
509// This means that after the execution of this slow path, `ref` will
510// always be up-to-date, but `obj.field` may not; i.e., after the
511// flip, `ref` will be a to-space reference, but `obj.field` will
512// probably still be a from-space reference (unless it gets updated by
513// another thread, or if another thread installed another object
514// reference (different from `ref`) in `obj.field`).
515//
516// If `entrypoint` is a valid location it is assumed to already be
517// holding the entrypoint. The case where the entrypoint is passed in
518// is for the GcRoot read barrier.
519class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
520 public:
521 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
522 Location ref,
523 Location entrypoint = Location::NoLocation())
524 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
525 DCHECK(kEmitCompilerReadBarrier);
526 }
527
528 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
529
530 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
531 LocationSummary* locations = instruction_->GetLocations();
532 Register ref_reg = ref_.AsRegister<Register>();
533 DCHECK(locations->CanCall());
534 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
535 DCHECK(instruction_->IsInstanceFieldGet() ||
536 instruction_->IsStaticFieldGet() ||
537 instruction_->IsArrayGet() ||
538 instruction_->IsArraySet() ||
539 instruction_->IsLoadClass() ||
540 instruction_->IsLoadString() ||
541 instruction_->IsInstanceOf() ||
542 instruction_->IsCheckCast() ||
543 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
544 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
545 << "Unexpected instruction in read barrier marking slow path: "
546 << instruction_->DebugName();
547
548 __ Bind(GetEntryLabel());
549 // No need to save live registers; it's taken care of by the
550 // entrypoint. Also, there is no need to update the stack mask,
551 // as this runtime call will not trigger a garbage collection.
552 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
553 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
554 (S2 <= ref_reg && ref_reg <= S7) ||
555 (ref_reg == FP)) << ref_reg;
556 // "Compact" slow path, saving two moves.
557 //
558 // Instead of using the standard runtime calling convention (input
559 // and output in A0 and V0 respectively):
560 //
561 // A0 <- ref
562 // V0 <- ReadBarrierMark(A0)
563 // ref <- V0
564 //
565 // we just use rX (the register containing `ref`) as input and output
566 // of a dedicated entrypoint:
567 //
568 // rX <- ReadBarrierMarkRegX(rX)
569 //
570 if (entrypoint_.IsValid()) {
571 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
572 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
573 __ Jalr(entrypoint_.AsRegister<Register>());
574 __ NopIfNoReordering();
575 } else {
576 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100577 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800578 // This runtime call does not require a stack map.
579 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
580 instruction_,
581 this,
582 /* direct */ false);
583 }
584 __ B(GetExitLabel());
585 }
586
587 private:
588 // The location (register) of the marked object reference.
589 const Location ref_;
590
591 // The location of the entrypoint if already loaded.
592 const Location entrypoint_;
593
594 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
595};
596
597// Slow path marking an object reference `ref` during a read barrier,
598// and if needed, atomically updating the field `obj.field` in the
599// object `obj` holding this reference after marking (contrary to
600// ReadBarrierMarkSlowPathMIPS above, which never tries to update
601// `obj.field`).
602//
603// This means that after the execution of this slow path, both `ref`
604// and `obj.field` will be up-to-date; i.e., after the flip, both will
605// hold the same to-space reference (unless another thread installed
606// another object reference (different from `ref`) in `obj.field`).
607class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
608 public:
609 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
610 Location ref,
611 Register obj,
612 Location field_offset,
613 Register temp1)
614 : SlowPathCodeMIPS(instruction),
615 ref_(ref),
616 obj_(obj),
617 field_offset_(field_offset),
618 temp1_(temp1) {
619 DCHECK(kEmitCompilerReadBarrier);
620 }
621
622 const char* GetDescription() const OVERRIDE {
623 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
624 }
625
626 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
627 LocationSummary* locations = instruction_->GetLocations();
628 Register ref_reg = ref_.AsRegister<Register>();
629 DCHECK(locations->CanCall());
630 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
631 // This slow path is only used by the UnsafeCASObject intrinsic.
632 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
633 << "Unexpected instruction in read barrier marking and field updating slow path: "
634 << instruction_->DebugName();
635 DCHECK(instruction_->GetLocations()->Intrinsified());
636 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
637 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
638
639 __ Bind(GetEntryLabel());
640
641 // Save the old reference.
642 // Note that we cannot use AT or TMP to save the old reference, as those
643 // are used by the code that follows, but we need the old reference after
644 // the call to the ReadBarrierMarkRegX entry point.
645 DCHECK_NE(temp1_, AT);
646 DCHECK_NE(temp1_, TMP);
647 __ Move(temp1_, ref_reg);
648
649 // No need to save live registers; it's taken care of by the
650 // entrypoint. Also, there is no need to update the stack mask,
651 // as this runtime call will not trigger a garbage collection.
652 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
653 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
654 (S2 <= ref_reg && ref_reg <= S7) ||
655 (ref_reg == FP)) << ref_reg;
656 // "Compact" slow path, saving two moves.
657 //
658 // Instead of using the standard runtime calling convention (input
659 // and output in A0 and V0 respectively):
660 //
661 // A0 <- ref
662 // V0 <- ReadBarrierMark(A0)
663 // ref <- V0
664 //
665 // we just use rX (the register containing `ref`) as input and output
666 // of a dedicated entrypoint:
667 //
668 // rX <- ReadBarrierMarkRegX(rX)
669 //
670 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100671 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800672 // This runtime call does not require a stack map.
673 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
674 instruction_,
675 this,
676 /* direct */ false);
677
678 // If the new reference is different from the old reference,
679 // update the field in the holder (`*(obj_ + field_offset_)`).
680 //
681 // Note that this field could also hold a different object, if
682 // another thread had concurrently changed it. In that case, the
683 // the compare-and-set (CAS) loop below would abort, leaving the
684 // field as-is.
685 MipsLabel done;
686 __ Beq(temp1_, ref_reg, &done);
687
688 // Update the the holder's field atomically. This may fail if
689 // mutator updates before us, but it's OK. This is achieved
690 // using a strong compare-and-set (CAS) operation with relaxed
691 // memory synchronization ordering, where the expected value is
692 // the old reference and the desired value is the new reference.
693
694 // Convenience aliases.
695 Register base = obj_;
696 // The UnsafeCASObject intrinsic uses a register pair as field
697 // offset ("long offset"), of which only the low part contains
698 // data.
699 Register offset = field_offset_.AsRegisterPairLow<Register>();
700 Register expected = temp1_;
701 Register value = ref_reg;
702 Register tmp_ptr = TMP; // Pointer to actual memory.
703 Register tmp = AT; // Value in memory.
704
705 __ Addu(tmp_ptr, base, offset);
706
707 if (kPoisonHeapReferences) {
708 __ PoisonHeapReference(expected);
709 // Do not poison `value` if it is the same register as
710 // `expected`, which has just been poisoned.
711 if (value != expected) {
712 __ PoisonHeapReference(value);
713 }
714 }
715
716 // do {
717 // tmp = [r_ptr] - expected;
718 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
719
720 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
721 MipsLabel loop_head, exit_loop;
722 __ Bind(&loop_head);
723 if (is_r6) {
724 __ LlR6(tmp, tmp_ptr);
725 } else {
726 __ LlR2(tmp, tmp_ptr);
727 }
728 __ Bne(tmp, expected, &exit_loop);
729 __ Move(tmp, value);
730 if (is_r6) {
731 __ ScR6(tmp, tmp_ptr);
732 } else {
733 __ ScR2(tmp, tmp_ptr);
734 }
735 __ Beqz(tmp, &loop_head);
736 __ Bind(&exit_loop);
737
738 if (kPoisonHeapReferences) {
739 __ UnpoisonHeapReference(expected);
740 // Do not unpoison `value` if it is the same register as
741 // `expected`, which has just been unpoisoned.
742 if (value != expected) {
743 __ UnpoisonHeapReference(value);
744 }
745 }
746
747 __ Bind(&done);
748 __ B(GetExitLabel());
749 }
750
751 private:
752 // The location (register) of the marked object reference.
753 const Location ref_;
754 // The register containing the object holding the marked object reference field.
755 const Register obj_;
756 // The location of the offset of the marked reference field within `obj_`.
757 Location field_offset_;
758
759 const Register temp1_;
760
761 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
762};
763
764// Slow path generating a read barrier for a heap reference.
765class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
766 public:
767 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
768 Location out,
769 Location ref,
770 Location obj,
771 uint32_t offset,
772 Location index)
773 : SlowPathCodeMIPS(instruction),
774 out_(out),
775 ref_(ref),
776 obj_(obj),
777 offset_(offset),
778 index_(index) {
779 DCHECK(kEmitCompilerReadBarrier);
780 // If `obj` is equal to `out` or `ref`, it means the initial object
781 // has been overwritten by (or after) the heap object reference load
782 // to be instrumented, e.g.:
783 //
784 // __ LoadFromOffset(kLoadWord, out, out, offset);
785 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
786 //
787 // In that case, we have lost the information about the original
788 // object, and the emitted read barrier cannot work properly.
789 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
790 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
791 }
792
793 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
794 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
795 LocationSummary* locations = instruction_->GetLocations();
796 Register reg_out = out_.AsRegister<Register>();
797 DCHECK(locations->CanCall());
798 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
799 DCHECK(instruction_->IsInstanceFieldGet() ||
800 instruction_->IsStaticFieldGet() ||
801 instruction_->IsArrayGet() ||
802 instruction_->IsInstanceOf() ||
803 instruction_->IsCheckCast() ||
804 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
805 << "Unexpected instruction in read barrier for heap reference slow path: "
806 << instruction_->DebugName();
807
808 __ Bind(GetEntryLabel());
809 SaveLiveRegisters(codegen, locations);
810
811 // We may have to change the index's value, but as `index_` is a
812 // constant member (like other "inputs" of this slow path),
813 // introduce a copy of it, `index`.
814 Location index = index_;
815 if (index_.IsValid()) {
816 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
817 if (instruction_->IsArrayGet()) {
818 // Compute the actual memory offset and store it in `index`.
819 Register index_reg = index_.AsRegister<Register>();
820 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
821 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
822 // We are about to change the value of `index_reg` (see the
823 // calls to art::mips::MipsAssembler::Sll and
824 // art::mips::MipsAssembler::Addiu32 below), but it has
825 // not been saved by the previous call to
826 // art::SlowPathCode::SaveLiveRegisters, as it is a
827 // callee-save register --
828 // art::SlowPathCode::SaveLiveRegisters does not consider
829 // callee-save registers, as it has been designed with the
830 // assumption that callee-save registers are supposed to be
831 // handled by the called function. So, as a callee-save
832 // register, `index_reg` _would_ eventually be saved onto
833 // the stack, but it would be too late: we would have
834 // changed its value earlier. Therefore, we manually save
835 // it here into another freely available register,
836 // `free_reg`, chosen of course among the caller-save
837 // registers (as a callee-save `free_reg` register would
838 // exhibit the same problem).
839 //
840 // Note we could have requested a temporary register from
841 // the register allocator instead; but we prefer not to, as
842 // this is a slow path, and we know we can find a
843 // caller-save register that is available.
844 Register free_reg = FindAvailableCallerSaveRegister(codegen);
845 __ Move(free_reg, index_reg);
846 index_reg = free_reg;
847 index = Location::RegisterLocation(index_reg);
848 } else {
849 // The initial register stored in `index_` has already been
850 // saved in the call to art::SlowPathCode::SaveLiveRegisters
851 // (as it is not a callee-save register), so we can freely
852 // use it.
853 }
854 // Shifting the index value contained in `index_reg` by the scale
855 // factor (2) cannot overflow in practice, as the runtime is
856 // unable to allocate object arrays with a size larger than
857 // 2^26 - 1 (that is, 2^28 - 4 bytes).
858 __ Sll(index_reg, index_reg, TIMES_4);
859 static_assert(
860 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
861 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
862 __ Addiu32(index_reg, index_reg, offset_);
863 } else {
864 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
865 // intrinsics, `index_` is not shifted by a scale factor of 2
866 // (as in the case of ArrayGet), as it is actually an offset
867 // to an object field within an object.
868 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
869 DCHECK(instruction_->GetLocations()->Intrinsified());
870 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
871 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
872 << instruction_->AsInvoke()->GetIntrinsic();
873 DCHECK_EQ(offset_, 0U);
874 DCHECK(index_.IsRegisterPair());
875 // UnsafeGet's offset location is a register pair, the low
876 // part contains the correct offset.
877 index = index_.ToLow();
878 }
879 }
880
881 // We're moving two or three locations to locations that could
882 // overlap, so we need a parallel move resolver.
883 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100884 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800885 parallel_move.AddMove(ref_,
886 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100887 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800888 nullptr);
889 parallel_move.AddMove(obj_,
890 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100891 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800892 nullptr);
893 if (index.IsValid()) {
894 parallel_move.AddMove(index,
895 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100896 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800897 nullptr);
898 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
899 } else {
900 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
901 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
902 }
903 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
904 instruction_,
905 instruction_->GetDexPc(),
906 this);
907 CheckEntrypointTypes<
908 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200909 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100910 calling_convention.GetReturnLocation(DataType::Type::kReference),
911 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800912
913 RestoreLiveRegisters(codegen, locations);
914 __ B(GetExitLabel());
915 }
916
917 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
918
919 private:
920 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
921 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
922 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
923 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
924 if (i != ref &&
925 i != obj &&
926 !codegen->IsCoreCalleeSaveRegister(i) &&
927 !codegen->IsBlockedCoreRegister(i)) {
928 return static_cast<Register>(i);
929 }
930 }
931 // We shall never fail to find a free caller-save register, as
932 // there are more than two core caller-save registers on MIPS
933 // (meaning it is possible to find one which is different from
934 // `ref` and `obj`).
935 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
936 LOG(FATAL) << "Could not find a free caller-save register";
937 UNREACHABLE();
938 }
939
940 const Location out_;
941 const Location ref_;
942 const Location obj_;
943 const uint32_t offset_;
944 // An additional location containing an index to an array.
945 // Only used for HArrayGet and the UnsafeGetObject &
946 // UnsafeGetObjectVolatile intrinsics.
947 const Location index_;
948
949 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
950};
951
952// Slow path generating a read barrier for a GC root.
953class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
954 public:
955 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
956 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
957 DCHECK(kEmitCompilerReadBarrier);
958 }
959
960 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
961 LocationSummary* locations = instruction_->GetLocations();
962 Register reg_out = out_.AsRegister<Register>();
963 DCHECK(locations->CanCall());
964 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
965 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
966 << "Unexpected instruction in read barrier for GC root slow path: "
967 << instruction_->DebugName();
968
969 __ Bind(GetEntryLabel());
970 SaveLiveRegisters(codegen, locations);
971
972 InvokeRuntimeCallingConvention calling_convention;
973 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +0200974 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
975 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100976 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800977 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
978 instruction_,
979 instruction_->GetDexPc(),
980 this);
981 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +0200982 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100983 calling_convention.GetReturnLocation(DataType::Type::kReference),
984 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800985
986 RestoreLiveRegisters(codegen, locations);
987 __ B(GetExitLabel());
988 }
989
990 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
991
992 private:
993 const Location out_;
994 const Location root_;
995
996 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
997};
998
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200999CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001000 const CompilerOptions& compiler_options,
1001 OptimizingCompilerStats* stats)
1002 : CodeGenerator(graph,
1003 kNumberOfCoreRegisters,
1004 kNumberOfFRegisters,
1005 kNumberOfRegisterPairs,
1006 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1007 arraysize(kCoreCalleeSaves)),
1008 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1009 arraysize(kFpuCalleeSaves)),
1010 compiler_options,
1011 stats),
1012 block_labels_(nullptr),
1013 location_builder_(graph, this),
1014 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001015 move_resolver_(graph->GetAllocator(), this),
Vladimir Markoa0431112018-06-25 09:32:54 +01001016 assembler_(graph->GetAllocator(),
1017 compiler_options.GetInstructionSetFeatures()->AsMipsInstructionSetFeatures()),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001018 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001019 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001020 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001021 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001022 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001023 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001024 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001025 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1026 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1027 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001028 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001029 // Save RA (containing the return address) to mimic Quick.
1030 AddAllocatedRegister(Location::RegisterLocation(RA));
1031}
1032
1033#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001034// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1035#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001036#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001037
1038void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1039 // Ensure that we fix up branches.
1040 __ FinalizeCode();
1041
1042 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001043 StackMapStream* stack_map_stream = GetStackMapStream();
1044 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
David Srbeckyd02b23f2018-05-29 23:27:22 +01001045 uint32_t old_position = stack_map_stream->GetStackMapNativePcOffset(i);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001046 uint32_t new_position = __ GetAdjustedPosition(old_position);
1047 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001048 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001049 }
1050
1051 // Adjust pc offsets for the disassembly information.
1052 if (disasm_info_ != nullptr) {
1053 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1054 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1055 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1056 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1057 it.second.start = __ GetAdjustedPosition(it.second.start);
1058 it.second.end = __ GetAdjustedPosition(it.second.end);
1059 }
1060 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1061 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1062 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1063 }
1064 }
1065
1066 CodeGenerator::Finalize(allocator);
1067}
1068
1069MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1070 return codegen_->GetAssembler();
1071}
1072
1073void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1074 DCHECK_LT(index, moves_.size());
1075 MoveOperands* move = moves_[index];
1076 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1077}
1078
1079void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1080 DCHECK_LT(index, moves_.size());
1081 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001082 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001083 Location loc1 = move->GetDestination();
1084 Location loc2 = move->GetSource();
1085
1086 DCHECK(!loc1.IsConstant());
1087 DCHECK(!loc2.IsConstant());
1088
1089 if (loc1.Equals(loc2)) {
1090 return;
1091 }
1092
1093 if (loc1.IsRegister() && loc2.IsRegister()) {
1094 // Swap 2 GPRs.
1095 Register r1 = loc1.AsRegister<Register>();
1096 Register r2 = loc2.AsRegister<Register>();
1097 __ Move(TMP, r2);
1098 __ Move(r2, r1);
1099 __ Move(r1, TMP);
1100 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001101 if (codegen_->GetGraph()->HasSIMD()) {
1102 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1103 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1104 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001105 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001106 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1107 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1108 if (type == DataType::Type::kFloat32) {
1109 __ MovS(FTMP, f2);
1110 __ MovS(f2, f1);
1111 __ MovS(f1, FTMP);
1112 } else {
1113 DCHECK_EQ(type, DataType::Type::kFloat64);
1114 __ MovD(FTMP, f2);
1115 __ MovD(f2, f1);
1116 __ MovD(f1, FTMP);
1117 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001118 }
1119 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1120 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1121 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001122 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001123 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1124 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001125 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001126 __ Move(TMP, r2);
1127 __ Mfc1(r2, f1);
1128 __ Mtc1(TMP, f1);
1129 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1130 // Swap 2 GPR register pairs.
1131 Register r1 = loc1.AsRegisterPairLow<Register>();
1132 Register r2 = loc2.AsRegisterPairLow<Register>();
1133 __ Move(TMP, r2);
1134 __ Move(r2, r1);
1135 __ Move(r1, TMP);
1136 r1 = loc1.AsRegisterPairHigh<Register>();
1137 r2 = loc2.AsRegisterPairHigh<Register>();
1138 __ Move(TMP, r2);
1139 __ Move(r2, r1);
1140 __ Move(r1, TMP);
1141 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1142 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1143 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001144 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001145 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1146 : loc2.AsFpuRegister<FRegister>();
1147 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1148 : loc2.AsRegisterPairLow<Register>();
1149 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1150 : loc2.AsRegisterPairHigh<Register>();
1151 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1152 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1153 // unpredictable and the following mfch1 will fail.
1154 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001155 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001156 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001157 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001158 __ Move(r2_l, TMP);
1159 __ Move(r2_h, AT);
1160 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1161 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1162 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1163 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001164 } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) {
1165 ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
David Brazdilcc0f3112016-01-28 17:14:52 +00001166 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1167 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001168 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1169 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001170 __ Move(TMP, reg);
1171 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1172 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1173 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1174 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1175 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1176 : loc2.AsRegisterPairLow<Register>();
1177 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1178 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001179 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001180 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1181 : loc2.GetHighStackIndex(kMipsWordSize);
1182 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001183 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001184 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001185 __ Move(TMP, reg_h);
1186 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1187 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001188 } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) ||
1189 (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) {
1190 Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2;
1191 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1192 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(fp_loc));
1193 __ LoadQFromOffset(fp_loc.AsFpuRegister<FRegister>(), SP, offset);
1194 __ StoreQToOffset(FTMP, SP, offset);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001195 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1196 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1197 : loc2.AsFpuRegister<FRegister>();
1198 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001199 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001200 __ MovS(FTMP, reg);
1201 __ LoadSFromOffset(reg, SP, offset);
1202 __ StoreSToOffset(FTMP, SP, offset);
1203 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001204 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001205 __ MovD(FTMP, reg);
1206 __ LoadDFromOffset(reg, SP, offset);
1207 __ StoreDToOffset(FTMP, SP, offset);
1208 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001209 } else {
1210 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1211 }
1212}
1213
1214void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1215 __ Pop(static_cast<Register>(reg));
1216}
1217
1218void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1219 __ Push(static_cast<Register>(reg));
1220}
1221
1222void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1223 // Allocate a scratch register other than TMP, if available.
1224 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1225 // automatically unspilled when the scratch scope object is destroyed).
1226 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1227 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001228 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001229 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1230 __ LoadFromOffset(kLoadWord,
1231 Register(ensure_scratch.GetRegister()),
1232 SP,
1233 index1 + stack_offset);
1234 __ LoadFromOffset(kLoadWord,
1235 TMP,
1236 SP,
1237 index2 + stack_offset);
1238 __ StoreToOffset(kStoreWord,
1239 Register(ensure_scratch.GetRegister()),
1240 SP,
1241 index2 + stack_offset);
1242 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1243 }
1244}
1245
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001246void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) {
1247 __ LoadQFromOffset(FTMP, SP, index1);
1248 __ LoadQFromOffset(FTMP2, SP, index2);
1249 __ StoreQToOffset(FTMP, SP, index2);
1250 __ StoreQToOffset(FTMP2, SP, index1);
1251}
1252
Alexey Frunze73296a72016-06-03 22:51:46 -07001253void CodeGeneratorMIPS::ComputeSpillMask() {
1254 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1255 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1256 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1257 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1258 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1259 // within the stack frame.
1260 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1261 core_spill_mask_ |= (1 << ZERO);
1262 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001263}
1264
1265bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001266 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001267 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1268 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1269 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001270 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001271}
1272
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001273static dwarf::Reg DWARFReg(Register reg) {
1274 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1275}
1276
1277// TODO: mapping of floating-point registers to DWARF.
1278
1279void CodeGeneratorMIPS::GenerateFrameEntry() {
1280 __ Bind(&frame_entry_label_);
1281
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001282 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01001283 __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
1284 __ Addiu(TMP, TMP, 1);
1285 __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001286 }
1287
Vladimir Marko33bff252017-11-01 14:35:42 +00001288 bool do_overflow_check =
1289 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001290
1291 if (do_overflow_check) {
1292 __ LoadFromOffset(kLoadWord,
1293 ZERO,
1294 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001295 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001296 RecordPcInfo(nullptr, 0);
1297 }
1298
1299 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001300 CHECK_EQ(fpu_spill_mask_, 0u);
1301 CHECK_EQ(core_spill_mask_, 1u << RA);
1302 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001303 return;
1304 }
1305
1306 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001307 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1308 LOG(FATAL) << "Stack frame larger than "
1309 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001310 }
1311
1312 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001313
Alexey Frunze73296a72016-06-03 22:51:46 -07001314 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001315 __ IncreaseFrameSize(ofs);
1316
Alexey Frunze73296a72016-06-03 22:51:46 -07001317 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1318 Register reg = static_cast<Register>(MostSignificantBit(mask));
1319 mask ^= 1u << reg;
1320 ofs -= kMipsWordSize;
1321 // The ZERO register is only included for alignment.
1322 if (reg != ZERO) {
1323 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001324 __ cfi().RelOffset(DWARFReg(reg), ofs);
1325 }
1326 }
1327
Alexey Frunze73296a72016-06-03 22:51:46 -07001328 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1329 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1330 mask ^= 1u << reg;
1331 ofs -= kMipsDoublewordSize;
1332 __ StoreDToOffset(reg, SP, ofs);
1333 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001334 }
1335
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001336 // Save the current method if we need it. Note that we do not
1337 // do this in HCurrentMethod, as the instruction might have been removed
1338 // in the SSA graph.
1339 if (RequiresCurrentMethod()) {
1340 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1341 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001342
1343 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1344 // Initialize should deoptimize flag to 0.
1345 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1346 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001347}
1348
1349void CodeGeneratorMIPS::GenerateFrameExit() {
1350 __ cfi().RememberState();
1351
1352 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001353 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001354
Alexey Frunze73296a72016-06-03 22:51:46 -07001355 // For better instruction scheduling restore RA before other registers.
1356 uint32_t ofs = GetFrameSize();
1357 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1358 Register reg = static_cast<Register>(MostSignificantBit(mask));
1359 mask ^= 1u << reg;
1360 ofs -= kMipsWordSize;
1361 // The ZERO register is only included for alignment.
1362 if (reg != ZERO) {
1363 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001364 __ cfi().Restore(DWARFReg(reg));
1365 }
1366 }
1367
Alexey Frunze73296a72016-06-03 22:51:46 -07001368 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1369 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1370 mask ^= 1u << reg;
1371 ofs -= kMipsDoublewordSize;
1372 __ LoadDFromOffset(reg, SP, ofs);
1373 // TODO: __ cfi().Restore(DWARFReg(reg));
1374 }
1375
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001376 size_t frame_size = GetFrameSize();
1377 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1378 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1379 bool reordering = __ SetReorder(false);
1380 if (exchange) {
1381 __ Jr(RA);
1382 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1383 } else {
1384 __ DecreaseFrameSize(frame_size);
1385 __ Jr(RA);
1386 __ Nop(); // In delay slot.
1387 }
1388 __ SetReorder(reordering);
1389 } else {
1390 __ Jr(RA);
1391 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001392 }
1393
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001394 __ cfi().RestoreState();
1395 __ cfi().DefCFAOffset(GetFrameSize());
1396}
1397
1398void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1399 __ Bind(GetLabelOf(block));
1400}
1401
Lena Djokicca8c2952017-05-29 11:31:46 +02001402VectorRegister VectorRegisterFrom(Location location) {
1403 DCHECK(location.IsFpuRegister());
1404 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1405}
1406
Lena Djokic8098da92017-06-28 12:07:50 +02001407void CodeGeneratorMIPS::MoveLocation(Location destination,
1408 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001409 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001410 if (source.Equals(destination)) {
1411 return;
1412 }
1413
Lena Djokic8098da92017-06-28 12:07:50 +02001414 if (source.IsConstant()) {
1415 MoveConstant(destination, source.GetConstant());
1416 } else {
1417 if (destination.IsRegister()) {
1418 if (source.IsRegister()) {
1419 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1420 } else if (source.IsFpuRegister()) {
1421 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1422 } else {
1423 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001424 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001425 }
1426 } else if (destination.IsRegisterPair()) {
1427 if (source.IsRegisterPair()) {
1428 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1429 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1430 } else if (source.IsFpuRegister()) {
1431 Register dst_high = destination.AsRegisterPairHigh<Register>();
1432 Register dst_low = destination.AsRegisterPairLow<Register>();
1433 FRegister src = source.AsFpuRegister<FRegister>();
1434 __ Mfc1(dst_low, src);
1435 __ MoveFromFpuHigh(dst_high, src);
1436 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001437 DCHECK(source.IsDoubleStackSlot())
1438 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001439 int32_t off = source.GetStackIndex();
1440 Register r = destination.AsRegisterPairLow<Register>();
1441 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1442 }
1443 } else if (destination.IsFpuRegister()) {
1444 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001445 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001446 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1447 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001448 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001449 FRegister dst = destination.AsFpuRegister<FRegister>();
1450 Register src_high = source.AsRegisterPairHigh<Register>();
1451 Register src_low = source.AsRegisterPairLow<Register>();
1452 __ Mtc1(src_low, dst);
1453 __ MoveToFpuHigh(src_high, dst);
1454 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001455 if (GetGraph()->HasSIMD()) {
1456 __ MoveV(VectorRegisterFrom(destination),
1457 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001458 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001459 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001460 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1461 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001462 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001463 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1464 }
Lena Djokic8098da92017-06-28 12:07:50 +02001465 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001466 } else if (source.IsSIMDStackSlot()) {
1467 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001468 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001469 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001470 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1471 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001472 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001473 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1474 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1475 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001476 } else if (destination.IsSIMDStackSlot()) {
1477 if (source.IsFpuRegister()) {
1478 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1479 } else {
1480 DCHECK(source.IsSIMDStackSlot());
1481 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1482 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1483 }
Lena Djokic8098da92017-06-28 12:07:50 +02001484 } else if (destination.IsDoubleStackSlot()) {
1485 int32_t dst_offset = destination.GetStackIndex();
1486 if (source.IsRegisterPair()) {
1487 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1488 } else if (source.IsFpuRegister()) {
1489 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1490 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001491 DCHECK(source.IsDoubleStackSlot())
1492 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001493 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1494 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1495 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1496 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1497 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001498 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001499 DCHECK(destination.IsStackSlot()) << destination;
1500 int32_t dst_offset = destination.GetStackIndex();
1501 if (source.IsRegister()) {
1502 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1503 } else if (source.IsFpuRegister()) {
1504 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1505 } else {
1506 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1507 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1508 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1509 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001510 }
1511 }
1512}
1513
1514void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1515 if (c->IsIntConstant() || c->IsNullConstant()) {
1516 // Move 32 bit constant.
1517 int32_t value = GetInt32ValueOf(c);
1518 if (destination.IsRegister()) {
1519 Register dst = destination.AsRegister<Register>();
1520 __ LoadConst32(dst, value);
1521 } else {
1522 DCHECK(destination.IsStackSlot())
1523 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001524 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001525 }
1526 } else if (c->IsLongConstant()) {
1527 // Move 64 bit constant.
1528 int64_t value = GetInt64ValueOf(c);
1529 if (destination.IsRegisterPair()) {
1530 Register r_h = destination.AsRegisterPairHigh<Register>();
1531 Register r_l = destination.AsRegisterPairLow<Register>();
1532 __ LoadConst64(r_h, r_l, value);
1533 } else {
1534 DCHECK(destination.IsDoubleStackSlot())
1535 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001536 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001537 }
1538 } else if (c->IsFloatConstant()) {
1539 // Move 32 bit float constant.
1540 int32_t value = GetInt32ValueOf(c);
1541 if (destination.IsFpuRegister()) {
1542 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1543 } else {
1544 DCHECK(destination.IsStackSlot())
1545 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001546 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001547 }
1548 } else {
1549 // Move 64 bit double constant.
1550 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1551 int64_t value = GetInt64ValueOf(c);
1552 if (destination.IsFpuRegister()) {
1553 FRegister fd = destination.AsFpuRegister<FRegister>();
1554 __ LoadDConst64(fd, value, TMP);
1555 } else {
1556 DCHECK(destination.IsDoubleStackSlot())
1557 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001558 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001559 }
1560 }
1561}
1562
1563void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1564 DCHECK(destination.IsRegister());
1565 Register dst = destination.AsRegister<Register>();
1566 __ LoadConst32(dst, value);
1567}
1568
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001569void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1570 if (location.IsRegister()) {
1571 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001572 } else if (location.IsRegisterPair()) {
1573 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1574 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001575 } else {
1576 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1577 }
1578}
1579
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001580template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001581inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1582 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001583 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001584 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001585 const DexFile* dex_file = info.target_dex_file;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001586 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001587 DCHECK(info.label.IsBound());
1588 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001589 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1590 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001591 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1592 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1593 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001594 : __ GetPcRelBaseLabelLocation();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001595 linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001596 }
1597}
1598
Vladimir Markob066d432018-01-03 13:14:37 +00001599linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
1600 const DexFile* target_dex_file,
1601 uint32_t pc_insn_offset,
1602 uint32_t boot_image_offset) {
1603 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
1604 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
1605}
1606
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001607void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001608 DCHECK(linker_patches->empty());
1609 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001610 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001611 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001612 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001613 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001614 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001615 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001616 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001617 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001618 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001619 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001620 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001621 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001622 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001623 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001624 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00001625 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
1626 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001627 DCHECK(boot_image_type_patches_.empty());
1628 DCHECK(boot_image_string_patches_.empty());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001629 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001630 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1631 method_bss_entry_patches_, linker_patches);
1632 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1633 type_bss_entry_patches_, linker_patches);
1634 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1635 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001636 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001637}
1638
Vladimir Markob066d432018-01-03 13:14:37 +00001639CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageRelRoPatch(
1640 uint32_t boot_image_offset,
1641 const PcRelativePatchInfo* info_high) {
1642 return NewPcRelativePatch(
1643 /* dex_file */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_);
1644}
1645
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001646CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001647 MethodReference target_method,
1648 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001649 return NewPcRelativePatch(
1650 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001651}
1652
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001653CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001654 MethodReference target_method,
1655 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001656 return NewPcRelativePatch(
1657 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001658}
1659
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001660CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001661 const DexFile& dex_file,
1662 dex::TypeIndex type_index,
1663 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001664 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001665}
1666
Vladimir Marko1998cd02017-01-13 13:02:58 +00001667CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001668 const DexFile& dex_file,
1669 dex::TypeIndex type_index,
1670 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001671 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001672}
1673
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001674CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001675 const DexFile& dex_file,
1676 dex::StringIndex string_index,
1677 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001678 return NewPcRelativePatch(
1679 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001680}
1681
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001682CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1683 const DexFile& dex_file,
1684 dex::StringIndex string_index,
1685 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001686 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001687}
1688
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001689CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001690 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001691 uint32_t offset_or_index,
1692 const PcRelativePatchInfo* info_high,
1693 ArenaDeque<PcRelativePatchInfo>* patches) {
1694 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001695 return &patches->back();
1696}
1697
Alexey Frunze06a46c42016-07-19 15:00:40 -07001698Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1699 return map->GetOrCreate(
1700 value,
1701 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1702}
1703
Alexey Frunze06a46c42016-07-19 15:00:40 -07001704Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001705 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001706}
1707
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001708void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001709 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001710 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001711 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001712 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001713 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001714 if (GetInstructionSetFeatures().IsR6()) {
1715 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001716 __ Bind(&info_high->label);
1717 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001718 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001719 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001720 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001721 } else {
1722 // If base is ZERO, emit NAL to obtain the actual base.
1723 if (base == ZERO) {
1724 // Generate a dummy PC-relative call to obtain PC.
1725 __ Nal();
1726 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001727 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001728 __ Lui(out, /* placeholder */ 0x1234);
1729 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1730 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1731 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001732 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001733 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001734 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001735 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001736 __ Addu(out, out, (base == ZERO) ? RA : base);
1737 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001738 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001739 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001740}
1741
Vladimir Markoeebb8212018-06-05 14:57:24 +01001742void CodeGeneratorMIPS::LoadBootImageAddress(Register reg, uint32_t boot_image_offset) {
1743 DCHECK(!GetCompilerOptions().IsBootImage());
1744 if (GetCompilerOptions().GetCompilePic()) {
1745 DCHECK(Runtime::Current()->IsAotCompiler());
1746 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
1747 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
1748 EmitPcRelativeAddressPlaceholderHigh(info_high, reg, /* base */ ZERO);
1749 __ Lw(reg, reg, /* placeholder */ 0x5678, &info_low->label);
1750 } else {
1751 gc::Heap* heap = Runtime::Current()->GetHeap();
1752 DCHECK(!heap->GetBootImageSpaces().empty());
1753 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_offset;
1754 __ LoadConst32(reg, dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address)));
1755 }
1756}
1757
Alexey Frunze627c1a02017-01-30 19:28:14 -08001758CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1759 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001760 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001761 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001762 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1763 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001764 return &jit_string_patches_.back();
1765}
1766
1767CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1768 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001769 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001770 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001771 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1772 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001773 return &jit_class_patches_.back();
1774}
1775
1776void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1777 const uint8_t* roots_data,
1778 const CodeGeneratorMIPS::JitPatchInfo& info,
1779 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001780 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1781 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001782 uintptr_t address =
1783 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1784 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1785 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001786 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1787 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1788 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1789 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001790 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001791 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1792 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001793 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001794 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001795 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1796 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001797 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001798 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1799 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001800}
1801
1802void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1803 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001804 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1805 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001806 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001807 }
1808 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001809 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1810 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001811 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001812 }
1813}
1814
Goran Jakovljevice114da22016-12-26 14:21:43 +01001815void CodeGeneratorMIPS::MarkGCCard(Register object,
1816 Register value,
1817 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001818 MipsLabel done;
1819 Register card = AT;
1820 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001821 if (value_can_be_null) {
1822 __ Beqz(value, &done);
1823 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001824 __ LoadFromOffset(kLoadWord,
1825 card,
1826 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001827 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001828 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1829 __ Addu(temp, card, temp);
1830 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001831 if (value_can_be_null) {
1832 __ Bind(&done);
1833 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001834}
1835
David Brazdil58282f42016-01-14 12:45:10 +00001836void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001837 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1838 blocked_core_registers_[ZERO] = true;
1839 blocked_core_registers_[K0] = true;
1840 blocked_core_registers_[K1] = true;
1841 blocked_core_registers_[GP] = true;
1842 blocked_core_registers_[SP] = true;
1843 blocked_core_registers_[RA] = true;
1844
1845 // AT and TMP(T8) are used as temporary/scratch registers
1846 // (similar to how AT is used by MIPS assemblers).
1847 blocked_core_registers_[AT] = true;
1848 blocked_core_registers_[TMP] = true;
1849 blocked_fpu_registers_[FTMP] = true;
1850
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001851 if (GetInstructionSetFeatures().HasMsa()) {
1852 // To be used just for MSA instructions.
1853 blocked_fpu_registers_[FTMP2] = true;
1854 }
1855
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001856 // Reserve suspend and thread registers.
1857 blocked_core_registers_[S0] = true;
1858 blocked_core_registers_[TR] = true;
1859
1860 // Reserve T9 for function calls
1861 blocked_core_registers_[T9] = true;
1862
1863 // Reserve odd-numbered FPU registers.
1864 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1865 blocked_fpu_registers_[i] = true;
1866 }
1867
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001868 if (GetGraph()->IsDebuggable()) {
1869 // Stubs do not save callee-save floating point registers. If the graph
1870 // is debuggable, we need to deal with these registers differently. For
1871 // now, just block them.
1872 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1873 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1874 }
1875 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876}
1877
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001878size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1879 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1880 return kMipsWordSize;
1881}
1882
1883size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1884 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1885 return kMipsWordSize;
1886}
1887
1888size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001889 if (GetGraph()->HasSIMD()) {
1890 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1891 } else {
1892 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1893 }
1894 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001895}
1896
1897size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001898 if (GetGraph()->HasSIMD()) {
1899 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1900 } else {
1901 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1902 }
1903 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001904}
1905
1906void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001907 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001908}
1909
1910void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001911 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912}
1913
Vladimir Markoa0431112018-06-25 09:32:54 +01001914const MipsInstructionSetFeatures& CodeGeneratorMIPS::GetInstructionSetFeatures() const {
1915 return *GetCompilerOptions().GetInstructionSetFeatures()->AsMipsInstructionSetFeatures();
1916}
1917
Serban Constantinescufca16662016-07-14 09:21:59 +01001918constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1919
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001920void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1921 HInstruction* instruction,
1922 uint32_t dex_pc,
1923 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001924 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001925 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1926 IsDirectEntrypoint(entrypoint));
1927 if (EntrypointRequiresStackMap(entrypoint)) {
1928 RecordPcInfo(instruction, dex_pc, slow_path);
1929 }
1930}
1931
1932void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1933 HInstruction* instruction,
1934 SlowPathCode* slow_path,
1935 bool direct) {
1936 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1937 GenerateInvokeRuntime(entry_point_offset, direct);
1938}
1939
1940void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001941 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001942 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001943 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001944 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001945 // Reserve argument space on stack (for $a0-$a3) for
1946 // entrypoints that directly reference native implementations.
1947 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001948 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001949 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001950 } else {
1951 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001952 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001953 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001954}
1955
1956void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1957 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001958 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1959 const size_t status_byte_offset =
1960 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1961 constexpr uint32_t shifted_initialized_value =
1962 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1963
1964 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01001965 __ Sltiu(TMP, TMP, shifted_initialized_value);
1966 __ Bnez(TMP, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001967 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1968 __ Sync(0);
1969 __ Bind(slow_path->GetExitLabel());
1970}
1971
Vladimir Marko175e7862018-03-27 09:03:13 +00001972void InstructionCodeGeneratorMIPS::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
1973 Register temp) {
1974 uint32_t path_to_root = check->GetBitstringPathToRoot();
1975 uint32_t mask = check->GetBitstringMask();
1976 DCHECK(IsPowerOfTwo(mask + 1));
1977 size_t mask_bits = WhichPowerOf2(mask + 1);
1978
1979 if (mask_bits == 16u) {
1980 // Load only the bitstring part of the status word.
1981 __ LoadFromOffset(
1982 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
1983 // Compare the bitstring bits using XOR.
1984 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1985 } else {
1986 // /* uint32_t */ temp = temp->status_
1987 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
1988 // Compare the bitstring bits using XOR.
1989 if (IsUint<16>(path_to_root)) {
1990 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1991 } else {
1992 __ LoadConst32(TMP, path_to_root);
1993 __ Xor(temp, temp, TMP);
1994 }
1995 // Shift out bits that do not contribute to the comparison.
1996 __ Sll(temp, temp, 32 - mask_bits);
1997 }
1998}
1999
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002000void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
2001 __ Sync(0); // Only stype 0 is supported.
2002}
2003
2004void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
2005 HBasicBlock* successor) {
2006 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07002007 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
2008
2009 if (slow_path == nullptr) {
2010 slow_path =
2011 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
2012 instruction->SetSlowPath(slow_path);
2013 codegen_->AddSlowPath(slow_path);
2014 if (successor != nullptr) {
2015 DCHECK(successor->IsLoopHeader());
2016 }
2017 } else {
2018 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2019 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002020
2021 __ LoadFromOffset(kLoadUnsignedHalfword,
2022 TMP,
2023 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002024 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002025 if (successor == nullptr) {
2026 __ Bnez(TMP, slow_path->GetEntryLabel());
2027 __ Bind(slow_path->GetReturnLabel());
2028 } else {
2029 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2030 __ B(slow_path->GetEntryLabel());
2031 // slow_path will return to GetLabelOf(successor).
2032 }
2033}
2034
2035InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2036 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002037 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002038 assembler_(codegen->GetAssembler()),
2039 codegen_(codegen) {}
2040
2041void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2042 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002043 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01002045 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002046 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002047 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002048 locations->SetInAt(0, Location::RequiresRegister());
2049 HInstruction* right = instruction->InputAt(1);
2050 bool can_use_imm = false;
2051 if (right->IsConstant()) {
2052 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2053 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2054 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002055 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002056 DCHECK(instruction->IsSub() || instruction->IsAdd());
2057 if (instruction->IsSub()) {
2058 imm = -imm;
2059 }
2060 if (isR6) {
2061 bool single_use = right->GetUses().HasExactlyOneElement();
2062 int16_t imm_high = High16Bits(imm);
2063 int16_t imm_low = Low16Bits(imm);
2064 if (imm_low < 0) {
2065 imm_high += 1;
2066 }
2067 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2068 } else {
2069 can_use_imm = IsInt<16>(imm);
2070 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002071 }
2072 }
2073 if (can_use_imm)
2074 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2075 else
2076 locations->SetInAt(1, Location::RequiresRegister());
2077 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2078 break;
2079 }
2080
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002081 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002082 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002083 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2084 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002085 break;
2086 }
2087
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002088 case DataType::Type::kFloat32:
2089 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002090 DCHECK(instruction->IsAdd() || instruction->IsSub());
2091 locations->SetInAt(0, Location::RequiresFpuRegister());
2092 locations->SetInAt(1, Location::RequiresFpuRegister());
2093 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2094 break;
2095
2096 default:
2097 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2098 }
2099}
2100
2101void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002102 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002103 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002104 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002105
2106 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002107 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002108 Register dst = locations->Out().AsRegister<Register>();
2109 Register lhs = locations->InAt(0).AsRegister<Register>();
2110 Location rhs_location = locations->InAt(1);
2111
2112 Register rhs_reg = ZERO;
2113 int32_t rhs_imm = 0;
2114 bool use_imm = rhs_location.IsConstant();
2115 if (use_imm) {
2116 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2117 } else {
2118 rhs_reg = rhs_location.AsRegister<Register>();
2119 }
2120
2121 if (instruction->IsAnd()) {
2122 if (use_imm)
2123 __ Andi(dst, lhs, rhs_imm);
2124 else
2125 __ And(dst, lhs, rhs_reg);
2126 } else if (instruction->IsOr()) {
2127 if (use_imm)
2128 __ Ori(dst, lhs, rhs_imm);
2129 else
2130 __ Or(dst, lhs, rhs_reg);
2131 } else if (instruction->IsXor()) {
2132 if (use_imm)
2133 __ Xori(dst, lhs, rhs_imm);
2134 else
2135 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002136 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002137 DCHECK(instruction->IsAdd() || instruction->IsSub());
2138 if (use_imm) {
2139 if (instruction->IsSub()) {
2140 rhs_imm = -rhs_imm;
2141 }
2142 if (IsInt<16>(rhs_imm)) {
2143 __ Addiu(dst, lhs, rhs_imm);
2144 } else {
2145 DCHECK(isR6);
2146 int16_t rhs_imm_high = High16Bits(rhs_imm);
2147 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2148 if (rhs_imm_low < 0) {
2149 rhs_imm_high += 1;
2150 }
2151 __ Aui(dst, lhs, rhs_imm_high);
2152 if (rhs_imm_low != 0) {
2153 __ Addiu(dst, dst, rhs_imm_low);
2154 }
2155 }
2156 } else if (instruction->IsAdd()) {
2157 __ Addu(dst, lhs, rhs_reg);
2158 } else {
2159 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002160 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002161 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002162 }
2163 break;
2164 }
2165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002166 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002167 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2168 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2169 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2170 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002171 Location rhs_location = locations->InAt(1);
2172 bool use_imm = rhs_location.IsConstant();
2173 if (!use_imm) {
2174 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2175 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2176 if (instruction->IsAnd()) {
2177 __ And(dst_low, lhs_low, rhs_low);
2178 __ And(dst_high, lhs_high, rhs_high);
2179 } else if (instruction->IsOr()) {
2180 __ Or(dst_low, lhs_low, rhs_low);
2181 __ Or(dst_high, lhs_high, rhs_high);
2182 } else if (instruction->IsXor()) {
2183 __ Xor(dst_low, lhs_low, rhs_low);
2184 __ Xor(dst_high, lhs_high, rhs_high);
2185 } else if (instruction->IsAdd()) {
2186 if (lhs_low == rhs_low) {
2187 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2188 __ Slt(TMP, lhs_low, ZERO);
2189 __ Addu(dst_low, lhs_low, rhs_low);
2190 } else {
2191 __ Addu(dst_low, lhs_low, rhs_low);
2192 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2193 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2194 }
2195 __ Addu(dst_high, lhs_high, rhs_high);
2196 __ Addu(dst_high, dst_high, TMP);
2197 } else {
2198 DCHECK(instruction->IsSub());
2199 __ Sltu(TMP, lhs_low, rhs_low);
2200 __ Subu(dst_low, lhs_low, rhs_low);
2201 __ Subu(dst_high, lhs_high, rhs_high);
2202 __ Subu(dst_high, dst_high, TMP);
2203 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002204 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002205 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2206 if (instruction->IsOr()) {
2207 uint32_t low = Low32Bits(value);
2208 uint32_t high = High32Bits(value);
2209 if (IsUint<16>(low)) {
2210 if (dst_low != lhs_low || low != 0) {
2211 __ Ori(dst_low, lhs_low, low);
2212 }
2213 } else {
2214 __ LoadConst32(TMP, low);
2215 __ Or(dst_low, lhs_low, TMP);
2216 }
2217 if (IsUint<16>(high)) {
2218 if (dst_high != lhs_high || high != 0) {
2219 __ Ori(dst_high, lhs_high, high);
2220 }
2221 } else {
2222 if (high != low) {
2223 __ LoadConst32(TMP, high);
2224 }
2225 __ Or(dst_high, lhs_high, TMP);
2226 }
2227 } else if (instruction->IsXor()) {
2228 uint32_t low = Low32Bits(value);
2229 uint32_t high = High32Bits(value);
2230 if (IsUint<16>(low)) {
2231 if (dst_low != lhs_low || low != 0) {
2232 __ Xori(dst_low, lhs_low, low);
2233 }
2234 } else {
2235 __ LoadConst32(TMP, low);
2236 __ Xor(dst_low, lhs_low, TMP);
2237 }
2238 if (IsUint<16>(high)) {
2239 if (dst_high != lhs_high || high != 0) {
2240 __ Xori(dst_high, lhs_high, high);
2241 }
2242 } else {
2243 if (high != low) {
2244 __ LoadConst32(TMP, high);
2245 }
2246 __ Xor(dst_high, lhs_high, TMP);
2247 }
2248 } else if (instruction->IsAnd()) {
2249 uint32_t low = Low32Bits(value);
2250 uint32_t high = High32Bits(value);
2251 if (IsUint<16>(low)) {
2252 __ Andi(dst_low, lhs_low, low);
2253 } else if (low != 0xFFFFFFFF) {
2254 __ LoadConst32(TMP, low);
2255 __ And(dst_low, lhs_low, TMP);
2256 } else if (dst_low != lhs_low) {
2257 __ Move(dst_low, lhs_low);
2258 }
2259 if (IsUint<16>(high)) {
2260 __ Andi(dst_high, lhs_high, high);
2261 } else if (high != 0xFFFFFFFF) {
2262 if (high != low) {
2263 __ LoadConst32(TMP, high);
2264 }
2265 __ And(dst_high, lhs_high, TMP);
2266 } else if (dst_high != lhs_high) {
2267 __ Move(dst_high, lhs_high);
2268 }
2269 } else {
2270 if (instruction->IsSub()) {
2271 value = -value;
2272 } else {
2273 DCHECK(instruction->IsAdd());
2274 }
2275 int32_t low = Low32Bits(value);
2276 int32_t high = High32Bits(value);
2277 if (IsInt<16>(low)) {
2278 if (dst_low != lhs_low || low != 0) {
2279 __ Addiu(dst_low, lhs_low, low);
2280 }
2281 if (low != 0) {
2282 __ Sltiu(AT, dst_low, low);
2283 }
2284 } else {
2285 __ LoadConst32(TMP, low);
2286 __ Addu(dst_low, lhs_low, TMP);
2287 __ Sltu(AT, dst_low, TMP);
2288 }
2289 if (IsInt<16>(high)) {
2290 if (dst_high != lhs_high || high != 0) {
2291 __ Addiu(dst_high, lhs_high, high);
2292 }
2293 } else {
2294 if (high != low) {
2295 __ LoadConst32(TMP, high);
2296 }
2297 __ Addu(dst_high, lhs_high, TMP);
2298 }
2299 if (low != 0) {
2300 __ Addu(dst_high, dst_high, AT);
2301 }
2302 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303 }
2304 break;
2305 }
2306
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002307 case DataType::Type::kFloat32:
2308 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002309 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2310 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2311 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2312 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002314 __ AddS(dst, lhs, rhs);
2315 } else {
2316 __ AddD(dst, lhs, rhs);
2317 }
2318 } else {
2319 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002321 __ SubS(dst, lhs, rhs);
2322 } else {
2323 __ SubD(dst, lhs, rhs);
2324 }
2325 }
2326 break;
2327 }
2328
2329 default:
2330 LOG(FATAL) << "Unexpected binary operation type " << type;
2331 }
2332}
2333
2334void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002335 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002336
Vladimir Markoca6fff82017-10-03 14:49:14 +01002337 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002338 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002339 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002340 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002341 locations->SetInAt(0, Location::RequiresRegister());
2342 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2343 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2344 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002345 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002346 locations->SetInAt(0, Location::RequiresRegister());
2347 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2348 locations->SetOut(Location::RequiresRegister());
2349 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002350 default:
2351 LOG(FATAL) << "Unexpected shift type " << type;
2352 }
2353}
2354
2355static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2356
2357void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002358 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002359 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002360 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002361
2362 Location rhs_location = locations->InAt(1);
2363 bool use_imm = rhs_location.IsConstant();
2364 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2365 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002366 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002367 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002368 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002369 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2370 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002371
2372 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002373 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002374 Register dst = locations->Out().AsRegister<Register>();
2375 Register lhs = locations->InAt(0).AsRegister<Register>();
2376 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002377 if (shift_value == 0) {
2378 if (dst != lhs) {
2379 __ Move(dst, lhs);
2380 }
2381 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002382 __ Sll(dst, lhs, shift_value);
2383 } else if (instr->IsShr()) {
2384 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002385 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002386 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002387 } else {
2388 if (has_ins_rotr) {
2389 __ Rotr(dst, lhs, shift_value);
2390 } else {
2391 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2392 __ Srl(dst, lhs, shift_value);
2393 __ Or(dst, dst, TMP);
2394 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002395 }
2396 } else {
2397 if (instr->IsShl()) {
2398 __ Sllv(dst, lhs, rhs_reg);
2399 } else if (instr->IsShr()) {
2400 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002401 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002402 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002403 } else {
2404 if (has_ins_rotr) {
2405 __ Rotrv(dst, lhs, rhs_reg);
2406 } else {
2407 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002408 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2409 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2410 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2411 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2412 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002413 __ Sllv(TMP, lhs, TMP);
2414 __ Srlv(dst, lhs, rhs_reg);
2415 __ Or(dst, dst, TMP);
2416 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002417 }
2418 }
2419 break;
2420 }
2421
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002422 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002423 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2424 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2425 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2426 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2427 if (use_imm) {
2428 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002429 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002430 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002431 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002432 if (instr->IsShl()) {
2433 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2434 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2435 __ Sll(dst_low, lhs_low, shift_value);
2436 } else if (instr->IsShr()) {
2437 __ Srl(dst_low, lhs_low, shift_value);
2438 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2439 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002440 } else if (instr->IsUShr()) {
2441 __ Srl(dst_low, lhs_low, shift_value);
2442 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2443 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002444 } else {
2445 __ Srl(dst_low, lhs_low, shift_value);
2446 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2447 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002448 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002449 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002450 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002451 if (instr->IsShl()) {
2452 __ Sll(dst_low, lhs_low, shift_value);
2453 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2454 __ Sll(dst_high, lhs_high, shift_value);
2455 __ Or(dst_high, dst_high, TMP);
2456 } else if (instr->IsShr()) {
2457 __ Sra(dst_high, lhs_high, shift_value);
2458 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2459 __ Srl(dst_low, lhs_low, shift_value);
2460 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002461 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002462 __ Srl(dst_high, lhs_high, shift_value);
2463 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2464 __ Srl(dst_low, lhs_low, shift_value);
2465 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002466 } else {
2467 __ Srl(TMP, lhs_low, shift_value);
2468 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2469 __ Or(dst_low, dst_low, TMP);
2470 __ Srl(TMP, lhs_high, shift_value);
2471 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2472 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002473 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002474 }
2475 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002476 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002477 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002478 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002479 __ Move(dst_low, ZERO);
2480 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002481 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002482 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002483 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002484 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002485 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002486 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002487 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002488 // 64-bit rotation by 32 is just a swap.
2489 __ Move(dst_low, lhs_high);
2490 __ Move(dst_high, lhs_low);
2491 } else {
2492 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002493 __ Srl(dst_low, lhs_high, shift_value_high);
2494 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2495 __ Srl(dst_high, lhs_low, shift_value_high);
2496 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002497 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002498 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2499 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002500 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002501 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2502 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002503 __ Or(dst_high, dst_high, TMP);
2504 }
2505 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002506 }
2507 }
2508 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002509 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002510 MipsLabel done;
2511 if (instr->IsShl()) {
2512 __ Sllv(dst_low, lhs_low, rhs_reg);
2513 __ Nor(AT, ZERO, rhs_reg);
2514 __ Srl(TMP, lhs_low, 1);
2515 __ Srlv(TMP, TMP, AT);
2516 __ Sllv(dst_high, lhs_high, rhs_reg);
2517 __ Or(dst_high, dst_high, TMP);
2518 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002519 if (isR6) {
2520 __ Beqzc(TMP, &done, /* is_bare */ true);
2521 __ Move(dst_high, dst_low);
2522 __ Move(dst_low, ZERO);
2523 } else {
2524 __ Movn(dst_high, dst_low, TMP);
2525 __ Movn(dst_low, ZERO, TMP);
2526 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002527 } else if (instr->IsShr()) {
2528 __ Srav(dst_high, lhs_high, rhs_reg);
2529 __ Nor(AT, ZERO, rhs_reg);
2530 __ Sll(TMP, lhs_high, 1);
2531 __ Sllv(TMP, TMP, AT);
2532 __ Srlv(dst_low, lhs_low, rhs_reg);
2533 __ Or(dst_low, dst_low, TMP);
2534 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002535 if (isR6) {
2536 __ Beqzc(TMP, &done, /* is_bare */ true);
2537 __ Move(dst_low, dst_high);
2538 __ Sra(dst_high, dst_high, 31);
2539 } else {
2540 __ Sra(AT, dst_high, 31);
2541 __ Movn(dst_low, dst_high, TMP);
2542 __ Movn(dst_high, AT, TMP);
2543 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002544 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002545 __ Srlv(dst_high, lhs_high, rhs_reg);
2546 __ Nor(AT, ZERO, rhs_reg);
2547 __ Sll(TMP, lhs_high, 1);
2548 __ Sllv(TMP, TMP, AT);
2549 __ Srlv(dst_low, lhs_low, rhs_reg);
2550 __ Or(dst_low, dst_low, TMP);
2551 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002552 if (isR6) {
2553 __ Beqzc(TMP, &done, /* is_bare */ true);
2554 __ Move(dst_low, dst_high);
2555 __ Move(dst_high, ZERO);
2556 } else {
2557 __ Movn(dst_low, dst_high, TMP);
2558 __ Movn(dst_high, ZERO, TMP);
2559 }
2560 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002561 __ Nor(AT, ZERO, rhs_reg);
2562 __ Srlv(TMP, lhs_low, rhs_reg);
2563 __ Sll(dst_low, lhs_high, 1);
2564 __ Sllv(dst_low, dst_low, AT);
2565 __ Or(dst_low, dst_low, TMP);
2566 __ Srlv(TMP, lhs_high, rhs_reg);
2567 __ Sll(dst_high, lhs_low, 1);
2568 __ Sllv(dst_high, dst_high, AT);
2569 __ Or(dst_high, dst_high, TMP);
2570 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002571 if (isR6) {
2572 __ Beqzc(TMP, &done, /* is_bare */ true);
2573 __ Move(TMP, dst_high);
2574 __ Move(dst_high, dst_low);
2575 __ Move(dst_low, TMP);
2576 } else {
2577 __ Movn(AT, dst_high, TMP);
2578 __ Movn(dst_high, dst_low, TMP);
2579 __ Movn(dst_low, AT, TMP);
2580 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002581 }
2582 __ Bind(&done);
2583 }
2584 break;
2585 }
2586
2587 default:
2588 LOG(FATAL) << "Unexpected shift operation type " << type;
2589 }
2590}
2591
2592void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2593 HandleBinaryOp(instruction);
2594}
2595
2596void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2597 HandleBinaryOp(instruction);
2598}
2599
2600void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2601 HandleBinaryOp(instruction);
2602}
2603
2604void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2605 HandleBinaryOp(instruction);
2606}
2607
2608void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002609 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002610 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002611 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002612 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002613 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2614 object_array_get_with_read_barrier
2615 ? LocationSummary::kCallOnSlowPath
2616 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002617 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2618 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2619 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 locations->SetInAt(0, Location::RequiresRegister());
2621 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002622 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002623 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2624 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002625 // The output overlaps in the case of an object array get with
2626 // read barriers enabled: we do not want the move to overwrite the
2627 // array's location, as we need it to emit the read barrier.
2628 locations->SetOut(Location::RequiresRegister(),
2629 object_array_get_with_read_barrier
2630 ? Location::kOutputOverlap
2631 : Location::kNoOutputOverlap);
2632 }
2633 // We need a temporary register for the read barrier marking slow
2634 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2635 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002636 bool temp_needed = instruction->GetIndex()->IsConstant()
2637 ? !kBakerReadBarrierThunksEnableForFields
2638 : !kBakerReadBarrierThunksEnableForArrays;
2639 if (temp_needed) {
2640 locations->AddTemp(Location::RequiresRegister());
2641 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002642 }
2643}
2644
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002645static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2646 auto null_checker = [codegen, instruction]() {
2647 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002648 };
2649 return null_checker;
2650}
2651
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002652void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2653 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002654 Location obj_loc = locations->InAt(0);
2655 Register obj = obj_loc.AsRegister<Register>();
2656 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002657 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002658 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002659 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002660
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002662 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2663 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002664 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002665 case DataType::Type::kBool:
2666 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002667 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002668 if (index.IsConstant()) {
2669 size_t offset =
2670 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002671 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002672 } else {
2673 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002674 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002675 }
2676 break;
2677 }
2678
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002679 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002680 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002681 if (index.IsConstant()) {
2682 size_t offset =
2683 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002684 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002685 } else {
2686 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002687 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002688 }
2689 break;
2690 }
2691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002692 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002693 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002694 if (maybe_compressed_char_at) {
2695 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2696 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2697 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2698 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2699 "Expecting 0=compressed, 1=uncompressed");
2700 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002701 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002702 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2703 if (maybe_compressed_char_at) {
2704 MipsLabel uncompressed_load, done;
2705 __ Bnez(TMP, &uncompressed_load);
2706 __ LoadFromOffset(kLoadUnsignedByte,
2707 out,
2708 obj,
2709 data_offset + (const_index << TIMES_1));
2710 __ B(&done);
2711 __ Bind(&uncompressed_load);
2712 __ LoadFromOffset(kLoadUnsignedHalfword,
2713 out,
2714 obj,
2715 data_offset + (const_index << TIMES_2));
2716 __ Bind(&done);
2717 } else {
2718 __ LoadFromOffset(kLoadUnsignedHalfword,
2719 out,
2720 obj,
2721 data_offset + (const_index << TIMES_2),
2722 null_checker);
2723 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002724 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002725 Register index_reg = index.AsRegister<Register>();
2726 if (maybe_compressed_char_at) {
2727 MipsLabel uncompressed_load, done;
2728 __ Bnez(TMP, &uncompressed_load);
2729 __ Addu(TMP, obj, index_reg);
2730 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2731 __ B(&done);
2732 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002733 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002734 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2735 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002736 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2737 __ Addu(TMP, index_reg, obj);
2738 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002739 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002740 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002741 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2742 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002743 }
2744 break;
2745 }
2746
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002747 case DataType::Type::kInt16: {
2748 Register out = out_loc.AsRegister<Register>();
2749 if (index.IsConstant()) {
2750 size_t offset =
2751 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2752 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002753 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2754 __ Addu(TMP, index.AsRegister<Register>(), obj);
2755 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002756 } else {
2757 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2758 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2759 }
2760 break;
2761 }
2762
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002763 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002764 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002765 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002766 if (index.IsConstant()) {
2767 size_t offset =
2768 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002769 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002770 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2771 __ Addu(TMP, index.AsRegister<Register>(), obj);
2772 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002773 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002774 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002775 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002776 }
2777 break;
2778 }
2779
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002780 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002781 static_assert(
2782 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2783 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2784 // /* HeapReference<Object> */ out =
2785 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2786 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002787 bool temp_needed = index.IsConstant()
2788 ? !kBakerReadBarrierThunksEnableForFields
2789 : !kBakerReadBarrierThunksEnableForArrays;
2790 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002791 // Note that a potential implicit null check is handled in this
2792 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002793 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2794 if (index.IsConstant()) {
2795 // Array load with a constant index can be treated as a field load.
2796 size_t offset =
2797 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2798 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2799 out_loc,
2800 obj,
2801 offset,
2802 temp,
2803 /* needs_null_check */ false);
2804 } else {
2805 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2806 out_loc,
2807 obj,
2808 data_offset,
2809 index,
2810 temp,
2811 /* needs_null_check */ false);
2812 }
Alexey Frunze15958152017-02-09 19:08:30 -08002813 } else {
2814 Register out = out_loc.AsRegister<Register>();
2815 if (index.IsConstant()) {
2816 size_t offset =
2817 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2818 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2819 // If read barriers are enabled, emit read barriers other than
2820 // Baker's using a slow path (and also unpoison the loaded
2821 // reference, if heap poisoning is enabled).
2822 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2823 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002824 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002825 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2826 // If read barriers are enabled, emit read barriers other than
2827 // Baker's using a slow path (and also unpoison the loaded
2828 // reference, if heap poisoning is enabled).
2829 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2830 out_loc,
2831 out_loc,
2832 obj_loc,
2833 data_offset,
2834 index);
2835 }
2836 }
2837 break;
2838 }
2839
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002840 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002841 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002842 if (index.IsConstant()) {
2843 size_t offset =
2844 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002845 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002846 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2847 __ Addu(TMP, index.AsRegister<Register>(), obj);
2848 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002849 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002850 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002851 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002852 }
2853 break;
2854 }
2855
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002856 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002857 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002858 if (index.IsConstant()) {
2859 size_t offset =
2860 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002861 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002862 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2863 __ Addu(TMP, index.AsRegister<Register>(), obj);
2864 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002865 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002866 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002867 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002868 }
2869 break;
2870 }
2871
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002872 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002873 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002874 if (index.IsConstant()) {
2875 size_t offset =
2876 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002877 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002878 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2879 __ Addu(TMP, index.AsRegister<Register>(), obj);
2880 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002881 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002882 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002883 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002884 }
2885 break;
2886 }
2887
Aart Bik66c158e2018-01-31 12:55:04 -08002888 case DataType::Type::kUint32:
2889 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002891 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2892 UNREACHABLE();
2893 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002894}
2895
2896void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002897 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002898 locations->SetInAt(0, Location::RequiresRegister());
2899 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2900}
2901
2902void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2903 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002904 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002905 Register obj = locations->InAt(0).AsRegister<Register>();
2906 Register out = locations->Out().AsRegister<Register>();
2907 __ LoadFromOffset(kLoadWord, out, obj, offset);
2908 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002909 // Mask out compression flag from String's array length.
2910 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2911 __ Srl(out, out, 1u);
2912 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002913}
2914
Alexey Frunzef58b2482016-09-02 22:14:06 -07002915Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2916 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2917 ? Location::ConstantLocation(instruction->AsConstant())
2918 : Location::RequiresRegister();
2919}
2920
2921Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2922 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2923 // We can store a non-zero float or double constant without first loading it into the FPU,
2924 // but we should only prefer this if the constant has a single use.
2925 if (instruction->IsConstant() &&
2926 (instruction->AsConstant()->IsZeroBitPattern() ||
2927 instruction->GetUses().HasExactlyOneElement())) {
2928 return Location::ConstantLocation(instruction->AsConstant());
2929 // Otherwise fall through and require an FPU register for the constant.
2930 }
2931 return Location::RequiresFpuRegister();
2932}
2933
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002934void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002935 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002936
2937 bool needs_write_barrier =
2938 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2939 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2940
Vladimir Markoca6fff82017-10-03 14:49:14 +01002941 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002942 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002943 may_need_runtime_call_for_type_check ?
2944 LocationSummary::kCallOnSlowPath :
2945 LocationSummary::kNoCall);
2946
2947 locations->SetInAt(0, Location::RequiresRegister());
2948 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002949 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002950 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002951 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002952 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2953 }
2954 if (needs_write_barrier) {
2955 // Temporary register for the write barrier.
2956 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002957 }
2958}
2959
2960void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2961 LocationSummary* locations = instruction->GetLocations();
2962 Register obj = locations->InAt(0).AsRegister<Register>();
2963 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002964 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002965 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002966 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002967 bool needs_write_barrier =
2968 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002969 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002970 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002971
2972 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002973 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002974 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002975 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002976 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002977 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002978 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002979 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002980 __ Addu(base_reg, obj, index.AsRegister<Register>());
2981 }
2982 if (value_location.IsConstant()) {
2983 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2984 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2985 } else {
2986 Register value = value_location.AsRegister<Register>();
2987 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002988 }
2989 break;
2990 }
2991
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002992 case DataType::Type::kUint16:
2993 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002994 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002995 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002996 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002997 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2998 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002999 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003000 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003001 }
3002 if (value_location.IsConstant()) {
3003 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3004 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
3005 } else {
3006 Register value = value_location.AsRegister<Register>();
3007 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003008 }
3009 break;
3010 }
3011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08003013 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3014 if (index.IsConstant()) {
3015 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003016 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3017 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08003018 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003019 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003020 }
3021 if (value_location.IsConstant()) {
3022 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3023 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3024 } else {
3025 Register value = value_location.AsRegister<Register>();
3026 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3027 }
3028 break;
3029 }
3030
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003031 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08003032 if (value_location.IsConstant()) {
3033 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003034 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003035 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003036 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003037 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003038 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003039 }
Alexey Frunze15958152017-02-09 19:08:30 -08003040 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3041 DCHECK_EQ(value, 0);
3042 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3043 DCHECK(!needs_write_barrier);
3044 DCHECK(!may_need_runtime_call_for_type_check);
3045 break;
3046 }
3047
3048 DCHECK(needs_write_barrier);
3049 Register value = value_location.AsRegister<Register>();
3050 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3051 Register temp2 = TMP; // Doesn't need to survive slow path.
3052 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3053 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3054 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3055 MipsLabel done;
3056 SlowPathCodeMIPS* slow_path = nullptr;
3057
3058 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003059 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003060 codegen_->AddSlowPath(slow_path);
3061 if (instruction->GetValueCanBeNull()) {
3062 MipsLabel non_zero;
3063 __ Bnez(value, &non_zero);
3064 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3065 if (index.IsConstant()) {
3066 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003067 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3068 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003069 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003070 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003071 }
Alexey Frunze15958152017-02-09 19:08:30 -08003072 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3073 __ B(&done);
3074 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003075 }
Alexey Frunze15958152017-02-09 19:08:30 -08003076
3077 // Note that when read barriers are enabled, the type checks
3078 // are performed without read barriers. This is fine, even in
3079 // the case where a class object is in the from-space after
3080 // the flip, as a comparison involving such a type would not
3081 // produce a false positive; it may of course produce a false
3082 // negative, in which case we would take the ArraySet slow
3083 // path.
3084
3085 // /* HeapReference<Class> */ temp1 = obj->klass_
3086 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3087 __ MaybeUnpoisonHeapReference(temp1);
3088
3089 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3090 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3091 // /* HeapReference<Class> */ temp2 = value->klass_
3092 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3093 // If heap poisoning is enabled, no need to unpoison `temp1`
3094 // nor `temp2`, as we are comparing two poisoned references.
3095
3096 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3097 MipsLabel do_put;
3098 __ Beq(temp1, temp2, &do_put);
3099 // If heap poisoning is enabled, the `temp1` reference has
3100 // not been unpoisoned yet; unpoison it now.
3101 __ MaybeUnpoisonHeapReference(temp1);
3102
3103 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3104 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3105 // If heap poisoning is enabled, no need to unpoison
3106 // `temp1`, as we are comparing against null below.
3107 __ Bnez(temp1, slow_path->GetEntryLabel());
3108 __ Bind(&do_put);
3109 } else {
3110 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3111 }
3112 }
3113
3114 Register source = value;
3115 if (kPoisonHeapReferences) {
3116 // Note that in the case where `value` is a null reference,
3117 // we do not enter this block, as a null reference does not
3118 // need poisoning.
3119 __ Move(temp1, value);
3120 __ PoisonHeapReference(temp1);
3121 source = temp1;
3122 }
3123
3124 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3125 if (index.IsConstant()) {
3126 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003127 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003128 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003129 }
3130 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3131
3132 if (!may_need_runtime_call_for_type_check) {
3133 codegen_->MaybeRecordImplicitNullCheck(instruction);
3134 }
3135
3136 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3137
3138 if (done.IsLinked()) {
3139 __ Bind(&done);
3140 }
3141
3142 if (slow_path != nullptr) {
3143 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003144 }
3145 break;
3146 }
3147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003149 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003150 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003151 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003152 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3153 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003154 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003155 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003156 }
3157 if (value_location.IsConstant()) {
3158 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3159 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3160 } else {
3161 Register value = value_location.AsRegisterPairLow<Register>();
3162 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003163 }
3164 break;
3165 }
3166
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003167 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003168 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003169 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003170 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003171 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3172 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003173 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003174 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003175 }
3176 if (value_location.IsConstant()) {
3177 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3178 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3179 } else {
3180 FRegister value = value_location.AsFpuRegister<FRegister>();
3181 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003182 }
3183 break;
3184 }
3185
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003186 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003187 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003188 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003189 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003190 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3191 __ Addu(base_reg, index.AsRegister<Register>(), obj);
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_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003194 }
3195 if (value_location.IsConstant()) {
3196 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3197 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3198 } else {
3199 FRegister value = value_location.AsFpuRegister<FRegister>();
3200 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003201 }
3202 break;
3203 }
3204
Aart Bik66c158e2018-01-31 12:55:04 -08003205 case DataType::Type::kUint32:
3206 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003207 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003208 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3209 UNREACHABLE();
3210 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003211}
3212
Lena Djokica2901602017-09-21 13:50:52 +02003213void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3214 HIntermediateArrayAddressIndex* instruction) {
3215 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003216 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003217
3218 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3219
3220 locations->SetInAt(0, Location::RequiresRegister());
3221 locations->SetInAt(1, Location::ConstantLocation(shift));
3222 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3223}
3224
3225void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3226 HIntermediateArrayAddressIndex* instruction) {
3227 LocationSummary* locations = instruction->GetLocations();
3228 Register index_reg = locations->InAt(0).AsRegister<Register>();
3229 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3230 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3231}
3232
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003233void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003234 RegisterSet caller_saves = RegisterSet::Empty();
3235 InvokeRuntimeCallingConvention calling_convention;
3236 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3237 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3238 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003239
3240 HInstruction* index = instruction->InputAt(0);
3241 HInstruction* length = instruction->InputAt(1);
3242
3243 bool const_index = false;
3244 bool const_length = false;
3245
3246 if (index->IsConstant()) {
3247 if (length->IsConstant()) {
3248 const_index = true;
3249 const_length = true;
3250 } else {
3251 int32_t index_value = index->AsIntConstant()->GetValue();
3252 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3253 const_index = true;
3254 }
3255 }
3256 } else if (length->IsConstant()) {
3257 int32_t length_value = length->AsIntConstant()->GetValue();
3258 if (IsUint<15>(length_value)) {
3259 const_length = true;
3260 }
3261 }
3262
3263 locations->SetInAt(0, const_index
3264 ? Location::ConstantLocation(index->AsConstant())
3265 : Location::RequiresRegister());
3266 locations->SetInAt(1, const_length
3267 ? Location::ConstantLocation(length->AsConstant())
3268 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003269}
3270
3271void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3272 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003273 Location index_loc = locations->InAt(0);
3274 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003275
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003276 if (length_loc.IsConstant()) {
3277 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3278 if (index_loc.IsConstant()) {
3279 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3280 if (index < 0 || index >= length) {
3281 BoundsCheckSlowPathMIPS* slow_path =
3282 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3283 codegen_->AddSlowPath(slow_path);
3284 __ B(slow_path->GetEntryLabel());
3285 } else {
3286 // Nothing to be done.
3287 }
3288 return;
3289 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003290
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003291 BoundsCheckSlowPathMIPS* slow_path =
3292 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3293 codegen_->AddSlowPath(slow_path);
3294 Register index = index_loc.AsRegister<Register>();
3295 if (length == 0) {
3296 __ B(slow_path->GetEntryLabel());
3297 } else if (length == 1) {
3298 __ Bnez(index, slow_path->GetEntryLabel());
3299 } else {
3300 DCHECK(IsUint<15>(length)) << length;
3301 __ Sltiu(TMP, index, length);
3302 __ Beqz(TMP, slow_path->GetEntryLabel());
3303 }
3304 } else {
3305 Register length = length_loc.AsRegister<Register>();
3306 BoundsCheckSlowPathMIPS* slow_path =
3307 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3308 codegen_->AddSlowPath(slow_path);
3309 if (index_loc.IsConstant()) {
3310 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3311 if (index < 0) {
3312 __ B(slow_path->GetEntryLabel());
3313 } else if (index == 0) {
3314 __ Blez(length, slow_path->GetEntryLabel());
3315 } else {
3316 DCHECK(IsInt<16>(index + 1)) << index;
3317 __ Sltiu(TMP, length, index + 1);
3318 __ Bnez(TMP, slow_path->GetEntryLabel());
3319 }
3320 } else {
3321 Register index = index_loc.AsRegister<Register>();
3322 __ Bgeu(index, length, slow_path->GetEntryLabel());
3323 }
3324 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003325}
3326
Alexey Frunze15958152017-02-09 19:08:30 -08003327// Temp is used for read barrier.
3328static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3329 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003330 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003331 (kUseBakerReadBarrier ||
3332 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3333 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3334 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3335 return 1;
3336 }
3337 return 0;
3338}
3339
3340// Extra temp is used for read barrier.
3341static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3342 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3343}
3344
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003345void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003346 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003347 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003348 LocationSummary* locations =
3349 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003350 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003351 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3352 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3353 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3354 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3355 } else {
3356 locations->SetInAt(1, Location::RequiresRegister());
3357 }
Alexey Frunze15958152017-02-09 19:08:30 -08003358 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003359}
3360
3361void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003362 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003363 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003364 Location obj_loc = locations->InAt(0);
3365 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko175e7862018-03-27 09:03:13 +00003366 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08003367 Location temp_loc = locations->GetTemp(0);
3368 Register temp = temp_loc.AsRegister<Register>();
3369 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3370 DCHECK_LE(num_temps, 2u);
3371 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003372 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3373 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3374 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3375 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3376 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3377 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3378 const uint32_t object_array_data_offset =
3379 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3380 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003381
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003382 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003383 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003384 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3385 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003386 codegen_->AddSlowPath(slow_path);
3387
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003388 // Avoid this check if we know `obj` is not null.
3389 if (instruction->MustDoNullCheck()) {
3390 __ Beqz(obj, &done);
3391 }
3392
3393 switch (type_check_kind) {
3394 case TypeCheckKind::kExactCheck:
3395 case TypeCheckKind::kArrayCheck: {
3396 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003397 GenerateReferenceLoadTwoRegisters(instruction,
3398 temp_loc,
3399 obj_loc,
3400 class_offset,
3401 maybe_temp2_loc,
3402 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003403 // Jump to slow path for throwing the exception or doing a
3404 // more involved array check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003405 __ Bne(temp, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003406 break;
3407 }
3408
3409 case TypeCheckKind::kAbstractClassCheck: {
3410 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003411 GenerateReferenceLoadTwoRegisters(instruction,
3412 temp_loc,
3413 obj_loc,
3414 class_offset,
3415 maybe_temp2_loc,
3416 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003417 // If the class is abstract, we eagerly fetch the super class of the
3418 // object to avoid doing a comparison we know will fail.
3419 MipsLabel loop;
3420 __ Bind(&loop);
3421 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003422 GenerateReferenceLoadOneRegister(instruction,
3423 temp_loc,
3424 super_offset,
3425 maybe_temp2_loc,
3426 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003427 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3428 // exception.
3429 __ Beqz(temp, slow_path->GetEntryLabel());
3430 // Otherwise, compare the classes.
Vladimir Marko175e7862018-03-27 09:03:13 +00003431 __ Bne(temp, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003432 break;
3433 }
3434
3435 case TypeCheckKind::kClassHierarchyCheck: {
3436 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003437 GenerateReferenceLoadTwoRegisters(instruction,
3438 temp_loc,
3439 obj_loc,
3440 class_offset,
3441 maybe_temp2_loc,
3442 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003443 // Walk over the class hierarchy to find a match.
3444 MipsLabel loop;
3445 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00003446 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003447 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003448 GenerateReferenceLoadOneRegister(instruction,
3449 temp_loc,
3450 super_offset,
3451 maybe_temp2_loc,
3452 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003453 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3454 // exception. Otherwise, jump to the beginning of the loop.
3455 __ Bnez(temp, &loop);
3456 __ B(slow_path->GetEntryLabel());
3457 break;
3458 }
3459
3460 case TypeCheckKind::kArrayObjectCheck: {
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 // Do an exact check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003469 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003470 // Otherwise, we need to check that the object's class is a non-primitive array.
3471 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003472 GenerateReferenceLoadOneRegister(instruction,
3473 temp_loc,
3474 component_offset,
3475 maybe_temp2_loc,
3476 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003477 // If the component type is null, jump to the slow path to throw the exception.
3478 __ Beqz(temp, slow_path->GetEntryLabel());
3479 // Otherwise, the object is indeed an array, further check that this component
3480 // type is not a primitive type.
3481 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3482 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3483 __ Bnez(temp, slow_path->GetEntryLabel());
3484 break;
3485 }
3486
3487 case TypeCheckKind::kUnresolvedCheck:
3488 // We always go into the type check slow path for the unresolved check case.
3489 // We cannot directly call the CheckCast runtime entry point
3490 // without resorting to a type checking slow path here (i.e. by
3491 // calling InvokeRuntime directly), as it would require to
3492 // assign fixed registers for the inputs of this HInstanceOf
3493 // instruction (following the runtime calling convention), which
3494 // might be cluttered by the potential first read barrier
3495 // emission at the beginning of this method.
3496 __ B(slow_path->GetEntryLabel());
3497 break;
3498
3499 case TypeCheckKind::kInterfaceCheck: {
3500 // Avoid read barriers to improve performance of the fast path. We can not get false
3501 // positives by doing this.
3502 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003503 GenerateReferenceLoadTwoRegisters(instruction,
3504 temp_loc,
3505 obj_loc,
3506 class_offset,
3507 maybe_temp2_loc,
3508 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003509 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003510 GenerateReferenceLoadTwoRegisters(instruction,
3511 temp_loc,
3512 temp_loc,
3513 iftable_offset,
3514 maybe_temp2_loc,
3515 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003516 // Iftable is never null.
3517 __ Lw(TMP, temp, array_length_offset);
3518 // Loop through the iftable and check if any class matches.
3519 MipsLabel loop;
3520 __ Bind(&loop);
3521 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3522 __ Beqz(TMP, slow_path->GetEntryLabel());
3523 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3524 __ MaybeUnpoisonHeapReference(AT);
3525 // Go to next interface.
3526 __ Addiu(TMP, TMP, -2);
3527 // Compare the classes and continue the loop if they do not match.
Vladimir Marko175e7862018-03-27 09:03:13 +00003528 __ Bne(AT, cls.AsRegister<Register>(), &loop);
3529 break;
3530 }
3531
3532 case TypeCheckKind::kBitstringCheck: {
3533 // /* HeapReference<Class> */ temp = obj->klass_
3534 GenerateReferenceLoadTwoRegisters(instruction,
3535 temp_loc,
3536 obj_loc,
3537 class_offset,
3538 maybe_temp2_loc,
3539 kWithoutReadBarrier);
3540
3541 GenerateBitstringTypeCheckCompare(instruction, temp);
3542 __ Bnez(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003543 break;
3544 }
3545 }
3546
3547 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003548 __ Bind(slow_path->GetExitLabel());
3549}
3550
3551void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3552 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003553 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003554 locations->SetInAt(0, Location::RequiresRegister());
3555 if (check->HasUses()) {
3556 locations->SetOut(Location::SameAsFirstInput());
3557 }
3558}
3559
3560void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3561 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003562 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003563 check->GetLoadClass(),
3564 check,
3565 check->GetDexPc(),
3566 true);
3567 codegen_->AddSlowPath(slow_path);
3568 GenerateClassInitializationCheck(slow_path,
3569 check->GetLocations()->InAt(0).AsRegister<Register>());
3570}
3571
3572void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003573 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003574
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003575 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003576 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003577
3578 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003579 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003580 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003581 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003582 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003583 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003584 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003585 locations->SetInAt(0, Location::RequiresRegister());
3586 locations->SetInAt(1, Location::RequiresRegister());
3587 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3588 break;
3589
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003590 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003591 locations->SetInAt(0, Location::RequiresRegister());
3592 locations->SetInAt(1, Location::RequiresRegister());
3593 // Output overlaps because it is written before doing the low comparison.
3594 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3595 break;
3596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003597 case DataType::Type::kFloat32:
3598 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003599 locations->SetInAt(0, Location::RequiresFpuRegister());
3600 locations->SetInAt(1, Location::RequiresFpuRegister());
3601 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003602 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003603
3604 default:
3605 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3606 }
3607}
3608
3609void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3610 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003611 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003612 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003613 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003614
3615 // 0 if: left == right
3616 // 1 if: left > right
3617 // -1 if: left < right
3618 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003619 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003620 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003621 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003622 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003623 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003624 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003625 Register lhs = locations->InAt(0).AsRegister<Register>();
3626 Register rhs = locations->InAt(1).AsRegister<Register>();
3627 __ Slt(TMP, lhs, rhs);
3628 __ Slt(res, rhs, lhs);
3629 __ Subu(res, res, TMP);
3630 break;
3631 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003632 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003633 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003634 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3635 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3636 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3637 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3638 // TODO: more efficient (direct) comparison with a constant.
3639 __ Slt(TMP, lhs_high, rhs_high);
3640 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3641 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3642 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3643 __ Sltu(TMP, lhs_low, rhs_low);
3644 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3645 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3646 __ Bind(&done);
3647 break;
3648 }
3649
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003650 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003651 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003652 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3653 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3654 MipsLabel done;
3655 if (isR6) {
3656 __ CmpEqS(FTMP, lhs, rhs);
3657 __ LoadConst32(res, 0);
3658 __ Bc1nez(FTMP, &done);
3659 if (gt_bias) {
3660 __ CmpLtS(FTMP, lhs, rhs);
3661 __ LoadConst32(res, -1);
3662 __ Bc1nez(FTMP, &done);
3663 __ LoadConst32(res, 1);
3664 } else {
3665 __ CmpLtS(FTMP, rhs, lhs);
3666 __ LoadConst32(res, 1);
3667 __ Bc1nez(FTMP, &done);
3668 __ LoadConst32(res, -1);
3669 }
3670 } else {
3671 if (gt_bias) {
3672 __ ColtS(0, lhs, rhs);
3673 __ LoadConst32(res, -1);
3674 __ Bc1t(0, &done);
3675 __ CeqS(0, lhs, rhs);
3676 __ LoadConst32(res, 1);
3677 __ Movt(res, ZERO, 0);
3678 } else {
3679 __ ColtS(0, rhs, lhs);
3680 __ LoadConst32(res, 1);
3681 __ Bc1t(0, &done);
3682 __ CeqS(0, lhs, rhs);
3683 __ LoadConst32(res, -1);
3684 __ Movt(res, ZERO, 0);
3685 }
3686 }
3687 __ Bind(&done);
3688 break;
3689 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003690 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003691 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003692 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3693 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3694 MipsLabel done;
3695 if (isR6) {
3696 __ CmpEqD(FTMP, lhs, rhs);
3697 __ LoadConst32(res, 0);
3698 __ Bc1nez(FTMP, &done);
3699 if (gt_bias) {
3700 __ CmpLtD(FTMP, lhs, rhs);
3701 __ LoadConst32(res, -1);
3702 __ Bc1nez(FTMP, &done);
3703 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003704 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003705 __ CmpLtD(FTMP, rhs, lhs);
3706 __ LoadConst32(res, 1);
3707 __ Bc1nez(FTMP, &done);
3708 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003709 }
3710 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003711 if (gt_bias) {
3712 __ ColtD(0, lhs, rhs);
3713 __ LoadConst32(res, -1);
3714 __ Bc1t(0, &done);
3715 __ CeqD(0, lhs, rhs);
3716 __ LoadConst32(res, 1);
3717 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003718 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003719 __ ColtD(0, rhs, lhs);
3720 __ LoadConst32(res, 1);
3721 __ Bc1t(0, &done);
3722 __ CeqD(0, lhs, rhs);
3723 __ LoadConst32(res, -1);
3724 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003725 }
3726 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003727 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003728 break;
3729 }
3730
3731 default:
3732 LOG(FATAL) << "Unimplemented compare type " << in_type;
3733 }
3734}
3735
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003736void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003737 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003738 switch (instruction->InputAt(0)->GetType()) {
3739 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003740 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003741 locations->SetInAt(0, Location::RequiresRegister());
3742 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3743 break;
3744
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003745 case DataType::Type::kFloat32:
3746 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003747 locations->SetInAt(0, Location::RequiresFpuRegister());
3748 locations->SetInAt(1, Location::RequiresFpuRegister());
3749 break;
3750 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003751 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003752 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3753 }
3754}
3755
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003756void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003757 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003758 return;
3759 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003760
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003761 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003762 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003763
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003764 switch (type) {
3765 default:
3766 // Integer case.
3767 GenerateIntCompare(instruction->GetCondition(), locations);
3768 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003769
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003770 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003771 GenerateLongCompare(instruction->GetCondition(), locations);
3772 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003773
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003774 case DataType::Type::kFloat32:
3775 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003776 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3777 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003778 }
3779}
3780
Alexey Frunze7e99e052015-11-24 19:28:01 -08003781void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3782 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003783
3784 LocationSummary* locations = instruction->GetLocations();
3785 Location second = locations->InAt(1);
3786 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003787 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003788 DCHECK(imm == 1 || imm == -1);
3789
Lena Djokic4b8025c2017-12-21 16:15:50 +01003790 if (instruction->GetResultType() == DataType::Type::kInt32) {
3791 Register out = locations->Out().AsRegister<Register>();
3792 Register dividend = locations->InAt(0).AsRegister<Register>();
3793
3794 if (instruction->IsRem()) {
3795 __ Move(out, ZERO);
3796 } else {
3797 if (imm == -1) {
3798 __ Subu(out, ZERO, dividend);
3799 } else if (out != dividend) {
3800 __ Move(out, dividend);
3801 }
3802 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003803 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003804 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3805 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3806 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3807 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3808 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3809
3810 if (instruction->IsRem()) {
3811 __ Move(out_high, ZERO);
3812 __ Move(out_low, ZERO);
3813 } else {
3814 if (imm == -1) {
3815 __ Subu(out_low, ZERO, in_low);
3816 __ Sltu(AT, ZERO, out_low);
3817 __ Subu(out_high, ZERO, in_high);
3818 __ Subu(out_high, out_high, AT);
3819 } else {
3820 __ Move(out_low, in_low);
3821 __ Move(out_high, in_high);
3822 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003823 }
3824 }
3825}
3826
3827void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3828 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003829
3830 LocationSummary* locations = instruction->GetLocations();
3831 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003832 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3833 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003834 DCHECK(second.IsConstant());
3835
Lena Djokic4b8025c2017-12-21 16:15:50 +01003836 if (instruction->GetResultType() == DataType::Type::kInt32) {
3837 Register out = locations->Out().AsRegister<Register>();
3838 Register dividend = locations->InAt(0).AsRegister<Register>();
3839 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3840 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3841 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003842
Lena Djokic4b8025c2017-12-21 16:15:50 +01003843 if (instruction->IsDiv()) {
3844 if (ctz_imm == 1) {
3845 // Fast path for division by +/-2, which is very common.
3846 __ Srl(TMP, dividend, 31);
3847 } else {
3848 __ Sra(TMP, dividend, 31);
3849 __ Srl(TMP, TMP, 32 - ctz_imm);
3850 }
3851 __ Addu(out, dividend, TMP);
3852 __ Sra(out, out, ctz_imm);
3853 if (imm < 0) {
3854 __ Subu(out, ZERO, out);
3855 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003856 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003857 if (ctz_imm == 1) {
3858 // Fast path for modulo +/-2, which is very common.
3859 __ Sra(TMP, dividend, 31);
3860 __ Subu(out, dividend, TMP);
3861 __ Andi(out, out, 1);
3862 __ Addu(out, out, TMP);
3863 } else {
3864 __ Sra(TMP, dividend, 31);
3865 __ Srl(TMP, TMP, 32 - ctz_imm);
3866 __ Addu(out, dividend, TMP);
3867 if (IsUint<16>(abs_imm - 1)) {
3868 __ Andi(out, out, abs_imm - 1);
3869 } else {
3870 if (is_r2_or_newer) {
3871 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3872 } else {
3873 __ Sll(out, out, 32 - ctz_imm);
3874 __ Srl(out, out, 32 - ctz_imm);
3875 }
3876 }
3877 __ Subu(out, out, TMP);
3878 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003879 }
3880 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003881 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3882 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3883 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3884 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3885 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3886 int64_t imm = Int64FromConstant(second.GetConstant());
3887 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3888 int ctz_imm = CTZ(abs_imm);
3889
3890 if (instruction->IsDiv()) {
3891 if (ctz_imm < 32) {
3892 if (ctz_imm == 1) {
3893 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003894 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003895 __ Sra(AT, in_high, 31);
3896 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003897 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003898 __ Addu(AT, AT, in_low);
3899 __ Sltu(TMP, AT, in_low);
3900 __ Addu(out_high, in_high, TMP);
3901 __ Srl(out_low, AT, ctz_imm);
3902 if (is_r2_or_newer) {
3903 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3904 __ Sra(out_high, out_high, ctz_imm);
3905 } else {
3906 __ Sll(AT, out_high, 32 - ctz_imm);
3907 __ Sra(out_high, out_high, ctz_imm);
3908 __ Or(out_low, out_low, AT);
3909 }
3910 if (imm < 0) {
3911 __ Subu(out_low, ZERO, out_low);
3912 __ Sltu(AT, ZERO, out_low);
3913 __ Subu(out_high, ZERO, out_high);
3914 __ Subu(out_high, out_high, AT);
3915 }
3916 } else if (ctz_imm == 32) {
3917 __ Sra(AT, in_high, 31);
3918 __ Addu(AT, AT, in_low);
3919 __ Sltu(AT, AT, in_low);
3920 __ Addu(out_low, in_high, AT);
3921 if (imm < 0) {
3922 __ Srl(TMP, out_low, 31);
3923 __ Subu(out_low, ZERO, out_low);
3924 __ Sltu(AT, ZERO, out_low);
3925 __ Subu(out_high, TMP, AT);
3926 } else {
3927 __ Sra(out_high, out_low, 31);
3928 }
3929 } else if (ctz_imm < 63) {
3930 __ Sra(AT, in_high, 31);
3931 __ Srl(TMP, AT, 64 - ctz_imm);
3932 __ Addu(AT, AT, in_low);
3933 __ Sltu(AT, AT, in_low);
3934 __ Addu(out_low, in_high, AT);
3935 __ Addu(out_low, out_low, TMP);
3936 __ Sra(out_low, out_low, ctz_imm - 32);
3937 if (imm < 0) {
3938 __ Subu(out_low, ZERO, out_low);
3939 }
3940 __ Sra(out_high, out_low, 31);
3941 } else {
3942 DCHECK_LT(imm, 0);
3943 if (is_r6) {
3944 __ Aui(AT, in_high, 0x8000);
3945 } else {
3946 __ Lui(AT, 0x8000);
3947 __ Xor(AT, AT, in_high);
3948 }
3949 __ Or(AT, AT, in_low);
3950 __ Sltiu(out_low, AT, 1);
3951 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003952 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003953 } else {
3954 if ((ctz_imm == 1) && !is_r6) {
3955 __ Andi(AT, in_low, 1);
3956 __ Sll(TMP, in_low, 31);
3957 __ And(TMP, in_high, TMP);
3958 __ Sra(out_high, TMP, 31);
3959 __ Or(out_low, out_high, AT);
3960 } else if (ctz_imm < 32) {
3961 __ Sra(AT, in_high, 31);
3962 if (ctz_imm <= 16) {
3963 __ Andi(out_low, in_low, abs_imm - 1);
3964 } else if (is_r2_or_newer) {
3965 __ Ext(out_low, in_low, 0, ctz_imm);
3966 } else {
3967 __ Sll(out_low, in_low, 32 - ctz_imm);
3968 __ Srl(out_low, out_low, 32 - ctz_imm);
3969 }
3970 if (is_r6) {
3971 __ Selnez(out_high, AT, out_low);
3972 } else {
3973 __ Movz(AT, ZERO, out_low);
3974 __ Move(out_high, AT);
3975 }
3976 if (is_r2_or_newer) {
3977 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
3978 } else {
3979 __ Sll(AT, out_high, ctz_imm);
3980 __ Or(out_low, out_low, AT);
3981 }
3982 } else if (ctz_imm == 32) {
3983 __ Sra(AT, in_high, 31);
3984 __ Move(out_low, in_low);
3985 if (is_r6) {
3986 __ Selnez(out_high, AT, out_low);
3987 } else {
3988 __ Movz(AT, ZERO, out_low);
3989 __ Move(out_high, AT);
3990 }
3991 } else if (ctz_imm < 63) {
3992 __ Sra(AT, in_high, 31);
3993 __ Move(TMP, in_low);
3994 if (ctz_imm - 32 <= 16) {
3995 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
3996 } else if (is_r2_or_newer) {
3997 __ Ext(out_high, in_high, 0, ctz_imm - 32);
3998 } else {
3999 __ Sll(out_high, in_high, 64 - ctz_imm);
4000 __ Srl(out_high, out_high, 64 - ctz_imm);
4001 }
4002 __ Move(out_low, TMP);
4003 __ Or(TMP, TMP, out_high);
4004 if (is_r6) {
4005 __ Selnez(AT, AT, TMP);
4006 } else {
4007 __ Movz(AT, ZERO, TMP);
4008 }
4009 if (is_r2_or_newer) {
4010 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
4011 } else {
4012 __ Sll(AT, AT, ctz_imm - 32);
4013 __ Or(out_high, out_high, AT);
4014 }
4015 } else {
4016 if (is_r6) {
4017 __ Aui(AT, in_high, 0x8000);
4018 } else {
4019 __ Lui(AT, 0x8000);
4020 __ Xor(AT, AT, in_high);
4021 }
4022 __ Or(AT, AT, in_low);
4023 __ Sltiu(AT, AT, 1);
4024 __ Sll(AT, AT, 31);
4025 __ Move(out_low, in_low);
4026 __ Xor(out_high, in_high, AT);
4027 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08004028 }
4029 }
4030}
4031
4032void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4033 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004034 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004035
4036 LocationSummary* locations = instruction->GetLocations();
4037 Location second = locations->InAt(1);
4038 DCHECK(second.IsConstant());
4039
4040 Register out = locations->Out().AsRegister<Register>();
4041 Register dividend = locations->InAt(0).AsRegister<Register>();
4042 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4043
4044 int64_t magic;
4045 int shift;
4046 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4047
4048 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4049
4050 __ LoadConst32(TMP, magic);
4051 if (isR6) {
4052 __ MuhR6(TMP, dividend, TMP);
4053 } else {
4054 __ MultR2(dividend, TMP);
4055 __ Mfhi(TMP);
4056 }
4057 if (imm > 0 && magic < 0) {
4058 __ Addu(TMP, TMP, dividend);
4059 } else if (imm < 0 && magic > 0) {
4060 __ Subu(TMP, TMP, dividend);
4061 }
4062
4063 if (shift != 0) {
4064 __ Sra(TMP, TMP, shift);
4065 }
4066
4067 if (instruction->IsDiv()) {
4068 __ Sra(out, TMP, 31);
4069 __ Subu(out, TMP, out);
4070 } else {
4071 __ Sra(AT, TMP, 31);
4072 __ Subu(AT, TMP, AT);
4073 __ LoadConst32(TMP, imm);
4074 if (isR6) {
4075 __ MulR6(TMP, AT, TMP);
4076 } else {
4077 __ MulR2(TMP, AT, TMP);
4078 }
4079 __ Subu(out, dividend, TMP);
4080 }
4081}
4082
4083void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4084 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004086
4087 LocationSummary* locations = instruction->GetLocations();
4088 Register out = locations->Out().AsRegister<Register>();
4089 Location second = locations->InAt(1);
4090
4091 if (second.IsConstant()) {
4092 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4093 if (imm == 0) {
4094 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4095 } else if (imm == 1 || imm == -1) {
4096 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004097 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004098 DivRemByPowerOfTwo(instruction);
4099 } else {
4100 DCHECK(imm <= -2 || imm >= 2);
4101 GenerateDivRemWithAnyConstant(instruction);
4102 }
4103 } else {
4104 Register dividend = locations->InAt(0).AsRegister<Register>();
4105 Register divisor = second.AsRegister<Register>();
4106 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4107 if (instruction->IsDiv()) {
4108 if (isR6) {
4109 __ DivR6(out, dividend, divisor);
4110 } else {
4111 __ DivR2(out, dividend, divisor);
4112 }
4113 } else {
4114 if (isR6) {
4115 __ ModR6(out, dividend, divisor);
4116 } else {
4117 __ ModR2(out, dividend, divisor);
4118 }
4119 }
4120 }
4121}
4122
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004123void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004124 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004125 bool call_long_div = false;
4126 if (type == DataType::Type::kInt64) {
4127 if (div->InputAt(1)->IsConstant()) {
4128 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4129 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4130 } else {
4131 call_long_div = true;
4132 }
4133 }
4134 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004135 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004136 : LocationSummary::kNoCall;
4137
Vladimir Markoca6fff82017-10-03 14:49:14 +01004138 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004139
4140 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004141 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004142 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004143 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004144 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4145 break;
4146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004147 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004148 if (call_long_div) {
4149 InvokeRuntimeCallingConvention calling_convention;
4150 locations->SetInAt(0, Location::RegisterPairLocation(
4151 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4152 locations->SetInAt(1, Location::RegisterPairLocation(
4153 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4154 locations->SetOut(calling_convention.GetReturnLocation(type));
4155 } else {
4156 locations->SetInAt(0, Location::RequiresRegister());
4157 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4158 locations->SetOut(Location::RequiresRegister());
4159 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004160 break;
4161 }
4162
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004163 case DataType::Type::kFloat32:
4164 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004165 locations->SetInAt(0, Location::RequiresFpuRegister());
4166 locations->SetInAt(1, Location::RequiresFpuRegister());
4167 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4168 break;
4169
4170 default:
4171 LOG(FATAL) << "Unexpected div type " << type;
4172 }
4173}
4174
4175void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004176 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004177 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004178
4179 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004180 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004181 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004182 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004183 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004184 if (locations->InAt(1).IsConstant()) {
4185 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4186 if (imm == 0) {
4187 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4188 } else if (imm == 1 || imm == -1) {
4189 DivRemOneOrMinusOne(instruction);
4190 } else {
4191 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4192 DivRemByPowerOfTwo(instruction);
4193 }
4194 } else {
4195 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4196 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4197 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004198 break;
4199 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004200 case DataType::Type::kFloat32:
4201 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004202 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4203 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4204 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004205 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004206 __ DivS(dst, lhs, rhs);
4207 } else {
4208 __ DivD(dst, lhs, rhs);
4209 }
4210 break;
4211 }
4212 default:
4213 LOG(FATAL) << "Unexpected div type " << type;
4214 }
4215}
4216
4217void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004218 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004219 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004220}
4221
4222void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004223 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004224 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004225 codegen_->AddSlowPath(slow_path);
4226 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004227 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004228
4229 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004230 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004231 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004232 case DataType::Type::kInt8:
4233 case DataType::Type::kUint16:
4234 case DataType::Type::kInt16:
4235 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004236 if (value.IsConstant()) {
4237 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4238 __ B(slow_path->GetEntryLabel());
4239 } else {
4240 // A division by a non-null constant is valid. We don't need to perform
4241 // any check, so simply fall through.
4242 }
4243 } else {
4244 DCHECK(value.IsRegister()) << value;
4245 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4246 }
4247 break;
4248 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004249 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004250 if (value.IsConstant()) {
4251 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4252 __ B(slow_path->GetEntryLabel());
4253 } else {
4254 // A division by a non-null constant is valid. We don't need to perform
4255 // any check, so simply fall through.
4256 }
4257 } else {
4258 DCHECK(value.IsRegisterPair()) << value;
4259 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4260 __ Beqz(TMP, slow_path->GetEntryLabel());
4261 }
4262 break;
4263 }
4264 default:
4265 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4266 }
4267}
4268
4269void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4270 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004271 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004272 locations->SetOut(Location::ConstantLocation(constant));
4273}
4274
4275void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4276 // Will be generated at use site.
4277}
4278
4279void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4280 exit->SetLocations(nullptr);
4281}
4282
4283void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4284}
4285
4286void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4287 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004288 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004289 locations->SetOut(Location::ConstantLocation(constant));
4290}
4291
4292void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4293 // Will be generated at use site.
4294}
4295
4296void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4297 got->SetLocations(nullptr);
4298}
4299
4300void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004301 if (successor->IsExitBlock()) {
4302 DCHECK(got->GetPrevious()->AlwaysThrows());
4303 return; // no code needed
4304 }
4305
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004306 HBasicBlock* block = got->GetBlock();
4307 HInstruction* previous = got->GetPrevious();
4308 HLoopInformation* info = block->GetLoopInformation();
4309
4310 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004311 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4312 __ Lw(AT, SP, kCurrentMethodStackOffset);
4313 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4314 __ Addiu(TMP, TMP, 1);
4315 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4316 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004317 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4318 return;
4319 }
4320 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4321 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4322 }
4323 if (!codegen_->GoesToNextBlock(block, successor)) {
4324 __ B(codegen_->GetLabelOf(successor));
4325 }
4326}
4327
4328void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4329 HandleGoto(got, got->GetSuccessor());
4330}
4331
4332void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4333 try_boundary->SetLocations(nullptr);
4334}
4335
4336void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4337 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4338 if (!successor->IsExitBlock()) {
4339 HandleGoto(try_boundary, successor);
4340 }
4341}
4342
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004343void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4344 LocationSummary* locations) {
4345 Register dst = locations->Out().AsRegister<Register>();
4346 Register lhs = locations->InAt(0).AsRegister<Register>();
4347 Location rhs_location = locations->InAt(1);
4348 Register rhs_reg = ZERO;
4349 int64_t rhs_imm = 0;
4350 bool use_imm = rhs_location.IsConstant();
4351 if (use_imm) {
4352 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4353 } else {
4354 rhs_reg = rhs_location.AsRegister<Register>();
4355 }
4356
4357 switch (cond) {
4358 case kCondEQ:
4359 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004360 if (use_imm && IsInt<16>(-rhs_imm)) {
4361 if (rhs_imm == 0) {
4362 if (cond == kCondEQ) {
4363 __ Sltiu(dst, lhs, 1);
4364 } else {
4365 __ Sltu(dst, ZERO, lhs);
4366 }
4367 } else {
4368 __ Addiu(dst, lhs, -rhs_imm);
4369 if (cond == kCondEQ) {
4370 __ Sltiu(dst, dst, 1);
4371 } else {
4372 __ Sltu(dst, ZERO, dst);
4373 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004374 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004375 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004376 if (use_imm && IsUint<16>(rhs_imm)) {
4377 __ Xori(dst, lhs, rhs_imm);
4378 } else {
4379 if (use_imm) {
4380 rhs_reg = TMP;
4381 __ LoadConst32(rhs_reg, rhs_imm);
4382 }
4383 __ Xor(dst, lhs, rhs_reg);
4384 }
4385 if (cond == kCondEQ) {
4386 __ Sltiu(dst, dst, 1);
4387 } else {
4388 __ Sltu(dst, ZERO, dst);
4389 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004390 }
4391 break;
4392
4393 case kCondLT:
4394 case kCondGE:
4395 if (use_imm && IsInt<16>(rhs_imm)) {
4396 __ Slti(dst, lhs, rhs_imm);
4397 } else {
4398 if (use_imm) {
4399 rhs_reg = TMP;
4400 __ LoadConst32(rhs_reg, rhs_imm);
4401 }
4402 __ Slt(dst, lhs, rhs_reg);
4403 }
4404 if (cond == kCondGE) {
4405 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4406 // only the slt instruction but no sge.
4407 __ Xori(dst, dst, 1);
4408 }
4409 break;
4410
4411 case kCondLE:
4412 case kCondGT:
4413 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4414 // Simulate lhs <= rhs via lhs < rhs + 1.
4415 __ Slti(dst, lhs, rhs_imm + 1);
4416 if (cond == kCondGT) {
4417 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4418 // only the slti instruction but no sgti.
4419 __ Xori(dst, dst, 1);
4420 }
4421 } else {
4422 if (use_imm) {
4423 rhs_reg = TMP;
4424 __ LoadConst32(rhs_reg, rhs_imm);
4425 }
4426 __ Slt(dst, rhs_reg, lhs);
4427 if (cond == kCondLE) {
4428 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4429 // only the slt instruction but no sle.
4430 __ Xori(dst, dst, 1);
4431 }
4432 }
4433 break;
4434
4435 case kCondB:
4436 case kCondAE:
4437 if (use_imm && IsInt<16>(rhs_imm)) {
4438 // Sltiu sign-extends its 16-bit immediate operand before
4439 // the comparison and thus lets us compare directly with
4440 // unsigned values in the ranges [0, 0x7fff] and
4441 // [0xffff8000, 0xffffffff].
4442 __ Sltiu(dst, lhs, rhs_imm);
4443 } else {
4444 if (use_imm) {
4445 rhs_reg = TMP;
4446 __ LoadConst32(rhs_reg, rhs_imm);
4447 }
4448 __ Sltu(dst, lhs, rhs_reg);
4449 }
4450 if (cond == kCondAE) {
4451 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4452 // only the sltu instruction but no sgeu.
4453 __ Xori(dst, dst, 1);
4454 }
4455 break;
4456
4457 case kCondBE:
4458 case kCondA:
4459 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4460 // Simulate lhs <= rhs via lhs < rhs + 1.
4461 // Note that this only works if rhs + 1 does not overflow
4462 // to 0, hence the check above.
4463 // Sltiu sign-extends its 16-bit immediate operand before
4464 // the comparison and thus lets us compare directly with
4465 // unsigned values in the ranges [0, 0x7fff] and
4466 // [0xffff8000, 0xffffffff].
4467 __ Sltiu(dst, lhs, rhs_imm + 1);
4468 if (cond == kCondA) {
4469 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4470 // only the sltiu instruction but no sgtiu.
4471 __ Xori(dst, dst, 1);
4472 }
4473 } else {
4474 if (use_imm) {
4475 rhs_reg = TMP;
4476 __ LoadConst32(rhs_reg, rhs_imm);
4477 }
4478 __ Sltu(dst, rhs_reg, lhs);
4479 if (cond == kCondBE) {
4480 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4481 // only the sltu instruction but no sleu.
4482 __ Xori(dst, dst, 1);
4483 }
4484 }
4485 break;
4486 }
4487}
4488
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004489bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4490 LocationSummary* input_locations,
4491 Register dst) {
4492 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4493 Location rhs_location = input_locations->InAt(1);
4494 Register rhs_reg = ZERO;
4495 int64_t rhs_imm = 0;
4496 bool use_imm = rhs_location.IsConstant();
4497 if (use_imm) {
4498 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4499 } else {
4500 rhs_reg = rhs_location.AsRegister<Register>();
4501 }
4502
4503 switch (cond) {
4504 case kCondEQ:
4505 case kCondNE:
4506 if (use_imm && IsInt<16>(-rhs_imm)) {
4507 __ Addiu(dst, lhs, -rhs_imm);
4508 } else if (use_imm && IsUint<16>(rhs_imm)) {
4509 __ Xori(dst, lhs, rhs_imm);
4510 } else {
4511 if (use_imm) {
4512 rhs_reg = TMP;
4513 __ LoadConst32(rhs_reg, rhs_imm);
4514 }
4515 __ Xor(dst, lhs, rhs_reg);
4516 }
4517 return (cond == kCondEQ);
4518
4519 case kCondLT:
4520 case kCondGE:
4521 if (use_imm && IsInt<16>(rhs_imm)) {
4522 __ Slti(dst, lhs, rhs_imm);
4523 } else {
4524 if (use_imm) {
4525 rhs_reg = TMP;
4526 __ LoadConst32(rhs_reg, rhs_imm);
4527 }
4528 __ Slt(dst, lhs, rhs_reg);
4529 }
4530 return (cond == kCondGE);
4531
4532 case kCondLE:
4533 case kCondGT:
4534 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4535 // Simulate lhs <= rhs via lhs < rhs + 1.
4536 __ Slti(dst, lhs, rhs_imm + 1);
4537 return (cond == kCondGT);
4538 } else {
4539 if (use_imm) {
4540 rhs_reg = TMP;
4541 __ LoadConst32(rhs_reg, rhs_imm);
4542 }
4543 __ Slt(dst, rhs_reg, lhs);
4544 return (cond == kCondLE);
4545 }
4546
4547 case kCondB:
4548 case kCondAE:
4549 if (use_imm && IsInt<16>(rhs_imm)) {
4550 // Sltiu sign-extends its 16-bit immediate operand before
4551 // the comparison and thus lets us compare directly with
4552 // unsigned values in the ranges [0, 0x7fff] and
4553 // [0xffff8000, 0xffffffff].
4554 __ Sltiu(dst, lhs, rhs_imm);
4555 } else {
4556 if (use_imm) {
4557 rhs_reg = TMP;
4558 __ LoadConst32(rhs_reg, rhs_imm);
4559 }
4560 __ Sltu(dst, lhs, rhs_reg);
4561 }
4562 return (cond == kCondAE);
4563
4564 case kCondBE:
4565 case kCondA:
4566 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4567 // Simulate lhs <= rhs via lhs < rhs + 1.
4568 // Note that this only works if rhs + 1 does not overflow
4569 // to 0, hence the check above.
4570 // Sltiu sign-extends its 16-bit immediate operand before
4571 // the comparison and thus lets us compare directly with
4572 // unsigned values in the ranges [0, 0x7fff] and
4573 // [0xffff8000, 0xffffffff].
4574 __ Sltiu(dst, lhs, rhs_imm + 1);
4575 return (cond == kCondA);
4576 } else {
4577 if (use_imm) {
4578 rhs_reg = TMP;
4579 __ LoadConst32(rhs_reg, rhs_imm);
4580 }
4581 __ Sltu(dst, rhs_reg, lhs);
4582 return (cond == kCondBE);
4583 }
4584 }
4585}
4586
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004587void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4588 LocationSummary* locations,
4589 MipsLabel* label) {
4590 Register lhs = locations->InAt(0).AsRegister<Register>();
4591 Location rhs_location = locations->InAt(1);
4592 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004593 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004594 bool use_imm = rhs_location.IsConstant();
4595 if (use_imm) {
4596 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4597 } else {
4598 rhs_reg = rhs_location.AsRegister<Register>();
4599 }
4600
4601 if (use_imm && rhs_imm == 0) {
4602 switch (cond) {
4603 case kCondEQ:
4604 case kCondBE: // <= 0 if zero
4605 __ Beqz(lhs, label);
4606 break;
4607 case kCondNE:
4608 case kCondA: // > 0 if non-zero
4609 __ Bnez(lhs, label);
4610 break;
4611 case kCondLT:
4612 __ Bltz(lhs, label);
4613 break;
4614 case kCondGE:
4615 __ Bgez(lhs, label);
4616 break;
4617 case kCondLE:
4618 __ Blez(lhs, label);
4619 break;
4620 case kCondGT:
4621 __ Bgtz(lhs, label);
4622 break;
4623 case kCondB: // always false
4624 break;
4625 case kCondAE: // always true
4626 __ B(label);
4627 break;
4628 }
4629 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004630 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4631 if (isR6 || !use_imm) {
4632 if (use_imm) {
4633 rhs_reg = TMP;
4634 __ LoadConst32(rhs_reg, rhs_imm);
4635 }
4636 switch (cond) {
4637 case kCondEQ:
4638 __ Beq(lhs, rhs_reg, label);
4639 break;
4640 case kCondNE:
4641 __ Bne(lhs, rhs_reg, label);
4642 break;
4643 case kCondLT:
4644 __ Blt(lhs, rhs_reg, label);
4645 break;
4646 case kCondGE:
4647 __ Bge(lhs, rhs_reg, label);
4648 break;
4649 case kCondLE:
4650 __ Bge(rhs_reg, lhs, label);
4651 break;
4652 case kCondGT:
4653 __ Blt(rhs_reg, lhs, label);
4654 break;
4655 case kCondB:
4656 __ Bltu(lhs, rhs_reg, label);
4657 break;
4658 case kCondAE:
4659 __ Bgeu(lhs, rhs_reg, label);
4660 break;
4661 case kCondBE:
4662 __ Bgeu(rhs_reg, lhs, label);
4663 break;
4664 case kCondA:
4665 __ Bltu(rhs_reg, lhs, label);
4666 break;
4667 }
4668 } else {
4669 // Special cases for more efficient comparison with constants on R2.
4670 switch (cond) {
4671 case kCondEQ:
4672 __ LoadConst32(TMP, rhs_imm);
4673 __ Beq(lhs, TMP, label);
4674 break;
4675 case kCondNE:
4676 __ LoadConst32(TMP, rhs_imm);
4677 __ Bne(lhs, TMP, label);
4678 break;
4679 case kCondLT:
4680 if (IsInt<16>(rhs_imm)) {
4681 __ Slti(TMP, lhs, rhs_imm);
4682 __ Bnez(TMP, label);
4683 } else {
4684 __ LoadConst32(TMP, rhs_imm);
4685 __ Blt(lhs, TMP, label);
4686 }
4687 break;
4688 case kCondGE:
4689 if (IsInt<16>(rhs_imm)) {
4690 __ Slti(TMP, lhs, rhs_imm);
4691 __ Beqz(TMP, label);
4692 } else {
4693 __ LoadConst32(TMP, rhs_imm);
4694 __ Bge(lhs, TMP, label);
4695 }
4696 break;
4697 case kCondLE:
4698 if (IsInt<16>(rhs_imm + 1)) {
4699 // Simulate lhs <= rhs via lhs < rhs + 1.
4700 __ Slti(TMP, lhs, rhs_imm + 1);
4701 __ Bnez(TMP, label);
4702 } else {
4703 __ LoadConst32(TMP, rhs_imm);
4704 __ Bge(TMP, lhs, label);
4705 }
4706 break;
4707 case kCondGT:
4708 if (IsInt<16>(rhs_imm + 1)) {
4709 // Simulate lhs > rhs via !(lhs < rhs + 1).
4710 __ Slti(TMP, lhs, rhs_imm + 1);
4711 __ Beqz(TMP, label);
4712 } else {
4713 __ LoadConst32(TMP, rhs_imm);
4714 __ Blt(TMP, lhs, label);
4715 }
4716 break;
4717 case kCondB:
4718 if (IsInt<16>(rhs_imm)) {
4719 __ Sltiu(TMP, lhs, rhs_imm);
4720 __ Bnez(TMP, label);
4721 } else {
4722 __ LoadConst32(TMP, rhs_imm);
4723 __ Bltu(lhs, TMP, label);
4724 }
4725 break;
4726 case kCondAE:
4727 if (IsInt<16>(rhs_imm)) {
4728 __ Sltiu(TMP, lhs, rhs_imm);
4729 __ Beqz(TMP, label);
4730 } else {
4731 __ LoadConst32(TMP, rhs_imm);
4732 __ Bgeu(lhs, TMP, label);
4733 }
4734 break;
4735 case kCondBE:
4736 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4737 // Simulate lhs <= rhs via lhs < rhs + 1.
4738 // Note that this only works if rhs + 1 does not overflow
4739 // to 0, hence the check above.
4740 __ Sltiu(TMP, lhs, rhs_imm + 1);
4741 __ Bnez(TMP, label);
4742 } else {
4743 __ LoadConst32(TMP, rhs_imm);
4744 __ Bgeu(TMP, lhs, label);
4745 }
4746 break;
4747 case kCondA:
4748 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4749 // Simulate lhs > rhs via !(lhs < rhs + 1).
4750 // Note that this only works if rhs + 1 does not overflow
4751 // to 0, hence the check above.
4752 __ Sltiu(TMP, lhs, rhs_imm + 1);
4753 __ Beqz(TMP, label);
4754 } else {
4755 __ LoadConst32(TMP, rhs_imm);
4756 __ Bltu(TMP, lhs, label);
4757 }
4758 break;
4759 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004760 }
4761 }
4762}
4763
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004764void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4765 LocationSummary* locations) {
4766 Register dst = locations->Out().AsRegister<Register>();
4767 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4768 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4769 Location rhs_location = locations->InAt(1);
4770 Register rhs_high = ZERO;
4771 Register rhs_low = ZERO;
4772 int64_t imm = 0;
4773 uint32_t imm_high = 0;
4774 uint32_t imm_low = 0;
4775 bool use_imm = rhs_location.IsConstant();
4776 if (use_imm) {
4777 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4778 imm_high = High32Bits(imm);
4779 imm_low = Low32Bits(imm);
4780 } else {
4781 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4782 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4783 }
4784 if (use_imm && imm == 0) {
4785 switch (cond) {
4786 case kCondEQ:
4787 case kCondBE: // <= 0 if zero
4788 __ Or(dst, lhs_high, lhs_low);
4789 __ Sltiu(dst, dst, 1);
4790 break;
4791 case kCondNE:
4792 case kCondA: // > 0 if non-zero
4793 __ Or(dst, lhs_high, lhs_low);
4794 __ Sltu(dst, ZERO, dst);
4795 break;
4796 case kCondLT:
4797 __ Slt(dst, lhs_high, ZERO);
4798 break;
4799 case kCondGE:
4800 __ Slt(dst, lhs_high, ZERO);
4801 __ Xori(dst, dst, 1);
4802 break;
4803 case kCondLE:
4804 __ Or(TMP, lhs_high, lhs_low);
4805 __ Sra(AT, lhs_high, 31);
4806 __ Sltu(dst, AT, TMP);
4807 __ Xori(dst, dst, 1);
4808 break;
4809 case kCondGT:
4810 __ Or(TMP, lhs_high, lhs_low);
4811 __ Sra(AT, lhs_high, 31);
4812 __ Sltu(dst, AT, TMP);
4813 break;
4814 case kCondB: // always false
4815 __ Andi(dst, dst, 0);
4816 break;
4817 case kCondAE: // always true
4818 __ Ori(dst, ZERO, 1);
4819 break;
4820 }
4821 } else if (use_imm) {
4822 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4823 switch (cond) {
4824 case kCondEQ:
4825 __ LoadConst32(TMP, imm_high);
4826 __ Xor(TMP, TMP, lhs_high);
4827 __ LoadConst32(AT, imm_low);
4828 __ Xor(AT, AT, lhs_low);
4829 __ Or(dst, TMP, AT);
4830 __ Sltiu(dst, dst, 1);
4831 break;
4832 case kCondNE:
4833 __ LoadConst32(TMP, imm_high);
4834 __ Xor(TMP, TMP, lhs_high);
4835 __ LoadConst32(AT, imm_low);
4836 __ Xor(AT, AT, lhs_low);
4837 __ Or(dst, TMP, AT);
4838 __ Sltu(dst, ZERO, dst);
4839 break;
4840 case kCondLT:
4841 case kCondGE:
4842 if (dst == lhs_low) {
4843 __ LoadConst32(TMP, imm_low);
4844 __ Sltu(dst, lhs_low, TMP);
4845 }
4846 __ LoadConst32(TMP, imm_high);
4847 __ Slt(AT, lhs_high, TMP);
4848 __ Slt(TMP, TMP, lhs_high);
4849 if (dst != lhs_low) {
4850 __ LoadConst32(dst, imm_low);
4851 __ Sltu(dst, lhs_low, dst);
4852 }
4853 __ Slt(dst, TMP, dst);
4854 __ Or(dst, dst, AT);
4855 if (cond == kCondGE) {
4856 __ Xori(dst, dst, 1);
4857 }
4858 break;
4859 case kCondGT:
4860 case kCondLE:
4861 if (dst == lhs_low) {
4862 __ LoadConst32(TMP, imm_low);
4863 __ Sltu(dst, TMP, lhs_low);
4864 }
4865 __ LoadConst32(TMP, imm_high);
4866 __ Slt(AT, TMP, lhs_high);
4867 __ Slt(TMP, lhs_high, TMP);
4868 if (dst != lhs_low) {
4869 __ LoadConst32(dst, imm_low);
4870 __ Sltu(dst, dst, lhs_low);
4871 }
4872 __ Slt(dst, TMP, dst);
4873 __ Or(dst, dst, AT);
4874 if (cond == kCondLE) {
4875 __ Xori(dst, dst, 1);
4876 }
4877 break;
4878 case kCondB:
4879 case kCondAE:
4880 if (dst == lhs_low) {
4881 __ LoadConst32(TMP, imm_low);
4882 __ Sltu(dst, lhs_low, TMP);
4883 }
4884 __ LoadConst32(TMP, imm_high);
4885 __ Sltu(AT, lhs_high, TMP);
4886 __ Sltu(TMP, TMP, lhs_high);
4887 if (dst != lhs_low) {
4888 __ LoadConst32(dst, imm_low);
4889 __ Sltu(dst, lhs_low, dst);
4890 }
4891 __ Slt(dst, TMP, dst);
4892 __ Or(dst, dst, AT);
4893 if (cond == kCondAE) {
4894 __ Xori(dst, dst, 1);
4895 }
4896 break;
4897 case kCondA:
4898 case kCondBE:
4899 if (dst == lhs_low) {
4900 __ LoadConst32(TMP, imm_low);
4901 __ Sltu(dst, TMP, lhs_low);
4902 }
4903 __ LoadConst32(TMP, imm_high);
4904 __ Sltu(AT, TMP, lhs_high);
4905 __ Sltu(TMP, lhs_high, TMP);
4906 if (dst != lhs_low) {
4907 __ LoadConst32(dst, imm_low);
4908 __ Sltu(dst, dst, lhs_low);
4909 }
4910 __ Slt(dst, TMP, dst);
4911 __ Or(dst, dst, AT);
4912 if (cond == kCondBE) {
4913 __ Xori(dst, dst, 1);
4914 }
4915 break;
4916 }
4917 } else {
4918 switch (cond) {
4919 case kCondEQ:
4920 __ Xor(TMP, lhs_high, rhs_high);
4921 __ Xor(AT, lhs_low, rhs_low);
4922 __ Or(dst, TMP, AT);
4923 __ Sltiu(dst, dst, 1);
4924 break;
4925 case kCondNE:
4926 __ Xor(TMP, lhs_high, rhs_high);
4927 __ Xor(AT, lhs_low, rhs_low);
4928 __ Or(dst, TMP, AT);
4929 __ Sltu(dst, ZERO, dst);
4930 break;
4931 case kCondLT:
4932 case kCondGE:
4933 __ Slt(TMP, rhs_high, lhs_high);
4934 __ Sltu(AT, lhs_low, rhs_low);
4935 __ Slt(TMP, TMP, AT);
4936 __ Slt(AT, lhs_high, rhs_high);
4937 __ Or(dst, AT, TMP);
4938 if (cond == kCondGE) {
4939 __ Xori(dst, dst, 1);
4940 }
4941 break;
4942 case kCondGT:
4943 case kCondLE:
4944 __ Slt(TMP, lhs_high, rhs_high);
4945 __ Sltu(AT, rhs_low, lhs_low);
4946 __ Slt(TMP, TMP, AT);
4947 __ Slt(AT, rhs_high, lhs_high);
4948 __ Or(dst, AT, TMP);
4949 if (cond == kCondLE) {
4950 __ Xori(dst, dst, 1);
4951 }
4952 break;
4953 case kCondB:
4954 case kCondAE:
4955 __ Sltu(TMP, rhs_high, lhs_high);
4956 __ Sltu(AT, lhs_low, rhs_low);
4957 __ Slt(TMP, TMP, AT);
4958 __ Sltu(AT, lhs_high, rhs_high);
4959 __ Or(dst, AT, TMP);
4960 if (cond == kCondAE) {
4961 __ Xori(dst, dst, 1);
4962 }
4963 break;
4964 case kCondA:
4965 case kCondBE:
4966 __ Sltu(TMP, lhs_high, rhs_high);
4967 __ Sltu(AT, rhs_low, lhs_low);
4968 __ Slt(TMP, TMP, AT);
4969 __ Sltu(AT, rhs_high, lhs_high);
4970 __ Or(dst, AT, TMP);
4971 if (cond == kCondBE) {
4972 __ Xori(dst, dst, 1);
4973 }
4974 break;
4975 }
4976 }
4977}
4978
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004979void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4980 LocationSummary* locations,
4981 MipsLabel* label) {
4982 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4983 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4984 Location rhs_location = locations->InAt(1);
4985 Register rhs_high = ZERO;
4986 Register rhs_low = ZERO;
4987 int64_t imm = 0;
4988 uint32_t imm_high = 0;
4989 uint32_t imm_low = 0;
4990 bool use_imm = rhs_location.IsConstant();
4991 if (use_imm) {
4992 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4993 imm_high = High32Bits(imm);
4994 imm_low = Low32Bits(imm);
4995 } else {
4996 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4997 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4998 }
4999
5000 if (use_imm && imm == 0) {
5001 switch (cond) {
5002 case kCondEQ:
5003 case kCondBE: // <= 0 if zero
5004 __ Or(TMP, lhs_high, lhs_low);
5005 __ Beqz(TMP, label);
5006 break;
5007 case kCondNE:
5008 case kCondA: // > 0 if non-zero
5009 __ Or(TMP, lhs_high, lhs_low);
5010 __ Bnez(TMP, label);
5011 break;
5012 case kCondLT:
5013 __ Bltz(lhs_high, label);
5014 break;
5015 case kCondGE:
5016 __ Bgez(lhs_high, label);
5017 break;
5018 case kCondLE:
5019 __ Or(TMP, lhs_high, lhs_low);
5020 __ Sra(AT, lhs_high, 31);
5021 __ Bgeu(AT, TMP, label);
5022 break;
5023 case kCondGT:
5024 __ Or(TMP, lhs_high, lhs_low);
5025 __ Sra(AT, lhs_high, 31);
5026 __ Bltu(AT, TMP, label);
5027 break;
5028 case kCondB: // always false
5029 break;
5030 case kCondAE: // always true
5031 __ B(label);
5032 break;
5033 }
5034 } else if (use_imm) {
5035 // TODO: more efficient comparison with constants without loading them into TMP/AT.
5036 switch (cond) {
5037 case kCondEQ:
5038 __ LoadConst32(TMP, imm_high);
5039 __ Xor(TMP, TMP, lhs_high);
5040 __ LoadConst32(AT, imm_low);
5041 __ Xor(AT, AT, lhs_low);
5042 __ Or(TMP, TMP, AT);
5043 __ Beqz(TMP, label);
5044 break;
5045 case kCondNE:
5046 __ LoadConst32(TMP, imm_high);
5047 __ Xor(TMP, TMP, lhs_high);
5048 __ LoadConst32(AT, imm_low);
5049 __ Xor(AT, AT, lhs_low);
5050 __ Or(TMP, TMP, AT);
5051 __ Bnez(TMP, label);
5052 break;
5053 case kCondLT:
5054 __ LoadConst32(TMP, imm_high);
5055 __ Blt(lhs_high, TMP, label);
5056 __ Slt(TMP, TMP, lhs_high);
5057 __ LoadConst32(AT, imm_low);
5058 __ Sltu(AT, lhs_low, AT);
5059 __ Blt(TMP, AT, label);
5060 break;
5061 case kCondGE:
5062 __ LoadConst32(TMP, imm_high);
5063 __ Blt(TMP, lhs_high, label);
5064 __ Slt(TMP, lhs_high, TMP);
5065 __ LoadConst32(AT, imm_low);
5066 __ Sltu(AT, lhs_low, AT);
5067 __ Or(TMP, TMP, AT);
5068 __ Beqz(TMP, label);
5069 break;
5070 case kCondLE:
5071 __ LoadConst32(TMP, imm_high);
5072 __ Blt(lhs_high, TMP, label);
5073 __ Slt(TMP, TMP, lhs_high);
5074 __ LoadConst32(AT, imm_low);
5075 __ Sltu(AT, AT, lhs_low);
5076 __ Or(TMP, TMP, AT);
5077 __ Beqz(TMP, label);
5078 break;
5079 case kCondGT:
5080 __ LoadConst32(TMP, imm_high);
5081 __ Blt(TMP, lhs_high, label);
5082 __ Slt(TMP, lhs_high, TMP);
5083 __ LoadConst32(AT, imm_low);
5084 __ Sltu(AT, AT, lhs_low);
5085 __ Blt(TMP, AT, label);
5086 break;
5087 case kCondB:
5088 __ LoadConst32(TMP, imm_high);
5089 __ Bltu(lhs_high, TMP, label);
5090 __ Sltu(TMP, TMP, lhs_high);
5091 __ LoadConst32(AT, imm_low);
5092 __ Sltu(AT, lhs_low, AT);
5093 __ Blt(TMP, AT, label);
5094 break;
5095 case kCondAE:
5096 __ LoadConst32(TMP, imm_high);
5097 __ Bltu(TMP, lhs_high, label);
5098 __ Sltu(TMP, lhs_high, TMP);
5099 __ LoadConst32(AT, imm_low);
5100 __ Sltu(AT, lhs_low, AT);
5101 __ Or(TMP, TMP, AT);
5102 __ Beqz(TMP, label);
5103 break;
5104 case kCondBE:
5105 __ LoadConst32(TMP, imm_high);
5106 __ Bltu(lhs_high, TMP, label);
5107 __ Sltu(TMP, TMP, lhs_high);
5108 __ LoadConst32(AT, imm_low);
5109 __ Sltu(AT, AT, lhs_low);
5110 __ Or(TMP, TMP, AT);
5111 __ Beqz(TMP, label);
5112 break;
5113 case kCondA:
5114 __ LoadConst32(TMP, imm_high);
5115 __ Bltu(TMP, lhs_high, label);
5116 __ Sltu(TMP, lhs_high, TMP);
5117 __ LoadConst32(AT, imm_low);
5118 __ Sltu(AT, AT, lhs_low);
5119 __ Blt(TMP, AT, label);
5120 break;
5121 }
5122 } else {
5123 switch (cond) {
5124 case kCondEQ:
5125 __ Xor(TMP, lhs_high, rhs_high);
5126 __ Xor(AT, lhs_low, rhs_low);
5127 __ Or(TMP, TMP, AT);
5128 __ Beqz(TMP, label);
5129 break;
5130 case kCondNE:
5131 __ Xor(TMP, lhs_high, rhs_high);
5132 __ Xor(AT, lhs_low, rhs_low);
5133 __ Or(TMP, TMP, AT);
5134 __ Bnez(TMP, label);
5135 break;
5136 case kCondLT:
5137 __ Blt(lhs_high, rhs_high, label);
5138 __ Slt(TMP, rhs_high, lhs_high);
5139 __ Sltu(AT, lhs_low, rhs_low);
5140 __ Blt(TMP, AT, label);
5141 break;
5142 case kCondGE:
5143 __ Blt(rhs_high, lhs_high, label);
5144 __ Slt(TMP, lhs_high, rhs_high);
5145 __ Sltu(AT, lhs_low, rhs_low);
5146 __ Or(TMP, TMP, AT);
5147 __ Beqz(TMP, label);
5148 break;
5149 case kCondLE:
5150 __ Blt(lhs_high, rhs_high, label);
5151 __ Slt(TMP, rhs_high, lhs_high);
5152 __ Sltu(AT, rhs_low, lhs_low);
5153 __ Or(TMP, TMP, AT);
5154 __ Beqz(TMP, label);
5155 break;
5156 case kCondGT:
5157 __ Blt(rhs_high, lhs_high, label);
5158 __ Slt(TMP, lhs_high, rhs_high);
5159 __ Sltu(AT, rhs_low, lhs_low);
5160 __ Blt(TMP, AT, label);
5161 break;
5162 case kCondB:
5163 __ Bltu(lhs_high, rhs_high, label);
5164 __ Sltu(TMP, rhs_high, lhs_high);
5165 __ Sltu(AT, lhs_low, rhs_low);
5166 __ Blt(TMP, AT, label);
5167 break;
5168 case kCondAE:
5169 __ Bltu(rhs_high, lhs_high, label);
5170 __ Sltu(TMP, lhs_high, rhs_high);
5171 __ Sltu(AT, lhs_low, rhs_low);
5172 __ Or(TMP, TMP, AT);
5173 __ Beqz(TMP, label);
5174 break;
5175 case kCondBE:
5176 __ Bltu(lhs_high, rhs_high, label);
5177 __ Sltu(TMP, rhs_high, lhs_high);
5178 __ Sltu(AT, rhs_low, lhs_low);
5179 __ Or(TMP, TMP, AT);
5180 __ Beqz(TMP, label);
5181 break;
5182 case kCondA:
5183 __ Bltu(rhs_high, lhs_high, label);
5184 __ Sltu(TMP, lhs_high, rhs_high);
5185 __ Sltu(AT, rhs_low, lhs_low);
5186 __ Blt(TMP, AT, label);
5187 break;
5188 }
5189 }
5190}
5191
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005192void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5193 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005194 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005195 LocationSummary* locations) {
5196 Register dst = locations->Out().AsRegister<Register>();
5197 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5198 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5199 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005200 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005201 if (isR6) {
5202 switch (cond) {
5203 case kCondEQ:
5204 __ CmpEqS(FTMP, lhs, rhs);
5205 __ Mfc1(dst, FTMP);
5206 __ Andi(dst, dst, 1);
5207 break;
5208 case kCondNE:
5209 __ CmpEqS(FTMP, lhs, rhs);
5210 __ Mfc1(dst, FTMP);
5211 __ Addiu(dst, dst, 1);
5212 break;
5213 case kCondLT:
5214 if (gt_bias) {
5215 __ CmpLtS(FTMP, lhs, rhs);
5216 } else {
5217 __ CmpUltS(FTMP, lhs, rhs);
5218 }
5219 __ Mfc1(dst, FTMP);
5220 __ Andi(dst, dst, 1);
5221 break;
5222 case kCondLE:
5223 if (gt_bias) {
5224 __ CmpLeS(FTMP, lhs, rhs);
5225 } else {
5226 __ CmpUleS(FTMP, lhs, rhs);
5227 }
5228 __ Mfc1(dst, FTMP);
5229 __ Andi(dst, dst, 1);
5230 break;
5231 case kCondGT:
5232 if (gt_bias) {
5233 __ CmpUltS(FTMP, rhs, lhs);
5234 } else {
5235 __ CmpLtS(FTMP, rhs, lhs);
5236 }
5237 __ Mfc1(dst, FTMP);
5238 __ Andi(dst, dst, 1);
5239 break;
5240 case kCondGE:
5241 if (gt_bias) {
5242 __ CmpUleS(FTMP, rhs, lhs);
5243 } else {
5244 __ CmpLeS(FTMP, rhs, lhs);
5245 }
5246 __ Mfc1(dst, FTMP);
5247 __ Andi(dst, dst, 1);
5248 break;
5249 default:
5250 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5251 UNREACHABLE();
5252 }
5253 } else {
5254 switch (cond) {
5255 case kCondEQ:
5256 __ CeqS(0, lhs, rhs);
5257 __ LoadConst32(dst, 1);
5258 __ Movf(dst, ZERO, 0);
5259 break;
5260 case kCondNE:
5261 __ CeqS(0, lhs, rhs);
5262 __ LoadConst32(dst, 1);
5263 __ Movt(dst, ZERO, 0);
5264 break;
5265 case kCondLT:
5266 if (gt_bias) {
5267 __ ColtS(0, lhs, rhs);
5268 } else {
5269 __ CultS(0, lhs, rhs);
5270 }
5271 __ LoadConst32(dst, 1);
5272 __ Movf(dst, ZERO, 0);
5273 break;
5274 case kCondLE:
5275 if (gt_bias) {
5276 __ ColeS(0, lhs, rhs);
5277 } else {
5278 __ CuleS(0, lhs, rhs);
5279 }
5280 __ LoadConst32(dst, 1);
5281 __ Movf(dst, ZERO, 0);
5282 break;
5283 case kCondGT:
5284 if (gt_bias) {
5285 __ CultS(0, rhs, lhs);
5286 } else {
5287 __ ColtS(0, rhs, lhs);
5288 }
5289 __ LoadConst32(dst, 1);
5290 __ Movf(dst, ZERO, 0);
5291 break;
5292 case kCondGE:
5293 if (gt_bias) {
5294 __ CuleS(0, rhs, lhs);
5295 } else {
5296 __ ColeS(0, rhs, lhs);
5297 }
5298 __ LoadConst32(dst, 1);
5299 __ Movf(dst, ZERO, 0);
5300 break;
5301 default:
5302 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5303 UNREACHABLE();
5304 }
5305 }
5306 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005307 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005308 if (isR6) {
5309 switch (cond) {
5310 case kCondEQ:
5311 __ CmpEqD(FTMP, lhs, rhs);
5312 __ Mfc1(dst, FTMP);
5313 __ Andi(dst, dst, 1);
5314 break;
5315 case kCondNE:
5316 __ CmpEqD(FTMP, lhs, rhs);
5317 __ Mfc1(dst, FTMP);
5318 __ Addiu(dst, dst, 1);
5319 break;
5320 case kCondLT:
5321 if (gt_bias) {
5322 __ CmpLtD(FTMP, lhs, rhs);
5323 } else {
5324 __ CmpUltD(FTMP, lhs, rhs);
5325 }
5326 __ Mfc1(dst, FTMP);
5327 __ Andi(dst, dst, 1);
5328 break;
5329 case kCondLE:
5330 if (gt_bias) {
5331 __ CmpLeD(FTMP, lhs, rhs);
5332 } else {
5333 __ CmpUleD(FTMP, lhs, rhs);
5334 }
5335 __ Mfc1(dst, FTMP);
5336 __ Andi(dst, dst, 1);
5337 break;
5338 case kCondGT:
5339 if (gt_bias) {
5340 __ CmpUltD(FTMP, rhs, lhs);
5341 } else {
5342 __ CmpLtD(FTMP, rhs, lhs);
5343 }
5344 __ Mfc1(dst, FTMP);
5345 __ Andi(dst, dst, 1);
5346 break;
5347 case kCondGE:
5348 if (gt_bias) {
5349 __ CmpUleD(FTMP, rhs, lhs);
5350 } else {
5351 __ CmpLeD(FTMP, rhs, lhs);
5352 }
5353 __ Mfc1(dst, FTMP);
5354 __ Andi(dst, dst, 1);
5355 break;
5356 default:
5357 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5358 UNREACHABLE();
5359 }
5360 } else {
5361 switch (cond) {
5362 case kCondEQ:
5363 __ CeqD(0, lhs, rhs);
5364 __ LoadConst32(dst, 1);
5365 __ Movf(dst, ZERO, 0);
5366 break;
5367 case kCondNE:
5368 __ CeqD(0, lhs, rhs);
5369 __ LoadConst32(dst, 1);
5370 __ Movt(dst, ZERO, 0);
5371 break;
5372 case kCondLT:
5373 if (gt_bias) {
5374 __ ColtD(0, lhs, rhs);
5375 } else {
5376 __ CultD(0, lhs, rhs);
5377 }
5378 __ LoadConst32(dst, 1);
5379 __ Movf(dst, ZERO, 0);
5380 break;
5381 case kCondLE:
5382 if (gt_bias) {
5383 __ ColeD(0, lhs, rhs);
5384 } else {
5385 __ CuleD(0, lhs, rhs);
5386 }
5387 __ LoadConst32(dst, 1);
5388 __ Movf(dst, ZERO, 0);
5389 break;
5390 case kCondGT:
5391 if (gt_bias) {
5392 __ CultD(0, rhs, lhs);
5393 } else {
5394 __ ColtD(0, rhs, lhs);
5395 }
5396 __ LoadConst32(dst, 1);
5397 __ Movf(dst, ZERO, 0);
5398 break;
5399 case kCondGE:
5400 if (gt_bias) {
5401 __ CuleD(0, rhs, lhs);
5402 } else {
5403 __ ColeD(0, rhs, lhs);
5404 }
5405 __ LoadConst32(dst, 1);
5406 __ Movf(dst, ZERO, 0);
5407 break;
5408 default:
5409 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5410 UNREACHABLE();
5411 }
5412 }
5413 }
5414}
5415
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005416bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5417 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005418 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005419 LocationSummary* input_locations,
5420 int cc) {
5421 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5422 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5423 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005424 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005425 switch (cond) {
5426 case kCondEQ:
5427 __ CeqS(cc, lhs, rhs);
5428 return false;
5429 case kCondNE:
5430 __ CeqS(cc, lhs, rhs);
5431 return true;
5432 case kCondLT:
5433 if (gt_bias) {
5434 __ ColtS(cc, lhs, rhs);
5435 } else {
5436 __ CultS(cc, lhs, rhs);
5437 }
5438 return false;
5439 case kCondLE:
5440 if (gt_bias) {
5441 __ ColeS(cc, lhs, rhs);
5442 } else {
5443 __ CuleS(cc, lhs, rhs);
5444 }
5445 return false;
5446 case kCondGT:
5447 if (gt_bias) {
5448 __ CultS(cc, rhs, lhs);
5449 } else {
5450 __ ColtS(cc, rhs, lhs);
5451 }
5452 return false;
5453 case kCondGE:
5454 if (gt_bias) {
5455 __ CuleS(cc, rhs, lhs);
5456 } else {
5457 __ ColeS(cc, rhs, lhs);
5458 }
5459 return false;
5460 default:
5461 LOG(FATAL) << "Unexpected non-floating-point condition";
5462 UNREACHABLE();
5463 }
5464 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005465 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005466 switch (cond) {
5467 case kCondEQ:
5468 __ CeqD(cc, lhs, rhs);
5469 return false;
5470 case kCondNE:
5471 __ CeqD(cc, lhs, rhs);
5472 return true;
5473 case kCondLT:
5474 if (gt_bias) {
5475 __ ColtD(cc, lhs, rhs);
5476 } else {
5477 __ CultD(cc, lhs, rhs);
5478 }
5479 return false;
5480 case kCondLE:
5481 if (gt_bias) {
5482 __ ColeD(cc, lhs, rhs);
5483 } else {
5484 __ CuleD(cc, lhs, rhs);
5485 }
5486 return false;
5487 case kCondGT:
5488 if (gt_bias) {
5489 __ CultD(cc, rhs, lhs);
5490 } else {
5491 __ ColtD(cc, rhs, lhs);
5492 }
5493 return false;
5494 case kCondGE:
5495 if (gt_bias) {
5496 __ CuleD(cc, rhs, lhs);
5497 } else {
5498 __ ColeD(cc, rhs, lhs);
5499 }
5500 return false;
5501 default:
5502 LOG(FATAL) << "Unexpected non-floating-point condition";
5503 UNREACHABLE();
5504 }
5505 }
5506}
5507
5508bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5509 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005510 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005511 LocationSummary* input_locations,
5512 FRegister dst) {
5513 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5514 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5515 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005516 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005517 switch (cond) {
5518 case kCondEQ:
5519 __ CmpEqS(dst, lhs, rhs);
5520 return false;
5521 case kCondNE:
5522 __ CmpEqS(dst, lhs, rhs);
5523 return true;
5524 case kCondLT:
5525 if (gt_bias) {
5526 __ CmpLtS(dst, lhs, rhs);
5527 } else {
5528 __ CmpUltS(dst, lhs, rhs);
5529 }
5530 return false;
5531 case kCondLE:
5532 if (gt_bias) {
5533 __ CmpLeS(dst, lhs, rhs);
5534 } else {
5535 __ CmpUleS(dst, lhs, rhs);
5536 }
5537 return false;
5538 case kCondGT:
5539 if (gt_bias) {
5540 __ CmpUltS(dst, rhs, lhs);
5541 } else {
5542 __ CmpLtS(dst, rhs, lhs);
5543 }
5544 return false;
5545 case kCondGE:
5546 if (gt_bias) {
5547 __ CmpUleS(dst, rhs, lhs);
5548 } else {
5549 __ CmpLeS(dst, rhs, lhs);
5550 }
5551 return false;
5552 default:
5553 LOG(FATAL) << "Unexpected non-floating-point condition";
5554 UNREACHABLE();
5555 }
5556 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005557 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005558 switch (cond) {
5559 case kCondEQ:
5560 __ CmpEqD(dst, lhs, rhs);
5561 return false;
5562 case kCondNE:
5563 __ CmpEqD(dst, lhs, rhs);
5564 return true;
5565 case kCondLT:
5566 if (gt_bias) {
5567 __ CmpLtD(dst, lhs, rhs);
5568 } else {
5569 __ CmpUltD(dst, lhs, rhs);
5570 }
5571 return false;
5572 case kCondLE:
5573 if (gt_bias) {
5574 __ CmpLeD(dst, lhs, rhs);
5575 } else {
5576 __ CmpUleD(dst, lhs, rhs);
5577 }
5578 return false;
5579 case kCondGT:
5580 if (gt_bias) {
5581 __ CmpUltD(dst, rhs, lhs);
5582 } else {
5583 __ CmpLtD(dst, rhs, lhs);
5584 }
5585 return false;
5586 case kCondGE:
5587 if (gt_bias) {
5588 __ CmpUleD(dst, rhs, lhs);
5589 } else {
5590 __ CmpLeD(dst, rhs, lhs);
5591 }
5592 return false;
5593 default:
5594 LOG(FATAL) << "Unexpected non-floating-point condition";
5595 UNREACHABLE();
5596 }
5597 }
5598}
5599
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005600void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5601 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005602 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005603 LocationSummary* locations,
5604 MipsLabel* label) {
5605 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5606 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5607 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005608 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005609 if (isR6) {
5610 switch (cond) {
5611 case kCondEQ:
5612 __ CmpEqS(FTMP, lhs, rhs);
5613 __ Bc1nez(FTMP, label);
5614 break;
5615 case kCondNE:
5616 __ CmpEqS(FTMP, lhs, rhs);
5617 __ Bc1eqz(FTMP, label);
5618 break;
5619 case kCondLT:
5620 if (gt_bias) {
5621 __ CmpLtS(FTMP, lhs, rhs);
5622 } else {
5623 __ CmpUltS(FTMP, lhs, rhs);
5624 }
5625 __ Bc1nez(FTMP, label);
5626 break;
5627 case kCondLE:
5628 if (gt_bias) {
5629 __ CmpLeS(FTMP, lhs, rhs);
5630 } else {
5631 __ CmpUleS(FTMP, lhs, rhs);
5632 }
5633 __ Bc1nez(FTMP, label);
5634 break;
5635 case kCondGT:
5636 if (gt_bias) {
5637 __ CmpUltS(FTMP, rhs, lhs);
5638 } else {
5639 __ CmpLtS(FTMP, rhs, lhs);
5640 }
5641 __ Bc1nez(FTMP, label);
5642 break;
5643 case kCondGE:
5644 if (gt_bias) {
5645 __ CmpUleS(FTMP, rhs, lhs);
5646 } else {
5647 __ CmpLeS(FTMP, rhs, lhs);
5648 }
5649 __ Bc1nez(FTMP, label);
5650 break;
5651 default:
5652 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005653 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005654 }
5655 } else {
5656 switch (cond) {
5657 case kCondEQ:
5658 __ CeqS(0, lhs, rhs);
5659 __ Bc1t(0, label);
5660 break;
5661 case kCondNE:
5662 __ CeqS(0, lhs, rhs);
5663 __ Bc1f(0, label);
5664 break;
5665 case kCondLT:
5666 if (gt_bias) {
5667 __ ColtS(0, lhs, rhs);
5668 } else {
5669 __ CultS(0, lhs, rhs);
5670 }
5671 __ Bc1t(0, label);
5672 break;
5673 case kCondLE:
5674 if (gt_bias) {
5675 __ ColeS(0, lhs, rhs);
5676 } else {
5677 __ CuleS(0, lhs, rhs);
5678 }
5679 __ Bc1t(0, label);
5680 break;
5681 case kCondGT:
5682 if (gt_bias) {
5683 __ CultS(0, rhs, lhs);
5684 } else {
5685 __ ColtS(0, rhs, lhs);
5686 }
5687 __ Bc1t(0, label);
5688 break;
5689 case kCondGE:
5690 if (gt_bias) {
5691 __ CuleS(0, rhs, lhs);
5692 } else {
5693 __ ColeS(0, rhs, lhs);
5694 }
5695 __ Bc1t(0, label);
5696 break;
5697 default:
5698 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005699 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005700 }
5701 }
5702 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005703 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005704 if (isR6) {
5705 switch (cond) {
5706 case kCondEQ:
5707 __ CmpEqD(FTMP, lhs, rhs);
5708 __ Bc1nez(FTMP, label);
5709 break;
5710 case kCondNE:
5711 __ CmpEqD(FTMP, lhs, rhs);
5712 __ Bc1eqz(FTMP, label);
5713 break;
5714 case kCondLT:
5715 if (gt_bias) {
5716 __ CmpLtD(FTMP, lhs, rhs);
5717 } else {
5718 __ CmpUltD(FTMP, lhs, rhs);
5719 }
5720 __ Bc1nez(FTMP, label);
5721 break;
5722 case kCondLE:
5723 if (gt_bias) {
5724 __ CmpLeD(FTMP, lhs, rhs);
5725 } else {
5726 __ CmpUleD(FTMP, lhs, rhs);
5727 }
5728 __ Bc1nez(FTMP, label);
5729 break;
5730 case kCondGT:
5731 if (gt_bias) {
5732 __ CmpUltD(FTMP, rhs, lhs);
5733 } else {
5734 __ CmpLtD(FTMP, rhs, lhs);
5735 }
5736 __ Bc1nez(FTMP, label);
5737 break;
5738 case kCondGE:
5739 if (gt_bias) {
5740 __ CmpUleD(FTMP, rhs, lhs);
5741 } else {
5742 __ CmpLeD(FTMP, rhs, lhs);
5743 }
5744 __ Bc1nez(FTMP, label);
5745 break;
5746 default:
5747 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005748 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005749 }
5750 } else {
5751 switch (cond) {
5752 case kCondEQ:
5753 __ CeqD(0, lhs, rhs);
5754 __ Bc1t(0, label);
5755 break;
5756 case kCondNE:
5757 __ CeqD(0, lhs, rhs);
5758 __ Bc1f(0, label);
5759 break;
5760 case kCondLT:
5761 if (gt_bias) {
5762 __ ColtD(0, lhs, rhs);
5763 } else {
5764 __ CultD(0, lhs, rhs);
5765 }
5766 __ Bc1t(0, label);
5767 break;
5768 case kCondLE:
5769 if (gt_bias) {
5770 __ ColeD(0, lhs, rhs);
5771 } else {
5772 __ CuleD(0, lhs, rhs);
5773 }
5774 __ Bc1t(0, label);
5775 break;
5776 case kCondGT:
5777 if (gt_bias) {
5778 __ CultD(0, rhs, lhs);
5779 } else {
5780 __ ColtD(0, rhs, lhs);
5781 }
5782 __ Bc1t(0, label);
5783 break;
5784 case kCondGE:
5785 if (gt_bias) {
5786 __ CuleD(0, rhs, lhs);
5787 } else {
5788 __ ColeD(0, rhs, lhs);
5789 }
5790 __ Bc1t(0, label);
5791 break;
5792 default:
5793 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005794 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005795 }
5796 }
5797 }
5798}
5799
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005800void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005801 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005802 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005803 MipsLabel* false_target) {
5804 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005805
David Brazdil0debae72015-11-12 18:37:00 +00005806 if (true_target == nullptr && false_target == nullptr) {
5807 // Nothing to do. The code always falls through.
5808 return;
5809 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005810 // Constant condition, statically compared against "true" (integer value 1).
5811 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005812 if (true_target != nullptr) {
5813 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005814 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005815 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005816 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005817 if (false_target != nullptr) {
5818 __ B(false_target);
5819 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005820 }
David Brazdil0debae72015-11-12 18:37:00 +00005821 return;
5822 }
5823
5824 // The following code generates these patterns:
5825 // (1) true_target == nullptr && false_target != nullptr
5826 // - opposite condition true => branch to false_target
5827 // (2) true_target != nullptr && false_target == nullptr
5828 // - condition true => branch to true_target
5829 // (3) true_target != nullptr && false_target != nullptr
5830 // - condition true => branch to true_target
5831 // - branch to false_target
5832 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005833 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005834 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005835 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005836 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005837 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5838 } else {
5839 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5840 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005841 } else {
5842 // The condition instruction has not been materialized, use its inputs as
5843 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005844 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005845 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005846 LocationSummary* locations = cond->GetLocations();
5847 IfCondition if_cond = condition->GetCondition();
5848 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005849
David Brazdil0debae72015-11-12 18:37:00 +00005850 if (true_target == nullptr) {
5851 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005852 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005853 }
5854
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005855 switch (type) {
5856 default:
5857 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5858 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005859 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005860 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5861 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005862 case DataType::Type::kFloat32:
5863 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005864 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5865 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005866 }
5867 }
David Brazdil0debae72015-11-12 18:37:00 +00005868
5869 // If neither branch falls through (case 3), the conditional branch to `true_target`
5870 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5871 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005872 __ B(false_target);
5873 }
5874}
5875
5876void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005877 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005878 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005879 locations->SetInAt(0, Location::RequiresRegister());
5880 }
5881}
5882
5883void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005884 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5885 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5886 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5887 nullptr : codegen_->GetLabelOf(true_successor);
5888 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5889 nullptr : codegen_->GetLabelOf(false_successor);
5890 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005891}
5892
5893void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005894 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005895 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005896 InvokeRuntimeCallingConvention calling_convention;
5897 RegisterSet caller_saves = RegisterSet::Empty();
5898 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5899 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005900 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005901 locations->SetInAt(0, Location::RequiresRegister());
5902 }
5903}
5904
5905void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005906 SlowPathCodeMIPS* slow_path =
5907 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005908 GenerateTestAndBranch(deoptimize,
5909 /* condition_input_index */ 0,
5910 slow_path->GetEntryLabel(),
5911 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005912}
5913
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005914// This function returns true if a conditional move can be generated for HSelect.
5915// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5916// branches and regular moves.
5917//
5918// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5919//
5920// While determining feasibility of a conditional move and setting inputs/outputs
5921// are two distinct tasks, this function does both because they share quite a bit
5922// of common logic.
5923static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5924 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5925 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5926 HCondition* condition = cond->AsCondition();
5927
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 DataType::Type cond_type =
5929 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5930 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005931
5932 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5933 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5934 bool is_true_value_zero_constant =
5935 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5936 bool is_false_value_zero_constant =
5937 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5938
5939 bool can_move_conditionally = false;
5940 bool use_const_for_false_in = false;
5941 bool use_const_for_true_in = false;
5942
5943 if (!cond->IsConstant()) {
5944 switch (cond_type) {
5945 default:
5946 switch (dst_type) {
5947 default:
5948 // Moving int on int condition.
5949 if (is_r6) {
5950 if (is_true_value_zero_constant) {
5951 // seleqz out_reg, false_reg, cond_reg
5952 can_move_conditionally = true;
5953 use_const_for_true_in = true;
5954 } else if (is_false_value_zero_constant) {
5955 // selnez out_reg, true_reg, cond_reg
5956 can_move_conditionally = true;
5957 use_const_for_false_in = true;
5958 } else if (materialized) {
5959 // Not materializing unmaterialized int conditions
5960 // to keep the instruction count low.
5961 // selnez AT, true_reg, cond_reg
5962 // seleqz TMP, false_reg, cond_reg
5963 // or out_reg, AT, TMP
5964 can_move_conditionally = true;
5965 }
5966 } else {
5967 // movn out_reg, true_reg/ZERO, cond_reg
5968 can_move_conditionally = true;
5969 use_const_for_true_in = is_true_value_zero_constant;
5970 }
5971 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005972 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005973 // Moving long on int condition.
5974 if (is_r6) {
5975 if (is_true_value_zero_constant) {
5976 // seleqz out_reg_lo, false_reg_lo, cond_reg
5977 // seleqz out_reg_hi, false_reg_hi, cond_reg
5978 can_move_conditionally = true;
5979 use_const_for_true_in = true;
5980 } else if (is_false_value_zero_constant) {
5981 // selnez out_reg_lo, true_reg_lo, cond_reg
5982 // selnez out_reg_hi, true_reg_hi, cond_reg
5983 can_move_conditionally = true;
5984 use_const_for_false_in = true;
5985 }
5986 // Other long conditional moves would generate 6+ instructions,
5987 // which is too many.
5988 } else {
5989 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5990 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5991 can_move_conditionally = true;
5992 use_const_for_true_in = is_true_value_zero_constant;
5993 }
5994 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005995 case DataType::Type::kFloat32:
5996 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005997 // Moving float/double on int condition.
5998 if (is_r6) {
5999 if (materialized) {
6000 // Not materializing unmaterialized int conditions
6001 // to keep the instruction count low.
6002 can_move_conditionally = true;
6003 if (is_true_value_zero_constant) {
6004 // sltu TMP, ZERO, cond_reg
6005 // mtc1 TMP, temp_cond_reg
6006 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6007 use_const_for_true_in = true;
6008 } else if (is_false_value_zero_constant) {
6009 // sltu TMP, ZERO, cond_reg
6010 // mtc1 TMP, temp_cond_reg
6011 // selnez.fmt out_reg, true_reg, temp_cond_reg
6012 use_const_for_false_in = true;
6013 } else {
6014 // sltu TMP, ZERO, cond_reg
6015 // mtc1 TMP, temp_cond_reg
6016 // sel.fmt temp_cond_reg, false_reg, true_reg
6017 // mov.fmt out_reg, temp_cond_reg
6018 }
6019 }
6020 } else {
6021 // movn.fmt out_reg, true_reg, cond_reg
6022 can_move_conditionally = true;
6023 }
6024 break;
6025 }
6026 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006027 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006028 // We don't materialize long comparison now
6029 // and use conditional branches instead.
6030 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006031 case DataType::Type::kFloat32:
6032 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006033 switch (dst_type) {
6034 default:
6035 // Moving int on float/double condition.
6036 if (is_r6) {
6037 if (is_true_value_zero_constant) {
6038 // mfc1 TMP, temp_cond_reg
6039 // seleqz out_reg, false_reg, TMP
6040 can_move_conditionally = true;
6041 use_const_for_true_in = true;
6042 } else if (is_false_value_zero_constant) {
6043 // mfc1 TMP, temp_cond_reg
6044 // selnez out_reg, true_reg, TMP
6045 can_move_conditionally = true;
6046 use_const_for_false_in = true;
6047 } else {
6048 // mfc1 TMP, temp_cond_reg
6049 // selnez AT, true_reg, TMP
6050 // seleqz TMP, false_reg, TMP
6051 // or out_reg, AT, TMP
6052 can_move_conditionally = true;
6053 }
6054 } else {
6055 // movt out_reg, true_reg/ZERO, cc
6056 can_move_conditionally = true;
6057 use_const_for_true_in = is_true_value_zero_constant;
6058 }
6059 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006060 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006061 // Moving long on float/double condition.
6062 if (is_r6) {
6063 if (is_true_value_zero_constant) {
6064 // mfc1 TMP, temp_cond_reg
6065 // seleqz out_reg_lo, false_reg_lo, TMP
6066 // seleqz out_reg_hi, false_reg_hi, TMP
6067 can_move_conditionally = true;
6068 use_const_for_true_in = true;
6069 } else if (is_false_value_zero_constant) {
6070 // mfc1 TMP, temp_cond_reg
6071 // selnez out_reg_lo, true_reg_lo, TMP
6072 // selnez out_reg_hi, true_reg_hi, TMP
6073 can_move_conditionally = true;
6074 use_const_for_false_in = true;
6075 }
6076 // Other long conditional moves would generate 6+ instructions,
6077 // which is too many.
6078 } else {
6079 // movt out_reg_lo, true_reg_lo/ZERO, cc
6080 // movt out_reg_hi, true_reg_hi/ZERO, cc
6081 can_move_conditionally = true;
6082 use_const_for_true_in = is_true_value_zero_constant;
6083 }
6084 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006085 case DataType::Type::kFloat32:
6086 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006087 // Moving float/double on float/double condition.
6088 if (is_r6) {
6089 can_move_conditionally = true;
6090 if (is_true_value_zero_constant) {
6091 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6092 use_const_for_true_in = true;
6093 } else if (is_false_value_zero_constant) {
6094 // selnez.fmt out_reg, true_reg, temp_cond_reg
6095 use_const_for_false_in = true;
6096 } else {
6097 // sel.fmt temp_cond_reg, false_reg, true_reg
6098 // mov.fmt out_reg, temp_cond_reg
6099 }
6100 } else {
6101 // movt.fmt out_reg, true_reg, cc
6102 can_move_conditionally = true;
6103 }
6104 break;
6105 }
6106 break;
6107 }
6108 }
6109
6110 if (can_move_conditionally) {
6111 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6112 } else {
6113 DCHECK(!use_const_for_false_in);
6114 DCHECK(!use_const_for_true_in);
6115 }
6116
6117 if (locations_to_set != nullptr) {
6118 if (use_const_for_false_in) {
6119 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6120 } else {
6121 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006122 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006123 ? Location::RequiresFpuRegister()
6124 : Location::RequiresRegister());
6125 }
6126 if (use_const_for_true_in) {
6127 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6128 } else {
6129 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006130 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006131 ? Location::RequiresFpuRegister()
6132 : Location::RequiresRegister());
6133 }
6134 if (materialized) {
6135 locations_to_set->SetInAt(2, Location::RequiresRegister());
6136 }
6137 // On R6 we don't require the output to be the same as the
6138 // first input for conditional moves unlike on R2.
6139 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6140 if (is_out_same_as_first_in) {
6141 locations_to_set->SetOut(Location::SameAsFirstInput());
6142 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006143 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006144 ? Location::RequiresFpuRegister()
6145 : Location::RequiresRegister());
6146 }
6147 }
6148
6149 return can_move_conditionally;
6150}
6151
6152void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6153 LocationSummary* locations = select->GetLocations();
6154 Location dst = locations->Out();
6155 Location src = locations->InAt(1);
6156 Register src_reg = ZERO;
6157 Register src_reg_high = ZERO;
6158 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6159 Register cond_reg = TMP;
6160 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006161 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006162 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006163 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006164
6165 if (IsBooleanValueOrMaterializedCondition(cond)) {
6166 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6167 } else {
6168 HCondition* condition = cond->AsCondition();
6169 LocationSummary* cond_locations = cond->GetLocations();
6170 IfCondition if_cond = condition->GetCondition();
6171 cond_type = condition->InputAt(0)->GetType();
6172 switch (cond_type) {
6173 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006174 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006175 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6176 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006177 case DataType::Type::kFloat32:
6178 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006179 cond_inverted = MaterializeFpCompareR2(if_cond,
6180 condition->IsGtBias(),
6181 cond_type,
6182 cond_locations,
6183 cond_cc);
6184 break;
6185 }
6186 }
6187
6188 DCHECK(dst.Equals(locations->InAt(0)));
6189 if (src.IsRegister()) {
6190 src_reg = src.AsRegister<Register>();
6191 } else if (src.IsRegisterPair()) {
6192 src_reg = src.AsRegisterPairLow<Register>();
6193 src_reg_high = src.AsRegisterPairHigh<Register>();
6194 } else if (src.IsConstant()) {
6195 DCHECK(src.GetConstant()->IsZeroBitPattern());
6196 }
6197
6198 switch (cond_type) {
6199 default:
6200 switch (dst_type) {
6201 default:
6202 if (cond_inverted) {
6203 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6204 } else {
6205 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6206 }
6207 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006208 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006209 if (cond_inverted) {
6210 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6211 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6212 } else {
6213 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6214 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6215 }
6216 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006217 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006218 if (cond_inverted) {
6219 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6220 } else {
6221 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6222 }
6223 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006224 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006225 if (cond_inverted) {
6226 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6227 } else {
6228 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6229 }
6230 break;
6231 }
6232 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006233 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006234 LOG(FATAL) << "Unreachable";
6235 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006236 case DataType::Type::kFloat32:
6237 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006238 switch (dst_type) {
6239 default:
6240 if (cond_inverted) {
6241 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6242 } else {
6243 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6244 }
6245 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006246 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006247 if (cond_inverted) {
6248 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6249 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6250 } else {
6251 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6252 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6253 }
6254 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006255 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006256 if (cond_inverted) {
6257 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6258 } else {
6259 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6260 }
6261 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006262 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006263 if (cond_inverted) {
6264 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6265 } else {
6266 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6267 }
6268 break;
6269 }
6270 break;
6271 }
6272}
6273
6274void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6275 LocationSummary* locations = select->GetLocations();
6276 Location dst = locations->Out();
6277 Location false_src = locations->InAt(0);
6278 Location true_src = locations->InAt(1);
6279 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6280 Register cond_reg = TMP;
6281 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006282 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006283 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006284 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006285
6286 if (IsBooleanValueOrMaterializedCondition(cond)) {
6287 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6288 } else {
6289 HCondition* condition = cond->AsCondition();
6290 LocationSummary* cond_locations = cond->GetLocations();
6291 IfCondition if_cond = condition->GetCondition();
6292 cond_type = condition->InputAt(0)->GetType();
6293 switch (cond_type) {
6294 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006295 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006296 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6297 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006298 case DataType::Type::kFloat32:
6299 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006300 cond_inverted = MaterializeFpCompareR6(if_cond,
6301 condition->IsGtBias(),
6302 cond_type,
6303 cond_locations,
6304 fcond_reg);
6305 break;
6306 }
6307 }
6308
6309 if (true_src.IsConstant()) {
6310 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6311 }
6312 if (false_src.IsConstant()) {
6313 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6314 }
6315
6316 switch (dst_type) {
6317 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006318 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006319 __ Mfc1(cond_reg, fcond_reg);
6320 }
6321 if (true_src.IsConstant()) {
6322 if (cond_inverted) {
6323 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6324 } else {
6325 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6326 }
6327 } else if (false_src.IsConstant()) {
6328 if (cond_inverted) {
6329 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6330 } else {
6331 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6332 }
6333 } else {
6334 DCHECK_NE(cond_reg, AT);
6335 if (cond_inverted) {
6336 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6337 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6338 } else {
6339 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6340 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6341 }
6342 __ Or(dst.AsRegister<Register>(), AT, TMP);
6343 }
6344 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006345 case DataType::Type::kInt64: {
6346 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006347 __ Mfc1(cond_reg, fcond_reg);
6348 }
6349 Register dst_lo = dst.AsRegisterPairLow<Register>();
6350 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6351 if (true_src.IsConstant()) {
6352 Register src_lo = false_src.AsRegisterPairLow<Register>();
6353 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6354 if (cond_inverted) {
6355 __ Selnez(dst_lo, src_lo, cond_reg);
6356 __ Selnez(dst_hi, src_hi, cond_reg);
6357 } else {
6358 __ Seleqz(dst_lo, src_lo, cond_reg);
6359 __ Seleqz(dst_hi, src_hi, cond_reg);
6360 }
6361 } else {
6362 DCHECK(false_src.IsConstant());
6363 Register src_lo = true_src.AsRegisterPairLow<Register>();
6364 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6365 if (cond_inverted) {
6366 __ Seleqz(dst_lo, src_lo, cond_reg);
6367 __ Seleqz(dst_hi, src_hi, cond_reg);
6368 } else {
6369 __ Selnez(dst_lo, src_lo, cond_reg);
6370 __ Selnez(dst_hi, src_hi, cond_reg);
6371 }
6372 }
6373 break;
6374 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006375 case DataType::Type::kFloat32: {
6376 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006377 // sel*.fmt tests bit 0 of the condition register, account for that.
6378 __ Sltu(TMP, ZERO, cond_reg);
6379 __ Mtc1(TMP, fcond_reg);
6380 }
6381 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6382 if (true_src.IsConstant()) {
6383 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6384 if (cond_inverted) {
6385 __ SelnezS(dst_reg, src_reg, fcond_reg);
6386 } else {
6387 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6388 }
6389 } else if (false_src.IsConstant()) {
6390 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6391 if (cond_inverted) {
6392 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6393 } else {
6394 __ SelnezS(dst_reg, src_reg, fcond_reg);
6395 }
6396 } else {
6397 if (cond_inverted) {
6398 __ SelS(fcond_reg,
6399 true_src.AsFpuRegister<FRegister>(),
6400 false_src.AsFpuRegister<FRegister>());
6401 } else {
6402 __ SelS(fcond_reg,
6403 false_src.AsFpuRegister<FRegister>(),
6404 true_src.AsFpuRegister<FRegister>());
6405 }
6406 __ MovS(dst_reg, fcond_reg);
6407 }
6408 break;
6409 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006410 case DataType::Type::kFloat64: {
6411 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006412 // sel*.fmt tests bit 0 of the condition register, account for that.
6413 __ Sltu(TMP, ZERO, cond_reg);
6414 __ Mtc1(TMP, fcond_reg);
6415 }
6416 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6417 if (true_src.IsConstant()) {
6418 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6419 if (cond_inverted) {
6420 __ SelnezD(dst_reg, src_reg, fcond_reg);
6421 } else {
6422 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6423 }
6424 } else if (false_src.IsConstant()) {
6425 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6426 if (cond_inverted) {
6427 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6428 } else {
6429 __ SelnezD(dst_reg, src_reg, fcond_reg);
6430 }
6431 } else {
6432 if (cond_inverted) {
6433 __ SelD(fcond_reg,
6434 true_src.AsFpuRegister<FRegister>(),
6435 false_src.AsFpuRegister<FRegister>());
6436 } else {
6437 __ SelD(fcond_reg,
6438 false_src.AsFpuRegister<FRegister>(),
6439 true_src.AsFpuRegister<FRegister>());
6440 }
6441 __ MovD(dst_reg, fcond_reg);
6442 }
6443 break;
6444 }
6445 }
6446}
6447
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006448void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006449 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006450 LocationSummary(flag, LocationSummary::kNoCall);
6451 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006452}
6453
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006454void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6455 __ LoadFromOffset(kLoadWord,
6456 flag->GetLocations()->Out().AsRegister<Register>(),
6457 SP,
6458 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006459}
6460
David Brazdil74eb1b22015-12-14 11:44:01 +00006461void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006462 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006463 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006464}
6465
6466void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006467 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6468 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6469 if (is_r6) {
6470 GenConditionalMoveR6(select);
6471 } else {
6472 GenConditionalMoveR2(select);
6473 }
6474 } else {
6475 LocationSummary* locations = select->GetLocations();
6476 MipsLabel false_target;
6477 GenerateTestAndBranch(select,
6478 /* condition_input_index */ 2,
6479 /* true_target */ nullptr,
6480 &false_target);
6481 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6482 __ Bind(&false_target);
6483 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006484}
6485
David Srbecky0cf44932015-12-09 14:09:59 +00006486void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006487 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006488}
6489
David Srbeckyd28f4a02016-03-14 17:14:24 +00006490void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6491 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006492}
6493
6494void CodeGeneratorMIPS::GenerateNop() {
6495 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006496}
6497
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006498void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006499 DataType::Type field_type = field_info.GetFieldType();
6500 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006501 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006502 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006503 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006504 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006505 instruction,
6506 generate_volatile
6507 ? LocationSummary::kCallOnMainOnly
6508 : (object_field_get_with_read_barrier
6509 ? LocationSummary::kCallOnSlowPath
6510 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006511
Alexey Frunzec61c0762017-04-10 13:54:23 -07006512 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6513 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6514 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006515 locations->SetInAt(0, Location::RequiresRegister());
6516 if (generate_volatile) {
6517 InvokeRuntimeCallingConvention calling_convention;
6518 // need A0 to hold base + offset
6519 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006520 if (field_type == DataType::Type::kInt64) {
6521 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006522 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006523 // Use Location::Any() to prevent situations when running out of available fp registers.
6524 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006525 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006526 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006527 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6528 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6529 }
6530 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006531 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006532 locations->SetOut(Location::RequiresFpuRegister());
6533 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006534 // The output overlaps in the case of an object field get with
6535 // read barriers enabled: we do not want the move to overwrite the
6536 // object's location, as we need it to emit the read barrier.
6537 locations->SetOut(Location::RequiresRegister(),
6538 object_field_get_with_read_barrier
6539 ? Location::kOutputOverlap
6540 : Location::kNoOutputOverlap);
6541 }
6542 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6543 // We need a temporary register for the read barrier marking slow
6544 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006545 if (!kBakerReadBarrierThunksEnableForFields) {
6546 locations->AddTemp(Location::RequiresRegister());
6547 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006548 }
6549 }
6550}
6551
6552void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6553 const FieldInfo& field_info,
6554 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006555 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6556 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006557 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006558 Location obj_loc = locations->InAt(0);
6559 Register obj = obj_loc.AsRegister<Register>();
6560 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006561 LoadOperandType load_type = kLoadUnsignedByte;
6562 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006563 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006564 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006565
6566 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006567 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006568 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006569 load_type = kLoadUnsignedByte;
6570 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006571 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006572 load_type = kLoadSignedByte;
6573 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006574 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006575 load_type = kLoadUnsignedHalfword;
6576 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006577 case DataType::Type::kInt16:
6578 load_type = kLoadSignedHalfword;
6579 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006580 case DataType::Type::kInt32:
6581 case DataType::Type::kFloat32:
6582 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006583 load_type = kLoadWord;
6584 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006585 case DataType::Type::kInt64:
6586 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006587 load_type = kLoadDoubleword;
6588 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006589 case DataType::Type::kUint32:
6590 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006591 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006592 LOG(FATAL) << "Unreachable type " << type;
6593 UNREACHABLE();
6594 }
6595
6596 if (is_volatile && load_type == kLoadDoubleword) {
6597 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006598 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006599 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006600 __ LoadFromOffset(kLoadWord,
6601 ZERO,
6602 locations->GetTemp(0).AsRegister<Register>(),
6603 0,
6604 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006605 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006606 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006607 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006608 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006609 if (dst_loc.IsFpuRegister()) {
6610 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006611 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006612 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006613 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006614 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006615 __ StoreToOffset(kStoreWord,
6616 locations->GetTemp(1).AsRegister<Register>(),
6617 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006618 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006619 __ StoreToOffset(kStoreWord,
6620 locations->GetTemp(2).AsRegister<Register>(),
6621 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006622 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006623 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006624 }
6625 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006626 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006627 // /* HeapReference<Object> */ dst = *(obj + offset)
6628 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006629 Location temp_loc =
6630 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006631 // Note that a potential implicit null check is handled in this
6632 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6633 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6634 dst_loc,
6635 obj,
6636 offset,
6637 temp_loc,
6638 /* needs_null_check */ true);
6639 if (is_volatile) {
6640 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6641 }
6642 } else {
6643 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6644 if (is_volatile) {
6645 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6646 }
6647 // If read barriers are enabled, emit read barriers other than
6648 // Baker's using a slow path (and also unpoison the loaded
6649 // reference, if heap poisoning is enabled).
6650 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6651 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006652 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006653 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006654 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006655 DCHECK(dst_loc.IsRegisterPair());
6656 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006657 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006658 DCHECK(dst_loc.IsRegister());
6659 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006660 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006661 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006662 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006663 DCHECK(dst_loc.IsFpuRegister());
6664 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006665 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006666 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006667 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006668 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006669 }
6670 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006671 }
6672
Alexey Frunze15958152017-02-09 19:08:30 -08006673 // Memory barriers, in the case of references, are handled in the
6674 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006675 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006676 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6677 }
6678}
6679
6680void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006681 DataType::Type field_type = field_info.GetFieldType();
6682 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006683 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006684 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006685 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006686
6687 locations->SetInAt(0, Location::RequiresRegister());
6688 if (generate_volatile) {
6689 InvokeRuntimeCallingConvention calling_convention;
6690 // need A0 to hold base + offset
6691 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006692 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006693 locations->SetInAt(1, Location::RegisterPairLocation(
6694 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6695 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006696 // Use Location::Any() to prevent situations when running out of available fp registers.
6697 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006698 // Pass FP parameters in core registers.
6699 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6700 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6701 }
6702 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006703 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006704 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006705 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006706 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006707 }
6708 }
6709}
6710
6711void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6712 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006713 uint32_t dex_pc,
6714 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006715 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006716 LocationSummary* locations = instruction->GetLocations();
6717 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006718 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006719 StoreOperandType store_type = kStoreByte;
6720 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006721 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006722 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006723 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724
6725 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006726 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006727 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006728 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006729 store_type = kStoreByte;
6730 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006731 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006732 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006733 store_type = kStoreHalfword;
6734 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006735 case DataType::Type::kInt32:
6736 case DataType::Type::kFloat32:
6737 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006738 store_type = kStoreWord;
6739 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006740 case DataType::Type::kInt64:
6741 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006742 store_type = kStoreDoubleword;
6743 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006744 case DataType::Type::kUint32:
6745 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006746 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006747 LOG(FATAL) << "Unreachable type " << type;
6748 UNREACHABLE();
6749 }
6750
6751 if (is_volatile) {
6752 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6753 }
6754
6755 if (is_volatile && store_type == kStoreDoubleword) {
6756 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006757 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006758 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006759 __ LoadFromOffset(kLoadWord,
6760 ZERO,
6761 locations->GetTemp(0).AsRegister<Register>(),
6762 0,
6763 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006764 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006765 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006766 if (value_location.IsFpuRegister()) {
6767 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6768 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006769 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006770 value_location.AsFpuRegister<FRegister>());
6771 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006772 __ LoadFromOffset(kLoadWord,
6773 locations->GetTemp(1).AsRegister<Register>(),
6774 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006775 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006776 __ LoadFromOffset(kLoadWord,
6777 locations->GetTemp(2).AsRegister<Register>(),
6778 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006779 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006780 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006781 DCHECK(value_location.IsConstant());
6782 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6783 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006784 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6785 locations->GetTemp(1).AsRegister<Register>(),
6786 value);
6787 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006788 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006789 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006790 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6791 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006792 if (value_location.IsConstant()) {
6793 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6794 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006795 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006796 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006797 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006798 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006799 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006800 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006801 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006802 if (kPoisonHeapReferences && needs_write_barrier) {
6803 // Note that in the case where `value` is a null reference,
6804 // we do not enter this block, as a null reference does not
6805 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006806 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006807 __ PoisonHeapReference(TMP, src);
6808 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6809 } else {
6810 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6811 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006812 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006813 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006814 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006815 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006816 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006817 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006818 }
6819 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006820 }
6821
Alexey Frunzec061de12017-02-14 13:27:23 -08006822 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006823 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006824 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006825 }
6826
6827 if (is_volatile) {
6828 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6829 }
6830}
6831
6832void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6833 HandleFieldGet(instruction, instruction->GetFieldInfo());
6834}
6835
6836void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6837 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6838}
6839
6840void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6841 HandleFieldSet(instruction, instruction->GetFieldInfo());
6842}
6843
6844void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006845 HandleFieldSet(instruction,
6846 instruction->GetFieldInfo(),
6847 instruction->GetDexPc(),
6848 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006849}
6850
Alexey Frunze15958152017-02-09 19:08:30 -08006851void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6852 HInstruction* instruction,
6853 Location out,
6854 uint32_t offset,
6855 Location maybe_temp,
6856 ReadBarrierOption read_barrier_option) {
6857 Register out_reg = out.AsRegister<Register>();
6858 if (read_barrier_option == kWithReadBarrier) {
6859 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006860 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6861 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6862 }
Alexey Frunze15958152017-02-09 19:08:30 -08006863 if (kUseBakerReadBarrier) {
6864 // Load with fast path based Baker's read barrier.
6865 // /* HeapReference<Object> */ out = *(out + offset)
6866 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6867 out,
6868 out_reg,
6869 offset,
6870 maybe_temp,
6871 /* needs_null_check */ false);
6872 } else {
6873 // Load with slow path based read barrier.
6874 // Save the value of `out` into `maybe_temp` before overwriting it
6875 // in the following move operation, as we will need it for the
6876 // read barrier below.
6877 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6878 // /* HeapReference<Object> */ out = *(out + offset)
6879 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6880 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6881 }
6882 } else {
6883 // Plain load with no read barrier.
6884 // /* HeapReference<Object> */ out = *(out + offset)
6885 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6886 __ MaybeUnpoisonHeapReference(out_reg);
6887 }
6888}
6889
6890void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6891 HInstruction* instruction,
6892 Location out,
6893 Location obj,
6894 uint32_t offset,
6895 Location maybe_temp,
6896 ReadBarrierOption read_barrier_option) {
6897 Register out_reg = out.AsRegister<Register>();
6898 Register obj_reg = obj.AsRegister<Register>();
6899 if (read_barrier_option == kWithReadBarrier) {
6900 CHECK(kEmitCompilerReadBarrier);
6901 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006902 if (!kBakerReadBarrierThunksEnableForFields) {
6903 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6904 }
Alexey Frunze15958152017-02-09 19:08:30 -08006905 // Load with fast path based Baker's read barrier.
6906 // /* HeapReference<Object> */ out = *(obj + offset)
6907 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6908 out,
6909 obj_reg,
6910 offset,
6911 maybe_temp,
6912 /* needs_null_check */ false);
6913 } else {
6914 // Load with slow path based read barrier.
6915 // /* HeapReference<Object> */ out = *(obj + offset)
6916 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6917 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6918 }
6919 } else {
6920 // Plain load with no read barrier.
6921 // /* HeapReference<Object> */ out = *(obj + offset)
6922 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6923 __ MaybeUnpoisonHeapReference(out_reg);
6924 }
6925}
6926
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006927static inline int GetBakerMarkThunkNumber(Register reg) {
6928 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6929 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6930 return reg - V0;
6931 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6932 return 14 + (reg - S2);
6933 } else if (reg == FP) { // One more.
6934 return 20;
6935 }
6936 LOG(FATAL) << "Unexpected register " << reg;
6937 UNREACHABLE();
6938}
6939
6940static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6941 int num = GetBakerMarkThunkNumber(reg) +
6942 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6943 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6944}
6945
6946static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6947 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6948 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6949}
6950
Alexey Frunze15958152017-02-09 19:08:30 -08006951void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6952 Location root,
6953 Register obj,
6954 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006955 ReadBarrierOption read_barrier_option,
6956 MipsLabel* label_low) {
6957 bool reordering;
6958 if (label_low != nullptr) {
6959 DCHECK_EQ(offset, 0x5678u);
6960 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006961 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006962 if (read_barrier_option == kWithReadBarrier) {
6963 DCHECK(kEmitCompilerReadBarrier);
6964 if (kUseBakerReadBarrier) {
6965 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6966 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006967 if (kBakerReadBarrierThunksEnableForGcRoots) {
6968 // Note that we do not actually check the value of `GetIsGcMarking()`
6969 // to decide whether to mark the loaded GC root or not. Instead, we
6970 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6971 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6972 // vice versa.
6973 //
6974 // We use thunks for the slow path. That thunk checks the reference
6975 // and jumps to the entrypoint if needed.
6976 //
6977 // temp = Thread::Current()->pReadBarrierMarkReg00
6978 // // AKA &art_quick_read_barrier_mark_introspection.
6979 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6980 // if (temp != nullptr) {
6981 // temp = &gc_root_thunk<root_reg>
6982 // root = temp(root)
6983 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006984
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006985 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6986 const int32_t entry_point_offset =
6987 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6988 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6989 int16_t offset_low = Low16Bits(offset);
6990 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6991 // extension in lw.
6992 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6993 Register base = short_offset ? obj : TMP;
6994 // Loading the entrypoint does not require a load acquire since it is only changed when
6995 // threads are suspended or running a checkpoint.
6996 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6997 reordering = __ SetReorder(false);
6998 if (!short_offset) {
6999 DCHECK(!label_low);
7000 __ AddUpper(base, obj, offset_high);
7001 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007002 MipsLabel skip_call;
7003 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007004 if (label_low != nullptr) {
7005 DCHECK(short_offset);
7006 __ Bind(label_low);
7007 }
7008 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7009 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
7010 // in delay slot.
7011 if (isR6) {
7012 __ Jialc(T9, thunk_disp);
7013 } else {
7014 __ Addiu(T9, T9, thunk_disp);
7015 __ Jalr(T9);
7016 __ Nop();
7017 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007018 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007019 __ SetReorder(reordering);
7020 } else {
7021 // Note that we do not actually check the value of `GetIsGcMarking()`
7022 // to decide whether to mark the loaded GC root or not. Instead, we
7023 // load into `temp` (T9) the read barrier mark entry point corresponding
7024 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
7025 // is false, and vice versa.
7026 //
7027 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7028 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7029 // if (temp != null) {
7030 // root = temp(root)
7031 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007032
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007033 if (label_low != nullptr) {
7034 reordering = __ SetReorder(false);
7035 __ Bind(label_low);
7036 }
7037 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7038 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7039 if (label_low != nullptr) {
7040 __ SetReorder(reordering);
7041 }
7042 static_assert(
7043 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7044 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7045 "have different sizes.");
7046 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7047 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7048 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08007049
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007050 // Slow path marking the GC root `root`.
7051 Location temp = Location::RegisterLocation(T9);
7052 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007053 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007054 instruction,
7055 root,
7056 /*entrypoint*/ temp);
7057 codegen_->AddSlowPath(slow_path);
7058
7059 const int32_t entry_point_offset =
7060 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
7061 // Loading the entrypoint does not require a load acquire since it is only changed when
7062 // threads are suspended or running a checkpoint.
7063 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7064 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
7065 __ Bind(slow_path->GetExitLabel());
7066 }
Alexey Frunze15958152017-02-09 19:08:30 -08007067 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007068 if (label_low != nullptr) {
7069 reordering = __ SetReorder(false);
7070 __ Bind(label_low);
7071 }
Alexey Frunze15958152017-02-09 19:08:30 -08007072 // GC root loaded through a slow path for read barriers other
7073 // than Baker's.
7074 // /* GcRoot<mirror::Object>* */ root = obj + offset
7075 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007076 if (label_low != nullptr) {
7077 __ SetReorder(reordering);
7078 }
Alexey Frunze15958152017-02-09 19:08:30 -08007079 // /* mirror::Object* */ root = root->Read()
7080 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7081 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007082 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007083 if (label_low != nullptr) {
7084 reordering = __ SetReorder(false);
7085 __ Bind(label_low);
7086 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007087 // Plain GC root load with no read barrier.
7088 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7089 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7090 // Note that GC roots are not affected by heap poisoning, thus we
7091 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007092 if (label_low != nullptr) {
7093 __ SetReorder(reordering);
7094 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007095 }
7096}
7097
Alexey Frunze15958152017-02-09 19:08:30 -08007098void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7099 Location ref,
7100 Register obj,
7101 uint32_t offset,
7102 Location temp,
7103 bool needs_null_check) {
7104 DCHECK(kEmitCompilerReadBarrier);
7105 DCHECK(kUseBakerReadBarrier);
7106
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007107 if (kBakerReadBarrierThunksEnableForFields) {
7108 // Note that we do not actually check the value of `GetIsGcMarking()`
7109 // to decide whether to mark the loaded reference or not. Instead, we
7110 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7111 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7112 // vice versa.
7113 //
7114 // We use thunks for the slow path. That thunk checks the reference
7115 // and jumps to the entrypoint if needed. If the holder is not gray,
7116 // it issues a load-load memory barrier and returns to the original
7117 // reference load.
7118 //
7119 // temp = Thread::Current()->pReadBarrierMarkReg00
7120 // // AKA &art_quick_read_barrier_mark_introspection.
7121 // if (temp != nullptr) {
7122 // temp = &field_array_thunk<holder_reg>
7123 // temp()
7124 // }
7125 // not_gray_return_address:
7126 // // If the offset is too large to fit into the lw instruction, we
7127 // // use an adjusted base register (TMP) here. This register
7128 // // receives bits 16 ... 31 of the offset before the thunk invocation
7129 // // and the thunk benefits from it.
7130 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7131 // gray_return_address:
7132
7133 DCHECK(temp.IsInvalid());
7134 bool isR6 = GetInstructionSetFeatures().IsR6();
7135 int16_t offset_low = Low16Bits(offset);
7136 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7137 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7138 bool reordering = __ SetReorder(false);
7139 const int32_t entry_point_offset =
7140 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7141 // There may have or may have not been a null check if the field offset is smaller than
7142 // the page size.
7143 // There must've been a null check in case it's actually a load from an array.
7144 // We will, however, perform an explicit null check in the thunk as it's easier to
7145 // do it than not.
7146 if (instruction->IsArrayGet()) {
7147 DCHECK(!needs_null_check);
7148 }
7149 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7150 // Loading the entrypoint does not require a load acquire since it is only changed when
7151 // threads are suspended or running a checkpoint.
7152 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7153 Register ref_reg = ref.AsRegister<Register>();
7154 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007155 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007156 if (short_offset) {
7157 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007158 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007159 __ Nop(); // In forbidden slot.
7160 __ Jialc(T9, thunk_disp);
7161 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007162 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007163 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7164 __ Jalr(T9);
7165 __ Nop(); // In delay slot.
7166 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007167 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007168 } else {
7169 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007170 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007171 __ Aui(base, obj, offset_high); // In delay slot.
7172 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007173 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007174 } else {
7175 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007176 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007177 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7178 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007179 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007180 __ Addu(base, base, obj); // In delay slot.
7181 }
7182 }
7183 // /* HeapReference<Object> */ ref = *(obj + offset)
7184 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7185 if (needs_null_check) {
7186 MaybeRecordImplicitNullCheck(instruction);
7187 }
7188 __ MaybeUnpoisonHeapReference(ref_reg);
7189 __ SetReorder(reordering);
7190 return;
7191 }
7192
Alexey Frunze15958152017-02-09 19:08:30 -08007193 // /* HeapReference<Object> */ ref = *(obj + offset)
7194 Location no_index = Location::NoLocation();
7195 ScaleFactor no_scale_factor = TIMES_1;
7196 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7197 ref,
7198 obj,
7199 offset,
7200 no_index,
7201 no_scale_factor,
7202 temp,
7203 needs_null_check);
7204}
7205
7206void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7207 Location ref,
7208 Register obj,
7209 uint32_t data_offset,
7210 Location index,
7211 Location temp,
7212 bool needs_null_check) {
7213 DCHECK(kEmitCompilerReadBarrier);
7214 DCHECK(kUseBakerReadBarrier);
7215
7216 static_assert(
7217 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7218 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007219 ScaleFactor scale_factor = TIMES_4;
7220
7221 if (kBakerReadBarrierThunksEnableForArrays) {
7222 // Note that we do not actually check the value of `GetIsGcMarking()`
7223 // to decide whether to mark the loaded reference or not. Instead, we
7224 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7225 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7226 // vice versa.
7227 //
7228 // We use thunks for the slow path. That thunk checks the reference
7229 // and jumps to the entrypoint if needed. If the holder is not gray,
7230 // it issues a load-load memory barrier and returns to the original
7231 // reference load.
7232 //
7233 // temp = Thread::Current()->pReadBarrierMarkReg00
7234 // // AKA &art_quick_read_barrier_mark_introspection.
7235 // if (temp != nullptr) {
7236 // temp = &field_array_thunk<holder_reg>
7237 // temp()
7238 // }
7239 // not_gray_return_address:
7240 // // The element address is pre-calculated in the TMP register before the
7241 // // thunk invocation and the thunk benefits from it.
7242 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7243 // gray_return_address:
7244
7245 DCHECK(temp.IsInvalid());
7246 DCHECK(index.IsValid());
7247 bool reordering = __ SetReorder(false);
7248 const int32_t entry_point_offset =
7249 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7250 // We will not do the explicit null check in the thunk as some form of a null check
7251 // must've been done earlier.
7252 DCHECK(!needs_null_check);
7253 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7254 // Loading the entrypoint does not require a load acquire since it is only changed when
7255 // threads are suspended or running a checkpoint.
7256 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7257 Register ref_reg = ref.AsRegister<Register>();
7258 Register index_reg = index.IsRegisterPair()
7259 ? index.AsRegisterPairLow<Register>()
7260 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007261 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007262 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007263 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007264 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7265 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007266 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007267 } else {
7268 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007269 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007270 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7271 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007272 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007273 __ Addu(TMP, TMP, obj); // In delay slot.
7274 }
7275 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7276 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7277 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7278 __ MaybeUnpoisonHeapReference(ref_reg);
7279 __ SetReorder(reordering);
7280 return;
7281 }
7282
Alexey Frunze15958152017-02-09 19:08:30 -08007283 // /* HeapReference<Object> */ ref =
7284 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007285 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7286 ref,
7287 obj,
7288 data_offset,
7289 index,
7290 scale_factor,
7291 temp,
7292 needs_null_check);
7293}
7294
7295void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7296 Location ref,
7297 Register obj,
7298 uint32_t offset,
7299 Location index,
7300 ScaleFactor scale_factor,
7301 Location temp,
7302 bool needs_null_check,
7303 bool always_update_field) {
7304 DCHECK(kEmitCompilerReadBarrier);
7305 DCHECK(kUseBakerReadBarrier);
7306
7307 // In slow path based read barriers, the read barrier call is
7308 // inserted after the original load. However, in fast path based
7309 // Baker's read barriers, we need to perform the load of
7310 // mirror::Object::monitor_ *before* the original reference load.
7311 // This load-load ordering is required by the read barrier.
7312 // The fast path/slow path (for Baker's algorithm) should look like:
7313 //
7314 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7315 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7316 // HeapReference<Object> ref = *src; // Original reference load.
7317 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7318 // if (is_gray) {
7319 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7320 // }
7321 //
7322 // Note: the original implementation in ReadBarrier::Barrier is
7323 // slightly more complex as it performs additional checks that we do
7324 // not do here for performance reasons.
7325
7326 Register ref_reg = ref.AsRegister<Register>();
7327 Register temp_reg = temp.AsRegister<Register>();
7328 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7329
7330 // /* int32_t */ monitor = obj->monitor_
7331 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7332 if (needs_null_check) {
7333 MaybeRecordImplicitNullCheck(instruction);
7334 }
7335 // /* LockWord */ lock_word = LockWord(monitor)
7336 static_assert(sizeof(LockWord) == sizeof(int32_t),
7337 "art::LockWord and int32_t have different sizes.");
7338
7339 __ Sync(0); // Barrier to prevent load-load reordering.
7340
7341 // The actual reference load.
7342 if (index.IsValid()) {
7343 // Load types involving an "index": ArrayGet,
7344 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7345 // intrinsics.
7346 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7347 if (index.IsConstant()) {
7348 size_t computed_offset =
7349 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7350 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7351 } else {
7352 // Handle the special case of the
7353 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7354 // intrinsics, which use a register pair as index ("long
7355 // offset"), of which only the low part contains data.
7356 Register index_reg = index.IsRegisterPair()
7357 ? index.AsRegisterPairLow<Register>()
7358 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007359 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007360 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7361 }
7362 } else {
7363 // /* HeapReference<Object> */ ref = *(obj + offset)
7364 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7365 }
7366
7367 // Object* ref = ref_addr->AsMirrorPtr()
7368 __ MaybeUnpoisonHeapReference(ref_reg);
7369
7370 // Slow path marking the object `ref` when it is gray.
7371 SlowPathCodeMIPS* slow_path;
7372 if (always_update_field) {
7373 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7374 // of the form `obj + field_offset`, where `obj` is a register and
7375 // `field_offset` is a register pair (of which only the lower half
7376 // is used). Thus `offset` and `scale_factor` above are expected
7377 // to be null in this code path.
7378 DCHECK_EQ(offset, 0u);
7379 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007380 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007381 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7382 ref,
7383 obj,
7384 /* field_offset */ index,
7385 temp_reg);
7386 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007387 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007388 }
7389 AddSlowPath(slow_path);
7390
7391 // if (rb_state == ReadBarrier::GrayState())
7392 // ref = ReadBarrier::Mark(ref);
7393 // Given the numeric representation, it's enough to check the low bit of the
7394 // rb_state. We do that by shifting the bit into the sign bit (31) and
7395 // performing a branch on less than zero.
7396 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7397 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7398 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7399 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7400 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7401 __ Bind(slow_path->GetExitLabel());
7402}
7403
7404void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7405 Location out,
7406 Location ref,
7407 Location obj,
7408 uint32_t offset,
7409 Location index) {
7410 DCHECK(kEmitCompilerReadBarrier);
7411
7412 // Insert a slow path based read barrier *after* the reference load.
7413 //
7414 // If heap poisoning is enabled, the unpoisoning of the loaded
7415 // reference will be carried out by the runtime within the slow
7416 // path.
7417 //
7418 // Note that `ref` currently does not get unpoisoned (when heap
7419 // poisoning is enabled), which is alright as the `ref` argument is
7420 // not used by the artReadBarrierSlow entry point.
7421 //
7422 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007423 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007424 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7425 AddSlowPath(slow_path);
7426
7427 __ B(slow_path->GetEntryLabel());
7428 __ Bind(slow_path->GetExitLabel());
7429}
7430
7431void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7432 Location out,
7433 Location ref,
7434 Location obj,
7435 uint32_t offset,
7436 Location index) {
7437 if (kEmitCompilerReadBarrier) {
7438 // Baker's read barriers shall be handled by the fast path
7439 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7440 DCHECK(!kUseBakerReadBarrier);
7441 // If heap poisoning is enabled, unpoisoning will be taken care of
7442 // by the runtime within the slow path.
7443 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7444 } else if (kPoisonHeapReferences) {
7445 __ UnpoisonHeapReference(out.AsRegister<Register>());
7446 }
7447}
7448
7449void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7450 Location out,
7451 Location root) {
7452 DCHECK(kEmitCompilerReadBarrier);
7453
7454 // Insert a slow path based read barrier *after* the GC root load.
7455 //
7456 // Note that GC roots are not affected by heap poisoning, so we do
7457 // not need to do anything special for this here.
7458 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007459 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007460 AddSlowPath(slow_path);
7461
7462 __ B(slow_path->GetEntryLabel());
7463 __ Bind(slow_path->GetExitLabel());
7464}
7465
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007466void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007467 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7468 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007469 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007470 switch (type_check_kind) {
7471 case TypeCheckKind::kExactCheck:
7472 case TypeCheckKind::kAbstractClassCheck:
7473 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007474 case TypeCheckKind::kArrayObjectCheck: {
7475 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7476 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7477 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007478 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007479 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007480 case TypeCheckKind::kArrayCheck:
7481 case TypeCheckKind::kUnresolvedCheck:
7482 case TypeCheckKind::kInterfaceCheck:
7483 call_kind = LocationSummary::kCallOnSlowPath;
7484 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007485 case TypeCheckKind::kBitstringCheck:
7486 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007487 }
7488
Vladimir Markoca6fff82017-10-03 14:49:14 +01007489 LocationSummary* locations =
7490 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007491 if (baker_read_barrier_slow_path) {
7492 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7493 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007494 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007495 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7496 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7497 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7498 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7499 } else {
7500 locations->SetInAt(1, Location::RequiresRegister());
7501 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007502 // The output does overlap inputs.
7503 // Note that TypeCheckSlowPathMIPS uses this register too.
7504 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007505 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007506}
7507
7508void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007509 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007510 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007511 Location obj_loc = locations->InAt(0);
7512 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko175e7862018-03-27 09:03:13 +00007513 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08007514 Location out_loc = locations->Out();
7515 Register out = out_loc.AsRegister<Register>();
7516 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7517 DCHECK_LE(num_temps, 1u);
7518 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007519 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7520 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7521 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7522 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007523 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007524 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007525
7526 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007527 // Avoid this check if we know `obj` is not null.
7528 if (instruction->MustDoNullCheck()) {
7529 __ Move(out, ZERO);
7530 __ Beqz(obj, &done);
7531 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007532
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007533 switch (type_check_kind) {
7534 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007535 ReadBarrierOption read_barrier_option =
7536 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007537 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007538 GenerateReferenceLoadTwoRegisters(instruction,
7539 out_loc,
7540 obj_loc,
7541 class_offset,
7542 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007543 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007544 // Classes must be equal for the instanceof to succeed.
Vladimir Marko175e7862018-03-27 09:03:13 +00007545 __ Xor(out, out, cls.AsRegister<Register>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007546 __ Sltiu(out, out, 1);
7547 break;
7548 }
7549
7550 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007551 ReadBarrierOption read_barrier_option =
7552 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007553 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007554 GenerateReferenceLoadTwoRegisters(instruction,
7555 out_loc,
7556 obj_loc,
7557 class_offset,
7558 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007559 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007560 // If the class is abstract, we eagerly fetch the super class of the
7561 // object to avoid doing a comparison we know will fail.
7562 MipsLabel loop;
7563 __ Bind(&loop);
7564 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007565 GenerateReferenceLoadOneRegister(instruction,
7566 out_loc,
7567 super_offset,
7568 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007569 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007570 // If `out` is null, we use it for the result, and jump to `done`.
7571 __ Beqz(out, &done);
Vladimir Marko175e7862018-03-27 09:03:13 +00007572 __ Bne(out, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007573 __ LoadConst32(out, 1);
7574 break;
7575 }
7576
7577 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007578 ReadBarrierOption read_barrier_option =
7579 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007580 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007581 GenerateReferenceLoadTwoRegisters(instruction,
7582 out_loc,
7583 obj_loc,
7584 class_offset,
7585 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007586 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007587 // Walk over the class hierarchy to find a match.
7588 MipsLabel loop, success;
7589 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00007590 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007591 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007592 GenerateReferenceLoadOneRegister(instruction,
7593 out_loc,
7594 super_offset,
7595 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007596 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007597 __ Bnez(out, &loop);
7598 // If `out` is null, we use it for the result, and jump to `done`.
7599 __ B(&done);
7600 __ Bind(&success);
7601 __ LoadConst32(out, 1);
7602 break;
7603 }
7604
7605 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007606 ReadBarrierOption read_barrier_option =
7607 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007608 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007609 GenerateReferenceLoadTwoRegisters(instruction,
7610 out_loc,
7611 obj_loc,
7612 class_offset,
7613 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007614 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007615 // Do an exact check.
7616 MipsLabel success;
Vladimir Marko175e7862018-03-27 09:03:13 +00007617 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007618 // Otherwise, we need to check that the object's class is a non-primitive array.
7619 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007620 GenerateReferenceLoadOneRegister(instruction,
7621 out_loc,
7622 component_offset,
7623 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007624 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007625 // If `out` is null, we use it for the result, and jump to `done`.
7626 __ Beqz(out, &done);
7627 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7628 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7629 __ Sltiu(out, out, 1);
7630 __ B(&done);
7631 __ Bind(&success);
7632 __ LoadConst32(out, 1);
7633 break;
7634 }
7635
7636 case TypeCheckKind::kArrayCheck: {
7637 // No read barrier since the slow path will retry upon failure.
7638 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007639 GenerateReferenceLoadTwoRegisters(instruction,
7640 out_loc,
7641 obj_loc,
7642 class_offset,
7643 maybe_temp_loc,
7644 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007645 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007646 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7647 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007648 codegen_->AddSlowPath(slow_path);
Vladimir Marko175e7862018-03-27 09:03:13 +00007649 __ Bne(out, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007650 __ LoadConst32(out, 1);
7651 break;
7652 }
7653
7654 case TypeCheckKind::kUnresolvedCheck:
7655 case TypeCheckKind::kInterfaceCheck: {
7656 // Note that we indeed only call on slow path, but we always go
7657 // into the slow path for the unresolved and interface check
7658 // cases.
7659 //
7660 // We cannot directly call the InstanceofNonTrivial runtime
7661 // entry point without resorting to a type checking slow path
7662 // here (i.e. by calling InvokeRuntime directly), as it would
7663 // require to assign fixed registers for the inputs of this
7664 // HInstanceOf instruction (following the runtime calling
7665 // convention), which might be cluttered by the potential first
7666 // read barrier emission at the beginning of this method.
7667 //
7668 // TODO: Introduce a new runtime entry point taking the object
7669 // to test (instead of its class) as argument, and let it deal
7670 // with the read barrier issues. This will let us refactor this
7671 // case of the `switch` code as it was previously (with a direct
7672 // call to the runtime not using a type checking slow path).
7673 // This should also be beneficial for the other cases above.
7674 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007675 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7676 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007677 codegen_->AddSlowPath(slow_path);
7678 __ B(slow_path->GetEntryLabel());
7679 break;
7680 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007681
7682 case TypeCheckKind::kBitstringCheck: {
7683 // /* HeapReference<Class> */ temp = obj->klass_
7684 GenerateReferenceLoadTwoRegisters(instruction,
7685 out_loc,
7686 obj_loc,
7687 class_offset,
7688 maybe_temp_loc,
7689 kWithoutReadBarrier);
7690
7691 GenerateBitstringTypeCheckCompare(instruction, out);
7692 __ Sltiu(out, out, 1);
7693 break;
7694 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007695 }
7696
7697 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007698
7699 if (slow_path != nullptr) {
7700 __ Bind(slow_path->GetExitLabel());
7701 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007702}
7703
7704void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007705 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007706 locations->SetOut(Location::ConstantLocation(constant));
7707}
7708
7709void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7710 // Will be generated at use site.
7711}
7712
7713void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007714 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007715 locations->SetOut(Location::ConstantLocation(constant));
7716}
7717
7718void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7719 // Will be generated at use site.
7720}
7721
7722void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7723 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7724 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7725}
7726
7727void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7728 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007729 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007730 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007731 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007732}
7733
7734void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7735 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7736 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007737 Location receiver = invoke->GetLocations()->InAt(0);
7738 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007739 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007740
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007741 // temp = object->GetClass();
7742 if (receiver.IsStackSlot()) {
7743 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7744 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7745 } else {
7746 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7747 }
7748 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007749 // Instead of simply (possibly) unpoisoning `temp` here, we should
7750 // emit a read barrier for the previous class reference load.
7751 // However this is not required in practice, as this is an
7752 // intermediate/temporary reference and because the current
7753 // concurrent copying collector keeps the from-space memory
7754 // intact/accessible until the end of the marking phase (the
7755 // concurrent copying collector may not in the future).
7756 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007757 __ LoadFromOffset(kLoadWord, temp, temp,
7758 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7759 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007760 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007761 // temp = temp->GetImtEntryAt(method_offset);
7762 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7763 // T9 = temp->GetEntryPoint();
7764 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
Lena Djokic3177e102018-02-28 11:32:40 +01007765 // Set the hidden argument.
7766 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7767 invoke->GetDexMethodIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007768 // T9();
7769 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007770 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007771 DCHECK(!codegen_->IsLeafMethod());
7772 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7773}
7774
7775void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007776 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7777 if (intrinsic.TryDispatch(invoke)) {
7778 return;
7779 }
7780
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007781 HandleInvoke(invoke);
7782}
7783
7784void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007785 // Explicit clinit checks triggered by static invokes must have been pruned by
7786 // art::PrepareForRegisterAllocation.
7787 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007788
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007789 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007790 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7791 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007792
Chris Larsen701566a2015-10-27 15:29:13 -07007793 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7794 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007795 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7796 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7797 }
Chris Larsen701566a2015-10-27 15:29:13 -07007798 return;
7799 }
7800
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007801 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007802
7803 // Add the extra input register if either the dex cache array base register
7804 // or the PC-relative base register for accessing literals is needed.
7805 if (has_extra_input) {
7806 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7807 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007808}
7809
Orion Hodsonac141392017-01-13 11:53:47 +00007810void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7811 HandleInvoke(invoke);
7812}
7813
7814void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7815 codegen_->GenerateInvokePolymorphicCall(invoke);
7816}
7817
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007818void LocationsBuilderMIPS::VisitInvokeCustom(HInvokeCustom* invoke) {
7819 HandleInvoke(invoke);
7820}
7821
7822void InstructionCodeGeneratorMIPS::VisitInvokeCustom(HInvokeCustom* invoke) {
7823 codegen_->GenerateInvokeCustomCall(invoke);
7824}
7825
Chris Larsen701566a2015-10-27 15:29:13 -07007826static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007827 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007828 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7829 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007830 return true;
7831 }
7832 return false;
7833}
7834
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007835HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007836 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007837 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007838 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007839 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007840 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007841 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007842 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007843 case HLoadString::LoadKind::kJitTableAddress:
7844 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007845 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007846 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007847 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007848 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007849 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007850 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007851}
7852
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007853HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7854 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007855 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007856 case HLoadClass::LoadKind::kInvalid:
7857 LOG(FATAL) << "UNREACHABLE";
7858 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007859 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007860 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007861 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007862 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007863 case HLoadClass::LoadKind::kBssEntry:
7864 DCHECK(!Runtime::Current()->UseJitCompilation());
7865 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007866 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007867 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007868 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007869 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007870 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007871 break;
7872 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007873 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007874}
7875
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007876Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7877 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007878 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007879 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007880 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7881 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7882 if (!invoke->GetLocations()->Intrinsified()) {
7883 return location.AsRegister<Register>();
7884 }
7885 // For intrinsics we allow any location, so it may be on the stack.
7886 if (!location.IsRegister()) {
7887 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7888 return temp;
7889 }
7890 // For register locations, check if the register was saved. If so, get it from the stack.
7891 // Note: There is a chance that the register was saved but not overwritten, so we could
7892 // save one load. However, since this is just an intrinsic slow path we prefer this
7893 // simple and more robust approach rather that trying to determine if that's the case.
7894 SlowPathCode* slow_path = GetCurrentSlowPath();
7895 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7896 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7897 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7898 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7899 return temp;
7900 }
7901 return location.AsRegister<Register>();
7902}
7903
Vladimir Markodc151b22015-10-15 18:02:30 +01007904HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7905 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007906 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007907 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007908}
7909
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007910void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7911 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007912 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007913 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007914 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7915 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007916 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007917 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7918 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007919 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7920 : ZERO;
7921
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007922 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007923 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007924 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007925 uint32_t offset =
7926 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007927 __ LoadFromOffset(kLoadWord,
7928 temp.AsRegister<Register>(),
7929 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007930 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007931 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007932 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007933 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007934 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007935 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007936 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7937 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007938 PcRelativePatchInfo* info_high = NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007939 PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007940 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007941 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007942 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7943 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007944 break;
7945 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007946 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7947 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7948 break;
Vladimir Markob066d432018-01-03 13:14:37 +00007949 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007950 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00007951 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
7952 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
7953 Register temp_reg = temp.AsRegister<Register>();
7954 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7955 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
7956 break;
7957 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007958 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007959 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007960 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007961 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7962 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007963 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007964 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7965 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007966 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007967 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007968 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7969 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7970 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007971 }
7972 }
7973
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007974 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007975 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007976 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007977 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007978 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7979 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007980 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007981 T9,
7982 callee_method.AsRegister<Register>(),
7983 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007984 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007985 // T9()
7986 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007987 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007988 break;
7989 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007990 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7991
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007992 DCHECK(!IsLeafMethod());
7993}
7994
7995void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007996 // Explicit clinit checks triggered by static invokes must have been pruned by
7997 // art::PrepareForRegisterAllocation.
7998 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007999
8000 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8001 return;
8002 }
8003
8004 LocationSummary* locations = invoke->GetLocations();
8005 codegen_->GenerateStaticOrDirectCall(invoke,
8006 locations->HasTemps()
8007 ? locations->GetTemp(0)
8008 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008009}
8010
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008011void CodeGeneratorMIPS::GenerateVirtualCall(
8012 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02008013 // Use the calling convention instead of the location of the receiver, as
8014 // intrinsics may have put the receiver in a different register. In the intrinsics
8015 // slow path, the arguments have been moved to the right place, so here we are
8016 // guaranteed that the receiver is the first register of the calling convention.
8017 InvokeDexCallingConvention calling_convention;
8018 Register receiver = calling_convention.GetRegisterAt(0);
8019
Chris Larsen3acee732015-11-18 13:31:08 -08008020 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008021 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8022 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
8023 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07008024 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008025
8026 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02008027 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08008028 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08008029 // Instead of simply (possibly) unpoisoning `temp` here, we should
8030 // emit a read barrier for the previous class reference load.
8031 // However this is not required in practice, as this is an
8032 // intermediate/temporary reference and because the current
8033 // concurrent copying collector keeps the from-space memory
8034 // intact/accessible until the end of the marking phase (the
8035 // concurrent copying collector may not in the future).
8036 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008037 // temp = temp->GetMethodAt(method_offset);
8038 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8039 // T9 = temp->GetEntryPoint();
8040 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
8041 // T9();
8042 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008043 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008044 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08008045}
8046
8047void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
8048 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8049 return;
8050 }
8051
8052 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008053 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008054}
8055
8056void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00008057 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008058 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008059 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008060 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8061 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008062 return;
8063 }
Vladimir Marko41559982017-01-06 14:04:23 +00008064 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008065 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008066 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08008067 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
8068 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07008069 ? LocationSummary::kCallOnSlowPath
8070 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008071 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008072 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
8073 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
8074 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008075 switch (load_kind) {
8076 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008077 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008078 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008079 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008080 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008081 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008082 break;
8083 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008084 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008085 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
8086 codegen_->ClobberRA();
8087 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008088 break;
8089 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008090 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008091 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008092 locations->SetInAt(0, Location::RequiresRegister());
8093 break;
8094 default:
8095 break;
8096 }
8097 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008098 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
8099 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8100 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008101 RegisterSet caller_saves = RegisterSet::Empty();
8102 InvokeRuntimeCallingConvention calling_convention;
8103 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8104 locations->SetCustomSlowPathCallerSaves(caller_saves);
8105 } else {
8106 // For non-Baker read barriers we have a temp-clobbering call.
8107 }
8108 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008109}
8110
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008111// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8112// move.
8113void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00008114 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008115 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00008116 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01008117 return;
8118 }
Vladimir Marko41559982017-01-06 14:04:23 +00008119 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008120
Vladimir Marko41559982017-01-06 14:04:23 +00008121 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008122 Location out_loc = locations->Out();
8123 Register out = out_loc.AsRegister<Register>();
8124 Register base_or_current_method_reg;
8125 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008126 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008127 switch (load_kind) {
8128 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008129 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008130 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008131 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008132 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008133 base_or_current_method_reg =
8134 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008135 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008136 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008137 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008138 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8139 break;
8140 default:
8141 base_or_current_method_reg = ZERO;
8142 break;
8143 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008144
Alexey Frunze15958152017-02-09 19:08:30 -08008145 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8146 ? kWithoutReadBarrier
8147 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008148 bool generate_null_check = false;
8149 switch (load_kind) {
8150 case HLoadClass::LoadKind::kReferrersClass: {
8151 DCHECK(!cls->CanCallRuntime());
8152 DCHECK(!cls->MustGenerateClinitCheck());
8153 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8154 GenerateGcRootFieldLoad(cls,
8155 out_loc,
8156 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008157 ArtMethod::DeclaringClassOffset().Int32Value(),
8158 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008159 break;
8160 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008161 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008162 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008163 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008164 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008165 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008166 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008167 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008168 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8169 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008170 base_or_current_method_reg);
8171 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008172 break;
8173 }
8174 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08008175 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008176 uint32_t address = dchecked_integral_cast<uint32_t>(
8177 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
8178 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008179 if (isR6 || !has_irreducible_loops) {
8180 __ LoadLiteral(out,
8181 base_or_current_method_reg,
8182 codegen_->DeduplicateBootImageAddressLiteral(address));
8183 } else {
8184 __ LoadConst32(out, address);
8185 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008186 break;
8187 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008188 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008189 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008190 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008191 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008192 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008193 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008194 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008195 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8196 out,
8197 base_or_current_method_reg);
8198 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008199 break;
8200 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008201 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008202 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8203 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008204 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8205 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008206 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008207 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008208 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008209 GenerateGcRootFieldLoad(cls,
8210 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008211 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008212 /* placeholder */ 0x5678,
8213 read_barrier_option,
8214 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008215 generate_null_check = true;
8216 break;
8217 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008218 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008219 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8220 cls->GetTypeIndex(),
8221 cls->GetClass());
8222 bool reordering = __ SetReorder(false);
8223 __ Bind(&info->high_label);
8224 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008225 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008226 GenerateGcRootFieldLoad(cls,
8227 out_loc,
8228 out,
8229 /* placeholder */ 0x5678,
8230 read_barrier_option,
8231 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008232 break;
8233 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008234 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008235 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008236 LOG(FATAL) << "UNREACHABLE";
8237 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008238 }
8239
8240 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8241 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008242 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00008243 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07008244 codegen_->AddSlowPath(slow_path);
8245 if (generate_null_check) {
8246 __ Beqz(out, slow_path->GetEntryLabel());
8247 }
8248 if (cls->MustGenerateClinitCheck()) {
8249 GenerateClassInitializationCheck(slow_path, out);
8250 } else {
8251 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008252 }
8253 }
8254}
8255
Orion Hodsondbaa5c72018-05-10 08:22:46 +01008256void LocationsBuilderMIPS::VisitLoadMethodHandle(HLoadMethodHandle* load) {
8257 InvokeRuntimeCallingConvention calling_convention;
8258 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8259 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, loc, loc);
8260}
8261
8262void InstructionCodeGeneratorMIPS::VisitLoadMethodHandle(HLoadMethodHandle* load) {
8263 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
8264}
8265
Orion Hodson18259d72018-04-12 11:18:23 +01008266void LocationsBuilderMIPS::VisitLoadMethodType(HLoadMethodType* load) {
8267 InvokeRuntimeCallingConvention calling_convention;
8268 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8269 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, loc, loc);
8270}
8271
8272void InstructionCodeGeneratorMIPS::VisitLoadMethodType(HLoadMethodType* load) {
8273 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
8274}
8275
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008276static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008277 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008278}
8279
8280void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8281 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008282 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008283 locations->SetOut(Location::RequiresRegister());
8284}
8285
8286void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8287 Register out = load->GetLocations()->Out().AsRegister<Register>();
8288 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8289}
8290
8291void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008292 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008293}
8294
8295void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8296 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8297}
8298
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008299void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008300 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008301 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008302 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008303 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008304 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008305 switch (load_kind) {
8306 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008307 case HLoadString::LoadKind::kBootImageAddress:
8308 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008309 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008310 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008311 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008312 break;
8313 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008314 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008315 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
8316 codegen_->ClobberRA();
8317 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008318 break;
8319 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008320 FALLTHROUGH_INTENDED;
8321 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008322 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008323 locations->SetInAt(0, Location::RequiresRegister());
8324 break;
8325 default:
8326 break;
8327 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008328 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008329 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008330 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008331 } else {
8332 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008333 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8334 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8335 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008336 RegisterSet caller_saves = RegisterSet::Empty();
8337 InvokeRuntimeCallingConvention calling_convention;
8338 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8339 locations->SetCustomSlowPathCallerSaves(caller_saves);
8340 } else {
8341 // For non-Baker read barriers we have a temp-clobbering call.
8342 }
8343 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008344 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008345}
8346
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008347// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8348// move.
8349void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008350 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008351 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008352 Location out_loc = locations->Out();
8353 Register out = out_loc.AsRegister<Register>();
8354 Register base_or_current_method_reg;
8355 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008356 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008357 switch (load_kind) {
8358 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008359 case HLoadString::LoadKind::kBootImageAddress:
8360 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008361 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008362 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008363 base_or_current_method_reg =
8364 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008365 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008366 default:
8367 base_or_current_method_reg = ZERO;
8368 break;
8369 }
8370
8371 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008372 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008373 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008374 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008375 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008376 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008377 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008378 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8379 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008380 base_or_current_method_reg);
8381 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008382 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008383 }
8384 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008385 uint32_t address = dchecked_integral_cast<uint32_t>(
8386 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8387 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008388 if (isR6 || !has_irreducible_loops) {
8389 __ LoadLiteral(out,
8390 base_or_current_method_reg,
8391 codegen_->DeduplicateBootImageAddressLiteral(address));
8392 } else {
8393 __ LoadConst32(out, address);
8394 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008395 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008396 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008397 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008398 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008399 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008400 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008401 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008402 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008403 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008404 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8405 out,
8406 base_or_current_method_reg);
8407 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8408 return;
8409 }
8410 case HLoadString::LoadKind::kBssEntry: {
8411 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8412 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8413 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8414 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8415 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008416 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008417 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008418 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008419 GenerateGcRootFieldLoad(load,
8420 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008421 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008422 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008423 kCompilerReadBarrierOption,
8424 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008425 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008426 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008427 codegen_->AddSlowPath(slow_path);
8428 __ Beqz(out, slow_path->GetEntryLabel());
8429 __ Bind(slow_path->GetExitLabel());
8430 return;
8431 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008432 case HLoadString::LoadKind::kJitTableAddress: {
8433 CodeGeneratorMIPS::JitPatchInfo* info =
8434 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8435 load->GetStringIndex(),
8436 load->GetString());
8437 bool reordering = __ SetReorder(false);
8438 __ Bind(&info->high_label);
8439 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008440 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008441 GenerateGcRootFieldLoad(load,
8442 out_loc,
8443 out,
8444 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008445 kCompilerReadBarrierOption,
8446 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008447 return;
8448 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008449 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008450 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008451 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008452
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008453 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008454 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008455 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008456 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008457 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008458 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8459 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008460}
8461
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008462void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008463 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008464 locations->SetOut(Location::ConstantLocation(constant));
8465}
8466
8467void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8468 // Will be generated at use site.
8469}
8470
8471void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008472 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8473 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008474 InvokeRuntimeCallingConvention calling_convention;
8475 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8476}
8477
8478void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8479 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008480 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008481 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8482 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008483 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008484 }
8485 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8486}
8487
8488void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8489 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008490 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008491 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008492 case DataType::Type::kInt32:
8493 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008494 locations->SetInAt(0, Location::RequiresRegister());
8495 locations->SetInAt(1, Location::RequiresRegister());
8496 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8497 break;
8498
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008499 case DataType::Type::kFloat32:
8500 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008501 locations->SetInAt(0, Location::RequiresFpuRegister());
8502 locations->SetInAt(1, Location::RequiresFpuRegister());
8503 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8504 break;
8505
8506 default:
8507 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8508 }
8509}
8510
8511void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008512 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008513 LocationSummary* locations = instruction->GetLocations();
8514 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8515
8516 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008517 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008518 Register dst = locations->Out().AsRegister<Register>();
8519 Register lhs = locations->InAt(0).AsRegister<Register>();
8520 Register rhs = locations->InAt(1).AsRegister<Register>();
8521
8522 if (isR6) {
8523 __ MulR6(dst, lhs, rhs);
8524 } else {
8525 __ MulR2(dst, lhs, rhs);
8526 }
8527 break;
8528 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008529 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008530 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8531 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8532 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8533 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8534 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8535 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8536
8537 // Extra checks to protect caused by the existance of A1_A2.
8538 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8539 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8540 DCHECK_NE(dst_high, lhs_low);
8541 DCHECK_NE(dst_high, rhs_low);
8542
8543 // A_B * C_D
8544 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8545 // dst_lo: [ low(B*D) ]
8546 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8547
8548 if (isR6) {
8549 __ MulR6(TMP, lhs_high, rhs_low);
8550 __ MulR6(dst_high, lhs_low, rhs_high);
8551 __ Addu(dst_high, dst_high, TMP);
8552 __ MuhuR6(TMP, lhs_low, rhs_low);
8553 __ Addu(dst_high, dst_high, TMP);
8554 __ MulR6(dst_low, lhs_low, rhs_low);
8555 } else {
8556 __ MulR2(TMP, lhs_high, rhs_low);
8557 __ MulR2(dst_high, lhs_low, rhs_high);
8558 __ Addu(dst_high, dst_high, TMP);
8559 __ MultuR2(lhs_low, rhs_low);
8560 __ Mfhi(TMP);
8561 __ Addu(dst_high, dst_high, TMP);
8562 __ Mflo(dst_low);
8563 }
8564 break;
8565 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008566 case DataType::Type::kFloat32:
8567 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008568 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8569 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8570 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008571 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008572 __ MulS(dst, lhs, rhs);
8573 } else {
8574 __ MulD(dst, lhs, rhs);
8575 }
8576 break;
8577 }
8578 default:
8579 LOG(FATAL) << "Unexpected mul type " << type;
8580 }
8581}
8582
8583void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8584 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008585 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008586 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008587 case DataType::Type::kInt32:
8588 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008589 locations->SetInAt(0, Location::RequiresRegister());
8590 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8591 break;
8592
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008593 case DataType::Type::kFloat32:
8594 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008595 locations->SetInAt(0, Location::RequiresFpuRegister());
8596 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8597 break;
8598
8599 default:
8600 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8601 }
8602}
8603
8604void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008605 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008606 LocationSummary* locations = instruction->GetLocations();
8607
8608 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008609 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008610 Register dst = locations->Out().AsRegister<Register>();
8611 Register src = locations->InAt(0).AsRegister<Register>();
8612 __ Subu(dst, ZERO, src);
8613 break;
8614 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008615 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008616 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8617 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8618 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8619 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8620 __ Subu(dst_low, ZERO, src_low);
8621 __ Sltu(TMP, ZERO, dst_low);
8622 __ Subu(dst_high, ZERO, src_high);
8623 __ Subu(dst_high, dst_high, TMP);
8624 break;
8625 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008626 case DataType::Type::kFloat32:
8627 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008628 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8629 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008630 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008631 __ NegS(dst, src);
8632 } else {
8633 __ NegD(dst, src);
8634 }
8635 break;
8636 }
8637 default:
8638 LOG(FATAL) << "Unexpected neg type " << type;
8639 }
8640}
8641
8642void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008643 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8644 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008645 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008646 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008647 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8648 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008649}
8650
8651void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008652 // Note: if heap poisoning is enabled, the entry point takes care
8653 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008654 QuickEntrypointEnum entrypoint =
8655 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8656 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008657 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008658 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008659}
8660
8661void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008662 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8663 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008664 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008665 if (instruction->IsStringAlloc()) {
8666 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8667 } else {
8668 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008669 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008670 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008671}
8672
8673void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008674 // Note: if heap poisoning is enabled, the entry point takes care
8675 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008676 if (instruction->IsStringAlloc()) {
8677 // String is allocated through StringFactory. Call NewEmptyString entry point.
8678 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008679 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008680 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8681 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8682 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008683 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008684 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8685 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008686 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008687 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008688 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008689}
8690
8691void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008692 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008693 locations->SetInAt(0, Location::RequiresRegister());
8694 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8695}
8696
8697void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008698 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008699 LocationSummary* locations = instruction->GetLocations();
8700
8701 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008702 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008703 Register dst = locations->Out().AsRegister<Register>();
8704 Register src = locations->InAt(0).AsRegister<Register>();
8705 __ Nor(dst, src, ZERO);
8706 break;
8707 }
8708
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008709 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008710 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8711 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8712 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8713 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8714 __ Nor(dst_high, src_high, ZERO);
8715 __ Nor(dst_low, src_low, ZERO);
8716 break;
8717 }
8718
8719 default:
8720 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8721 }
8722}
8723
8724void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008725 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008726 locations->SetInAt(0, Location::RequiresRegister());
8727 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8728}
8729
8730void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8731 LocationSummary* locations = instruction->GetLocations();
8732 __ Xori(locations->Out().AsRegister<Register>(),
8733 locations->InAt(0).AsRegister<Register>(),
8734 1);
8735}
8736
8737void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008738 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8739 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008740}
8741
Calin Juravle2ae48182016-03-16 14:05:09 +00008742void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8743 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008744 return;
8745 }
8746 Location obj = instruction->GetLocations()->InAt(0);
8747
8748 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008749 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008750}
8751
Calin Juravle2ae48182016-03-16 14:05:09 +00008752void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008753 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008754 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008755
8756 Location obj = instruction->GetLocations()->InAt(0);
8757
8758 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8759}
8760
8761void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008762 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008763}
8764
8765void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8766 HandleBinaryOp(instruction);
8767}
8768
8769void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8770 HandleBinaryOp(instruction);
8771}
8772
8773void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8774 LOG(FATAL) << "Unreachable";
8775}
8776
8777void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008778 if (instruction->GetNext()->IsSuspendCheck() &&
8779 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8780 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8781 // The back edge will generate the suspend check.
8782 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8783 }
8784
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008785 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8786}
8787
8788void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008789 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008790 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8791 if (location.IsStackSlot()) {
8792 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8793 } else if (location.IsDoubleStackSlot()) {
8794 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8795 }
8796 locations->SetOut(location);
8797}
8798
8799void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8800 ATTRIBUTE_UNUSED) {
8801 // Nothing to do, the parameter is already at its location.
8802}
8803
8804void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8805 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008806 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008807 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8808}
8809
8810void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8811 ATTRIBUTE_UNUSED) {
8812 // Nothing to do, the method is already at its location.
8813}
8814
8815void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008816 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008817 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008818 locations->SetInAt(i, Location::Any());
8819 }
8820 locations->SetOut(Location::Any());
8821}
8822
8823void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8824 LOG(FATAL) << "Unreachable";
8825}
8826
8827void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008828 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008829 bool call_rem;
8830 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8831 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8832 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8833 } else {
8834 call_rem = (type != DataType::Type::kInt32);
8835 }
8836 LocationSummary::CallKind call_kind = call_rem
8837 ? LocationSummary::kCallOnMainOnly
8838 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008839 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008840
8841 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008842 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008843 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008844 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008845 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8846 break;
8847
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008848 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008849 if (call_rem) {
8850 InvokeRuntimeCallingConvention calling_convention;
8851 locations->SetInAt(0, Location::RegisterPairLocation(
8852 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8853 locations->SetInAt(1, Location::RegisterPairLocation(
8854 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8855 locations->SetOut(calling_convention.GetReturnLocation(type));
8856 } else {
8857 locations->SetInAt(0, Location::RequiresRegister());
8858 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8859 locations->SetOut(Location::RequiresRegister());
8860 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008861 break;
8862 }
8863
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008864 case DataType::Type::kFloat32:
8865 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008866 InvokeRuntimeCallingConvention calling_convention;
8867 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8868 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8869 locations->SetOut(calling_convention.GetReturnLocation(type));
8870 break;
8871 }
8872
8873 default:
8874 LOG(FATAL) << "Unexpected rem type " << type;
8875 }
8876}
8877
8878void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008879 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008880 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008881
8882 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008883 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008884 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008885 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008886 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008887 if (locations->InAt(1).IsConstant()) {
8888 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8889 if (imm == 0) {
8890 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8891 } else if (imm == 1 || imm == -1) {
8892 DivRemOneOrMinusOne(instruction);
8893 } else {
8894 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8895 DivRemByPowerOfTwo(instruction);
8896 }
8897 } else {
8898 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8899 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8900 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008901 break;
8902 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008903 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008904 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008905 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008906 break;
8907 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008908 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008909 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008910 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008911 break;
8912 }
8913 default:
8914 LOG(FATAL) << "Unexpected rem type " << type;
8915 }
8916}
8917
Aart Bik1f8d51b2018-02-15 10:42:37 -08008918static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
8919 LocationSummary* locations = new (allocator) LocationSummary(minmax);
8920 switch (minmax->GetResultType()) {
8921 case DataType::Type::kInt32:
8922 case DataType::Type::kInt64:
8923 locations->SetInAt(0, Location::RequiresRegister());
8924 locations->SetInAt(1, Location::RequiresRegister());
8925 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8926 break;
8927 case DataType::Type::kFloat32:
8928 case DataType::Type::kFloat64:
8929 locations->SetInAt(0, Location::RequiresFpuRegister());
8930 locations->SetInAt(1, Location::RequiresFpuRegister());
8931 locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap);
8932 break;
8933 default:
8934 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
8935 }
8936}
8937
Aart Bik351df3e2018-03-07 11:54:57 -08008938void InstructionCodeGeneratorMIPS::GenerateMinMaxInt(LocationSummary* locations,
8939 bool is_min,
8940 bool isR6,
8941 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08008942 if (isR6) {
8943 // Some architectures, such as ARM and MIPS (prior to r6), have a
8944 // conditional move instruction which only changes the target
8945 // (output) register if the condition is true (MIPS prior to r6 had
8946 // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions
8947 // always change the target (output) register. If the condition is
8948 // true the output register gets the contents of the "rs" register;
8949 // otherwise, the output register is set to zero. One consequence
8950 // of this is that to implement something like "rd = c==0 ? rs : rt"
8951 // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions.
8952 // After executing this pair of instructions one of the output
8953 // registers from the pair will necessarily contain zero. Then the
8954 // code ORs the output registers from the SELEQZ/SELNEZ instructions
8955 // to get the final result.
8956 //
8957 // The initial test to see if the output register is same as the
8958 // first input register is needed to make sure that value in the
8959 // first input register isn't clobbered before we've finished
8960 // computing the output value. The logic in the corresponding else
8961 // clause performs the same task but makes sure the second input
8962 // register isn't clobbered in the event that it's the same register
8963 // as the output register; the else clause also handles the case
8964 // where the output register is distinct from both the first, and the
8965 // second input registers.
8966 if (type == DataType::Type::kInt64) {
8967 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8968 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8969 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
8970 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
8971 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8972 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8973
8974 MipsLabel compare_done;
8975
8976 if (a_lo == b_lo) {
8977 if (out_lo != a_lo) {
8978 __ Move(out_lo, a_lo);
8979 __ Move(out_hi, a_hi);
8980 }
8981 } else {
8982 __ Slt(TMP, b_hi, a_hi);
8983 __ Bne(b_hi, a_hi, &compare_done);
8984
8985 __ Sltu(TMP, b_lo, a_lo);
8986
8987 __ Bind(&compare_done);
8988
8989 if (is_min) {
8990 __ Seleqz(AT, a_lo, TMP);
8991 __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo
8992 // because at this point we're
8993 // done using a_lo/b_lo.
8994 } else {
8995 __ Selnez(AT, a_lo, TMP);
8996 __ Seleqz(out_lo, b_lo, TMP); // ditto
8997 }
8998 __ Or(out_lo, out_lo, AT);
8999 if (is_min) {
9000 __ Seleqz(AT, a_hi, TMP);
9001 __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
9002 } else {
9003 __ Selnez(AT, a_hi, TMP);
9004 __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
9005 }
9006 __ Or(out_hi, out_hi, AT);
9007 }
9008 } else {
9009 DCHECK_EQ(type, DataType::Type::kInt32);
9010 Register a = locations->InAt(0).AsRegister<Register>();
9011 Register b = locations->InAt(1).AsRegister<Register>();
9012 Register out = locations->Out().AsRegister<Register>();
9013
9014 if (a == b) {
9015 if (out != a) {
9016 __ Move(out, a);
9017 }
9018 } else {
9019 __ Slt(AT, b, a);
9020 if (is_min) {
9021 __ Seleqz(TMP, a, AT);
9022 __ Selnez(AT, b, AT);
9023 } else {
9024 __ Selnez(TMP, a, AT);
9025 __ Seleqz(AT, b, AT);
9026 }
9027 __ Or(out, TMP, AT);
9028 }
9029 }
9030 } else { // !isR6
9031 if (type == DataType::Type::kInt64) {
9032 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9033 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9034 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
9035 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
9036 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9037 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9038
9039 MipsLabel compare_done;
9040
9041 if (a_lo == b_lo) {
9042 if (out_lo != a_lo) {
9043 __ Move(out_lo, a_lo);
9044 __ Move(out_hi, a_hi);
9045 }
9046 } else {
9047 __ Slt(TMP, a_hi, b_hi);
9048 __ Bne(a_hi, b_hi, &compare_done);
9049
9050 __ Sltu(TMP, a_lo, b_lo);
9051
9052 __ Bind(&compare_done);
9053
9054 if (is_min) {
9055 if (out_lo != a_lo) {
9056 __ Movn(out_hi, a_hi, TMP);
9057 __ Movn(out_lo, a_lo, TMP);
9058 }
9059 if (out_lo != b_lo) {
9060 __ Movz(out_hi, b_hi, TMP);
9061 __ Movz(out_lo, b_lo, TMP);
9062 }
9063 } else {
9064 if (out_lo != a_lo) {
9065 __ Movz(out_hi, a_hi, TMP);
9066 __ Movz(out_lo, a_lo, TMP);
9067 }
9068 if (out_lo != b_lo) {
9069 __ Movn(out_hi, b_hi, TMP);
9070 __ Movn(out_lo, b_lo, TMP);
9071 }
9072 }
9073 }
9074 } else {
9075 DCHECK_EQ(type, DataType::Type::kInt32);
9076 Register a = locations->InAt(0).AsRegister<Register>();
9077 Register b = locations->InAt(1).AsRegister<Register>();
9078 Register out = locations->Out().AsRegister<Register>();
9079
9080 if (a == b) {
9081 if (out != a) {
9082 __ Move(out, a);
9083 }
9084 } else {
9085 __ Slt(AT, a, b);
9086 if (is_min) {
9087 if (out != a) {
9088 __ Movn(out, a, AT);
9089 }
9090 if (out != b) {
9091 __ Movz(out, b, AT);
9092 }
9093 } else {
9094 if (out != a) {
9095 __ Movz(out, a, AT);
9096 }
9097 if (out != b) {
9098 __ Movn(out, b, AT);
9099 }
9100 }
9101 }
9102 }
9103 }
9104}
9105
9106void InstructionCodeGeneratorMIPS::GenerateMinMaxFP(LocationSummary* locations,
9107 bool is_min,
9108 bool isR6,
9109 DataType::Type type) {
9110 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9111 FRegister a = locations->InAt(0).AsFpuRegister<FRegister>();
9112 FRegister b = locations->InAt(1).AsFpuRegister<FRegister>();
9113
9114 if (isR6) {
9115 MipsLabel noNaNs;
9116 MipsLabel done;
9117 FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
9118
9119 // When Java computes min/max it prefers a NaN to a number; the
9120 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
9121 // the inputs is a NaN and the other is a valid number, the MIPS
9122 // instruction will return the number; Java wants the NaN value
9123 // returned. This is why there is extra logic preceding the use of
9124 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
9125 // NaN, return the NaN, otherwise return the min/max.
9126 if (type == DataType::Type::kFloat64) {
9127 __ CmpUnD(FTMP, a, b);
9128 __ Bc1eqz(FTMP, &noNaNs);
9129
9130 // One of the inputs is a NaN
9131 __ CmpEqD(ftmp, a, a);
9132 // If a == a then b is the NaN, otherwise a is the NaN.
9133 __ SelD(ftmp, a, b);
9134
9135 if (ftmp != out) {
9136 __ MovD(out, ftmp);
9137 }
9138
9139 __ B(&done);
9140
9141 __ Bind(&noNaNs);
9142
9143 if (is_min) {
9144 __ MinD(out, a, b);
9145 } else {
9146 __ MaxD(out, a, b);
9147 }
9148 } else {
9149 DCHECK_EQ(type, DataType::Type::kFloat32);
9150 __ CmpUnS(FTMP, a, b);
9151 __ Bc1eqz(FTMP, &noNaNs);
9152
9153 // One of the inputs is a NaN
9154 __ CmpEqS(ftmp, a, a);
9155 // If a == a then b is the NaN, otherwise a is the NaN.
9156 __ SelS(ftmp, a, b);
9157
9158 if (ftmp != out) {
9159 __ MovS(out, ftmp);
9160 }
9161
9162 __ B(&done);
9163
9164 __ Bind(&noNaNs);
9165
9166 if (is_min) {
9167 __ MinS(out, a, b);
9168 } else {
9169 __ MaxS(out, a, b);
9170 }
9171 }
9172
9173 __ Bind(&done);
9174
9175 } else { // !isR6
9176 MipsLabel ordered;
9177 MipsLabel compare;
9178 MipsLabel select;
9179 MipsLabel done;
9180
9181 if (type == DataType::Type::kFloat64) {
9182 __ CunD(a, b);
9183 } else {
9184 DCHECK_EQ(type, DataType::Type::kFloat32);
9185 __ CunS(a, b);
9186 }
9187 __ Bc1f(&ordered);
9188
9189 // a or b (or both) is a NaN. Return one, which is a NaN.
9190 if (type == DataType::Type::kFloat64) {
9191 __ CeqD(b, b);
9192 } else {
9193 __ CeqS(b, b);
9194 }
9195 __ B(&select);
9196
9197 __ Bind(&ordered);
9198
9199 // Neither is a NaN.
9200 // a == b? (-0.0 compares equal with +0.0)
9201 // If equal, handle zeroes, else compare further.
9202 if (type == DataType::Type::kFloat64) {
9203 __ CeqD(a, b);
9204 } else {
9205 __ CeqS(a, b);
9206 }
9207 __ Bc1f(&compare);
9208
9209 // a == b either bit for bit or one is -0.0 and the other is +0.0.
9210 if (type == DataType::Type::kFloat64) {
9211 __ MoveFromFpuHigh(TMP, a);
9212 __ MoveFromFpuHigh(AT, b);
9213 } else {
9214 __ Mfc1(TMP, a);
9215 __ Mfc1(AT, b);
9216 }
9217
9218 if (is_min) {
9219 // -0.0 prevails over +0.0.
9220 __ Or(TMP, TMP, AT);
9221 } else {
9222 // +0.0 prevails over -0.0.
9223 __ And(TMP, TMP, AT);
9224 }
9225
9226 if (type == DataType::Type::kFloat64) {
9227 __ Mfc1(AT, a);
9228 __ Mtc1(AT, out);
9229 __ MoveToFpuHigh(TMP, out);
9230 } else {
9231 __ Mtc1(TMP, out);
9232 }
9233 __ B(&done);
9234
9235 __ Bind(&compare);
9236
9237 if (type == DataType::Type::kFloat64) {
9238 if (is_min) {
9239 // return (a <= b) ? a : b;
9240 __ ColeD(a, b);
9241 } else {
9242 // return (a >= b) ? a : b;
9243 __ ColeD(b, a); // b <= a
9244 }
9245 } else {
9246 if (is_min) {
9247 // return (a <= b) ? a : b;
9248 __ ColeS(a, b);
9249 } else {
9250 // return (a >= b) ? a : b;
9251 __ ColeS(b, a); // b <= a
9252 }
9253 }
9254
9255 __ Bind(&select);
9256
9257 if (type == DataType::Type::kFloat64) {
9258 __ MovtD(out, a);
9259 __ MovfD(out, b);
9260 } else {
9261 __ MovtS(out, a);
9262 __ MovfS(out, b);
9263 }
9264
9265 __ Bind(&done);
9266 }
9267}
9268
Aart Bik351df3e2018-03-07 11:54:57 -08009269void InstructionCodeGeneratorMIPS::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
9270 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9271 DataType::Type type = minmax->GetResultType();
9272 switch (type) {
9273 case DataType::Type::kInt32:
9274 case DataType::Type::kInt64:
9275 GenerateMinMaxInt(minmax->GetLocations(), is_min, isR6, type);
9276 break;
9277 case DataType::Type::kFloat32:
9278 case DataType::Type::kFloat64:
9279 GenerateMinMaxFP(minmax->GetLocations(), is_min, isR6, type);
9280 break;
9281 default:
9282 LOG(FATAL) << "Unexpected type for HMinMax " << type;
9283 }
9284}
9285
Aart Bik1f8d51b2018-02-15 10:42:37 -08009286void LocationsBuilderMIPS::VisitMin(HMin* min) {
9287 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
9288}
9289
9290void InstructionCodeGeneratorMIPS::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08009291 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009292}
9293
9294void LocationsBuilderMIPS::VisitMax(HMax* max) {
9295 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
9296}
9297
9298void InstructionCodeGeneratorMIPS::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08009299 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009300}
9301
Aart Bik3dad3412018-02-28 12:01:46 -08009302void LocationsBuilderMIPS::VisitAbs(HAbs* abs) {
9303 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
9304 switch (abs->GetResultType()) {
9305 case DataType::Type::kInt32:
9306 case DataType::Type::kInt64:
9307 locations->SetInAt(0, Location::RequiresRegister());
9308 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9309 break;
9310 case DataType::Type::kFloat32:
9311 case DataType::Type::kFloat64:
9312 locations->SetInAt(0, Location::RequiresFpuRegister());
9313 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9314 break;
9315 default:
9316 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9317 }
9318}
9319
9320void InstructionCodeGeneratorMIPS::GenerateAbsFP(LocationSummary* locations,
9321 DataType::Type type,
9322 bool isR2OrNewer,
9323 bool isR6) {
9324 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
9325 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9326
9327 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
9328 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
9329 // (signaling NaN may become quiet though).
9330 //
9331 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
9332 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
9333 // affected by this instruction.
9334 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
9335 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
9336 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
9337 if (isR6) {
9338 if (type == DataType::Type::kFloat64) {
9339 __ AbsD(out, in);
9340 } else {
9341 DCHECK_EQ(type, DataType::Type::kFloat32);
9342 __ AbsS(out, in);
9343 }
9344 } else {
9345 if (type == DataType::Type::kFloat64) {
9346 if (in != out) {
9347 __ MovD(out, in);
9348 }
9349 __ MoveFromFpuHigh(TMP, in);
9350 // ins instruction is not available for R1.
9351 if (isR2OrNewer) {
9352 __ Ins(TMP, ZERO, 31, 1);
9353 } else {
9354 __ Sll(TMP, TMP, 1);
9355 __ Srl(TMP, TMP, 1);
9356 }
9357 __ MoveToFpuHigh(TMP, out);
9358 } else {
9359 DCHECK_EQ(type, DataType::Type::kFloat32);
9360 __ Mfc1(TMP, in);
9361 // ins instruction is not available for R1.
9362 if (isR2OrNewer) {
9363 __ Ins(TMP, ZERO, 31, 1);
9364 } else {
9365 __ Sll(TMP, TMP, 1);
9366 __ Srl(TMP, TMP, 1);
9367 }
9368 __ Mtc1(TMP, out);
9369 }
9370 }
9371}
9372
9373void InstructionCodeGeneratorMIPS::VisitAbs(HAbs* abs) {
9374 LocationSummary* locations = abs->GetLocations();
9375 bool isR2OrNewer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
9376 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9377 switch (abs->GetResultType()) {
9378 case DataType::Type::kInt32: {
9379 Register in = locations->InAt(0).AsRegister<Register>();
9380 Register out = locations->Out().AsRegister<Register>();
9381 __ Sra(AT, in, 31);
9382 __ Xor(out, in, AT);
9383 __ Subu(out, out, AT);
9384 break;
9385 }
9386 case DataType::Type::kInt64: {
9387 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9388 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9389 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9390 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9391 // The comments in this section show the analogous operations which would
9392 // be performed if we had 64-bit registers "in", and "out".
9393 // __ Dsra32(AT, in, 31);
9394 __ Sra(AT, in_hi, 31);
9395 // __ Xor(out, in, AT);
9396 __ Xor(TMP, in_lo, AT);
9397 __ Xor(out_hi, in_hi, AT);
9398 // __ Dsubu(out, out, AT);
9399 __ Subu(out_lo, TMP, AT);
9400 __ Sltu(TMP, out_lo, TMP);
9401 __ Addu(out_hi, out_hi, TMP);
9402 break;
9403 }
9404 case DataType::Type::kFloat32:
9405 case DataType::Type::kFloat64:
9406 GenerateAbsFP(locations, abs->GetResultType(), isR2OrNewer, isR6);
9407 break;
9408 default:
9409 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9410 }
9411}
9412
Igor Murashkind01745e2017-04-05 16:40:31 -07009413void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
9414 constructor_fence->SetLocations(nullptr);
9415}
9416
9417void InstructionCodeGeneratorMIPS::VisitConstructorFence(
9418 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
9419 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
9420}
9421
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009422void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9423 memory_barrier->SetLocations(nullptr);
9424}
9425
9426void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9427 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
9428}
9429
9430void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009431 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009432 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009433 locations->SetInAt(0, MipsReturnLocation(return_type));
9434}
9435
9436void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
9437 codegen_->GenerateFrameExit();
9438}
9439
9440void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
9441 ret->SetLocations(nullptr);
9442}
9443
9444void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
9445 codegen_->GenerateFrameExit();
9446}
9447
Alexey Frunze92d90602015-12-18 18:16:36 -08009448void LocationsBuilderMIPS::VisitRor(HRor* ror) {
9449 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009450}
9451
Alexey Frunze92d90602015-12-18 18:16:36 -08009452void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
9453 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009454}
9455
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009456void LocationsBuilderMIPS::VisitShl(HShl* shl) {
9457 HandleShift(shl);
9458}
9459
9460void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
9461 HandleShift(shl);
9462}
9463
9464void LocationsBuilderMIPS::VisitShr(HShr* shr) {
9465 HandleShift(shr);
9466}
9467
9468void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
9469 HandleShift(shr);
9470}
9471
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009472void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
9473 HandleBinaryOp(instruction);
9474}
9475
9476void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
9477 HandleBinaryOp(instruction);
9478}
9479
9480void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9481 HandleFieldGet(instruction, instruction->GetFieldInfo());
9482}
9483
9484void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9485 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
9486}
9487
9488void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
9489 HandleFieldSet(instruction, instruction->GetFieldInfo());
9490}
9491
9492void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01009493 HandleFieldSet(instruction,
9494 instruction->GetFieldInfo(),
9495 instruction->GetDexPc(),
9496 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009497}
9498
9499void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
9500 HUnresolvedInstanceFieldGet* instruction) {
9501 FieldAccessCallingConventionMIPS calling_convention;
9502 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9503 instruction->GetFieldType(),
9504 calling_convention);
9505}
9506
9507void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
9508 HUnresolvedInstanceFieldGet* instruction) {
9509 FieldAccessCallingConventionMIPS calling_convention;
9510 codegen_->GenerateUnresolvedFieldAccess(instruction,
9511 instruction->GetFieldType(),
9512 instruction->GetFieldIndex(),
9513 instruction->GetDexPc(),
9514 calling_convention);
9515}
9516
9517void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
9518 HUnresolvedInstanceFieldSet* instruction) {
9519 FieldAccessCallingConventionMIPS calling_convention;
9520 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9521 instruction->GetFieldType(),
9522 calling_convention);
9523}
9524
9525void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
9526 HUnresolvedInstanceFieldSet* instruction) {
9527 FieldAccessCallingConventionMIPS calling_convention;
9528 codegen_->GenerateUnresolvedFieldAccess(instruction,
9529 instruction->GetFieldType(),
9530 instruction->GetFieldIndex(),
9531 instruction->GetDexPc(),
9532 calling_convention);
9533}
9534
9535void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
9536 HUnresolvedStaticFieldGet* instruction) {
9537 FieldAccessCallingConventionMIPS calling_convention;
9538 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9539 instruction->GetFieldType(),
9540 calling_convention);
9541}
9542
9543void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
9544 HUnresolvedStaticFieldGet* instruction) {
9545 FieldAccessCallingConventionMIPS calling_convention;
9546 codegen_->GenerateUnresolvedFieldAccess(instruction,
9547 instruction->GetFieldType(),
9548 instruction->GetFieldIndex(),
9549 instruction->GetDexPc(),
9550 calling_convention);
9551}
9552
9553void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
9554 HUnresolvedStaticFieldSet* instruction) {
9555 FieldAccessCallingConventionMIPS calling_convention;
9556 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9557 instruction->GetFieldType(),
9558 calling_convention);
9559}
9560
9561void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9562 HUnresolvedStaticFieldSet* instruction) {
9563 FieldAccessCallingConventionMIPS calling_convention;
9564 codegen_->GenerateUnresolvedFieldAccess(instruction,
9565 instruction->GetFieldType(),
9566 instruction->GetFieldIndex(),
9567 instruction->GetDexPc(),
9568 calling_convention);
9569}
9570
9571void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009572 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9573 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009574 // In suspend check slow path, usually there are no caller-save registers at all.
9575 // If SIMD instructions are present, however, we force spilling all live SIMD
9576 // registers in full width (since the runtime only saves/restores lower part).
9577 locations->SetCustomSlowPathCallerSaves(
9578 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009579}
9580
9581void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9582 HBasicBlock* block = instruction->GetBlock();
9583 if (block->GetLoopInformation() != nullptr) {
9584 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9585 // The back edge will generate the suspend check.
9586 return;
9587 }
9588 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9589 // The goto will generate the suspend check.
9590 return;
9591 }
9592 GenerateSuspendCheck(instruction, nullptr);
9593}
9594
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009595void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009596 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9597 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009598 InvokeRuntimeCallingConvention calling_convention;
9599 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9600}
9601
9602void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009603 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009604 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9605}
9606
9607void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009608 DataType::Type input_type = conversion->GetInputType();
9609 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009610 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9611 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009612 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009613
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009614 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9615 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009616 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9617 }
9618
9619 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009620 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009621 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9622 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009623 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009624 }
9625
Vladimir Markoca6fff82017-10-03 14:49:14 +01009626 LocationSummary* locations =
9627 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009628
9629 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009630 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009631 locations->SetInAt(0, Location::RequiresFpuRegister());
9632 } else {
9633 locations->SetInAt(0, Location::RequiresRegister());
9634 }
9635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009636 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009637 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9638 } else {
9639 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9640 }
9641 } else {
9642 InvokeRuntimeCallingConvention calling_convention;
9643
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009644 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009645 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9646 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009647 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009648 locations->SetInAt(0, Location::RegisterPairLocation(
9649 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9650 }
9651
9652 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9653 }
9654}
9655
9656void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9657 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009658 DataType::Type result_type = conversion->GetResultType();
9659 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009660 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009661 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009662
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009663 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9664 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009665
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009666 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009667 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9668 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9669 Register src = locations->InAt(0).AsRegister<Register>();
9670
Alexey Frunzea871ef12016-06-27 15:20:11 -07009671 if (dst_low != src) {
9672 __ Move(dst_low, src);
9673 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009674 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009675 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009676 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009677 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009678 ? locations->InAt(0).AsRegisterPairLow<Register>()
9679 : locations->InAt(0).AsRegister<Register>();
9680
9681 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009682 case DataType::Type::kUint8:
9683 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009684 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009685 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009686 if (has_sign_extension) {
9687 __ Seb(dst, src);
9688 } else {
9689 __ Sll(dst, src, 24);
9690 __ Sra(dst, dst, 24);
9691 }
9692 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009693 case DataType::Type::kUint16:
9694 __ Andi(dst, src, 0xFFFF);
9695 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009696 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009697 if (has_sign_extension) {
9698 __ Seh(dst, src);
9699 } else {
9700 __ Sll(dst, src, 16);
9701 __ Sra(dst, dst, 16);
9702 }
9703 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009704 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009705 if (dst != src) {
9706 __ Move(dst, src);
9707 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009708 break;
9709
9710 default:
9711 LOG(FATAL) << "Unexpected type conversion from " << input_type
9712 << " to " << result_type;
9713 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009714 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9715 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009716 if (isR6) {
9717 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9718 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9719 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9720 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9721 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9722 __ Mtc1(src_low, FTMP);
9723 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009724 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009725 __ Cvtsl(dst, FTMP);
9726 } else {
9727 __ Cvtdl(dst, FTMP);
9728 }
9729 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009730 QuickEntrypointEnum entrypoint =
9731 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009732 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009733 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009734 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9735 } else {
9736 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9737 }
9738 }
9739 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009740 Register src = locations->InAt(0).AsRegister<Register>();
9741 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9742 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009743 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009744 __ Cvtsw(dst, FTMP);
9745 } else {
9746 __ Cvtdw(dst, FTMP);
9747 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009748 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009749 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9750 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009751
9752 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9753 // value of the output type if the input is outside of the range after the truncation or
9754 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9755 // results. This matches the desired float/double-to-int/long conversion exactly.
9756 //
9757 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9758 // value when the input is either a NaN or is outside of the range of the output type
9759 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9760 // the same result.
9761 //
9762 // The code takes care of the different behaviors by first comparing the input to the
9763 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9764 // If the input is greater than or equal to the minimum, it procedes to the truncate
9765 // instruction, which will handle such an input the same way irrespective of NAN2008.
9766 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9767 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009768 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009769 if (isR6) {
9770 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9771 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9772 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9773 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9774 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009775
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009776 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009777 __ TruncLS(FTMP, src);
9778 } else {
9779 __ TruncLD(FTMP, src);
9780 }
9781 __ Mfc1(dst_low, FTMP);
9782 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009783 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009784 QuickEntrypointEnum entrypoint =
9785 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009786 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009787 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009788 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9789 } else {
9790 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9791 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009792 }
9793 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009794 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9795 Register dst = locations->Out().AsRegister<Register>();
9796 MipsLabel truncate;
9797 MipsLabel done;
9798
Lena Djokicf4e23a82017-05-09 15:43:45 +02009799 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009800 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009801 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9802 __ LoadConst32(TMP, min_val);
9803 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009804 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009805 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9806 __ LoadConst32(TMP, High32Bits(min_val));
9807 __ Mtc1(ZERO, FTMP);
9808 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009809 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009811 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009812 __ ColeS(0, FTMP, src);
9813 } else {
9814 __ ColeD(0, FTMP, src);
9815 }
9816 __ Bc1t(0, &truncate);
9817
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009818 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009819 __ CeqS(0, src, src);
9820 } else {
9821 __ CeqD(0, src, src);
9822 }
9823 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9824 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009825
9826 __ B(&done);
9827
9828 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009829 }
9830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009831 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009832 __ TruncWS(FTMP, src);
9833 } else {
9834 __ TruncWD(FTMP, src);
9835 }
9836 __ Mfc1(dst, FTMP);
9837
Lena Djokicf4e23a82017-05-09 15:43:45 +02009838 if (!isR6) {
9839 __ Bind(&done);
9840 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009841 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009842 } else if (DataType::IsFloatingPointType(result_type) &&
9843 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009844 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9845 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009846 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009847 __ Cvtsd(dst, src);
9848 } else {
9849 __ Cvtds(dst, src);
9850 }
9851 } else {
9852 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9853 << " to " << result_type;
9854 }
9855}
9856
9857void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9858 HandleShift(ushr);
9859}
9860
9861void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9862 HandleShift(ushr);
9863}
9864
9865void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9866 HandleBinaryOp(instruction);
9867}
9868
9869void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9870 HandleBinaryOp(instruction);
9871}
9872
9873void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9874 // Nothing to do, this should be removed during prepare for register allocator.
9875 LOG(FATAL) << "Unreachable";
9876}
9877
9878void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9879 // Nothing to do, this should be removed during prepare for register allocator.
9880 LOG(FATAL) << "Unreachable";
9881}
9882
9883void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009884 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009885}
9886
9887void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009888 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009889}
9890
9891void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009892 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009893}
9894
9895void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009896 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009897}
9898
9899void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009900 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009901}
9902
9903void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009904 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009905}
9906
9907void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009908 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009909}
9910
9911void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009912 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009913}
9914
9915void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009916 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009917}
9918
9919void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009920 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009921}
9922
9923void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009924 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009925}
9926
9927void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009928 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009929}
9930
9931void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009932 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009933}
9934
9935void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009936 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009937}
9938
9939void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009940 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009941}
9942
9943void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009944 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009945}
9946
9947void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009948 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009949}
9950
9951void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009952 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009953}
9954
9955void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009956 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009957}
9958
9959void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009960 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009961}
9962
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009963void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9964 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009965 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009966 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009967 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9968 uint32_t num_entries = switch_instr->GetNumEntries();
9969 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9970 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9971 // instruction to simulate PC-relative addressing when accessing the jump table.
9972 // NAL clobbers RA. Make sure RA is preserved.
9973 codegen_->ClobberRA();
9974 }
9975 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009976}
9977
Alexey Frunze96b66822016-09-10 02:32:44 -07009978void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9979 int32_t lower_bound,
9980 uint32_t num_entries,
9981 HBasicBlock* switch_block,
9982 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009983 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009984 Register temp_reg = TMP;
9985 __ Addiu32(temp_reg, value_reg, -lower_bound);
9986 // Jump to default if index is negative
9987 // Note: We don't check the case that index is positive while value < lower_bound, because in
9988 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9989 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9990
Alexey Frunze96b66822016-09-10 02:32:44 -07009991 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009992 // Jump to successors[0] if value == lower_bound.
9993 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9994 int32_t last_index = 0;
9995 for (; num_entries - last_index > 2; last_index += 2) {
9996 __ Addiu(temp_reg, temp_reg, -2);
9997 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9998 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9999 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
10000 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
10001 }
10002 if (num_entries - last_index == 2) {
10003 // The last missing case_value.
10004 __ Addiu(temp_reg, temp_reg, -1);
10005 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010006 }
10007
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010008 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -070010009 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010010 __ B(codegen_->GetLabelOf(default_block));
10011 }
10012}
10013
Alexey Frunze96b66822016-09-10 02:32:44 -070010014void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
10015 Register constant_area,
10016 int32_t lower_bound,
10017 uint32_t num_entries,
10018 HBasicBlock* switch_block,
10019 HBasicBlock* default_block) {
10020 // Create a jump table.
10021 std::vector<MipsLabel*> labels(num_entries);
10022 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
10023 for (uint32_t i = 0; i < num_entries; i++) {
10024 labels[i] = codegen_->GetLabelOf(successors[i]);
10025 }
10026 JumpTable* table = __ CreateJumpTable(std::move(labels));
10027
10028 // Is the value in range?
10029 __ Addiu32(TMP, value_reg, -lower_bound);
10030 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
10031 __ Sltiu(AT, TMP, num_entries);
10032 __ Beqz(AT, codegen_->GetLabelOf(default_block));
10033 } else {
10034 __ LoadConst32(AT, num_entries);
10035 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
10036 }
10037
10038 // We are in the range of the table.
10039 // Load the target address from the jump table, indexing by the value.
10040 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -070010041 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -070010042 __ Lw(TMP, TMP, 0);
10043 // Compute the absolute target address by adding the table start address
10044 // (the table contains offsets to targets relative to its start).
10045 __ Addu(TMP, TMP, AT);
10046 // And jump.
10047 __ Jr(TMP);
10048 __ NopIfNoReordering();
10049}
10050
10051void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
10052 int32_t lower_bound = switch_instr->GetStartValue();
10053 uint32_t num_entries = switch_instr->GetNumEntries();
10054 LocationSummary* locations = switch_instr->GetLocations();
10055 Register value_reg = locations->InAt(0).AsRegister<Register>();
10056 HBasicBlock* switch_block = switch_instr->GetBlock();
10057 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10058
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010059 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -070010060 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010061 //
10062 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
10063 // to access the jump table and it is implemented by changing HPackedSwitch to
10064 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
10065 // VisitMipsPackedSwitch()).
10066 //
10067 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
10068 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
10069 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -070010070 GenTableBasedPackedSwitch(value_reg,
10071 ZERO,
10072 lower_bound,
10073 num_entries,
10074 switch_block,
10075 default_block);
10076 } else {
10077 GenPackedSwitchWithCompares(value_reg,
10078 lower_bound,
10079 num_entries,
10080 switch_block,
10081 default_block);
10082 }
10083}
10084
10085void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10086 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010087 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -070010088 locations->SetInAt(0, Location::RequiresRegister());
10089 // Constant area pointer (HMipsComputeBaseMethodAddress).
10090 locations->SetInAt(1, Location::RequiresRegister());
10091}
10092
10093void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10094 int32_t lower_bound = switch_instr->GetStartValue();
10095 uint32_t num_entries = switch_instr->GetNumEntries();
10096 LocationSummary* locations = switch_instr->GetLocations();
10097 Register value_reg = locations->InAt(0).AsRegister<Register>();
10098 Register constant_area = locations->InAt(1).AsRegister<Register>();
10099 HBasicBlock* switch_block = switch_instr->GetBlock();
10100 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10101
10102 // This is an R2-only path. HPackedSwitch has been changed to
10103 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
10104 // required to address the jump table relative to PC.
10105 GenTableBasedPackedSwitch(value_reg,
10106 constant_area,
10107 lower_bound,
10108 num_entries,
10109 switch_block,
10110 default_block);
10111}
10112
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010113void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
10114 HMipsComputeBaseMethodAddress* insn) {
10115 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010116 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010117 locations->SetOut(Location::RequiresRegister());
10118}
10119
10120void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
10121 HMipsComputeBaseMethodAddress* insn) {
10122 LocationSummary* locations = insn->GetLocations();
10123 Register reg = locations->Out().AsRegister<Register>();
10124
10125 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
10126
10127 // Generate a dummy PC-relative call to obtain PC.
10128 __ Nal();
10129 // Grab the return address off RA.
10130 __ Move(reg, RA);
10131
10132 // Remember this offset (the obtained PC value) for later use with constant area.
10133 __ BindPcRelBaseLabel();
10134}
10135
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010136void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10137 // The trampoline uses the same calling convention as dex calling conventions,
10138 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
10139 // the method_idx.
10140 HandleInvoke(invoke);
10141}
10142
10143void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10144 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
10145}
10146
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010147void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10148 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010149 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010150 locations->SetInAt(0, Location::RequiresRegister());
10151 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010152}
10153
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010154void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10155 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +000010156 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010157 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010158 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010159 __ LoadFromOffset(kLoadWord,
10160 locations->Out().AsRegister<Register>(),
10161 locations->InAt(0).AsRegister<Register>(),
10162 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010163 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010164 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +000010165 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000010166 __ LoadFromOffset(kLoadWord,
10167 locations->Out().AsRegister<Register>(),
10168 locations->InAt(0).AsRegister<Register>(),
10169 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010170 __ LoadFromOffset(kLoadWord,
10171 locations->Out().AsRegister<Register>(),
10172 locations->Out().AsRegister<Register>(),
10173 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010174 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010175}
10176
xueliang.zhonge0eb4832017-10-30 13:43:14 +000010177void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10178 ATTRIBUTE_UNUSED) {
10179 LOG(FATAL) << "Unreachable";
10180}
10181
10182void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10183 ATTRIBUTE_UNUSED) {
10184 LOG(FATAL) << "Unreachable";
10185}
10186
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010187#undef __
10188#undef QUICK_ENTRY_POINT
10189
10190} // namespace mips
10191} // namespace art