blob: 4aed2c091c3ae9010ed771e310acaa3e98916b3b [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)),
Vladimir Marko6fd16062018-06-26 11:02:04 +01001026 boot_image_intrinsic_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001027 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1028 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001029 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001030 // Save RA (containing the return address) to mimic Quick.
1031 AddAllocatedRegister(Location::RegisterLocation(RA));
1032}
1033
1034#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001035// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1036#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001037#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001038
1039void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1040 // Ensure that we fix up branches.
1041 __ FinalizeCode();
1042
1043 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001044 StackMapStream* stack_map_stream = GetStackMapStream();
1045 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
David Srbeckyd02b23f2018-05-29 23:27:22 +01001046 uint32_t old_position = stack_map_stream->GetStackMapNativePcOffset(i);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001047 uint32_t new_position = __ GetAdjustedPosition(old_position);
1048 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001049 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001050 }
1051
1052 // Adjust pc offsets for the disassembly information.
1053 if (disasm_info_ != nullptr) {
1054 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1055 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1056 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1057 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1058 it.second.start = __ GetAdjustedPosition(it.second.start);
1059 it.second.end = __ GetAdjustedPosition(it.second.end);
1060 }
1061 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1062 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1063 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1064 }
1065 }
1066
1067 CodeGenerator::Finalize(allocator);
1068}
1069
1070MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1071 return codegen_->GetAssembler();
1072}
1073
1074void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1075 DCHECK_LT(index, moves_.size());
1076 MoveOperands* move = moves_[index];
1077 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1078}
1079
1080void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1081 DCHECK_LT(index, moves_.size());
1082 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001083 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001084 Location loc1 = move->GetDestination();
1085 Location loc2 = move->GetSource();
1086
1087 DCHECK(!loc1.IsConstant());
1088 DCHECK(!loc2.IsConstant());
1089
1090 if (loc1.Equals(loc2)) {
1091 return;
1092 }
1093
1094 if (loc1.IsRegister() && loc2.IsRegister()) {
1095 // Swap 2 GPRs.
1096 Register r1 = loc1.AsRegister<Register>();
1097 Register r2 = loc2.AsRegister<Register>();
1098 __ Move(TMP, r2);
1099 __ Move(r2, r1);
1100 __ Move(r1, TMP);
1101 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001102 if (codegen_->GetGraph()->HasSIMD()) {
1103 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1104 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1105 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001106 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001107 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1108 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1109 if (type == DataType::Type::kFloat32) {
1110 __ MovS(FTMP, f2);
1111 __ MovS(f2, f1);
1112 __ MovS(f1, FTMP);
1113 } else {
1114 DCHECK_EQ(type, DataType::Type::kFloat64);
1115 __ MovD(FTMP, f2);
1116 __ MovD(f2, f1);
1117 __ MovD(f1, FTMP);
1118 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001119 }
1120 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1121 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1122 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001123 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001124 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1125 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001126 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001127 __ Move(TMP, r2);
1128 __ Mfc1(r2, f1);
1129 __ Mtc1(TMP, f1);
1130 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1131 // Swap 2 GPR register pairs.
1132 Register r1 = loc1.AsRegisterPairLow<Register>();
1133 Register r2 = loc2.AsRegisterPairLow<Register>();
1134 __ Move(TMP, r2);
1135 __ Move(r2, r1);
1136 __ Move(r1, TMP);
1137 r1 = loc1.AsRegisterPairHigh<Register>();
1138 r2 = loc2.AsRegisterPairHigh<Register>();
1139 __ Move(TMP, r2);
1140 __ Move(r2, r1);
1141 __ Move(r1, TMP);
1142 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1143 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1144 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001146 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1147 : loc2.AsFpuRegister<FRegister>();
1148 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1149 : loc2.AsRegisterPairLow<Register>();
1150 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1151 : loc2.AsRegisterPairHigh<Register>();
1152 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1153 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1154 // unpredictable and the following mfch1 will fail.
1155 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001156 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001157 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001158 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001159 __ Move(r2_l, TMP);
1160 __ Move(r2_h, AT);
1161 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1162 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1163 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1164 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001165 } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) {
1166 ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
David Brazdilcc0f3112016-01-28 17:14:52 +00001167 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1168 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001169 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1170 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001171 __ Move(TMP, reg);
1172 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1173 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1174 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1175 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1176 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1177 : loc2.AsRegisterPairLow<Register>();
1178 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1179 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001180 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001181 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1182 : loc2.GetHighStackIndex(kMipsWordSize);
1183 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001184 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001185 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001186 __ Move(TMP, reg_h);
1187 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1188 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001189 } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) ||
1190 (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) {
1191 Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2;
1192 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1193 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(fp_loc));
1194 __ LoadQFromOffset(fp_loc.AsFpuRegister<FRegister>(), SP, offset);
1195 __ StoreQToOffset(FTMP, SP, offset);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001196 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1197 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1198 : loc2.AsFpuRegister<FRegister>();
1199 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001200 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001201 __ MovS(FTMP, reg);
1202 __ LoadSFromOffset(reg, SP, offset);
1203 __ StoreSToOffset(FTMP, SP, offset);
1204 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001205 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001206 __ MovD(FTMP, reg);
1207 __ LoadDFromOffset(reg, SP, offset);
1208 __ StoreDToOffset(FTMP, SP, offset);
1209 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210 } else {
1211 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1212 }
1213}
1214
1215void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1216 __ Pop(static_cast<Register>(reg));
1217}
1218
1219void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1220 __ Push(static_cast<Register>(reg));
1221}
1222
1223void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1224 // Allocate a scratch register other than TMP, if available.
1225 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1226 // automatically unspilled when the scratch scope object is destroyed).
1227 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1228 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001229 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001230 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1231 __ LoadFromOffset(kLoadWord,
1232 Register(ensure_scratch.GetRegister()),
1233 SP,
1234 index1 + stack_offset);
1235 __ LoadFromOffset(kLoadWord,
1236 TMP,
1237 SP,
1238 index2 + stack_offset);
1239 __ StoreToOffset(kStoreWord,
1240 Register(ensure_scratch.GetRegister()),
1241 SP,
1242 index2 + stack_offset);
1243 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1244 }
1245}
1246
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001247void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) {
1248 __ LoadQFromOffset(FTMP, SP, index1);
1249 __ LoadQFromOffset(FTMP2, SP, index2);
1250 __ StoreQToOffset(FTMP, SP, index2);
1251 __ StoreQToOffset(FTMP2, SP, index1);
1252}
1253
Alexey Frunze73296a72016-06-03 22:51:46 -07001254void CodeGeneratorMIPS::ComputeSpillMask() {
1255 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1256 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1257 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1258 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1259 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1260 // within the stack frame.
1261 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1262 core_spill_mask_ |= (1 << ZERO);
1263 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001264}
1265
1266bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001267 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001268 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1269 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1270 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001271 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001272}
1273
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001274static dwarf::Reg DWARFReg(Register reg) {
1275 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1276}
1277
1278// TODO: mapping of floating-point registers to DWARF.
1279
1280void CodeGeneratorMIPS::GenerateFrameEntry() {
1281 __ Bind(&frame_entry_label_);
1282
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001283 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01001284 __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
1285 __ Addiu(TMP, TMP, 1);
1286 __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001287 }
1288
Vladimir Marko33bff252017-11-01 14:35:42 +00001289 bool do_overflow_check =
1290 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001291
1292 if (do_overflow_check) {
1293 __ LoadFromOffset(kLoadWord,
1294 ZERO,
1295 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001296 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 RecordPcInfo(nullptr, 0);
1298 }
1299
1300 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001301 CHECK_EQ(fpu_spill_mask_, 0u);
1302 CHECK_EQ(core_spill_mask_, 1u << RA);
1303 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304 return;
1305 }
1306
1307 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001308 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1309 LOG(FATAL) << "Stack frame larger than "
1310 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311 }
1312
1313 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001314
Alexey Frunze73296a72016-06-03 22:51:46 -07001315 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316 __ IncreaseFrameSize(ofs);
1317
Alexey Frunze73296a72016-06-03 22:51:46 -07001318 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1319 Register reg = static_cast<Register>(MostSignificantBit(mask));
1320 mask ^= 1u << reg;
1321 ofs -= kMipsWordSize;
1322 // The ZERO register is only included for alignment.
1323 if (reg != ZERO) {
1324 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001325 __ cfi().RelOffset(DWARFReg(reg), ofs);
1326 }
1327 }
1328
Alexey Frunze73296a72016-06-03 22:51:46 -07001329 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1330 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1331 mask ^= 1u << reg;
1332 ofs -= kMipsDoublewordSize;
1333 __ StoreDToOffset(reg, SP, ofs);
1334 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001335 }
1336
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001337 // Save the current method if we need it. Note that we do not
1338 // do this in HCurrentMethod, as the instruction might have been removed
1339 // in the SSA graph.
1340 if (RequiresCurrentMethod()) {
1341 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1342 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001343
1344 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1345 // Initialize should deoptimize flag to 0.
1346 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1347 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001348}
1349
1350void CodeGeneratorMIPS::GenerateFrameExit() {
1351 __ cfi().RememberState();
1352
1353 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001354 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355
Alexey Frunze73296a72016-06-03 22:51:46 -07001356 // For better instruction scheduling restore RA before other registers.
1357 uint32_t ofs = GetFrameSize();
1358 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1359 Register reg = static_cast<Register>(MostSignificantBit(mask));
1360 mask ^= 1u << reg;
1361 ofs -= kMipsWordSize;
1362 // The ZERO register is only included for alignment.
1363 if (reg != ZERO) {
1364 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001365 __ cfi().Restore(DWARFReg(reg));
1366 }
1367 }
1368
Alexey Frunze73296a72016-06-03 22:51:46 -07001369 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1370 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1371 mask ^= 1u << reg;
1372 ofs -= kMipsDoublewordSize;
1373 __ LoadDFromOffset(reg, SP, ofs);
1374 // TODO: __ cfi().Restore(DWARFReg(reg));
1375 }
1376
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001377 size_t frame_size = GetFrameSize();
1378 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1379 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1380 bool reordering = __ SetReorder(false);
1381 if (exchange) {
1382 __ Jr(RA);
1383 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1384 } else {
1385 __ DecreaseFrameSize(frame_size);
1386 __ Jr(RA);
1387 __ Nop(); // In delay slot.
1388 }
1389 __ SetReorder(reordering);
1390 } else {
1391 __ Jr(RA);
1392 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001393 }
1394
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001395 __ cfi().RestoreState();
1396 __ cfi().DefCFAOffset(GetFrameSize());
1397}
1398
1399void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1400 __ Bind(GetLabelOf(block));
1401}
1402
Lena Djokicca8c2952017-05-29 11:31:46 +02001403VectorRegister VectorRegisterFrom(Location location) {
1404 DCHECK(location.IsFpuRegister());
1405 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1406}
1407
Lena Djokic8098da92017-06-28 12:07:50 +02001408void CodeGeneratorMIPS::MoveLocation(Location destination,
1409 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001410 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 if (source.Equals(destination)) {
1412 return;
1413 }
1414
Lena Djokic8098da92017-06-28 12:07:50 +02001415 if (source.IsConstant()) {
1416 MoveConstant(destination, source.GetConstant());
1417 } else {
1418 if (destination.IsRegister()) {
1419 if (source.IsRegister()) {
1420 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1421 } else if (source.IsFpuRegister()) {
1422 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1423 } else {
1424 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001425 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001426 }
1427 } else if (destination.IsRegisterPair()) {
1428 if (source.IsRegisterPair()) {
1429 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1430 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1431 } else if (source.IsFpuRegister()) {
1432 Register dst_high = destination.AsRegisterPairHigh<Register>();
1433 Register dst_low = destination.AsRegisterPairLow<Register>();
1434 FRegister src = source.AsFpuRegister<FRegister>();
1435 __ Mfc1(dst_low, src);
1436 __ MoveFromFpuHigh(dst_high, src);
1437 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001438 DCHECK(source.IsDoubleStackSlot())
1439 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001440 int32_t off = source.GetStackIndex();
1441 Register r = destination.AsRegisterPairLow<Register>();
1442 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1443 }
1444 } else if (destination.IsFpuRegister()) {
1445 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001446 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001447 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1448 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001450 FRegister dst = destination.AsFpuRegister<FRegister>();
1451 Register src_high = source.AsRegisterPairHigh<Register>();
1452 Register src_low = source.AsRegisterPairLow<Register>();
1453 __ Mtc1(src_low, dst);
1454 __ MoveToFpuHigh(src_high, dst);
1455 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001456 if (GetGraph()->HasSIMD()) {
1457 __ MoveV(VectorRegisterFrom(destination),
1458 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001459 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001461 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1462 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001464 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1465 }
Lena Djokic8098da92017-06-28 12:07:50 +02001466 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001467 } else if (source.IsSIMDStackSlot()) {
1468 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001469 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001470 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001471 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1472 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001474 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1475 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1476 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001477 } else if (destination.IsSIMDStackSlot()) {
1478 if (source.IsFpuRegister()) {
1479 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1480 } else {
1481 DCHECK(source.IsSIMDStackSlot());
1482 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1483 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1484 }
Lena Djokic8098da92017-06-28 12:07:50 +02001485 } else if (destination.IsDoubleStackSlot()) {
1486 int32_t dst_offset = destination.GetStackIndex();
1487 if (source.IsRegisterPair()) {
1488 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1489 } else if (source.IsFpuRegister()) {
1490 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1491 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001492 DCHECK(source.IsDoubleStackSlot())
1493 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001494 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1495 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1496 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1497 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1498 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001499 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001500 DCHECK(destination.IsStackSlot()) << destination;
1501 int32_t dst_offset = destination.GetStackIndex();
1502 if (source.IsRegister()) {
1503 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1504 } else if (source.IsFpuRegister()) {
1505 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1506 } else {
1507 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1508 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1509 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1510 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001511 }
1512 }
1513}
1514
1515void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1516 if (c->IsIntConstant() || c->IsNullConstant()) {
1517 // Move 32 bit constant.
1518 int32_t value = GetInt32ValueOf(c);
1519 if (destination.IsRegister()) {
1520 Register dst = destination.AsRegister<Register>();
1521 __ LoadConst32(dst, value);
1522 } else {
1523 DCHECK(destination.IsStackSlot())
1524 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001525 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001526 }
1527 } else if (c->IsLongConstant()) {
1528 // Move 64 bit constant.
1529 int64_t value = GetInt64ValueOf(c);
1530 if (destination.IsRegisterPair()) {
1531 Register r_h = destination.AsRegisterPairHigh<Register>();
1532 Register r_l = destination.AsRegisterPairLow<Register>();
1533 __ LoadConst64(r_h, r_l, value);
1534 } else {
1535 DCHECK(destination.IsDoubleStackSlot())
1536 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001537 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001538 }
1539 } else if (c->IsFloatConstant()) {
1540 // Move 32 bit float constant.
1541 int32_t value = GetInt32ValueOf(c);
1542 if (destination.IsFpuRegister()) {
1543 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1544 } else {
1545 DCHECK(destination.IsStackSlot())
1546 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001547 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548 }
1549 } else {
1550 // Move 64 bit double constant.
1551 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1552 int64_t value = GetInt64ValueOf(c);
1553 if (destination.IsFpuRegister()) {
1554 FRegister fd = destination.AsFpuRegister<FRegister>();
1555 __ LoadDConst64(fd, value, TMP);
1556 } else {
1557 DCHECK(destination.IsDoubleStackSlot())
1558 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001559 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001560 }
1561 }
1562}
1563
1564void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1565 DCHECK(destination.IsRegister());
1566 Register dst = destination.AsRegister<Register>();
1567 __ LoadConst32(dst, value);
1568}
1569
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001570void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1571 if (location.IsRegister()) {
1572 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001573 } else if (location.IsRegisterPair()) {
1574 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1575 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576 } else {
1577 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1578 }
1579}
1580
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001581template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001582inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1583 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001584 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001585 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001586 const DexFile* dex_file = info.target_dex_file;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001587 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001588 DCHECK(info.label.IsBound());
1589 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001590 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1591 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001592 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1593 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1594 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001595 : __ GetPcRelBaseLabelLocation();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001596 linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001597 }
1598}
1599
Vladimir Marko6fd16062018-06-26 11:02:04 +01001600template <linker::LinkerPatch (*Factory)(size_t, uint32_t, uint32_t)>
1601linker::LinkerPatch NoDexFileAdapter(size_t literal_offset,
1602 const DexFile* target_dex_file,
1603 uint32_t pc_insn_offset,
1604 uint32_t boot_image_offset) {
1605 DCHECK(target_dex_file == nullptr); // Unused for these patches, should be null.
1606 return Factory(literal_offset, pc_insn_offset, boot_image_offset);
Vladimir Markob066d432018-01-03 13:14:37 +00001607}
1608
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001609void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001610 DCHECK(linker_patches->empty());
1611 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001612 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001613 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001614 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001615 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001616 boot_image_string_patches_.size() +
Vladimir Marko6fd16062018-06-26 11:02:04 +01001617 string_bss_entry_patches_.size() +
1618 boot_image_intrinsic_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001619 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001620 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001621 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001622 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001623 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001624 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001625 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001626 boot_image_string_patches_, linker_patches);
Vladimir Marko6fd16062018-06-26 11:02:04 +01001627 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::IntrinsicReferencePatch>>(
1628 boot_image_intrinsic_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001629 } else {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001630 EmitPcRelativeLinkerPatches<NoDexFileAdapter<linker::LinkerPatch::DataBimgRelRoPatch>>(
Vladimir Markob066d432018-01-03 13:14:37 +00001631 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001632 DCHECK(boot_image_type_patches_.empty());
1633 DCHECK(boot_image_string_patches_.empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001634 DCHECK(boot_image_intrinsic_patches_.empty());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001635 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001636 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1637 method_bss_entry_patches_, linker_patches);
1638 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1639 type_bss_entry_patches_, linker_patches);
1640 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1641 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001642 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001643}
1644
Vladimir Marko6fd16062018-06-26 11:02:04 +01001645CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageIntrinsicPatch(
1646 uint32_t intrinsic_data,
1647 const PcRelativePatchInfo* info_high) {
1648 return NewPcRelativePatch(
1649 /* dex_file */ nullptr, intrinsic_data, info_high, &boot_image_intrinsic_patches_);
1650}
1651
Vladimir Markob066d432018-01-03 13:14:37 +00001652CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageRelRoPatch(
1653 uint32_t boot_image_offset,
1654 const PcRelativePatchInfo* info_high) {
1655 return NewPcRelativePatch(
1656 /* dex_file */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_);
1657}
1658
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001659CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001660 MethodReference target_method,
1661 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001662 return NewPcRelativePatch(
1663 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001664}
1665
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001666CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001667 MethodReference target_method,
1668 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001669 return NewPcRelativePatch(
1670 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001671}
1672
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001673CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001674 const DexFile& dex_file,
1675 dex::TypeIndex type_index,
1676 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001677 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001678}
1679
Vladimir Marko1998cd02017-01-13 13:02:58 +00001680CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001681 const DexFile& dex_file,
1682 dex::TypeIndex type_index,
1683 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001684 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001685}
1686
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001687CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001688 const DexFile& dex_file,
1689 dex::StringIndex string_index,
1690 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001691 return NewPcRelativePatch(
1692 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001693}
1694
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001695CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1696 const DexFile& dex_file,
1697 dex::StringIndex string_index,
1698 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001699 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001700}
1701
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001702CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001703 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001704 uint32_t offset_or_index,
1705 const PcRelativePatchInfo* info_high,
1706 ArenaDeque<PcRelativePatchInfo>* patches) {
1707 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001708 return &patches->back();
1709}
1710
Alexey Frunze06a46c42016-07-19 15:00:40 -07001711Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1712 return map->GetOrCreate(
1713 value,
1714 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1715}
1716
Alexey Frunze06a46c42016-07-19 15:00:40 -07001717Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001718 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001719}
1720
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001721void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001722 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001723 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001724 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001725 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001726 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001727 if (GetInstructionSetFeatures().IsR6()) {
1728 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001729 __ Bind(&info_high->label);
1730 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001731 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001732 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001733 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001734 } else {
1735 // If base is ZERO, emit NAL to obtain the actual base.
1736 if (base == ZERO) {
1737 // Generate a dummy PC-relative call to obtain PC.
1738 __ Nal();
1739 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001740 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001741 __ Lui(out, /* placeholder */ 0x1234);
1742 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1743 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1744 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001745 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001746 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001747 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001748 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001749 __ Addu(out, out, (base == ZERO) ? RA : base);
1750 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001751 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001752 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001753}
1754
Vladimir Marko6fd16062018-06-26 11:02:04 +01001755void CodeGeneratorMIPS::LoadBootImageAddress(Register reg, uint32_t boot_image_reference) {
1756 if (GetCompilerOptions().IsBootImage()) {
1757 PcRelativePatchInfo* info_high = NewBootImageIntrinsicPatch(boot_image_reference);
1758 PcRelativePatchInfo* info_low = NewBootImageIntrinsicPatch(boot_image_reference, info_high);
1759 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, /* base */ ZERO);
1760 __ Addiu(reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001761 } else if (Runtime::Current()->IsAotCompiler()) {
Vladimir Marko6fd16062018-06-26 11:02:04 +01001762 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_reference);
1763 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_reference, info_high);
Vladimir Markoeebb8212018-06-05 14:57:24 +01001764 EmitPcRelativeAddressPlaceholderHigh(info_high, reg, /* base */ ZERO);
1765 __ Lw(reg, reg, /* placeholder */ 0x5678, &info_low->label);
1766 } else {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01001767 DCHECK(Runtime::Current()->UseJitCompilation());
Vladimir Markoeebb8212018-06-05 14:57:24 +01001768 gc::Heap* heap = Runtime::Current()->GetHeap();
1769 DCHECK(!heap->GetBootImageSpaces().empty());
Vladimir Marko6fd16062018-06-26 11:02:04 +01001770 const uint8_t* address = heap->GetBootImageSpaces()[0]->Begin() + boot_image_reference;
Vladimir Markoeebb8212018-06-05 14:57:24 +01001771 __ LoadConst32(reg, dchecked_integral_cast<uint32_t>(reinterpret_cast<uintptr_t>(address)));
1772 }
1773}
1774
Vladimir Marko6fd16062018-06-26 11:02:04 +01001775void CodeGeneratorMIPS::AllocateInstanceForIntrinsic(HInvokeStaticOrDirect* invoke,
1776 uint32_t boot_image_offset) {
1777 DCHECK(invoke->IsStatic());
1778 InvokeRuntimeCallingConvention calling_convention;
1779 Register argument = calling_convention.GetRegisterAt(0);
1780 if (GetCompilerOptions().IsBootImage()) {
1781 DCHECK_EQ(boot_image_offset, IntrinsicVisitor::IntegerValueOfInfo::kInvalidReference);
1782 // Load the class the same way as for HLoadClass::LoadKind::kBootImageLinkTimePcRelative.
1783 MethodReference target_method = invoke->GetTargetMethod();
1784 dex::TypeIndex type_idx = target_method.dex_file->GetMethodId(target_method.index).class_idx_;
1785 PcRelativePatchInfo* info_high = NewBootImageTypePatch(*target_method.dex_file, type_idx);
1786 PcRelativePatchInfo* info_low =
1787 NewBootImageTypePatch(*target_method.dex_file, type_idx, info_high);
1788 EmitPcRelativeAddressPlaceholderHigh(info_high, argument, /* base */ ZERO);
1789 __ Addiu(argument, argument, /* placeholder */ 0x5678, &info_low->label);
1790 } else {
1791 LoadBootImageAddress(argument, boot_image_offset);
1792 }
1793 InvokeRuntime(kQuickAllocObjectInitialized, invoke, invoke->GetDexPc());
1794 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
1795}
1796
Alexey Frunze627c1a02017-01-30 19:28:14 -08001797CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1798 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001799 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001800 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001801 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1802 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001803 return &jit_string_patches_.back();
1804}
1805
1806CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1807 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001808 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001809 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001810 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1811 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001812 return &jit_class_patches_.back();
1813}
1814
1815void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1816 const uint8_t* roots_data,
1817 const CodeGeneratorMIPS::JitPatchInfo& info,
1818 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001819 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1820 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001821 uintptr_t address =
1822 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1823 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1824 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001825 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1826 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1827 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1828 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001829 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001830 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1831 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001832 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001833 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001834 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1835 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001836 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001837 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1838 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001839}
1840
1841void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1842 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001843 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1844 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001845 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001846 }
1847 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001848 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1849 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001850 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001851 }
1852}
1853
Goran Jakovljevice114da22016-12-26 14:21:43 +01001854void CodeGeneratorMIPS::MarkGCCard(Register object,
1855 Register value,
1856 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001857 MipsLabel done;
1858 Register card = AT;
1859 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001860 if (value_can_be_null) {
1861 __ Beqz(value, &done);
1862 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 __ LoadFromOffset(kLoadWord,
1864 card,
1865 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001866 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1868 __ Addu(temp, card, temp);
1869 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001870 if (value_can_be_null) {
1871 __ Bind(&done);
1872 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001873}
1874
David Brazdil58282f42016-01-14 12:45:10 +00001875void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001876 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1877 blocked_core_registers_[ZERO] = true;
1878 blocked_core_registers_[K0] = true;
1879 blocked_core_registers_[K1] = true;
1880 blocked_core_registers_[GP] = true;
1881 blocked_core_registers_[SP] = true;
1882 blocked_core_registers_[RA] = true;
1883
1884 // AT and TMP(T8) are used as temporary/scratch registers
1885 // (similar to how AT is used by MIPS assemblers).
1886 blocked_core_registers_[AT] = true;
1887 blocked_core_registers_[TMP] = true;
1888 blocked_fpu_registers_[FTMP] = true;
1889
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001890 if (GetInstructionSetFeatures().HasMsa()) {
1891 // To be used just for MSA instructions.
1892 blocked_fpu_registers_[FTMP2] = true;
1893 }
1894
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001895 // Reserve suspend and thread registers.
1896 blocked_core_registers_[S0] = true;
1897 blocked_core_registers_[TR] = true;
1898
1899 // Reserve T9 for function calls
1900 blocked_core_registers_[T9] = true;
1901
1902 // Reserve odd-numbered FPU registers.
1903 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1904 blocked_fpu_registers_[i] = true;
1905 }
1906
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001907 if (GetGraph()->IsDebuggable()) {
1908 // Stubs do not save callee-save floating point registers. If the graph
1909 // is debuggable, we need to deal with these registers differently. For
1910 // now, just block them.
1911 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1912 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1913 }
1914 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915}
1916
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001917size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1918 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1919 return kMipsWordSize;
1920}
1921
1922size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1923 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1924 return kMipsWordSize;
1925}
1926
1927size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001928 if (GetGraph()->HasSIMD()) {
1929 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1930 } else {
1931 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1932 }
1933 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934}
1935
1936size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001937 if (GetGraph()->HasSIMD()) {
1938 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1939 } else {
1940 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1941 }
1942 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001943}
1944
1945void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001946 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001947}
1948
1949void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001950 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001951}
1952
Vladimir Markoa0431112018-06-25 09:32:54 +01001953const MipsInstructionSetFeatures& CodeGeneratorMIPS::GetInstructionSetFeatures() const {
1954 return *GetCompilerOptions().GetInstructionSetFeatures()->AsMipsInstructionSetFeatures();
1955}
1956
Serban Constantinescufca16662016-07-14 09:21:59 +01001957constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1958
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1960 HInstruction* instruction,
1961 uint32_t dex_pc,
1962 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001963 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001964 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1965 IsDirectEntrypoint(entrypoint));
1966 if (EntrypointRequiresStackMap(entrypoint)) {
1967 RecordPcInfo(instruction, dex_pc, slow_path);
1968 }
1969}
1970
1971void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1972 HInstruction* instruction,
1973 SlowPathCode* slow_path,
1974 bool direct) {
1975 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1976 GenerateInvokeRuntime(entry_point_offset, direct);
1977}
1978
1979void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001980 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001981 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001982 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001983 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001984 // Reserve argument space on stack (for $a0-$a3) for
1985 // entrypoints that directly reference native implementations.
1986 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001987 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001988 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001989 } else {
1990 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001991 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001992 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001993}
1994
1995void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1996 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001997 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1998 const size_t status_byte_offset =
1999 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
2000 constexpr uint32_t shifted_initialized_value =
2001 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
2002
2003 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01002004 __ Sltiu(TMP, TMP, shifted_initialized_value);
2005 __ Bnez(TMP, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002006 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
2007 __ Sync(0);
2008 __ Bind(slow_path->GetExitLabel());
2009}
2010
Vladimir Marko175e7862018-03-27 09:03:13 +00002011void InstructionCodeGeneratorMIPS::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
2012 Register temp) {
2013 uint32_t path_to_root = check->GetBitstringPathToRoot();
2014 uint32_t mask = check->GetBitstringMask();
2015 DCHECK(IsPowerOfTwo(mask + 1));
2016 size_t mask_bits = WhichPowerOf2(mask + 1);
2017
2018 if (mask_bits == 16u) {
2019 // Load only the bitstring part of the status word.
2020 __ LoadFromOffset(
2021 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
2022 // Compare the bitstring bits using XOR.
2023 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
2024 } else {
2025 // /* uint32_t */ temp = temp->status_
2026 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
2027 // Compare the bitstring bits using XOR.
2028 if (IsUint<16>(path_to_root)) {
2029 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
2030 } else {
2031 __ LoadConst32(TMP, path_to_root);
2032 __ Xor(temp, temp, TMP);
2033 }
2034 // Shift out bits that do not contribute to the comparison.
2035 __ Sll(temp, temp, 32 - mask_bits);
2036 }
2037}
2038
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002039void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
2040 __ Sync(0); // Only stype 0 is supported.
2041}
2042
2043void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
2044 HBasicBlock* successor) {
2045 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07002046 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
2047
2048 if (slow_path == nullptr) {
2049 slow_path =
2050 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
2051 instruction->SetSlowPath(slow_path);
2052 codegen_->AddSlowPath(slow_path);
2053 if (successor != nullptr) {
2054 DCHECK(successor->IsLoopHeader());
2055 }
2056 } else {
2057 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2058 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059
2060 __ LoadFromOffset(kLoadUnsignedHalfword,
2061 TMP,
2062 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002063 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002064 if (successor == nullptr) {
2065 __ Bnez(TMP, slow_path->GetEntryLabel());
2066 __ Bind(slow_path->GetReturnLabel());
2067 } else {
2068 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2069 __ B(slow_path->GetEntryLabel());
2070 // slow_path will return to GetLabelOf(successor).
2071 }
2072}
2073
2074InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2075 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002076 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002077 assembler_(codegen->GetAssembler()),
2078 codegen_(codegen) {}
2079
2080void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2081 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002082 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01002084 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002085 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002086 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002087 locations->SetInAt(0, Location::RequiresRegister());
2088 HInstruction* right = instruction->InputAt(1);
2089 bool can_use_imm = false;
2090 if (right->IsConstant()) {
2091 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2092 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2093 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002094 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002095 DCHECK(instruction->IsSub() || instruction->IsAdd());
2096 if (instruction->IsSub()) {
2097 imm = -imm;
2098 }
2099 if (isR6) {
2100 bool single_use = right->GetUses().HasExactlyOneElement();
2101 int16_t imm_high = High16Bits(imm);
2102 int16_t imm_low = Low16Bits(imm);
2103 if (imm_low < 0) {
2104 imm_high += 1;
2105 }
2106 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2107 } else {
2108 can_use_imm = IsInt<16>(imm);
2109 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002110 }
2111 }
2112 if (can_use_imm)
2113 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2114 else
2115 locations->SetInAt(1, Location::RequiresRegister());
2116 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2117 break;
2118 }
2119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002120 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002121 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002122 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2123 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002124 break;
2125 }
2126
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002127 case DataType::Type::kFloat32:
2128 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002129 DCHECK(instruction->IsAdd() || instruction->IsSub());
2130 locations->SetInAt(0, Location::RequiresFpuRegister());
2131 locations->SetInAt(1, Location::RequiresFpuRegister());
2132 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2133 break;
2134
2135 default:
2136 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2137 }
2138}
2139
2140void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002141 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002142 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002143 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002144
2145 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002146 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002147 Register dst = locations->Out().AsRegister<Register>();
2148 Register lhs = locations->InAt(0).AsRegister<Register>();
2149 Location rhs_location = locations->InAt(1);
2150
2151 Register rhs_reg = ZERO;
2152 int32_t rhs_imm = 0;
2153 bool use_imm = rhs_location.IsConstant();
2154 if (use_imm) {
2155 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2156 } else {
2157 rhs_reg = rhs_location.AsRegister<Register>();
2158 }
2159
2160 if (instruction->IsAnd()) {
2161 if (use_imm)
2162 __ Andi(dst, lhs, rhs_imm);
2163 else
2164 __ And(dst, lhs, rhs_reg);
2165 } else if (instruction->IsOr()) {
2166 if (use_imm)
2167 __ Ori(dst, lhs, rhs_imm);
2168 else
2169 __ Or(dst, lhs, rhs_reg);
2170 } else if (instruction->IsXor()) {
2171 if (use_imm)
2172 __ Xori(dst, lhs, rhs_imm);
2173 else
2174 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002175 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002176 DCHECK(instruction->IsAdd() || instruction->IsSub());
2177 if (use_imm) {
2178 if (instruction->IsSub()) {
2179 rhs_imm = -rhs_imm;
2180 }
2181 if (IsInt<16>(rhs_imm)) {
2182 __ Addiu(dst, lhs, rhs_imm);
2183 } else {
2184 DCHECK(isR6);
2185 int16_t rhs_imm_high = High16Bits(rhs_imm);
2186 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2187 if (rhs_imm_low < 0) {
2188 rhs_imm_high += 1;
2189 }
2190 __ Aui(dst, lhs, rhs_imm_high);
2191 if (rhs_imm_low != 0) {
2192 __ Addiu(dst, dst, rhs_imm_low);
2193 }
2194 }
2195 } else if (instruction->IsAdd()) {
2196 __ Addu(dst, lhs, rhs_reg);
2197 } else {
2198 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002199 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002200 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002201 }
2202 break;
2203 }
2204
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002205 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002206 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2207 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2208 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2209 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002210 Location rhs_location = locations->InAt(1);
2211 bool use_imm = rhs_location.IsConstant();
2212 if (!use_imm) {
2213 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2214 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2215 if (instruction->IsAnd()) {
2216 __ And(dst_low, lhs_low, rhs_low);
2217 __ And(dst_high, lhs_high, rhs_high);
2218 } else if (instruction->IsOr()) {
2219 __ Or(dst_low, lhs_low, rhs_low);
2220 __ Or(dst_high, lhs_high, rhs_high);
2221 } else if (instruction->IsXor()) {
2222 __ Xor(dst_low, lhs_low, rhs_low);
2223 __ Xor(dst_high, lhs_high, rhs_high);
2224 } else if (instruction->IsAdd()) {
2225 if (lhs_low == rhs_low) {
2226 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2227 __ Slt(TMP, lhs_low, ZERO);
2228 __ Addu(dst_low, lhs_low, rhs_low);
2229 } else {
2230 __ Addu(dst_low, lhs_low, rhs_low);
2231 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2232 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2233 }
2234 __ Addu(dst_high, lhs_high, rhs_high);
2235 __ Addu(dst_high, dst_high, TMP);
2236 } else {
2237 DCHECK(instruction->IsSub());
2238 __ Sltu(TMP, lhs_low, rhs_low);
2239 __ Subu(dst_low, lhs_low, rhs_low);
2240 __ Subu(dst_high, lhs_high, rhs_high);
2241 __ Subu(dst_high, dst_high, TMP);
2242 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002243 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002244 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2245 if (instruction->IsOr()) {
2246 uint32_t low = Low32Bits(value);
2247 uint32_t high = High32Bits(value);
2248 if (IsUint<16>(low)) {
2249 if (dst_low != lhs_low || low != 0) {
2250 __ Ori(dst_low, lhs_low, low);
2251 }
2252 } else {
2253 __ LoadConst32(TMP, low);
2254 __ Or(dst_low, lhs_low, TMP);
2255 }
2256 if (IsUint<16>(high)) {
2257 if (dst_high != lhs_high || high != 0) {
2258 __ Ori(dst_high, lhs_high, high);
2259 }
2260 } else {
2261 if (high != low) {
2262 __ LoadConst32(TMP, high);
2263 }
2264 __ Or(dst_high, lhs_high, TMP);
2265 }
2266 } else if (instruction->IsXor()) {
2267 uint32_t low = Low32Bits(value);
2268 uint32_t high = High32Bits(value);
2269 if (IsUint<16>(low)) {
2270 if (dst_low != lhs_low || low != 0) {
2271 __ Xori(dst_low, lhs_low, low);
2272 }
2273 } else {
2274 __ LoadConst32(TMP, low);
2275 __ Xor(dst_low, lhs_low, TMP);
2276 }
2277 if (IsUint<16>(high)) {
2278 if (dst_high != lhs_high || high != 0) {
2279 __ Xori(dst_high, lhs_high, high);
2280 }
2281 } else {
2282 if (high != low) {
2283 __ LoadConst32(TMP, high);
2284 }
2285 __ Xor(dst_high, lhs_high, TMP);
2286 }
2287 } else if (instruction->IsAnd()) {
2288 uint32_t low = Low32Bits(value);
2289 uint32_t high = High32Bits(value);
2290 if (IsUint<16>(low)) {
2291 __ Andi(dst_low, lhs_low, low);
2292 } else if (low != 0xFFFFFFFF) {
2293 __ LoadConst32(TMP, low);
2294 __ And(dst_low, lhs_low, TMP);
2295 } else if (dst_low != lhs_low) {
2296 __ Move(dst_low, lhs_low);
2297 }
2298 if (IsUint<16>(high)) {
2299 __ Andi(dst_high, lhs_high, high);
2300 } else if (high != 0xFFFFFFFF) {
2301 if (high != low) {
2302 __ LoadConst32(TMP, high);
2303 }
2304 __ And(dst_high, lhs_high, TMP);
2305 } else if (dst_high != lhs_high) {
2306 __ Move(dst_high, lhs_high);
2307 }
2308 } else {
2309 if (instruction->IsSub()) {
2310 value = -value;
2311 } else {
2312 DCHECK(instruction->IsAdd());
2313 }
2314 int32_t low = Low32Bits(value);
2315 int32_t high = High32Bits(value);
2316 if (IsInt<16>(low)) {
2317 if (dst_low != lhs_low || low != 0) {
2318 __ Addiu(dst_low, lhs_low, low);
2319 }
2320 if (low != 0) {
2321 __ Sltiu(AT, dst_low, low);
2322 }
2323 } else {
2324 __ LoadConst32(TMP, low);
2325 __ Addu(dst_low, lhs_low, TMP);
2326 __ Sltu(AT, dst_low, TMP);
2327 }
2328 if (IsInt<16>(high)) {
2329 if (dst_high != lhs_high || high != 0) {
2330 __ Addiu(dst_high, lhs_high, high);
2331 }
2332 } else {
2333 if (high != low) {
2334 __ LoadConst32(TMP, high);
2335 }
2336 __ Addu(dst_high, lhs_high, TMP);
2337 }
2338 if (low != 0) {
2339 __ Addu(dst_high, dst_high, AT);
2340 }
2341 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002342 }
2343 break;
2344 }
2345
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002346 case DataType::Type::kFloat32:
2347 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002348 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2349 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2350 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2351 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002352 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002353 __ AddS(dst, lhs, rhs);
2354 } else {
2355 __ AddD(dst, lhs, rhs);
2356 }
2357 } else {
2358 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002359 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002360 __ SubS(dst, lhs, rhs);
2361 } else {
2362 __ SubD(dst, lhs, rhs);
2363 }
2364 }
2365 break;
2366 }
2367
2368 default:
2369 LOG(FATAL) << "Unexpected binary operation type " << type;
2370 }
2371}
2372
2373void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002374 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002375
Vladimir Markoca6fff82017-10-03 14:49:14 +01002376 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002379 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002380 locations->SetInAt(0, Location::RequiresRegister());
2381 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2382 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2383 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002384 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002385 locations->SetInAt(0, Location::RequiresRegister());
2386 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2387 locations->SetOut(Location::RequiresRegister());
2388 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 default:
2390 LOG(FATAL) << "Unexpected shift type " << type;
2391 }
2392}
2393
2394static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2395
2396void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002397 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002398 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002399 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002400
2401 Location rhs_location = locations->InAt(1);
2402 bool use_imm = rhs_location.IsConstant();
2403 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2404 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002405 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002406 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002407 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002408 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2409 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002410
2411 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002412 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002413 Register dst = locations->Out().AsRegister<Register>();
2414 Register lhs = locations->InAt(0).AsRegister<Register>();
2415 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002416 if (shift_value == 0) {
2417 if (dst != lhs) {
2418 __ Move(dst, lhs);
2419 }
2420 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 __ Sll(dst, lhs, shift_value);
2422 } else if (instr->IsShr()) {
2423 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002424 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002425 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002426 } else {
2427 if (has_ins_rotr) {
2428 __ Rotr(dst, lhs, shift_value);
2429 } else {
2430 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2431 __ Srl(dst, lhs, shift_value);
2432 __ Or(dst, dst, TMP);
2433 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434 }
2435 } else {
2436 if (instr->IsShl()) {
2437 __ Sllv(dst, lhs, rhs_reg);
2438 } else if (instr->IsShr()) {
2439 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002440 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002441 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002442 } else {
2443 if (has_ins_rotr) {
2444 __ Rotrv(dst, lhs, rhs_reg);
2445 } else {
2446 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002447 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2448 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2449 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2450 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2451 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002452 __ Sllv(TMP, lhs, TMP);
2453 __ Srlv(dst, lhs, rhs_reg);
2454 __ Or(dst, dst, TMP);
2455 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002456 }
2457 }
2458 break;
2459 }
2460
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002461 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002462 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2463 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2464 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2465 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2466 if (use_imm) {
2467 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002468 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002469 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002470 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002471 if (instr->IsShl()) {
2472 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2473 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2474 __ Sll(dst_low, lhs_low, shift_value);
2475 } else if (instr->IsShr()) {
2476 __ Srl(dst_low, lhs_low, shift_value);
2477 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2478 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002479 } else if (instr->IsUShr()) {
2480 __ Srl(dst_low, lhs_low, shift_value);
2481 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2482 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002483 } else {
2484 __ Srl(dst_low, lhs_low, shift_value);
2485 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2486 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002487 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002489 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002490 if (instr->IsShl()) {
2491 __ Sll(dst_low, lhs_low, shift_value);
2492 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2493 __ Sll(dst_high, lhs_high, shift_value);
2494 __ Or(dst_high, dst_high, TMP);
2495 } else if (instr->IsShr()) {
2496 __ Sra(dst_high, lhs_high, shift_value);
2497 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2498 __ Srl(dst_low, lhs_low, shift_value);
2499 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002500 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002501 __ Srl(dst_high, lhs_high, shift_value);
2502 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2503 __ Srl(dst_low, lhs_low, shift_value);
2504 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002505 } else {
2506 __ Srl(TMP, lhs_low, shift_value);
2507 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2508 __ Or(dst_low, dst_low, TMP);
2509 __ Srl(TMP, lhs_high, shift_value);
2510 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2511 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002512 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002513 }
2514 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002515 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002516 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002517 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002518 __ Move(dst_low, ZERO);
2519 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002520 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002521 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002522 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002523 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002524 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002525 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002526 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002527 // 64-bit rotation by 32 is just a swap.
2528 __ Move(dst_low, lhs_high);
2529 __ Move(dst_high, lhs_low);
2530 } else {
2531 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002532 __ Srl(dst_low, lhs_high, shift_value_high);
2533 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2534 __ Srl(dst_high, lhs_low, shift_value_high);
2535 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002536 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002537 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2538 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002539 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002540 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2541 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002542 __ Or(dst_high, dst_high, TMP);
2543 }
2544 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002545 }
2546 }
2547 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002548 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002549 MipsLabel done;
2550 if (instr->IsShl()) {
2551 __ Sllv(dst_low, lhs_low, rhs_reg);
2552 __ Nor(AT, ZERO, rhs_reg);
2553 __ Srl(TMP, lhs_low, 1);
2554 __ Srlv(TMP, TMP, AT);
2555 __ Sllv(dst_high, lhs_high, rhs_reg);
2556 __ Or(dst_high, dst_high, TMP);
2557 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002558 if (isR6) {
2559 __ Beqzc(TMP, &done, /* is_bare */ true);
2560 __ Move(dst_high, dst_low);
2561 __ Move(dst_low, ZERO);
2562 } else {
2563 __ Movn(dst_high, dst_low, TMP);
2564 __ Movn(dst_low, ZERO, TMP);
2565 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002566 } else if (instr->IsShr()) {
2567 __ Srav(dst_high, lhs_high, rhs_reg);
2568 __ Nor(AT, ZERO, rhs_reg);
2569 __ Sll(TMP, lhs_high, 1);
2570 __ Sllv(TMP, TMP, AT);
2571 __ Srlv(dst_low, lhs_low, rhs_reg);
2572 __ Or(dst_low, dst_low, TMP);
2573 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002574 if (isR6) {
2575 __ Beqzc(TMP, &done, /* is_bare */ true);
2576 __ Move(dst_low, dst_high);
2577 __ Sra(dst_high, dst_high, 31);
2578 } else {
2579 __ Sra(AT, dst_high, 31);
2580 __ Movn(dst_low, dst_high, TMP);
2581 __ Movn(dst_high, AT, TMP);
2582 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002583 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002584 __ Srlv(dst_high, lhs_high, rhs_reg);
2585 __ Nor(AT, ZERO, rhs_reg);
2586 __ Sll(TMP, lhs_high, 1);
2587 __ Sllv(TMP, TMP, AT);
2588 __ Srlv(dst_low, lhs_low, rhs_reg);
2589 __ Or(dst_low, dst_low, TMP);
2590 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002591 if (isR6) {
2592 __ Beqzc(TMP, &done, /* is_bare */ true);
2593 __ Move(dst_low, dst_high);
2594 __ Move(dst_high, ZERO);
2595 } else {
2596 __ Movn(dst_low, dst_high, TMP);
2597 __ Movn(dst_high, ZERO, TMP);
2598 }
2599 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002600 __ Nor(AT, ZERO, rhs_reg);
2601 __ Srlv(TMP, lhs_low, rhs_reg);
2602 __ Sll(dst_low, lhs_high, 1);
2603 __ Sllv(dst_low, dst_low, AT);
2604 __ Or(dst_low, dst_low, TMP);
2605 __ Srlv(TMP, lhs_high, rhs_reg);
2606 __ Sll(dst_high, lhs_low, 1);
2607 __ Sllv(dst_high, dst_high, AT);
2608 __ Or(dst_high, dst_high, TMP);
2609 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002610 if (isR6) {
2611 __ Beqzc(TMP, &done, /* is_bare */ true);
2612 __ Move(TMP, dst_high);
2613 __ Move(dst_high, dst_low);
2614 __ Move(dst_low, TMP);
2615 } else {
2616 __ Movn(AT, dst_high, TMP);
2617 __ Movn(dst_high, dst_low, TMP);
2618 __ Movn(dst_low, AT, TMP);
2619 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 }
2621 __ Bind(&done);
2622 }
2623 break;
2624 }
2625
2626 default:
2627 LOG(FATAL) << "Unexpected shift operation type " << type;
2628 }
2629}
2630
2631void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2632 HandleBinaryOp(instruction);
2633}
2634
2635void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2636 HandleBinaryOp(instruction);
2637}
2638
2639void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2640 HandleBinaryOp(instruction);
2641}
2642
2643void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2644 HandleBinaryOp(instruction);
2645}
2646
2647void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002648 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002649 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002650 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002651 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002652 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2653 object_array_get_with_read_barrier
2654 ? LocationSummary::kCallOnSlowPath
2655 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002656 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2657 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2658 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002659 locations->SetInAt(0, Location::RequiresRegister());
2660 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002661 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002662 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2663 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002664 // The output overlaps in the case of an object array get with
2665 // read barriers enabled: we do not want the move to overwrite the
2666 // array's location, as we need it to emit the read barrier.
2667 locations->SetOut(Location::RequiresRegister(),
2668 object_array_get_with_read_barrier
2669 ? Location::kOutputOverlap
2670 : Location::kNoOutputOverlap);
2671 }
2672 // We need a temporary register for the read barrier marking slow
2673 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2674 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002675 bool temp_needed = instruction->GetIndex()->IsConstant()
2676 ? !kBakerReadBarrierThunksEnableForFields
2677 : !kBakerReadBarrierThunksEnableForArrays;
2678 if (temp_needed) {
2679 locations->AddTemp(Location::RequiresRegister());
2680 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002681 }
2682}
2683
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002684static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2685 auto null_checker = [codegen, instruction]() {
2686 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002687 };
2688 return null_checker;
2689}
2690
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002691void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2692 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002693 Location obj_loc = locations->InAt(0);
2694 Register obj = obj_loc.AsRegister<Register>();
2695 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002697 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002698 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002701 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2702 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002703 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002704 case DataType::Type::kBool:
2705 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002706 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002707 if (index.IsConstant()) {
2708 size_t offset =
2709 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002710 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002711 } else {
2712 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002713 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002714 }
2715 break;
2716 }
2717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002719 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002720 if (index.IsConstant()) {
2721 size_t offset =
2722 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002723 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002724 } else {
2725 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002726 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002727 }
2728 break;
2729 }
2730
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002731 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002732 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002733 if (maybe_compressed_char_at) {
2734 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2735 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2736 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2737 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2738 "Expecting 0=compressed, 1=uncompressed");
2739 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002740 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002741 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2742 if (maybe_compressed_char_at) {
2743 MipsLabel uncompressed_load, done;
2744 __ Bnez(TMP, &uncompressed_load);
2745 __ LoadFromOffset(kLoadUnsignedByte,
2746 out,
2747 obj,
2748 data_offset + (const_index << TIMES_1));
2749 __ B(&done);
2750 __ Bind(&uncompressed_load);
2751 __ LoadFromOffset(kLoadUnsignedHalfword,
2752 out,
2753 obj,
2754 data_offset + (const_index << TIMES_2));
2755 __ Bind(&done);
2756 } else {
2757 __ LoadFromOffset(kLoadUnsignedHalfword,
2758 out,
2759 obj,
2760 data_offset + (const_index << TIMES_2),
2761 null_checker);
2762 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002763 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002764 Register index_reg = index.AsRegister<Register>();
2765 if (maybe_compressed_char_at) {
2766 MipsLabel uncompressed_load, done;
2767 __ Bnez(TMP, &uncompressed_load);
2768 __ Addu(TMP, obj, index_reg);
2769 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2770 __ B(&done);
2771 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002772 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002773 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2774 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002775 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2776 __ Addu(TMP, index_reg, obj);
2777 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002778 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002779 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002780 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2781 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002782 }
2783 break;
2784 }
2785
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002786 case DataType::Type::kInt16: {
2787 Register out = out_loc.AsRegister<Register>();
2788 if (index.IsConstant()) {
2789 size_t offset =
2790 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2791 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002792 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2793 __ Addu(TMP, index.AsRegister<Register>(), obj);
2794 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002795 } else {
2796 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2797 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2798 }
2799 break;
2800 }
2801
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002802 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002803 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002804 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002805 if (index.IsConstant()) {
2806 size_t offset =
2807 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002808 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002809 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2810 __ Addu(TMP, index.AsRegister<Register>(), obj);
2811 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002812 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002813 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002814 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002815 }
2816 break;
2817 }
2818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002819 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002820 static_assert(
2821 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2822 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2823 // /* HeapReference<Object> */ out =
2824 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2825 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002826 bool temp_needed = index.IsConstant()
2827 ? !kBakerReadBarrierThunksEnableForFields
2828 : !kBakerReadBarrierThunksEnableForArrays;
2829 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002830 // Note that a potential implicit null check is handled in this
2831 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002832 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2833 if (index.IsConstant()) {
2834 // Array load with a constant index can be treated as a field load.
2835 size_t offset =
2836 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2837 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2838 out_loc,
2839 obj,
2840 offset,
2841 temp,
2842 /* needs_null_check */ false);
2843 } else {
2844 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2845 out_loc,
2846 obj,
2847 data_offset,
2848 index,
2849 temp,
2850 /* needs_null_check */ false);
2851 }
Alexey Frunze15958152017-02-09 19:08:30 -08002852 } else {
2853 Register out = out_loc.AsRegister<Register>();
2854 if (index.IsConstant()) {
2855 size_t offset =
2856 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2857 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2858 // If read barriers are enabled, emit read barriers other than
2859 // Baker's using a slow path (and also unpoison the loaded
2860 // reference, if heap poisoning is enabled).
2861 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2862 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002863 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002864 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2865 // If read barriers are enabled, emit read barriers other than
2866 // Baker's using a slow path (and also unpoison the loaded
2867 // reference, if heap poisoning is enabled).
2868 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2869 out_loc,
2870 out_loc,
2871 obj_loc,
2872 data_offset,
2873 index);
2874 }
2875 }
2876 break;
2877 }
2878
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002879 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002880 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002881 if (index.IsConstant()) {
2882 size_t offset =
2883 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002884 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002885 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2886 __ Addu(TMP, index.AsRegister<Register>(), obj);
2887 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002888 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002889 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002890 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002891 }
2892 break;
2893 }
2894
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002895 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002896 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002897 if (index.IsConstant()) {
2898 size_t offset =
2899 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002900 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002901 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2902 __ Addu(TMP, index.AsRegister<Register>(), obj);
2903 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002904 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002905 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002906 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002907 }
2908 break;
2909 }
2910
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002911 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002912 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002913 if (index.IsConstant()) {
2914 size_t offset =
2915 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002916 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002917 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2918 __ Addu(TMP, index.AsRegister<Register>(), obj);
2919 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002920 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002921 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002922 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002923 }
2924 break;
2925 }
2926
Aart Bik66c158e2018-01-31 12:55:04 -08002927 case DataType::Type::kUint32:
2928 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002929 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002930 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2931 UNREACHABLE();
2932 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002933}
2934
2935void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002936 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002937 locations->SetInAt(0, Location::RequiresRegister());
2938 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2939}
2940
2941void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2942 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002943 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002944 Register obj = locations->InAt(0).AsRegister<Register>();
2945 Register out = locations->Out().AsRegister<Register>();
2946 __ LoadFromOffset(kLoadWord, out, obj, offset);
2947 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002948 // Mask out compression flag from String's array length.
2949 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2950 __ Srl(out, out, 1u);
2951 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002952}
2953
Alexey Frunzef58b2482016-09-02 22:14:06 -07002954Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2955 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2956 ? Location::ConstantLocation(instruction->AsConstant())
2957 : Location::RequiresRegister();
2958}
2959
2960Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2961 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2962 // We can store a non-zero float or double constant without first loading it into the FPU,
2963 // but we should only prefer this if the constant has a single use.
2964 if (instruction->IsConstant() &&
2965 (instruction->AsConstant()->IsZeroBitPattern() ||
2966 instruction->GetUses().HasExactlyOneElement())) {
2967 return Location::ConstantLocation(instruction->AsConstant());
2968 // Otherwise fall through and require an FPU register for the constant.
2969 }
2970 return Location::RequiresFpuRegister();
2971}
2972
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002973void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002974 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002975
2976 bool needs_write_barrier =
2977 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2978 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2979
Vladimir Markoca6fff82017-10-03 14:49:14 +01002980 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002981 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002982 may_need_runtime_call_for_type_check ?
2983 LocationSummary::kCallOnSlowPath :
2984 LocationSummary::kNoCall);
2985
2986 locations->SetInAt(0, Location::RequiresRegister());
2987 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002988 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002989 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002990 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002991 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2992 }
2993 if (needs_write_barrier) {
2994 // Temporary register for the write barrier.
2995 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002996 }
2997}
2998
2999void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
3000 LocationSummary* locations = instruction->GetLocations();
3001 Register obj = locations->InAt(0).AsRegister<Register>();
3002 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003003 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003004 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08003005 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003006 bool needs_write_barrier =
3007 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01003008 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003009 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003010
3011 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003013 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003014 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003015 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003016 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003017 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003018 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003019 __ Addu(base_reg, obj, index.AsRegister<Register>());
3020 }
3021 if (value_location.IsConstant()) {
3022 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3023 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
3024 } else {
3025 Register value = value_location.AsRegister<Register>();
3026 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003027 }
3028 break;
3029 }
3030
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003031 case DataType::Type::kUint16:
3032 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003033 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003034 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003035 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02003036 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3037 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003038 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003039 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003040 }
3041 if (value_location.IsConstant()) {
3042 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3043 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
3044 } else {
3045 Register value = value_location.AsRegister<Register>();
3046 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003047 }
3048 break;
3049 }
3050
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003051 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08003052 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3053 if (index.IsConstant()) {
3054 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003055 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3056 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08003057 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003058 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003059 }
3060 if (value_location.IsConstant()) {
3061 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3062 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3063 } else {
3064 Register value = value_location.AsRegister<Register>();
3065 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3066 }
3067 break;
3068 }
3069
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003070 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08003071 if (value_location.IsConstant()) {
3072 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003073 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003074 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003075 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003076 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003077 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003078 }
Alexey Frunze15958152017-02-09 19:08:30 -08003079 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3080 DCHECK_EQ(value, 0);
3081 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3082 DCHECK(!needs_write_barrier);
3083 DCHECK(!may_need_runtime_call_for_type_check);
3084 break;
3085 }
3086
3087 DCHECK(needs_write_barrier);
3088 Register value = value_location.AsRegister<Register>();
3089 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3090 Register temp2 = TMP; // Doesn't need to survive slow path.
3091 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3092 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3093 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3094 MipsLabel done;
3095 SlowPathCodeMIPS* slow_path = nullptr;
3096
3097 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003098 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003099 codegen_->AddSlowPath(slow_path);
3100 if (instruction->GetValueCanBeNull()) {
3101 MipsLabel non_zero;
3102 __ Bnez(value, &non_zero);
3103 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3104 if (index.IsConstant()) {
3105 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003106 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3107 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003108 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003109 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003110 }
Alexey Frunze15958152017-02-09 19:08:30 -08003111 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3112 __ B(&done);
3113 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003114 }
Alexey Frunze15958152017-02-09 19:08:30 -08003115
3116 // Note that when read barriers are enabled, the type checks
3117 // are performed without read barriers. This is fine, even in
3118 // the case where a class object is in the from-space after
3119 // the flip, as a comparison involving such a type would not
3120 // produce a false positive; it may of course produce a false
3121 // negative, in which case we would take the ArraySet slow
3122 // path.
3123
3124 // /* HeapReference<Class> */ temp1 = obj->klass_
3125 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3126 __ MaybeUnpoisonHeapReference(temp1);
3127
3128 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3129 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3130 // /* HeapReference<Class> */ temp2 = value->klass_
3131 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3132 // If heap poisoning is enabled, no need to unpoison `temp1`
3133 // nor `temp2`, as we are comparing two poisoned references.
3134
3135 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3136 MipsLabel do_put;
3137 __ Beq(temp1, temp2, &do_put);
3138 // If heap poisoning is enabled, the `temp1` reference has
3139 // not been unpoisoned yet; unpoison it now.
3140 __ MaybeUnpoisonHeapReference(temp1);
3141
3142 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3143 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3144 // If heap poisoning is enabled, no need to unpoison
3145 // `temp1`, as we are comparing against null below.
3146 __ Bnez(temp1, slow_path->GetEntryLabel());
3147 __ Bind(&do_put);
3148 } else {
3149 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3150 }
3151 }
3152
3153 Register source = value;
3154 if (kPoisonHeapReferences) {
3155 // Note that in the case where `value` is a null reference,
3156 // we do not enter this block, as a null reference does not
3157 // need poisoning.
3158 __ Move(temp1, value);
3159 __ PoisonHeapReference(temp1);
3160 source = temp1;
3161 }
3162
3163 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3164 if (index.IsConstant()) {
3165 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003166 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003167 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003168 }
3169 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3170
3171 if (!may_need_runtime_call_for_type_check) {
3172 codegen_->MaybeRecordImplicitNullCheck(instruction);
3173 }
3174
3175 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3176
3177 if (done.IsLinked()) {
3178 __ Bind(&done);
3179 }
3180
3181 if (slow_path != nullptr) {
3182 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003183 }
3184 break;
3185 }
3186
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003187 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003188 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003189 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003190 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003191 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3192 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003193 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003194 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003195 }
3196 if (value_location.IsConstant()) {
3197 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3198 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3199 } else {
3200 Register value = value_location.AsRegisterPairLow<Register>();
3201 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003202 }
3203 break;
3204 }
3205
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003206 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003207 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003208 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003209 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003210 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3211 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003212 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003213 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003214 }
3215 if (value_location.IsConstant()) {
3216 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3217 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3218 } else {
3219 FRegister value = value_location.AsFpuRegister<FRegister>();
3220 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003221 }
3222 break;
3223 }
3224
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003225 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003226 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003227 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003228 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003229 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3230 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003231 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003232 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003233 }
3234 if (value_location.IsConstant()) {
3235 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3236 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3237 } else {
3238 FRegister value = value_location.AsFpuRegister<FRegister>();
3239 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003240 }
3241 break;
3242 }
3243
Aart Bik66c158e2018-01-31 12:55:04 -08003244 case DataType::Type::kUint32:
3245 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003246 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003247 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3248 UNREACHABLE();
3249 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003250}
3251
Lena Djokica2901602017-09-21 13:50:52 +02003252void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3253 HIntermediateArrayAddressIndex* instruction) {
3254 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003255 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003256
3257 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3258
3259 locations->SetInAt(0, Location::RequiresRegister());
3260 locations->SetInAt(1, Location::ConstantLocation(shift));
3261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3262}
3263
3264void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3265 HIntermediateArrayAddressIndex* instruction) {
3266 LocationSummary* locations = instruction->GetLocations();
3267 Register index_reg = locations->InAt(0).AsRegister<Register>();
3268 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3269 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3270}
3271
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003272void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003273 RegisterSet caller_saves = RegisterSet::Empty();
3274 InvokeRuntimeCallingConvention calling_convention;
3275 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3276 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3277 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003278
3279 HInstruction* index = instruction->InputAt(0);
3280 HInstruction* length = instruction->InputAt(1);
3281
3282 bool const_index = false;
3283 bool const_length = false;
3284
3285 if (index->IsConstant()) {
3286 if (length->IsConstant()) {
3287 const_index = true;
3288 const_length = true;
3289 } else {
3290 int32_t index_value = index->AsIntConstant()->GetValue();
3291 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3292 const_index = true;
3293 }
3294 }
3295 } else if (length->IsConstant()) {
3296 int32_t length_value = length->AsIntConstant()->GetValue();
3297 if (IsUint<15>(length_value)) {
3298 const_length = true;
3299 }
3300 }
3301
3302 locations->SetInAt(0, const_index
3303 ? Location::ConstantLocation(index->AsConstant())
3304 : Location::RequiresRegister());
3305 locations->SetInAt(1, const_length
3306 ? Location::ConstantLocation(length->AsConstant())
3307 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003308}
3309
3310void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3311 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003312 Location index_loc = locations->InAt(0);
3313 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003314
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003315 if (length_loc.IsConstant()) {
3316 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3317 if (index_loc.IsConstant()) {
3318 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3319 if (index < 0 || index >= length) {
3320 BoundsCheckSlowPathMIPS* slow_path =
3321 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3322 codegen_->AddSlowPath(slow_path);
3323 __ B(slow_path->GetEntryLabel());
3324 } else {
3325 // Nothing to be done.
3326 }
3327 return;
3328 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003329
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003330 BoundsCheckSlowPathMIPS* slow_path =
3331 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3332 codegen_->AddSlowPath(slow_path);
3333 Register index = index_loc.AsRegister<Register>();
3334 if (length == 0) {
3335 __ B(slow_path->GetEntryLabel());
3336 } else if (length == 1) {
3337 __ Bnez(index, slow_path->GetEntryLabel());
3338 } else {
3339 DCHECK(IsUint<15>(length)) << length;
3340 __ Sltiu(TMP, index, length);
3341 __ Beqz(TMP, slow_path->GetEntryLabel());
3342 }
3343 } else {
3344 Register length = length_loc.AsRegister<Register>();
3345 BoundsCheckSlowPathMIPS* slow_path =
3346 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3347 codegen_->AddSlowPath(slow_path);
3348 if (index_loc.IsConstant()) {
3349 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3350 if (index < 0) {
3351 __ B(slow_path->GetEntryLabel());
3352 } else if (index == 0) {
3353 __ Blez(length, slow_path->GetEntryLabel());
3354 } else {
3355 DCHECK(IsInt<16>(index + 1)) << index;
3356 __ Sltiu(TMP, length, index + 1);
3357 __ Bnez(TMP, slow_path->GetEntryLabel());
3358 }
3359 } else {
3360 Register index = index_loc.AsRegister<Register>();
3361 __ Bgeu(index, length, slow_path->GetEntryLabel());
3362 }
3363 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003364}
3365
Alexey Frunze15958152017-02-09 19:08:30 -08003366// Temp is used for read barrier.
3367static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3368 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003369 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003370 (kUseBakerReadBarrier ||
3371 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3372 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3373 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3374 return 1;
3375 }
3376 return 0;
3377}
3378
3379// Extra temp is used for read barrier.
3380static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3381 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3382}
3383
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003384void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003385 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003386 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003387 LocationSummary* locations =
3388 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003389 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00003390 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3391 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3392 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3393 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3394 } else {
3395 locations->SetInAt(1, Location::RequiresRegister());
3396 }
Alexey Frunze15958152017-02-09 19:08:30 -08003397 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003398}
3399
3400void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003401 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003402 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003403 Location obj_loc = locations->InAt(0);
3404 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko175e7862018-03-27 09:03:13 +00003405 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08003406 Location temp_loc = locations->GetTemp(0);
3407 Register temp = temp_loc.AsRegister<Register>();
3408 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3409 DCHECK_LE(num_temps, 2u);
3410 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003411 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3412 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3413 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3414 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3415 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3416 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3417 const uint32_t object_array_data_offset =
3418 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3419 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003420
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003421 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003422 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003423 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3424 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003425 codegen_->AddSlowPath(slow_path);
3426
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003427 // Avoid this check if we know `obj` is not null.
3428 if (instruction->MustDoNullCheck()) {
3429 __ Beqz(obj, &done);
3430 }
3431
3432 switch (type_check_kind) {
3433 case TypeCheckKind::kExactCheck:
3434 case TypeCheckKind::kArrayCheck: {
3435 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003436 GenerateReferenceLoadTwoRegisters(instruction,
3437 temp_loc,
3438 obj_loc,
3439 class_offset,
3440 maybe_temp2_loc,
3441 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003442 // Jump to slow path for throwing the exception or doing a
3443 // more involved array check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003444 __ Bne(temp, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003445 break;
3446 }
3447
3448 case TypeCheckKind::kAbstractClassCheck: {
3449 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003450 GenerateReferenceLoadTwoRegisters(instruction,
3451 temp_loc,
3452 obj_loc,
3453 class_offset,
3454 maybe_temp2_loc,
3455 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003456 // If the class is abstract, we eagerly fetch the super class of the
3457 // object to avoid doing a comparison we know will fail.
3458 MipsLabel loop;
3459 __ Bind(&loop);
3460 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003461 GenerateReferenceLoadOneRegister(instruction,
3462 temp_loc,
3463 super_offset,
3464 maybe_temp2_loc,
3465 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003466 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3467 // exception.
3468 __ Beqz(temp, slow_path->GetEntryLabel());
3469 // Otherwise, compare the classes.
Vladimir Marko175e7862018-03-27 09:03:13 +00003470 __ Bne(temp, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003471 break;
3472 }
3473
3474 case TypeCheckKind::kClassHierarchyCheck: {
3475 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003476 GenerateReferenceLoadTwoRegisters(instruction,
3477 temp_loc,
3478 obj_loc,
3479 class_offset,
3480 maybe_temp2_loc,
3481 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003482 // Walk over the class hierarchy to find a match.
3483 MipsLabel loop;
3484 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00003485 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003486 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003487 GenerateReferenceLoadOneRegister(instruction,
3488 temp_loc,
3489 super_offset,
3490 maybe_temp2_loc,
3491 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003492 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3493 // exception. Otherwise, jump to the beginning of the loop.
3494 __ Bnez(temp, &loop);
3495 __ B(slow_path->GetEntryLabel());
3496 break;
3497 }
3498
3499 case TypeCheckKind::kArrayObjectCheck: {
3500 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003501 GenerateReferenceLoadTwoRegisters(instruction,
3502 temp_loc,
3503 obj_loc,
3504 class_offset,
3505 maybe_temp2_loc,
3506 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003507 // Do an exact check.
Vladimir Marko175e7862018-03-27 09:03:13 +00003508 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003509 // Otherwise, we need to check that the object's class is a non-primitive array.
3510 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003511 GenerateReferenceLoadOneRegister(instruction,
3512 temp_loc,
3513 component_offset,
3514 maybe_temp2_loc,
3515 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003516 // If the component type is null, jump to the slow path to throw the exception.
3517 __ Beqz(temp, slow_path->GetEntryLabel());
3518 // Otherwise, the object is indeed an array, further check that this component
3519 // type is not a primitive type.
3520 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3521 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3522 __ Bnez(temp, slow_path->GetEntryLabel());
3523 break;
3524 }
3525
3526 case TypeCheckKind::kUnresolvedCheck:
3527 // We always go into the type check slow path for the unresolved check case.
3528 // We cannot directly call the CheckCast runtime entry point
3529 // without resorting to a type checking slow path here (i.e. by
3530 // calling InvokeRuntime directly), as it would require to
3531 // assign fixed registers for the inputs of this HInstanceOf
3532 // instruction (following the runtime calling convention), which
3533 // might be cluttered by the potential first read barrier
3534 // emission at the beginning of this method.
3535 __ B(slow_path->GetEntryLabel());
3536 break;
3537
3538 case TypeCheckKind::kInterfaceCheck: {
3539 // Avoid read barriers to improve performance of the fast path. We can not get false
3540 // positives by doing this.
3541 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003542 GenerateReferenceLoadTwoRegisters(instruction,
3543 temp_loc,
3544 obj_loc,
3545 class_offset,
3546 maybe_temp2_loc,
3547 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003548 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003549 GenerateReferenceLoadTwoRegisters(instruction,
3550 temp_loc,
3551 temp_loc,
3552 iftable_offset,
3553 maybe_temp2_loc,
3554 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003555 // Iftable is never null.
3556 __ Lw(TMP, temp, array_length_offset);
3557 // Loop through the iftable and check if any class matches.
3558 MipsLabel loop;
3559 __ Bind(&loop);
3560 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3561 __ Beqz(TMP, slow_path->GetEntryLabel());
3562 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3563 __ MaybeUnpoisonHeapReference(AT);
3564 // Go to next interface.
3565 __ Addiu(TMP, TMP, -2);
3566 // Compare the classes and continue the loop if they do not match.
Vladimir Marko175e7862018-03-27 09:03:13 +00003567 __ Bne(AT, cls.AsRegister<Register>(), &loop);
3568 break;
3569 }
3570
3571 case TypeCheckKind::kBitstringCheck: {
3572 // /* HeapReference<Class> */ temp = obj->klass_
3573 GenerateReferenceLoadTwoRegisters(instruction,
3574 temp_loc,
3575 obj_loc,
3576 class_offset,
3577 maybe_temp2_loc,
3578 kWithoutReadBarrier);
3579
3580 GenerateBitstringTypeCheckCompare(instruction, temp);
3581 __ Bnez(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003582 break;
3583 }
3584 }
3585
3586 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003587 __ Bind(slow_path->GetExitLabel());
3588}
3589
3590void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3591 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003592 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003593 locations->SetInAt(0, Location::RequiresRegister());
3594 if (check->HasUses()) {
3595 locations->SetOut(Location::SameAsFirstInput());
3596 }
3597}
3598
3599void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3600 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003601 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003602 check->GetLoadClass(),
3603 check,
3604 check->GetDexPc(),
3605 true);
3606 codegen_->AddSlowPath(slow_path);
3607 GenerateClassInitializationCheck(slow_path,
3608 check->GetLocations()->InAt(0).AsRegister<Register>());
3609}
3610
3611void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003612 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003613
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003614 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003615 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003616
3617 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003618 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003619 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003620 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003621 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003622 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003623 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003624 locations->SetInAt(0, Location::RequiresRegister());
3625 locations->SetInAt(1, Location::RequiresRegister());
3626 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3627 break;
3628
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003629 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003630 locations->SetInAt(0, Location::RequiresRegister());
3631 locations->SetInAt(1, Location::RequiresRegister());
3632 // Output overlaps because it is written before doing the low comparison.
3633 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3634 break;
3635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003636 case DataType::Type::kFloat32:
3637 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003638 locations->SetInAt(0, Location::RequiresFpuRegister());
3639 locations->SetInAt(1, Location::RequiresFpuRegister());
3640 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003641 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003642
3643 default:
3644 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3645 }
3646}
3647
3648void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3649 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003650 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003651 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003652 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003653
3654 // 0 if: left == right
3655 // 1 if: left > right
3656 // -1 if: left < right
3657 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003658 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003659 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003660 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003661 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003662 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003663 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003664 Register lhs = locations->InAt(0).AsRegister<Register>();
3665 Register rhs = locations->InAt(1).AsRegister<Register>();
3666 __ Slt(TMP, lhs, rhs);
3667 __ Slt(res, rhs, lhs);
3668 __ Subu(res, res, TMP);
3669 break;
3670 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003671 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003672 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003673 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3674 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3675 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3676 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3677 // TODO: more efficient (direct) comparison with a constant.
3678 __ Slt(TMP, lhs_high, rhs_high);
3679 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3680 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3681 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3682 __ Sltu(TMP, lhs_low, rhs_low);
3683 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3684 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3685 __ Bind(&done);
3686 break;
3687 }
3688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003689 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003690 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003691 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3692 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3693 MipsLabel done;
3694 if (isR6) {
3695 __ CmpEqS(FTMP, lhs, rhs);
3696 __ LoadConst32(res, 0);
3697 __ Bc1nez(FTMP, &done);
3698 if (gt_bias) {
3699 __ CmpLtS(FTMP, lhs, rhs);
3700 __ LoadConst32(res, -1);
3701 __ Bc1nez(FTMP, &done);
3702 __ LoadConst32(res, 1);
3703 } else {
3704 __ CmpLtS(FTMP, rhs, lhs);
3705 __ LoadConst32(res, 1);
3706 __ Bc1nez(FTMP, &done);
3707 __ LoadConst32(res, -1);
3708 }
3709 } else {
3710 if (gt_bias) {
3711 __ ColtS(0, lhs, rhs);
3712 __ LoadConst32(res, -1);
3713 __ Bc1t(0, &done);
3714 __ CeqS(0, lhs, rhs);
3715 __ LoadConst32(res, 1);
3716 __ Movt(res, ZERO, 0);
3717 } else {
3718 __ ColtS(0, rhs, lhs);
3719 __ LoadConst32(res, 1);
3720 __ Bc1t(0, &done);
3721 __ CeqS(0, lhs, rhs);
3722 __ LoadConst32(res, -1);
3723 __ Movt(res, ZERO, 0);
3724 }
3725 }
3726 __ Bind(&done);
3727 break;
3728 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003729 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003730 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003731 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3732 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3733 MipsLabel done;
3734 if (isR6) {
3735 __ CmpEqD(FTMP, lhs, rhs);
3736 __ LoadConst32(res, 0);
3737 __ Bc1nez(FTMP, &done);
3738 if (gt_bias) {
3739 __ CmpLtD(FTMP, lhs, rhs);
3740 __ LoadConst32(res, -1);
3741 __ Bc1nez(FTMP, &done);
3742 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003743 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003744 __ CmpLtD(FTMP, rhs, lhs);
3745 __ LoadConst32(res, 1);
3746 __ Bc1nez(FTMP, &done);
3747 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003748 }
3749 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003750 if (gt_bias) {
3751 __ ColtD(0, lhs, rhs);
3752 __ LoadConst32(res, -1);
3753 __ Bc1t(0, &done);
3754 __ CeqD(0, lhs, rhs);
3755 __ LoadConst32(res, 1);
3756 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003757 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003758 __ ColtD(0, rhs, lhs);
3759 __ LoadConst32(res, 1);
3760 __ Bc1t(0, &done);
3761 __ CeqD(0, lhs, rhs);
3762 __ LoadConst32(res, -1);
3763 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003764 }
3765 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003766 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003767 break;
3768 }
3769
3770 default:
3771 LOG(FATAL) << "Unimplemented compare type " << in_type;
3772 }
3773}
3774
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003775void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003776 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003777 switch (instruction->InputAt(0)->GetType()) {
3778 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003779 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003780 locations->SetInAt(0, Location::RequiresRegister());
3781 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3782 break;
3783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003784 case DataType::Type::kFloat32:
3785 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003786 locations->SetInAt(0, Location::RequiresFpuRegister());
3787 locations->SetInAt(1, Location::RequiresFpuRegister());
3788 break;
3789 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003790 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003791 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3792 }
3793}
3794
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003795void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003796 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003797 return;
3798 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003799
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003800 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003801 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003802
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003803 switch (type) {
3804 default:
3805 // Integer case.
3806 GenerateIntCompare(instruction->GetCondition(), locations);
3807 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003808
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003809 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003810 GenerateLongCompare(instruction->GetCondition(), locations);
3811 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003812
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003813 case DataType::Type::kFloat32:
3814 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003815 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3816 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003817 }
3818}
3819
Alexey Frunze7e99e052015-11-24 19:28:01 -08003820void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3821 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003822
3823 LocationSummary* locations = instruction->GetLocations();
3824 Location second = locations->InAt(1);
3825 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003826 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003827 DCHECK(imm == 1 || imm == -1);
3828
Lena Djokic4b8025c2017-12-21 16:15:50 +01003829 if (instruction->GetResultType() == DataType::Type::kInt32) {
3830 Register out = locations->Out().AsRegister<Register>();
3831 Register dividend = locations->InAt(0).AsRegister<Register>();
3832
3833 if (instruction->IsRem()) {
3834 __ Move(out, ZERO);
3835 } else {
3836 if (imm == -1) {
3837 __ Subu(out, ZERO, dividend);
3838 } else if (out != dividend) {
3839 __ Move(out, dividend);
3840 }
3841 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003842 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003843 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3844 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3845 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3846 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3847 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3848
3849 if (instruction->IsRem()) {
3850 __ Move(out_high, ZERO);
3851 __ Move(out_low, ZERO);
3852 } else {
3853 if (imm == -1) {
3854 __ Subu(out_low, ZERO, in_low);
3855 __ Sltu(AT, ZERO, out_low);
3856 __ Subu(out_high, ZERO, in_high);
3857 __ Subu(out_high, out_high, AT);
3858 } else {
3859 __ Move(out_low, in_low);
3860 __ Move(out_high, in_high);
3861 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003862 }
3863 }
3864}
3865
3866void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3867 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003868
3869 LocationSummary* locations = instruction->GetLocations();
3870 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003871 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3872 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003873 DCHECK(second.IsConstant());
3874
Lena Djokic4b8025c2017-12-21 16:15:50 +01003875 if (instruction->GetResultType() == DataType::Type::kInt32) {
3876 Register out = locations->Out().AsRegister<Register>();
3877 Register dividend = locations->InAt(0).AsRegister<Register>();
3878 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3879 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3880 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003881
Lena Djokic4b8025c2017-12-21 16:15:50 +01003882 if (instruction->IsDiv()) {
3883 if (ctz_imm == 1) {
3884 // Fast path for division by +/-2, which is very common.
3885 __ Srl(TMP, dividend, 31);
3886 } else {
3887 __ Sra(TMP, dividend, 31);
3888 __ Srl(TMP, TMP, 32 - ctz_imm);
3889 }
3890 __ Addu(out, dividend, TMP);
3891 __ Sra(out, out, ctz_imm);
3892 if (imm < 0) {
3893 __ Subu(out, ZERO, out);
3894 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003895 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003896 if (ctz_imm == 1) {
3897 // Fast path for modulo +/-2, which is very common.
3898 __ Sra(TMP, dividend, 31);
3899 __ Subu(out, dividend, TMP);
3900 __ Andi(out, out, 1);
3901 __ Addu(out, out, TMP);
3902 } else {
3903 __ Sra(TMP, dividend, 31);
3904 __ Srl(TMP, TMP, 32 - ctz_imm);
3905 __ Addu(out, dividend, TMP);
3906 if (IsUint<16>(abs_imm - 1)) {
3907 __ Andi(out, out, abs_imm - 1);
3908 } else {
3909 if (is_r2_or_newer) {
3910 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3911 } else {
3912 __ Sll(out, out, 32 - ctz_imm);
3913 __ Srl(out, out, 32 - ctz_imm);
3914 }
3915 }
3916 __ Subu(out, out, TMP);
3917 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003918 }
3919 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003920 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3921 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3922 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3923 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3924 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3925 int64_t imm = Int64FromConstant(second.GetConstant());
3926 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3927 int ctz_imm = CTZ(abs_imm);
3928
3929 if (instruction->IsDiv()) {
3930 if (ctz_imm < 32) {
3931 if (ctz_imm == 1) {
3932 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003933 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003934 __ Sra(AT, in_high, 31);
3935 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003936 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003937 __ Addu(AT, AT, in_low);
3938 __ Sltu(TMP, AT, in_low);
3939 __ Addu(out_high, in_high, TMP);
3940 __ Srl(out_low, AT, ctz_imm);
3941 if (is_r2_or_newer) {
3942 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3943 __ Sra(out_high, out_high, ctz_imm);
3944 } else {
3945 __ Sll(AT, out_high, 32 - ctz_imm);
3946 __ Sra(out_high, out_high, ctz_imm);
3947 __ Or(out_low, out_low, AT);
3948 }
3949 if (imm < 0) {
3950 __ Subu(out_low, ZERO, out_low);
3951 __ Sltu(AT, ZERO, out_low);
3952 __ Subu(out_high, ZERO, out_high);
3953 __ Subu(out_high, out_high, AT);
3954 }
3955 } else if (ctz_imm == 32) {
3956 __ Sra(AT, in_high, 31);
3957 __ Addu(AT, AT, in_low);
3958 __ Sltu(AT, AT, in_low);
3959 __ Addu(out_low, in_high, AT);
3960 if (imm < 0) {
3961 __ Srl(TMP, out_low, 31);
3962 __ Subu(out_low, ZERO, out_low);
3963 __ Sltu(AT, ZERO, out_low);
3964 __ Subu(out_high, TMP, AT);
3965 } else {
3966 __ Sra(out_high, out_low, 31);
3967 }
3968 } else if (ctz_imm < 63) {
3969 __ Sra(AT, in_high, 31);
3970 __ Srl(TMP, AT, 64 - ctz_imm);
3971 __ Addu(AT, AT, in_low);
3972 __ Sltu(AT, AT, in_low);
3973 __ Addu(out_low, in_high, AT);
3974 __ Addu(out_low, out_low, TMP);
3975 __ Sra(out_low, out_low, ctz_imm - 32);
3976 if (imm < 0) {
3977 __ Subu(out_low, ZERO, out_low);
3978 }
3979 __ Sra(out_high, out_low, 31);
3980 } else {
3981 DCHECK_LT(imm, 0);
3982 if (is_r6) {
3983 __ Aui(AT, in_high, 0x8000);
3984 } else {
3985 __ Lui(AT, 0x8000);
3986 __ Xor(AT, AT, in_high);
3987 }
3988 __ Or(AT, AT, in_low);
3989 __ Sltiu(out_low, AT, 1);
3990 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003991 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003992 } else {
3993 if ((ctz_imm == 1) && !is_r6) {
3994 __ Andi(AT, in_low, 1);
3995 __ Sll(TMP, in_low, 31);
3996 __ And(TMP, in_high, TMP);
3997 __ Sra(out_high, TMP, 31);
3998 __ Or(out_low, out_high, AT);
3999 } else if (ctz_imm < 32) {
4000 __ Sra(AT, in_high, 31);
4001 if (ctz_imm <= 16) {
4002 __ Andi(out_low, in_low, abs_imm - 1);
4003 } else if (is_r2_or_newer) {
4004 __ Ext(out_low, in_low, 0, ctz_imm);
4005 } else {
4006 __ Sll(out_low, in_low, 32 - ctz_imm);
4007 __ Srl(out_low, out_low, 32 - ctz_imm);
4008 }
4009 if (is_r6) {
4010 __ Selnez(out_high, AT, out_low);
4011 } else {
4012 __ Movz(AT, ZERO, out_low);
4013 __ Move(out_high, AT);
4014 }
4015 if (is_r2_or_newer) {
4016 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
4017 } else {
4018 __ Sll(AT, out_high, ctz_imm);
4019 __ Or(out_low, out_low, AT);
4020 }
4021 } else if (ctz_imm == 32) {
4022 __ Sra(AT, in_high, 31);
4023 __ Move(out_low, in_low);
4024 if (is_r6) {
4025 __ Selnez(out_high, AT, out_low);
4026 } else {
4027 __ Movz(AT, ZERO, out_low);
4028 __ Move(out_high, AT);
4029 }
4030 } else if (ctz_imm < 63) {
4031 __ Sra(AT, in_high, 31);
4032 __ Move(TMP, in_low);
4033 if (ctz_imm - 32 <= 16) {
4034 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
4035 } else if (is_r2_or_newer) {
4036 __ Ext(out_high, in_high, 0, ctz_imm - 32);
4037 } else {
4038 __ Sll(out_high, in_high, 64 - ctz_imm);
4039 __ Srl(out_high, out_high, 64 - ctz_imm);
4040 }
4041 __ Move(out_low, TMP);
4042 __ Or(TMP, TMP, out_high);
4043 if (is_r6) {
4044 __ Selnez(AT, AT, TMP);
4045 } else {
4046 __ Movz(AT, ZERO, TMP);
4047 }
4048 if (is_r2_or_newer) {
4049 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
4050 } else {
4051 __ Sll(AT, AT, ctz_imm - 32);
4052 __ Or(out_high, out_high, AT);
4053 }
4054 } else {
4055 if (is_r6) {
4056 __ Aui(AT, in_high, 0x8000);
4057 } else {
4058 __ Lui(AT, 0x8000);
4059 __ Xor(AT, AT, in_high);
4060 }
4061 __ Or(AT, AT, in_low);
4062 __ Sltiu(AT, AT, 1);
4063 __ Sll(AT, AT, 31);
4064 __ Move(out_low, in_low);
4065 __ Xor(out_high, in_high, AT);
4066 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08004067 }
4068 }
4069}
4070
4071void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4072 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004073 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004074
4075 LocationSummary* locations = instruction->GetLocations();
4076 Location second = locations->InAt(1);
4077 DCHECK(second.IsConstant());
4078
4079 Register out = locations->Out().AsRegister<Register>();
4080 Register dividend = locations->InAt(0).AsRegister<Register>();
4081 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4082
4083 int64_t magic;
4084 int shift;
4085 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4086
4087 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4088
4089 __ LoadConst32(TMP, magic);
4090 if (isR6) {
4091 __ MuhR6(TMP, dividend, TMP);
4092 } else {
4093 __ MultR2(dividend, TMP);
4094 __ Mfhi(TMP);
4095 }
4096 if (imm > 0 && magic < 0) {
4097 __ Addu(TMP, TMP, dividend);
4098 } else if (imm < 0 && magic > 0) {
4099 __ Subu(TMP, TMP, dividend);
4100 }
4101
4102 if (shift != 0) {
4103 __ Sra(TMP, TMP, shift);
4104 }
4105
4106 if (instruction->IsDiv()) {
4107 __ Sra(out, TMP, 31);
4108 __ Subu(out, TMP, out);
4109 } else {
4110 __ Sra(AT, TMP, 31);
4111 __ Subu(AT, TMP, AT);
4112 __ LoadConst32(TMP, imm);
4113 if (isR6) {
4114 __ MulR6(TMP, AT, TMP);
4115 } else {
4116 __ MulR2(TMP, AT, TMP);
4117 }
4118 __ Subu(out, dividend, TMP);
4119 }
4120}
4121
4122void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4123 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004124 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004125
4126 LocationSummary* locations = instruction->GetLocations();
4127 Register out = locations->Out().AsRegister<Register>();
4128 Location second = locations->InAt(1);
4129
4130 if (second.IsConstant()) {
4131 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4132 if (imm == 0) {
4133 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4134 } else if (imm == 1 || imm == -1) {
4135 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004136 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004137 DivRemByPowerOfTwo(instruction);
4138 } else {
4139 DCHECK(imm <= -2 || imm >= 2);
4140 GenerateDivRemWithAnyConstant(instruction);
4141 }
4142 } else {
4143 Register dividend = locations->InAt(0).AsRegister<Register>();
4144 Register divisor = second.AsRegister<Register>();
4145 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4146 if (instruction->IsDiv()) {
4147 if (isR6) {
4148 __ DivR6(out, dividend, divisor);
4149 } else {
4150 __ DivR2(out, dividend, divisor);
4151 }
4152 } else {
4153 if (isR6) {
4154 __ ModR6(out, dividend, divisor);
4155 } else {
4156 __ ModR2(out, dividend, divisor);
4157 }
4158 }
4159 }
4160}
4161
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004162void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004163 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004164 bool call_long_div = false;
4165 if (type == DataType::Type::kInt64) {
4166 if (div->InputAt(1)->IsConstant()) {
4167 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4168 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4169 } else {
4170 call_long_div = true;
4171 }
4172 }
4173 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004174 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004175 : LocationSummary::kNoCall;
4176
Vladimir Markoca6fff82017-10-03 14:49:14 +01004177 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004178
4179 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004180 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004181 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004182 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004183 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4184 break;
4185
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004186 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004187 if (call_long_div) {
4188 InvokeRuntimeCallingConvention calling_convention;
4189 locations->SetInAt(0, Location::RegisterPairLocation(
4190 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4191 locations->SetInAt(1, Location::RegisterPairLocation(
4192 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4193 locations->SetOut(calling_convention.GetReturnLocation(type));
4194 } else {
4195 locations->SetInAt(0, Location::RequiresRegister());
4196 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4197 locations->SetOut(Location::RequiresRegister());
4198 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004199 break;
4200 }
4201
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004202 case DataType::Type::kFloat32:
4203 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004204 locations->SetInAt(0, Location::RequiresFpuRegister());
4205 locations->SetInAt(1, Location::RequiresFpuRegister());
4206 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4207 break;
4208
4209 default:
4210 LOG(FATAL) << "Unexpected div type " << type;
4211 }
4212}
4213
4214void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004215 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004216 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004217
4218 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004219 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004220 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004221 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004222 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004223 if (locations->InAt(1).IsConstant()) {
4224 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4225 if (imm == 0) {
4226 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4227 } else if (imm == 1 || imm == -1) {
4228 DivRemOneOrMinusOne(instruction);
4229 } else {
4230 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4231 DivRemByPowerOfTwo(instruction);
4232 }
4233 } else {
4234 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4235 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4236 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004237 break;
4238 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004239 case DataType::Type::kFloat32:
4240 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004241 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4242 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4243 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004244 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004245 __ DivS(dst, lhs, rhs);
4246 } else {
4247 __ DivD(dst, lhs, rhs);
4248 }
4249 break;
4250 }
4251 default:
4252 LOG(FATAL) << "Unexpected div type " << type;
4253 }
4254}
4255
4256void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004257 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004258 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004259}
4260
4261void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004262 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004263 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004264 codegen_->AddSlowPath(slow_path);
4265 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004266 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004267
4268 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004269 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004270 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004271 case DataType::Type::kInt8:
4272 case DataType::Type::kUint16:
4273 case DataType::Type::kInt16:
4274 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004275 if (value.IsConstant()) {
4276 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4277 __ B(slow_path->GetEntryLabel());
4278 } else {
4279 // A division by a non-null constant is valid. We don't need to perform
4280 // any check, so simply fall through.
4281 }
4282 } else {
4283 DCHECK(value.IsRegister()) << value;
4284 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4285 }
4286 break;
4287 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004288 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004289 if (value.IsConstant()) {
4290 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4291 __ B(slow_path->GetEntryLabel());
4292 } else {
4293 // A division by a non-null constant is valid. We don't need to perform
4294 // any check, so simply fall through.
4295 }
4296 } else {
4297 DCHECK(value.IsRegisterPair()) << value;
4298 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4299 __ Beqz(TMP, slow_path->GetEntryLabel());
4300 }
4301 break;
4302 }
4303 default:
4304 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4305 }
4306}
4307
4308void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4309 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004310 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004311 locations->SetOut(Location::ConstantLocation(constant));
4312}
4313
4314void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4315 // Will be generated at use site.
4316}
4317
4318void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4319 exit->SetLocations(nullptr);
4320}
4321
4322void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4323}
4324
4325void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4326 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004327 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004328 locations->SetOut(Location::ConstantLocation(constant));
4329}
4330
4331void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4332 // Will be generated at use site.
4333}
4334
4335void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4336 got->SetLocations(nullptr);
4337}
4338
4339void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004340 if (successor->IsExitBlock()) {
4341 DCHECK(got->GetPrevious()->AlwaysThrows());
4342 return; // no code needed
4343 }
4344
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004345 HBasicBlock* block = got->GetBlock();
4346 HInstruction* previous = got->GetPrevious();
4347 HLoopInformation* info = block->GetLoopInformation();
4348
4349 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004350 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4351 __ Lw(AT, SP, kCurrentMethodStackOffset);
4352 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4353 __ Addiu(TMP, TMP, 1);
4354 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4355 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004356 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4357 return;
4358 }
4359 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4360 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4361 }
4362 if (!codegen_->GoesToNextBlock(block, successor)) {
4363 __ B(codegen_->GetLabelOf(successor));
4364 }
4365}
4366
4367void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4368 HandleGoto(got, got->GetSuccessor());
4369}
4370
4371void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4372 try_boundary->SetLocations(nullptr);
4373}
4374
4375void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4376 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4377 if (!successor->IsExitBlock()) {
4378 HandleGoto(try_boundary, successor);
4379 }
4380}
4381
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004382void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4383 LocationSummary* locations) {
4384 Register dst = locations->Out().AsRegister<Register>();
4385 Register lhs = locations->InAt(0).AsRegister<Register>();
4386 Location rhs_location = locations->InAt(1);
4387 Register rhs_reg = ZERO;
4388 int64_t rhs_imm = 0;
4389 bool use_imm = rhs_location.IsConstant();
4390 if (use_imm) {
4391 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4392 } else {
4393 rhs_reg = rhs_location.AsRegister<Register>();
4394 }
4395
4396 switch (cond) {
4397 case kCondEQ:
4398 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004399 if (use_imm && IsInt<16>(-rhs_imm)) {
4400 if (rhs_imm == 0) {
4401 if (cond == kCondEQ) {
4402 __ Sltiu(dst, lhs, 1);
4403 } else {
4404 __ Sltu(dst, ZERO, lhs);
4405 }
4406 } else {
4407 __ Addiu(dst, lhs, -rhs_imm);
4408 if (cond == kCondEQ) {
4409 __ Sltiu(dst, dst, 1);
4410 } else {
4411 __ Sltu(dst, ZERO, dst);
4412 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004413 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004414 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004415 if (use_imm && IsUint<16>(rhs_imm)) {
4416 __ Xori(dst, lhs, rhs_imm);
4417 } else {
4418 if (use_imm) {
4419 rhs_reg = TMP;
4420 __ LoadConst32(rhs_reg, rhs_imm);
4421 }
4422 __ Xor(dst, lhs, rhs_reg);
4423 }
4424 if (cond == kCondEQ) {
4425 __ Sltiu(dst, dst, 1);
4426 } else {
4427 __ Sltu(dst, ZERO, dst);
4428 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004429 }
4430 break;
4431
4432 case kCondLT:
4433 case kCondGE:
4434 if (use_imm && IsInt<16>(rhs_imm)) {
4435 __ Slti(dst, lhs, rhs_imm);
4436 } else {
4437 if (use_imm) {
4438 rhs_reg = TMP;
4439 __ LoadConst32(rhs_reg, rhs_imm);
4440 }
4441 __ Slt(dst, lhs, rhs_reg);
4442 }
4443 if (cond == kCondGE) {
4444 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4445 // only the slt instruction but no sge.
4446 __ Xori(dst, dst, 1);
4447 }
4448 break;
4449
4450 case kCondLE:
4451 case kCondGT:
4452 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4453 // Simulate lhs <= rhs via lhs < rhs + 1.
4454 __ Slti(dst, lhs, rhs_imm + 1);
4455 if (cond == kCondGT) {
4456 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4457 // only the slti instruction but no sgti.
4458 __ Xori(dst, dst, 1);
4459 }
4460 } else {
4461 if (use_imm) {
4462 rhs_reg = TMP;
4463 __ LoadConst32(rhs_reg, rhs_imm);
4464 }
4465 __ Slt(dst, rhs_reg, lhs);
4466 if (cond == kCondLE) {
4467 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4468 // only the slt instruction but no sle.
4469 __ Xori(dst, dst, 1);
4470 }
4471 }
4472 break;
4473
4474 case kCondB:
4475 case kCondAE:
4476 if (use_imm && IsInt<16>(rhs_imm)) {
4477 // Sltiu sign-extends its 16-bit immediate operand before
4478 // the comparison and thus lets us compare directly with
4479 // unsigned values in the ranges [0, 0x7fff] and
4480 // [0xffff8000, 0xffffffff].
4481 __ Sltiu(dst, lhs, rhs_imm);
4482 } else {
4483 if (use_imm) {
4484 rhs_reg = TMP;
4485 __ LoadConst32(rhs_reg, rhs_imm);
4486 }
4487 __ Sltu(dst, lhs, rhs_reg);
4488 }
4489 if (cond == kCondAE) {
4490 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4491 // only the sltu instruction but no sgeu.
4492 __ Xori(dst, dst, 1);
4493 }
4494 break;
4495
4496 case kCondBE:
4497 case kCondA:
4498 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4499 // Simulate lhs <= rhs via lhs < rhs + 1.
4500 // Note that this only works if rhs + 1 does not overflow
4501 // to 0, hence the check above.
4502 // Sltiu sign-extends its 16-bit immediate operand before
4503 // the comparison and thus lets us compare directly with
4504 // unsigned values in the ranges [0, 0x7fff] and
4505 // [0xffff8000, 0xffffffff].
4506 __ Sltiu(dst, lhs, rhs_imm + 1);
4507 if (cond == kCondA) {
4508 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4509 // only the sltiu instruction but no sgtiu.
4510 __ Xori(dst, dst, 1);
4511 }
4512 } else {
4513 if (use_imm) {
4514 rhs_reg = TMP;
4515 __ LoadConst32(rhs_reg, rhs_imm);
4516 }
4517 __ Sltu(dst, rhs_reg, lhs);
4518 if (cond == kCondBE) {
4519 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4520 // only the sltu instruction but no sleu.
4521 __ Xori(dst, dst, 1);
4522 }
4523 }
4524 break;
4525 }
4526}
4527
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004528bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4529 LocationSummary* input_locations,
4530 Register dst) {
4531 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4532 Location rhs_location = input_locations->InAt(1);
4533 Register rhs_reg = ZERO;
4534 int64_t rhs_imm = 0;
4535 bool use_imm = rhs_location.IsConstant();
4536 if (use_imm) {
4537 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4538 } else {
4539 rhs_reg = rhs_location.AsRegister<Register>();
4540 }
4541
4542 switch (cond) {
4543 case kCondEQ:
4544 case kCondNE:
4545 if (use_imm && IsInt<16>(-rhs_imm)) {
4546 __ Addiu(dst, lhs, -rhs_imm);
4547 } else if (use_imm && IsUint<16>(rhs_imm)) {
4548 __ Xori(dst, lhs, rhs_imm);
4549 } else {
4550 if (use_imm) {
4551 rhs_reg = TMP;
4552 __ LoadConst32(rhs_reg, rhs_imm);
4553 }
4554 __ Xor(dst, lhs, rhs_reg);
4555 }
4556 return (cond == kCondEQ);
4557
4558 case kCondLT:
4559 case kCondGE:
4560 if (use_imm && IsInt<16>(rhs_imm)) {
4561 __ Slti(dst, lhs, rhs_imm);
4562 } else {
4563 if (use_imm) {
4564 rhs_reg = TMP;
4565 __ LoadConst32(rhs_reg, rhs_imm);
4566 }
4567 __ Slt(dst, lhs, rhs_reg);
4568 }
4569 return (cond == kCondGE);
4570
4571 case kCondLE:
4572 case kCondGT:
4573 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4574 // Simulate lhs <= rhs via lhs < rhs + 1.
4575 __ Slti(dst, lhs, rhs_imm + 1);
4576 return (cond == kCondGT);
4577 } else {
4578 if (use_imm) {
4579 rhs_reg = TMP;
4580 __ LoadConst32(rhs_reg, rhs_imm);
4581 }
4582 __ Slt(dst, rhs_reg, lhs);
4583 return (cond == kCondLE);
4584 }
4585
4586 case kCondB:
4587 case kCondAE:
4588 if (use_imm && IsInt<16>(rhs_imm)) {
4589 // Sltiu sign-extends its 16-bit immediate operand before
4590 // the comparison and thus lets us compare directly with
4591 // unsigned values in the ranges [0, 0x7fff] and
4592 // [0xffff8000, 0xffffffff].
4593 __ Sltiu(dst, lhs, rhs_imm);
4594 } else {
4595 if (use_imm) {
4596 rhs_reg = TMP;
4597 __ LoadConst32(rhs_reg, rhs_imm);
4598 }
4599 __ Sltu(dst, lhs, rhs_reg);
4600 }
4601 return (cond == kCondAE);
4602
4603 case kCondBE:
4604 case kCondA:
4605 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4606 // Simulate lhs <= rhs via lhs < rhs + 1.
4607 // Note that this only works if rhs + 1 does not overflow
4608 // to 0, hence the check above.
4609 // Sltiu sign-extends its 16-bit immediate operand before
4610 // the comparison and thus lets us compare directly with
4611 // unsigned values in the ranges [0, 0x7fff] and
4612 // [0xffff8000, 0xffffffff].
4613 __ Sltiu(dst, lhs, rhs_imm + 1);
4614 return (cond == kCondA);
4615 } else {
4616 if (use_imm) {
4617 rhs_reg = TMP;
4618 __ LoadConst32(rhs_reg, rhs_imm);
4619 }
4620 __ Sltu(dst, rhs_reg, lhs);
4621 return (cond == kCondBE);
4622 }
4623 }
4624}
4625
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004626void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4627 LocationSummary* locations,
4628 MipsLabel* label) {
4629 Register lhs = locations->InAt(0).AsRegister<Register>();
4630 Location rhs_location = locations->InAt(1);
4631 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004632 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004633 bool use_imm = rhs_location.IsConstant();
4634 if (use_imm) {
4635 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4636 } else {
4637 rhs_reg = rhs_location.AsRegister<Register>();
4638 }
4639
4640 if (use_imm && rhs_imm == 0) {
4641 switch (cond) {
4642 case kCondEQ:
4643 case kCondBE: // <= 0 if zero
4644 __ Beqz(lhs, label);
4645 break;
4646 case kCondNE:
4647 case kCondA: // > 0 if non-zero
4648 __ Bnez(lhs, label);
4649 break;
4650 case kCondLT:
4651 __ Bltz(lhs, label);
4652 break;
4653 case kCondGE:
4654 __ Bgez(lhs, label);
4655 break;
4656 case kCondLE:
4657 __ Blez(lhs, label);
4658 break;
4659 case kCondGT:
4660 __ Bgtz(lhs, label);
4661 break;
4662 case kCondB: // always false
4663 break;
4664 case kCondAE: // always true
4665 __ B(label);
4666 break;
4667 }
4668 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004669 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4670 if (isR6 || !use_imm) {
4671 if (use_imm) {
4672 rhs_reg = TMP;
4673 __ LoadConst32(rhs_reg, rhs_imm);
4674 }
4675 switch (cond) {
4676 case kCondEQ:
4677 __ Beq(lhs, rhs_reg, label);
4678 break;
4679 case kCondNE:
4680 __ Bne(lhs, rhs_reg, label);
4681 break;
4682 case kCondLT:
4683 __ Blt(lhs, rhs_reg, label);
4684 break;
4685 case kCondGE:
4686 __ Bge(lhs, rhs_reg, label);
4687 break;
4688 case kCondLE:
4689 __ Bge(rhs_reg, lhs, label);
4690 break;
4691 case kCondGT:
4692 __ Blt(rhs_reg, lhs, label);
4693 break;
4694 case kCondB:
4695 __ Bltu(lhs, rhs_reg, label);
4696 break;
4697 case kCondAE:
4698 __ Bgeu(lhs, rhs_reg, label);
4699 break;
4700 case kCondBE:
4701 __ Bgeu(rhs_reg, lhs, label);
4702 break;
4703 case kCondA:
4704 __ Bltu(rhs_reg, lhs, label);
4705 break;
4706 }
4707 } else {
4708 // Special cases for more efficient comparison with constants on R2.
4709 switch (cond) {
4710 case kCondEQ:
4711 __ LoadConst32(TMP, rhs_imm);
4712 __ Beq(lhs, TMP, label);
4713 break;
4714 case kCondNE:
4715 __ LoadConst32(TMP, rhs_imm);
4716 __ Bne(lhs, TMP, label);
4717 break;
4718 case kCondLT:
4719 if (IsInt<16>(rhs_imm)) {
4720 __ Slti(TMP, lhs, rhs_imm);
4721 __ Bnez(TMP, label);
4722 } else {
4723 __ LoadConst32(TMP, rhs_imm);
4724 __ Blt(lhs, TMP, label);
4725 }
4726 break;
4727 case kCondGE:
4728 if (IsInt<16>(rhs_imm)) {
4729 __ Slti(TMP, lhs, rhs_imm);
4730 __ Beqz(TMP, label);
4731 } else {
4732 __ LoadConst32(TMP, rhs_imm);
4733 __ Bge(lhs, TMP, label);
4734 }
4735 break;
4736 case kCondLE:
4737 if (IsInt<16>(rhs_imm + 1)) {
4738 // Simulate lhs <= rhs via lhs < rhs + 1.
4739 __ Slti(TMP, lhs, rhs_imm + 1);
4740 __ Bnez(TMP, label);
4741 } else {
4742 __ LoadConst32(TMP, rhs_imm);
4743 __ Bge(TMP, lhs, label);
4744 }
4745 break;
4746 case kCondGT:
4747 if (IsInt<16>(rhs_imm + 1)) {
4748 // Simulate lhs > rhs via !(lhs < rhs + 1).
4749 __ Slti(TMP, lhs, rhs_imm + 1);
4750 __ Beqz(TMP, label);
4751 } else {
4752 __ LoadConst32(TMP, rhs_imm);
4753 __ Blt(TMP, lhs, label);
4754 }
4755 break;
4756 case kCondB:
4757 if (IsInt<16>(rhs_imm)) {
4758 __ Sltiu(TMP, lhs, rhs_imm);
4759 __ Bnez(TMP, label);
4760 } else {
4761 __ LoadConst32(TMP, rhs_imm);
4762 __ Bltu(lhs, TMP, label);
4763 }
4764 break;
4765 case kCondAE:
4766 if (IsInt<16>(rhs_imm)) {
4767 __ Sltiu(TMP, lhs, rhs_imm);
4768 __ Beqz(TMP, label);
4769 } else {
4770 __ LoadConst32(TMP, rhs_imm);
4771 __ Bgeu(lhs, TMP, label);
4772 }
4773 break;
4774 case kCondBE:
4775 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4776 // Simulate lhs <= rhs via lhs < rhs + 1.
4777 // Note that this only works if rhs + 1 does not overflow
4778 // to 0, hence the check above.
4779 __ Sltiu(TMP, lhs, rhs_imm + 1);
4780 __ Bnez(TMP, label);
4781 } else {
4782 __ LoadConst32(TMP, rhs_imm);
4783 __ Bgeu(TMP, lhs, label);
4784 }
4785 break;
4786 case kCondA:
4787 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4788 // Simulate lhs > rhs via !(lhs < rhs + 1).
4789 // Note that this only works if rhs + 1 does not overflow
4790 // to 0, hence the check above.
4791 __ Sltiu(TMP, lhs, rhs_imm + 1);
4792 __ Beqz(TMP, label);
4793 } else {
4794 __ LoadConst32(TMP, rhs_imm);
4795 __ Bltu(TMP, lhs, label);
4796 }
4797 break;
4798 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004799 }
4800 }
4801}
4802
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004803void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4804 LocationSummary* locations) {
4805 Register dst = locations->Out().AsRegister<Register>();
4806 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4807 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4808 Location rhs_location = locations->InAt(1);
4809 Register rhs_high = ZERO;
4810 Register rhs_low = ZERO;
4811 int64_t imm = 0;
4812 uint32_t imm_high = 0;
4813 uint32_t imm_low = 0;
4814 bool use_imm = rhs_location.IsConstant();
4815 if (use_imm) {
4816 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4817 imm_high = High32Bits(imm);
4818 imm_low = Low32Bits(imm);
4819 } else {
4820 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4821 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4822 }
4823 if (use_imm && imm == 0) {
4824 switch (cond) {
4825 case kCondEQ:
4826 case kCondBE: // <= 0 if zero
4827 __ Or(dst, lhs_high, lhs_low);
4828 __ Sltiu(dst, dst, 1);
4829 break;
4830 case kCondNE:
4831 case kCondA: // > 0 if non-zero
4832 __ Or(dst, lhs_high, lhs_low);
4833 __ Sltu(dst, ZERO, dst);
4834 break;
4835 case kCondLT:
4836 __ Slt(dst, lhs_high, ZERO);
4837 break;
4838 case kCondGE:
4839 __ Slt(dst, lhs_high, ZERO);
4840 __ Xori(dst, dst, 1);
4841 break;
4842 case kCondLE:
4843 __ Or(TMP, lhs_high, lhs_low);
4844 __ Sra(AT, lhs_high, 31);
4845 __ Sltu(dst, AT, TMP);
4846 __ Xori(dst, dst, 1);
4847 break;
4848 case kCondGT:
4849 __ Or(TMP, lhs_high, lhs_low);
4850 __ Sra(AT, lhs_high, 31);
4851 __ Sltu(dst, AT, TMP);
4852 break;
4853 case kCondB: // always false
4854 __ Andi(dst, dst, 0);
4855 break;
4856 case kCondAE: // always true
4857 __ Ori(dst, ZERO, 1);
4858 break;
4859 }
4860 } else if (use_imm) {
4861 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4862 switch (cond) {
4863 case kCondEQ:
4864 __ LoadConst32(TMP, imm_high);
4865 __ Xor(TMP, TMP, lhs_high);
4866 __ LoadConst32(AT, imm_low);
4867 __ Xor(AT, AT, lhs_low);
4868 __ Or(dst, TMP, AT);
4869 __ Sltiu(dst, dst, 1);
4870 break;
4871 case kCondNE:
4872 __ LoadConst32(TMP, imm_high);
4873 __ Xor(TMP, TMP, lhs_high);
4874 __ LoadConst32(AT, imm_low);
4875 __ Xor(AT, AT, lhs_low);
4876 __ Or(dst, TMP, AT);
4877 __ Sltu(dst, ZERO, dst);
4878 break;
4879 case kCondLT:
4880 case kCondGE:
4881 if (dst == lhs_low) {
4882 __ LoadConst32(TMP, imm_low);
4883 __ Sltu(dst, lhs_low, TMP);
4884 }
4885 __ LoadConst32(TMP, imm_high);
4886 __ Slt(AT, lhs_high, TMP);
4887 __ Slt(TMP, TMP, lhs_high);
4888 if (dst != lhs_low) {
4889 __ LoadConst32(dst, imm_low);
4890 __ Sltu(dst, lhs_low, dst);
4891 }
4892 __ Slt(dst, TMP, dst);
4893 __ Or(dst, dst, AT);
4894 if (cond == kCondGE) {
4895 __ Xori(dst, dst, 1);
4896 }
4897 break;
4898 case kCondGT:
4899 case kCondLE:
4900 if (dst == lhs_low) {
4901 __ LoadConst32(TMP, imm_low);
4902 __ Sltu(dst, TMP, lhs_low);
4903 }
4904 __ LoadConst32(TMP, imm_high);
4905 __ Slt(AT, TMP, lhs_high);
4906 __ Slt(TMP, lhs_high, TMP);
4907 if (dst != lhs_low) {
4908 __ LoadConst32(dst, imm_low);
4909 __ Sltu(dst, dst, lhs_low);
4910 }
4911 __ Slt(dst, TMP, dst);
4912 __ Or(dst, dst, AT);
4913 if (cond == kCondLE) {
4914 __ Xori(dst, dst, 1);
4915 }
4916 break;
4917 case kCondB:
4918 case kCondAE:
4919 if (dst == lhs_low) {
4920 __ LoadConst32(TMP, imm_low);
4921 __ Sltu(dst, lhs_low, TMP);
4922 }
4923 __ LoadConst32(TMP, imm_high);
4924 __ Sltu(AT, lhs_high, TMP);
4925 __ Sltu(TMP, TMP, lhs_high);
4926 if (dst != lhs_low) {
4927 __ LoadConst32(dst, imm_low);
4928 __ Sltu(dst, lhs_low, dst);
4929 }
4930 __ Slt(dst, TMP, dst);
4931 __ Or(dst, dst, AT);
4932 if (cond == kCondAE) {
4933 __ Xori(dst, dst, 1);
4934 }
4935 break;
4936 case kCondA:
4937 case kCondBE:
4938 if (dst == lhs_low) {
4939 __ LoadConst32(TMP, imm_low);
4940 __ Sltu(dst, TMP, lhs_low);
4941 }
4942 __ LoadConst32(TMP, imm_high);
4943 __ Sltu(AT, TMP, lhs_high);
4944 __ Sltu(TMP, lhs_high, TMP);
4945 if (dst != lhs_low) {
4946 __ LoadConst32(dst, imm_low);
4947 __ Sltu(dst, dst, lhs_low);
4948 }
4949 __ Slt(dst, TMP, dst);
4950 __ Or(dst, dst, AT);
4951 if (cond == kCondBE) {
4952 __ Xori(dst, dst, 1);
4953 }
4954 break;
4955 }
4956 } else {
4957 switch (cond) {
4958 case kCondEQ:
4959 __ Xor(TMP, lhs_high, rhs_high);
4960 __ Xor(AT, lhs_low, rhs_low);
4961 __ Or(dst, TMP, AT);
4962 __ Sltiu(dst, dst, 1);
4963 break;
4964 case kCondNE:
4965 __ Xor(TMP, lhs_high, rhs_high);
4966 __ Xor(AT, lhs_low, rhs_low);
4967 __ Or(dst, TMP, AT);
4968 __ Sltu(dst, ZERO, dst);
4969 break;
4970 case kCondLT:
4971 case kCondGE:
4972 __ Slt(TMP, rhs_high, lhs_high);
4973 __ Sltu(AT, lhs_low, rhs_low);
4974 __ Slt(TMP, TMP, AT);
4975 __ Slt(AT, lhs_high, rhs_high);
4976 __ Or(dst, AT, TMP);
4977 if (cond == kCondGE) {
4978 __ Xori(dst, dst, 1);
4979 }
4980 break;
4981 case kCondGT:
4982 case kCondLE:
4983 __ Slt(TMP, lhs_high, rhs_high);
4984 __ Sltu(AT, rhs_low, lhs_low);
4985 __ Slt(TMP, TMP, AT);
4986 __ Slt(AT, rhs_high, lhs_high);
4987 __ Or(dst, AT, TMP);
4988 if (cond == kCondLE) {
4989 __ Xori(dst, dst, 1);
4990 }
4991 break;
4992 case kCondB:
4993 case kCondAE:
4994 __ Sltu(TMP, rhs_high, lhs_high);
4995 __ Sltu(AT, lhs_low, rhs_low);
4996 __ Slt(TMP, TMP, AT);
4997 __ Sltu(AT, lhs_high, rhs_high);
4998 __ Or(dst, AT, TMP);
4999 if (cond == kCondAE) {
5000 __ Xori(dst, dst, 1);
5001 }
5002 break;
5003 case kCondA:
5004 case kCondBE:
5005 __ Sltu(TMP, lhs_high, rhs_high);
5006 __ Sltu(AT, rhs_low, lhs_low);
5007 __ Slt(TMP, TMP, AT);
5008 __ Sltu(AT, rhs_high, lhs_high);
5009 __ Or(dst, AT, TMP);
5010 if (cond == kCondBE) {
5011 __ Xori(dst, dst, 1);
5012 }
5013 break;
5014 }
5015 }
5016}
5017
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005018void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
5019 LocationSummary* locations,
5020 MipsLabel* label) {
5021 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
5022 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
5023 Location rhs_location = locations->InAt(1);
5024 Register rhs_high = ZERO;
5025 Register rhs_low = ZERO;
5026 int64_t imm = 0;
5027 uint32_t imm_high = 0;
5028 uint32_t imm_low = 0;
5029 bool use_imm = rhs_location.IsConstant();
5030 if (use_imm) {
5031 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
5032 imm_high = High32Bits(imm);
5033 imm_low = Low32Bits(imm);
5034 } else {
5035 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
5036 rhs_low = rhs_location.AsRegisterPairLow<Register>();
5037 }
5038
5039 if (use_imm && imm == 0) {
5040 switch (cond) {
5041 case kCondEQ:
5042 case kCondBE: // <= 0 if zero
5043 __ Or(TMP, lhs_high, lhs_low);
5044 __ Beqz(TMP, label);
5045 break;
5046 case kCondNE:
5047 case kCondA: // > 0 if non-zero
5048 __ Or(TMP, lhs_high, lhs_low);
5049 __ Bnez(TMP, label);
5050 break;
5051 case kCondLT:
5052 __ Bltz(lhs_high, label);
5053 break;
5054 case kCondGE:
5055 __ Bgez(lhs_high, label);
5056 break;
5057 case kCondLE:
5058 __ Or(TMP, lhs_high, lhs_low);
5059 __ Sra(AT, lhs_high, 31);
5060 __ Bgeu(AT, TMP, label);
5061 break;
5062 case kCondGT:
5063 __ Or(TMP, lhs_high, lhs_low);
5064 __ Sra(AT, lhs_high, 31);
5065 __ Bltu(AT, TMP, label);
5066 break;
5067 case kCondB: // always false
5068 break;
5069 case kCondAE: // always true
5070 __ B(label);
5071 break;
5072 }
5073 } else if (use_imm) {
5074 // TODO: more efficient comparison with constants without loading them into TMP/AT.
5075 switch (cond) {
5076 case kCondEQ:
5077 __ LoadConst32(TMP, imm_high);
5078 __ Xor(TMP, TMP, lhs_high);
5079 __ LoadConst32(AT, imm_low);
5080 __ Xor(AT, AT, lhs_low);
5081 __ Or(TMP, TMP, AT);
5082 __ Beqz(TMP, label);
5083 break;
5084 case kCondNE:
5085 __ LoadConst32(TMP, imm_high);
5086 __ Xor(TMP, TMP, lhs_high);
5087 __ LoadConst32(AT, imm_low);
5088 __ Xor(AT, AT, lhs_low);
5089 __ Or(TMP, TMP, AT);
5090 __ Bnez(TMP, label);
5091 break;
5092 case kCondLT:
5093 __ LoadConst32(TMP, imm_high);
5094 __ Blt(lhs_high, TMP, label);
5095 __ Slt(TMP, TMP, lhs_high);
5096 __ LoadConst32(AT, imm_low);
5097 __ Sltu(AT, lhs_low, AT);
5098 __ Blt(TMP, AT, label);
5099 break;
5100 case kCondGE:
5101 __ LoadConst32(TMP, imm_high);
5102 __ Blt(TMP, lhs_high, label);
5103 __ Slt(TMP, lhs_high, TMP);
5104 __ LoadConst32(AT, imm_low);
5105 __ Sltu(AT, lhs_low, AT);
5106 __ Or(TMP, TMP, AT);
5107 __ Beqz(TMP, label);
5108 break;
5109 case kCondLE:
5110 __ LoadConst32(TMP, imm_high);
5111 __ Blt(lhs_high, TMP, label);
5112 __ Slt(TMP, TMP, lhs_high);
5113 __ LoadConst32(AT, imm_low);
5114 __ Sltu(AT, AT, lhs_low);
5115 __ Or(TMP, TMP, AT);
5116 __ Beqz(TMP, label);
5117 break;
5118 case kCondGT:
5119 __ LoadConst32(TMP, imm_high);
5120 __ Blt(TMP, lhs_high, label);
5121 __ Slt(TMP, lhs_high, TMP);
5122 __ LoadConst32(AT, imm_low);
5123 __ Sltu(AT, AT, lhs_low);
5124 __ Blt(TMP, AT, label);
5125 break;
5126 case kCondB:
5127 __ LoadConst32(TMP, imm_high);
5128 __ Bltu(lhs_high, TMP, label);
5129 __ Sltu(TMP, TMP, lhs_high);
5130 __ LoadConst32(AT, imm_low);
5131 __ Sltu(AT, lhs_low, AT);
5132 __ Blt(TMP, AT, label);
5133 break;
5134 case kCondAE:
5135 __ LoadConst32(TMP, imm_high);
5136 __ Bltu(TMP, lhs_high, label);
5137 __ Sltu(TMP, lhs_high, TMP);
5138 __ LoadConst32(AT, imm_low);
5139 __ Sltu(AT, lhs_low, AT);
5140 __ Or(TMP, TMP, AT);
5141 __ Beqz(TMP, label);
5142 break;
5143 case kCondBE:
5144 __ LoadConst32(TMP, imm_high);
5145 __ Bltu(lhs_high, TMP, label);
5146 __ Sltu(TMP, TMP, lhs_high);
5147 __ LoadConst32(AT, imm_low);
5148 __ Sltu(AT, AT, lhs_low);
5149 __ Or(TMP, TMP, AT);
5150 __ Beqz(TMP, label);
5151 break;
5152 case kCondA:
5153 __ LoadConst32(TMP, imm_high);
5154 __ Bltu(TMP, lhs_high, label);
5155 __ Sltu(TMP, lhs_high, TMP);
5156 __ LoadConst32(AT, imm_low);
5157 __ Sltu(AT, AT, lhs_low);
5158 __ Blt(TMP, AT, label);
5159 break;
5160 }
5161 } else {
5162 switch (cond) {
5163 case kCondEQ:
5164 __ Xor(TMP, lhs_high, rhs_high);
5165 __ Xor(AT, lhs_low, rhs_low);
5166 __ Or(TMP, TMP, AT);
5167 __ Beqz(TMP, label);
5168 break;
5169 case kCondNE:
5170 __ Xor(TMP, lhs_high, rhs_high);
5171 __ Xor(AT, lhs_low, rhs_low);
5172 __ Or(TMP, TMP, AT);
5173 __ Bnez(TMP, label);
5174 break;
5175 case kCondLT:
5176 __ Blt(lhs_high, rhs_high, label);
5177 __ Slt(TMP, rhs_high, lhs_high);
5178 __ Sltu(AT, lhs_low, rhs_low);
5179 __ Blt(TMP, AT, label);
5180 break;
5181 case kCondGE:
5182 __ Blt(rhs_high, lhs_high, label);
5183 __ Slt(TMP, lhs_high, rhs_high);
5184 __ Sltu(AT, lhs_low, rhs_low);
5185 __ Or(TMP, TMP, AT);
5186 __ Beqz(TMP, label);
5187 break;
5188 case kCondLE:
5189 __ Blt(lhs_high, rhs_high, label);
5190 __ Slt(TMP, rhs_high, lhs_high);
5191 __ Sltu(AT, rhs_low, lhs_low);
5192 __ Or(TMP, TMP, AT);
5193 __ Beqz(TMP, label);
5194 break;
5195 case kCondGT:
5196 __ Blt(rhs_high, lhs_high, label);
5197 __ Slt(TMP, lhs_high, rhs_high);
5198 __ Sltu(AT, rhs_low, lhs_low);
5199 __ Blt(TMP, AT, label);
5200 break;
5201 case kCondB:
5202 __ Bltu(lhs_high, rhs_high, label);
5203 __ Sltu(TMP, rhs_high, lhs_high);
5204 __ Sltu(AT, lhs_low, rhs_low);
5205 __ Blt(TMP, AT, label);
5206 break;
5207 case kCondAE:
5208 __ Bltu(rhs_high, lhs_high, label);
5209 __ Sltu(TMP, lhs_high, rhs_high);
5210 __ Sltu(AT, lhs_low, rhs_low);
5211 __ Or(TMP, TMP, AT);
5212 __ Beqz(TMP, label);
5213 break;
5214 case kCondBE:
5215 __ Bltu(lhs_high, rhs_high, label);
5216 __ Sltu(TMP, rhs_high, lhs_high);
5217 __ Sltu(AT, rhs_low, lhs_low);
5218 __ Or(TMP, TMP, AT);
5219 __ Beqz(TMP, label);
5220 break;
5221 case kCondA:
5222 __ Bltu(rhs_high, lhs_high, label);
5223 __ Sltu(TMP, lhs_high, rhs_high);
5224 __ Sltu(AT, rhs_low, lhs_low);
5225 __ Blt(TMP, AT, label);
5226 break;
5227 }
5228 }
5229}
5230
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005231void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5232 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005233 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005234 LocationSummary* locations) {
5235 Register dst = locations->Out().AsRegister<Register>();
5236 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5237 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5238 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005239 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005240 if (isR6) {
5241 switch (cond) {
5242 case kCondEQ:
5243 __ CmpEqS(FTMP, lhs, rhs);
5244 __ Mfc1(dst, FTMP);
5245 __ Andi(dst, dst, 1);
5246 break;
5247 case kCondNE:
5248 __ CmpEqS(FTMP, lhs, rhs);
5249 __ Mfc1(dst, FTMP);
5250 __ Addiu(dst, dst, 1);
5251 break;
5252 case kCondLT:
5253 if (gt_bias) {
5254 __ CmpLtS(FTMP, lhs, rhs);
5255 } else {
5256 __ CmpUltS(FTMP, lhs, rhs);
5257 }
5258 __ Mfc1(dst, FTMP);
5259 __ Andi(dst, dst, 1);
5260 break;
5261 case kCondLE:
5262 if (gt_bias) {
5263 __ CmpLeS(FTMP, lhs, rhs);
5264 } else {
5265 __ CmpUleS(FTMP, lhs, rhs);
5266 }
5267 __ Mfc1(dst, FTMP);
5268 __ Andi(dst, dst, 1);
5269 break;
5270 case kCondGT:
5271 if (gt_bias) {
5272 __ CmpUltS(FTMP, rhs, lhs);
5273 } else {
5274 __ CmpLtS(FTMP, rhs, lhs);
5275 }
5276 __ Mfc1(dst, FTMP);
5277 __ Andi(dst, dst, 1);
5278 break;
5279 case kCondGE:
5280 if (gt_bias) {
5281 __ CmpUleS(FTMP, rhs, lhs);
5282 } else {
5283 __ CmpLeS(FTMP, rhs, lhs);
5284 }
5285 __ Mfc1(dst, FTMP);
5286 __ Andi(dst, dst, 1);
5287 break;
5288 default:
5289 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5290 UNREACHABLE();
5291 }
5292 } else {
5293 switch (cond) {
5294 case kCondEQ:
5295 __ CeqS(0, lhs, rhs);
5296 __ LoadConst32(dst, 1);
5297 __ Movf(dst, ZERO, 0);
5298 break;
5299 case kCondNE:
5300 __ CeqS(0, lhs, rhs);
5301 __ LoadConst32(dst, 1);
5302 __ Movt(dst, ZERO, 0);
5303 break;
5304 case kCondLT:
5305 if (gt_bias) {
5306 __ ColtS(0, lhs, rhs);
5307 } else {
5308 __ CultS(0, lhs, rhs);
5309 }
5310 __ LoadConst32(dst, 1);
5311 __ Movf(dst, ZERO, 0);
5312 break;
5313 case kCondLE:
5314 if (gt_bias) {
5315 __ ColeS(0, lhs, rhs);
5316 } else {
5317 __ CuleS(0, lhs, rhs);
5318 }
5319 __ LoadConst32(dst, 1);
5320 __ Movf(dst, ZERO, 0);
5321 break;
5322 case kCondGT:
5323 if (gt_bias) {
5324 __ CultS(0, rhs, lhs);
5325 } else {
5326 __ ColtS(0, rhs, lhs);
5327 }
5328 __ LoadConst32(dst, 1);
5329 __ Movf(dst, ZERO, 0);
5330 break;
5331 case kCondGE:
5332 if (gt_bias) {
5333 __ CuleS(0, rhs, lhs);
5334 } else {
5335 __ ColeS(0, rhs, lhs);
5336 }
5337 __ LoadConst32(dst, 1);
5338 __ Movf(dst, ZERO, 0);
5339 break;
5340 default:
5341 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5342 UNREACHABLE();
5343 }
5344 }
5345 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005346 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005347 if (isR6) {
5348 switch (cond) {
5349 case kCondEQ:
5350 __ CmpEqD(FTMP, lhs, rhs);
5351 __ Mfc1(dst, FTMP);
5352 __ Andi(dst, dst, 1);
5353 break;
5354 case kCondNE:
5355 __ CmpEqD(FTMP, lhs, rhs);
5356 __ Mfc1(dst, FTMP);
5357 __ Addiu(dst, dst, 1);
5358 break;
5359 case kCondLT:
5360 if (gt_bias) {
5361 __ CmpLtD(FTMP, lhs, rhs);
5362 } else {
5363 __ CmpUltD(FTMP, lhs, rhs);
5364 }
5365 __ Mfc1(dst, FTMP);
5366 __ Andi(dst, dst, 1);
5367 break;
5368 case kCondLE:
5369 if (gt_bias) {
5370 __ CmpLeD(FTMP, lhs, rhs);
5371 } else {
5372 __ CmpUleD(FTMP, lhs, rhs);
5373 }
5374 __ Mfc1(dst, FTMP);
5375 __ Andi(dst, dst, 1);
5376 break;
5377 case kCondGT:
5378 if (gt_bias) {
5379 __ CmpUltD(FTMP, rhs, lhs);
5380 } else {
5381 __ CmpLtD(FTMP, rhs, lhs);
5382 }
5383 __ Mfc1(dst, FTMP);
5384 __ Andi(dst, dst, 1);
5385 break;
5386 case kCondGE:
5387 if (gt_bias) {
5388 __ CmpUleD(FTMP, rhs, lhs);
5389 } else {
5390 __ CmpLeD(FTMP, rhs, lhs);
5391 }
5392 __ Mfc1(dst, FTMP);
5393 __ Andi(dst, dst, 1);
5394 break;
5395 default:
5396 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5397 UNREACHABLE();
5398 }
5399 } else {
5400 switch (cond) {
5401 case kCondEQ:
5402 __ CeqD(0, lhs, rhs);
5403 __ LoadConst32(dst, 1);
5404 __ Movf(dst, ZERO, 0);
5405 break;
5406 case kCondNE:
5407 __ CeqD(0, lhs, rhs);
5408 __ LoadConst32(dst, 1);
5409 __ Movt(dst, ZERO, 0);
5410 break;
5411 case kCondLT:
5412 if (gt_bias) {
5413 __ ColtD(0, lhs, rhs);
5414 } else {
5415 __ CultD(0, lhs, rhs);
5416 }
5417 __ LoadConst32(dst, 1);
5418 __ Movf(dst, ZERO, 0);
5419 break;
5420 case kCondLE:
5421 if (gt_bias) {
5422 __ ColeD(0, lhs, rhs);
5423 } else {
5424 __ CuleD(0, lhs, rhs);
5425 }
5426 __ LoadConst32(dst, 1);
5427 __ Movf(dst, ZERO, 0);
5428 break;
5429 case kCondGT:
5430 if (gt_bias) {
5431 __ CultD(0, rhs, lhs);
5432 } else {
5433 __ ColtD(0, rhs, lhs);
5434 }
5435 __ LoadConst32(dst, 1);
5436 __ Movf(dst, ZERO, 0);
5437 break;
5438 case kCondGE:
5439 if (gt_bias) {
5440 __ CuleD(0, rhs, lhs);
5441 } else {
5442 __ ColeD(0, rhs, lhs);
5443 }
5444 __ LoadConst32(dst, 1);
5445 __ Movf(dst, ZERO, 0);
5446 break;
5447 default:
5448 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5449 UNREACHABLE();
5450 }
5451 }
5452 }
5453}
5454
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005455bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5456 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005457 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005458 LocationSummary* input_locations,
5459 int cc) {
5460 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5461 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5462 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005463 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005464 switch (cond) {
5465 case kCondEQ:
5466 __ CeqS(cc, lhs, rhs);
5467 return false;
5468 case kCondNE:
5469 __ CeqS(cc, lhs, rhs);
5470 return true;
5471 case kCondLT:
5472 if (gt_bias) {
5473 __ ColtS(cc, lhs, rhs);
5474 } else {
5475 __ CultS(cc, lhs, rhs);
5476 }
5477 return false;
5478 case kCondLE:
5479 if (gt_bias) {
5480 __ ColeS(cc, lhs, rhs);
5481 } else {
5482 __ CuleS(cc, lhs, rhs);
5483 }
5484 return false;
5485 case kCondGT:
5486 if (gt_bias) {
5487 __ CultS(cc, rhs, lhs);
5488 } else {
5489 __ ColtS(cc, rhs, lhs);
5490 }
5491 return false;
5492 case kCondGE:
5493 if (gt_bias) {
5494 __ CuleS(cc, rhs, lhs);
5495 } else {
5496 __ ColeS(cc, rhs, lhs);
5497 }
5498 return false;
5499 default:
5500 LOG(FATAL) << "Unexpected non-floating-point condition";
5501 UNREACHABLE();
5502 }
5503 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005504 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005505 switch (cond) {
5506 case kCondEQ:
5507 __ CeqD(cc, lhs, rhs);
5508 return false;
5509 case kCondNE:
5510 __ CeqD(cc, lhs, rhs);
5511 return true;
5512 case kCondLT:
5513 if (gt_bias) {
5514 __ ColtD(cc, lhs, rhs);
5515 } else {
5516 __ CultD(cc, lhs, rhs);
5517 }
5518 return false;
5519 case kCondLE:
5520 if (gt_bias) {
5521 __ ColeD(cc, lhs, rhs);
5522 } else {
5523 __ CuleD(cc, lhs, rhs);
5524 }
5525 return false;
5526 case kCondGT:
5527 if (gt_bias) {
5528 __ CultD(cc, rhs, lhs);
5529 } else {
5530 __ ColtD(cc, rhs, lhs);
5531 }
5532 return false;
5533 case kCondGE:
5534 if (gt_bias) {
5535 __ CuleD(cc, rhs, lhs);
5536 } else {
5537 __ ColeD(cc, rhs, lhs);
5538 }
5539 return false;
5540 default:
5541 LOG(FATAL) << "Unexpected non-floating-point condition";
5542 UNREACHABLE();
5543 }
5544 }
5545}
5546
5547bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5548 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005549 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005550 LocationSummary* input_locations,
5551 FRegister dst) {
5552 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5553 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5554 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005555 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005556 switch (cond) {
5557 case kCondEQ:
5558 __ CmpEqS(dst, lhs, rhs);
5559 return false;
5560 case kCondNE:
5561 __ CmpEqS(dst, lhs, rhs);
5562 return true;
5563 case kCondLT:
5564 if (gt_bias) {
5565 __ CmpLtS(dst, lhs, rhs);
5566 } else {
5567 __ CmpUltS(dst, lhs, rhs);
5568 }
5569 return false;
5570 case kCondLE:
5571 if (gt_bias) {
5572 __ CmpLeS(dst, lhs, rhs);
5573 } else {
5574 __ CmpUleS(dst, lhs, rhs);
5575 }
5576 return false;
5577 case kCondGT:
5578 if (gt_bias) {
5579 __ CmpUltS(dst, rhs, lhs);
5580 } else {
5581 __ CmpLtS(dst, rhs, lhs);
5582 }
5583 return false;
5584 case kCondGE:
5585 if (gt_bias) {
5586 __ CmpUleS(dst, rhs, lhs);
5587 } else {
5588 __ CmpLeS(dst, rhs, lhs);
5589 }
5590 return false;
5591 default:
5592 LOG(FATAL) << "Unexpected non-floating-point condition";
5593 UNREACHABLE();
5594 }
5595 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005596 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005597 switch (cond) {
5598 case kCondEQ:
5599 __ CmpEqD(dst, lhs, rhs);
5600 return false;
5601 case kCondNE:
5602 __ CmpEqD(dst, lhs, rhs);
5603 return true;
5604 case kCondLT:
5605 if (gt_bias) {
5606 __ CmpLtD(dst, lhs, rhs);
5607 } else {
5608 __ CmpUltD(dst, lhs, rhs);
5609 }
5610 return false;
5611 case kCondLE:
5612 if (gt_bias) {
5613 __ CmpLeD(dst, lhs, rhs);
5614 } else {
5615 __ CmpUleD(dst, lhs, rhs);
5616 }
5617 return false;
5618 case kCondGT:
5619 if (gt_bias) {
5620 __ CmpUltD(dst, rhs, lhs);
5621 } else {
5622 __ CmpLtD(dst, rhs, lhs);
5623 }
5624 return false;
5625 case kCondGE:
5626 if (gt_bias) {
5627 __ CmpUleD(dst, rhs, lhs);
5628 } else {
5629 __ CmpLeD(dst, rhs, lhs);
5630 }
5631 return false;
5632 default:
5633 LOG(FATAL) << "Unexpected non-floating-point condition";
5634 UNREACHABLE();
5635 }
5636 }
5637}
5638
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005639void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5640 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005641 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005642 LocationSummary* locations,
5643 MipsLabel* label) {
5644 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5645 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5646 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005647 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005648 if (isR6) {
5649 switch (cond) {
5650 case kCondEQ:
5651 __ CmpEqS(FTMP, lhs, rhs);
5652 __ Bc1nez(FTMP, label);
5653 break;
5654 case kCondNE:
5655 __ CmpEqS(FTMP, lhs, rhs);
5656 __ Bc1eqz(FTMP, label);
5657 break;
5658 case kCondLT:
5659 if (gt_bias) {
5660 __ CmpLtS(FTMP, lhs, rhs);
5661 } else {
5662 __ CmpUltS(FTMP, lhs, rhs);
5663 }
5664 __ Bc1nez(FTMP, label);
5665 break;
5666 case kCondLE:
5667 if (gt_bias) {
5668 __ CmpLeS(FTMP, lhs, rhs);
5669 } else {
5670 __ CmpUleS(FTMP, lhs, rhs);
5671 }
5672 __ Bc1nez(FTMP, label);
5673 break;
5674 case kCondGT:
5675 if (gt_bias) {
5676 __ CmpUltS(FTMP, rhs, lhs);
5677 } else {
5678 __ CmpLtS(FTMP, rhs, lhs);
5679 }
5680 __ Bc1nez(FTMP, label);
5681 break;
5682 case kCondGE:
5683 if (gt_bias) {
5684 __ CmpUleS(FTMP, rhs, lhs);
5685 } else {
5686 __ CmpLeS(FTMP, rhs, lhs);
5687 }
5688 __ Bc1nez(FTMP, label);
5689 break;
5690 default:
5691 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005692 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005693 }
5694 } else {
5695 switch (cond) {
5696 case kCondEQ:
5697 __ CeqS(0, lhs, rhs);
5698 __ Bc1t(0, label);
5699 break;
5700 case kCondNE:
5701 __ CeqS(0, lhs, rhs);
5702 __ Bc1f(0, label);
5703 break;
5704 case kCondLT:
5705 if (gt_bias) {
5706 __ ColtS(0, lhs, rhs);
5707 } else {
5708 __ CultS(0, lhs, rhs);
5709 }
5710 __ Bc1t(0, label);
5711 break;
5712 case kCondLE:
5713 if (gt_bias) {
5714 __ ColeS(0, lhs, rhs);
5715 } else {
5716 __ CuleS(0, lhs, rhs);
5717 }
5718 __ Bc1t(0, label);
5719 break;
5720 case kCondGT:
5721 if (gt_bias) {
5722 __ CultS(0, rhs, lhs);
5723 } else {
5724 __ ColtS(0, rhs, lhs);
5725 }
5726 __ Bc1t(0, label);
5727 break;
5728 case kCondGE:
5729 if (gt_bias) {
5730 __ CuleS(0, rhs, lhs);
5731 } else {
5732 __ ColeS(0, rhs, lhs);
5733 }
5734 __ Bc1t(0, label);
5735 break;
5736 default:
5737 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005738 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005739 }
5740 }
5741 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005742 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005743 if (isR6) {
5744 switch (cond) {
5745 case kCondEQ:
5746 __ CmpEqD(FTMP, lhs, rhs);
5747 __ Bc1nez(FTMP, label);
5748 break;
5749 case kCondNE:
5750 __ CmpEqD(FTMP, lhs, rhs);
5751 __ Bc1eqz(FTMP, label);
5752 break;
5753 case kCondLT:
5754 if (gt_bias) {
5755 __ CmpLtD(FTMP, lhs, rhs);
5756 } else {
5757 __ CmpUltD(FTMP, lhs, rhs);
5758 }
5759 __ Bc1nez(FTMP, label);
5760 break;
5761 case kCondLE:
5762 if (gt_bias) {
5763 __ CmpLeD(FTMP, lhs, rhs);
5764 } else {
5765 __ CmpUleD(FTMP, lhs, rhs);
5766 }
5767 __ Bc1nez(FTMP, label);
5768 break;
5769 case kCondGT:
5770 if (gt_bias) {
5771 __ CmpUltD(FTMP, rhs, lhs);
5772 } else {
5773 __ CmpLtD(FTMP, rhs, lhs);
5774 }
5775 __ Bc1nez(FTMP, label);
5776 break;
5777 case kCondGE:
5778 if (gt_bias) {
5779 __ CmpUleD(FTMP, rhs, lhs);
5780 } else {
5781 __ CmpLeD(FTMP, rhs, lhs);
5782 }
5783 __ Bc1nez(FTMP, label);
5784 break;
5785 default:
5786 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005787 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005788 }
5789 } else {
5790 switch (cond) {
5791 case kCondEQ:
5792 __ CeqD(0, lhs, rhs);
5793 __ Bc1t(0, label);
5794 break;
5795 case kCondNE:
5796 __ CeqD(0, lhs, rhs);
5797 __ Bc1f(0, label);
5798 break;
5799 case kCondLT:
5800 if (gt_bias) {
5801 __ ColtD(0, lhs, rhs);
5802 } else {
5803 __ CultD(0, lhs, rhs);
5804 }
5805 __ Bc1t(0, label);
5806 break;
5807 case kCondLE:
5808 if (gt_bias) {
5809 __ ColeD(0, lhs, rhs);
5810 } else {
5811 __ CuleD(0, lhs, rhs);
5812 }
5813 __ Bc1t(0, label);
5814 break;
5815 case kCondGT:
5816 if (gt_bias) {
5817 __ CultD(0, rhs, lhs);
5818 } else {
5819 __ ColtD(0, rhs, lhs);
5820 }
5821 __ Bc1t(0, label);
5822 break;
5823 case kCondGE:
5824 if (gt_bias) {
5825 __ CuleD(0, rhs, lhs);
5826 } else {
5827 __ ColeD(0, rhs, lhs);
5828 }
5829 __ Bc1t(0, label);
5830 break;
5831 default:
5832 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005833 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005834 }
5835 }
5836 }
5837}
5838
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005839void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005840 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005841 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005842 MipsLabel* false_target) {
5843 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005844
David Brazdil0debae72015-11-12 18:37:00 +00005845 if (true_target == nullptr && false_target == nullptr) {
5846 // Nothing to do. The code always falls through.
5847 return;
5848 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005849 // Constant condition, statically compared against "true" (integer value 1).
5850 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005851 if (true_target != nullptr) {
5852 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005853 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005854 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005855 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005856 if (false_target != nullptr) {
5857 __ B(false_target);
5858 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005859 }
David Brazdil0debae72015-11-12 18:37:00 +00005860 return;
5861 }
5862
5863 // The following code generates these patterns:
5864 // (1) true_target == nullptr && false_target != nullptr
5865 // - opposite condition true => branch to false_target
5866 // (2) true_target != nullptr && false_target == nullptr
5867 // - condition true => branch to true_target
5868 // (3) true_target != nullptr && false_target != nullptr
5869 // - condition true => branch to true_target
5870 // - branch to false_target
5871 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005872 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005873 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005874 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005875 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005876 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5877 } else {
5878 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5879 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005880 } else {
5881 // The condition instruction has not been materialized, use its inputs as
5882 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005883 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005884 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005885 LocationSummary* locations = cond->GetLocations();
5886 IfCondition if_cond = condition->GetCondition();
5887 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005888
David Brazdil0debae72015-11-12 18:37:00 +00005889 if (true_target == nullptr) {
5890 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005891 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005892 }
5893
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005894 switch (type) {
5895 default:
5896 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5897 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005898 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005899 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5900 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005901 case DataType::Type::kFloat32:
5902 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005903 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5904 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005905 }
5906 }
David Brazdil0debae72015-11-12 18:37:00 +00005907
5908 // If neither branch falls through (case 3), the conditional branch to `true_target`
5909 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5910 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005911 __ B(false_target);
5912 }
5913}
5914
5915void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005916 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005917 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005918 locations->SetInAt(0, Location::RequiresRegister());
5919 }
5920}
5921
5922void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005923 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5924 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5925 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5926 nullptr : codegen_->GetLabelOf(true_successor);
5927 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5928 nullptr : codegen_->GetLabelOf(false_successor);
5929 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005930}
5931
5932void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005933 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005934 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005935 InvokeRuntimeCallingConvention calling_convention;
5936 RegisterSet caller_saves = RegisterSet::Empty();
5937 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5938 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005939 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005940 locations->SetInAt(0, Location::RequiresRegister());
5941 }
5942}
5943
5944void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005945 SlowPathCodeMIPS* slow_path =
5946 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005947 GenerateTestAndBranch(deoptimize,
5948 /* condition_input_index */ 0,
5949 slow_path->GetEntryLabel(),
5950 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005951}
5952
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005953// This function returns true if a conditional move can be generated for HSelect.
5954// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5955// branches and regular moves.
5956//
5957// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5958//
5959// While determining feasibility of a conditional move and setting inputs/outputs
5960// are two distinct tasks, this function does both because they share quite a bit
5961// of common logic.
5962static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5963 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5964 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5965 HCondition* condition = cond->AsCondition();
5966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 DataType::Type cond_type =
5968 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5969 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005970
5971 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5972 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5973 bool is_true_value_zero_constant =
5974 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5975 bool is_false_value_zero_constant =
5976 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5977
5978 bool can_move_conditionally = false;
5979 bool use_const_for_false_in = false;
5980 bool use_const_for_true_in = false;
5981
5982 if (!cond->IsConstant()) {
5983 switch (cond_type) {
5984 default:
5985 switch (dst_type) {
5986 default:
5987 // Moving int on int condition.
5988 if (is_r6) {
5989 if (is_true_value_zero_constant) {
5990 // seleqz out_reg, false_reg, cond_reg
5991 can_move_conditionally = true;
5992 use_const_for_true_in = true;
5993 } else if (is_false_value_zero_constant) {
5994 // selnez out_reg, true_reg, cond_reg
5995 can_move_conditionally = true;
5996 use_const_for_false_in = true;
5997 } else if (materialized) {
5998 // Not materializing unmaterialized int conditions
5999 // to keep the instruction count low.
6000 // selnez AT, true_reg, cond_reg
6001 // seleqz TMP, false_reg, cond_reg
6002 // or out_reg, AT, TMP
6003 can_move_conditionally = true;
6004 }
6005 } else {
6006 // movn out_reg, true_reg/ZERO, cond_reg
6007 can_move_conditionally = true;
6008 use_const_for_true_in = is_true_value_zero_constant;
6009 }
6010 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006011 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006012 // Moving long on int condition.
6013 if (is_r6) {
6014 if (is_true_value_zero_constant) {
6015 // seleqz out_reg_lo, false_reg_lo, cond_reg
6016 // seleqz out_reg_hi, false_reg_hi, cond_reg
6017 can_move_conditionally = true;
6018 use_const_for_true_in = true;
6019 } else if (is_false_value_zero_constant) {
6020 // selnez out_reg_lo, true_reg_lo, cond_reg
6021 // selnez out_reg_hi, true_reg_hi, cond_reg
6022 can_move_conditionally = true;
6023 use_const_for_false_in = true;
6024 }
6025 // Other long conditional moves would generate 6+ instructions,
6026 // which is too many.
6027 } else {
6028 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
6029 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
6030 can_move_conditionally = true;
6031 use_const_for_true_in = is_true_value_zero_constant;
6032 }
6033 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006034 case DataType::Type::kFloat32:
6035 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006036 // Moving float/double on int condition.
6037 if (is_r6) {
6038 if (materialized) {
6039 // Not materializing unmaterialized int conditions
6040 // to keep the instruction count low.
6041 can_move_conditionally = true;
6042 if (is_true_value_zero_constant) {
6043 // sltu TMP, ZERO, cond_reg
6044 // mtc1 TMP, temp_cond_reg
6045 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6046 use_const_for_true_in = true;
6047 } else if (is_false_value_zero_constant) {
6048 // sltu TMP, ZERO, cond_reg
6049 // mtc1 TMP, temp_cond_reg
6050 // selnez.fmt out_reg, true_reg, temp_cond_reg
6051 use_const_for_false_in = true;
6052 } else {
6053 // sltu TMP, ZERO, cond_reg
6054 // mtc1 TMP, temp_cond_reg
6055 // sel.fmt temp_cond_reg, false_reg, true_reg
6056 // mov.fmt out_reg, temp_cond_reg
6057 }
6058 }
6059 } else {
6060 // movn.fmt out_reg, true_reg, cond_reg
6061 can_move_conditionally = true;
6062 }
6063 break;
6064 }
6065 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006066 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006067 // We don't materialize long comparison now
6068 // and use conditional branches instead.
6069 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006070 case DataType::Type::kFloat32:
6071 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006072 switch (dst_type) {
6073 default:
6074 // Moving int on float/double condition.
6075 if (is_r6) {
6076 if (is_true_value_zero_constant) {
6077 // mfc1 TMP, temp_cond_reg
6078 // seleqz out_reg, false_reg, TMP
6079 can_move_conditionally = true;
6080 use_const_for_true_in = true;
6081 } else if (is_false_value_zero_constant) {
6082 // mfc1 TMP, temp_cond_reg
6083 // selnez out_reg, true_reg, TMP
6084 can_move_conditionally = true;
6085 use_const_for_false_in = true;
6086 } else {
6087 // mfc1 TMP, temp_cond_reg
6088 // selnez AT, true_reg, TMP
6089 // seleqz TMP, false_reg, TMP
6090 // or out_reg, AT, TMP
6091 can_move_conditionally = true;
6092 }
6093 } else {
6094 // movt out_reg, true_reg/ZERO, cc
6095 can_move_conditionally = true;
6096 use_const_for_true_in = is_true_value_zero_constant;
6097 }
6098 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006099 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006100 // Moving long on float/double condition.
6101 if (is_r6) {
6102 if (is_true_value_zero_constant) {
6103 // mfc1 TMP, temp_cond_reg
6104 // seleqz out_reg_lo, false_reg_lo, TMP
6105 // seleqz out_reg_hi, false_reg_hi, TMP
6106 can_move_conditionally = true;
6107 use_const_for_true_in = true;
6108 } else if (is_false_value_zero_constant) {
6109 // mfc1 TMP, temp_cond_reg
6110 // selnez out_reg_lo, true_reg_lo, TMP
6111 // selnez out_reg_hi, true_reg_hi, TMP
6112 can_move_conditionally = true;
6113 use_const_for_false_in = true;
6114 }
6115 // Other long conditional moves would generate 6+ instructions,
6116 // which is too many.
6117 } else {
6118 // movt out_reg_lo, true_reg_lo/ZERO, cc
6119 // movt out_reg_hi, true_reg_hi/ZERO, cc
6120 can_move_conditionally = true;
6121 use_const_for_true_in = is_true_value_zero_constant;
6122 }
6123 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006124 case DataType::Type::kFloat32:
6125 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006126 // Moving float/double on float/double condition.
6127 if (is_r6) {
6128 can_move_conditionally = true;
6129 if (is_true_value_zero_constant) {
6130 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6131 use_const_for_true_in = true;
6132 } else if (is_false_value_zero_constant) {
6133 // selnez.fmt out_reg, true_reg, temp_cond_reg
6134 use_const_for_false_in = true;
6135 } else {
6136 // sel.fmt temp_cond_reg, false_reg, true_reg
6137 // mov.fmt out_reg, temp_cond_reg
6138 }
6139 } else {
6140 // movt.fmt out_reg, true_reg, cc
6141 can_move_conditionally = true;
6142 }
6143 break;
6144 }
6145 break;
6146 }
6147 }
6148
6149 if (can_move_conditionally) {
6150 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6151 } else {
6152 DCHECK(!use_const_for_false_in);
6153 DCHECK(!use_const_for_true_in);
6154 }
6155
6156 if (locations_to_set != nullptr) {
6157 if (use_const_for_false_in) {
6158 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6159 } else {
6160 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006161 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006162 ? Location::RequiresFpuRegister()
6163 : Location::RequiresRegister());
6164 }
6165 if (use_const_for_true_in) {
6166 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6167 } else {
6168 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006169 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006170 ? Location::RequiresFpuRegister()
6171 : Location::RequiresRegister());
6172 }
6173 if (materialized) {
6174 locations_to_set->SetInAt(2, Location::RequiresRegister());
6175 }
6176 // On R6 we don't require the output to be the same as the
6177 // first input for conditional moves unlike on R2.
6178 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6179 if (is_out_same_as_first_in) {
6180 locations_to_set->SetOut(Location::SameAsFirstInput());
6181 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006182 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006183 ? Location::RequiresFpuRegister()
6184 : Location::RequiresRegister());
6185 }
6186 }
6187
6188 return can_move_conditionally;
6189}
6190
6191void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6192 LocationSummary* locations = select->GetLocations();
6193 Location dst = locations->Out();
6194 Location src = locations->InAt(1);
6195 Register src_reg = ZERO;
6196 Register src_reg_high = ZERO;
6197 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6198 Register cond_reg = TMP;
6199 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006200 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006201 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006202 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006203
6204 if (IsBooleanValueOrMaterializedCondition(cond)) {
6205 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6206 } else {
6207 HCondition* condition = cond->AsCondition();
6208 LocationSummary* cond_locations = cond->GetLocations();
6209 IfCondition if_cond = condition->GetCondition();
6210 cond_type = condition->InputAt(0)->GetType();
6211 switch (cond_type) {
6212 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006213 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006214 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6215 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006216 case DataType::Type::kFloat32:
6217 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006218 cond_inverted = MaterializeFpCompareR2(if_cond,
6219 condition->IsGtBias(),
6220 cond_type,
6221 cond_locations,
6222 cond_cc);
6223 break;
6224 }
6225 }
6226
6227 DCHECK(dst.Equals(locations->InAt(0)));
6228 if (src.IsRegister()) {
6229 src_reg = src.AsRegister<Register>();
6230 } else if (src.IsRegisterPair()) {
6231 src_reg = src.AsRegisterPairLow<Register>();
6232 src_reg_high = src.AsRegisterPairHigh<Register>();
6233 } else if (src.IsConstant()) {
6234 DCHECK(src.GetConstant()->IsZeroBitPattern());
6235 }
6236
6237 switch (cond_type) {
6238 default:
6239 switch (dst_type) {
6240 default:
6241 if (cond_inverted) {
6242 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6243 } else {
6244 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6245 }
6246 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006247 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006248 if (cond_inverted) {
6249 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6250 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6251 } else {
6252 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6253 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6254 }
6255 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006256 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006257 if (cond_inverted) {
6258 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6259 } else {
6260 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6261 }
6262 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006263 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006264 if (cond_inverted) {
6265 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6266 } else {
6267 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6268 }
6269 break;
6270 }
6271 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006272 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006273 LOG(FATAL) << "Unreachable";
6274 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006275 case DataType::Type::kFloat32:
6276 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006277 switch (dst_type) {
6278 default:
6279 if (cond_inverted) {
6280 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6281 } else {
6282 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6283 }
6284 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006285 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006286 if (cond_inverted) {
6287 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6288 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6289 } else {
6290 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6291 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6292 }
6293 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006294 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006295 if (cond_inverted) {
6296 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6297 } else {
6298 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6299 }
6300 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006302 if (cond_inverted) {
6303 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6304 } else {
6305 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6306 }
6307 break;
6308 }
6309 break;
6310 }
6311}
6312
6313void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6314 LocationSummary* locations = select->GetLocations();
6315 Location dst = locations->Out();
6316 Location false_src = locations->InAt(0);
6317 Location true_src = locations->InAt(1);
6318 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6319 Register cond_reg = TMP;
6320 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006321 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006322 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006323 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006324
6325 if (IsBooleanValueOrMaterializedCondition(cond)) {
6326 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6327 } else {
6328 HCondition* condition = cond->AsCondition();
6329 LocationSummary* cond_locations = cond->GetLocations();
6330 IfCondition if_cond = condition->GetCondition();
6331 cond_type = condition->InputAt(0)->GetType();
6332 switch (cond_type) {
6333 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006334 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006335 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6336 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006337 case DataType::Type::kFloat32:
6338 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006339 cond_inverted = MaterializeFpCompareR6(if_cond,
6340 condition->IsGtBias(),
6341 cond_type,
6342 cond_locations,
6343 fcond_reg);
6344 break;
6345 }
6346 }
6347
6348 if (true_src.IsConstant()) {
6349 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6350 }
6351 if (false_src.IsConstant()) {
6352 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6353 }
6354
6355 switch (dst_type) {
6356 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006357 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006358 __ Mfc1(cond_reg, fcond_reg);
6359 }
6360 if (true_src.IsConstant()) {
6361 if (cond_inverted) {
6362 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6363 } else {
6364 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6365 }
6366 } else if (false_src.IsConstant()) {
6367 if (cond_inverted) {
6368 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6369 } else {
6370 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6371 }
6372 } else {
6373 DCHECK_NE(cond_reg, AT);
6374 if (cond_inverted) {
6375 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6376 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6377 } else {
6378 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6379 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6380 }
6381 __ Or(dst.AsRegister<Register>(), AT, TMP);
6382 }
6383 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006384 case DataType::Type::kInt64: {
6385 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006386 __ Mfc1(cond_reg, fcond_reg);
6387 }
6388 Register dst_lo = dst.AsRegisterPairLow<Register>();
6389 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6390 if (true_src.IsConstant()) {
6391 Register src_lo = false_src.AsRegisterPairLow<Register>();
6392 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6393 if (cond_inverted) {
6394 __ Selnez(dst_lo, src_lo, cond_reg);
6395 __ Selnez(dst_hi, src_hi, cond_reg);
6396 } else {
6397 __ Seleqz(dst_lo, src_lo, cond_reg);
6398 __ Seleqz(dst_hi, src_hi, cond_reg);
6399 }
6400 } else {
6401 DCHECK(false_src.IsConstant());
6402 Register src_lo = true_src.AsRegisterPairLow<Register>();
6403 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6404 if (cond_inverted) {
6405 __ Seleqz(dst_lo, src_lo, cond_reg);
6406 __ Seleqz(dst_hi, src_hi, cond_reg);
6407 } else {
6408 __ Selnez(dst_lo, src_lo, cond_reg);
6409 __ Selnez(dst_hi, src_hi, cond_reg);
6410 }
6411 }
6412 break;
6413 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006414 case DataType::Type::kFloat32: {
6415 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006416 // sel*.fmt tests bit 0 of the condition register, account for that.
6417 __ Sltu(TMP, ZERO, cond_reg);
6418 __ Mtc1(TMP, fcond_reg);
6419 }
6420 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6421 if (true_src.IsConstant()) {
6422 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6423 if (cond_inverted) {
6424 __ SelnezS(dst_reg, src_reg, fcond_reg);
6425 } else {
6426 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6427 }
6428 } else if (false_src.IsConstant()) {
6429 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6430 if (cond_inverted) {
6431 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6432 } else {
6433 __ SelnezS(dst_reg, src_reg, fcond_reg);
6434 }
6435 } else {
6436 if (cond_inverted) {
6437 __ SelS(fcond_reg,
6438 true_src.AsFpuRegister<FRegister>(),
6439 false_src.AsFpuRegister<FRegister>());
6440 } else {
6441 __ SelS(fcond_reg,
6442 false_src.AsFpuRegister<FRegister>(),
6443 true_src.AsFpuRegister<FRegister>());
6444 }
6445 __ MovS(dst_reg, fcond_reg);
6446 }
6447 break;
6448 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006449 case DataType::Type::kFloat64: {
6450 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006451 // sel*.fmt tests bit 0 of the condition register, account for that.
6452 __ Sltu(TMP, ZERO, cond_reg);
6453 __ Mtc1(TMP, fcond_reg);
6454 }
6455 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6456 if (true_src.IsConstant()) {
6457 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6458 if (cond_inverted) {
6459 __ SelnezD(dst_reg, src_reg, fcond_reg);
6460 } else {
6461 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6462 }
6463 } else if (false_src.IsConstant()) {
6464 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6465 if (cond_inverted) {
6466 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6467 } else {
6468 __ SelnezD(dst_reg, src_reg, fcond_reg);
6469 }
6470 } else {
6471 if (cond_inverted) {
6472 __ SelD(fcond_reg,
6473 true_src.AsFpuRegister<FRegister>(),
6474 false_src.AsFpuRegister<FRegister>());
6475 } else {
6476 __ SelD(fcond_reg,
6477 false_src.AsFpuRegister<FRegister>(),
6478 true_src.AsFpuRegister<FRegister>());
6479 }
6480 __ MovD(dst_reg, fcond_reg);
6481 }
6482 break;
6483 }
6484 }
6485}
6486
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006487void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006488 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006489 LocationSummary(flag, LocationSummary::kNoCall);
6490 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006491}
6492
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006493void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6494 __ LoadFromOffset(kLoadWord,
6495 flag->GetLocations()->Out().AsRegister<Register>(),
6496 SP,
6497 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006498}
6499
David Brazdil74eb1b22015-12-14 11:44:01 +00006500void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006501 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006502 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006503}
6504
6505void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006506 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6507 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6508 if (is_r6) {
6509 GenConditionalMoveR6(select);
6510 } else {
6511 GenConditionalMoveR2(select);
6512 }
6513 } else {
6514 LocationSummary* locations = select->GetLocations();
6515 MipsLabel false_target;
6516 GenerateTestAndBranch(select,
6517 /* condition_input_index */ 2,
6518 /* true_target */ nullptr,
6519 &false_target);
6520 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6521 __ Bind(&false_target);
6522 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006523}
6524
David Srbecky0cf44932015-12-09 14:09:59 +00006525void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006526 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006527}
6528
David Srbeckyd28f4a02016-03-14 17:14:24 +00006529void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6530 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006531}
6532
6533void CodeGeneratorMIPS::GenerateNop() {
6534 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006535}
6536
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006537void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006538 DataType::Type field_type = field_info.GetFieldType();
6539 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006540 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006541 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006542 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006543 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006544 instruction,
6545 generate_volatile
6546 ? LocationSummary::kCallOnMainOnly
6547 : (object_field_get_with_read_barrier
6548 ? LocationSummary::kCallOnSlowPath
6549 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006550
Alexey Frunzec61c0762017-04-10 13:54:23 -07006551 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6552 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6553 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006554 locations->SetInAt(0, Location::RequiresRegister());
6555 if (generate_volatile) {
6556 InvokeRuntimeCallingConvention calling_convention;
6557 // need A0 to hold base + offset
6558 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006559 if (field_type == DataType::Type::kInt64) {
6560 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006561 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006562 // Use Location::Any() to prevent situations when running out of available fp registers.
6563 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006564 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006565 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006566 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6567 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6568 }
6569 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006570 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006571 locations->SetOut(Location::RequiresFpuRegister());
6572 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006573 // The output overlaps in the case of an object field get with
6574 // read barriers enabled: we do not want the move to overwrite the
6575 // object's location, as we need it to emit the read barrier.
6576 locations->SetOut(Location::RequiresRegister(),
6577 object_field_get_with_read_barrier
6578 ? Location::kOutputOverlap
6579 : Location::kNoOutputOverlap);
6580 }
6581 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6582 // We need a temporary register for the read barrier marking slow
6583 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006584 if (!kBakerReadBarrierThunksEnableForFields) {
6585 locations->AddTemp(Location::RequiresRegister());
6586 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006587 }
6588 }
6589}
6590
6591void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6592 const FieldInfo& field_info,
6593 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006594 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6595 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006596 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006597 Location obj_loc = locations->InAt(0);
6598 Register obj = obj_loc.AsRegister<Register>();
6599 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006600 LoadOperandType load_type = kLoadUnsignedByte;
6601 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006602 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006603 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006604
6605 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006606 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006607 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006608 load_type = kLoadUnsignedByte;
6609 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006610 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006611 load_type = kLoadSignedByte;
6612 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006613 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006614 load_type = kLoadUnsignedHalfword;
6615 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006616 case DataType::Type::kInt16:
6617 load_type = kLoadSignedHalfword;
6618 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006619 case DataType::Type::kInt32:
6620 case DataType::Type::kFloat32:
6621 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006622 load_type = kLoadWord;
6623 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006624 case DataType::Type::kInt64:
6625 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006626 load_type = kLoadDoubleword;
6627 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006628 case DataType::Type::kUint32:
6629 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006630 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006631 LOG(FATAL) << "Unreachable type " << type;
6632 UNREACHABLE();
6633 }
6634
6635 if (is_volatile && load_type == kLoadDoubleword) {
6636 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006637 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006638 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006639 __ LoadFromOffset(kLoadWord,
6640 ZERO,
6641 locations->GetTemp(0).AsRegister<Register>(),
6642 0,
6643 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006644 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006645 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006646 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006647 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006648 if (dst_loc.IsFpuRegister()) {
6649 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006650 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006651 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006652 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006653 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006654 __ StoreToOffset(kStoreWord,
6655 locations->GetTemp(1).AsRegister<Register>(),
6656 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006657 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006658 __ StoreToOffset(kStoreWord,
6659 locations->GetTemp(2).AsRegister<Register>(),
6660 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006661 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006662 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006663 }
6664 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006665 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006666 // /* HeapReference<Object> */ dst = *(obj + offset)
6667 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006668 Location temp_loc =
6669 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006670 // Note that a potential implicit null check is handled in this
6671 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6672 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6673 dst_loc,
6674 obj,
6675 offset,
6676 temp_loc,
6677 /* needs_null_check */ true);
6678 if (is_volatile) {
6679 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6680 }
6681 } else {
6682 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6683 if (is_volatile) {
6684 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6685 }
6686 // If read barriers are enabled, emit read barriers other than
6687 // Baker's using a slow path (and also unpoison the loaded
6688 // reference, if heap poisoning is enabled).
6689 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6690 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006691 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006692 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006693 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006694 DCHECK(dst_loc.IsRegisterPair());
6695 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006696 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006697 DCHECK(dst_loc.IsRegister());
6698 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006699 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006700 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006701 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006702 DCHECK(dst_loc.IsFpuRegister());
6703 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006704 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006705 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006706 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006707 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006708 }
6709 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006710 }
6711
Alexey Frunze15958152017-02-09 19:08:30 -08006712 // Memory barriers, in the case of references, are handled in the
6713 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006714 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006715 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6716 }
6717}
6718
6719void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006720 DataType::Type field_type = field_info.GetFieldType();
6721 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006722 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006723 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006724 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006725
6726 locations->SetInAt(0, Location::RequiresRegister());
6727 if (generate_volatile) {
6728 InvokeRuntimeCallingConvention calling_convention;
6729 // need A0 to hold base + offset
6730 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006731 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006732 locations->SetInAt(1, Location::RegisterPairLocation(
6733 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6734 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006735 // Use Location::Any() to prevent situations when running out of available fp registers.
6736 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006737 // Pass FP parameters in core registers.
6738 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6739 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6740 }
6741 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006742 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006743 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006744 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006745 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006746 }
6747 }
6748}
6749
6750void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6751 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006752 uint32_t dex_pc,
6753 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006754 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006755 LocationSummary* locations = instruction->GetLocations();
6756 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006757 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006758 StoreOperandType store_type = kStoreByte;
6759 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006760 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006761 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006762 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006763
6764 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006765 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006766 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006767 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006768 store_type = kStoreByte;
6769 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006770 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006771 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006772 store_type = kStoreHalfword;
6773 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006774 case DataType::Type::kInt32:
6775 case DataType::Type::kFloat32:
6776 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006777 store_type = kStoreWord;
6778 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006779 case DataType::Type::kInt64:
6780 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006781 store_type = kStoreDoubleword;
6782 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006783 case DataType::Type::kUint32:
6784 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006785 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006786 LOG(FATAL) << "Unreachable type " << type;
6787 UNREACHABLE();
6788 }
6789
6790 if (is_volatile) {
6791 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6792 }
6793
6794 if (is_volatile && store_type == kStoreDoubleword) {
6795 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006796 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006797 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006798 __ LoadFromOffset(kLoadWord,
6799 ZERO,
6800 locations->GetTemp(0).AsRegister<Register>(),
6801 0,
6802 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006803 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006804 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006805 if (value_location.IsFpuRegister()) {
6806 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6807 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006808 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006809 value_location.AsFpuRegister<FRegister>());
6810 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006811 __ LoadFromOffset(kLoadWord,
6812 locations->GetTemp(1).AsRegister<Register>(),
6813 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006814 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006815 __ LoadFromOffset(kLoadWord,
6816 locations->GetTemp(2).AsRegister<Register>(),
6817 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006818 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006819 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006820 DCHECK(value_location.IsConstant());
6821 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6822 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006823 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6824 locations->GetTemp(1).AsRegister<Register>(),
6825 value);
6826 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006827 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006828 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006829 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6830 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006831 if (value_location.IsConstant()) {
6832 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6833 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006834 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006835 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006836 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006837 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006838 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006839 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006840 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006841 if (kPoisonHeapReferences && needs_write_barrier) {
6842 // Note that in the case where `value` is a null reference,
6843 // we do not enter this block, as a null reference does not
6844 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006845 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006846 __ PoisonHeapReference(TMP, src);
6847 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6848 } else {
6849 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6850 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006851 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006852 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006853 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006854 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006855 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006856 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006857 }
6858 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006859 }
6860
Alexey Frunzec061de12017-02-14 13:27:23 -08006861 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006862 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006863 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006864 }
6865
6866 if (is_volatile) {
6867 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6868 }
6869}
6870
6871void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6872 HandleFieldGet(instruction, instruction->GetFieldInfo());
6873}
6874
6875void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6876 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6877}
6878
6879void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6880 HandleFieldSet(instruction, instruction->GetFieldInfo());
6881}
6882
6883void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006884 HandleFieldSet(instruction,
6885 instruction->GetFieldInfo(),
6886 instruction->GetDexPc(),
6887 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006888}
6889
Alexey Frunze15958152017-02-09 19:08:30 -08006890void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6891 HInstruction* instruction,
6892 Location out,
6893 uint32_t offset,
6894 Location maybe_temp,
6895 ReadBarrierOption read_barrier_option) {
6896 Register out_reg = out.AsRegister<Register>();
6897 if (read_barrier_option == kWithReadBarrier) {
6898 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006899 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6900 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6901 }
Alexey Frunze15958152017-02-09 19:08:30 -08006902 if (kUseBakerReadBarrier) {
6903 // Load with fast path based Baker's read barrier.
6904 // /* HeapReference<Object> */ out = *(out + offset)
6905 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6906 out,
6907 out_reg,
6908 offset,
6909 maybe_temp,
6910 /* needs_null_check */ false);
6911 } else {
6912 // Load with slow path based read barrier.
6913 // Save the value of `out` into `maybe_temp` before overwriting it
6914 // in the following move operation, as we will need it for the
6915 // read barrier below.
6916 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6917 // /* HeapReference<Object> */ out = *(out + offset)
6918 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6919 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6920 }
6921 } else {
6922 // Plain load with no read barrier.
6923 // /* HeapReference<Object> */ out = *(out + offset)
6924 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6925 __ MaybeUnpoisonHeapReference(out_reg);
6926 }
6927}
6928
6929void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6930 HInstruction* instruction,
6931 Location out,
6932 Location obj,
6933 uint32_t offset,
6934 Location maybe_temp,
6935 ReadBarrierOption read_barrier_option) {
6936 Register out_reg = out.AsRegister<Register>();
6937 Register obj_reg = obj.AsRegister<Register>();
6938 if (read_barrier_option == kWithReadBarrier) {
6939 CHECK(kEmitCompilerReadBarrier);
6940 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006941 if (!kBakerReadBarrierThunksEnableForFields) {
6942 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6943 }
Alexey Frunze15958152017-02-09 19:08:30 -08006944 // Load with fast path based Baker's read barrier.
6945 // /* HeapReference<Object> */ out = *(obj + offset)
6946 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6947 out,
6948 obj_reg,
6949 offset,
6950 maybe_temp,
6951 /* needs_null_check */ false);
6952 } else {
6953 // Load with slow path based read barrier.
6954 // /* HeapReference<Object> */ out = *(obj + offset)
6955 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6956 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6957 }
6958 } else {
6959 // Plain load with no read barrier.
6960 // /* HeapReference<Object> */ out = *(obj + offset)
6961 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6962 __ MaybeUnpoisonHeapReference(out_reg);
6963 }
6964}
6965
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006966static inline int GetBakerMarkThunkNumber(Register reg) {
6967 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6968 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6969 return reg - V0;
6970 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6971 return 14 + (reg - S2);
6972 } else if (reg == FP) { // One more.
6973 return 20;
6974 }
6975 LOG(FATAL) << "Unexpected register " << reg;
6976 UNREACHABLE();
6977}
6978
6979static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6980 int num = GetBakerMarkThunkNumber(reg) +
6981 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6982 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6983}
6984
6985static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6986 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6987 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6988}
6989
Alexey Frunze15958152017-02-09 19:08:30 -08006990void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6991 Location root,
6992 Register obj,
6993 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006994 ReadBarrierOption read_barrier_option,
6995 MipsLabel* label_low) {
6996 bool reordering;
6997 if (label_low != nullptr) {
6998 DCHECK_EQ(offset, 0x5678u);
6999 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007000 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007001 if (read_barrier_option == kWithReadBarrier) {
7002 DCHECK(kEmitCompilerReadBarrier);
7003 if (kUseBakerReadBarrier) {
7004 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
7005 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007006 if (kBakerReadBarrierThunksEnableForGcRoots) {
7007 // Note that we do not actually check the value of `GetIsGcMarking()`
7008 // to decide whether to mark the loaded GC root or not. Instead, we
7009 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7010 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7011 // vice versa.
7012 //
7013 // We use thunks for the slow path. That thunk checks the reference
7014 // and jumps to the entrypoint if needed.
7015 //
7016 // temp = Thread::Current()->pReadBarrierMarkReg00
7017 // // AKA &art_quick_read_barrier_mark_introspection.
7018 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7019 // if (temp != nullptr) {
7020 // temp = &gc_root_thunk<root_reg>
7021 // root = temp(root)
7022 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007023
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007024 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7025 const int32_t entry_point_offset =
7026 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7027 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
7028 int16_t offset_low = Low16Bits(offset);
7029 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
7030 // extension in lw.
7031 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7032 Register base = short_offset ? obj : TMP;
7033 // Loading the entrypoint does not require a load acquire since it is only changed when
7034 // threads are suspended or running a checkpoint.
7035 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7036 reordering = __ SetReorder(false);
7037 if (!short_offset) {
7038 DCHECK(!label_low);
7039 __ AddUpper(base, obj, offset_high);
7040 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007041 MipsLabel skip_call;
7042 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007043 if (label_low != nullptr) {
7044 DCHECK(short_offset);
7045 __ Bind(label_low);
7046 }
7047 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7048 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
7049 // in delay slot.
7050 if (isR6) {
7051 __ Jialc(T9, thunk_disp);
7052 } else {
7053 __ Addiu(T9, T9, thunk_disp);
7054 __ Jalr(T9);
7055 __ Nop();
7056 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007057 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007058 __ SetReorder(reordering);
7059 } else {
7060 // Note that we do not actually check the value of `GetIsGcMarking()`
7061 // to decide whether to mark the loaded GC root or not. Instead, we
7062 // load into `temp` (T9) the read barrier mark entry point corresponding
7063 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
7064 // is false, and vice versa.
7065 //
7066 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7067 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7068 // if (temp != null) {
7069 // root = temp(root)
7070 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007071
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007072 if (label_low != nullptr) {
7073 reordering = __ SetReorder(false);
7074 __ Bind(label_low);
7075 }
7076 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7077 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7078 if (label_low != nullptr) {
7079 __ SetReorder(reordering);
7080 }
7081 static_assert(
7082 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7083 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7084 "have different sizes.");
7085 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7086 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7087 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08007088
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007089 // Slow path marking the GC root `root`.
7090 Location temp = Location::RegisterLocation(T9);
7091 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007092 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007093 instruction,
7094 root,
7095 /*entrypoint*/ temp);
7096 codegen_->AddSlowPath(slow_path);
7097
7098 const int32_t entry_point_offset =
7099 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
7100 // Loading the entrypoint does not require a load acquire since it is only changed when
7101 // threads are suspended or running a checkpoint.
7102 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7103 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
7104 __ Bind(slow_path->GetExitLabel());
7105 }
Alexey Frunze15958152017-02-09 19:08:30 -08007106 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007107 if (label_low != nullptr) {
7108 reordering = __ SetReorder(false);
7109 __ Bind(label_low);
7110 }
Alexey Frunze15958152017-02-09 19:08:30 -08007111 // GC root loaded through a slow path for read barriers other
7112 // than Baker's.
7113 // /* GcRoot<mirror::Object>* */ root = obj + offset
7114 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007115 if (label_low != nullptr) {
7116 __ SetReorder(reordering);
7117 }
Alexey Frunze15958152017-02-09 19:08:30 -08007118 // /* mirror::Object* */ root = root->Read()
7119 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7120 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007121 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007122 if (label_low != nullptr) {
7123 reordering = __ SetReorder(false);
7124 __ Bind(label_low);
7125 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007126 // Plain GC root load with no read barrier.
7127 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7128 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7129 // Note that GC roots are not affected by heap poisoning, thus we
7130 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007131 if (label_low != nullptr) {
7132 __ SetReorder(reordering);
7133 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007134 }
7135}
7136
Alexey Frunze15958152017-02-09 19:08:30 -08007137void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7138 Location ref,
7139 Register obj,
7140 uint32_t offset,
7141 Location temp,
7142 bool needs_null_check) {
7143 DCHECK(kEmitCompilerReadBarrier);
7144 DCHECK(kUseBakerReadBarrier);
7145
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007146 if (kBakerReadBarrierThunksEnableForFields) {
7147 // Note that we do not actually check the value of `GetIsGcMarking()`
7148 // to decide whether to mark the loaded reference or not. Instead, we
7149 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7150 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7151 // vice versa.
7152 //
7153 // We use thunks for the slow path. That thunk checks the reference
7154 // and jumps to the entrypoint if needed. If the holder is not gray,
7155 // it issues a load-load memory barrier and returns to the original
7156 // reference load.
7157 //
7158 // temp = Thread::Current()->pReadBarrierMarkReg00
7159 // // AKA &art_quick_read_barrier_mark_introspection.
7160 // if (temp != nullptr) {
7161 // temp = &field_array_thunk<holder_reg>
7162 // temp()
7163 // }
7164 // not_gray_return_address:
7165 // // If the offset is too large to fit into the lw instruction, we
7166 // // use an adjusted base register (TMP) here. This register
7167 // // receives bits 16 ... 31 of the offset before the thunk invocation
7168 // // and the thunk benefits from it.
7169 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7170 // gray_return_address:
7171
7172 DCHECK(temp.IsInvalid());
7173 bool isR6 = GetInstructionSetFeatures().IsR6();
7174 int16_t offset_low = Low16Bits(offset);
7175 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7176 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7177 bool reordering = __ SetReorder(false);
7178 const int32_t entry_point_offset =
7179 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7180 // There may have or may have not been a null check if the field offset is smaller than
7181 // the page size.
7182 // There must've been a null check in case it's actually a load from an array.
7183 // We will, however, perform an explicit null check in the thunk as it's easier to
7184 // do it than not.
7185 if (instruction->IsArrayGet()) {
7186 DCHECK(!needs_null_check);
7187 }
7188 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7189 // Loading the entrypoint does not require a load acquire since it is only changed when
7190 // threads are suspended or running a checkpoint.
7191 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7192 Register ref_reg = ref.AsRegister<Register>();
7193 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007194 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007195 if (short_offset) {
7196 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007197 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007198 __ Nop(); // In forbidden slot.
7199 __ Jialc(T9, thunk_disp);
7200 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007201 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007202 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7203 __ Jalr(T9);
7204 __ Nop(); // In delay slot.
7205 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007206 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007207 } else {
7208 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007209 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007210 __ Aui(base, obj, offset_high); // In delay slot.
7211 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007212 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007213 } else {
7214 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007215 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007216 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7217 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007218 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007219 __ Addu(base, base, obj); // In delay slot.
7220 }
7221 }
7222 // /* HeapReference<Object> */ ref = *(obj + offset)
7223 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7224 if (needs_null_check) {
7225 MaybeRecordImplicitNullCheck(instruction);
7226 }
7227 __ MaybeUnpoisonHeapReference(ref_reg);
7228 __ SetReorder(reordering);
7229 return;
7230 }
7231
Alexey Frunze15958152017-02-09 19:08:30 -08007232 // /* HeapReference<Object> */ ref = *(obj + offset)
7233 Location no_index = Location::NoLocation();
7234 ScaleFactor no_scale_factor = TIMES_1;
7235 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7236 ref,
7237 obj,
7238 offset,
7239 no_index,
7240 no_scale_factor,
7241 temp,
7242 needs_null_check);
7243}
7244
7245void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7246 Location ref,
7247 Register obj,
7248 uint32_t data_offset,
7249 Location index,
7250 Location temp,
7251 bool needs_null_check) {
7252 DCHECK(kEmitCompilerReadBarrier);
7253 DCHECK(kUseBakerReadBarrier);
7254
7255 static_assert(
7256 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7257 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007258 ScaleFactor scale_factor = TIMES_4;
7259
7260 if (kBakerReadBarrierThunksEnableForArrays) {
7261 // Note that we do not actually check the value of `GetIsGcMarking()`
7262 // to decide whether to mark the loaded reference or not. Instead, we
7263 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7264 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7265 // vice versa.
7266 //
7267 // We use thunks for the slow path. That thunk checks the reference
7268 // and jumps to the entrypoint if needed. If the holder is not gray,
7269 // it issues a load-load memory barrier and returns to the original
7270 // reference load.
7271 //
7272 // temp = Thread::Current()->pReadBarrierMarkReg00
7273 // // AKA &art_quick_read_barrier_mark_introspection.
7274 // if (temp != nullptr) {
7275 // temp = &field_array_thunk<holder_reg>
7276 // temp()
7277 // }
7278 // not_gray_return_address:
7279 // // The element address is pre-calculated in the TMP register before the
7280 // // thunk invocation and the thunk benefits from it.
7281 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7282 // gray_return_address:
7283
7284 DCHECK(temp.IsInvalid());
7285 DCHECK(index.IsValid());
7286 bool reordering = __ SetReorder(false);
7287 const int32_t entry_point_offset =
7288 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7289 // We will not do the explicit null check in the thunk as some form of a null check
7290 // must've been done earlier.
7291 DCHECK(!needs_null_check);
7292 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7293 // Loading the entrypoint does not require a load acquire since it is only changed when
7294 // threads are suspended or running a checkpoint.
7295 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7296 Register ref_reg = ref.AsRegister<Register>();
7297 Register index_reg = index.IsRegisterPair()
7298 ? index.AsRegisterPairLow<Register>()
7299 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007300 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007301 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007302 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007303 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7304 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007305 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007306 } else {
7307 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007308 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007309 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7310 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007311 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007312 __ Addu(TMP, TMP, obj); // In delay slot.
7313 }
7314 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7315 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7316 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7317 __ MaybeUnpoisonHeapReference(ref_reg);
7318 __ SetReorder(reordering);
7319 return;
7320 }
7321
Alexey Frunze15958152017-02-09 19:08:30 -08007322 // /* HeapReference<Object> */ ref =
7323 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007324 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7325 ref,
7326 obj,
7327 data_offset,
7328 index,
7329 scale_factor,
7330 temp,
7331 needs_null_check);
7332}
7333
7334void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7335 Location ref,
7336 Register obj,
7337 uint32_t offset,
7338 Location index,
7339 ScaleFactor scale_factor,
7340 Location temp,
7341 bool needs_null_check,
7342 bool always_update_field) {
7343 DCHECK(kEmitCompilerReadBarrier);
7344 DCHECK(kUseBakerReadBarrier);
7345
7346 // In slow path based read barriers, the read barrier call is
7347 // inserted after the original load. However, in fast path based
7348 // Baker's read barriers, we need to perform the load of
7349 // mirror::Object::monitor_ *before* the original reference load.
7350 // This load-load ordering is required by the read barrier.
7351 // The fast path/slow path (for Baker's algorithm) should look like:
7352 //
7353 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7354 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7355 // HeapReference<Object> ref = *src; // Original reference load.
7356 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7357 // if (is_gray) {
7358 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7359 // }
7360 //
7361 // Note: the original implementation in ReadBarrier::Barrier is
7362 // slightly more complex as it performs additional checks that we do
7363 // not do here for performance reasons.
7364
7365 Register ref_reg = ref.AsRegister<Register>();
7366 Register temp_reg = temp.AsRegister<Register>();
7367 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7368
7369 // /* int32_t */ monitor = obj->monitor_
7370 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7371 if (needs_null_check) {
7372 MaybeRecordImplicitNullCheck(instruction);
7373 }
7374 // /* LockWord */ lock_word = LockWord(monitor)
7375 static_assert(sizeof(LockWord) == sizeof(int32_t),
7376 "art::LockWord and int32_t have different sizes.");
7377
7378 __ Sync(0); // Barrier to prevent load-load reordering.
7379
7380 // The actual reference load.
7381 if (index.IsValid()) {
7382 // Load types involving an "index": ArrayGet,
7383 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7384 // intrinsics.
7385 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7386 if (index.IsConstant()) {
7387 size_t computed_offset =
7388 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7389 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7390 } else {
7391 // Handle the special case of the
7392 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7393 // intrinsics, which use a register pair as index ("long
7394 // offset"), of which only the low part contains data.
7395 Register index_reg = index.IsRegisterPair()
7396 ? index.AsRegisterPairLow<Register>()
7397 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007398 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007399 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7400 }
7401 } else {
7402 // /* HeapReference<Object> */ ref = *(obj + offset)
7403 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7404 }
7405
7406 // Object* ref = ref_addr->AsMirrorPtr()
7407 __ MaybeUnpoisonHeapReference(ref_reg);
7408
7409 // Slow path marking the object `ref` when it is gray.
7410 SlowPathCodeMIPS* slow_path;
7411 if (always_update_field) {
7412 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7413 // of the form `obj + field_offset`, where `obj` is a register and
7414 // `field_offset` is a register pair (of which only the lower half
7415 // is used). Thus `offset` and `scale_factor` above are expected
7416 // to be null in this code path.
7417 DCHECK_EQ(offset, 0u);
7418 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007419 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007420 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7421 ref,
7422 obj,
7423 /* field_offset */ index,
7424 temp_reg);
7425 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007426 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007427 }
7428 AddSlowPath(slow_path);
7429
7430 // if (rb_state == ReadBarrier::GrayState())
7431 // ref = ReadBarrier::Mark(ref);
7432 // Given the numeric representation, it's enough to check the low bit of the
7433 // rb_state. We do that by shifting the bit into the sign bit (31) and
7434 // performing a branch on less than zero.
7435 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7436 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7437 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7438 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7439 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7440 __ Bind(slow_path->GetExitLabel());
7441}
7442
7443void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7444 Location out,
7445 Location ref,
7446 Location obj,
7447 uint32_t offset,
7448 Location index) {
7449 DCHECK(kEmitCompilerReadBarrier);
7450
7451 // Insert a slow path based read barrier *after* the reference load.
7452 //
7453 // If heap poisoning is enabled, the unpoisoning of the loaded
7454 // reference will be carried out by the runtime within the slow
7455 // path.
7456 //
7457 // Note that `ref` currently does not get unpoisoned (when heap
7458 // poisoning is enabled), which is alright as the `ref` argument is
7459 // not used by the artReadBarrierSlow entry point.
7460 //
7461 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007462 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007463 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7464 AddSlowPath(slow_path);
7465
7466 __ B(slow_path->GetEntryLabel());
7467 __ Bind(slow_path->GetExitLabel());
7468}
7469
7470void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7471 Location out,
7472 Location ref,
7473 Location obj,
7474 uint32_t offset,
7475 Location index) {
7476 if (kEmitCompilerReadBarrier) {
7477 // Baker's read barriers shall be handled by the fast path
7478 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7479 DCHECK(!kUseBakerReadBarrier);
7480 // If heap poisoning is enabled, unpoisoning will be taken care of
7481 // by the runtime within the slow path.
7482 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7483 } else if (kPoisonHeapReferences) {
7484 __ UnpoisonHeapReference(out.AsRegister<Register>());
7485 }
7486}
7487
7488void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7489 Location out,
7490 Location root) {
7491 DCHECK(kEmitCompilerReadBarrier);
7492
7493 // Insert a slow path based read barrier *after* the GC root load.
7494 //
7495 // Note that GC roots are not affected by heap poisoning, so we do
7496 // not need to do anything special for this here.
7497 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007498 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007499 AddSlowPath(slow_path);
7500
7501 __ B(slow_path->GetEntryLabel());
7502 __ Bind(slow_path->GetExitLabel());
7503}
7504
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007505void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007506 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7507 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007508 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007509 switch (type_check_kind) {
7510 case TypeCheckKind::kExactCheck:
7511 case TypeCheckKind::kAbstractClassCheck:
7512 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007513 case TypeCheckKind::kArrayObjectCheck: {
7514 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7515 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7516 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007517 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007518 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007519 case TypeCheckKind::kArrayCheck:
7520 case TypeCheckKind::kUnresolvedCheck:
7521 case TypeCheckKind::kInterfaceCheck:
7522 call_kind = LocationSummary::kCallOnSlowPath;
7523 break;
Vladimir Marko175e7862018-03-27 09:03:13 +00007524 case TypeCheckKind::kBitstringCheck:
7525 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007526 }
7527
Vladimir Markoca6fff82017-10-03 14:49:14 +01007528 LocationSummary* locations =
7529 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007530 if (baker_read_barrier_slow_path) {
7531 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7532 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007533 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko175e7862018-03-27 09:03:13 +00007534 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7535 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7536 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7537 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7538 } else {
7539 locations->SetInAt(1, Location::RequiresRegister());
7540 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007541 // The output does overlap inputs.
7542 // Note that TypeCheckSlowPathMIPS uses this register too.
7543 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007544 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007545}
7546
7547void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007548 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007549 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007550 Location obj_loc = locations->InAt(0);
7551 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko175e7862018-03-27 09:03:13 +00007552 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08007553 Location out_loc = locations->Out();
7554 Register out = out_loc.AsRegister<Register>();
7555 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7556 DCHECK_LE(num_temps, 1u);
7557 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007558 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7559 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7560 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7561 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007562 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007563 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007564
7565 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007566 // Avoid this check if we know `obj` is not null.
7567 if (instruction->MustDoNullCheck()) {
7568 __ Move(out, ZERO);
7569 __ Beqz(obj, &done);
7570 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007571
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007572 switch (type_check_kind) {
7573 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007574 ReadBarrierOption read_barrier_option =
7575 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007576 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007577 GenerateReferenceLoadTwoRegisters(instruction,
7578 out_loc,
7579 obj_loc,
7580 class_offset,
7581 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007582 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007583 // Classes must be equal for the instanceof to succeed.
Vladimir Marko175e7862018-03-27 09:03:13 +00007584 __ Xor(out, out, cls.AsRegister<Register>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007585 __ Sltiu(out, out, 1);
7586 break;
7587 }
7588
7589 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007590 ReadBarrierOption read_barrier_option =
7591 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007592 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007593 GenerateReferenceLoadTwoRegisters(instruction,
7594 out_loc,
7595 obj_loc,
7596 class_offset,
7597 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007598 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007599 // If the class is abstract, we eagerly fetch the super class of the
7600 // object to avoid doing a comparison we know will fail.
7601 MipsLabel loop;
7602 __ Bind(&loop);
7603 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007604 GenerateReferenceLoadOneRegister(instruction,
7605 out_loc,
7606 super_offset,
7607 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007608 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007609 // If `out` is null, we use it for the result, and jump to `done`.
7610 __ Beqz(out, &done);
Vladimir Marko175e7862018-03-27 09:03:13 +00007611 __ Bne(out, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007612 __ LoadConst32(out, 1);
7613 break;
7614 }
7615
7616 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007617 ReadBarrierOption read_barrier_option =
7618 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007619 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007620 GenerateReferenceLoadTwoRegisters(instruction,
7621 out_loc,
7622 obj_loc,
7623 class_offset,
7624 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007625 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007626 // Walk over the class hierarchy to find a match.
7627 MipsLabel loop, success;
7628 __ Bind(&loop);
Vladimir Marko175e7862018-03-27 09:03:13 +00007629 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007630 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007631 GenerateReferenceLoadOneRegister(instruction,
7632 out_loc,
7633 super_offset,
7634 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007635 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007636 __ Bnez(out, &loop);
7637 // If `out` is null, we use it for the result, and jump to `done`.
7638 __ B(&done);
7639 __ Bind(&success);
7640 __ LoadConst32(out, 1);
7641 break;
7642 }
7643
7644 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007645 ReadBarrierOption read_barrier_option =
7646 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007647 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007648 GenerateReferenceLoadTwoRegisters(instruction,
7649 out_loc,
7650 obj_loc,
7651 class_offset,
7652 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007653 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007654 // Do an exact check.
7655 MipsLabel success;
Vladimir Marko175e7862018-03-27 09:03:13 +00007656 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007657 // Otherwise, we need to check that the object's class is a non-primitive array.
7658 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007659 GenerateReferenceLoadOneRegister(instruction,
7660 out_loc,
7661 component_offset,
7662 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007663 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007664 // If `out` is null, we use it for the result, and jump to `done`.
7665 __ Beqz(out, &done);
7666 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7667 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7668 __ Sltiu(out, out, 1);
7669 __ B(&done);
7670 __ Bind(&success);
7671 __ LoadConst32(out, 1);
7672 break;
7673 }
7674
7675 case TypeCheckKind::kArrayCheck: {
7676 // No read barrier since the slow path will retry upon failure.
7677 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007678 GenerateReferenceLoadTwoRegisters(instruction,
7679 out_loc,
7680 obj_loc,
7681 class_offset,
7682 maybe_temp_loc,
7683 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007684 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007685 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7686 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007687 codegen_->AddSlowPath(slow_path);
Vladimir Marko175e7862018-03-27 09:03:13 +00007688 __ Bne(out, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007689 __ LoadConst32(out, 1);
7690 break;
7691 }
7692
7693 case TypeCheckKind::kUnresolvedCheck:
7694 case TypeCheckKind::kInterfaceCheck: {
7695 // Note that we indeed only call on slow path, but we always go
7696 // into the slow path for the unresolved and interface check
7697 // cases.
7698 //
7699 // We cannot directly call the InstanceofNonTrivial runtime
7700 // entry point without resorting to a type checking slow path
7701 // here (i.e. by calling InvokeRuntime directly), as it would
7702 // require to assign fixed registers for the inputs of this
7703 // HInstanceOf instruction (following the runtime calling
7704 // convention), which might be cluttered by the potential first
7705 // read barrier emission at the beginning of this method.
7706 //
7707 // TODO: Introduce a new runtime entry point taking the object
7708 // to test (instead of its class) as argument, and let it deal
7709 // with the read barrier issues. This will let us refactor this
7710 // case of the `switch` code as it was previously (with a direct
7711 // call to the runtime not using a type checking slow path).
7712 // This should also be beneficial for the other cases above.
7713 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007714 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7715 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007716 codegen_->AddSlowPath(slow_path);
7717 __ B(slow_path->GetEntryLabel());
7718 break;
7719 }
Vladimir Marko175e7862018-03-27 09:03:13 +00007720
7721 case TypeCheckKind::kBitstringCheck: {
7722 // /* HeapReference<Class> */ temp = obj->klass_
7723 GenerateReferenceLoadTwoRegisters(instruction,
7724 out_loc,
7725 obj_loc,
7726 class_offset,
7727 maybe_temp_loc,
7728 kWithoutReadBarrier);
7729
7730 GenerateBitstringTypeCheckCompare(instruction, out);
7731 __ Sltiu(out, out, 1);
7732 break;
7733 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007734 }
7735
7736 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007737
7738 if (slow_path != nullptr) {
7739 __ Bind(slow_path->GetExitLabel());
7740 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007741}
7742
7743void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007744 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007745 locations->SetOut(Location::ConstantLocation(constant));
7746}
7747
7748void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7749 // Will be generated at use site.
7750}
7751
7752void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007753 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007754 locations->SetOut(Location::ConstantLocation(constant));
7755}
7756
7757void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7758 // Will be generated at use site.
7759}
7760
7761void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7762 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7763 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7764}
7765
7766void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7767 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007768 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007769 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007770 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007771}
7772
7773void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7774 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7775 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007776 Location receiver = invoke->GetLocations()->InAt(0);
7777 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007778 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007779
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007780 // temp = object->GetClass();
7781 if (receiver.IsStackSlot()) {
7782 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7783 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7784 } else {
7785 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7786 }
7787 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007788 // Instead of simply (possibly) unpoisoning `temp` here, we should
7789 // emit a read barrier for the previous class reference load.
7790 // However this is not required in practice, as this is an
7791 // intermediate/temporary reference and because the current
7792 // concurrent copying collector keeps the from-space memory
7793 // intact/accessible until the end of the marking phase (the
7794 // concurrent copying collector may not in the future).
7795 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007796 __ LoadFromOffset(kLoadWord, temp, temp,
7797 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7798 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007799 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007800 // temp = temp->GetImtEntryAt(method_offset);
7801 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7802 // T9 = temp->GetEntryPoint();
7803 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
Lena Djokic3177e102018-02-28 11:32:40 +01007804 // Set the hidden argument.
7805 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7806 invoke->GetDexMethodIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007807 // T9();
7808 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007809 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007810 DCHECK(!codegen_->IsLeafMethod());
7811 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7812}
7813
7814void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007815 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7816 if (intrinsic.TryDispatch(invoke)) {
7817 return;
7818 }
7819
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007820 HandleInvoke(invoke);
7821}
7822
7823void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007824 // Explicit clinit checks triggered by static invokes must have been pruned by
7825 // art::PrepareForRegisterAllocation.
7826 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007827
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007828 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007829 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7830 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007831
Chris Larsen701566a2015-10-27 15:29:13 -07007832 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7833 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007834 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7835 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7836 }
Chris Larsen701566a2015-10-27 15:29:13 -07007837 return;
7838 }
7839
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007840 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007841
7842 // Add the extra input register if either the dex cache array base register
7843 // or the PC-relative base register for accessing literals is needed.
7844 if (has_extra_input) {
7845 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7846 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007847}
7848
Orion Hodsonac141392017-01-13 11:53:47 +00007849void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7850 HandleInvoke(invoke);
7851}
7852
7853void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7854 codegen_->GenerateInvokePolymorphicCall(invoke);
7855}
7856
Orion Hodson4c8e12e2018-05-18 08:33:20 +01007857void LocationsBuilderMIPS::VisitInvokeCustom(HInvokeCustom* invoke) {
7858 HandleInvoke(invoke);
7859}
7860
7861void InstructionCodeGeneratorMIPS::VisitInvokeCustom(HInvokeCustom* invoke) {
7862 codegen_->GenerateInvokeCustomCall(invoke);
7863}
7864
Chris Larsen701566a2015-10-27 15:29:13 -07007865static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007866 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007867 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7868 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007869 return true;
7870 }
7871 return false;
7872}
7873
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007874HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007875 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007876 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007877 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007878 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007879 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007880 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007881 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007882 case HLoadString::LoadKind::kJitBootImageAddress:
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007883 case HLoadString::LoadKind::kJitTableAddress:
7884 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007885 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007886 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007887 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007888 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007889 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007890}
7891
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007892HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7893 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007894 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007895 case HLoadClass::LoadKind::kInvalid:
7896 LOG(FATAL) << "UNREACHABLE";
7897 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007898 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007899 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007900 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007901 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007902 case HLoadClass::LoadKind::kBssEntry:
7903 DCHECK(!Runtime::Current()->UseJitCompilation());
7904 break;
Vladimir Marko8e524ad2018-07-13 10:27:43 +01007905 case HLoadClass::LoadKind::kJitBootImageAddress:
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007906 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007907 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007908 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007909 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007910 break;
7911 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007912 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007913}
7914
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007915Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7916 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007917 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007918 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007919 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7920 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7921 if (!invoke->GetLocations()->Intrinsified()) {
7922 return location.AsRegister<Register>();
7923 }
7924 // For intrinsics we allow any location, so it may be on the stack.
7925 if (!location.IsRegister()) {
7926 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7927 return temp;
7928 }
7929 // For register locations, check if the register was saved. If so, get it from the stack.
7930 // Note: There is a chance that the register was saved but not overwritten, so we could
7931 // save one load. However, since this is just an intrinsic slow path we prefer this
7932 // simple and more robust approach rather that trying to determine if that's the case.
7933 SlowPathCode* slow_path = GetCurrentSlowPath();
7934 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7935 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7936 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7937 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7938 return temp;
7939 }
7940 return location.AsRegister<Register>();
7941}
7942
Vladimir Markodc151b22015-10-15 18:02:30 +01007943HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7944 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007945 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007946 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007947}
7948
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007949void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7950 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007951 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007952 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007953 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7954 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007955 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007956 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7957 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007958 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7959 : ZERO;
7960
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007961 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007962 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007963 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007964 uint32_t offset =
7965 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007966 __ LoadFromOffset(kLoadWord,
7967 temp.AsRegister<Register>(),
7968 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007969 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007970 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007971 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007972 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007973 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007974 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007975 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7976 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007977 PcRelativePatchInfo* info_high = NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007978 PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007979 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007980 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007981 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7982 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007983 break;
7984 }
Vladimir Markob066d432018-01-03 13:14:37 +00007985 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007986 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00007987 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
7988 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
7989 Register temp_reg = temp.AsRegister<Register>();
7990 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7991 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
7992 break;
7993 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007994 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007995 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007996 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007997 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7998 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007999 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008000 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
8001 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008002 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01008003 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008004 case HInvokeStaticOrDirect::MethodLoadKind::kJitDirectAddress:
8005 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
8006 break;
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008007 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
8008 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
8009 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008010 }
8011 }
8012
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008013 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008014 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008015 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008016 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008017 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
8018 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01008019 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008020 T9,
8021 callee_method.AsRegister<Register>(),
8022 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07008023 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008024 // T9()
8025 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008026 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008027 break;
8028 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008029 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
8030
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008031 DCHECK(!IsLeafMethod());
8032}
8033
8034void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00008035 // Explicit clinit checks triggered by static invokes must have been pruned by
8036 // art::PrepareForRegisterAllocation.
8037 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008038
8039 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8040 return;
8041 }
8042
8043 LocationSummary* locations = invoke->GetLocations();
8044 codegen_->GenerateStaticOrDirectCall(invoke,
8045 locations->HasTemps()
8046 ? locations->GetTemp(0)
8047 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008048}
8049
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008050void CodeGeneratorMIPS::GenerateVirtualCall(
8051 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02008052 // Use the calling convention instead of the location of the receiver, as
8053 // intrinsics may have put the receiver in a different register. In the intrinsics
8054 // slow path, the arguments have been moved to the right place, so here we are
8055 // guaranteed that the receiver is the first register of the calling convention.
8056 InvokeDexCallingConvention calling_convention;
8057 Register receiver = calling_convention.GetRegisterAt(0);
8058
Chris Larsen3acee732015-11-18 13:31:08 -08008059 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008060 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
8061 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
8062 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07008063 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008064
8065 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02008066 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08008067 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08008068 // Instead of simply (possibly) unpoisoning `temp` here, we should
8069 // emit a read barrier for the previous class reference load.
8070 // However this is not required in practice, as this is an
8071 // intermediate/temporary reference and because the current
8072 // concurrent copying collector keeps the from-space memory
8073 // intact/accessible until the end of the marking phase (the
8074 // concurrent copying collector may not in the future).
8075 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008076 // temp = temp->GetMethodAt(method_offset);
8077 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8078 // T9 = temp->GetEntryPoint();
8079 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
8080 // T9();
8081 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008082 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008083 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08008084}
8085
8086void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
8087 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8088 return;
8089 }
8090
8091 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008092 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008093}
8094
8095void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00008096 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008097 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008098 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008099 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8100 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008101 return;
8102 }
Vladimir Marko41559982017-01-06 14:04:23 +00008103 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008104 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008105 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08008106 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
8107 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07008108 ? LocationSummary::kCallOnSlowPath
8109 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008110 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008111 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
8112 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
8113 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008114 switch (load_kind) {
8115 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008116 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008117 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008118 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008119 case HLoadClass::LoadKind::kJitBootImageAddress:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008120 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008121 break;
8122 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008123 if (has_irreducible_loops) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008124 if (load_kind != HLoadClass::LoadKind::kJitBootImageAddress) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008125 codegen_->ClobberRA();
8126 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008127 break;
8128 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008129 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008130 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008131 locations->SetInAt(0, Location::RequiresRegister());
8132 break;
8133 default:
8134 break;
8135 }
8136 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008137 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
8138 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8139 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008140 RegisterSet caller_saves = RegisterSet::Empty();
8141 InvokeRuntimeCallingConvention calling_convention;
8142 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8143 locations->SetCustomSlowPathCallerSaves(caller_saves);
8144 } else {
8145 // For non-Baker read barriers we have a temp-clobbering call.
8146 }
8147 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008148}
8149
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008150// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8151// move.
8152void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00008153 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008154 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00008155 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01008156 return;
8157 }
Vladimir Marko41559982017-01-06 14:04:23 +00008158 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008159
Vladimir Marko41559982017-01-06 14:04:23 +00008160 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008161 Location out_loc = locations->Out();
8162 Register out = out_loc.AsRegister<Register>();
8163 Register base_or_current_method_reg;
8164 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008165 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008166 switch (load_kind) {
8167 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008168 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008169 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008170 case HLoadClass::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008171 case HLoadClass::LoadKind::kJitBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008172 base_or_current_method_reg =
8173 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008174 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008175 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008176 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008177 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8178 break;
8179 default:
8180 base_or_current_method_reg = ZERO;
8181 break;
8182 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008183
Alexey Frunze15958152017-02-09 19:08:30 -08008184 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8185 ? kWithoutReadBarrier
8186 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008187 bool generate_null_check = false;
8188 switch (load_kind) {
8189 case HLoadClass::LoadKind::kReferrersClass: {
8190 DCHECK(!cls->CanCallRuntime());
8191 DCHECK(!cls->MustGenerateClinitCheck());
8192 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8193 GenerateGcRootFieldLoad(cls,
8194 out_loc,
8195 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008196 ArtMethod::DeclaringClassOffset().Int32Value(),
8197 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008198 break;
8199 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008200 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008201 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008202 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008203 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008204 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008205 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008206 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008207 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8208 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008209 base_or_current_method_reg);
8210 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008211 break;
8212 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008213 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008214 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008215 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008216 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008217 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008218 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008219 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008220 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8221 out,
8222 base_or_current_method_reg);
8223 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008224 break;
8225 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008226 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008227 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8228 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008229 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8230 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008231 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008232 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008233 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008234 GenerateGcRootFieldLoad(cls,
8235 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008236 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008237 /* placeholder */ 0x5678,
8238 read_barrier_option,
8239 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008240 generate_null_check = true;
8241 break;
8242 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008243 case HLoadClass::LoadKind::kJitBootImageAddress: {
8244 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
8245 uint32_t address = reinterpret_cast32<uint32_t>(cls->GetClass().Get());
8246 DCHECK_NE(address, 0u);
8247 if (isR6 || !has_irreducible_loops) {
8248 __ LoadLiteral(out,
8249 base_or_current_method_reg,
8250 codegen_->DeduplicateBootImageAddressLiteral(address));
8251 } else {
8252 __ LoadConst32(out, address);
8253 }
8254 break;
8255 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008256 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008257 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8258 cls->GetTypeIndex(),
8259 cls->GetClass());
8260 bool reordering = __ SetReorder(false);
8261 __ Bind(&info->high_label);
8262 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008263 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008264 GenerateGcRootFieldLoad(cls,
8265 out_loc,
8266 out,
8267 /* placeholder */ 0x5678,
8268 read_barrier_option,
8269 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008270 break;
8271 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008272 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008273 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008274 LOG(FATAL) << "UNREACHABLE";
8275 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008276 }
8277
8278 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8279 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008280 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00008281 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07008282 codegen_->AddSlowPath(slow_path);
8283 if (generate_null_check) {
8284 __ Beqz(out, slow_path->GetEntryLabel());
8285 }
8286 if (cls->MustGenerateClinitCheck()) {
8287 GenerateClassInitializationCheck(slow_path, out);
8288 } else {
8289 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008290 }
8291 }
8292}
8293
Orion Hodsondbaa5c72018-05-10 08:22:46 +01008294void LocationsBuilderMIPS::VisitLoadMethodHandle(HLoadMethodHandle* load) {
8295 InvokeRuntimeCallingConvention calling_convention;
8296 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8297 CodeGenerator::CreateLoadMethodHandleRuntimeCallLocationSummary(load, loc, loc);
8298}
8299
8300void InstructionCodeGeneratorMIPS::VisitLoadMethodHandle(HLoadMethodHandle* load) {
8301 codegen_->GenerateLoadMethodHandleRuntimeCall(load);
8302}
8303
Orion Hodson18259d72018-04-12 11:18:23 +01008304void LocationsBuilderMIPS::VisitLoadMethodType(HLoadMethodType* load) {
8305 InvokeRuntimeCallingConvention calling_convention;
8306 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8307 CodeGenerator::CreateLoadMethodTypeRuntimeCallLocationSummary(load, loc, loc);
8308}
8309
8310void InstructionCodeGeneratorMIPS::VisitLoadMethodType(HLoadMethodType* load) {
8311 codegen_->GenerateLoadMethodTypeRuntimeCall(load);
8312}
8313
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008314static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008315 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008316}
8317
8318void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8319 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008320 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008321 locations->SetOut(Location::RequiresRegister());
8322}
8323
8324void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8325 Register out = load->GetLocations()->Out().AsRegister<Register>();
8326 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8327}
8328
8329void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008330 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008331}
8332
8333void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8334 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8335}
8336
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008337void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008338 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008339 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008340 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008341 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008342 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008343 switch (load_kind) {
8344 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008345 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008346 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008347 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008348 case HLoadString::LoadKind::kJitBootImageAddress:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008349 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008350 break;
8351 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008352 if (has_irreducible_loops) {
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008353 if (load_kind != HLoadString::LoadKind::kJitBootImageAddress) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008354 codegen_->ClobberRA();
8355 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008356 break;
8357 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008358 FALLTHROUGH_INTENDED;
8359 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008360 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008361 locations->SetInAt(0, Location::RequiresRegister());
8362 break;
8363 default:
8364 break;
8365 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008366 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008367 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008368 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008369 } else {
8370 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008371 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8372 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8373 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008374 RegisterSet caller_saves = RegisterSet::Empty();
8375 InvokeRuntimeCallingConvention calling_convention;
8376 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8377 locations->SetCustomSlowPathCallerSaves(caller_saves);
8378 } else {
8379 // For non-Baker read barriers we have a temp-clobbering call.
8380 }
8381 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008382 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008383}
8384
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008385// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8386// move.
8387void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008388 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008389 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008390 Location out_loc = locations->Out();
8391 Register out = out_loc.AsRegister<Register>();
8392 Register base_or_current_method_reg;
8393 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008394 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008395 switch (load_kind) {
8396 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008397 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008398 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008399 case HLoadString::LoadKind::kBssEntry:
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008400 case HLoadString::LoadKind::kJitBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008401 base_or_current_method_reg =
8402 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008403 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008404 default:
8405 base_or_current_method_reg = ZERO;
8406 break;
8407 }
8408
8409 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008410 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008411 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008412 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008413 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008414 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008415 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008416 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8417 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008418 base_or_current_method_reg);
8419 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008420 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008421 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008422 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008423 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008424 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008425 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008426 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008427 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008428 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008429 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8430 out,
8431 base_or_current_method_reg);
8432 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8433 return;
8434 }
8435 case HLoadString::LoadKind::kBssEntry: {
8436 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8437 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8438 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8439 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8440 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008441 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008442 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008443 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008444 GenerateGcRootFieldLoad(load,
8445 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008446 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008447 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008448 kCompilerReadBarrierOption,
8449 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008450 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008451 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008452 codegen_->AddSlowPath(slow_path);
8453 __ Beqz(out, slow_path->GetEntryLabel());
8454 __ Bind(slow_path->GetExitLabel());
8455 return;
8456 }
Vladimir Marko8e524ad2018-07-13 10:27:43 +01008457 case HLoadString::LoadKind::kJitBootImageAddress: {
8458 uint32_t address = reinterpret_cast32<uint32_t>(load->GetString().Get());
8459 DCHECK_NE(address, 0u);
8460 if (isR6 || !has_irreducible_loops) {
8461 __ LoadLiteral(out,
8462 base_or_current_method_reg,
8463 codegen_->DeduplicateBootImageAddressLiteral(address));
8464 } else {
8465 __ LoadConst32(out, address);
8466 }
8467 return;
8468 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008469 case HLoadString::LoadKind::kJitTableAddress: {
8470 CodeGeneratorMIPS::JitPatchInfo* info =
8471 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8472 load->GetStringIndex(),
8473 load->GetString());
8474 bool reordering = __ SetReorder(false);
8475 __ Bind(&info->high_label);
8476 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008477 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008478 GenerateGcRootFieldLoad(load,
8479 out_loc,
8480 out,
8481 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008482 kCompilerReadBarrierOption,
8483 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008484 return;
8485 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008486 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008487 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008488 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008489
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008490 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008491 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008492 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008493 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008494 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008495 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8496 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008497}
8498
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008499void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008500 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008501 locations->SetOut(Location::ConstantLocation(constant));
8502}
8503
8504void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8505 // Will be generated at use site.
8506}
8507
8508void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008509 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8510 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008511 InvokeRuntimeCallingConvention calling_convention;
8512 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8513}
8514
8515void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8516 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008517 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008518 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8519 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008520 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008521 }
8522 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8523}
8524
8525void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8526 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008527 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008528 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008529 case DataType::Type::kInt32:
8530 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008531 locations->SetInAt(0, Location::RequiresRegister());
8532 locations->SetInAt(1, Location::RequiresRegister());
8533 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8534 break;
8535
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008536 case DataType::Type::kFloat32:
8537 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008538 locations->SetInAt(0, Location::RequiresFpuRegister());
8539 locations->SetInAt(1, Location::RequiresFpuRegister());
8540 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8541 break;
8542
8543 default:
8544 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8545 }
8546}
8547
8548void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008549 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008550 LocationSummary* locations = instruction->GetLocations();
8551 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8552
8553 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008554 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008555 Register dst = locations->Out().AsRegister<Register>();
8556 Register lhs = locations->InAt(0).AsRegister<Register>();
8557 Register rhs = locations->InAt(1).AsRegister<Register>();
8558
8559 if (isR6) {
8560 __ MulR6(dst, lhs, rhs);
8561 } else {
8562 __ MulR2(dst, lhs, rhs);
8563 }
8564 break;
8565 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008566 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008567 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8568 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8569 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8570 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8571 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8572 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8573
8574 // Extra checks to protect caused by the existance of A1_A2.
8575 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8576 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8577 DCHECK_NE(dst_high, lhs_low);
8578 DCHECK_NE(dst_high, rhs_low);
8579
8580 // A_B * C_D
8581 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8582 // dst_lo: [ low(B*D) ]
8583 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8584
8585 if (isR6) {
8586 __ MulR6(TMP, lhs_high, rhs_low);
8587 __ MulR6(dst_high, lhs_low, rhs_high);
8588 __ Addu(dst_high, dst_high, TMP);
8589 __ MuhuR6(TMP, lhs_low, rhs_low);
8590 __ Addu(dst_high, dst_high, TMP);
8591 __ MulR6(dst_low, lhs_low, rhs_low);
8592 } else {
8593 __ MulR2(TMP, lhs_high, rhs_low);
8594 __ MulR2(dst_high, lhs_low, rhs_high);
8595 __ Addu(dst_high, dst_high, TMP);
8596 __ MultuR2(lhs_low, rhs_low);
8597 __ Mfhi(TMP);
8598 __ Addu(dst_high, dst_high, TMP);
8599 __ Mflo(dst_low);
8600 }
8601 break;
8602 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008603 case DataType::Type::kFloat32:
8604 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008605 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8606 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8607 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008608 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008609 __ MulS(dst, lhs, rhs);
8610 } else {
8611 __ MulD(dst, lhs, rhs);
8612 }
8613 break;
8614 }
8615 default:
8616 LOG(FATAL) << "Unexpected mul type " << type;
8617 }
8618}
8619
8620void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8621 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008622 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008623 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008624 case DataType::Type::kInt32:
8625 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008626 locations->SetInAt(0, Location::RequiresRegister());
8627 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8628 break;
8629
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008630 case DataType::Type::kFloat32:
8631 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008632 locations->SetInAt(0, Location::RequiresFpuRegister());
8633 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8634 break;
8635
8636 default:
8637 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8638 }
8639}
8640
8641void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008642 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008643 LocationSummary* locations = instruction->GetLocations();
8644
8645 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008646 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008647 Register dst = locations->Out().AsRegister<Register>();
8648 Register src = locations->InAt(0).AsRegister<Register>();
8649 __ Subu(dst, ZERO, src);
8650 break;
8651 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008652 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008653 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8654 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8655 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8656 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8657 __ Subu(dst_low, ZERO, src_low);
8658 __ Sltu(TMP, ZERO, dst_low);
8659 __ Subu(dst_high, ZERO, src_high);
8660 __ Subu(dst_high, dst_high, TMP);
8661 break;
8662 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008663 case DataType::Type::kFloat32:
8664 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008665 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8666 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008667 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008668 __ NegS(dst, src);
8669 } else {
8670 __ NegD(dst, src);
8671 }
8672 break;
8673 }
8674 default:
8675 LOG(FATAL) << "Unexpected neg type " << type;
8676 }
8677}
8678
8679void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008680 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8681 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008682 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008683 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008684 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8685 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008686}
8687
8688void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008689 // Note: if heap poisoning is enabled, the entry point takes care
8690 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008691 QuickEntrypointEnum entrypoint =
8692 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8693 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008694 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008695 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008696}
8697
8698void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008699 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8700 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008701 InvokeRuntimeCallingConvention calling_convention;
Alex Lightd109e302018-06-27 10:25:41 -07008702 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008703 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008704}
8705
8706void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alex Lightd109e302018-06-27 10:25:41 -07008707 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
8708 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008709}
8710
8711void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008712 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008713 locations->SetInAt(0, Location::RequiresRegister());
8714 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8715}
8716
8717void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008718 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008719 LocationSummary* locations = instruction->GetLocations();
8720
8721 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008722 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008723 Register dst = locations->Out().AsRegister<Register>();
8724 Register src = locations->InAt(0).AsRegister<Register>();
8725 __ Nor(dst, src, ZERO);
8726 break;
8727 }
8728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008729 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008730 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8731 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8732 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8733 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8734 __ Nor(dst_high, src_high, ZERO);
8735 __ Nor(dst_low, src_low, ZERO);
8736 break;
8737 }
8738
8739 default:
8740 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8741 }
8742}
8743
8744void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008745 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008746 locations->SetInAt(0, Location::RequiresRegister());
8747 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8748}
8749
8750void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8751 LocationSummary* locations = instruction->GetLocations();
8752 __ Xori(locations->Out().AsRegister<Register>(),
8753 locations->InAt(0).AsRegister<Register>(),
8754 1);
8755}
8756
8757void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008758 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8759 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008760}
8761
Calin Juravle2ae48182016-03-16 14:05:09 +00008762void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8763 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008764 return;
8765 }
8766 Location obj = instruction->GetLocations()->InAt(0);
8767
8768 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008769 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008770}
8771
Calin Juravle2ae48182016-03-16 14:05:09 +00008772void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008773 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008774 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008775
8776 Location obj = instruction->GetLocations()->InAt(0);
8777
8778 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8779}
8780
8781void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008782 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008783}
8784
8785void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8786 HandleBinaryOp(instruction);
8787}
8788
8789void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8790 HandleBinaryOp(instruction);
8791}
8792
8793void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8794 LOG(FATAL) << "Unreachable";
8795}
8796
8797void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008798 if (instruction->GetNext()->IsSuspendCheck() &&
8799 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8800 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8801 // The back edge will generate the suspend check.
8802 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8803 }
8804
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008805 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8806}
8807
8808void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008809 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008810 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8811 if (location.IsStackSlot()) {
8812 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8813 } else if (location.IsDoubleStackSlot()) {
8814 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8815 }
8816 locations->SetOut(location);
8817}
8818
8819void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8820 ATTRIBUTE_UNUSED) {
8821 // Nothing to do, the parameter is already at its location.
8822}
8823
8824void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8825 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008826 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008827 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8828}
8829
8830void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8831 ATTRIBUTE_UNUSED) {
8832 // Nothing to do, the method is already at its location.
8833}
8834
8835void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008836 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008837 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008838 locations->SetInAt(i, Location::Any());
8839 }
8840 locations->SetOut(Location::Any());
8841}
8842
8843void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8844 LOG(FATAL) << "Unreachable";
8845}
8846
8847void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008848 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008849 bool call_rem;
8850 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8851 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8852 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8853 } else {
8854 call_rem = (type != DataType::Type::kInt32);
8855 }
8856 LocationSummary::CallKind call_kind = call_rem
8857 ? LocationSummary::kCallOnMainOnly
8858 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008859 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008860
8861 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008862 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008863 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008864 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008865 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8866 break;
8867
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008868 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008869 if (call_rem) {
8870 InvokeRuntimeCallingConvention calling_convention;
8871 locations->SetInAt(0, Location::RegisterPairLocation(
8872 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8873 locations->SetInAt(1, Location::RegisterPairLocation(
8874 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8875 locations->SetOut(calling_convention.GetReturnLocation(type));
8876 } else {
8877 locations->SetInAt(0, Location::RequiresRegister());
8878 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8879 locations->SetOut(Location::RequiresRegister());
8880 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008881 break;
8882 }
8883
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008884 case DataType::Type::kFloat32:
8885 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008886 InvokeRuntimeCallingConvention calling_convention;
8887 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8888 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8889 locations->SetOut(calling_convention.GetReturnLocation(type));
8890 break;
8891 }
8892
8893 default:
8894 LOG(FATAL) << "Unexpected rem type " << type;
8895 }
8896}
8897
8898void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008899 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008900 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008901
8902 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008903 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008904 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008905 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008906 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008907 if (locations->InAt(1).IsConstant()) {
8908 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8909 if (imm == 0) {
8910 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8911 } else if (imm == 1 || imm == -1) {
8912 DivRemOneOrMinusOne(instruction);
8913 } else {
8914 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8915 DivRemByPowerOfTwo(instruction);
8916 }
8917 } else {
8918 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8919 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8920 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008921 break;
8922 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008923 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008924 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008925 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008926 break;
8927 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008928 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008929 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008930 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008931 break;
8932 }
8933 default:
8934 LOG(FATAL) << "Unexpected rem type " << type;
8935 }
8936}
8937
Aart Bik1f8d51b2018-02-15 10:42:37 -08008938static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
8939 LocationSummary* locations = new (allocator) LocationSummary(minmax);
8940 switch (minmax->GetResultType()) {
8941 case DataType::Type::kInt32:
8942 case DataType::Type::kInt64:
8943 locations->SetInAt(0, Location::RequiresRegister());
8944 locations->SetInAt(1, Location::RequiresRegister());
8945 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8946 break;
8947 case DataType::Type::kFloat32:
8948 case DataType::Type::kFloat64:
8949 locations->SetInAt(0, Location::RequiresFpuRegister());
8950 locations->SetInAt(1, Location::RequiresFpuRegister());
8951 locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap);
8952 break;
8953 default:
8954 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
8955 }
8956}
8957
Aart Bik351df3e2018-03-07 11:54:57 -08008958void InstructionCodeGeneratorMIPS::GenerateMinMaxInt(LocationSummary* locations,
8959 bool is_min,
8960 bool isR6,
8961 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08008962 if (isR6) {
8963 // Some architectures, such as ARM and MIPS (prior to r6), have a
8964 // conditional move instruction which only changes the target
8965 // (output) register if the condition is true (MIPS prior to r6 had
8966 // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions
8967 // always change the target (output) register. If the condition is
8968 // true the output register gets the contents of the "rs" register;
8969 // otherwise, the output register is set to zero. One consequence
8970 // of this is that to implement something like "rd = c==0 ? rs : rt"
8971 // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions.
8972 // After executing this pair of instructions one of the output
8973 // registers from the pair will necessarily contain zero. Then the
8974 // code ORs the output registers from the SELEQZ/SELNEZ instructions
8975 // to get the final result.
8976 //
8977 // The initial test to see if the output register is same as the
8978 // first input register is needed to make sure that value in the
8979 // first input register isn't clobbered before we've finished
8980 // computing the output value. The logic in the corresponding else
8981 // clause performs the same task but makes sure the second input
8982 // register isn't clobbered in the event that it's the same register
8983 // as the output register; the else clause also handles the case
8984 // where the output register is distinct from both the first, and the
8985 // second input registers.
8986 if (type == DataType::Type::kInt64) {
8987 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8988 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8989 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
8990 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
8991 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8992 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8993
8994 MipsLabel compare_done;
8995
8996 if (a_lo == b_lo) {
8997 if (out_lo != a_lo) {
8998 __ Move(out_lo, a_lo);
8999 __ Move(out_hi, a_hi);
9000 }
9001 } else {
9002 __ Slt(TMP, b_hi, a_hi);
9003 __ Bne(b_hi, a_hi, &compare_done);
9004
9005 __ Sltu(TMP, b_lo, a_lo);
9006
9007 __ Bind(&compare_done);
9008
9009 if (is_min) {
9010 __ Seleqz(AT, a_lo, TMP);
9011 __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo
9012 // because at this point we're
9013 // done using a_lo/b_lo.
9014 } else {
9015 __ Selnez(AT, a_lo, TMP);
9016 __ Seleqz(out_lo, b_lo, TMP); // ditto
9017 }
9018 __ Or(out_lo, out_lo, AT);
9019 if (is_min) {
9020 __ Seleqz(AT, a_hi, TMP);
9021 __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
9022 } else {
9023 __ Selnez(AT, a_hi, TMP);
9024 __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
9025 }
9026 __ Or(out_hi, out_hi, AT);
9027 }
9028 } else {
9029 DCHECK_EQ(type, DataType::Type::kInt32);
9030 Register a = locations->InAt(0).AsRegister<Register>();
9031 Register b = locations->InAt(1).AsRegister<Register>();
9032 Register out = locations->Out().AsRegister<Register>();
9033
9034 if (a == b) {
9035 if (out != a) {
9036 __ Move(out, a);
9037 }
9038 } else {
9039 __ Slt(AT, b, a);
9040 if (is_min) {
9041 __ Seleqz(TMP, a, AT);
9042 __ Selnez(AT, b, AT);
9043 } else {
9044 __ Selnez(TMP, a, AT);
9045 __ Seleqz(AT, b, AT);
9046 }
9047 __ Or(out, TMP, AT);
9048 }
9049 }
9050 } else { // !isR6
9051 if (type == DataType::Type::kInt64) {
9052 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9053 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9054 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
9055 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
9056 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9057 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9058
9059 MipsLabel compare_done;
9060
9061 if (a_lo == b_lo) {
9062 if (out_lo != a_lo) {
9063 __ Move(out_lo, a_lo);
9064 __ Move(out_hi, a_hi);
9065 }
9066 } else {
9067 __ Slt(TMP, a_hi, b_hi);
9068 __ Bne(a_hi, b_hi, &compare_done);
9069
9070 __ Sltu(TMP, a_lo, b_lo);
9071
9072 __ Bind(&compare_done);
9073
9074 if (is_min) {
9075 if (out_lo != a_lo) {
9076 __ Movn(out_hi, a_hi, TMP);
9077 __ Movn(out_lo, a_lo, TMP);
9078 }
9079 if (out_lo != b_lo) {
9080 __ Movz(out_hi, b_hi, TMP);
9081 __ Movz(out_lo, b_lo, TMP);
9082 }
9083 } else {
9084 if (out_lo != a_lo) {
9085 __ Movz(out_hi, a_hi, TMP);
9086 __ Movz(out_lo, a_lo, TMP);
9087 }
9088 if (out_lo != b_lo) {
9089 __ Movn(out_hi, b_hi, TMP);
9090 __ Movn(out_lo, b_lo, TMP);
9091 }
9092 }
9093 }
9094 } else {
9095 DCHECK_EQ(type, DataType::Type::kInt32);
9096 Register a = locations->InAt(0).AsRegister<Register>();
9097 Register b = locations->InAt(1).AsRegister<Register>();
9098 Register out = locations->Out().AsRegister<Register>();
9099
9100 if (a == b) {
9101 if (out != a) {
9102 __ Move(out, a);
9103 }
9104 } else {
9105 __ Slt(AT, a, b);
9106 if (is_min) {
9107 if (out != a) {
9108 __ Movn(out, a, AT);
9109 }
9110 if (out != b) {
9111 __ Movz(out, b, AT);
9112 }
9113 } else {
9114 if (out != a) {
9115 __ Movz(out, a, AT);
9116 }
9117 if (out != b) {
9118 __ Movn(out, b, AT);
9119 }
9120 }
9121 }
9122 }
9123 }
9124}
9125
9126void InstructionCodeGeneratorMIPS::GenerateMinMaxFP(LocationSummary* locations,
9127 bool is_min,
9128 bool isR6,
9129 DataType::Type type) {
9130 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9131 FRegister a = locations->InAt(0).AsFpuRegister<FRegister>();
9132 FRegister b = locations->InAt(1).AsFpuRegister<FRegister>();
9133
9134 if (isR6) {
9135 MipsLabel noNaNs;
9136 MipsLabel done;
9137 FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
9138
9139 // When Java computes min/max it prefers a NaN to a number; the
9140 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
9141 // the inputs is a NaN and the other is a valid number, the MIPS
9142 // instruction will return the number; Java wants the NaN value
9143 // returned. This is why there is extra logic preceding the use of
9144 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
9145 // NaN, return the NaN, otherwise return the min/max.
9146 if (type == DataType::Type::kFloat64) {
9147 __ CmpUnD(FTMP, a, b);
9148 __ Bc1eqz(FTMP, &noNaNs);
9149
9150 // One of the inputs is a NaN
9151 __ CmpEqD(ftmp, a, a);
9152 // If a == a then b is the NaN, otherwise a is the NaN.
9153 __ SelD(ftmp, a, b);
9154
9155 if (ftmp != out) {
9156 __ MovD(out, ftmp);
9157 }
9158
9159 __ B(&done);
9160
9161 __ Bind(&noNaNs);
9162
9163 if (is_min) {
9164 __ MinD(out, a, b);
9165 } else {
9166 __ MaxD(out, a, b);
9167 }
9168 } else {
9169 DCHECK_EQ(type, DataType::Type::kFloat32);
9170 __ CmpUnS(FTMP, a, b);
9171 __ Bc1eqz(FTMP, &noNaNs);
9172
9173 // One of the inputs is a NaN
9174 __ CmpEqS(ftmp, a, a);
9175 // If a == a then b is the NaN, otherwise a is the NaN.
9176 __ SelS(ftmp, a, b);
9177
9178 if (ftmp != out) {
9179 __ MovS(out, ftmp);
9180 }
9181
9182 __ B(&done);
9183
9184 __ Bind(&noNaNs);
9185
9186 if (is_min) {
9187 __ MinS(out, a, b);
9188 } else {
9189 __ MaxS(out, a, b);
9190 }
9191 }
9192
9193 __ Bind(&done);
9194
9195 } else { // !isR6
9196 MipsLabel ordered;
9197 MipsLabel compare;
9198 MipsLabel select;
9199 MipsLabel done;
9200
9201 if (type == DataType::Type::kFloat64) {
9202 __ CunD(a, b);
9203 } else {
9204 DCHECK_EQ(type, DataType::Type::kFloat32);
9205 __ CunS(a, b);
9206 }
9207 __ Bc1f(&ordered);
9208
9209 // a or b (or both) is a NaN. Return one, which is a NaN.
9210 if (type == DataType::Type::kFloat64) {
9211 __ CeqD(b, b);
9212 } else {
9213 __ CeqS(b, b);
9214 }
9215 __ B(&select);
9216
9217 __ Bind(&ordered);
9218
9219 // Neither is a NaN.
9220 // a == b? (-0.0 compares equal with +0.0)
9221 // If equal, handle zeroes, else compare further.
9222 if (type == DataType::Type::kFloat64) {
9223 __ CeqD(a, b);
9224 } else {
9225 __ CeqS(a, b);
9226 }
9227 __ Bc1f(&compare);
9228
9229 // a == b either bit for bit or one is -0.0 and the other is +0.0.
9230 if (type == DataType::Type::kFloat64) {
9231 __ MoveFromFpuHigh(TMP, a);
9232 __ MoveFromFpuHigh(AT, b);
9233 } else {
9234 __ Mfc1(TMP, a);
9235 __ Mfc1(AT, b);
9236 }
9237
9238 if (is_min) {
9239 // -0.0 prevails over +0.0.
9240 __ Or(TMP, TMP, AT);
9241 } else {
9242 // +0.0 prevails over -0.0.
9243 __ And(TMP, TMP, AT);
9244 }
9245
9246 if (type == DataType::Type::kFloat64) {
9247 __ Mfc1(AT, a);
9248 __ Mtc1(AT, out);
9249 __ MoveToFpuHigh(TMP, out);
9250 } else {
9251 __ Mtc1(TMP, out);
9252 }
9253 __ B(&done);
9254
9255 __ Bind(&compare);
9256
9257 if (type == DataType::Type::kFloat64) {
9258 if (is_min) {
9259 // return (a <= b) ? a : b;
9260 __ ColeD(a, b);
9261 } else {
9262 // return (a >= b) ? a : b;
9263 __ ColeD(b, a); // b <= a
9264 }
9265 } else {
9266 if (is_min) {
9267 // return (a <= b) ? a : b;
9268 __ ColeS(a, b);
9269 } else {
9270 // return (a >= b) ? a : b;
9271 __ ColeS(b, a); // b <= a
9272 }
9273 }
9274
9275 __ Bind(&select);
9276
9277 if (type == DataType::Type::kFloat64) {
9278 __ MovtD(out, a);
9279 __ MovfD(out, b);
9280 } else {
9281 __ MovtS(out, a);
9282 __ MovfS(out, b);
9283 }
9284
9285 __ Bind(&done);
9286 }
9287}
9288
Aart Bik351df3e2018-03-07 11:54:57 -08009289void InstructionCodeGeneratorMIPS::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
9290 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9291 DataType::Type type = minmax->GetResultType();
9292 switch (type) {
9293 case DataType::Type::kInt32:
9294 case DataType::Type::kInt64:
9295 GenerateMinMaxInt(minmax->GetLocations(), is_min, isR6, type);
9296 break;
9297 case DataType::Type::kFloat32:
9298 case DataType::Type::kFloat64:
9299 GenerateMinMaxFP(minmax->GetLocations(), is_min, isR6, type);
9300 break;
9301 default:
9302 LOG(FATAL) << "Unexpected type for HMinMax " << type;
9303 }
9304}
9305
Aart Bik1f8d51b2018-02-15 10:42:37 -08009306void LocationsBuilderMIPS::VisitMin(HMin* min) {
9307 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
9308}
9309
9310void InstructionCodeGeneratorMIPS::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08009311 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009312}
9313
9314void LocationsBuilderMIPS::VisitMax(HMax* max) {
9315 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
9316}
9317
9318void InstructionCodeGeneratorMIPS::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08009319 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009320}
9321
Aart Bik3dad3412018-02-28 12:01:46 -08009322void LocationsBuilderMIPS::VisitAbs(HAbs* abs) {
9323 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
9324 switch (abs->GetResultType()) {
9325 case DataType::Type::kInt32:
9326 case DataType::Type::kInt64:
9327 locations->SetInAt(0, Location::RequiresRegister());
9328 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9329 break;
9330 case DataType::Type::kFloat32:
9331 case DataType::Type::kFloat64:
9332 locations->SetInAt(0, Location::RequiresFpuRegister());
9333 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9334 break;
9335 default:
9336 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9337 }
9338}
9339
9340void InstructionCodeGeneratorMIPS::GenerateAbsFP(LocationSummary* locations,
9341 DataType::Type type,
9342 bool isR2OrNewer,
9343 bool isR6) {
9344 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
9345 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9346
9347 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
9348 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
9349 // (signaling NaN may become quiet though).
9350 //
9351 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
9352 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
9353 // affected by this instruction.
9354 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
9355 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
9356 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
9357 if (isR6) {
9358 if (type == DataType::Type::kFloat64) {
9359 __ AbsD(out, in);
9360 } else {
9361 DCHECK_EQ(type, DataType::Type::kFloat32);
9362 __ AbsS(out, in);
9363 }
9364 } else {
9365 if (type == DataType::Type::kFloat64) {
9366 if (in != out) {
9367 __ MovD(out, in);
9368 }
9369 __ MoveFromFpuHigh(TMP, in);
9370 // ins instruction is not available for R1.
9371 if (isR2OrNewer) {
9372 __ Ins(TMP, ZERO, 31, 1);
9373 } else {
9374 __ Sll(TMP, TMP, 1);
9375 __ Srl(TMP, TMP, 1);
9376 }
9377 __ MoveToFpuHigh(TMP, out);
9378 } else {
9379 DCHECK_EQ(type, DataType::Type::kFloat32);
9380 __ Mfc1(TMP, in);
9381 // ins instruction is not available for R1.
9382 if (isR2OrNewer) {
9383 __ Ins(TMP, ZERO, 31, 1);
9384 } else {
9385 __ Sll(TMP, TMP, 1);
9386 __ Srl(TMP, TMP, 1);
9387 }
9388 __ Mtc1(TMP, out);
9389 }
9390 }
9391}
9392
9393void InstructionCodeGeneratorMIPS::VisitAbs(HAbs* abs) {
9394 LocationSummary* locations = abs->GetLocations();
9395 bool isR2OrNewer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
9396 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9397 switch (abs->GetResultType()) {
9398 case DataType::Type::kInt32: {
9399 Register in = locations->InAt(0).AsRegister<Register>();
9400 Register out = locations->Out().AsRegister<Register>();
9401 __ Sra(AT, in, 31);
9402 __ Xor(out, in, AT);
9403 __ Subu(out, out, AT);
9404 break;
9405 }
9406 case DataType::Type::kInt64: {
9407 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9408 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9409 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9410 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9411 // The comments in this section show the analogous operations which would
9412 // be performed if we had 64-bit registers "in", and "out".
9413 // __ Dsra32(AT, in, 31);
9414 __ Sra(AT, in_hi, 31);
9415 // __ Xor(out, in, AT);
9416 __ Xor(TMP, in_lo, AT);
9417 __ Xor(out_hi, in_hi, AT);
9418 // __ Dsubu(out, out, AT);
9419 __ Subu(out_lo, TMP, AT);
9420 __ Sltu(TMP, out_lo, TMP);
9421 __ Addu(out_hi, out_hi, TMP);
9422 break;
9423 }
9424 case DataType::Type::kFloat32:
9425 case DataType::Type::kFloat64:
9426 GenerateAbsFP(locations, abs->GetResultType(), isR2OrNewer, isR6);
9427 break;
9428 default:
9429 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9430 }
9431}
9432
Igor Murashkind01745e2017-04-05 16:40:31 -07009433void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
9434 constructor_fence->SetLocations(nullptr);
9435}
9436
9437void InstructionCodeGeneratorMIPS::VisitConstructorFence(
9438 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
9439 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
9440}
9441
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009442void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9443 memory_barrier->SetLocations(nullptr);
9444}
9445
9446void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9447 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
9448}
9449
9450void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009451 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009452 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009453 locations->SetInAt(0, MipsReturnLocation(return_type));
9454}
9455
9456void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
9457 codegen_->GenerateFrameExit();
9458}
9459
9460void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
9461 ret->SetLocations(nullptr);
9462}
9463
9464void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
9465 codegen_->GenerateFrameExit();
9466}
9467
Alexey Frunze92d90602015-12-18 18:16:36 -08009468void LocationsBuilderMIPS::VisitRor(HRor* ror) {
9469 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009470}
9471
Alexey Frunze92d90602015-12-18 18:16:36 -08009472void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
9473 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009474}
9475
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009476void LocationsBuilderMIPS::VisitShl(HShl* shl) {
9477 HandleShift(shl);
9478}
9479
9480void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
9481 HandleShift(shl);
9482}
9483
9484void LocationsBuilderMIPS::VisitShr(HShr* shr) {
9485 HandleShift(shr);
9486}
9487
9488void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
9489 HandleShift(shr);
9490}
9491
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009492void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
9493 HandleBinaryOp(instruction);
9494}
9495
9496void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
9497 HandleBinaryOp(instruction);
9498}
9499
9500void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9501 HandleFieldGet(instruction, instruction->GetFieldInfo());
9502}
9503
9504void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9505 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
9506}
9507
9508void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
9509 HandleFieldSet(instruction, instruction->GetFieldInfo());
9510}
9511
9512void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01009513 HandleFieldSet(instruction,
9514 instruction->GetFieldInfo(),
9515 instruction->GetDexPc(),
9516 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009517}
9518
9519void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
9520 HUnresolvedInstanceFieldGet* instruction) {
9521 FieldAccessCallingConventionMIPS calling_convention;
9522 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9523 instruction->GetFieldType(),
9524 calling_convention);
9525}
9526
9527void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
9528 HUnresolvedInstanceFieldGet* instruction) {
9529 FieldAccessCallingConventionMIPS calling_convention;
9530 codegen_->GenerateUnresolvedFieldAccess(instruction,
9531 instruction->GetFieldType(),
9532 instruction->GetFieldIndex(),
9533 instruction->GetDexPc(),
9534 calling_convention);
9535}
9536
9537void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
9538 HUnresolvedInstanceFieldSet* instruction) {
9539 FieldAccessCallingConventionMIPS calling_convention;
9540 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9541 instruction->GetFieldType(),
9542 calling_convention);
9543}
9544
9545void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
9546 HUnresolvedInstanceFieldSet* instruction) {
9547 FieldAccessCallingConventionMIPS calling_convention;
9548 codegen_->GenerateUnresolvedFieldAccess(instruction,
9549 instruction->GetFieldType(),
9550 instruction->GetFieldIndex(),
9551 instruction->GetDexPc(),
9552 calling_convention);
9553}
9554
9555void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
9556 HUnresolvedStaticFieldGet* instruction) {
9557 FieldAccessCallingConventionMIPS calling_convention;
9558 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9559 instruction->GetFieldType(),
9560 calling_convention);
9561}
9562
9563void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
9564 HUnresolvedStaticFieldGet* instruction) {
9565 FieldAccessCallingConventionMIPS calling_convention;
9566 codegen_->GenerateUnresolvedFieldAccess(instruction,
9567 instruction->GetFieldType(),
9568 instruction->GetFieldIndex(),
9569 instruction->GetDexPc(),
9570 calling_convention);
9571}
9572
9573void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
9574 HUnresolvedStaticFieldSet* instruction) {
9575 FieldAccessCallingConventionMIPS calling_convention;
9576 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9577 instruction->GetFieldType(),
9578 calling_convention);
9579}
9580
9581void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9582 HUnresolvedStaticFieldSet* instruction) {
9583 FieldAccessCallingConventionMIPS calling_convention;
9584 codegen_->GenerateUnresolvedFieldAccess(instruction,
9585 instruction->GetFieldType(),
9586 instruction->GetFieldIndex(),
9587 instruction->GetDexPc(),
9588 calling_convention);
9589}
9590
9591void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009592 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9593 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009594 // In suspend check slow path, usually there are no caller-save registers at all.
9595 // If SIMD instructions are present, however, we force spilling all live SIMD
9596 // registers in full width (since the runtime only saves/restores lower part).
9597 locations->SetCustomSlowPathCallerSaves(
9598 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009599}
9600
9601void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9602 HBasicBlock* block = instruction->GetBlock();
9603 if (block->GetLoopInformation() != nullptr) {
9604 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9605 // The back edge will generate the suspend check.
9606 return;
9607 }
9608 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9609 // The goto will generate the suspend check.
9610 return;
9611 }
9612 GenerateSuspendCheck(instruction, nullptr);
9613}
9614
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009615void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009616 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9617 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009618 InvokeRuntimeCallingConvention calling_convention;
9619 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9620}
9621
9622void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009623 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009624 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9625}
9626
9627void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009628 DataType::Type input_type = conversion->GetInputType();
9629 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009630 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9631 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009632 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009633
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009634 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9635 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009636 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9637 }
9638
9639 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009640 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009641 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9642 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009643 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009644 }
9645
Vladimir Markoca6fff82017-10-03 14:49:14 +01009646 LocationSummary* locations =
9647 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009648
9649 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009650 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009651 locations->SetInAt(0, Location::RequiresFpuRegister());
9652 } else {
9653 locations->SetInAt(0, Location::RequiresRegister());
9654 }
9655
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009656 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009657 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9658 } else {
9659 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9660 }
9661 } else {
9662 InvokeRuntimeCallingConvention calling_convention;
9663
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009664 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009665 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9666 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009667 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009668 locations->SetInAt(0, Location::RegisterPairLocation(
9669 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9670 }
9671
9672 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9673 }
9674}
9675
9676void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9677 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009678 DataType::Type result_type = conversion->GetResultType();
9679 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009680 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009681 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009682
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009683 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9684 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009686 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009687 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9688 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9689 Register src = locations->InAt(0).AsRegister<Register>();
9690
Alexey Frunzea871ef12016-06-27 15:20:11 -07009691 if (dst_low != src) {
9692 __ Move(dst_low, src);
9693 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009694 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009695 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009696 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009697 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009698 ? locations->InAt(0).AsRegisterPairLow<Register>()
9699 : locations->InAt(0).AsRegister<Register>();
9700
9701 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009702 case DataType::Type::kUint8:
9703 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009704 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009705 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009706 if (has_sign_extension) {
9707 __ Seb(dst, src);
9708 } else {
9709 __ Sll(dst, src, 24);
9710 __ Sra(dst, dst, 24);
9711 }
9712 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009713 case DataType::Type::kUint16:
9714 __ Andi(dst, src, 0xFFFF);
9715 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009716 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009717 if (has_sign_extension) {
9718 __ Seh(dst, src);
9719 } else {
9720 __ Sll(dst, src, 16);
9721 __ Sra(dst, dst, 16);
9722 }
9723 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009724 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009725 if (dst != src) {
9726 __ Move(dst, src);
9727 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009728 break;
9729
9730 default:
9731 LOG(FATAL) << "Unexpected type conversion from " << input_type
9732 << " to " << result_type;
9733 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009734 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9735 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009736 if (isR6) {
9737 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9738 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9739 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9740 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9741 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9742 __ Mtc1(src_low, FTMP);
9743 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009744 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009745 __ Cvtsl(dst, FTMP);
9746 } else {
9747 __ Cvtdl(dst, FTMP);
9748 }
9749 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009750 QuickEntrypointEnum entrypoint =
9751 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009752 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009753 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009754 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9755 } else {
9756 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9757 }
9758 }
9759 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009760 Register src = locations->InAt(0).AsRegister<Register>();
9761 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9762 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009763 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009764 __ Cvtsw(dst, FTMP);
9765 } else {
9766 __ Cvtdw(dst, FTMP);
9767 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009768 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009769 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9770 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009771
9772 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9773 // value of the output type if the input is outside of the range after the truncation or
9774 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9775 // results. This matches the desired float/double-to-int/long conversion exactly.
9776 //
9777 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9778 // value when the input is either a NaN or is outside of the range of the output type
9779 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9780 // the same result.
9781 //
9782 // The code takes care of the different behaviors by first comparing the input to the
9783 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9784 // If the input is greater than or equal to the minimum, it procedes to the truncate
9785 // instruction, which will handle such an input the same way irrespective of NAN2008.
9786 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9787 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009788 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009789 if (isR6) {
9790 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9791 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9792 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9793 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9794 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009795
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009796 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009797 __ TruncLS(FTMP, src);
9798 } else {
9799 __ TruncLD(FTMP, src);
9800 }
9801 __ Mfc1(dst_low, FTMP);
9802 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009803 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009804 QuickEntrypointEnum entrypoint =
9805 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009806 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009807 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009808 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9809 } else {
9810 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9811 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009812 }
9813 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009814 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9815 Register dst = locations->Out().AsRegister<Register>();
9816 MipsLabel truncate;
9817 MipsLabel done;
9818
Lena Djokicf4e23a82017-05-09 15:43:45 +02009819 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009820 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009821 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9822 __ LoadConst32(TMP, min_val);
9823 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009824 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009825 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9826 __ LoadConst32(TMP, High32Bits(min_val));
9827 __ Mtc1(ZERO, FTMP);
9828 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009829 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009830
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009831 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009832 __ ColeS(0, FTMP, src);
9833 } else {
9834 __ ColeD(0, FTMP, src);
9835 }
9836 __ Bc1t(0, &truncate);
9837
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009838 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009839 __ CeqS(0, src, src);
9840 } else {
9841 __ CeqD(0, src, src);
9842 }
9843 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9844 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009845
9846 __ B(&done);
9847
9848 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009849 }
9850
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009851 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009852 __ TruncWS(FTMP, src);
9853 } else {
9854 __ TruncWD(FTMP, src);
9855 }
9856 __ Mfc1(dst, FTMP);
9857
Lena Djokicf4e23a82017-05-09 15:43:45 +02009858 if (!isR6) {
9859 __ Bind(&done);
9860 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009861 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009862 } else if (DataType::IsFloatingPointType(result_type) &&
9863 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009864 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9865 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009866 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009867 __ Cvtsd(dst, src);
9868 } else {
9869 __ Cvtds(dst, src);
9870 }
9871 } else {
9872 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9873 << " to " << result_type;
9874 }
9875}
9876
9877void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9878 HandleShift(ushr);
9879}
9880
9881void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9882 HandleShift(ushr);
9883}
9884
9885void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9886 HandleBinaryOp(instruction);
9887}
9888
9889void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9890 HandleBinaryOp(instruction);
9891}
9892
9893void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9894 // Nothing to do, this should be removed during prepare for register allocator.
9895 LOG(FATAL) << "Unreachable";
9896}
9897
9898void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9899 // Nothing to do, this should be removed during prepare for register allocator.
9900 LOG(FATAL) << "Unreachable";
9901}
9902
9903void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009904 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009905}
9906
9907void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009908 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009909}
9910
9911void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009912 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009913}
9914
9915void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009916 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009917}
9918
9919void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009920 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009921}
9922
9923void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009924 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009925}
9926
9927void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009928 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009929}
9930
9931void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009932 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009933}
9934
9935void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009936 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009937}
9938
9939void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009940 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009941}
9942
9943void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009944 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009945}
9946
9947void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009948 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009949}
9950
9951void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009952 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009953}
9954
9955void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009956 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009957}
9958
9959void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009960 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009961}
9962
9963void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009964 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009965}
9966
9967void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009968 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009969}
9970
9971void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009972 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009973}
9974
9975void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009976 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009977}
9978
9979void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009980 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009981}
9982
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009983void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9984 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009985 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009986 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009987 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9988 uint32_t num_entries = switch_instr->GetNumEntries();
9989 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9990 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9991 // instruction to simulate PC-relative addressing when accessing the jump table.
9992 // NAL clobbers RA. Make sure RA is preserved.
9993 codegen_->ClobberRA();
9994 }
9995 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009996}
9997
Alexey Frunze96b66822016-09-10 02:32:44 -07009998void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9999 int32_t lower_bound,
10000 uint32_t num_entries,
10001 HBasicBlock* switch_block,
10002 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010003 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010004 Register temp_reg = TMP;
10005 __ Addiu32(temp_reg, value_reg, -lower_bound);
10006 // Jump to default if index is negative
10007 // Note: We don't check the case that index is positive while value < lower_bound, because in
10008 // this case, index >= num_entries must be true. So that we can save one branch instruction.
10009 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
10010
Alexey Frunze96b66822016-09-10 02:32:44 -070010011 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010012 // Jump to successors[0] if value == lower_bound.
10013 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
10014 int32_t last_index = 0;
10015 for (; num_entries - last_index > 2; last_index += 2) {
10016 __ Addiu(temp_reg, temp_reg, -2);
10017 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
10018 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
10019 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
10020 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
10021 }
10022 if (num_entries - last_index == 2) {
10023 // The last missing case_value.
10024 __ Addiu(temp_reg, temp_reg, -1);
10025 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010026 }
10027
Vladimir Markof3e0ee22015-12-17 15:23:13 +000010028 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -070010029 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010030 __ B(codegen_->GetLabelOf(default_block));
10031 }
10032}
10033
Alexey Frunze96b66822016-09-10 02:32:44 -070010034void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
10035 Register constant_area,
10036 int32_t lower_bound,
10037 uint32_t num_entries,
10038 HBasicBlock* switch_block,
10039 HBasicBlock* default_block) {
10040 // Create a jump table.
10041 std::vector<MipsLabel*> labels(num_entries);
10042 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
10043 for (uint32_t i = 0; i < num_entries; i++) {
10044 labels[i] = codegen_->GetLabelOf(successors[i]);
10045 }
10046 JumpTable* table = __ CreateJumpTable(std::move(labels));
10047
10048 // Is the value in range?
10049 __ Addiu32(TMP, value_reg, -lower_bound);
10050 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
10051 __ Sltiu(AT, TMP, num_entries);
10052 __ Beqz(AT, codegen_->GetLabelOf(default_block));
10053 } else {
10054 __ LoadConst32(AT, num_entries);
10055 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
10056 }
10057
10058 // We are in the range of the table.
10059 // Load the target address from the jump table, indexing by the value.
10060 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -070010061 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -070010062 __ Lw(TMP, TMP, 0);
10063 // Compute the absolute target address by adding the table start address
10064 // (the table contains offsets to targets relative to its start).
10065 __ Addu(TMP, TMP, AT);
10066 // And jump.
10067 __ Jr(TMP);
10068 __ NopIfNoReordering();
10069}
10070
10071void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
10072 int32_t lower_bound = switch_instr->GetStartValue();
10073 uint32_t num_entries = switch_instr->GetNumEntries();
10074 LocationSummary* locations = switch_instr->GetLocations();
10075 Register value_reg = locations->InAt(0).AsRegister<Register>();
10076 HBasicBlock* switch_block = switch_instr->GetBlock();
10077 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10078
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010079 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -070010080 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010081 //
10082 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
10083 // to access the jump table and it is implemented by changing HPackedSwitch to
10084 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
10085 // VisitMipsPackedSwitch()).
10086 //
10087 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
10088 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
10089 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -070010090 GenTableBasedPackedSwitch(value_reg,
10091 ZERO,
10092 lower_bound,
10093 num_entries,
10094 switch_block,
10095 default_block);
10096 } else {
10097 GenPackedSwitchWithCompares(value_reg,
10098 lower_bound,
10099 num_entries,
10100 switch_block,
10101 default_block);
10102 }
10103}
10104
10105void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10106 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010107 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -070010108 locations->SetInAt(0, Location::RequiresRegister());
10109 // Constant area pointer (HMipsComputeBaseMethodAddress).
10110 locations->SetInAt(1, Location::RequiresRegister());
10111}
10112
10113void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10114 int32_t lower_bound = switch_instr->GetStartValue();
10115 uint32_t num_entries = switch_instr->GetNumEntries();
10116 LocationSummary* locations = switch_instr->GetLocations();
10117 Register value_reg = locations->InAt(0).AsRegister<Register>();
10118 Register constant_area = locations->InAt(1).AsRegister<Register>();
10119 HBasicBlock* switch_block = switch_instr->GetBlock();
10120 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10121
10122 // This is an R2-only path. HPackedSwitch has been changed to
10123 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
10124 // required to address the jump table relative to PC.
10125 GenTableBasedPackedSwitch(value_reg,
10126 constant_area,
10127 lower_bound,
10128 num_entries,
10129 switch_block,
10130 default_block);
10131}
10132
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010133void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
10134 HMipsComputeBaseMethodAddress* insn) {
10135 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010136 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010137 locations->SetOut(Location::RequiresRegister());
10138}
10139
10140void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
10141 HMipsComputeBaseMethodAddress* insn) {
10142 LocationSummary* locations = insn->GetLocations();
10143 Register reg = locations->Out().AsRegister<Register>();
10144
10145 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
10146
10147 // Generate a dummy PC-relative call to obtain PC.
10148 __ Nal();
10149 // Grab the return address off RA.
10150 __ Move(reg, RA);
10151
10152 // Remember this offset (the obtained PC value) for later use with constant area.
10153 __ BindPcRelBaseLabel();
10154}
10155
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010156void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10157 // The trampoline uses the same calling convention as dex calling conventions,
10158 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
10159 // the method_idx.
10160 HandleInvoke(invoke);
10161}
10162
10163void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10164 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
10165}
10166
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010167void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10168 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010169 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010170 locations->SetInAt(0, Location::RequiresRegister());
10171 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010172}
10173
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010174void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10175 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +000010176 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010177 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010178 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010179 __ LoadFromOffset(kLoadWord,
10180 locations->Out().AsRegister<Register>(),
10181 locations->InAt(0).AsRegister<Register>(),
10182 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010183 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010184 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +000010185 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000010186 __ LoadFromOffset(kLoadWord,
10187 locations->Out().AsRegister<Register>(),
10188 locations->InAt(0).AsRegister<Register>(),
10189 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010190 __ LoadFromOffset(kLoadWord,
10191 locations->Out().AsRegister<Register>(),
10192 locations->Out().AsRegister<Register>(),
10193 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010194 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010195}
10196
xueliang.zhonge0eb4832017-10-30 13:43:14 +000010197void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10198 ATTRIBUTE_UNUSED) {
10199 LOG(FATAL) << "Unreachable";
10200}
10201
10202void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10203 ATTRIBUTE_UNUSED) {
10204 LOG(FATAL) << "Unreachable";
10205}
10206
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010207#undef __
10208#undef QUICK_ENTRY_POINT
10209
10210} // namespace mips
10211} // namespace art