blob: 25e2eddbfabc9f9c8f9fa765fe30bb66d2322c2a [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"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010036#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020037#include "thread.h"
38#include "utils/assembler.h"
39#include "utils/mips/assembler_mips.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace mips {
44
45static constexpr int kCurrentMethodStackOffset = 0;
46static constexpr Register kMethodRegisterArgument = A0;
47
Alexey Frunze4147fcc2017-06-17 19:57:27 -070048// Flags controlling the use of thunks for Baker read barriers.
49constexpr bool kBakerReadBarrierThunksEnableForFields = true;
50constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
51constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
52
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020054 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010055 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010057 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010058 case DataType::Type::kInt8:
59 case DataType::Type::kUint16:
60 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -080061 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010062 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020063 return Location::RegisterLocation(V0);
64
Aart Bik66c158e2018-01-31 12:55:04 -080065 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020067 return Location::RegisterPairLocation(V0, V1);
68
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010069 case DataType::Type::kFloat32:
70 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020071 return Location::FpuRegisterLocation(F0);
72
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020074 return Location();
75 }
76 UNREACHABLE();
77}
78
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010079Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020080 return MipsReturnLocation(type);
81}
82
83Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
84 return Location::RegisterLocation(kMethodRegisterArgument);
85}
86
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020088 Location next_location;
89
90 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010091 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010093 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010094 case DataType::Type::kInt8:
95 case DataType::Type::kUint16:
96 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010097 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020098 uint32_t gp_index = gp_index_++;
99 if (gp_index < calling_convention.GetNumberOfRegisters()) {
100 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
101 } else {
102 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
103 next_location = Location::StackSlot(stack_offset);
104 }
105 break;
106 }
107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100108 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200109 uint32_t gp_index = gp_index_;
110 gp_index_ += 2;
111 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800112 Register reg = calling_convention.GetRegisterAt(gp_index);
113 if (reg == A1 || reg == A3) {
114 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200115 gp_index++;
116 }
117 Register low_even = calling_convention.GetRegisterAt(gp_index);
118 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
119 DCHECK_EQ(low_even + 1, high_odd);
120 next_location = Location::RegisterPairLocation(low_even, high_odd);
121 } else {
122 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
123 next_location = Location::DoubleStackSlot(stack_offset);
124 }
125 break;
126 }
127
128 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
129 // will take up the even/odd pair, while floats are stored in even regs only.
130 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100131 case DataType::Type::kFloat32:
132 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200133 uint32_t float_index = float_index_++;
134 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
135 next_location = Location::FpuRegisterLocation(
136 calling_convention.GetFpuRegisterAt(float_index));
137 } else {
138 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100139 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
140 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200141 }
142 break;
143 }
144
Aart Bik66c158e2018-01-31 12:55:04 -0800145 case DataType::Type::kUint32:
146 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200148 LOG(FATAL) << "Unexpected parameter type " << type;
149 break;
150 }
151
152 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200154
155 return next_location;
156}
157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100158Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200159 return MipsReturnLocation(type);
160}
161
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100162// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
163#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700164#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200165
166class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
167 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000168 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200169
170 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
171 LocationSummary* locations = instruction_->GetLocations();
172 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
173 __ Bind(GetEntryLabel());
174 if (instruction_->CanThrowIntoCatchBlock()) {
175 // Live registers will be restored in the catch block if caught.
176 SaveLiveRegisters(codegen, instruction_->GetLocations());
177 }
178 // We're moving two locations to locations that could overlap, so we need a parallel
179 // move resolver.
180 InvokeRuntimeCallingConvention calling_convention;
181 codegen->EmitParallelMoves(locations->InAt(0),
182 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100183 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200184 locations->InAt(1),
185 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100186 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100187 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
188 ? kQuickThrowStringBounds
189 : kQuickThrowArrayBounds;
190 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100191 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200192 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
193 }
194
195 bool IsFatal() const OVERRIDE { return true; }
196
197 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
198
199 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200200 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
201};
202
203class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
204 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000205 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206
207 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
208 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
209 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100210 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200211 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
212 }
213
214 bool IsFatal() const OVERRIDE { return true; }
215
216 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
217
218 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200219 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
220};
221
222class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
223 public:
224 LoadClassSlowPathMIPS(HLoadClass* cls,
225 HInstruction* at,
226 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000227 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700228 : SlowPathCodeMIPS(at),
229 cls_(cls),
230 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000231 do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
233 }
234
235 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000236 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700237 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200238 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700239 InvokeRuntimeCallingConvention calling_convention;
240 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200241 __ Bind(GetEntryLabel());
242 SaveLiveRegisters(codegen, locations);
243
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 dex::TypeIndex type_index = cls_->GetTypeIndex();
245 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100246 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
247 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000248 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200249 if (do_clinit_) {
250 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
251 } else {
252 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
253 }
254
255 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200256 if (out.IsValid()) {
257 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100258 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700259 mips_codegen->MoveLocation(out,
260 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
261 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200262 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200263 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700264
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200265 __ B(GetExitLabel());
266 }
267
268 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
269
270 private:
271 // The class this slow path will load.
272 HLoadClass* const cls_;
273
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
281};
282
283class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
284 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000285 explicit LoadStringSlowPathMIPS(HLoadString* instruction)
286 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700289 DCHECK(instruction_->IsLoadString());
290 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200291 LocationSummary* locations = instruction_->GetLocations();
292 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000293 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200294 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700295 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200296 __ Bind(GetEntryLabel());
297 SaveLiveRegisters(codegen, locations);
298
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000299 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100300 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200301 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700302
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100303 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200304 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000308
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200309 __ B(GetExitLabel());
310 }
311
312 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
313
314 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200315 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
316};
317
318class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
319 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000320 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200321
322 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
323 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
324 __ Bind(GetEntryLabel());
325 if (instruction_->CanThrowIntoCatchBlock()) {
326 // Live registers will be restored in the catch block if caught.
327 SaveLiveRegisters(codegen, instruction_->GetLocations());
328 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100329 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330 instruction_,
331 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100332 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200333 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
334 }
335
336 bool IsFatal() const OVERRIDE { return true; }
337
338 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
339
340 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
342};
343
344class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
345 public:
346 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000347 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200348
349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200350 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
352 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200353 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100354 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200355 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200356 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200357 if (successor_ == nullptr) {
358 __ B(GetReturnLabel());
359 } else {
360 __ B(mips_codegen->GetLabelOf(successor_));
361 }
362 }
363
364 MipsLabel* GetReturnLabel() {
365 DCHECK(successor_ == nullptr);
366 return &return_label_;
367 }
368
369 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
370
Chris Larsena2045912017-11-02 12:39:54 -0700371 HBasicBlock* GetSuccessor() const {
372 return successor_;
373 }
374
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200375 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200376 // If not null, the block to branch to after the suspend check.
377 HBasicBlock* const successor_;
378
379 // If `successor_` is null, the label to branch to after the suspend check.
380 MipsLabel return_label_;
381
382 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
383};
384
385class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
386 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800387 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
388 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200392 uint32_t dex_pc = instruction_->GetDexPc();
393 DCHECK(instruction_->IsCheckCast()
394 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
395 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
396
397 __ Bind(GetEntryLabel());
Alexey Frunzedfc30af2018-01-24 16:25:10 -0800398 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800399 SaveLiveRegisters(codegen, locations);
400 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200401
402 // We're moving two locations to locations that could overlap, so we need a parallel
403 // move resolver.
404 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800405 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200406 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100407 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800408 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200409 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100410 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200411 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100412 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800413 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100414 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200415 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
416 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 } else {
418 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800419 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
420 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 }
422
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800423 if (!is_fatal_) {
424 RestoreLiveRegisters(codegen, locations);
425 __ B(GetExitLabel());
426 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200427 }
428
429 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
430
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800431 bool IsFatal() const OVERRIDE { return is_fatal_; }
432
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800434 const bool is_fatal_;
435
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200436 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
437};
438
439class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
440 public:
Aart Bik42249c32016-01-07 15:33:50 -0800441 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000442 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443
444 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800445 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100447 LocationSummary* locations = instruction_->GetLocations();
448 SaveLiveRegisters(codegen, locations);
449 InvokeRuntimeCallingConvention calling_convention;
450 __ LoadConst32(calling_convention.GetRegisterAt(0),
451 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100452 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100453 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 }
455
456 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
457
458 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200459 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
460};
461
Alexey Frunze15958152017-02-09 19:08:30 -0800462class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
463 public:
464 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
465
466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 LocationSummary* locations = instruction_->GetLocations();
468 __ Bind(GetEntryLabel());
469 SaveLiveRegisters(codegen, locations);
470
471 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100472 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800473 parallel_move.AddMove(
474 locations->InAt(0),
475 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100476 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800477 nullptr);
478 parallel_move.AddMove(
479 locations->InAt(1),
480 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100481 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800482 nullptr);
483 parallel_move.AddMove(
484 locations->InAt(2),
485 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100486 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800487 nullptr);
488 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
489
490 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
491 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
492 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
493 RestoreLiveRegisters(codegen, locations);
494 __ B(GetExitLabel());
495 }
496
497 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
498
499 private:
500 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
501};
502
503// Slow path marking an object reference `ref` during a read
504// barrier. The field `obj.field` in the object `obj` holding this
505// reference does not get updated by this slow path after marking (see
506// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
507//
508// This means that after the execution of this slow path, `ref` will
509// always be up-to-date, but `obj.field` may not; i.e., after the
510// flip, `ref` will be a to-space reference, but `obj.field` will
511// probably still be a from-space reference (unless it gets updated by
512// another thread, or if another thread installed another object
513// reference (different from `ref`) in `obj.field`).
514//
515// If `entrypoint` is a valid location it is assumed to already be
516// holding the entrypoint. The case where the entrypoint is passed in
517// is for the GcRoot read barrier.
518class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
519 public:
520 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
521 Location ref,
522 Location entrypoint = Location::NoLocation())
523 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
524 DCHECK(kEmitCompilerReadBarrier);
525 }
526
527 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
528
529 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
530 LocationSummary* locations = instruction_->GetLocations();
531 Register ref_reg = ref_.AsRegister<Register>();
532 DCHECK(locations->CanCall());
533 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
534 DCHECK(instruction_->IsInstanceFieldGet() ||
535 instruction_->IsStaticFieldGet() ||
536 instruction_->IsArrayGet() ||
537 instruction_->IsArraySet() ||
538 instruction_->IsLoadClass() ||
539 instruction_->IsLoadString() ||
540 instruction_->IsInstanceOf() ||
541 instruction_->IsCheckCast() ||
542 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
543 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
544 << "Unexpected instruction in read barrier marking slow path: "
545 << instruction_->DebugName();
546
547 __ Bind(GetEntryLabel());
548 // No need to save live registers; it's taken care of by the
549 // entrypoint. Also, there is no need to update the stack mask,
550 // as this runtime call will not trigger a garbage collection.
551 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
552 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
553 (S2 <= ref_reg && ref_reg <= S7) ||
554 (ref_reg == FP)) << ref_reg;
555 // "Compact" slow path, saving two moves.
556 //
557 // Instead of using the standard runtime calling convention (input
558 // and output in A0 and V0 respectively):
559 //
560 // A0 <- ref
561 // V0 <- ReadBarrierMark(A0)
562 // ref <- V0
563 //
564 // we just use rX (the register containing `ref`) as input and output
565 // of a dedicated entrypoint:
566 //
567 // rX <- ReadBarrierMarkRegX(rX)
568 //
569 if (entrypoint_.IsValid()) {
570 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
571 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
572 __ Jalr(entrypoint_.AsRegister<Register>());
573 __ NopIfNoReordering();
574 } else {
575 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100576 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800577 // This runtime call does not require a stack map.
578 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
579 instruction_,
580 this,
581 /* direct */ false);
582 }
583 __ B(GetExitLabel());
584 }
585
586 private:
587 // The location (register) of the marked object reference.
588 const Location ref_;
589
590 // The location of the entrypoint if already loaded.
591 const Location entrypoint_;
592
593 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
594};
595
596// Slow path marking an object reference `ref` during a read barrier,
597// and if needed, atomically updating the field `obj.field` in the
598// object `obj` holding this reference after marking (contrary to
599// ReadBarrierMarkSlowPathMIPS above, which never tries to update
600// `obj.field`).
601//
602// This means that after the execution of this slow path, both `ref`
603// and `obj.field` will be up-to-date; i.e., after the flip, both will
604// hold the same to-space reference (unless another thread installed
605// another object reference (different from `ref`) in `obj.field`).
606class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
607 public:
608 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
609 Location ref,
610 Register obj,
611 Location field_offset,
612 Register temp1)
613 : SlowPathCodeMIPS(instruction),
614 ref_(ref),
615 obj_(obj),
616 field_offset_(field_offset),
617 temp1_(temp1) {
618 DCHECK(kEmitCompilerReadBarrier);
619 }
620
621 const char* GetDescription() const OVERRIDE {
622 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
623 }
624
625 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
626 LocationSummary* locations = instruction_->GetLocations();
627 Register ref_reg = ref_.AsRegister<Register>();
628 DCHECK(locations->CanCall());
629 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
630 // This slow path is only used by the UnsafeCASObject intrinsic.
631 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
632 << "Unexpected instruction in read barrier marking and field updating slow path: "
633 << instruction_->DebugName();
634 DCHECK(instruction_->GetLocations()->Intrinsified());
635 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
636 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
637
638 __ Bind(GetEntryLabel());
639
640 // Save the old reference.
641 // Note that we cannot use AT or TMP to save the old reference, as those
642 // are used by the code that follows, but we need the old reference after
643 // the call to the ReadBarrierMarkRegX entry point.
644 DCHECK_NE(temp1_, AT);
645 DCHECK_NE(temp1_, TMP);
646 __ Move(temp1_, ref_reg);
647
648 // No need to save live registers; it's taken care of by the
649 // entrypoint. Also, there is no need to update the stack mask,
650 // as this runtime call will not trigger a garbage collection.
651 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
652 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
653 (S2 <= ref_reg && ref_reg <= S7) ||
654 (ref_reg == FP)) << ref_reg;
655 // "Compact" slow path, saving two moves.
656 //
657 // Instead of using the standard runtime calling convention (input
658 // and output in A0 and V0 respectively):
659 //
660 // A0 <- ref
661 // V0 <- ReadBarrierMark(A0)
662 // ref <- V0
663 //
664 // we just use rX (the register containing `ref`) as input and output
665 // of a dedicated entrypoint:
666 //
667 // rX <- ReadBarrierMarkRegX(rX)
668 //
669 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100670 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800671 // This runtime call does not require a stack map.
672 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
673 instruction_,
674 this,
675 /* direct */ false);
676
677 // If the new reference is different from the old reference,
678 // update the field in the holder (`*(obj_ + field_offset_)`).
679 //
680 // Note that this field could also hold a different object, if
681 // another thread had concurrently changed it. In that case, the
682 // the compare-and-set (CAS) loop below would abort, leaving the
683 // field as-is.
684 MipsLabel done;
685 __ Beq(temp1_, ref_reg, &done);
686
687 // Update the the holder's field atomically. This may fail if
688 // mutator updates before us, but it's OK. This is achieved
689 // using a strong compare-and-set (CAS) operation with relaxed
690 // memory synchronization ordering, where the expected value is
691 // the old reference and the desired value is the new reference.
692
693 // Convenience aliases.
694 Register base = obj_;
695 // The UnsafeCASObject intrinsic uses a register pair as field
696 // offset ("long offset"), of which only the low part contains
697 // data.
698 Register offset = field_offset_.AsRegisterPairLow<Register>();
699 Register expected = temp1_;
700 Register value = ref_reg;
701 Register tmp_ptr = TMP; // Pointer to actual memory.
702 Register tmp = AT; // Value in memory.
703
704 __ Addu(tmp_ptr, base, offset);
705
706 if (kPoisonHeapReferences) {
707 __ PoisonHeapReference(expected);
708 // Do not poison `value` if it is the same register as
709 // `expected`, which has just been poisoned.
710 if (value != expected) {
711 __ PoisonHeapReference(value);
712 }
713 }
714
715 // do {
716 // tmp = [r_ptr] - expected;
717 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
718
719 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
720 MipsLabel loop_head, exit_loop;
721 __ Bind(&loop_head);
722 if (is_r6) {
723 __ LlR6(tmp, tmp_ptr);
724 } else {
725 __ LlR2(tmp, tmp_ptr);
726 }
727 __ Bne(tmp, expected, &exit_loop);
728 __ Move(tmp, value);
729 if (is_r6) {
730 __ ScR6(tmp, tmp_ptr);
731 } else {
732 __ ScR2(tmp, tmp_ptr);
733 }
734 __ Beqz(tmp, &loop_head);
735 __ Bind(&exit_loop);
736
737 if (kPoisonHeapReferences) {
738 __ UnpoisonHeapReference(expected);
739 // Do not unpoison `value` if it is the same register as
740 // `expected`, which has just been unpoisoned.
741 if (value != expected) {
742 __ UnpoisonHeapReference(value);
743 }
744 }
745
746 __ Bind(&done);
747 __ B(GetExitLabel());
748 }
749
750 private:
751 // The location (register) of the marked object reference.
752 const Location ref_;
753 // The register containing the object holding the marked object reference field.
754 const Register obj_;
755 // The location of the offset of the marked reference field within `obj_`.
756 Location field_offset_;
757
758 const Register temp1_;
759
760 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
761};
762
763// Slow path generating a read barrier for a heap reference.
764class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
765 public:
766 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
767 Location out,
768 Location ref,
769 Location obj,
770 uint32_t offset,
771 Location index)
772 : SlowPathCodeMIPS(instruction),
773 out_(out),
774 ref_(ref),
775 obj_(obj),
776 offset_(offset),
777 index_(index) {
778 DCHECK(kEmitCompilerReadBarrier);
779 // If `obj` is equal to `out` or `ref`, it means the initial object
780 // has been overwritten by (or after) the heap object reference load
781 // to be instrumented, e.g.:
782 //
783 // __ LoadFromOffset(kLoadWord, out, out, offset);
784 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
785 //
786 // In that case, we have lost the information about the original
787 // object, and the emitted read barrier cannot work properly.
788 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
789 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
790 }
791
792 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
793 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
794 LocationSummary* locations = instruction_->GetLocations();
795 Register reg_out = out_.AsRegister<Register>();
796 DCHECK(locations->CanCall());
797 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
798 DCHECK(instruction_->IsInstanceFieldGet() ||
799 instruction_->IsStaticFieldGet() ||
800 instruction_->IsArrayGet() ||
801 instruction_->IsInstanceOf() ||
802 instruction_->IsCheckCast() ||
803 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
804 << "Unexpected instruction in read barrier for heap reference slow path: "
805 << instruction_->DebugName();
806
807 __ Bind(GetEntryLabel());
808 SaveLiveRegisters(codegen, locations);
809
810 // We may have to change the index's value, but as `index_` is a
811 // constant member (like other "inputs" of this slow path),
812 // introduce a copy of it, `index`.
813 Location index = index_;
814 if (index_.IsValid()) {
815 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
816 if (instruction_->IsArrayGet()) {
817 // Compute the actual memory offset and store it in `index`.
818 Register index_reg = index_.AsRegister<Register>();
819 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
820 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
821 // We are about to change the value of `index_reg` (see the
822 // calls to art::mips::MipsAssembler::Sll and
823 // art::mips::MipsAssembler::Addiu32 below), but it has
824 // not been saved by the previous call to
825 // art::SlowPathCode::SaveLiveRegisters, as it is a
826 // callee-save register --
827 // art::SlowPathCode::SaveLiveRegisters does not consider
828 // callee-save registers, as it has been designed with the
829 // assumption that callee-save registers are supposed to be
830 // handled by the called function. So, as a callee-save
831 // register, `index_reg` _would_ eventually be saved onto
832 // the stack, but it would be too late: we would have
833 // changed its value earlier. Therefore, we manually save
834 // it here into another freely available register,
835 // `free_reg`, chosen of course among the caller-save
836 // registers (as a callee-save `free_reg` register would
837 // exhibit the same problem).
838 //
839 // Note we could have requested a temporary register from
840 // the register allocator instead; but we prefer not to, as
841 // this is a slow path, and we know we can find a
842 // caller-save register that is available.
843 Register free_reg = FindAvailableCallerSaveRegister(codegen);
844 __ Move(free_reg, index_reg);
845 index_reg = free_reg;
846 index = Location::RegisterLocation(index_reg);
847 } else {
848 // The initial register stored in `index_` has already been
849 // saved in the call to art::SlowPathCode::SaveLiveRegisters
850 // (as it is not a callee-save register), so we can freely
851 // use it.
852 }
853 // Shifting the index value contained in `index_reg` by the scale
854 // factor (2) cannot overflow in practice, as the runtime is
855 // unable to allocate object arrays with a size larger than
856 // 2^26 - 1 (that is, 2^28 - 4 bytes).
857 __ Sll(index_reg, index_reg, TIMES_4);
858 static_assert(
859 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
860 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
861 __ Addiu32(index_reg, index_reg, offset_);
862 } else {
863 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
864 // intrinsics, `index_` is not shifted by a scale factor of 2
865 // (as in the case of ArrayGet), as it is actually an offset
866 // to an object field within an object.
867 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
868 DCHECK(instruction_->GetLocations()->Intrinsified());
869 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
870 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
871 << instruction_->AsInvoke()->GetIntrinsic();
872 DCHECK_EQ(offset_, 0U);
873 DCHECK(index_.IsRegisterPair());
874 // UnsafeGet's offset location is a register pair, the low
875 // part contains the correct offset.
876 index = index_.ToLow();
877 }
878 }
879
880 // We're moving two or three locations to locations that could
881 // overlap, so we need a parallel move resolver.
882 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100883 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800884 parallel_move.AddMove(ref_,
885 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100886 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800887 nullptr);
888 parallel_move.AddMove(obj_,
889 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100890 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800891 nullptr);
892 if (index.IsValid()) {
893 parallel_move.AddMove(index,
894 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100895 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800896 nullptr);
897 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
898 } else {
899 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
900 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
901 }
902 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
903 instruction_,
904 instruction_->GetDexPc(),
905 this);
906 CheckEntrypointTypes<
907 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200908 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100909 calling_convention.GetReturnLocation(DataType::Type::kReference),
910 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800911
912 RestoreLiveRegisters(codegen, locations);
913 __ B(GetExitLabel());
914 }
915
916 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
917
918 private:
919 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
920 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
921 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
922 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
923 if (i != ref &&
924 i != obj &&
925 !codegen->IsCoreCalleeSaveRegister(i) &&
926 !codegen->IsBlockedCoreRegister(i)) {
927 return static_cast<Register>(i);
928 }
929 }
930 // We shall never fail to find a free caller-save register, as
931 // there are more than two core caller-save registers on MIPS
932 // (meaning it is possible to find one which is different from
933 // `ref` and `obj`).
934 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
935 LOG(FATAL) << "Could not find a free caller-save register";
936 UNREACHABLE();
937 }
938
939 const Location out_;
940 const Location ref_;
941 const Location obj_;
942 const uint32_t offset_;
943 // An additional location containing an index to an array.
944 // Only used for HArrayGet and the UnsafeGetObject &
945 // UnsafeGetObjectVolatile intrinsics.
946 const Location index_;
947
948 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
949};
950
951// Slow path generating a read barrier for a GC root.
952class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
953 public:
954 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
955 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
956 DCHECK(kEmitCompilerReadBarrier);
957 }
958
959 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
960 LocationSummary* locations = instruction_->GetLocations();
961 Register reg_out = out_.AsRegister<Register>();
962 DCHECK(locations->CanCall());
963 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
964 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
965 << "Unexpected instruction in read barrier for GC root slow path: "
966 << instruction_->DebugName();
967
968 __ Bind(GetEntryLabel());
969 SaveLiveRegisters(codegen, locations);
970
971 InvokeRuntimeCallingConvention calling_convention;
972 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +0200973 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
974 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100975 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800976 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
977 instruction_,
978 instruction_->GetDexPc(),
979 this);
980 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +0200981 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100982 calling_convention.GetReturnLocation(DataType::Type::kReference),
983 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800984
985 RestoreLiveRegisters(codegen, locations);
986 __ B(GetExitLabel());
987 }
988
989 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
990
991 private:
992 const Location out_;
993 const Location root_;
994
995 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
996};
997
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200998CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
999 const MipsInstructionSetFeatures& isa_features,
1000 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),
1016 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001017 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001018 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001019 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001020 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001021 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001022 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001023 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001024 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001025 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1026 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1027 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001028 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001029 // Save RA (containing the return address) to mimic Quick.
1030 AddAllocatedRegister(Location::RegisterLocation(RA));
1031}
1032
1033#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001034// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1035#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001036#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001037
1038void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1039 // Ensure that we fix up branches.
1040 __ FinalizeCode();
1041
1042 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001043 StackMapStream* stack_map_stream = GetStackMapStream();
1044 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001045 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +00001046 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips);
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 Markob066d432018-01-03 13:14:37 +00001600linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
1601 const DexFile* target_dex_file,
1602 uint32_t pc_insn_offset,
1603 uint32_t boot_image_offset) {
1604 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
1605 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
1606}
1607
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001608void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001609 DCHECK(linker_patches->empty());
1610 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001611 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001612 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001613 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001614 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001615 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001616 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001617 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001618 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001619 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001620 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001621 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001622 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001623 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001624 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001625 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00001626 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
1627 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001628 DCHECK(boot_image_type_patches_.empty());
1629 DCHECK(boot_image_string_patches_.empty());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001630 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001631 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1632 method_bss_entry_patches_, linker_patches);
1633 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1634 type_bss_entry_patches_, linker_patches);
1635 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1636 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001637 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001638}
1639
Vladimir Markob066d432018-01-03 13:14:37 +00001640CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageRelRoPatch(
1641 uint32_t boot_image_offset,
1642 const PcRelativePatchInfo* info_high) {
1643 return NewPcRelativePatch(
1644 /* dex_file */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_);
1645}
1646
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001647CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001648 MethodReference target_method,
1649 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001650 return NewPcRelativePatch(
1651 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001652}
1653
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001654CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001655 MethodReference target_method,
1656 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001657 return NewPcRelativePatch(
1658 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001659}
1660
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001661CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001662 const DexFile& dex_file,
1663 dex::TypeIndex type_index,
1664 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001665 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001666}
1667
Vladimir Marko1998cd02017-01-13 13:02:58 +00001668CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001669 const DexFile& dex_file,
1670 dex::TypeIndex type_index,
1671 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001672 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001673}
1674
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001675CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001676 const DexFile& dex_file,
1677 dex::StringIndex string_index,
1678 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001679 return NewPcRelativePatch(
1680 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001681}
1682
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001683CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1684 const DexFile& dex_file,
1685 dex::StringIndex string_index,
1686 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001687 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001688}
1689
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001690CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001691 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001692 uint32_t offset_or_index,
1693 const PcRelativePatchInfo* info_high,
1694 ArenaDeque<PcRelativePatchInfo>* patches) {
1695 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001696 return &patches->back();
1697}
1698
Alexey Frunze06a46c42016-07-19 15:00:40 -07001699Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1700 return map->GetOrCreate(
1701 value,
1702 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1703}
1704
Alexey Frunze06a46c42016-07-19 15:00:40 -07001705Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001706 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001707}
1708
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001709void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001710 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001711 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001712 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001713 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001714 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001715 if (GetInstructionSetFeatures().IsR6()) {
1716 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001717 __ Bind(&info_high->label);
1718 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001719 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001720 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001721 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001722 } else {
1723 // If base is ZERO, emit NAL to obtain the actual base.
1724 if (base == ZERO) {
1725 // Generate a dummy PC-relative call to obtain PC.
1726 __ Nal();
1727 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001728 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001729 __ Lui(out, /* placeholder */ 0x1234);
1730 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1731 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1732 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001733 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001734 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001735 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001736 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001737 __ Addu(out, out, (base == ZERO) ? RA : base);
1738 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001739 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001740 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001741}
1742
Alexey Frunze627c1a02017-01-30 19:28:14 -08001743CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1744 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001745 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001746 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001747 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1748 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001749 return &jit_string_patches_.back();
1750}
1751
1752CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1753 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001754 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001755 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001756 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1757 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001758 return &jit_class_patches_.back();
1759}
1760
1761void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1762 const uint8_t* roots_data,
1763 const CodeGeneratorMIPS::JitPatchInfo& info,
1764 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001765 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1766 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001767 uintptr_t address =
1768 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1769 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1770 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001771 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1772 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1773 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1774 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001775 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001776 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1777 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001778 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001779 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001780 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1781 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001782 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001783 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1784 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001785}
1786
1787void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1788 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001789 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1790 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001791 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001792 }
1793 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001794 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1795 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001796 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001797 }
1798}
1799
Goran Jakovljevice114da22016-12-26 14:21:43 +01001800void CodeGeneratorMIPS::MarkGCCard(Register object,
1801 Register value,
1802 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001803 MipsLabel done;
1804 Register card = AT;
1805 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001806 if (value_can_be_null) {
1807 __ Beqz(value, &done);
1808 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001809 __ LoadFromOffset(kLoadWord,
1810 card,
1811 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001812 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001813 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1814 __ Addu(temp, card, temp);
1815 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001816 if (value_can_be_null) {
1817 __ Bind(&done);
1818 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001819}
1820
David Brazdil58282f42016-01-14 12:45:10 +00001821void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001822 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1823 blocked_core_registers_[ZERO] = true;
1824 blocked_core_registers_[K0] = true;
1825 blocked_core_registers_[K1] = true;
1826 blocked_core_registers_[GP] = true;
1827 blocked_core_registers_[SP] = true;
1828 blocked_core_registers_[RA] = true;
1829
1830 // AT and TMP(T8) are used as temporary/scratch registers
1831 // (similar to how AT is used by MIPS assemblers).
1832 blocked_core_registers_[AT] = true;
1833 blocked_core_registers_[TMP] = true;
1834 blocked_fpu_registers_[FTMP] = true;
1835
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001836 if (GetInstructionSetFeatures().HasMsa()) {
1837 // To be used just for MSA instructions.
1838 blocked_fpu_registers_[FTMP2] = true;
1839 }
1840
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001841 // Reserve suspend and thread registers.
1842 blocked_core_registers_[S0] = true;
1843 blocked_core_registers_[TR] = true;
1844
1845 // Reserve T9 for function calls
1846 blocked_core_registers_[T9] = true;
1847
1848 // Reserve odd-numbered FPU registers.
1849 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1850 blocked_fpu_registers_[i] = true;
1851 }
1852
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001853 if (GetGraph()->IsDebuggable()) {
1854 // Stubs do not save callee-save floating point registers. If the graph
1855 // is debuggable, we need to deal with these registers differently. For
1856 // now, just block them.
1857 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1858 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1859 }
1860 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861}
1862
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1864 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1865 return kMipsWordSize;
1866}
1867
1868size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1869 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1870 return kMipsWordSize;
1871}
1872
1873size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001874 if (GetGraph()->HasSIMD()) {
1875 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1876 } else {
1877 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1878 }
1879 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880}
1881
1882size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001883 if (GetGraph()->HasSIMD()) {
1884 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1885 } else {
1886 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1887 }
1888 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001889}
1890
1891void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001892 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001893}
1894
1895void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001896 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001897}
1898
Serban Constantinescufca16662016-07-14 09:21:59 +01001899constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1900
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1902 HInstruction* instruction,
1903 uint32_t dex_pc,
1904 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001905 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001906 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1907 IsDirectEntrypoint(entrypoint));
1908 if (EntrypointRequiresStackMap(entrypoint)) {
1909 RecordPcInfo(instruction, dex_pc, slow_path);
1910 }
1911}
1912
1913void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1914 HInstruction* instruction,
1915 SlowPathCode* slow_path,
1916 bool direct) {
1917 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1918 GenerateInvokeRuntime(entry_point_offset, direct);
1919}
1920
1921void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001922 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001923 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001924 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001925 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001926 // Reserve argument space on stack (for $a0-$a3) for
1927 // entrypoints that directly reference native implementations.
1928 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001929 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001930 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001931 } else {
1932 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001934 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001935}
1936
1937void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1938 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001939 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1940 const size_t status_byte_offset =
1941 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1942 constexpr uint32_t shifted_initialized_value =
1943 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1944
1945 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01001946 __ Sltiu(TMP, TMP, shifted_initialized_value);
1947 __ Bnez(TMP, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001948 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1949 __ Sync(0);
1950 __ Bind(slow_path->GetExitLabel());
1951}
1952
Vladimir Marko3f413232018-02-12 18:39:15 +00001953void InstructionCodeGeneratorMIPS::GenerateBitstringTypeCheckCompare(HTypeCheckInstruction* check,
1954 Register temp) {
1955 uint32_t path_to_root = check->GetBitstringPathToRoot();
1956 uint32_t mask = check->GetBitstringMask();
1957 DCHECK(IsPowerOfTwo(mask + 1));
1958 size_t mask_bits = WhichPowerOf2(mask + 1);
1959
1960 if (mask_bits == 16u) {
1961 // Load only the bitstring part of the status word.
1962 __ LoadFromOffset(
1963 kLoadUnsignedHalfword, temp, temp, mirror::Class::StatusOffset().Int32Value());
1964 // Compare the bitstring bits using XOR.
1965 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1966 } else {
1967 // /* uint32_t */ temp = temp->status_
1968 __ LoadFromOffset(kLoadWord, temp, temp, mirror::Class::StatusOffset().Int32Value());
1969 // Compare the bitstring bits using XOR.
1970 if (IsUint<16>(path_to_root)) {
1971 __ Xori(temp, temp, dchecked_integral_cast<uint16_t>(path_to_root));
1972 } else {
1973 __ LoadConst32(TMP, path_to_root);
1974 __ Xor(temp, temp, TMP);
1975 }
1976 // Shift out bits that do not contribute to the comparison.
1977 __ Sll(temp, temp, 32 - mask_bits);
1978 }
1979}
1980
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001981void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1982 __ Sync(0); // Only stype 0 is supported.
1983}
1984
1985void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1986 HBasicBlock* successor) {
1987 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001988 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1989
1990 if (slow_path == nullptr) {
1991 slow_path =
1992 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1993 instruction->SetSlowPath(slow_path);
1994 codegen_->AddSlowPath(slow_path);
1995 if (successor != nullptr) {
1996 DCHECK(successor->IsLoopHeader());
1997 }
1998 } else {
1999 DCHECK_EQ(slow_path->GetSuccessor(), successor);
2000 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002001
2002 __ LoadFromOffset(kLoadUnsignedHalfword,
2003 TMP,
2004 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002005 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002006 if (successor == nullptr) {
2007 __ Bnez(TMP, slow_path->GetEntryLabel());
2008 __ Bind(slow_path->GetReturnLabel());
2009 } else {
2010 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2011 __ B(slow_path->GetEntryLabel());
2012 // slow_path will return to GetLabelOf(successor).
2013 }
2014}
2015
2016InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2017 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002018 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002019 assembler_(codegen->GetAssembler()),
2020 codegen_(codegen) {}
2021
2022void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2023 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01002024 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01002026 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002027 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002028 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002029 locations->SetInAt(0, Location::RequiresRegister());
2030 HInstruction* right = instruction->InputAt(1);
2031 bool can_use_imm = false;
2032 if (right->IsConstant()) {
2033 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2034 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2035 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002036 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002037 DCHECK(instruction->IsSub() || instruction->IsAdd());
2038 if (instruction->IsSub()) {
2039 imm = -imm;
2040 }
2041 if (isR6) {
2042 bool single_use = right->GetUses().HasExactlyOneElement();
2043 int16_t imm_high = High16Bits(imm);
2044 int16_t imm_low = Low16Bits(imm);
2045 if (imm_low < 0) {
2046 imm_high += 1;
2047 }
2048 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2049 } else {
2050 can_use_imm = IsInt<16>(imm);
2051 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002052 }
2053 }
2054 if (can_use_imm)
2055 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2056 else
2057 locations->SetInAt(1, Location::RequiresRegister());
2058 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2059 break;
2060 }
2061
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002063 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002064 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2065 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002066 break;
2067 }
2068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002069 case DataType::Type::kFloat32:
2070 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002071 DCHECK(instruction->IsAdd() || instruction->IsSub());
2072 locations->SetInAt(0, Location::RequiresFpuRegister());
2073 locations->SetInAt(1, Location::RequiresFpuRegister());
2074 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2075 break;
2076
2077 default:
2078 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2079 }
2080}
2081
2082void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002083 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002084 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002085 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002086
2087 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002088 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002089 Register dst = locations->Out().AsRegister<Register>();
2090 Register lhs = locations->InAt(0).AsRegister<Register>();
2091 Location rhs_location = locations->InAt(1);
2092
2093 Register rhs_reg = ZERO;
2094 int32_t rhs_imm = 0;
2095 bool use_imm = rhs_location.IsConstant();
2096 if (use_imm) {
2097 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2098 } else {
2099 rhs_reg = rhs_location.AsRegister<Register>();
2100 }
2101
2102 if (instruction->IsAnd()) {
2103 if (use_imm)
2104 __ Andi(dst, lhs, rhs_imm);
2105 else
2106 __ And(dst, lhs, rhs_reg);
2107 } else if (instruction->IsOr()) {
2108 if (use_imm)
2109 __ Ori(dst, lhs, rhs_imm);
2110 else
2111 __ Or(dst, lhs, rhs_reg);
2112 } else if (instruction->IsXor()) {
2113 if (use_imm)
2114 __ Xori(dst, lhs, rhs_imm);
2115 else
2116 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002117 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002118 DCHECK(instruction->IsAdd() || instruction->IsSub());
2119 if (use_imm) {
2120 if (instruction->IsSub()) {
2121 rhs_imm = -rhs_imm;
2122 }
2123 if (IsInt<16>(rhs_imm)) {
2124 __ Addiu(dst, lhs, rhs_imm);
2125 } else {
2126 DCHECK(isR6);
2127 int16_t rhs_imm_high = High16Bits(rhs_imm);
2128 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2129 if (rhs_imm_low < 0) {
2130 rhs_imm_high += 1;
2131 }
2132 __ Aui(dst, lhs, rhs_imm_high);
2133 if (rhs_imm_low != 0) {
2134 __ Addiu(dst, dst, rhs_imm_low);
2135 }
2136 }
2137 } else if (instruction->IsAdd()) {
2138 __ Addu(dst, lhs, rhs_reg);
2139 } else {
2140 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002141 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002142 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002143 }
2144 break;
2145 }
2146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002147 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002148 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2149 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2150 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2151 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002152 Location rhs_location = locations->InAt(1);
2153 bool use_imm = rhs_location.IsConstant();
2154 if (!use_imm) {
2155 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2156 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2157 if (instruction->IsAnd()) {
2158 __ And(dst_low, lhs_low, rhs_low);
2159 __ And(dst_high, lhs_high, rhs_high);
2160 } else if (instruction->IsOr()) {
2161 __ Or(dst_low, lhs_low, rhs_low);
2162 __ Or(dst_high, lhs_high, rhs_high);
2163 } else if (instruction->IsXor()) {
2164 __ Xor(dst_low, lhs_low, rhs_low);
2165 __ Xor(dst_high, lhs_high, rhs_high);
2166 } else if (instruction->IsAdd()) {
2167 if (lhs_low == rhs_low) {
2168 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2169 __ Slt(TMP, lhs_low, ZERO);
2170 __ Addu(dst_low, lhs_low, rhs_low);
2171 } else {
2172 __ Addu(dst_low, lhs_low, rhs_low);
2173 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2174 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2175 }
2176 __ Addu(dst_high, lhs_high, rhs_high);
2177 __ Addu(dst_high, dst_high, TMP);
2178 } else {
2179 DCHECK(instruction->IsSub());
2180 __ Sltu(TMP, lhs_low, rhs_low);
2181 __ Subu(dst_low, lhs_low, rhs_low);
2182 __ Subu(dst_high, lhs_high, rhs_high);
2183 __ Subu(dst_high, dst_high, TMP);
2184 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002185 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002186 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2187 if (instruction->IsOr()) {
2188 uint32_t low = Low32Bits(value);
2189 uint32_t high = High32Bits(value);
2190 if (IsUint<16>(low)) {
2191 if (dst_low != lhs_low || low != 0) {
2192 __ Ori(dst_low, lhs_low, low);
2193 }
2194 } else {
2195 __ LoadConst32(TMP, low);
2196 __ Or(dst_low, lhs_low, TMP);
2197 }
2198 if (IsUint<16>(high)) {
2199 if (dst_high != lhs_high || high != 0) {
2200 __ Ori(dst_high, lhs_high, high);
2201 }
2202 } else {
2203 if (high != low) {
2204 __ LoadConst32(TMP, high);
2205 }
2206 __ Or(dst_high, lhs_high, TMP);
2207 }
2208 } else if (instruction->IsXor()) {
2209 uint32_t low = Low32Bits(value);
2210 uint32_t high = High32Bits(value);
2211 if (IsUint<16>(low)) {
2212 if (dst_low != lhs_low || low != 0) {
2213 __ Xori(dst_low, lhs_low, low);
2214 }
2215 } else {
2216 __ LoadConst32(TMP, low);
2217 __ Xor(dst_low, lhs_low, TMP);
2218 }
2219 if (IsUint<16>(high)) {
2220 if (dst_high != lhs_high || high != 0) {
2221 __ Xori(dst_high, lhs_high, high);
2222 }
2223 } else {
2224 if (high != low) {
2225 __ LoadConst32(TMP, high);
2226 }
2227 __ Xor(dst_high, lhs_high, TMP);
2228 }
2229 } else if (instruction->IsAnd()) {
2230 uint32_t low = Low32Bits(value);
2231 uint32_t high = High32Bits(value);
2232 if (IsUint<16>(low)) {
2233 __ Andi(dst_low, lhs_low, low);
2234 } else if (low != 0xFFFFFFFF) {
2235 __ LoadConst32(TMP, low);
2236 __ And(dst_low, lhs_low, TMP);
2237 } else if (dst_low != lhs_low) {
2238 __ Move(dst_low, lhs_low);
2239 }
2240 if (IsUint<16>(high)) {
2241 __ Andi(dst_high, lhs_high, high);
2242 } else if (high != 0xFFFFFFFF) {
2243 if (high != low) {
2244 __ LoadConst32(TMP, high);
2245 }
2246 __ And(dst_high, lhs_high, TMP);
2247 } else if (dst_high != lhs_high) {
2248 __ Move(dst_high, lhs_high);
2249 }
2250 } else {
2251 if (instruction->IsSub()) {
2252 value = -value;
2253 } else {
2254 DCHECK(instruction->IsAdd());
2255 }
2256 int32_t low = Low32Bits(value);
2257 int32_t high = High32Bits(value);
2258 if (IsInt<16>(low)) {
2259 if (dst_low != lhs_low || low != 0) {
2260 __ Addiu(dst_low, lhs_low, low);
2261 }
2262 if (low != 0) {
2263 __ Sltiu(AT, dst_low, low);
2264 }
2265 } else {
2266 __ LoadConst32(TMP, low);
2267 __ Addu(dst_low, lhs_low, TMP);
2268 __ Sltu(AT, dst_low, TMP);
2269 }
2270 if (IsInt<16>(high)) {
2271 if (dst_high != lhs_high || high != 0) {
2272 __ Addiu(dst_high, lhs_high, high);
2273 }
2274 } else {
2275 if (high != low) {
2276 __ LoadConst32(TMP, high);
2277 }
2278 __ Addu(dst_high, lhs_high, TMP);
2279 }
2280 if (low != 0) {
2281 __ Addu(dst_high, dst_high, AT);
2282 }
2283 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002284 }
2285 break;
2286 }
2287
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002288 case DataType::Type::kFloat32:
2289 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002290 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2291 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2292 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2293 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002294 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002295 __ AddS(dst, lhs, rhs);
2296 } else {
2297 __ AddD(dst, lhs, rhs);
2298 }
2299 } else {
2300 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002301 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002302 __ SubS(dst, lhs, rhs);
2303 } else {
2304 __ SubD(dst, lhs, rhs);
2305 }
2306 }
2307 break;
2308 }
2309
2310 default:
2311 LOG(FATAL) << "Unexpected binary operation type " << type;
2312 }
2313}
2314
2315void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002316 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002317
Vladimir Markoca6fff82017-10-03 14:49:14 +01002318 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002319 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002320 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002321 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002322 locations->SetInAt(0, Location::RequiresRegister());
2323 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2324 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2325 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002326 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002327 locations->SetInAt(0, Location::RequiresRegister());
2328 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2329 locations->SetOut(Location::RequiresRegister());
2330 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002331 default:
2332 LOG(FATAL) << "Unexpected shift type " << type;
2333 }
2334}
2335
2336static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2337
2338void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002339 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002340 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002341 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002342
2343 Location rhs_location = locations->InAt(1);
2344 bool use_imm = rhs_location.IsConstant();
2345 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2346 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002347 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002348 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002349 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002350 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2351 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002352
2353 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002354 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002355 Register dst = locations->Out().AsRegister<Register>();
2356 Register lhs = locations->InAt(0).AsRegister<Register>();
2357 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002358 if (shift_value == 0) {
2359 if (dst != lhs) {
2360 __ Move(dst, lhs);
2361 }
2362 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002363 __ Sll(dst, lhs, shift_value);
2364 } else if (instr->IsShr()) {
2365 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002366 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002367 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002368 } else {
2369 if (has_ins_rotr) {
2370 __ Rotr(dst, lhs, shift_value);
2371 } else {
2372 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2373 __ Srl(dst, lhs, shift_value);
2374 __ Or(dst, dst, TMP);
2375 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 }
2377 } else {
2378 if (instr->IsShl()) {
2379 __ Sllv(dst, lhs, rhs_reg);
2380 } else if (instr->IsShr()) {
2381 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002382 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002383 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002384 } else {
2385 if (has_ins_rotr) {
2386 __ Rotrv(dst, lhs, rhs_reg);
2387 } else {
2388 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002389 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2390 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2391 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2392 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2393 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002394 __ Sllv(TMP, lhs, TMP);
2395 __ Srlv(dst, lhs, rhs_reg);
2396 __ Or(dst, dst, TMP);
2397 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002398 }
2399 }
2400 break;
2401 }
2402
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002403 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002404 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2405 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2406 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2407 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2408 if (use_imm) {
2409 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002410 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002411 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002412 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002413 if (instr->IsShl()) {
2414 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2415 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2416 __ Sll(dst_low, lhs_low, shift_value);
2417 } else if (instr->IsShr()) {
2418 __ Srl(dst_low, lhs_low, shift_value);
2419 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2420 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002421 } else if (instr->IsUShr()) {
2422 __ Srl(dst_low, lhs_low, shift_value);
2423 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2424 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002425 } else {
2426 __ Srl(dst_low, lhs_low, shift_value);
2427 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2428 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002429 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002430 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002431 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002432 if (instr->IsShl()) {
2433 __ Sll(dst_low, lhs_low, shift_value);
2434 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2435 __ Sll(dst_high, lhs_high, shift_value);
2436 __ Or(dst_high, dst_high, TMP);
2437 } else if (instr->IsShr()) {
2438 __ Sra(dst_high, lhs_high, shift_value);
2439 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2440 __ Srl(dst_low, lhs_low, shift_value);
2441 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002442 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002443 __ Srl(dst_high, lhs_high, shift_value);
2444 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2445 __ Srl(dst_low, lhs_low, shift_value);
2446 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002447 } else {
2448 __ Srl(TMP, lhs_low, shift_value);
2449 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2450 __ Or(dst_low, dst_low, TMP);
2451 __ Srl(TMP, lhs_high, shift_value);
2452 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2453 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002454 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002455 }
2456 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002457 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002458 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002459 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002460 __ Move(dst_low, ZERO);
2461 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002462 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002463 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002464 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002465 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002466 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002467 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002468 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002469 // 64-bit rotation by 32 is just a swap.
2470 __ Move(dst_low, lhs_high);
2471 __ Move(dst_high, lhs_low);
2472 } else {
2473 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002474 __ Srl(dst_low, lhs_high, shift_value_high);
2475 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2476 __ Srl(dst_high, lhs_low, shift_value_high);
2477 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002478 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002479 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2480 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002481 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002482 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2483 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002484 __ Or(dst_high, dst_high, TMP);
2485 }
2486 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002487 }
2488 }
2489 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002490 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002491 MipsLabel done;
2492 if (instr->IsShl()) {
2493 __ Sllv(dst_low, lhs_low, rhs_reg);
2494 __ Nor(AT, ZERO, rhs_reg);
2495 __ Srl(TMP, lhs_low, 1);
2496 __ Srlv(TMP, TMP, AT);
2497 __ Sllv(dst_high, lhs_high, rhs_reg);
2498 __ Or(dst_high, dst_high, TMP);
2499 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002500 if (isR6) {
2501 __ Beqzc(TMP, &done, /* is_bare */ true);
2502 __ Move(dst_high, dst_low);
2503 __ Move(dst_low, ZERO);
2504 } else {
2505 __ Movn(dst_high, dst_low, TMP);
2506 __ Movn(dst_low, ZERO, TMP);
2507 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002508 } else if (instr->IsShr()) {
2509 __ Srav(dst_high, lhs_high, rhs_reg);
2510 __ Nor(AT, ZERO, rhs_reg);
2511 __ Sll(TMP, lhs_high, 1);
2512 __ Sllv(TMP, TMP, AT);
2513 __ Srlv(dst_low, lhs_low, rhs_reg);
2514 __ Or(dst_low, dst_low, TMP);
2515 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002516 if (isR6) {
2517 __ Beqzc(TMP, &done, /* is_bare */ true);
2518 __ Move(dst_low, dst_high);
2519 __ Sra(dst_high, dst_high, 31);
2520 } else {
2521 __ Sra(AT, dst_high, 31);
2522 __ Movn(dst_low, dst_high, TMP);
2523 __ Movn(dst_high, AT, TMP);
2524 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002525 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002526 __ Srlv(dst_high, lhs_high, rhs_reg);
2527 __ Nor(AT, ZERO, rhs_reg);
2528 __ Sll(TMP, lhs_high, 1);
2529 __ Sllv(TMP, TMP, AT);
2530 __ Srlv(dst_low, lhs_low, rhs_reg);
2531 __ Or(dst_low, dst_low, TMP);
2532 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002533 if (isR6) {
2534 __ Beqzc(TMP, &done, /* is_bare */ true);
2535 __ Move(dst_low, dst_high);
2536 __ Move(dst_high, ZERO);
2537 } else {
2538 __ Movn(dst_low, dst_high, TMP);
2539 __ Movn(dst_high, ZERO, TMP);
2540 }
2541 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002542 __ Nor(AT, ZERO, rhs_reg);
2543 __ Srlv(TMP, lhs_low, rhs_reg);
2544 __ Sll(dst_low, lhs_high, 1);
2545 __ Sllv(dst_low, dst_low, AT);
2546 __ Or(dst_low, dst_low, TMP);
2547 __ Srlv(TMP, lhs_high, rhs_reg);
2548 __ Sll(dst_high, lhs_low, 1);
2549 __ Sllv(dst_high, dst_high, AT);
2550 __ Or(dst_high, dst_high, TMP);
2551 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002552 if (isR6) {
2553 __ Beqzc(TMP, &done, /* is_bare */ true);
2554 __ Move(TMP, dst_high);
2555 __ Move(dst_high, dst_low);
2556 __ Move(dst_low, TMP);
2557 } else {
2558 __ Movn(AT, dst_high, TMP);
2559 __ Movn(dst_high, dst_low, TMP);
2560 __ Movn(dst_low, AT, TMP);
2561 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002562 }
2563 __ Bind(&done);
2564 }
2565 break;
2566 }
2567
2568 default:
2569 LOG(FATAL) << "Unexpected shift operation type " << type;
2570 }
2571}
2572
2573void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2574 HandleBinaryOp(instruction);
2575}
2576
2577void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2578 HandleBinaryOp(instruction);
2579}
2580
2581void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2582 HandleBinaryOp(instruction);
2583}
2584
2585void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2586 HandleBinaryOp(instruction);
2587}
2588
2589void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002590 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002591 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002592 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002593 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002594 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2595 object_array_get_with_read_barrier
2596 ? LocationSummary::kCallOnSlowPath
2597 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002598 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2599 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2600 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002601 locations->SetInAt(0, Location::RequiresRegister());
2602 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002603 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002604 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2605 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002606 // The output overlaps in the case of an object array get with
2607 // read barriers enabled: we do not want the move to overwrite the
2608 // array's location, as we need it to emit the read barrier.
2609 locations->SetOut(Location::RequiresRegister(),
2610 object_array_get_with_read_barrier
2611 ? Location::kOutputOverlap
2612 : Location::kNoOutputOverlap);
2613 }
2614 // We need a temporary register for the read barrier marking slow
2615 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2616 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002617 bool temp_needed = instruction->GetIndex()->IsConstant()
2618 ? !kBakerReadBarrierThunksEnableForFields
2619 : !kBakerReadBarrierThunksEnableForArrays;
2620 if (temp_needed) {
2621 locations->AddTemp(Location::RequiresRegister());
2622 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002623 }
2624}
2625
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002626static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2627 auto null_checker = [codegen, instruction]() {
2628 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002629 };
2630 return null_checker;
2631}
2632
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002633void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2634 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002635 Location obj_loc = locations->InAt(0);
2636 Register obj = obj_loc.AsRegister<Register>();
2637 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002638 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002639 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002640 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002641
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002642 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002643 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2644 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002645 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002646 case DataType::Type::kBool:
2647 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002648 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002649 if (index.IsConstant()) {
2650 size_t offset =
2651 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002652 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002653 } else {
2654 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002655 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656 }
2657 break;
2658 }
2659
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002660 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002661 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002662 if (index.IsConstant()) {
2663 size_t offset =
2664 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002665 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002666 } else {
2667 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002668 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002669 }
2670 break;
2671 }
2672
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002673 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002674 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002675 if (maybe_compressed_char_at) {
2676 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2677 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2678 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2679 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2680 "Expecting 0=compressed, 1=uncompressed");
2681 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002682 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002683 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2684 if (maybe_compressed_char_at) {
2685 MipsLabel uncompressed_load, done;
2686 __ Bnez(TMP, &uncompressed_load);
2687 __ LoadFromOffset(kLoadUnsignedByte,
2688 out,
2689 obj,
2690 data_offset + (const_index << TIMES_1));
2691 __ B(&done);
2692 __ Bind(&uncompressed_load);
2693 __ LoadFromOffset(kLoadUnsignedHalfword,
2694 out,
2695 obj,
2696 data_offset + (const_index << TIMES_2));
2697 __ Bind(&done);
2698 } else {
2699 __ LoadFromOffset(kLoadUnsignedHalfword,
2700 out,
2701 obj,
2702 data_offset + (const_index << TIMES_2),
2703 null_checker);
2704 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002705 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002706 Register index_reg = index.AsRegister<Register>();
2707 if (maybe_compressed_char_at) {
2708 MipsLabel uncompressed_load, done;
2709 __ Bnez(TMP, &uncompressed_load);
2710 __ Addu(TMP, obj, index_reg);
2711 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2712 __ B(&done);
2713 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002714 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002715 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2716 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002717 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2718 __ Addu(TMP, index_reg, obj);
2719 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002720 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002721 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002722 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2723 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002724 }
2725 break;
2726 }
2727
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002728 case DataType::Type::kInt16: {
2729 Register out = out_loc.AsRegister<Register>();
2730 if (index.IsConstant()) {
2731 size_t offset =
2732 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2733 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002734 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2735 __ Addu(TMP, index.AsRegister<Register>(), obj);
2736 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002737 } else {
2738 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2739 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2740 }
2741 break;
2742 }
2743
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002744 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002745 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002746 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002747 if (index.IsConstant()) {
2748 size_t offset =
2749 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002750 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002751 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2752 __ Addu(TMP, index.AsRegister<Register>(), obj);
2753 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002754 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002755 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002756 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002757 }
2758 break;
2759 }
2760
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002761 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002762 static_assert(
2763 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2764 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2765 // /* HeapReference<Object> */ out =
2766 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2767 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002768 bool temp_needed = index.IsConstant()
2769 ? !kBakerReadBarrierThunksEnableForFields
2770 : !kBakerReadBarrierThunksEnableForArrays;
2771 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002772 // Note that a potential implicit null check is handled in this
2773 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002774 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2775 if (index.IsConstant()) {
2776 // Array load with a constant index can be treated as a field load.
2777 size_t offset =
2778 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2779 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2780 out_loc,
2781 obj,
2782 offset,
2783 temp,
2784 /* needs_null_check */ false);
2785 } else {
2786 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2787 out_loc,
2788 obj,
2789 data_offset,
2790 index,
2791 temp,
2792 /* needs_null_check */ false);
2793 }
Alexey Frunze15958152017-02-09 19:08:30 -08002794 } else {
2795 Register out = out_loc.AsRegister<Register>();
2796 if (index.IsConstant()) {
2797 size_t offset =
2798 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2799 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2800 // If read barriers are enabled, emit read barriers other than
2801 // Baker's using a slow path (and also unpoison the loaded
2802 // reference, if heap poisoning is enabled).
2803 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2804 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002805 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002806 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2807 // If read barriers are enabled, emit read barriers other than
2808 // Baker's using a slow path (and also unpoison the loaded
2809 // reference, if heap poisoning is enabled).
2810 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2811 out_loc,
2812 out_loc,
2813 obj_loc,
2814 data_offset,
2815 index);
2816 }
2817 }
2818 break;
2819 }
2820
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002821 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002822 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002823 if (index.IsConstant()) {
2824 size_t offset =
2825 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002826 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002827 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2828 __ Addu(TMP, index.AsRegister<Register>(), obj);
2829 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002830 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002831 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002832 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002833 }
2834 break;
2835 }
2836
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002837 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002838 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002839 if (index.IsConstant()) {
2840 size_t offset =
2841 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002842 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002843 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2844 __ Addu(TMP, index.AsRegister<Register>(), obj);
2845 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002846 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002847 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002848 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002849 }
2850 break;
2851 }
2852
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002853 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002854 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002855 if (index.IsConstant()) {
2856 size_t offset =
2857 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002858 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002859 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2860 __ Addu(TMP, index.AsRegister<Register>(), obj);
2861 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002862 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002863 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002864 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002865 }
2866 break;
2867 }
2868
Aart Bik66c158e2018-01-31 12:55:04 -08002869 case DataType::Type::kUint32:
2870 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002871 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002872 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2873 UNREACHABLE();
2874 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002875}
2876
2877void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002878 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002879 locations->SetInAt(0, Location::RequiresRegister());
2880 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2881}
2882
2883void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2884 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002885 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002886 Register obj = locations->InAt(0).AsRegister<Register>();
2887 Register out = locations->Out().AsRegister<Register>();
2888 __ LoadFromOffset(kLoadWord, out, obj, offset);
2889 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002890 // Mask out compression flag from String's array length.
2891 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2892 __ Srl(out, out, 1u);
2893 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002894}
2895
Alexey Frunzef58b2482016-09-02 22:14:06 -07002896Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2897 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2898 ? Location::ConstantLocation(instruction->AsConstant())
2899 : Location::RequiresRegister();
2900}
2901
2902Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2903 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2904 // We can store a non-zero float or double constant without first loading it into the FPU,
2905 // but we should only prefer this if the constant has a single use.
2906 if (instruction->IsConstant() &&
2907 (instruction->AsConstant()->IsZeroBitPattern() ||
2908 instruction->GetUses().HasExactlyOneElement())) {
2909 return Location::ConstantLocation(instruction->AsConstant());
2910 // Otherwise fall through and require an FPU register for the constant.
2911 }
2912 return Location::RequiresFpuRegister();
2913}
2914
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002915void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002916 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002917
2918 bool needs_write_barrier =
2919 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2920 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2921
Vladimir Markoca6fff82017-10-03 14:49:14 +01002922 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002923 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002924 may_need_runtime_call_for_type_check ?
2925 LocationSummary::kCallOnSlowPath :
2926 LocationSummary::kNoCall);
2927
2928 locations->SetInAt(0, Location::RequiresRegister());
2929 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002931 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002932 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002933 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2934 }
2935 if (needs_write_barrier) {
2936 // Temporary register for the write barrier.
2937 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002938 }
2939}
2940
2941void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2942 LocationSummary* locations = instruction->GetLocations();
2943 Register obj = locations->InAt(0).AsRegister<Register>();
2944 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002945 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002946 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002947 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002948 bool needs_write_barrier =
2949 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002950 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002951 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002952
2953 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002955 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002956 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002957 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002958 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002959 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002960 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002961 __ Addu(base_reg, obj, index.AsRegister<Register>());
2962 }
2963 if (value_location.IsConstant()) {
2964 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2965 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2966 } else {
2967 Register value = value_location.AsRegister<Register>();
2968 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002969 }
2970 break;
2971 }
2972
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002973 case DataType::Type::kUint16:
2974 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002975 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002976 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002977 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002978 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2979 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002980 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002981 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002982 }
2983 if (value_location.IsConstant()) {
2984 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2985 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2986 } else {
2987 Register value = value_location.AsRegister<Register>();
2988 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002989 }
2990 break;
2991 }
2992
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002993 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002994 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2995 if (index.IsConstant()) {
2996 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002997 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2998 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002999 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003000 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003001 }
3002 if (value_location.IsConstant()) {
3003 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3004 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3005 } else {
3006 Register value = value_location.AsRegister<Register>();
3007 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3008 }
3009 break;
3010 }
3011
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003012 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08003013 if (value_location.IsConstant()) {
3014 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003015 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_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_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003018 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003019 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003020 }
Alexey Frunze15958152017-02-09 19:08:30 -08003021 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3022 DCHECK_EQ(value, 0);
3023 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3024 DCHECK(!needs_write_barrier);
3025 DCHECK(!may_need_runtime_call_for_type_check);
3026 break;
3027 }
3028
3029 DCHECK(needs_write_barrier);
3030 Register value = value_location.AsRegister<Register>();
3031 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3032 Register temp2 = TMP; // Doesn't need to survive slow path.
3033 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3034 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3035 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3036 MipsLabel done;
3037 SlowPathCodeMIPS* slow_path = nullptr;
3038
3039 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003040 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003041 codegen_->AddSlowPath(slow_path);
3042 if (instruction->GetValueCanBeNull()) {
3043 MipsLabel non_zero;
3044 __ Bnez(value, &non_zero);
3045 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3046 if (index.IsConstant()) {
3047 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003048 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3049 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003050 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003051 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003052 }
Alexey Frunze15958152017-02-09 19:08:30 -08003053 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3054 __ B(&done);
3055 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003056 }
Alexey Frunze15958152017-02-09 19:08:30 -08003057
3058 // Note that when read barriers are enabled, the type checks
3059 // are performed without read barriers. This is fine, even in
3060 // the case where a class object is in the from-space after
3061 // the flip, as a comparison involving such a type would not
3062 // produce a false positive; it may of course produce a false
3063 // negative, in which case we would take the ArraySet slow
3064 // path.
3065
3066 // /* HeapReference<Class> */ temp1 = obj->klass_
3067 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3068 __ MaybeUnpoisonHeapReference(temp1);
3069
3070 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3071 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3072 // /* HeapReference<Class> */ temp2 = value->klass_
3073 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3074 // If heap poisoning is enabled, no need to unpoison `temp1`
3075 // nor `temp2`, as we are comparing two poisoned references.
3076
3077 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3078 MipsLabel do_put;
3079 __ Beq(temp1, temp2, &do_put);
3080 // If heap poisoning is enabled, the `temp1` reference has
3081 // not been unpoisoned yet; unpoison it now.
3082 __ MaybeUnpoisonHeapReference(temp1);
3083
3084 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3085 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3086 // If heap poisoning is enabled, no need to unpoison
3087 // `temp1`, as we are comparing against null below.
3088 __ Bnez(temp1, slow_path->GetEntryLabel());
3089 __ Bind(&do_put);
3090 } else {
3091 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3092 }
3093 }
3094
3095 Register source = value;
3096 if (kPoisonHeapReferences) {
3097 // Note that in the case where `value` is a null reference,
3098 // we do not enter this block, as a null reference does not
3099 // need poisoning.
3100 __ Move(temp1, value);
3101 __ PoisonHeapReference(temp1);
3102 source = temp1;
3103 }
3104
3105 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3106 if (index.IsConstant()) {
3107 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003108 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003109 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003110 }
3111 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3112
3113 if (!may_need_runtime_call_for_type_check) {
3114 codegen_->MaybeRecordImplicitNullCheck(instruction);
3115 }
3116
3117 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3118
3119 if (done.IsLinked()) {
3120 __ Bind(&done);
3121 }
3122
3123 if (slow_path != nullptr) {
3124 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003125 }
3126 break;
3127 }
3128
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003129 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003130 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003131 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003132 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003133 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3134 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003135 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003136 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003137 }
3138 if (value_location.IsConstant()) {
3139 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3140 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3141 } else {
3142 Register value = value_location.AsRegisterPairLow<Register>();
3143 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003144 }
3145 break;
3146 }
3147
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003148 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003149 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003150 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003151 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003152 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3153 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003154 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003155 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003156 }
3157 if (value_location.IsConstant()) {
3158 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3159 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3160 } else {
3161 FRegister value = value_location.AsFpuRegister<FRegister>();
3162 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003163 }
3164 break;
3165 }
3166
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003167 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003168 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003169 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003170 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003171 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3172 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003173 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003174 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003175 }
3176 if (value_location.IsConstant()) {
3177 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3178 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3179 } else {
3180 FRegister value = value_location.AsFpuRegister<FRegister>();
3181 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003182 }
3183 break;
3184 }
3185
Aart Bik66c158e2018-01-31 12:55:04 -08003186 case DataType::Type::kUint32:
3187 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003188 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003189 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3190 UNREACHABLE();
3191 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003192}
3193
Lena Djokica2901602017-09-21 13:50:52 +02003194void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3195 HIntermediateArrayAddressIndex* instruction) {
3196 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003197 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003198
3199 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3200
3201 locations->SetInAt(0, Location::RequiresRegister());
3202 locations->SetInAt(1, Location::ConstantLocation(shift));
3203 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3204}
3205
3206void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3207 HIntermediateArrayAddressIndex* instruction) {
3208 LocationSummary* locations = instruction->GetLocations();
3209 Register index_reg = locations->InAt(0).AsRegister<Register>();
3210 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3211 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3212}
3213
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003214void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003215 RegisterSet caller_saves = RegisterSet::Empty();
3216 InvokeRuntimeCallingConvention calling_convention;
3217 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3218 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3219 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003220
3221 HInstruction* index = instruction->InputAt(0);
3222 HInstruction* length = instruction->InputAt(1);
3223
3224 bool const_index = false;
3225 bool const_length = false;
3226
3227 if (index->IsConstant()) {
3228 if (length->IsConstant()) {
3229 const_index = true;
3230 const_length = true;
3231 } else {
3232 int32_t index_value = index->AsIntConstant()->GetValue();
3233 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3234 const_index = true;
3235 }
3236 }
3237 } else if (length->IsConstant()) {
3238 int32_t length_value = length->AsIntConstant()->GetValue();
3239 if (IsUint<15>(length_value)) {
3240 const_length = true;
3241 }
3242 }
3243
3244 locations->SetInAt(0, const_index
3245 ? Location::ConstantLocation(index->AsConstant())
3246 : Location::RequiresRegister());
3247 locations->SetInAt(1, const_length
3248 ? Location::ConstantLocation(length->AsConstant())
3249 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003250}
3251
3252void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3253 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003254 Location index_loc = locations->InAt(0);
3255 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003256
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003257 if (length_loc.IsConstant()) {
3258 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3259 if (index_loc.IsConstant()) {
3260 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3261 if (index < 0 || index >= length) {
3262 BoundsCheckSlowPathMIPS* slow_path =
3263 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3264 codegen_->AddSlowPath(slow_path);
3265 __ B(slow_path->GetEntryLabel());
3266 } else {
3267 // Nothing to be done.
3268 }
3269 return;
3270 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003271
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003272 BoundsCheckSlowPathMIPS* slow_path =
3273 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3274 codegen_->AddSlowPath(slow_path);
3275 Register index = index_loc.AsRegister<Register>();
3276 if (length == 0) {
3277 __ B(slow_path->GetEntryLabel());
3278 } else if (length == 1) {
3279 __ Bnez(index, slow_path->GetEntryLabel());
3280 } else {
3281 DCHECK(IsUint<15>(length)) << length;
3282 __ Sltiu(TMP, index, length);
3283 __ Beqz(TMP, slow_path->GetEntryLabel());
3284 }
3285 } else {
3286 Register length = length_loc.AsRegister<Register>();
3287 BoundsCheckSlowPathMIPS* slow_path =
3288 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3289 codegen_->AddSlowPath(slow_path);
3290 if (index_loc.IsConstant()) {
3291 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3292 if (index < 0) {
3293 __ B(slow_path->GetEntryLabel());
3294 } else if (index == 0) {
3295 __ Blez(length, slow_path->GetEntryLabel());
3296 } else {
3297 DCHECK(IsInt<16>(index + 1)) << index;
3298 __ Sltiu(TMP, length, index + 1);
3299 __ Bnez(TMP, slow_path->GetEntryLabel());
3300 }
3301 } else {
3302 Register index = index_loc.AsRegister<Register>();
3303 __ Bgeu(index, length, slow_path->GetEntryLabel());
3304 }
3305 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003306}
3307
Alexey Frunze15958152017-02-09 19:08:30 -08003308// Temp is used for read barrier.
3309static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3310 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003311 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003312 (kUseBakerReadBarrier ||
3313 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3314 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3315 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3316 return 1;
3317 }
3318 return 0;
3319}
3320
3321// Extra temp is used for read barrier.
3322static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3323 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3324}
3325
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003326void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003327 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003328 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003329 LocationSummary* locations =
3330 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003331 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko3f413232018-02-12 18:39:15 +00003332 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
3333 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
3334 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
3335 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
3336 } else {
3337 locations->SetInAt(1, Location::RequiresRegister());
3338 }
Alexey Frunze15958152017-02-09 19:08:30 -08003339 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003340}
3341
3342void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003343 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003344 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003345 Location obj_loc = locations->InAt(0);
3346 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko3f413232018-02-12 18:39:15 +00003347 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08003348 Location temp_loc = locations->GetTemp(0);
3349 Register temp = temp_loc.AsRegister<Register>();
3350 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3351 DCHECK_LE(num_temps, 2u);
3352 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003353 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3354 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3355 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3356 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3357 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3358 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3359 const uint32_t object_array_data_offset =
3360 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3361 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003362
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003363 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003364 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003365 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3366 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003367 codegen_->AddSlowPath(slow_path);
3368
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003369 // Avoid this check if we know `obj` is not null.
3370 if (instruction->MustDoNullCheck()) {
3371 __ Beqz(obj, &done);
3372 }
3373
3374 switch (type_check_kind) {
3375 case TypeCheckKind::kExactCheck:
3376 case TypeCheckKind::kArrayCheck: {
3377 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003378 GenerateReferenceLoadTwoRegisters(instruction,
3379 temp_loc,
3380 obj_loc,
3381 class_offset,
3382 maybe_temp2_loc,
3383 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003384 // Jump to slow path for throwing the exception or doing a
3385 // more involved array check.
Vladimir Marko3f413232018-02-12 18:39:15 +00003386 __ Bne(temp, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003387 break;
3388 }
3389
3390 case TypeCheckKind::kAbstractClassCheck: {
3391 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003392 GenerateReferenceLoadTwoRegisters(instruction,
3393 temp_loc,
3394 obj_loc,
3395 class_offset,
3396 maybe_temp2_loc,
3397 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003398 // If the class is abstract, we eagerly fetch the super class of the
3399 // object to avoid doing a comparison we know will fail.
3400 MipsLabel loop;
3401 __ Bind(&loop);
3402 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003403 GenerateReferenceLoadOneRegister(instruction,
3404 temp_loc,
3405 super_offset,
3406 maybe_temp2_loc,
3407 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003408 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3409 // exception.
3410 __ Beqz(temp, slow_path->GetEntryLabel());
3411 // Otherwise, compare the classes.
Vladimir Marko3f413232018-02-12 18:39:15 +00003412 __ Bne(temp, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003413 break;
3414 }
3415
3416 case TypeCheckKind::kClassHierarchyCheck: {
3417 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003418 GenerateReferenceLoadTwoRegisters(instruction,
3419 temp_loc,
3420 obj_loc,
3421 class_offset,
3422 maybe_temp2_loc,
3423 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003424 // Walk over the class hierarchy to find a match.
3425 MipsLabel loop;
3426 __ Bind(&loop);
Vladimir Marko3f413232018-02-12 18:39:15 +00003427 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003428 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003429 GenerateReferenceLoadOneRegister(instruction,
3430 temp_loc,
3431 super_offset,
3432 maybe_temp2_loc,
3433 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003434 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3435 // exception. Otherwise, jump to the beginning of the loop.
3436 __ Bnez(temp, &loop);
3437 __ B(slow_path->GetEntryLabel());
3438 break;
3439 }
3440
3441 case TypeCheckKind::kArrayObjectCheck: {
3442 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003443 GenerateReferenceLoadTwoRegisters(instruction,
3444 temp_loc,
3445 obj_loc,
3446 class_offset,
3447 maybe_temp2_loc,
3448 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003449 // Do an exact check.
Vladimir Marko3f413232018-02-12 18:39:15 +00003450 __ Beq(temp, cls.AsRegister<Register>(), &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003451 // Otherwise, we need to check that the object's class is a non-primitive array.
3452 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003453 GenerateReferenceLoadOneRegister(instruction,
3454 temp_loc,
3455 component_offset,
3456 maybe_temp2_loc,
3457 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003458 // If the component type is null, jump to the slow path to throw the exception.
3459 __ Beqz(temp, slow_path->GetEntryLabel());
3460 // Otherwise, the object is indeed an array, further check that this component
3461 // type is not a primitive type.
3462 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3463 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3464 __ Bnez(temp, slow_path->GetEntryLabel());
3465 break;
3466 }
3467
3468 case TypeCheckKind::kUnresolvedCheck:
3469 // We always go into the type check slow path for the unresolved check case.
3470 // We cannot directly call the CheckCast runtime entry point
3471 // without resorting to a type checking slow path here (i.e. by
3472 // calling InvokeRuntime directly), as it would require to
3473 // assign fixed registers for the inputs of this HInstanceOf
3474 // instruction (following the runtime calling convention), which
3475 // might be cluttered by the potential first read barrier
3476 // emission at the beginning of this method.
3477 __ B(slow_path->GetEntryLabel());
3478 break;
3479
3480 case TypeCheckKind::kInterfaceCheck: {
3481 // Avoid read barriers to improve performance of the fast path. We can not get false
3482 // positives by doing this.
3483 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003484 GenerateReferenceLoadTwoRegisters(instruction,
3485 temp_loc,
3486 obj_loc,
3487 class_offset,
3488 maybe_temp2_loc,
3489 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003490 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003491 GenerateReferenceLoadTwoRegisters(instruction,
3492 temp_loc,
3493 temp_loc,
3494 iftable_offset,
3495 maybe_temp2_loc,
3496 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003497 // Iftable is never null.
3498 __ Lw(TMP, temp, array_length_offset);
3499 // Loop through the iftable and check if any class matches.
3500 MipsLabel loop;
3501 __ Bind(&loop);
3502 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3503 __ Beqz(TMP, slow_path->GetEntryLabel());
3504 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3505 __ MaybeUnpoisonHeapReference(AT);
3506 // Go to next interface.
3507 __ Addiu(TMP, TMP, -2);
3508 // Compare the classes and continue the loop if they do not match.
Vladimir Marko3f413232018-02-12 18:39:15 +00003509 __ Bne(AT, cls.AsRegister<Register>(), &loop);
3510 break;
3511 }
3512
3513 case TypeCheckKind::kBitstringCheck: {
3514 // /* HeapReference<Class> */ temp = obj->klass_
3515 GenerateReferenceLoadTwoRegisters(instruction,
3516 temp_loc,
3517 obj_loc,
3518 class_offset,
3519 maybe_temp2_loc,
3520 kWithoutReadBarrier);
3521
3522 GenerateBitstringTypeCheckCompare(instruction, temp);
3523 __ Bnez(temp, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003524 break;
3525 }
3526 }
3527
3528 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003529 __ Bind(slow_path->GetExitLabel());
3530}
3531
3532void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3533 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003534 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003535 locations->SetInAt(0, Location::RequiresRegister());
3536 if (check->HasUses()) {
3537 locations->SetOut(Location::SameAsFirstInput());
3538 }
3539}
3540
3541void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3542 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003543 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003544 check->GetLoadClass(),
3545 check,
3546 check->GetDexPc(),
3547 true);
3548 codegen_->AddSlowPath(slow_path);
3549 GenerateClassInitializationCheck(slow_path,
3550 check->GetLocations()->InAt(0).AsRegister<Register>());
3551}
3552
3553void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003554 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003555
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003556 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003557 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003558
3559 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003560 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003561 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003562 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003563 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003564 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003565 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003566 locations->SetInAt(0, Location::RequiresRegister());
3567 locations->SetInAt(1, Location::RequiresRegister());
3568 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3569 break;
3570
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003571 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003572 locations->SetInAt(0, Location::RequiresRegister());
3573 locations->SetInAt(1, Location::RequiresRegister());
3574 // Output overlaps because it is written before doing the low comparison.
3575 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3576 break;
3577
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003578 case DataType::Type::kFloat32:
3579 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003580 locations->SetInAt(0, Location::RequiresFpuRegister());
3581 locations->SetInAt(1, Location::RequiresFpuRegister());
3582 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003583 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003584
3585 default:
3586 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3587 }
3588}
3589
3590void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3591 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003592 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003593 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003594 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003595
3596 // 0 if: left == right
3597 // 1 if: left > right
3598 // -1 if: left < right
3599 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003600 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003601 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003602 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003603 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003604 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003605 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003606 Register lhs = locations->InAt(0).AsRegister<Register>();
3607 Register rhs = locations->InAt(1).AsRegister<Register>();
3608 __ Slt(TMP, lhs, rhs);
3609 __ Slt(res, rhs, lhs);
3610 __ Subu(res, res, TMP);
3611 break;
3612 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003613 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003614 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003615 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3616 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3617 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3618 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3619 // TODO: more efficient (direct) comparison with a constant.
3620 __ Slt(TMP, lhs_high, rhs_high);
3621 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3622 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3623 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3624 __ Sltu(TMP, lhs_low, rhs_low);
3625 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3626 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3627 __ Bind(&done);
3628 break;
3629 }
3630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003631 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003632 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003633 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3634 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3635 MipsLabel done;
3636 if (isR6) {
3637 __ CmpEqS(FTMP, lhs, rhs);
3638 __ LoadConst32(res, 0);
3639 __ Bc1nez(FTMP, &done);
3640 if (gt_bias) {
3641 __ CmpLtS(FTMP, lhs, rhs);
3642 __ LoadConst32(res, -1);
3643 __ Bc1nez(FTMP, &done);
3644 __ LoadConst32(res, 1);
3645 } else {
3646 __ CmpLtS(FTMP, rhs, lhs);
3647 __ LoadConst32(res, 1);
3648 __ Bc1nez(FTMP, &done);
3649 __ LoadConst32(res, -1);
3650 }
3651 } else {
3652 if (gt_bias) {
3653 __ ColtS(0, lhs, rhs);
3654 __ LoadConst32(res, -1);
3655 __ Bc1t(0, &done);
3656 __ CeqS(0, lhs, rhs);
3657 __ LoadConst32(res, 1);
3658 __ Movt(res, ZERO, 0);
3659 } else {
3660 __ ColtS(0, rhs, lhs);
3661 __ LoadConst32(res, 1);
3662 __ Bc1t(0, &done);
3663 __ CeqS(0, lhs, rhs);
3664 __ LoadConst32(res, -1);
3665 __ Movt(res, ZERO, 0);
3666 }
3667 }
3668 __ Bind(&done);
3669 break;
3670 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003671 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003672 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003673 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3674 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3675 MipsLabel done;
3676 if (isR6) {
3677 __ CmpEqD(FTMP, lhs, rhs);
3678 __ LoadConst32(res, 0);
3679 __ Bc1nez(FTMP, &done);
3680 if (gt_bias) {
3681 __ CmpLtD(FTMP, lhs, rhs);
3682 __ LoadConst32(res, -1);
3683 __ Bc1nez(FTMP, &done);
3684 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003685 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003686 __ CmpLtD(FTMP, rhs, lhs);
3687 __ LoadConst32(res, 1);
3688 __ Bc1nez(FTMP, &done);
3689 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003690 }
3691 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003692 if (gt_bias) {
3693 __ ColtD(0, lhs, rhs);
3694 __ LoadConst32(res, -1);
3695 __ Bc1t(0, &done);
3696 __ CeqD(0, lhs, rhs);
3697 __ LoadConst32(res, 1);
3698 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003699 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003700 __ ColtD(0, rhs, lhs);
3701 __ LoadConst32(res, 1);
3702 __ Bc1t(0, &done);
3703 __ CeqD(0, lhs, rhs);
3704 __ LoadConst32(res, -1);
3705 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003706 }
3707 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003708 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003709 break;
3710 }
3711
3712 default:
3713 LOG(FATAL) << "Unimplemented compare type " << in_type;
3714 }
3715}
3716
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003717void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003718 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003719 switch (instruction->InputAt(0)->GetType()) {
3720 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003721 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003722 locations->SetInAt(0, Location::RequiresRegister());
3723 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3724 break;
3725
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003726 case DataType::Type::kFloat32:
3727 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003728 locations->SetInAt(0, Location::RequiresFpuRegister());
3729 locations->SetInAt(1, Location::RequiresFpuRegister());
3730 break;
3731 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003732 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003733 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3734 }
3735}
3736
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003737void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003738 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003739 return;
3740 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003741
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003742 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003743 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003744
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003745 switch (type) {
3746 default:
3747 // Integer case.
3748 GenerateIntCompare(instruction->GetCondition(), locations);
3749 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003750
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003751 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003752 GenerateLongCompare(instruction->GetCondition(), locations);
3753 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003754
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003755 case DataType::Type::kFloat32:
3756 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003757 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3758 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003759 }
3760}
3761
Alexey Frunze7e99e052015-11-24 19:28:01 -08003762void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3763 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003764
3765 LocationSummary* locations = instruction->GetLocations();
3766 Location second = locations->InAt(1);
3767 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003768 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003769 DCHECK(imm == 1 || imm == -1);
3770
Lena Djokic4b8025c2017-12-21 16:15:50 +01003771 if (instruction->GetResultType() == DataType::Type::kInt32) {
3772 Register out = locations->Out().AsRegister<Register>();
3773 Register dividend = locations->InAt(0).AsRegister<Register>();
3774
3775 if (instruction->IsRem()) {
3776 __ Move(out, ZERO);
3777 } else {
3778 if (imm == -1) {
3779 __ Subu(out, ZERO, dividend);
3780 } else if (out != dividend) {
3781 __ Move(out, dividend);
3782 }
3783 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003784 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003785 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3786 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3787 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3788 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3789 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3790
3791 if (instruction->IsRem()) {
3792 __ Move(out_high, ZERO);
3793 __ Move(out_low, ZERO);
3794 } else {
3795 if (imm == -1) {
3796 __ Subu(out_low, ZERO, in_low);
3797 __ Sltu(AT, ZERO, out_low);
3798 __ Subu(out_high, ZERO, in_high);
3799 __ Subu(out_high, out_high, AT);
3800 } else {
3801 __ Move(out_low, in_low);
3802 __ Move(out_high, in_high);
3803 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003804 }
3805 }
3806}
3807
3808void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3809 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003810
3811 LocationSummary* locations = instruction->GetLocations();
3812 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003813 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3814 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003815 DCHECK(second.IsConstant());
3816
Lena Djokic4b8025c2017-12-21 16:15:50 +01003817 if (instruction->GetResultType() == DataType::Type::kInt32) {
3818 Register out = locations->Out().AsRegister<Register>();
3819 Register dividend = locations->InAt(0).AsRegister<Register>();
3820 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3821 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3822 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003823
Lena Djokic4b8025c2017-12-21 16:15:50 +01003824 if (instruction->IsDiv()) {
3825 if (ctz_imm == 1) {
3826 // Fast path for division by +/-2, which is very common.
3827 __ Srl(TMP, dividend, 31);
3828 } else {
3829 __ Sra(TMP, dividend, 31);
3830 __ Srl(TMP, TMP, 32 - ctz_imm);
3831 }
3832 __ Addu(out, dividend, TMP);
3833 __ Sra(out, out, ctz_imm);
3834 if (imm < 0) {
3835 __ Subu(out, ZERO, out);
3836 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003837 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003838 if (ctz_imm == 1) {
3839 // Fast path for modulo +/-2, which is very common.
3840 __ Sra(TMP, dividend, 31);
3841 __ Subu(out, dividend, TMP);
3842 __ Andi(out, out, 1);
3843 __ Addu(out, out, TMP);
3844 } else {
3845 __ Sra(TMP, dividend, 31);
3846 __ Srl(TMP, TMP, 32 - ctz_imm);
3847 __ Addu(out, dividend, TMP);
3848 if (IsUint<16>(abs_imm - 1)) {
3849 __ Andi(out, out, abs_imm - 1);
3850 } else {
3851 if (is_r2_or_newer) {
3852 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3853 } else {
3854 __ Sll(out, out, 32 - ctz_imm);
3855 __ Srl(out, out, 32 - ctz_imm);
3856 }
3857 }
3858 __ Subu(out, out, TMP);
3859 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003860 }
3861 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003862 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3863 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3864 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3865 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3866 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3867 int64_t imm = Int64FromConstant(second.GetConstant());
3868 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3869 int ctz_imm = CTZ(abs_imm);
3870
3871 if (instruction->IsDiv()) {
3872 if (ctz_imm < 32) {
3873 if (ctz_imm == 1) {
3874 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003875 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003876 __ Sra(AT, in_high, 31);
3877 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003878 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003879 __ Addu(AT, AT, in_low);
3880 __ Sltu(TMP, AT, in_low);
3881 __ Addu(out_high, in_high, TMP);
3882 __ Srl(out_low, AT, ctz_imm);
3883 if (is_r2_or_newer) {
3884 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3885 __ Sra(out_high, out_high, ctz_imm);
3886 } else {
3887 __ Sll(AT, out_high, 32 - ctz_imm);
3888 __ Sra(out_high, out_high, ctz_imm);
3889 __ Or(out_low, out_low, AT);
3890 }
3891 if (imm < 0) {
3892 __ Subu(out_low, ZERO, out_low);
3893 __ Sltu(AT, ZERO, out_low);
3894 __ Subu(out_high, ZERO, out_high);
3895 __ Subu(out_high, out_high, AT);
3896 }
3897 } else if (ctz_imm == 32) {
3898 __ Sra(AT, in_high, 31);
3899 __ Addu(AT, AT, in_low);
3900 __ Sltu(AT, AT, in_low);
3901 __ Addu(out_low, in_high, AT);
3902 if (imm < 0) {
3903 __ Srl(TMP, out_low, 31);
3904 __ Subu(out_low, ZERO, out_low);
3905 __ Sltu(AT, ZERO, out_low);
3906 __ Subu(out_high, TMP, AT);
3907 } else {
3908 __ Sra(out_high, out_low, 31);
3909 }
3910 } else if (ctz_imm < 63) {
3911 __ Sra(AT, in_high, 31);
3912 __ Srl(TMP, AT, 64 - ctz_imm);
3913 __ Addu(AT, AT, in_low);
3914 __ Sltu(AT, AT, in_low);
3915 __ Addu(out_low, in_high, AT);
3916 __ Addu(out_low, out_low, TMP);
3917 __ Sra(out_low, out_low, ctz_imm - 32);
3918 if (imm < 0) {
3919 __ Subu(out_low, ZERO, out_low);
3920 }
3921 __ Sra(out_high, out_low, 31);
3922 } else {
3923 DCHECK_LT(imm, 0);
3924 if (is_r6) {
3925 __ Aui(AT, in_high, 0x8000);
3926 } else {
3927 __ Lui(AT, 0x8000);
3928 __ Xor(AT, AT, in_high);
3929 }
3930 __ Or(AT, AT, in_low);
3931 __ Sltiu(out_low, AT, 1);
3932 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003933 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003934 } else {
3935 if ((ctz_imm == 1) && !is_r6) {
3936 __ Andi(AT, in_low, 1);
3937 __ Sll(TMP, in_low, 31);
3938 __ And(TMP, in_high, TMP);
3939 __ Sra(out_high, TMP, 31);
3940 __ Or(out_low, out_high, AT);
3941 } else if (ctz_imm < 32) {
3942 __ Sra(AT, in_high, 31);
3943 if (ctz_imm <= 16) {
3944 __ Andi(out_low, in_low, abs_imm - 1);
3945 } else if (is_r2_or_newer) {
3946 __ Ext(out_low, in_low, 0, ctz_imm);
3947 } else {
3948 __ Sll(out_low, in_low, 32 - ctz_imm);
3949 __ Srl(out_low, out_low, 32 - ctz_imm);
3950 }
3951 if (is_r6) {
3952 __ Selnez(out_high, AT, out_low);
3953 } else {
3954 __ Movz(AT, ZERO, out_low);
3955 __ Move(out_high, AT);
3956 }
3957 if (is_r2_or_newer) {
3958 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
3959 } else {
3960 __ Sll(AT, out_high, ctz_imm);
3961 __ Or(out_low, out_low, AT);
3962 }
3963 } else if (ctz_imm == 32) {
3964 __ Sra(AT, in_high, 31);
3965 __ Move(out_low, in_low);
3966 if (is_r6) {
3967 __ Selnez(out_high, AT, out_low);
3968 } else {
3969 __ Movz(AT, ZERO, out_low);
3970 __ Move(out_high, AT);
3971 }
3972 } else if (ctz_imm < 63) {
3973 __ Sra(AT, in_high, 31);
3974 __ Move(TMP, in_low);
3975 if (ctz_imm - 32 <= 16) {
3976 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
3977 } else if (is_r2_or_newer) {
3978 __ Ext(out_high, in_high, 0, ctz_imm - 32);
3979 } else {
3980 __ Sll(out_high, in_high, 64 - ctz_imm);
3981 __ Srl(out_high, out_high, 64 - ctz_imm);
3982 }
3983 __ Move(out_low, TMP);
3984 __ Or(TMP, TMP, out_high);
3985 if (is_r6) {
3986 __ Selnez(AT, AT, TMP);
3987 } else {
3988 __ Movz(AT, ZERO, TMP);
3989 }
3990 if (is_r2_or_newer) {
3991 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
3992 } else {
3993 __ Sll(AT, AT, ctz_imm - 32);
3994 __ Or(out_high, out_high, AT);
3995 }
3996 } else {
3997 if (is_r6) {
3998 __ Aui(AT, in_high, 0x8000);
3999 } else {
4000 __ Lui(AT, 0x8000);
4001 __ Xor(AT, AT, in_high);
4002 }
4003 __ Or(AT, AT, in_low);
4004 __ Sltiu(AT, AT, 1);
4005 __ Sll(AT, AT, 31);
4006 __ Move(out_low, in_low);
4007 __ Xor(out_high, in_high, AT);
4008 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08004009 }
4010 }
4011}
4012
4013void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
4014 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004015 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004016
4017 LocationSummary* locations = instruction->GetLocations();
4018 Location second = locations->InAt(1);
4019 DCHECK(second.IsConstant());
4020
4021 Register out = locations->Out().AsRegister<Register>();
4022 Register dividend = locations->InAt(0).AsRegister<Register>();
4023 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4024
4025 int64_t magic;
4026 int shift;
4027 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
4028
4029 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4030
4031 __ LoadConst32(TMP, magic);
4032 if (isR6) {
4033 __ MuhR6(TMP, dividend, TMP);
4034 } else {
4035 __ MultR2(dividend, TMP);
4036 __ Mfhi(TMP);
4037 }
4038 if (imm > 0 && magic < 0) {
4039 __ Addu(TMP, TMP, dividend);
4040 } else if (imm < 0 && magic > 0) {
4041 __ Subu(TMP, TMP, dividend);
4042 }
4043
4044 if (shift != 0) {
4045 __ Sra(TMP, TMP, shift);
4046 }
4047
4048 if (instruction->IsDiv()) {
4049 __ Sra(out, TMP, 31);
4050 __ Subu(out, TMP, out);
4051 } else {
4052 __ Sra(AT, TMP, 31);
4053 __ Subu(AT, TMP, AT);
4054 __ LoadConst32(TMP, imm);
4055 if (isR6) {
4056 __ MulR6(TMP, AT, TMP);
4057 } else {
4058 __ MulR2(TMP, AT, TMP);
4059 }
4060 __ Subu(out, dividend, TMP);
4061 }
4062}
4063
4064void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4065 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004067
4068 LocationSummary* locations = instruction->GetLocations();
4069 Register out = locations->Out().AsRegister<Register>();
4070 Location second = locations->InAt(1);
4071
4072 if (second.IsConstant()) {
4073 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4074 if (imm == 0) {
4075 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4076 } else if (imm == 1 || imm == -1) {
4077 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004078 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004079 DivRemByPowerOfTwo(instruction);
4080 } else {
4081 DCHECK(imm <= -2 || imm >= 2);
4082 GenerateDivRemWithAnyConstant(instruction);
4083 }
4084 } else {
4085 Register dividend = locations->InAt(0).AsRegister<Register>();
4086 Register divisor = second.AsRegister<Register>();
4087 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4088 if (instruction->IsDiv()) {
4089 if (isR6) {
4090 __ DivR6(out, dividend, divisor);
4091 } else {
4092 __ DivR2(out, dividend, divisor);
4093 }
4094 } else {
4095 if (isR6) {
4096 __ ModR6(out, dividend, divisor);
4097 } else {
4098 __ ModR2(out, dividend, divisor);
4099 }
4100 }
4101 }
4102}
4103
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004104void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004105 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004106 bool call_long_div = false;
4107 if (type == DataType::Type::kInt64) {
4108 if (div->InputAt(1)->IsConstant()) {
4109 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4110 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4111 } else {
4112 call_long_div = true;
4113 }
4114 }
4115 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004116 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004117 : LocationSummary::kNoCall;
4118
Vladimir Markoca6fff82017-10-03 14:49:14 +01004119 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004120
4121 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004122 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004123 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004124 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004125 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4126 break;
4127
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004128 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004129 if (call_long_div) {
4130 InvokeRuntimeCallingConvention calling_convention;
4131 locations->SetInAt(0, Location::RegisterPairLocation(
4132 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4133 locations->SetInAt(1, Location::RegisterPairLocation(
4134 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4135 locations->SetOut(calling_convention.GetReturnLocation(type));
4136 } else {
4137 locations->SetInAt(0, Location::RequiresRegister());
4138 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4139 locations->SetOut(Location::RequiresRegister());
4140 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004141 break;
4142 }
4143
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004144 case DataType::Type::kFloat32:
4145 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004146 locations->SetInAt(0, Location::RequiresFpuRegister());
4147 locations->SetInAt(1, Location::RequiresFpuRegister());
4148 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4149 break;
4150
4151 default:
4152 LOG(FATAL) << "Unexpected div type " << type;
4153 }
4154}
4155
4156void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004157 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004158 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004159
4160 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004161 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004162 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004163 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004164 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004165 if (locations->InAt(1).IsConstant()) {
4166 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4167 if (imm == 0) {
4168 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4169 } else if (imm == 1 || imm == -1) {
4170 DivRemOneOrMinusOne(instruction);
4171 } else {
4172 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4173 DivRemByPowerOfTwo(instruction);
4174 }
4175 } else {
4176 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4177 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4178 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004179 break;
4180 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004181 case DataType::Type::kFloat32:
4182 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004183 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4184 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4185 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004186 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004187 __ DivS(dst, lhs, rhs);
4188 } else {
4189 __ DivD(dst, lhs, rhs);
4190 }
4191 break;
4192 }
4193 default:
4194 LOG(FATAL) << "Unexpected div type " << type;
4195 }
4196}
4197
4198void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004199 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004200 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004201}
4202
4203void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004204 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004205 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004206 codegen_->AddSlowPath(slow_path);
4207 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004208 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004209
4210 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004211 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004212 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004213 case DataType::Type::kInt8:
4214 case DataType::Type::kUint16:
4215 case DataType::Type::kInt16:
4216 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004217 if (value.IsConstant()) {
4218 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4219 __ B(slow_path->GetEntryLabel());
4220 } else {
4221 // A division by a non-null constant is valid. We don't need to perform
4222 // any check, so simply fall through.
4223 }
4224 } else {
4225 DCHECK(value.IsRegister()) << value;
4226 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4227 }
4228 break;
4229 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004230 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004231 if (value.IsConstant()) {
4232 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4233 __ B(slow_path->GetEntryLabel());
4234 } else {
4235 // A division by a non-null constant is valid. We don't need to perform
4236 // any check, so simply fall through.
4237 }
4238 } else {
4239 DCHECK(value.IsRegisterPair()) << value;
4240 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4241 __ Beqz(TMP, slow_path->GetEntryLabel());
4242 }
4243 break;
4244 }
4245 default:
4246 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4247 }
4248}
4249
4250void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4251 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004252 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004253 locations->SetOut(Location::ConstantLocation(constant));
4254}
4255
4256void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4257 // Will be generated at use site.
4258}
4259
4260void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4261 exit->SetLocations(nullptr);
4262}
4263
4264void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4265}
4266
4267void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4268 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004269 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004270 locations->SetOut(Location::ConstantLocation(constant));
4271}
4272
4273void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4274 // Will be generated at use site.
4275}
4276
4277void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4278 got->SetLocations(nullptr);
4279}
4280
4281void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004282 if (successor->IsExitBlock()) {
4283 DCHECK(got->GetPrevious()->AlwaysThrows());
4284 return; // no code needed
4285 }
4286
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004287 HBasicBlock* block = got->GetBlock();
4288 HInstruction* previous = got->GetPrevious();
4289 HLoopInformation* info = block->GetLoopInformation();
4290
4291 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004292 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4293 __ Lw(AT, SP, kCurrentMethodStackOffset);
4294 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4295 __ Addiu(TMP, TMP, 1);
4296 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4297 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004298 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4299 return;
4300 }
4301 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4302 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4303 }
4304 if (!codegen_->GoesToNextBlock(block, successor)) {
4305 __ B(codegen_->GetLabelOf(successor));
4306 }
4307}
4308
4309void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4310 HandleGoto(got, got->GetSuccessor());
4311}
4312
4313void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4314 try_boundary->SetLocations(nullptr);
4315}
4316
4317void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4318 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4319 if (!successor->IsExitBlock()) {
4320 HandleGoto(try_boundary, successor);
4321 }
4322}
4323
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004324void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4325 LocationSummary* locations) {
4326 Register dst = locations->Out().AsRegister<Register>();
4327 Register lhs = locations->InAt(0).AsRegister<Register>();
4328 Location rhs_location = locations->InAt(1);
4329 Register rhs_reg = ZERO;
4330 int64_t rhs_imm = 0;
4331 bool use_imm = rhs_location.IsConstant();
4332 if (use_imm) {
4333 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4334 } else {
4335 rhs_reg = rhs_location.AsRegister<Register>();
4336 }
4337
4338 switch (cond) {
4339 case kCondEQ:
4340 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004341 if (use_imm && IsInt<16>(-rhs_imm)) {
4342 if (rhs_imm == 0) {
4343 if (cond == kCondEQ) {
4344 __ Sltiu(dst, lhs, 1);
4345 } else {
4346 __ Sltu(dst, ZERO, lhs);
4347 }
4348 } else {
4349 __ Addiu(dst, lhs, -rhs_imm);
4350 if (cond == kCondEQ) {
4351 __ Sltiu(dst, dst, 1);
4352 } else {
4353 __ Sltu(dst, ZERO, dst);
4354 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004355 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004356 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004357 if (use_imm && IsUint<16>(rhs_imm)) {
4358 __ Xori(dst, lhs, rhs_imm);
4359 } else {
4360 if (use_imm) {
4361 rhs_reg = TMP;
4362 __ LoadConst32(rhs_reg, rhs_imm);
4363 }
4364 __ Xor(dst, lhs, rhs_reg);
4365 }
4366 if (cond == kCondEQ) {
4367 __ Sltiu(dst, dst, 1);
4368 } else {
4369 __ Sltu(dst, ZERO, dst);
4370 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004371 }
4372 break;
4373
4374 case kCondLT:
4375 case kCondGE:
4376 if (use_imm && IsInt<16>(rhs_imm)) {
4377 __ Slti(dst, lhs, rhs_imm);
4378 } else {
4379 if (use_imm) {
4380 rhs_reg = TMP;
4381 __ LoadConst32(rhs_reg, rhs_imm);
4382 }
4383 __ Slt(dst, lhs, rhs_reg);
4384 }
4385 if (cond == kCondGE) {
4386 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4387 // only the slt instruction but no sge.
4388 __ Xori(dst, dst, 1);
4389 }
4390 break;
4391
4392 case kCondLE:
4393 case kCondGT:
4394 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4395 // Simulate lhs <= rhs via lhs < rhs + 1.
4396 __ Slti(dst, lhs, rhs_imm + 1);
4397 if (cond == kCondGT) {
4398 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4399 // only the slti instruction but no sgti.
4400 __ Xori(dst, dst, 1);
4401 }
4402 } else {
4403 if (use_imm) {
4404 rhs_reg = TMP;
4405 __ LoadConst32(rhs_reg, rhs_imm);
4406 }
4407 __ Slt(dst, rhs_reg, lhs);
4408 if (cond == kCondLE) {
4409 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4410 // only the slt instruction but no sle.
4411 __ Xori(dst, dst, 1);
4412 }
4413 }
4414 break;
4415
4416 case kCondB:
4417 case kCondAE:
4418 if (use_imm && IsInt<16>(rhs_imm)) {
4419 // Sltiu sign-extends its 16-bit immediate operand before
4420 // the comparison and thus lets us compare directly with
4421 // unsigned values in the ranges [0, 0x7fff] and
4422 // [0xffff8000, 0xffffffff].
4423 __ Sltiu(dst, lhs, rhs_imm);
4424 } else {
4425 if (use_imm) {
4426 rhs_reg = TMP;
4427 __ LoadConst32(rhs_reg, rhs_imm);
4428 }
4429 __ Sltu(dst, lhs, rhs_reg);
4430 }
4431 if (cond == kCondAE) {
4432 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4433 // only the sltu instruction but no sgeu.
4434 __ Xori(dst, dst, 1);
4435 }
4436 break;
4437
4438 case kCondBE:
4439 case kCondA:
4440 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4441 // Simulate lhs <= rhs via lhs < rhs + 1.
4442 // Note that this only works if rhs + 1 does not overflow
4443 // to 0, hence the check above.
4444 // Sltiu sign-extends its 16-bit immediate operand before
4445 // the comparison and thus lets us compare directly with
4446 // unsigned values in the ranges [0, 0x7fff] and
4447 // [0xffff8000, 0xffffffff].
4448 __ Sltiu(dst, lhs, rhs_imm + 1);
4449 if (cond == kCondA) {
4450 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4451 // only the sltiu instruction but no sgtiu.
4452 __ Xori(dst, dst, 1);
4453 }
4454 } else {
4455 if (use_imm) {
4456 rhs_reg = TMP;
4457 __ LoadConst32(rhs_reg, rhs_imm);
4458 }
4459 __ Sltu(dst, rhs_reg, lhs);
4460 if (cond == kCondBE) {
4461 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4462 // only the sltu instruction but no sleu.
4463 __ Xori(dst, dst, 1);
4464 }
4465 }
4466 break;
4467 }
4468}
4469
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004470bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4471 LocationSummary* input_locations,
4472 Register dst) {
4473 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4474 Location rhs_location = input_locations->InAt(1);
4475 Register rhs_reg = ZERO;
4476 int64_t rhs_imm = 0;
4477 bool use_imm = rhs_location.IsConstant();
4478 if (use_imm) {
4479 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4480 } else {
4481 rhs_reg = rhs_location.AsRegister<Register>();
4482 }
4483
4484 switch (cond) {
4485 case kCondEQ:
4486 case kCondNE:
4487 if (use_imm && IsInt<16>(-rhs_imm)) {
4488 __ Addiu(dst, lhs, -rhs_imm);
4489 } else if (use_imm && IsUint<16>(rhs_imm)) {
4490 __ Xori(dst, lhs, rhs_imm);
4491 } else {
4492 if (use_imm) {
4493 rhs_reg = TMP;
4494 __ LoadConst32(rhs_reg, rhs_imm);
4495 }
4496 __ Xor(dst, lhs, rhs_reg);
4497 }
4498 return (cond == kCondEQ);
4499
4500 case kCondLT:
4501 case kCondGE:
4502 if (use_imm && IsInt<16>(rhs_imm)) {
4503 __ Slti(dst, lhs, rhs_imm);
4504 } else {
4505 if (use_imm) {
4506 rhs_reg = TMP;
4507 __ LoadConst32(rhs_reg, rhs_imm);
4508 }
4509 __ Slt(dst, lhs, rhs_reg);
4510 }
4511 return (cond == kCondGE);
4512
4513 case kCondLE:
4514 case kCondGT:
4515 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4516 // Simulate lhs <= rhs via lhs < rhs + 1.
4517 __ Slti(dst, lhs, rhs_imm + 1);
4518 return (cond == kCondGT);
4519 } else {
4520 if (use_imm) {
4521 rhs_reg = TMP;
4522 __ LoadConst32(rhs_reg, rhs_imm);
4523 }
4524 __ Slt(dst, rhs_reg, lhs);
4525 return (cond == kCondLE);
4526 }
4527
4528 case kCondB:
4529 case kCondAE:
4530 if (use_imm && IsInt<16>(rhs_imm)) {
4531 // Sltiu sign-extends its 16-bit immediate operand before
4532 // the comparison and thus lets us compare directly with
4533 // unsigned values in the ranges [0, 0x7fff] and
4534 // [0xffff8000, 0xffffffff].
4535 __ Sltiu(dst, lhs, rhs_imm);
4536 } else {
4537 if (use_imm) {
4538 rhs_reg = TMP;
4539 __ LoadConst32(rhs_reg, rhs_imm);
4540 }
4541 __ Sltu(dst, lhs, rhs_reg);
4542 }
4543 return (cond == kCondAE);
4544
4545 case kCondBE:
4546 case kCondA:
4547 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4548 // Simulate lhs <= rhs via lhs < rhs + 1.
4549 // Note that this only works if rhs + 1 does not overflow
4550 // to 0, hence the check above.
4551 // Sltiu sign-extends its 16-bit immediate operand before
4552 // the comparison and thus lets us compare directly with
4553 // unsigned values in the ranges [0, 0x7fff] and
4554 // [0xffff8000, 0xffffffff].
4555 __ Sltiu(dst, lhs, rhs_imm + 1);
4556 return (cond == kCondA);
4557 } else {
4558 if (use_imm) {
4559 rhs_reg = TMP;
4560 __ LoadConst32(rhs_reg, rhs_imm);
4561 }
4562 __ Sltu(dst, rhs_reg, lhs);
4563 return (cond == kCondBE);
4564 }
4565 }
4566}
4567
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004568void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4569 LocationSummary* locations,
4570 MipsLabel* label) {
4571 Register lhs = locations->InAt(0).AsRegister<Register>();
4572 Location rhs_location = locations->InAt(1);
4573 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004574 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004575 bool use_imm = rhs_location.IsConstant();
4576 if (use_imm) {
4577 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4578 } else {
4579 rhs_reg = rhs_location.AsRegister<Register>();
4580 }
4581
4582 if (use_imm && rhs_imm == 0) {
4583 switch (cond) {
4584 case kCondEQ:
4585 case kCondBE: // <= 0 if zero
4586 __ Beqz(lhs, label);
4587 break;
4588 case kCondNE:
4589 case kCondA: // > 0 if non-zero
4590 __ Bnez(lhs, label);
4591 break;
4592 case kCondLT:
4593 __ Bltz(lhs, label);
4594 break;
4595 case kCondGE:
4596 __ Bgez(lhs, label);
4597 break;
4598 case kCondLE:
4599 __ Blez(lhs, label);
4600 break;
4601 case kCondGT:
4602 __ Bgtz(lhs, label);
4603 break;
4604 case kCondB: // always false
4605 break;
4606 case kCondAE: // always true
4607 __ B(label);
4608 break;
4609 }
4610 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004611 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4612 if (isR6 || !use_imm) {
4613 if (use_imm) {
4614 rhs_reg = TMP;
4615 __ LoadConst32(rhs_reg, rhs_imm);
4616 }
4617 switch (cond) {
4618 case kCondEQ:
4619 __ Beq(lhs, rhs_reg, label);
4620 break;
4621 case kCondNE:
4622 __ Bne(lhs, rhs_reg, label);
4623 break;
4624 case kCondLT:
4625 __ Blt(lhs, rhs_reg, label);
4626 break;
4627 case kCondGE:
4628 __ Bge(lhs, rhs_reg, label);
4629 break;
4630 case kCondLE:
4631 __ Bge(rhs_reg, lhs, label);
4632 break;
4633 case kCondGT:
4634 __ Blt(rhs_reg, lhs, label);
4635 break;
4636 case kCondB:
4637 __ Bltu(lhs, rhs_reg, label);
4638 break;
4639 case kCondAE:
4640 __ Bgeu(lhs, rhs_reg, label);
4641 break;
4642 case kCondBE:
4643 __ Bgeu(rhs_reg, lhs, label);
4644 break;
4645 case kCondA:
4646 __ Bltu(rhs_reg, lhs, label);
4647 break;
4648 }
4649 } else {
4650 // Special cases for more efficient comparison with constants on R2.
4651 switch (cond) {
4652 case kCondEQ:
4653 __ LoadConst32(TMP, rhs_imm);
4654 __ Beq(lhs, TMP, label);
4655 break;
4656 case kCondNE:
4657 __ LoadConst32(TMP, rhs_imm);
4658 __ Bne(lhs, TMP, label);
4659 break;
4660 case kCondLT:
4661 if (IsInt<16>(rhs_imm)) {
4662 __ Slti(TMP, lhs, rhs_imm);
4663 __ Bnez(TMP, label);
4664 } else {
4665 __ LoadConst32(TMP, rhs_imm);
4666 __ Blt(lhs, TMP, label);
4667 }
4668 break;
4669 case kCondGE:
4670 if (IsInt<16>(rhs_imm)) {
4671 __ Slti(TMP, lhs, rhs_imm);
4672 __ Beqz(TMP, label);
4673 } else {
4674 __ LoadConst32(TMP, rhs_imm);
4675 __ Bge(lhs, TMP, label);
4676 }
4677 break;
4678 case kCondLE:
4679 if (IsInt<16>(rhs_imm + 1)) {
4680 // Simulate lhs <= rhs via lhs < rhs + 1.
4681 __ Slti(TMP, lhs, rhs_imm + 1);
4682 __ Bnez(TMP, label);
4683 } else {
4684 __ LoadConst32(TMP, rhs_imm);
4685 __ Bge(TMP, lhs, label);
4686 }
4687 break;
4688 case kCondGT:
4689 if (IsInt<16>(rhs_imm + 1)) {
4690 // Simulate lhs > rhs via !(lhs < rhs + 1).
4691 __ Slti(TMP, lhs, rhs_imm + 1);
4692 __ Beqz(TMP, label);
4693 } else {
4694 __ LoadConst32(TMP, rhs_imm);
4695 __ Blt(TMP, lhs, label);
4696 }
4697 break;
4698 case kCondB:
4699 if (IsInt<16>(rhs_imm)) {
4700 __ Sltiu(TMP, lhs, rhs_imm);
4701 __ Bnez(TMP, label);
4702 } else {
4703 __ LoadConst32(TMP, rhs_imm);
4704 __ Bltu(lhs, TMP, label);
4705 }
4706 break;
4707 case kCondAE:
4708 if (IsInt<16>(rhs_imm)) {
4709 __ Sltiu(TMP, lhs, rhs_imm);
4710 __ Beqz(TMP, label);
4711 } else {
4712 __ LoadConst32(TMP, rhs_imm);
4713 __ Bgeu(lhs, TMP, label);
4714 }
4715 break;
4716 case kCondBE:
4717 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4718 // Simulate lhs <= rhs via lhs < rhs + 1.
4719 // Note that this only works if rhs + 1 does not overflow
4720 // to 0, hence the check above.
4721 __ Sltiu(TMP, lhs, rhs_imm + 1);
4722 __ Bnez(TMP, label);
4723 } else {
4724 __ LoadConst32(TMP, rhs_imm);
4725 __ Bgeu(TMP, lhs, label);
4726 }
4727 break;
4728 case kCondA:
4729 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4730 // Simulate lhs > rhs via !(lhs < rhs + 1).
4731 // Note that this only works if rhs + 1 does not overflow
4732 // to 0, hence the check above.
4733 __ Sltiu(TMP, lhs, rhs_imm + 1);
4734 __ Beqz(TMP, label);
4735 } else {
4736 __ LoadConst32(TMP, rhs_imm);
4737 __ Bltu(TMP, lhs, label);
4738 }
4739 break;
4740 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004741 }
4742 }
4743}
4744
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004745void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4746 LocationSummary* locations) {
4747 Register dst = locations->Out().AsRegister<Register>();
4748 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4749 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4750 Location rhs_location = locations->InAt(1);
4751 Register rhs_high = ZERO;
4752 Register rhs_low = ZERO;
4753 int64_t imm = 0;
4754 uint32_t imm_high = 0;
4755 uint32_t imm_low = 0;
4756 bool use_imm = rhs_location.IsConstant();
4757 if (use_imm) {
4758 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4759 imm_high = High32Bits(imm);
4760 imm_low = Low32Bits(imm);
4761 } else {
4762 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4763 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4764 }
4765 if (use_imm && imm == 0) {
4766 switch (cond) {
4767 case kCondEQ:
4768 case kCondBE: // <= 0 if zero
4769 __ Or(dst, lhs_high, lhs_low);
4770 __ Sltiu(dst, dst, 1);
4771 break;
4772 case kCondNE:
4773 case kCondA: // > 0 if non-zero
4774 __ Or(dst, lhs_high, lhs_low);
4775 __ Sltu(dst, ZERO, dst);
4776 break;
4777 case kCondLT:
4778 __ Slt(dst, lhs_high, ZERO);
4779 break;
4780 case kCondGE:
4781 __ Slt(dst, lhs_high, ZERO);
4782 __ Xori(dst, dst, 1);
4783 break;
4784 case kCondLE:
4785 __ Or(TMP, lhs_high, lhs_low);
4786 __ Sra(AT, lhs_high, 31);
4787 __ Sltu(dst, AT, TMP);
4788 __ Xori(dst, dst, 1);
4789 break;
4790 case kCondGT:
4791 __ Or(TMP, lhs_high, lhs_low);
4792 __ Sra(AT, lhs_high, 31);
4793 __ Sltu(dst, AT, TMP);
4794 break;
4795 case kCondB: // always false
4796 __ Andi(dst, dst, 0);
4797 break;
4798 case kCondAE: // always true
4799 __ Ori(dst, ZERO, 1);
4800 break;
4801 }
4802 } else if (use_imm) {
4803 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4804 switch (cond) {
4805 case kCondEQ:
4806 __ LoadConst32(TMP, imm_high);
4807 __ Xor(TMP, TMP, lhs_high);
4808 __ LoadConst32(AT, imm_low);
4809 __ Xor(AT, AT, lhs_low);
4810 __ Or(dst, TMP, AT);
4811 __ Sltiu(dst, dst, 1);
4812 break;
4813 case kCondNE:
4814 __ LoadConst32(TMP, imm_high);
4815 __ Xor(TMP, TMP, lhs_high);
4816 __ LoadConst32(AT, imm_low);
4817 __ Xor(AT, AT, lhs_low);
4818 __ Or(dst, TMP, AT);
4819 __ Sltu(dst, ZERO, dst);
4820 break;
4821 case kCondLT:
4822 case kCondGE:
4823 if (dst == lhs_low) {
4824 __ LoadConst32(TMP, imm_low);
4825 __ Sltu(dst, lhs_low, TMP);
4826 }
4827 __ LoadConst32(TMP, imm_high);
4828 __ Slt(AT, lhs_high, TMP);
4829 __ Slt(TMP, TMP, lhs_high);
4830 if (dst != lhs_low) {
4831 __ LoadConst32(dst, imm_low);
4832 __ Sltu(dst, lhs_low, dst);
4833 }
4834 __ Slt(dst, TMP, dst);
4835 __ Or(dst, dst, AT);
4836 if (cond == kCondGE) {
4837 __ Xori(dst, dst, 1);
4838 }
4839 break;
4840 case kCondGT:
4841 case kCondLE:
4842 if (dst == lhs_low) {
4843 __ LoadConst32(TMP, imm_low);
4844 __ Sltu(dst, TMP, lhs_low);
4845 }
4846 __ LoadConst32(TMP, imm_high);
4847 __ Slt(AT, TMP, lhs_high);
4848 __ Slt(TMP, lhs_high, TMP);
4849 if (dst != lhs_low) {
4850 __ LoadConst32(dst, imm_low);
4851 __ Sltu(dst, dst, lhs_low);
4852 }
4853 __ Slt(dst, TMP, dst);
4854 __ Or(dst, dst, AT);
4855 if (cond == kCondLE) {
4856 __ Xori(dst, dst, 1);
4857 }
4858 break;
4859 case kCondB:
4860 case kCondAE:
4861 if (dst == lhs_low) {
4862 __ LoadConst32(TMP, imm_low);
4863 __ Sltu(dst, lhs_low, TMP);
4864 }
4865 __ LoadConst32(TMP, imm_high);
4866 __ Sltu(AT, lhs_high, TMP);
4867 __ Sltu(TMP, TMP, lhs_high);
4868 if (dst != lhs_low) {
4869 __ LoadConst32(dst, imm_low);
4870 __ Sltu(dst, lhs_low, dst);
4871 }
4872 __ Slt(dst, TMP, dst);
4873 __ Or(dst, dst, AT);
4874 if (cond == kCondAE) {
4875 __ Xori(dst, dst, 1);
4876 }
4877 break;
4878 case kCondA:
4879 case kCondBE:
4880 if (dst == lhs_low) {
4881 __ LoadConst32(TMP, imm_low);
4882 __ Sltu(dst, TMP, lhs_low);
4883 }
4884 __ LoadConst32(TMP, imm_high);
4885 __ Sltu(AT, TMP, lhs_high);
4886 __ Sltu(TMP, lhs_high, TMP);
4887 if (dst != lhs_low) {
4888 __ LoadConst32(dst, imm_low);
4889 __ Sltu(dst, dst, lhs_low);
4890 }
4891 __ Slt(dst, TMP, dst);
4892 __ Or(dst, dst, AT);
4893 if (cond == kCondBE) {
4894 __ Xori(dst, dst, 1);
4895 }
4896 break;
4897 }
4898 } else {
4899 switch (cond) {
4900 case kCondEQ:
4901 __ Xor(TMP, lhs_high, rhs_high);
4902 __ Xor(AT, lhs_low, rhs_low);
4903 __ Or(dst, TMP, AT);
4904 __ Sltiu(dst, dst, 1);
4905 break;
4906 case kCondNE:
4907 __ Xor(TMP, lhs_high, rhs_high);
4908 __ Xor(AT, lhs_low, rhs_low);
4909 __ Or(dst, TMP, AT);
4910 __ Sltu(dst, ZERO, dst);
4911 break;
4912 case kCondLT:
4913 case kCondGE:
4914 __ Slt(TMP, rhs_high, lhs_high);
4915 __ Sltu(AT, lhs_low, rhs_low);
4916 __ Slt(TMP, TMP, AT);
4917 __ Slt(AT, lhs_high, rhs_high);
4918 __ Or(dst, AT, TMP);
4919 if (cond == kCondGE) {
4920 __ Xori(dst, dst, 1);
4921 }
4922 break;
4923 case kCondGT:
4924 case kCondLE:
4925 __ Slt(TMP, lhs_high, rhs_high);
4926 __ Sltu(AT, rhs_low, lhs_low);
4927 __ Slt(TMP, TMP, AT);
4928 __ Slt(AT, rhs_high, lhs_high);
4929 __ Or(dst, AT, TMP);
4930 if (cond == kCondLE) {
4931 __ Xori(dst, dst, 1);
4932 }
4933 break;
4934 case kCondB:
4935 case kCondAE:
4936 __ Sltu(TMP, rhs_high, lhs_high);
4937 __ Sltu(AT, lhs_low, rhs_low);
4938 __ Slt(TMP, TMP, AT);
4939 __ Sltu(AT, lhs_high, rhs_high);
4940 __ Or(dst, AT, TMP);
4941 if (cond == kCondAE) {
4942 __ Xori(dst, dst, 1);
4943 }
4944 break;
4945 case kCondA:
4946 case kCondBE:
4947 __ Sltu(TMP, lhs_high, rhs_high);
4948 __ Sltu(AT, rhs_low, lhs_low);
4949 __ Slt(TMP, TMP, AT);
4950 __ Sltu(AT, rhs_high, lhs_high);
4951 __ Or(dst, AT, TMP);
4952 if (cond == kCondBE) {
4953 __ Xori(dst, dst, 1);
4954 }
4955 break;
4956 }
4957 }
4958}
4959
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004960void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4961 LocationSummary* locations,
4962 MipsLabel* label) {
4963 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4964 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4965 Location rhs_location = locations->InAt(1);
4966 Register rhs_high = ZERO;
4967 Register rhs_low = ZERO;
4968 int64_t imm = 0;
4969 uint32_t imm_high = 0;
4970 uint32_t imm_low = 0;
4971 bool use_imm = rhs_location.IsConstant();
4972 if (use_imm) {
4973 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4974 imm_high = High32Bits(imm);
4975 imm_low = Low32Bits(imm);
4976 } else {
4977 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4978 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4979 }
4980
4981 if (use_imm && imm == 0) {
4982 switch (cond) {
4983 case kCondEQ:
4984 case kCondBE: // <= 0 if zero
4985 __ Or(TMP, lhs_high, lhs_low);
4986 __ Beqz(TMP, label);
4987 break;
4988 case kCondNE:
4989 case kCondA: // > 0 if non-zero
4990 __ Or(TMP, lhs_high, lhs_low);
4991 __ Bnez(TMP, label);
4992 break;
4993 case kCondLT:
4994 __ Bltz(lhs_high, label);
4995 break;
4996 case kCondGE:
4997 __ Bgez(lhs_high, label);
4998 break;
4999 case kCondLE:
5000 __ Or(TMP, lhs_high, lhs_low);
5001 __ Sra(AT, lhs_high, 31);
5002 __ Bgeu(AT, TMP, label);
5003 break;
5004 case kCondGT:
5005 __ Or(TMP, lhs_high, lhs_low);
5006 __ Sra(AT, lhs_high, 31);
5007 __ Bltu(AT, TMP, label);
5008 break;
5009 case kCondB: // always false
5010 break;
5011 case kCondAE: // always true
5012 __ B(label);
5013 break;
5014 }
5015 } else if (use_imm) {
5016 // TODO: more efficient comparison with constants without loading them into TMP/AT.
5017 switch (cond) {
5018 case kCondEQ:
5019 __ LoadConst32(TMP, imm_high);
5020 __ Xor(TMP, TMP, lhs_high);
5021 __ LoadConst32(AT, imm_low);
5022 __ Xor(AT, AT, lhs_low);
5023 __ Or(TMP, TMP, AT);
5024 __ Beqz(TMP, label);
5025 break;
5026 case kCondNE:
5027 __ LoadConst32(TMP, imm_high);
5028 __ Xor(TMP, TMP, lhs_high);
5029 __ LoadConst32(AT, imm_low);
5030 __ Xor(AT, AT, lhs_low);
5031 __ Or(TMP, TMP, AT);
5032 __ Bnez(TMP, label);
5033 break;
5034 case kCondLT:
5035 __ LoadConst32(TMP, imm_high);
5036 __ Blt(lhs_high, TMP, label);
5037 __ Slt(TMP, TMP, lhs_high);
5038 __ LoadConst32(AT, imm_low);
5039 __ Sltu(AT, lhs_low, AT);
5040 __ Blt(TMP, AT, label);
5041 break;
5042 case kCondGE:
5043 __ LoadConst32(TMP, imm_high);
5044 __ Blt(TMP, lhs_high, label);
5045 __ Slt(TMP, lhs_high, TMP);
5046 __ LoadConst32(AT, imm_low);
5047 __ Sltu(AT, lhs_low, AT);
5048 __ Or(TMP, TMP, AT);
5049 __ Beqz(TMP, label);
5050 break;
5051 case kCondLE:
5052 __ LoadConst32(TMP, imm_high);
5053 __ Blt(lhs_high, TMP, label);
5054 __ Slt(TMP, TMP, lhs_high);
5055 __ LoadConst32(AT, imm_low);
5056 __ Sltu(AT, AT, lhs_low);
5057 __ Or(TMP, TMP, AT);
5058 __ Beqz(TMP, label);
5059 break;
5060 case kCondGT:
5061 __ LoadConst32(TMP, imm_high);
5062 __ Blt(TMP, lhs_high, label);
5063 __ Slt(TMP, lhs_high, TMP);
5064 __ LoadConst32(AT, imm_low);
5065 __ Sltu(AT, AT, lhs_low);
5066 __ Blt(TMP, AT, label);
5067 break;
5068 case kCondB:
5069 __ LoadConst32(TMP, imm_high);
5070 __ Bltu(lhs_high, TMP, label);
5071 __ Sltu(TMP, TMP, lhs_high);
5072 __ LoadConst32(AT, imm_low);
5073 __ Sltu(AT, lhs_low, AT);
5074 __ Blt(TMP, AT, label);
5075 break;
5076 case kCondAE:
5077 __ LoadConst32(TMP, imm_high);
5078 __ Bltu(TMP, lhs_high, label);
5079 __ Sltu(TMP, lhs_high, TMP);
5080 __ LoadConst32(AT, imm_low);
5081 __ Sltu(AT, lhs_low, AT);
5082 __ Or(TMP, TMP, AT);
5083 __ Beqz(TMP, label);
5084 break;
5085 case kCondBE:
5086 __ LoadConst32(TMP, imm_high);
5087 __ Bltu(lhs_high, TMP, label);
5088 __ Sltu(TMP, TMP, lhs_high);
5089 __ LoadConst32(AT, imm_low);
5090 __ Sltu(AT, AT, lhs_low);
5091 __ Or(TMP, TMP, AT);
5092 __ Beqz(TMP, label);
5093 break;
5094 case kCondA:
5095 __ LoadConst32(TMP, imm_high);
5096 __ Bltu(TMP, lhs_high, label);
5097 __ Sltu(TMP, lhs_high, TMP);
5098 __ LoadConst32(AT, imm_low);
5099 __ Sltu(AT, AT, lhs_low);
5100 __ Blt(TMP, AT, label);
5101 break;
5102 }
5103 } else {
5104 switch (cond) {
5105 case kCondEQ:
5106 __ Xor(TMP, lhs_high, rhs_high);
5107 __ Xor(AT, lhs_low, rhs_low);
5108 __ Or(TMP, TMP, AT);
5109 __ Beqz(TMP, label);
5110 break;
5111 case kCondNE:
5112 __ Xor(TMP, lhs_high, rhs_high);
5113 __ Xor(AT, lhs_low, rhs_low);
5114 __ Or(TMP, TMP, AT);
5115 __ Bnez(TMP, label);
5116 break;
5117 case kCondLT:
5118 __ Blt(lhs_high, rhs_high, label);
5119 __ Slt(TMP, rhs_high, lhs_high);
5120 __ Sltu(AT, lhs_low, rhs_low);
5121 __ Blt(TMP, AT, label);
5122 break;
5123 case kCondGE:
5124 __ Blt(rhs_high, lhs_high, label);
5125 __ Slt(TMP, lhs_high, rhs_high);
5126 __ Sltu(AT, lhs_low, rhs_low);
5127 __ Or(TMP, TMP, AT);
5128 __ Beqz(TMP, label);
5129 break;
5130 case kCondLE:
5131 __ Blt(lhs_high, rhs_high, label);
5132 __ Slt(TMP, rhs_high, lhs_high);
5133 __ Sltu(AT, rhs_low, lhs_low);
5134 __ Or(TMP, TMP, AT);
5135 __ Beqz(TMP, label);
5136 break;
5137 case kCondGT:
5138 __ Blt(rhs_high, lhs_high, label);
5139 __ Slt(TMP, lhs_high, rhs_high);
5140 __ Sltu(AT, rhs_low, lhs_low);
5141 __ Blt(TMP, AT, label);
5142 break;
5143 case kCondB:
5144 __ Bltu(lhs_high, rhs_high, label);
5145 __ Sltu(TMP, rhs_high, lhs_high);
5146 __ Sltu(AT, lhs_low, rhs_low);
5147 __ Blt(TMP, AT, label);
5148 break;
5149 case kCondAE:
5150 __ Bltu(rhs_high, lhs_high, label);
5151 __ Sltu(TMP, lhs_high, rhs_high);
5152 __ Sltu(AT, lhs_low, rhs_low);
5153 __ Or(TMP, TMP, AT);
5154 __ Beqz(TMP, label);
5155 break;
5156 case kCondBE:
5157 __ Bltu(lhs_high, rhs_high, label);
5158 __ Sltu(TMP, rhs_high, lhs_high);
5159 __ Sltu(AT, rhs_low, lhs_low);
5160 __ Or(TMP, TMP, AT);
5161 __ Beqz(TMP, label);
5162 break;
5163 case kCondA:
5164 __ Bltu(rhs_high, lhs_high, label);
5165 __ Sltu(TMP, lhs_high, rhs_high);
5166 __ Sltu(AT, rhs_low, lhs_low);
5167 __ Blt(TMP, AT, label);
5168 break;
5169 }
5170 }
5171}
5172
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005173void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5174 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005175 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005176 LocationSummary* locations) {
5177 Register dst = locations->Out().AsRegister<Register>();
5178 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5179 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5180 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005181 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005182 if (isR6) {
5183 switch (cond) {
5184 case kCondEQ:
5185 __ CmpEqS(FTMP, lhs, rhs);
5186 __ Mfc1(dst, FTMP);
5187 __ Andi(dst, dst, 1);
5188 break;
5189 case kCondNE:
5190 __ CmpEqS(FTMP, lhs, rhs);
5191 __ Mfc1(dst, FTMP);
5192 __ Addiu(dst, dst, 1);
5193 break;
5194 case kCondLT:
5195 if (gt_bias) {
5196 __ CmpLtS(FTMP, lhs, rhs);
5197 } else {
5198 __ CmpUltS(FTMP, lhs, rhs);
5199 }
5200 __ Mfc1(dst, FTMP);
5201 __ Andi(dst, dst, 1);
5202 break;
5203 case kCondLE:
5204 if (gt_bias) {
5205 __ CmpLeS(FTMP, lhs, rhs);
5206 } else {
5207 __ CmpUleS(FTMP, lhs, rhs);
5208 }
5209 __ Mfc1(dst, FTMP);
5210 __ Andi(dst, dst, 1);
5211 break;
5212 case kCondGT:
5213 if (gt_bias) {
5214 __ CmpUltS(FTMP, rhs, lhs);
5215 } else {
5216 __ CmpLtS(FTMP, rhs, lhs);
5217 }
5218 __ Mfc1(dst, FTMP);
5219 __ Andi(dst, dst, 1);
5220 break;
5221 case kCondGE:
5222 if (gt_bias) {
5223 __ CmpUleS(FTMP, rhs, lhs);
5224 } else {
5225 __ CmpLeS(FTMP, rhs, lhs);
5226 }
5227 __ Mfc1(dst, FTMP);
5228 __ Andi(dst, dst, 1);
5229 break;
5230 default:
5231 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5232 UNREACHABLE();
5233 }
5234 } else {
5235 switch (cond) {
5236 case kCondEQ:
5237 __ CeqS(0, lhs, rhs);
5238 __ LoadConst32(dst, 1);
5239 __ Movf(dst, ZERO, 0);
5240 break;
5241 case kCondNE:
5242 __ CeqS(0, lhs, rhs);
5243 __ LoadConst32(dst, 1);
5244 __ Movt(dst, ZERO, 0);
5245 break;
5246 case kCondLT:
5247 if (gt_bias) {
5248 __ ColtS(0, lhs, rhs);
5249 } else {
5250 __ CultS(0, lhs, rhs);
5251 }
5252 __ LoadConst32(dst, 1);
5253 __ Movf(dst, ZERO, 0);
5254 break;
5255 case kCondLE:
5256 if (gt_bias) {
5257 __ ColeS(0, lhs, rhs);
5258 } else {
5259 __ CuleS(0, lhs, rhs);
5260 }
5261 __ LoadConst32(dst, 1);
5262 __ Movf(dst, ZERO, 0);
5263 break;
5264 case kCondGT:
5265 if (gt_bias) {
5266 __ CultS(0, rhs, lhs);
5267 } else {
5268 __ ColtS(0, rhs, lhs);
5269 }
5270 __ LoadConst32(dst, 1);
5271 __ Movf(dst, ZERO, 0);
5272 break;
5273 case kCondGE:
5274 if (gt_bias) {
5275 __ CuleS(0, rhs, lhs);
5276 } else {
5277 __ ColeS(0, rhs, lhs);
5278 }
5279 __ LoadConst32(dst, 1);
5280 __ Movf(dst, ZERO, 0);
5281 break;
5282 default:
5283 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5284 UNREACHABLE();
5285 }
5286 }
5287 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005288 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005289 if (isR6) {
5290 switch (cond) {
5291 case kCondEQ:
5292 __ CmpEqD(FTMP, lhs, rhs);
5293 __ Mfc1(dst, FTMP);
5294 __ Andi(dst, dst, 1);
5295 break;
5296 case kCondNE:
5297 __ CmpEqD(FTMP, lhs, rhs);
5298 __ Mfc1(dst, FTMP);
5299 __ Addiu(dst, dst, 1);
5300 break;
5301 case kCondLT:
5302 if (gt_bias) {
5303 __ CmpLtD(FTMP, lhs, rhs);
5304 } else {
5305 __ CmpUltD(FTMP, lhs, rhs);
5306 }
5307 __ Mfc1(dst, FTMP);
5308 __ Andi(dst, dst, 1);
5309 break;
5310 case kCondLE:
5311 if (gt_bias) {
5312 __ CmpLeD(FTMP, lhs, rhs);
5313 } else {
5314 __ CmpUleD(FTMP, lhs, rhs);
5315 }
5316 __ Mfc1(dst, FTMP);
5317 __ Andi(dst, dst, 1);
5318 break;
5319 case kCondGT:
5320 if (gt_bias) {
5321 __ CmpUltD(FTMP, rhs, lhs);
5322 } else {
5323 __ CmpLtD(FTMP, rhs, lhs);
5324 }
5325 __ Mfc1(dst, FTMP);
5326 __ Andi(dst, dst, 1);
5327 break;
5328 case kCondGE:
5329 if (gt_bias) {
5330 __ CmpUleD(FTMP, rhs, lhs);
5331 } else {
5332 __ CmpLeD(FTMP, rhs, lhs);
5333 }
5334 __ Mfc1(dst, FTMP);
5335 __ Andi(dst, dst, 1);
5336 break;
5337 default:
5338 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5339 UNREACHABLE();
5340 }
5341 } else {
5342 switch (cond) {
5343 case kCondEQ:
5344 __ CeqD(0, lhs, rhs);
5345 __ LoadConst32(dst, 1);
5346 __ Movf(dst, ZERO, 0);
5347 break;
5348 case kCondNE:
5349 __ CeqD(0, lhs, rhs);
5350 __ LoadConst32(dst, 1);
5351 __ Movt(dst, ZERO, 0);
5352 break;
5353 case kCondLT:
5354 if (gt_bias) {
5355 __ ColtD(0, lhs, rhs);
5356 } else {
5357 __ CultD(0, lhs, rhs);
5358 }
5359 __ LoadConst32(dst, 1);
5360 __ Movf(dst, ZERO, 0);
5361 break;
5362 case kCondLE:
5363 if (gt_bias) {
5364 __ ColeD(0, lhs, rhs);
5365 } else {
5366 __ CuleD(0, lhs, rhs);
5367 }
5368 __ LoadConst32(dst, 1);
5369 __ Movf(dst, ZERO, 0);
5370 break;
5371 case kCondGT:
5372 if (gt_bias) {
5373 __ CultD(0, rhs, lhs);
5374 } else {
5375 __ ColtD(0, rhs, lhs);
5376 }
5377 __ LoadConst32(dst, 1);
5378 __ Movf(dst, ZERO, 0);
5379 break;
5380 case kCondGE:
5381 if (gt_bias) {
5382 __ CuleD(0, rhs, lhs);
5383 } else {
5384 __ ColeD(0, rhs, lhs);
5385 }
5386 __ LoadConst32(dst, 1);
5387 __ Movf(dst, ZERO, 0);
5388 break;
5389 default:
5390 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5391 UNREACHABLE();
5392 }
5393 }
5394 }
5395}
5396
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005397bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5398 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005399 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005400 LocationSummary* input_locations,
5401 int cc) {
5402 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5403 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5404 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005405 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005406 switch (cond) {
5407 case kCondEQ:
5408 __ CeqS(cc, lhs, rhs);
5409 return false;
5410 case kCondNE:
5411 __ CeqS(cc, lhs, rhs);
5412 return true;
5413 case kCondLT:
5414 if (gt_bias) {
5415 __ ColtS(cc, lhs, rhs);
5416 } else {
5417 __ CultS(cc, lhs, rhs);
5418 }
5419 return false;
5420 case kCondLE:
5421 if (gt_bias) {
5422 __ ColeS(cc, lhs, rhs);
5423 } else {
5424 __ CuleS(cc, lhs, rhs);
5425 }
5426 return false;
5427 case kCondGT:
5428 if (gt_bias) {
5429 __ CultS(cc, rhs, lhs);
5430 } else {
5431 __ ColtS(cc, rhs, lhs);
5432 }
5433 return false;
5434 case kCondGE:
5435 if (gt_bias) {
5436 __ CuleS(cc, rhs, lhs);
5437 } else {
5438 __ ColeS(cc, rhs, lhs);
5439 }
5440 return false;
5441 default:
5442 LOG(FATAL) << "Unexpected non-floating-point condition";
5443 UNREACHABLE();
5444 }
5445 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005446 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005447 switch (cond) {
5448 case kCondEQ:
5449 __ CeqD(cc, lhs, rhs);
5450 return false;
5451 case kCondNE:
5452 __ CeqD(cc, lhs, rhs);
5453 return true;
5454 case kCondLT:
5455 if (gt_bias) {
5456 __ ColtD(cc, lhs, rhs);
5457 } else {
5458 __ CultD(cc, lhs, rhs);
5459 }
5460 return false;
5461 case kCondLE:
5462 if (gt_bias) {
5463 __ ColeD(cc, lhs, rhs);
5464 } else {
5465 __ CuleD(cc, lhs, rhs);
5466 }
5467 return false;
5468 case kCondGT:
5469 if (gt_bias) {
5470 __ CultD(cc, rhs, lhs);
5471 } else {
5472 __ ColtD(cc, rhs, lhs);
5473 }
5474 return false;
5475 case kCondGE:
5476 if (gt_bias) {
5477 __ CuleD(cc, rhs, lhs);
5478 } else {
5479 __ ColeD(cc, rhs, lhs);
5480 }
5481 return false;
5482 default:
5483 LOG(FATAL) << "Unexpected non-floating-point condition";
5484 UNREACHABLE();
5485 }
5486 }
5487}
5488
5489bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5490 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005491 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005492 LocationSummary* input_locations,
5493 FRegister dst) {
5494 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5495 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5496 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005497 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005498 switch (cond) {
5499 case kCondEQ:
5500 __ CmpEqS(dst, lhs, rhs);
5501 return false;
5502 case kCondNE:
5503 __ CmpEqS(dst, lhs, rhs);
5504 return true;
5505 case kCondLT:
5506 if (gt_bias) {
5507 __ CmpLtS(dst, lhs, rhs);
5508 } else {
5509 __ CmpUltS(dst, lhs, rhs);
5510 }
5511 return false;
5512 case kCondLE:
5513 if (gt_bias) {
5514 __ CmpLeS(dst, lhs, rhs);
5515 } else {
5516 __ CmpUleS(dst, lhs, rhs);
5517 }
5518 return false;
5519 case kCondGT:
5520 if (gt_bias) {
5521 __ CmpUltS(dst, rhs, lhs);
5522 } else {
5523 __ CmpLtS(dst, rhs, lhs);
5524 }
5525 return false;
5526 case kCondGE:
5527 if (gt_bias) {
5528 __ CmpUleS(dst, rhs, lhs);
5529 } else {
5530 __ CmpLeS(dst, rhs, lhs);
5531 }
5532 return false;
5533 default:
5534 LOG(FATAL) << "Unexpected non-floating-point condition";
5535 UNREACHABLE();
5536 }
5537 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005538 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005539 switch (cond) {
5540 case kCondEQ:
5541 __ CmpEqD(dst, lhs, rhs);
5542 return false;
5543 case kCondNE:
5544 __ CmpEqD(dst, lhs, rhs);
5545 return true;
5546 case kCondLT:
5547 if (gt_bias) {
5548 __ CmpLtD(dst, lhs, rhs);
5549 } else {
5550 __ CmpUltD(dst, lhs, rhs);
5551 }
5552 return false;
5553 case kCondLE:
5554 if (gt_bias) {
5555 __ CmpLeD(dst, lhs, rhs);
5556 } else {
5557 __ CmpUleD(dst, lhs, rhs);
5558 }
5559 return false;
5560 case kCondGT:
5561 if (gt_bias) {
5562 __ CmpUltD(dst, rhs, lhs);
5563 } else {
5564 __ CmpLtD(dst, rhs, lhs);
5565 }
5566 return false;
5567 case kCondGE:
5568 if (gt_bias) {
5569 __ CmpUleD(dst, rhs, lhs);
5570 } else {
5571 __ CmpLeD(dst, rhs, lhs);
5572 }
5573 return false;
5574 default:
5575 LOG(FATAL) << "Unexpected non-floating-point condition";
5576 UNREACHABLE();
5577 }
5578 }
5579}
5580
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005581void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5582 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005583 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005584 LocationSummary* locations,
5585 MipsLabel* label) {
5586 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5587 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5588 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005589 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005590 if (isR6) {
5591 switch (cond) {
5592 case kCondEQ:
5593 __ CmpEqS(FTMP, lhs, rhs);
5594 __ Bc1nez(FTMP, label);
5595 break;
5596 case kCondNE:
5597 __ CmpEqS(FTMP, lhs, rhs);
5598 __ Bc1eqz(FTMP, label);
5599 break;
5600 case kCondLT:
5601 if (gt_bias) {
5602 __ CmpLtS(FTMP, lhs, rhs);
5603 } else {
5604 __ CmpUltS(FTMP, lhs, rhs);
5605 }
5606 __ Bc1nez(FTMP, label);
5607 break;
5608 case kCondLE:
5609 if (gt_bias) {
5610 __ CmpLeS(FTMP, lhs, rhs);
5611 } else {
5612 __ CmpUleS(FTMP, lhs, rhs);
5613 }
5614 __ Bc1nez(FTMP, label);
5615 break;
5616 case kCondGT:
5617 if (gt_bias) {
5618 __ CmpUltS(FTMP, rhs, lhs);
5619 } else {
5620 __ CmpLtS(FTMP, rhs, lhs);
5621 }
5622 __ Bc1nez(FTMP, label);
5623 break;
5624 case kCondGE:
5625 if (gt_bias) {
5626 __ CmpUleS(FTMP, rhs, lhs);
5627 } else {
5628 __ CmpLeS(FTMP, rhs, lhs);
5629 }
5630 __ Bc1nez(FTMP, label);
5631 break;
5632 default:
5633 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005634 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005635 }
5636 } else {
5637 switch (cond) {
5638 case kCondEQ:
5639 __ CeqS(0, lhs, rhs);
5640 __ Bc1t(0, label);
5641 break;
5642 case kCondNE:
5643 __ CeqS(0, lhs, rhs);
5644 __ Bc1f(0, label);
5645 break;
5646 case kCondLT:
5647 if (gt_bias) {
5648 __ ColtS(0, lhs, rhs);
5649 } else {
5650 __ CultS(0, lhs, rhs);
5651 }
5652 __ Bc1t(0, label);
5653 break;
5654 case kCondLE:
5655 if (gt_bias) {
5656 __ ColeS(0, lhs, rhs);
5657 } else {
5658 __ CuleS(0, lhs, rhs);
5659 }
5660 __ Bc1t(0, label);
5661 break;
5662 case kCondGT:
5663 if (gt_bias) {
5664 __ CultS(0, rhs, lhs);
5665 } else {
5666 __ ColtS(0, rhs, lhs);
5667 }
5668 __ Bc1t(0, label);
5669 break;
5670 case kCondGE:
5671 if (gt_bias) {
5672 __ CuleS(0, rhs, lhs);
5673 } else {
5674 __ ColeS(0, rhs, lhs);
5675 }
5676 __ Bc1t(0, label);
5677 break;
5678 default:
5679 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005680 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005681 }
5682 }
5683 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005684 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005685 if (isR6) {
5686 switch (cond) {
5687 case kCondEQ:
5688 __ CmpEqD(FTMP, lhs, rhs);
5689 __ Bc1nez(FTMP, label);
5690 break;
5691 case kCondNE:
5692 __ CmpEqD(FTMP, lhs, rhs);
5693 __ Bc1eqz(FTMP, label);
5694 break;
5695 case kCondLT:
5696 if (gt_bias) {
5697 __ CmpLtD(FTMP, lhs, rhs);
5698 } else {
5699 __ CmpUltD(FTMP, lhs, rhs);
5700 }
5701 __ Bc1nez(FTMP, label);
5702 break;
5703 case kCondLE:
5704 if (gt_bias) {
5705 __ CmpLeD(FTMP, lhs, rhs);
5706 } else {
5707 __ CmpUleD(FTMP, lhs, rhs);
5708 }
5709 __ Bc1nez(FTMP, label);
5710 break;
5711 case kCondGT:
5712 if (gt_bias) {
5713 __ CmpUltD(FTMP, rhs, lhs);
5714 } else {
5715 __ CmpLtD(FTMP, rhs, lhs);
5716 }
5717 __ Bc1nez(FTMP, label);
5718 break;
5719 case kCondGE:
5720 if (gt_bias) {
5721 __ CmpUleD(FTMP, rhs, lhs);
5722 } else {
5723 __ CmpLeD(FTMP, rhs, lhs);
5724 }
5725 __ Bc1nez(FTMP, label);
5726 break;
5727 default:
5728 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005729 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005730 }
5731 } else {
5732 switch (cond) {
5733 case kCondEQ:
5734 __ CeqD(0, lhs, rhs);
5735 __ Bc1t(0, label);
5736 break;
5737 case kCondNE:
5738 __ CeqD(0, lhs, rhs);
5739 __ Bc1f(0, label);
5740 break;
5741 case kCondLT:
5742 if (gt_bias) {
5743 __ ColtD(0, lhs, rhs);
5744 } else {
5745 __ CultD(0, lhs, rhs);
5746 }
5747 __ Bc1t(0, label);
5748 break;
5749 case kCondLE:
5750 if (gt_bias) {
5751 __ ColeD(0, lhs, rhs);
5752 } else {
5753 __ CuleD(0, lhs, rhs);
5754 }
5755 __ Bc1t(0, label);
5756 break;
5757 case kCondGT:
5758 if (gt_bias) {
5759 __ CultD(0, rhs, lhs);
5760 } else {
5761 __ ColtD(0, rhs, lhs);
5762 }
5763 __ Bc1t(0, label);
5764 break;
5765 case kCondGE:
5766 if (gt_bias) {
5767 __ CuleD(0, rhs, lhs);
5768 } else {
5769 __ ColeD(0, rhs, lhs);
5770 }
5771 __ Bc1t(0, label);
5772 break;
5773 default:
5774 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005775 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005776 }
5777 }
5778 }
5779}
5780
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005781void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005782 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005783 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005784 MipsLabel* false_target) {
5785 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005786
David Brazdil0debae72015-11-12 18:37:00 +00005787 if (true_target == nullptr && false_target == nullptr) {
5788 // Nothing to do. The code always falls through.
5789 return;
5790 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005791 // Constant condition, statically compared against "true" (integer value 1).
5792 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005793 if (true_target != nullptr) {
5794 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005795 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005796 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005797 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005798 if (false_target != nullptr) {
5799 __ B(false_target);
5800 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005801 }
David Brazdil0debae72015-11-12 18:37:00 +00005802 return;
5803 }
5804
5805 // The following code generates these patterns:
5806 // (1) true_target == nullptr && false_target != nullptr
5807 // - opposite condition true => branch to false_target
5808 // (2) true_target != nullptr && false_target == nullptr
5809 // - condition true => branch to true_target
5810 // (3) true_target != nullptr && false_target != nullptr
5811 // - condition true => branch to true_target
5812 // - branch to false_target
5813 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005814 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005815 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005816 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005817 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005818 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5819 } else {
5820 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5821 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005822 } else {
5823 // The condition instruction has not been materialized, use its inputs as
5824 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005825 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005826 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005827 LocationSummary* locations = cond->GetLocations();
5828 IfCondition if_cond = condition->GetCondition();
5829 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005830
David Brazdil0debae72015-11-12 18:37:00 +00005831 if (true_target == nullptr) {
5832 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005833 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005834 }
5835
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005836 switch (type) {
5837 default:
5838 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5839 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005840 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005841 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5842 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005843 case DataType::Type::kFloat32:
5844 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005845 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5846 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005847 }
5848 }
David Brazdil0debae72015-11-12 18:37:00 +00005849
5850 // If neither branch falls through (case 3), the conditional branch to `true_target`
5851 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5852 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005853 __ B(false_target);
5854 }
5855}
5856
5857void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005858 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005859 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005860 locations->SetInAt(0, Location::RequiresRegister());
5861 }
5862}
5863
5864void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005865 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5866 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5867 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5868 nullptr : codegen_->GetLabelOf(true_successor);
5869 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5870 nullptr : codegen_->GetLabelOf(false_successor);
5871 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005872}
5873
5874void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005875 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005876 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005877 InvokeRuntimeCallingConvention calling_convention;
5878 RegisterSet caller_saves = RegisterSet::Empty();
5879 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5880 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005881 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005882 locations->SetInAt(0, Location::RequiresRegister());
5883 }
5884}
5885
5886void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005887 SlowPathCodeMIPS* slow_path =
5888 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005889 GenerateTestAndBranch(deoptimize,
5890 /* condition_input_index */ 0,
5891 slow_path->GetEntryLabel(),
5892 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005893}
5894
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005895// This function returns true if a conditional move can be generated for HSelect.
5896// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5897// branches and regular moves.
5898//
5899// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5900//
5901// While determining feasibility of a conditional move and setting inputs/outputs
5902// are two distinct tasks, this function does both because they share quite a bit
5903// of common logic.
5904static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5905 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5906 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5907 HCondition* condition = cond->AsCondition();
5908
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005909 DataType::Type cond_type =
5910 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5911 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005912
5913 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5914 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5915 bool is_true_value_zero_constant =
5916 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5917 bool is_false_value_zero_constant =
5918 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5919
5920 bool can_move_conditionally = false;
5921 bool use_const_for_false_in = false;
5922 bool use_const_for_true_in = false;
5923
5924 if (!cond->IsConstant()) {
5925 switch (cond_type) {
5926 default:
5927 switch (dst_type) {
5928 default:
5929 // Moving int on int condition.
5930 if (is_r6) {
5931 if (is_true_value_zero_constant) {
5932 // seleqz out_reg, false_reg, cond_reg
5933 can_move_conditionally = true;
5934 use_const_for_true_in = true;
5935 } else if (is_false_value_zero_constant) {
5936 // selnez out_reg, true_reg, cond_reg
5937 can_move_conditionally = true;
5938 use_const_for_false_in = true;
5939 } else if (materialized) {
5940 // Not materializing unmaterialized int conditions
5941 // to keep the instruction count low.
5942 // selnez AT, true_reg, cond_reg
5943 // seleqz TMP, false_reg, cond_reg
5944 // or out_reg, AT, TMP
5945 can_move_conditionally = true;
5946 }
5947 } else {
5948 // movn out_reg, true_reg/ZERO, cond_reg
5949 can_move_conditionally = true;
5950 use_const_for_true_in = is_true_value_zero_constant;
5951 }
5952 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005954 // Moving long on int condition.
5955 if (is_r6) {
5956 if (is_true_value_zero_constant) {
5957 // seleqz out_reg_lo, false_reg_lo, cond_reg
5958 // seleqz out_reg_hi, false_reg_hi, cond_reg
5959 can_move_conditionally = true;
5960 use_const_for_true_in = true;
5961 } else if (is_false_value_zero_constant) {
5962 // selnez out_reg_lo, true_reg_lo, cond_reg
5963 // selnez out_reg_hi, true_reg_hi, cond_reg
5964 can_move_conditionally = true;
5965 use_const_for_false_in = true;
5966 }
5967 // Other long conditional moves would generate 6+ instructions,
5968 // which is too many.
5969 } else {
5970 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5971 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5972 can_move_conditionally = true;
5973 use_const_for_true_in = is_true_value_zero_constant;
5974 }
5975 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 case DataType::Type::kFloat32:
5977 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005978 // Moving float/double on int condition.
5979 if (is_r6) {
5980 if (materialized) {
5981 // Not materializing unmaterialized int conditions
5982 // to keep the instruction count low.
5983 can_move_conditionally = true;
5984 if (is_true_value_zero_constant) {
5985 // sltu TMP, ZERO, cond_reg
5986 // mtc1 TMP, temp_cond_reg
5987 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5988 use_const_for_true_in = true;
5989 } else if (is_false_value_zero_constant) {
5990 // sltu TMP, ZERO, cond_reg
5991 // mtc1 TMP, temp_cond_reg
5992 // selnez.fmt out_reg, true_reg, temp_cond_reg
5993 use_const_for_false_in = true;
5994 } else {
5995 // sltu TMP, ZERO, cond_reg
5996 // mtc1 TMP, temp_cond_reg
5997 // sel.fmt temp_cond_reg, false_reg, true_reg
5998 // mov.fmt out_reg, temp_cond_reg
5999 }
6000 }
6001 } else {
6002 // movn.fmt out_reg, true_reg, cond_reg
6003 can_move_conditionally = true;
6004 }
6005 break;
6006 }
6007 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006008 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006009 // We don't materialize long comparison now
6010 // and use conditional branches instead.
6011 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006012 case DataType::Type::kFloat32:
6013 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006014 switch (dst_type) {
6015 default:
6016 // Moving int on float/double condition.
6017 if (is_r6) {
6018 if (is_true_value_zero_constant) {
6019 // mfc1 TMP, temp_cond_reg
6020 // seleqz out_reg, false_reg, TMP
6021 can_move_conditionally = true;
6022 use_const_for_true_in = true;
6023 } else if (is_false_value_zero_constant) {
6024 // mfc1 TMP, temp_cond_reg
6025 // selnez out_reg, true_reg, TMP
6026 can_move_conditionally = true;
6027 use_const_for_false_in = true;
6028 } else {
6029 // mfc1 TMP, temp_cond_reg
6030 // selnez AT, true_reg, TMP
6031 // seleqz TMP, false_reg, TMP
6032 // or out_reg, AT, TMP
6033 can_move_conditionally = true;
6034 }
6035 } else {
6036 // movt out_reg, true_reg/ZERO, cc
6037 can_move_conditionally = true;
6038 use_const_for_true_in = is_true_value_zero_constant;
6039 }
6040 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006041 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006042 // Moving long on float/double condition.
6043 if (is_r6) {
6044 if (is_true_value_zero_constant) {
6045 // mfc1 TMP, temp_cond_reg
6046 // seleqz out_reg_lo, false_reg_lo, TMP
6047 // seleqz out_reg_hi, false_reg_hi, TMP
6048 can_move_conditionally = true;
6049 use_const_for_true_in = true;
6050 } else if (is_false_value_zero_constant) {
6051 // mfc1 TMP, temp_cond_reg
6052 // selnez out_reg_lo, true_reg_lo, TMP
6053 // selnez out_reg_hi, true_reg_hi, TMP
6054 can_move_conditionally = true;
6055 use_const_for_false_in = true;
6056 }
6057 // Other long conditional moves would generate 6+ instructions,
6058 // which is too many.
6059 } else {
6060 // movt out_reg_lo, true_reg_lo/ZERO, cc
6061 // movt out_reg_hi, true_reg_hi/ZERO, cc
6062 can_move_conditionally = true;
6063 use_const_for_true_in = is_true_value_zero_constant;
6064 }
6065 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006066 case DataType::Type::kFloat32:
6067 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006068 // Moving float/double on float/double condition.
6069 if (is_r6) {
6070 can_move_conditionally = true;
6071 if (is_true_value_zero_constant) {
6072 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6073 use_const_for_true_in = true;
6074 } else if (is_false_value_zero_constant) {
6075 // selnez.fmt out_reg, true_reg, temp_cond_reg
6076 use_const_for_false_in = true;
6077 } else {
6078 // sel.fmt temp_cond_reg, false_reg, true_reg
6079 // mov.fmt out_reg, temp_cond_reg
6080 }
6081 } else {
6082 // movt.fmt out_reg, true_reg, cc
6083 can_move_conditionally = true;
6084 }
6085 break;
6086 }
6087 break;
6088 }
6089 }
6090
6091 if (can_move_conditionally) {
6092 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6093 } else {
6094 DCHECK(!use_const_for_false_in);
6095 DCHECK(!use_const_for_true_in);
6096 }
6097
6098 if (locations_to_set != nullptr) {
6099 if (use_const_for_false_in) {
6100 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6101 } else {
6102 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006103 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006104 ? Location::RequiresFpuRegister()
6105 : Location::RequiresRegister());
6106 }
6107 if (use_const_for_true_in) {
6108 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6109 } else {
6110 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006111 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006112 ? Location::RequiresFpuRegister()
6113 : Location::RequiresRegister());
6114 }
6115 if (materialized) {
6116 locations_to_set->SetInAt(2, Location::RequiresRegister());
6117 }
6118 // On R6 we don't require the output to be the same as the
6119 // first input for conditional moves unlike on R2.
6120 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6121 if (is_out_same_as_first_in) {
6122 locations_to_set->SetOut(Location::SameAsFirstInput());
6123 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006124 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006125 ? Location::RequiresFpuRegister()
6126 : Location::RequiresRegister());
6127 }
6128 }
6129
6130 return can_move_conditionally;
6131}
6132
6133void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6134 LocationSummary* locations = select->GetLocations();
6135 Location dst = locations->Out();
6136 Location src = locations->InAt(1);
6137 Register src_reg = ZERO;
6138 Register src_reg_high = ZERO;
6139 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6140 Register cond_reg = TMP;
6141 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006142 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006143 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006144 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006145
6146 if (IsBooleanValueOrMaterializedCondition(cond)) {
6147 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6148 } else {
6149 HCondition* condition = cond->AsCondition();
6150 LocationSummary* cond_locations = cond->GetLocations();
6151 IfCondition if_cond = condition->GetCondition();
6152 cond_type = condition->InputAt(0)->GetType();
6153 switch (cond_type) {
6154 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006155 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006156 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6157 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006158 case DataType::Type::kFloat32:
6159 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006160 cond_inverted = MaterializeFpCompareR2(if_cond,
6161 condition->IsGtBias(),
6162 cond_type,
6163 cond_locations,
6164 cond_cc);
6165 break;
6166 }
6167 }
6168
6169 DCHECK(dst.Equals(locations->InAt(0)));
6170 if (src.IsRegister()) {
6171 src_reg = src.AsRegister<Register>();
6172 } else if (src.IsRegisterPair()) {
6173 src_reg = src.AsRegisterPairLow<Register>();
6174 src_reg_high = src.AsRegisterPairHigh<Register>();
6175 } else if (src.IsConstant()) {
6176 DCHECK(src.GetConstant()->IsZeroBitPattern());
6177 }
6178
6179 switch (cond_type) {
6180 default:
6181 switch (dst_type) {
6182 default:
6183 if (cond_inverted) {
6184 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6185 } else {
6186 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6187 }
6188 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006189 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006190 if (cond_inverted) {
6191 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6192 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6193 } else {
6194 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6195 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6196 }
6197 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006198 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006199 if (cond_inverted) {
6200 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6201 } else {
6202 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6203 }
6204 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006205 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006206 if (cond_inverted) {
6207 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6208 } else {
6209 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6210 }
6211 break;
6212 }
6213 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006214 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006215 LOG(FATAL) << "Unreachable";
6216 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006217 case DataType::Type::kFloat32:
6218 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006219 switch (dst_type) {
6220 default:
6221 if (cond_inverted) {
6222 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6223 } else {
6224 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6225 }
6226 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006227 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006228 if (cond_inverted) {
6229 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6230 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6231 } else {
6232 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6233 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6234 }
6235 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006236 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006237 if (cond_inverted) {
6238 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6239 } else {
6240 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6241 }
6242 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006243 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006244 if (cond_inverted) {
6245 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6246 } else {
6247 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6248 }
6249 break;
6250 }
6251 break;
6252 }
6253}
6254
6255void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6256 LocationSummary* locations = select->GetLocations();
6257 Location dst = locations->Out();
6258 Location false_src = locations->InAt(0);
6259 Location true_src = locations->InAt(1);
6260 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6261 Register cond_reg = TMP;
6262 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006263 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006264 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006265 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006266
6267 if (IsBooleanValueOrMaterializedCondition(cond)) {
6268 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6269 } else {
6270 HCondition* condition = cond->AsCondition();
6271 LocationSummary* cond_locations = cond->GetLocations();
6272 IfCondition if_cond = condition->GetCondition();
6273 cond_type = condition->InputAt(0)->GetType();
6274 switch (cond_type) {
6275 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006276 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006277 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6278 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006279 case DataType::Type::kFloat32:
6280 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006281 cond_inverted = MaterializeFpCompareR6(if_cond,
6282 condition->IsGtBias(),
6283 cond_type,
6284 cond_locations,
6285 fcond_reg);
6286 break;
6287 }
6288 }
6289
6290 if (true_src.IsConstant()) {
6291 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6292 }
6293 if (false_src.IsConstant()) {
6294 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6295 }
6296
6297 switch (dst_type) {
6298 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006299 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006300 __ Mfc1(cond_reg, fcond_reg);
6301 }
6302 if (true_src.IsConstant()) {
6303 if (cond_inverted) {
6304 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6305 } else {
6306 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6307 }
6308 } else if (false_src.IsConstant()) {
6309 if (cond_inverted) {
6310 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6311 } else {
6312 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6313 }
6314 } else {
6315 DCHECK_NE(cond_reg, AT);
6316 if (cond_inverted) {
6317 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6318 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6319 } else {
6320 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6321 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6322 }
6323 __ Or(dst.AsRegister<Register>(), AT, TMP);
6324 }
6325 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006326 case DataType::Type::kInt64: {
6327 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006328 __ Mfc1(cond_reg, fcond_reg);
6329 }
6330 Register dst_lo = dst.AsRegisterPairLow<Register>();
6331 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6332 if (true_src.IsConstant()) {
6333 Register src_lo = false_src.AsRegisterPairLow<Register>();
6334 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6335 if (cond_inverted) {
6336 __ Selnez(dst_lo, src_lo, cond_reg);
6337 __ Selnez(dst_hi, src_hi, cond_reg);
6338 } else {
6339 __ Seleqz(dst_lo, src_lo, cond_reg);
6340 __ Seleqz(dst_hi, src_hi, cond_reg);
6341 }
6342 } else {
6343 DCHECK(false_src.IsConstant());
6344 Register src_lo = true_src.AsRegisterPairLow<Register>();
6345 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6346 if (cond_inverted) {
6347 __ Seleqz(dst_lo, src_lo, cond_reg);
6348 __ Seleqz(dst_hi, src_hi, cond_reg);
6349 } else {
6350 __ Selnez(dst_lo, src_lo, cond_reg);
6351 __ Selnez(dst_hi, src_hi, cond_reg);
6352 }
6353 }
6354 break;
6355 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006356 case DataType::Type::kFloat32: {
6357 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006358 // sel*.fmt tests bit 0 of the condition register, account for that.
6359 __ Sltu(TMP, ZERO, cond_reg);
6360 __ Mtc1(TMP, fcond_reg);
6361 }
6362 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6363 if (true_src.IsConstant()) {
6364 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6365 if (cond_inverted) {
6366 __ SelnezS(dst_reg, src_reg, fcond_reg);
6367 } else {
6368 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6369 }
6370 } else if (false_src.IsConstant()) {
6371 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6372 if (cond_inverted) {
6373 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6374 } else {
6375 __ SelnezS(dst_reg, src_reg, fcond_reg);
6376 }
6377 } else {
6378 if (cond_inverted) {
6379 __ SelS(fcond_reg,
6380 true_src.AsFpuRegister<FRegister>(),
6381 false_src.AsFpuRegister<FRegister>());
6382 } else {
6383 __ SelS(fcond_reg,
6384 false_src.AsFpuRegister<FRegister>(),
6385 true_src.AsFpuRegister<FRegister>());
6386 }
6387 __ MovS(dst_reg, fcond_reg);
6388 }
6389 break;
6390 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006391 case DataType::Type::kFloat64: {
6392 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006393 // sel*.fmt tests bit 0 of the condition register, account for that.
6394 __ Sltu(TMP, ZERO, cond_reg);
6395 __ Mtc1(TMP, fcond_reg);
6396 }
6397 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6398 if (true_src.IsConstant()) {
6399 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6400 if (cond_inverted) {
6401 __ SelnezD(dst_reg, src_reg, fcond_reg);
6402 } else {
6403 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6404 }
6405 } else if (false_src.IsConstant()) {
6406 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6407 if (cond_inverted) {
6408 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6409 } else {
6410 __ SelnezD(dst_reg, src_reg, fcond_reg);
6411 }
6412 } else {
6413 if (cond_inverted) {
6414 __ SelD(fcond_reg,
6415 true_src.AsFpuRegister<FRegister>(),
6416 false_src.AsFpuRegister<FRegister>());
6417 } else {
6418 __ SelD(fcond_reg,
6419 false_src.AsFpuRegister<FRegister>(),
6420 true_src.AsFpuRegister<FRegister>());
6421 }
6422 __ MovD(dst_reg, fcond_reg);
6423 }
6424 break;
6425 }
6426 }
6427}
6428
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006429void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006430 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006431 LocationSummary(flag, LocationSummary::kNoCall);
6432 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006433}
6434
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006435void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6436 __ LoadFromOffset(kLoadWord,
6437 flag->GetLocations()->Out().AsRegister<Register>(),
6438 SP,
6439 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006440}
6441
David Brazdil74eb1b22015-12-14 11:44:01 +00006442void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006443 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006444 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006445}
6446
6447void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006448 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6449 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6450 if (is_r6) {
6451 GenConditionalMoveR6(select);
6452 } else {
6453 GenConditionalMoveR2(select);
6454 }
6455 } else {
6456 LocationSummary* locations = select->GetLocations();
6457 MipsLabel false_target;
6458 GenerateTestAndBranch(select,
6459 /* condition_input_index */ 2,
6460 /* true_target */ nullptr,
6461 &false_target);
6462 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6463 __ Bind(&false_target);
6464 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006465}
6466
David Srbecky0cf44932015-12-09 14:09:59 +00006467void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006468 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006469}
6470
David Srbeckyd28f4a02016-03-14 17:14:24 +00006471void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6472 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006473}
6474
6475void CodeGeneratorMIPS::GenerateNop() {
6476 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006477}
6478
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006479void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006480 DataType::Type field_type = field_info.GetFieldType();
6481 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006482 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006483 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006484 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006485 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006486 instruction,
6487 generate_volatile
6488 ? LocationSummary::kCallOnMainOnly
6489 : (object_field_get_with_read_barrier
6490 ? LocationSummary::kCallOnSlowPath
6491 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006492
Alexey Frunzec61c0762017-04-10 13:54:23 -07006493 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6494 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6495 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006496 locations->SetInAt(0, Location::RequiresRegister());
6497 if (generate_volatile) {
6498 InvokeRuntimeCallingConvention calling_convention;
6499 // need A0 to hold base + offset
6500 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006501 if (field_type == DataType::Type::kInt64) {
6502 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006503 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006504 // Use Location::Any() to prevent situations when running out of available fp registers.
6505 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006506 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006507 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006508 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6509 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6510 }
6511 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006512 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006513 locations->SetOut(Location::RequiresFpuRegister());
6514 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006515 // The output overlaps in the case of an object field get with
6516 // read barriers enabled: we do not want the move to overwrite the
6517 // object's location, as we need it to emit the read barrier.
6518 locations->SetOut(Location::RequiresRegister(),
6519 object_field_get_with_read_barrier
6520 ? Location::kOutputOverlap
6521 : Location::kNoOutputOverlap);
6522 }
6523 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6524 // We need a temporary register for the read barrier marking slow
6525 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006526 if (!kBakerReadBarrierThunksEnableForFields) {
6527 locations->AddTemp(Location::RequiresRegister());
6528 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006529 }
6530 }
6531}
6532
6533void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6534 const FieldInfo& field_info,
6535 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006536 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6537 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006538 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006539 Location obj_loc = locations->InAt(0);
6540 Register obj = obj_loc.AsRegister<Register>();
6541 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006542 LoadOperandType load_type = kLoadUnsignedByte;
6543 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006544 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006545 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006546
6547 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006548 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006549 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006550 load_type = kLoadUnsignedByte;
6551 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006552 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006553 load_type = kLoadSignedByte;
6554 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006555 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006556 load_type = kLoadUnsignedHalfword;
6557 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006558 case DataType::Type::kInt16:
6559 load_type = kLoadSignedHalfword;
6560 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006561 case DataType::Type::kInt32:
6562 case DataType::Type::kFloat32:
6563 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006564 load_type = kLoadWord;
6565 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006566 case DataType::Type::kInt64:
6567 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006568 load_type = kLoadDoubleword;
6569 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006570 case DataType::Type::kUint32:
6571 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006572 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006573 LOG(FATAL) << "Unreachable type " << type;
6574 UNREACHABLE();
6575 }
6576
6577 if (is_volatile && load_type == kLoadDoubleword) {
6578 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006579 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006580 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006581 __ LoadFromOffset(kLoadWord,
6582 ZERO,
6583 locations->GetTemp(0).AsRegister<Register>(),
6584 0,
6585 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006586 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006587 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006588 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006589 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006590 if (dst_loc.IsFpuRegister()) {
6591 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006592 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006593 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006594 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006595 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006596 __ StoreToOffset(kStoreWord,
6597 locations->GetTemp(1).AsRegister<Register>(),
6598 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006599 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006600 __ StoreToOffset(kStoreWord,
6601 locations->GetTemp(2).AsRegister<Register>(),
6602 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006603 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006604 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006605 }
6606 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006607 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006608 // /* HeapReference<Object> */ dst = *(obj + offset)
6609 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006610 Location temp_loc =
6611 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006612 // Note that a potential implicit null check is handled in this
6613 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6614 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6615 dst_loc,
6616 obj,
6617 offset,
6618 temp_loc,
6619 /* needs_null_check */ true);
6620 if (is_volatile) {
6621 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6622 }
6623 } else {
6624 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6625 if (is_volatile) {
6626 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6627 }
6628 // If read barriers are enabled, emit read barriers other than
6629 // Baker's using a slow path (and also unpoison the loaded
6630 // reference, if heap poisoning is enabled).
6631 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6632 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006633 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006634 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006635 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006636 DCHECK(dst_loc.IsRegisterPair());
6637 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006638 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006639 DCHECK(dst_loc.IsRegister());
6640 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006641 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006642 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006643 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006644 DCHECK(dst_loc.IsFpuRegister());
6645 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006646 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006647 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006648 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006649 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006650 }
6651 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006652 }
6653
Alexey Frunze15958152017-02-09 19:08:30 -08006654 // Memory barriers, in the case of references, are handled in the
6655 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006656 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006657 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6658 }
6659}
6660
6661void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006662 DataType::Type field_type = field_info.GetFieldType();
6663 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006664 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006665 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006666 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006667
6668 locations->SetInAt(0, Location::RequiresRegister());
6669 if (generate_volatile) {
6670 InvokeRuntimeCallingConvention calling_convention;
6671 // need A0 to hold base + offset
6672 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006673 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006674 locations->SetInAt(1, Location::RegisterPairLocation(
6675 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6676 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006677 // Use Location::Any() to prevent situations when running out of available fp registers.
6678 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006679 // Pass FP parameters in core registers.
6680 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6681 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6682 }
6683 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006684 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006685 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006686 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006687 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006688 }
6689 }
6690}
6691
6692void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6693 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006694 uint32_t dex_pc,
6695 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006696 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006697 LocationSummary* locations = instruction->GetLocations();
6698 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006699 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006700 StoreOperandType store_type = kStoreByte;
6701 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006702 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006703 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006704 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006705
6706 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006707 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006708 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006709 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006710 store_type = kStoreByte;
6711 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006712 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006713 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006714 store_type = kStoreHalfword;
6715 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006716 case DataType::Type::kInt32:
6717 case DataType::Type::kFloat32:
6718 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006719 store_type = kStoreWord;
6720 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006721 case DataType::Type::kInt64:
6722 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006723 store_type = kStoreDoubleword;
6724 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006725 case DataType::Type::kUint32:
6726 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006727 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006728 LOG(FATAL) << "Unreachable type " << type;
6729 UNREACHABLE();
6730 }
6731
6732 if (is_volatile) {
6733 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6734 }
6735
6736 if (is_volatile && store_type == kStoreDoubleword) {
6737 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006738 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006739 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006740 __ LoadFromOffset(kLoadWord,
6741 ZERO,
6742 locations->GetTemp(0).AsRegister<Register>(),
6743 0,
6744 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006745 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006746 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006747 if (value_location.IsFpuRegister()) {
6748 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6749 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006750 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006751 value_location.AsFpuRegister<FRegister>());
6752 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006753 __ LoadFromOffset(kLoadWord,
6754 locations->GetTemp(1).AsRegister<Register>(),
6755 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006756 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006757 __ LoadFromOffset(kLoadWord,
6758 locations->GetTemp(2).AsRegister<Register>(),
6759 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006760 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006761 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006762 DCHECK(value_location.IsConstant());
6763 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6764 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006765 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6766 locations->GetTemp(1).AsRegister<Register>(),
6767 value);
6768 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006769 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006770 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006771 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6772 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006773 if (value_location.IsConstant()) {
6774 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6775 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006776 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006777 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006778 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006779 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006780 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006781 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006782 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006783 if (kPoisonHeapReferences && needs_write_barrier) {
6784 // Note that in the case where `value` is a null reference,
6785 // we do not enter this block, as a null reference does not
6786 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006787 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006788 __ PoisonHeapReference(TMP, src);
6789 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6790 } else {
6791 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6792 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006793 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006794 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006795 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006796 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006797 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006798 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006799 }
6800 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006801 }
6802
Alexey Frunzec061de12017-02-14 13:27:23 -08006803 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006804 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006805 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006806 }
6807
6808 if (is_volatile) {
6809 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6810 }
6811}
6812
6813void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6814 HandleFieldGet(instruction, instruction->GetFieldInfo());
6815}
6816
6817void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6818 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6819}
6820
6821void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6822 HandleFieldSet(instruction, instruction->GetFieldInfo());
6823}
6824
6825void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006826 HandleFieldSet(instruction,
6827 instruction->GetFieldInfo(),
6828 instruction->GetDexPc(),
6829 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006830}
6831
Alexey Frunze15958152017-02-09 19:08:30 -08006832void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6833 HInstruction* instruction,
6834 Location out,
6835 uint32_t offset,
6836 Location maybe_temp,
6837 ReadBarrierOption read_barrier_option) {
6838 Register out_reg = out.AsRegister<Register>();
6839 if (read_barrier_option == kWithReadBarrier) {
6840 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006841 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6842 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6843 }
Alexey Frunze15958152017-02-09 19:08:30 -08006844 if (kUseBakerReadBarrier) {
6845 // Load with fast path based Baker's read barrier.
6846 // /* HeapReference<Object> */ out = *(out + offset)
6847 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6848 out,
6849 out_reg,
6850 offset,
6851 maybe_temp,
6852 /* needs_null_check */ false);
6853 } else {
6854 // Load with slow path based read barrier.
6855 // Save the value of `out` into `maybe_temp` before overwriting it
6856 // in the following move operation, as we will need it for the
6857 // read barrier below.
6858 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6859 // /* HeapReference<Object> */ out = *(out + offset)
6860 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6861 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6862 }
6863 } else {
6864 // Plain load with no read barrier.
6865 // /* HeapReference<Object> */ out = *(out + offset)
6866 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6867 __ MaybeUnpoisonHeapReference(out_reg);
6868 }
6869}
6870
6871void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6872 HInstruction* instruction,
6873 Location out,
6874 Location obj,
6875 uint32_t offset,
6876 Location maybe_temp,
6877 ReadBarrierOption read_barrier_option) {
6878 Register out_reg = out.AsRegister<Register>();
6879 Register obj_reg = obj.AsRegister<Register>();
6880 if (read_barrier_option == kWithReadBarrier) {
6881 CHECK(kEmitCompilerReadBarrier);
6882 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006883 if (!kBakerReadBarrierThunksEnableForFields) {
6884 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6885 }
Alexey Frunze15958152017-02-09 19:08:30 -08006886 // Load with fast path based Baker's read barrier.
6887 // /* HeapReference<Object> */ out = *(obj + offset)
6888 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6889 out,
6890 obj_reg,
6891 offset,
6892 maybe_temp,
6893 /* needs_null_check */ false);
6894 } else {
6895 // Load with slow path based read barrier.
6896 // /* HeapReference<Object> */ out = *(obj + offset)
6897 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6898 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6899 }
6900 } else {
6901 // Plain load with no read barrier.
6902 // /* HeapReference<Object> */ out = *(obj + offset)
6903 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6904 __ MaybeUnpoisonHeapReference(out_reg);
6905 }
6906}
6907
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006908static inline int GetBakerMarkThunkNumber(Register reg) {
6909 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6910 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6911 return reg - V0;
6912 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6913 return 14 + (reg - S2);
6914 } else if (reg == FP) { // One more.
6915 return 20;
6916 }
6917 LOG(FATAL) << "Unexpected register " << reg;
6918 UNREACHABLE();
6919}
6920
6921static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6922 int num = GetBakerMarkThunkNumber(reg) +
6923 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6924 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6925}
6926
6927static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6928 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6929 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6930}
6931
Alexey Frunze15958152017-02-09 19:08:30 -08006932void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6933 Location root,
6934 Register obj,
6935 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006936 ReadBarrierOption read_barrier_option,
6937 MipsLabel* label_low) {
6938 bool reordering;
6939 if (label_low != nullptr) {
6940 DCHECK_EQ(offset, 0x5678u);
6941 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006942 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006943 if (read_barrier_option == kWithReadBarrier) {
6944 DCHECK(kEmitCompilerReadBarrier);
6945 if (kUseBakerReadBarrier) {
6946 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6947 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006948 if (kBakerReadBarrierThunksEnableForGcRoots) {
6949 // Note that we do not actually check the value of `GetIsGcMarking()`
6950 // to decide whether to mark the loaded GC root or not. Instead, we
6951 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6952 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6953 // vice versa.
6954 //
6955 // We use thunks for the slow path. That thunk checks the reference
6956 // and jumps to the entrypoint if needed.
6957 //
6958 // temp = Thread::Current()->pReadBarrierMarkReg00
6959 // // AKA &art_quick_read_barrier_mark_introspection.
6960 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6961 // if (temp != nullptr) {
6962 // temp = &gc_root_thunk<root_reg>
6963 // root = temp(root)
6964 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006965
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006966 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6967 const int32_t entry_point_offset =
6968 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6969 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6970 int16_t offset_low = Low16Bits(offset);
6971 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6972 // extension in lw.
6973 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6974 Register base = short_offset ? obj : TMP;
6975 // Loading the entrypoint does not require a load acquire since it is only changed when
6976 // threads are suspended or running a checkpoint.
6977 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6978 reordering = __ SetReorder(false);
6979 if (!short_offset) {
6980 DCHECK(!label_low);
6981 __ AddUpper(base, obj, offset_high);
6982 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006983 MipsLabel skip_call;
6984 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006985 if (label_low != nullptr) {
6986 DCHECK(short_offset);
6987 __ Bind(label_low);
6988 }
6989 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6990 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6991 // in delay slot.
6992 if (isR6) {
6993 __ Jialc(T9, thunk_disp);
6994 } else {
6995 __ Addiu(T9, T9, thunk_disp);
6996 __ Jalr(T9);
6997 __ Nop();
6998 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006999 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007000 __ SetReorder(reordering);
7001 } else {
7002 // Note that we do not actually check the value of `GetIsGcMarking()`
7003 // to decide whether to mark the loaded GC root or not. Instead, we
7004 // load into `temp` (T9) the read barrier mark entry point corresponding
7005 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
7006 // is false, and vice versa.
7007 //
7008 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
7009 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
7010 // if (temp != null) {
7011 // root = temp(root)
7012 // }
Alexey Frunze15958152017-02-09 19:08:30 -08007013
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007014 if (label_low != nullptr) {
7015 reordering = __ SetReorder(false);
7016 __ Bind(label_low);
7017 }
7018 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7019 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7020 if (label_low != nullptr) {
7021 __ SetReorder(reordering);
7022 }
7023 static_assert(
7024 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
7025 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
7026 "have different sizes.");
7027 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
7028 "art::mirror::CompressedReference<mirror::Object> and int32_t "
7029 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08007030
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007031 // Slow path marking the GC root `root`.
7032 Location temp = Location::RegisterLocation(T9);
7033 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007034 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007035 instruction,
7036 root,
7037 /*entrypoint*/ temp);
7038 codegen_->AddSlowPath(slow_path);
7039
7040 const int32_t entry_point_offset =
7041 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
7042 // Loading the entrypoint does not require a load acquire since it is only changed when
7043 // threads are suspended or running a checkpoint.
7044 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
7045 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
7046 __ Bind(slow_path->GetExitLabel());
7047 }
Alexey Frunze15958152017-02-09 19:08:30 -08007048 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007049 if (label_low != nullptr) {
7050 reordering = __ SetReorder(false);
7051 __ Bind(label_low);
7052 }
Alexey Frunze15958152017-02-09 19:08:30 -08007053 // GC root loaded through a slow path for read barriers other
7054 // than Baker's.
7055 // /* GcRoot<mirror::Object>* */ root = obj + offset
7056 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007057 if (label_low != nullptr) {
7058 __ SetReorder(reordering);
7059 }
Alexey Frunze15958152017-02-09 19:08:30 -08007060 // /* mirror::Object* */ root = root->Read()
7061 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7062 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007063 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007064 if (label_low != nullptr) {
7065 reordering = __ SetReorder(false);
7066 __ Bind(label_low);
7067 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007068 // Plain GC root load with no read barrier.
7069 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7070 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7071 // Note that GC roots are not affected by heap poisoning, thus we
7072 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007073 if (label_low != nullptr) {
7074 __ SetReorder(reordering);
7075 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007076 }
7077}
7078
Alexey Frunze15958152017-02-09 19:08:30 -08007079void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7080 Location ref,
7081 Register obj,
7082 uint32_t offset,
7083 Location temp,
7084 bool needs_null_check) {
7085 DCHECK(kEmitCompilerReadBarrier);
7086 DCHECK(kUseBakerReadBarrier);
7087
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007088 if (kBakerReadBarrierThunksEnableForFields) {
7089 // Note that we do not actually check the value of `GetIsGcMarking()`
7090 // to decide whether to mark the loaded reference or not. Instead, we
7091 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7092 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7093 // vice versa.
7094 //
7095 // We use thunks for the slow path. That thunk checks the reference
7096 // and jumps to the entrypoint if needed. If the holder is not gray,
7097 // it issues a load-load memory barrier and returns to the original
7098 // reference load.
7099 //
7100 // temp = Thread::Current()->pReadBarrierMarkReg00
7101 // // AKA &art_quick_read_barrier_mark_introspection.
7102 // if (temp != nullptr) {
7103 // temp = &field_array_thunk<holder_reg>
7104 // temp()
7105 // }
7106 // not_gray_return_address:
7107 // // If the offset is too large to fit into the lw instruction, we
7108 // // use an adjusted base register (TMP) here. This register
7109 // // receives bits 16 ... 31 of the offset before the thunk invocation
7110 // // and the thunk benefits from it.
7111 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7112 // gray_return_address:
7113
7114 DCHECK(temp.IsInvalid());
7115 bool isR6 = GetInstructionSetFeatures().IsR6();
7116 int16_t offset_low = Low16Bits(offset);
7117 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7118 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7119 bool reordering = __ SetReorder(false);
7120 const int32_t entry_point_offset =
7121 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7122 // There may have or may have not been a null check if the field offset is smaller than
7123 // the page size.
7124 // There must've been a null check in case it's actually a load from an array.
7125 // We will, however, perform an explicit null check in the thunk as it's easier to
7126 // do it than not.
7127 if (instruction->IsArrayGet()) {
7128 DCHECK(!needs_null_check);
7129 }
7130 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7131 // Loading the entrypoint does not require a load acquire since it is only changed when
7132 // threads are suspended or running a checkpoint.
7133 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7134 Register ref_reg = ref.AsRegister<Register>();
7135 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007136 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007137 if (short_offset) {
7138 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007139 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007140 __ Nop(); // In forbidden slot.
7141 __ Jialc(T9, thunk_disp);
7142 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007143 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007144 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7145 __ Jalr(T9);
7146 __ Nop(); // In delay slot.
7147 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007148 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007149 } else {
7150 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007151 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007152 __ Aui(base, obj, offset_high); // In delay slot.
7153 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007154 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007155 } else {
7156 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007157 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007158 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7159 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007160 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007161 __ Addu(base, base, obj); // In delay slot.
7162 }
7163 }
7164 // /* HeapReference<Object> */ ref = *(obj + offset)
7165 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7166 if (needs_null_check) {
7167 MaybeRecordImplicitNullCheck(instruction);
7168 }
7169 __ MaybeUnpoisonHeapReference(ref_reg);
7170 __ SetReorder(reordering);
7171 return;
7172 }
7173
Alexey Frunze15958152017-02-09 19:08:30 -08007174 // /* HeapReference<Object> */ ref = *(obj + offset)
7175 Location no_index = Location::NoLocation();
7176 ScaleFactor no_scale_factor = TIMES_1;
7177 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7178 ref,
7179 obj,
7180 offset,
7181 no_index,
7182 no_scale_factor,
7183 temp,
7184 needs_null_check);
7185}
7186
7187void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7188 Location ref,
7189 Register obj,
7190 uint32_t data_offset,
7191 Location index,
7192 Location temp,
7193 bool needs_null_check) {
7194 DCHECK(kEmitCompilerReadBarrier);
7195 DCHECK(kUseBakerReadBarrier);
7196
7197 static_assert(
7198 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7199 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007200 ScaleFactor scale_factor = TIMES_4;
7201
7202 if (kBakerReadBarrierThunksEnableForArrays) {
7203 // Note that we do not actually check the value of `GetIsGcMarking()`
7204 // to decide whether to mark the loaded reference or not. Instead, we
7205 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7206 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7207 // vice versa.
7208 //
7209 // We use thunks for the slow path. That thunk checks the reference
7210 // and jumps to the entrypoint if needed. If the holder is not gray,
7211 // it issues a load-load memory barrier and returns to the original
7212 // reference load.
7213 //
7214 // temp = Thread::Current()->pReadBarrierMarkReg00
7215 // // AKA &art_quick_read_barrier_mark_introspection.
7216 // if (temp != nullptr) {
7217 // temp = &field_array_thunk<holder_reg>
7218 // temp()
7219 // }
7220 // not_gray_return_address:
7221 // // The element address is pre-calculated in the TMP register before the
7222 // // thunk invocation and the thunk benefits from it.
7223 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7224 // gray_return_address:
7225
7226 DCHECK(temp.IsInvalid());
7227 DCHECK(index.IsValid());
7228 bool reordering = __ SetReorder(false);
7229 const int32_t entry_point_offset =
7230 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7231 // We will not do the explicit null check in the thunk as some form of a null check
7232 // must've been done earlier.
7233 DCHECK(!needs_null_check);
7234 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7235 // Loading the entrypoint does not require a load acquire since it is only changed when
7236 // threads are suspended or running a checkpoint.
7237 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7238 Register ref_reg = ref.AsRegister<Register>();
7239 Register index_reg = index.IsRegisterPair()
7240 ? index.AsRegisterPairLow<Register>()
7241 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007242 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007243 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007244 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007245 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7246 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007247 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007248 } else {
7249 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007250 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007251 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7252 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007253 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007254 __ Addu(TMP, TMP, obj); // In delay slot.
7255 }
7256 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7257 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7258 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7259 __ MaybeUnpoisonHeapReference(ref_reg);
7260 __ SetReorder(reordering);
7261 return;
7262 }
7263
Alexey Frunze15958152017-02-09 19:08:30 -08007264 // /* HeapReference<Object> */ ref =
7265 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007266 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7267 ref,
7268 obj,
7269 data_offset,
7270 index,
7271 scale_factor,
7272 temp,
7273 needs_null_check);
7274}
7275
7276void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7277 Location ref,
7278 Register obj,
7279 uint32_t offset,
7280 Location index,
7281 ScaleFactor scale_factor,
7282 Location temp,
7283 bool needs_null_check,
7284 bool always_update_field) {
7285 DCHECK(kEmitCompilerReadBarrier);
7286 DCHECK(kUseBakerReadBarrier);
7287
7288 // In slow path based read barriers, the read barrier call is
7289 // inserted after the original load. However, in fast path based
7290 // Baker's read barriers, we need to perform the load of
7291 // mirror::Object::monitor_ *before* the original reference load.
7292 // This load-load ordering is required by the read barrier.
7293 // The fast path/slow path (for Baker's algorithm) should look like:
7294 //
7295 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7296 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7297 // HeapReference<Object> ref = *src; // Original reference load.
7298 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7299 // if (is_gray) {
7300 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7301 // }
7302 //
7303 // Note: the original implementation in ReadBarrier::Barrier is
7304 // slightly more complex as it performs additional checks that we do
7305 // not do here for performance reasons.
7306
7307 Register ref_reg = ref.AsRegister<Register>();
7308 Register temp_reg = temp.AsRegister<Register>();
7309 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7310
7311 // /* int32_t */ monitor = obj->monitor_
7312 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7313 if (needs_null_check) {
7314 MaybeRecordImplicitNullCheck(instruction);
7315 }
7316 // /* LockWord */ lock_word = LockWord(monitor)
7317 static_assert(sizeof(LockWord) == sizeof(int32_t),
7318 "art::LockWord and int32_t have different sizes.");
7319
7320 __ Sync(0); // Barrier to prevent load-load reordering.
7321
7322 // The actual reference load.
7323 if (index.IsValid()) {
7324 // Load types involving an "index": ArrayGet,
7325 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7326 // intrinsics.
7327 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7328 if (index.IsConstant()) {
7329 size_t computed_offset =
7330 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7331 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7332 } else {
7333 // Handle the special case of the
7334 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7335 // intrinsics, which use a register pair as index ("long
7336 // offset"), of which only the low part contains data.
7337 Register index_reg = index.IsRegisterPair()
7338 ? index.AsRegisterPairLow<Register>()
7339 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007340 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007341 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7342 }
7343 } else {
7344 // /* HeapReference<Object> */ ref = *(obj + offset)
7345 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7346 }
7347
7348 // Object* ref = ref_addr->AsMirrorPtr()
7349 __ MaybeUnpoisonHeapReference(ref_reg);
7350
7351 // Slow path marking the object `ref` when it is gray.
7352 SlowPathCodeMIPS* slow_path;
7353 if (always_update_field) {
7354 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7355 // of the form `obj + field_offset`, where `obj` is a register and
7356 // `field_offset` is a register pair (of which only the lower half
7357 // is used). Thus `offset` and `scale_factor` above are expected
7358 // to be null in this code path.
7359 DCHECK_EQ(offset, 0u);
7360 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007361 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007362 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7363 ref,
7364 obj,
7365 /* field_offset */ index,
7366 temp_reg);
7367 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007368 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007369 }
7370 AddSlowPath(slow_path);
7371
7372 // if (rb_state == ReadBarrier::GrayState())
7373 // ref = ReadBarrier::Mark(ref);
7374 // Given the numeric representation, it's enough to check the low bit of the
7375 // rb_state. We do that by shifting the bit into the sign bit (31) and
7376 // performing a branch on less than zero.
7377 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7378 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7379 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7380 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7381 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7382 __ Bind(slow_path->GetExitLabel());
7383}
7384
7385void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7386 Location out,
7387 Location ref,
7388 Location obj,
7389 uint32_t offset,
7390 Location index) {
7391 DCHECK(kEmitCompilerReadBarrier);
7392
7393 // Insert a slow path based read barrier *after* the reference load.
7394 //
7395 // If heap poisoning is enabled, the unpoisoning of the loaded
7396 // reference will be carried out by the runtime within the slow
7397 // path.
7398 //
7399 // Note that `ref` currently does not get unpoisoned (when heap
7400 // poisoning is enabled), which is alright as the `ref` argument is
7401 // not used by the artReadBarrierSlow entry point.
7402 //
7403 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007404 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007405 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7406 AddSlowPath(slow_path);
7407
7408 __ B(slow_path->GetEntryLabel());
7409 __ Bind(slow_path->GetExitLabel());
7410}
7411
7412void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7413 Location out,
7414 Location ref,
7415 Location obj,
7416 uint32_t offset,
7417 Location index) {
7418 if (kEmitCompilerReadBarrier) {
7419 // Baker's read barriers shall be handled by the fast path
7420 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7421 DCHECK(!kUseBakerReadBarrier);
7422 // If heap poisoning is enabled, unpoisoning will be taken care of
7423 // by the runtime within the slow path.
7424 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7425 } else if (kPoisonHeapReferences) {
7426 __ UnpoisonHeapReference(out.AsRegister<Register>());
7427 }
7428}
7429
7430void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7431 Location out,
7432 Location root) {
7433 DCHECK(kEmitCompilerReadBarrier);
7434
7435 // Insert a slow path based read barrier *after* the GC root load.
7436 //
7437 // Note that GC roots are not affected by heap poisoning, so we do
7438 // not need to do anything special for this here.
7439 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007440 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007441 AddSlowPath(slow_path);
7442
7443 __ B(slow_path->GetEntryLabel());
7444 __ Bind(slow_path->GetExitLabel());
7445}
7446
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007447void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007448 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7449 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007450 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007451 switch (type_check_kind) {
7452 case TypeCheckKind::kExactCheck:
7453 case TypeCheckKind::kAbstractClassCheck:
7454 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007455 case TypeCheckKind::kArrayObjectCheck: {
7456 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7457 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7458 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007459 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007460 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007461 case TypeCheckKind::kArrayCheck:
7462 case TypeCheckKind::kUnresolvedCheck:
7463 case TypeCheckKind::kInterfaceCheck:
7464 call_kind = LocationSummary::kCallOnSlowPath;
7465 break;
Vladimir Marko3f413232018-02-12 18:39:15 +00007466 case TypeCheckKind::kBitstringCheck:
7467 break;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007468 }
7469
Vladimir Markoca6fff82017-10-03 14:49:14 +01007470 LocationSummary* locations =
7471 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007472 if (baker_read_barrier_slow_path) {
7473 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7474 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007475 locations->SetInAt(0, Location::RequiresRegister());
Vladimir Marko3f413232018-02-12 18:39:15 +00007476 if (type_check_kind == TypeCheckKind::kBitstringCheck) {
7477 locations->SetInAt(1, Location::ConstantLocation(instruction->InputAt(1)->AsConstant()));
7478 locations->SetInAt(2, Location::ConstantLocation(instruction->InputAt(2)->AsConstant()));
7479 locations->SetInAt(3, Location::ConstantLocation(instruction->InputAt(3)->AsConstant()));
7480 } else {
7481 locations->SetInAt(1, Location::RequiresRegister());
7482 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007483 // The output does overlap inputs.
7484 // Note that TypeCheckSlowPathMIPS uses this register too.
7485 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007486 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007487}
7488
7489void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007490 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007491 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007492 Location obj_loc = locations->InAt(0);
7493 Register obj = obj_loc.AsRegister<Register>();
Vladimir Marko3f413232018-02-12 18:39:15 +00007494 Location cls = locations->InAt(1);
Alexey Frunze15958152017-02-09 19:08:30 -08007495 Location out_loc = locations->Out();
7496 Register out = out_loc.AsRegister<Register>();
7497 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7498 DCHECK_LE(num_temps, 1u);
7499 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007500 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7501 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7502 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7503 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007504 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007505 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007506
7507 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007508 // Avoid this check if we know `obj` is not null.
7509 if (instruction->MustDoNullCheck()) {
7510 __ Move(out, ZERO);
7511 __ Beqz(obj, &done);
7512 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007513
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007514 switch (type_check_kind) {
7515 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007516 ReadBarrierOption read_barrier_option =
7517 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007518 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007519 GenerateReferenceLoadTwoRegisters(instruction,
7520 out_loc,
7521 obj_loc,
7522 class_offset,
7523 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007524 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007525 // Classes must be equal for the instanceof to succeed.
Vladimir Marko3f413232018-02-12 18:39:15 +00007526 __ Xor(out, out, cls.AsRegister<Register>());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007527 __ Sltiu(out, out, 1);
7528 break;
7529 }
7530
7531 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007532 ReadBarrierOption read_barrier_option =
7533 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007534 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007535 GenerateReferenceLoadTwoRegisters(instruction,
7536 out_loc,
7537 obj_loc,
7538 class_offset,
7539 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007540 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007541 // If the class is abstract, we eagerly fetch the super class of the
7542 // object to avoid doing a comparison we know will fail.
7543 MipsLabel loop;
7544 __ Bind(&loop);
7545 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007546 GenerateReferenceLoadOneRegister(instruction,
7547 out_loc,
7548 super_offset,
7549 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007550 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007551 // If `out` is null, we use it for the result, and jump to `done`.
7552 __ Beqz(out, &done);
Vladimir Marko3f413232018-02-12 18:39:15 +00007553 __ Bne(out, cls.AsRegister<Register>(), &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007554 __ LoadConst32(out, 1);
7555 break;
7556 }
7557
7558 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007559 ReadBarrierOption read_barrier_option =
7560 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007561 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007562 GenerateReferenceLoadTwoRegisters(instruction,
7563 out_loc,
7564 obj_loc,
7565 class_offset,
7566 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007567 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007568 // Walk over the class hierarchy to find a match.
7569 MipsLabel loop, success;
7570 __ Bind(&loop);
Vladimir Marko3f413232018-02-12 18:39:15 +00007571 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007572 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007573 GenerateReferenceLoadOneRegister(instruction,
7574 out_loc,
7575 super_offset,
7576 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007577 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007578 __ Bnez(out, &loop);
7579 // If `out` is null, we use it for the result, and jump to `done`.
7580 __ B(&done);
7581 __ Bind(&success);
7582 __ LoadConst32(out, 1);
7583 break;
7584 }
7585
7586 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007587 ReadBarrierOption read_barrier_option =
7588 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007589 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007590 GenerateReferenceLoadTwoRegisters(instruction,
7591 out_loc,
7592 obj_loc,
7593 class_offset,
7594 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007595 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007596 // Do an exact check.
7597 MipsLabel success;
Vladimir Marko3f413232018-02-12 18:39:15 +00007598 __ Beq(out, cls.AsRegister<Register>(), &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007599 // Otherwise, we need to check that the object's class is a non-primitive array.
7600 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007601 GenerateReferenceLoadOneRegister(instruction,
7602 out_loc,
7603 component_offset,
7604 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007605 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007606 // If `out` is null, we use it for the result, and jump to `done`.
7607 __ Beqz(out, &done);
7608 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7609 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7610 __ Sltiu(out, out, 1);
7611 __ B(&done);
7612 __ Bind(&success);
7613 __ LoadConst32(out, 1);
7614 break;
7615 }
7616
7617 case TypeCheckKind::kArrayCheck: {
7618 // No read barrier since the slow path will retry upon failure.
7619 // /* 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,
7625 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007626 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007627 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7628 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007629 codegen_->AddSlowPath(slow_path);
Vladimir Marko3f413232018-02-12 18:39:15 +00007630 __ Bne(out, cls.AsRegister<Register>(), slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007631 __ LoadConst32(out, 1);
7632 break;
7633 }
7634
7635 case TypeCheckKind::kUnresolvedCheck:
7636 case TypeCheckKind::kInterfaceCheck: {
7637 // Note that we indeed only call on slow path, but we always go
7638 // into the slow path for the unresolved and interface check
7639 // cases.
7640 //
7641 // We cannot directly call the InstanceofNonTrivial runtime
7642 // entry point without resorting to a type checking slow path
7643 // here (i.e. by calling InvokeRuntime directly), as it would
7644 // require to assign fixed registers for the inputs of this
7645 // HInstanceOf instruction (following the runtime calling
7646 // convention), which might be cluttered by the potential first
7647 // read barrier emission at the beginning of this method.
7648 //
7649 // TODO: Introduce a new runtime entry point taking the object
7650 // to test (instead of its class) as argument, and let it deal
7651 // with the read barrier issues. This will let us refactor this
7652 // case of the `switch` code as it was previously (with a direct
7653 // call to the runtime not using a type checking slow path).
7654 // This should also be beneficial for the other cases above.
7655 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007656 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7657 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007658 codegen_->AddSlowPath(slow_path);
7659 __ B(slow_path->GetEntryLabel());
7660 break;
7661 }
Vladimir Marko3f413232018-02-12 18:39:15 +00007662
7663 case TypeCheckKind::kBitstringCheck: {
7664 // /* HeapReference<Class> */ temp = obj->klass_
7665 GenerateReferenceLoadTwoRegisters(instruction,
7666 out_loc,
7667 obj_loc,
7668 class_offset,
7669 maybe_temp_loc,
7670 kWithoutReadBarrier);
7671
7672 GenerateBitstringTypeCheckCompare(instruction, out);
7673 __ Sltiu(out, out, 1);
7674 break;
7675 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007676 }
7677
7678 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007679
7680 if (slow_path != nullptr) {
7681 __ Bind(slow_path->GetExitLabel());
7682 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007683}
7684
7685void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007686 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007687 locations->SetOut(Location::ConstantLocation(constant));
7688}
7689
7690void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7691 // Will be generated at use site.
7692}
7693
7694void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007695 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007696 locations->SetOut(Location::ConstantLocation(constant));
7697}
7698
7699void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7700 // Will be generated at use site.
7701}
7702
7703void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7704 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7705 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7706}
7707
7708void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7709 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007710 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007711 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007712 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007713}
7714
7715void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7716 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7717 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007718 Location receiver = invoke->GetLocations()->InAt(0);
7719 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007720 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007721
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007722 // temp = object->GetClass();
7723 if (receiver.IsStackSlot()) {
7724 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7725 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7726 } else {
7727 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7728 }
7729 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007730 // Instead of simply (possibly) unpoisoning `temp` here, we should
7731 // emit a read barrier for the previous class reference load.
7732 // However this is not required in practice, as this is an
7733 // intermediate/temporary reference and because the current
7734 // concurrent copying collector keeps the from-space memory
7735 // intact/accessible until the end of the marking phase (the
7736 // concurrent copying collector may not in the future).
7737 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007738 __ LoadFromOffset(kLoadWord, temp, temp,
7739 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7740 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007741 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007742 // temp = temp->GetImtEntryAt(method_offset);
7743 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7744 // T9 = temp->GetEntryPoint();
7745 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
Lena Djokic3177e102018-02-28 11:32:40 +01007746 // Set the hidden argument.
7747 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7748 invoke->GetDexMethodIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007749 // T9();
7750 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007751 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007752 DCHECK(!codegen_->IsLeafMethod());
7753 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7754}
7755
7756void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007757 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7758 if (intrinsic.TryDispatch(invoke)) {
7759 return;
7760 }
7761
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007762 HandleInvoke(invoke);
7763}
7764
7765void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007766 // Explicit clinit checks triggered by static invokes must have been pruned by
7767 // art::PrepareForRegisterAllocation.
7768 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007769
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007770 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007771 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7772 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007773
Chris Larsen701566a2015-10-27 15:29:13 -07007774 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7775 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007776 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7777 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7778 }
Chris Larsen701566a2015-10-27 15:29:13 -07007779 return;
7780 }
7781
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007782 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007783
7784 // Add the extra input register if either the dex cache array base register
7785 // or the PC-relative base register for accessing literals is needed.
7786 if (has_extra_input) {
7787 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7788 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007789}
7790
Orion Hodsonac141392017-01-13 11:53:47 +00007791void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7792 HandleInvoke(invoke);
7793}
7794
7795void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7796 codegen_->GenerateInvokePolymorphicCall(invoke);
7797}
7798
Chris Larsen701566a2015-10-27 15:29:13 -07007799static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007800 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007801 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7802 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007803 return true;
7804 }
7805 return false;
7806}
7807
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007808HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007809 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007810 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007811 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007812 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007813 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007814 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007815 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007816 case HLoadString::LoadKind::kJitTableAddress:
7817 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007818 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007819 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007820 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007821 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007822 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007823 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007824}
7825
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007826HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7827 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007828 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007829 case HLoadClass::LoadKind::kInvalid:
7830 LOG(FATAL) << "UNREACHABLE";
7831 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007832 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007833 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007834 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007835 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007836 case HLoadClass::LoadKind::kBssEntry:
7837 DCHECK(!Runtime::Current()->UseJitCompilation());
7838 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007839 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007840 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007841 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007842 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007843 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007844 break;
7845 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007846 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007847}
7848
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007849Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7850 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007851 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007852 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007853 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7854 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7855 if (!invoke->GetLocations()->Intrinsified()) {
7856 return location.AsRegister<Register>();
7857 }
7858 // For intrinsics we allow any location, so it may be on the stack.
7859 if (!location.IsRegister()) {
7860 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7861 return temp;
7862 }
7863 // For register locations, check if the register was saved. If so, get it from the stack.
7864 // Note: There is a chance that the register was saved but not overwritten, so we could
7865 // save one load. However, since this is just an intrinsic slow path we prefer this
7866 // simple and more robust approach rather that trying to determine if that's the case.
7867 SlowPathCode* slow_path = GetCurrentSlowPath();
7868 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7869 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7870 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7871 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7872 return temp;
7873 }
7874 return location.AsRegister<Register>();
7875}
7876
Vladimir Markodc151b22015-10-15 18:02:30 +01007877HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7878 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007879 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007880 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007881}
7882
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007883void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7884 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007885 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007886 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007887 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7888 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007889 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007890 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7891 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007892 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7893 : ZERO;
7894
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007895 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007896 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007897 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007898 uint32_t offset =
7899 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007900 __ LoadFromOffset(kLoadWord,
7901 temp.AsRegister<Register>(),
7902 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007903 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007904 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007905 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007906 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007907 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007908 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007909 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7910 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007911 PcRelativePatchInfo* info_high = NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007912 PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007913 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007914 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007915 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7916 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007917 break;
7918 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007919 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7920 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7921 break;
Vladimir Markob066d432018-01-03 13:14:37 +00007922 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007923 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00007924 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
7925 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
7926 Register temp_reg = temp.AsRegister<Register>();
7927 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7928 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
7929 break;
7930 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007931 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007932 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007933 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007934 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7935 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007936 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007937 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7938 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007939 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007940 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007941 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7942 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7943 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007944 }
7945 }
7946
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007947 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007948 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007949 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007950 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007951 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7952 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007953 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007954 T9,
7955 callee_method.AsRegister<Register>(),
7956 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007957 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007958 // T9()
7959 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007960 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007961 break;
7962 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007963 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7964
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007965 DCHECK(!IsLeafMethod());
7966}
7967
7968void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007969 // Explicit clinit checks triggered by static invokes must have been pruned by
7970 // art::PrepareForRegisterAllocation.
7971 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007972
7973 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7974 return;
7975 }
7976
7977 LocationSummary* locations = invoke->GetLocations();
7978 codegen_->GenerateStaticOrDirectCall(invoke,
7979 locations->HasTemps()
7980 ? locations->GetTemp(0)
7981 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007982}
7983
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007984void CodeGeneratorMIPS::GenerateVirtualCall(
7985 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007986 // Use the calling convention instead of the location of the receiver, as
7987 // intrinsics may have put the receiver in a different register. In the intrinsics
7988 // slow path, the arguments have been moved to the right place, so here we are
7989 // guaranteed that the receiver is the first register of the calling convention.
7990 InvokeDexCallingConvention calling_convention;
7991 Register receiver = calling_convention.GetRegisterAt(0);
7992
Chris Larsen3acee732015-11-18 13:31:08 -08007993 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007994 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7995 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7996 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007997 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007998
7999 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02008000 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08008001 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08008002 // Instead of simply (possibly) unpoisoning `temp` here, we should
8003 // emit a read barrier for the previous class reference load.
8004 // However this is not required in practice, as this is an
8005 // intermediate/temporary reference and because the current
8006 // concurrent copying collector keeps the from-space memory
8007 // intact/accessible until the end of the marking phase (the
8008 // concurrent copying collector may not in the future).
8009 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008010 // temp = temp->GetMethodAt(method_offset);
8011 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
8012 // T9 = temp->GetEntryPoint();
8013 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
8014 // T9();
8015 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008016 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01008017 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08008018}
8019
8020void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
8021 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
8022 return;
8023 }
8024
8025 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008026 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008027}
8028
8029void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00008030 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008031 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008032 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008033 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
8034 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008035 return;
8036 }
Vladimir Marko41559982017-01-06 14:04:23 +00008037 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008038 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008039 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08008040 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
8041 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07008042 ? LocationSummary::kCallOnSlowPath
8043 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008044 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07008045 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
8046 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
8047 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008048 switch (load_kind) {
8049 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008050 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008051 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008052 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008053 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008054 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008055 break;
8056 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008057 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008058 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
8059 codegen_->ClobberRA();
8060 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008061 break;
8062 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008063 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008064 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008065 locations->SetInAt(0, Location::RequiresRegister());
8066 break;
8067 default:
8068 break;
8069 }
8070 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008071 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
8072 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8073 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008074 RegisterSet caller_saves = RegisterSet::Empty();
8075 InvokeRuntimeCallingConvention calling_convention;
8076 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8077 locations->SetCustomSlowPathCallerSaves(caller_saves);
8078 } else {
8079 // For non-Baker read barriers we have a temp-clobbering call.
8080 }
8081 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008082}
8083
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008084// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8085// move.
8086void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00008087 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008088 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00008089 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01008090 return;
8091 }
Vladimir Marko41559982017-01-06 14:04:23 +00008092 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008093
Vladimir Marko41559982017-01-06 14:04:23 +00008094 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008095 Location out_loc = locations->Out();
8096 Register out = out_loc.AsRegister<Register>();
8097 Register base_or_current_method_reg;
8098 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008099 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008100 switch (load_kind) {
8101 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008102 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008103 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008104 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008105 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008106 base_or_current_method_reg =
8107 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008108 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008109 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008110 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008111 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8112 break;
8113 default:
8114 base_or_current_method_reg = ZERO;
8115 break;
8116 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008117
Alexey Frunze15958152017-02-09 19:08:30 -08008118 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8119 ? kWithoutReadBarrier
8120 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008121 bool generate_null_check = false;
8122 switch (load_kind) {
8123 case HLoadClass::LoadKind::kReferrersClass: {
8124 DCHECK(!cls->CanCallRuntime());
8125 DCHECK(!cls->MustGenerateClinitCheck());
8126 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8127 GenerateGcRootFieldLoad(cls,
8128 out_loc,
8129 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008130 ArtMethod::DeclaringClassOffset().Int32Value(),
8131 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008132 break;
8133 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008134 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008135 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008136 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008137 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008138 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008139 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008140 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008141 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8142 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008143 base_or_current_method_reg);
8144 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008145 break;
8146 }
8147 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08008148 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008149 uint32_t address = dchecked_integral_cast<uint32_t>(
8150 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
8151 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008152 if (isR6 || !has_irreducible_loops) {
8153 __ LoadLiteral(out,
8154 base_or_current_method_reg,
8155 codegen_->DeduplicateBootImageAddressLiteral(address));
8156 } else {
8157 __ LoadConst32(out, address);
8158 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008159 break;
8160 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008161 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008162 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008163 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008164 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008165 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008166 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008167 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008168 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8169 out,
8170 base_or_current_method_reg);
8171 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008172 break;
8173 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008174 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008175 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8176 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008177 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8178 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008179 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008180 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008181 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008182 GenerateGcRootFieldLoad(cls,
8183 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008184 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008185 /* placeholder */ 0x5678,
8186 read_barrier_option,
8187 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008188 generate_null_check = true;
8189 break;
8190 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008191 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008192 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8193 cls->GetTypeIndex(),
8194 cls->GetClass());
8195 bool reordering = __ SetReorder(false);
8196 __ Bind(&info->high_label);
8197 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008198 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008199 GenerateGcRootFieldLoad(cls,
8200 out_loc,
8201 out,
8202 /* placeholder */ 0x5678,
8203 read_barrier_option,
8204 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008205 break;
8206 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008207 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008208 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008209 LOG(FATAL) << "UNREACHABLE";
8210 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008211 }
8212
8213 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8214 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008215 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00008216 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07008217 codegen_->AddSlowPath(slow_path);
8218 if (generate_null_check) {
8219 __ Beqz(out, slow_path->GetEntryLabel());
8220 }
8221 if (cls->MustGenerateClinitCheck()) {
8222 GenerateClassInitializationCheck(slow_path, out);
8223 } else {
8224 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008225 }
8226 }
8227}
8228
8229static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008230 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008231}
8232
8233void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8234 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008235 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008236 locations->SetOut(Location::RequiresRegister());
8237}
8238
8239void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8240 Register out = load->GetLocations()->Out().AsRegister<Register>();
8241 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8242}
8243
8244void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008245 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008246}
8247
8248void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8249 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8250}
8251
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008252void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008253 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008254 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008255 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008256 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008257 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008258 switch (load_kind) {
8259 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008260 case HLoadString::LoadKind::kBootImageAddress:
8261 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008262 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008263 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008264 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008265 break;
8266 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008267 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008268 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
8269 codegen_->ClobberRA();
8270 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008271 break;
8272 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008273 FALLTHROUGH_INTENDED;
8274 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008275 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008276 locations->SetInAt(0, Location::RequiresRegister());
8277 break;
8278 default:
8279 break;
8280 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008281 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008282 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008283 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008284 } else {
8285 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008286 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8287 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8288 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008289 RegisterSet caller_saves = RegisterSet::Empty();
8290 InvokeRuntimeCallingConvention calling_convention;
8291 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8292 locations->SetCustomSlowPathCallerSaves(caller_saves);
8293 } else {
8294 // For non-Baker read barriers we have a temp-clobbering call.
8295 }
8296 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008297 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008298}
8299
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008300// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8301// move.
8302void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008303 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008304 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008305 Location out_loc = locations->Out();
8306 Register out = out_loc.AsRegister<Register>();
8307 Register base_or_current_method_reg;
8308 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008309 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008310 switch (load_kind) {
8311 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008312 case HLoadString::LoadKind::kBootImageAddress:
8313 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008314 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008315 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008316 base_or_current_method_reg =
8317 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008318 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008319 default:
8320 base_or_current_method_reg = ZERO;
8321 break;
8322 }
8323
8324 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008325 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008326 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008327 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008328 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008329 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008330 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008331 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8332 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008333 base_or_current_method_reg);
8334 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008335 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008336 }
8337 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008338 uint32_t address = dchecked_integral_cast<uint32_t>(
8339 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8340 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008341 if (isR6 || !has_irreducible_loops) {
8342 __ LoadLiteral(out,
8343 base_or_current_method_reg,
8344 codegen_->DeduplicateBootImageAddressLiteral(address));
8345 } else {
8346 __ LoadConst32(out, address);
8347 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008348 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008349 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008350 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008351 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008352 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008353 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008354 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008355 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008356 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008357 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8358 out,
8359 base_or_current_method_reg);
8360 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8361 return;
8362 }
8363 case HLoadString::LoadKind::kBssEntry: {
8364 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8365 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8366 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8367 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8368 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008369 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008370 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008371 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008372 GenerateGcRootFieldLoad(load,
8373 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008374 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008375 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008376 kCompilerReadBarrierOption,
8377 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008378 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008379 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008380 codegen_->AddSlowPath(slow_path);
8381 __ Beqz(out, slow_path->GetEntryLabel());
8382 __ Bind(slow_path->GetExitLabel());
8383 return;
8384 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008385 case HLoadString::LoadKind::kJitTableAddress: {
8386 CodeGeneratorMIPS::JitPatchInfo* info =
8387 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8388 load->GetStringIndex(),
8389 load->GetString());
8390 bool reordering = __ SetReorder(false);
8391 __ Bind(&info->high_label);
8392 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008393 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008394 GenerateGcRootFieldLoad(load,
8395 out_loc,
8396 out,
8397 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008398 kCompilerReadBarrierOption,
8399 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008400 return;
8401 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008402 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008403 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008404 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008405
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008406 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008407 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008408 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008409 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008410 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008411 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8412 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008413}
8414
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008415void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008416 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008417 locations->SetOut(Location::ConstantLocation(constant));
8418}
8419
8420void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8421 // Will be generated at use site.
8422}
8423
8424void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008425 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8426 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008427 InvokeRuntimeCallingConvention calling_convention;
8428 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8429}
8430
8431void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8432 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008433 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008434 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8435 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008436 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008437 }
8438 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8439}
8440
8441void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8442 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008443 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008444 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008445 case DataType::Type::kInt32:
8446 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008447 locations->SetInAt(0, Location::RequiresRegister());
8448 locations->SetInAt(1, Location::RequiresRegister());
8449 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8450 break;
8451
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008452 case DataType::Type::kFloat32:
8453 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008454 locations->SetInAt(0, Location::RequiresFpuRegister());
8455 locations->SetInAt(1, Location::RequiresFpuRegister());
8456 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8457 break;
8458
8459 default:
8460 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8461 }
8462}
8463
8464void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008465 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008466 LocationSummary* locations = instruction->GetLocations();
8467 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8468
8469 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008470 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008471 Register dst = locations->Out().AsRegister<Register>();
8472 Register lhs = locations->InAt(0).AsRegister<Register>();
8473 Register rhs = locations->InAt(1).AsRegister<Register>();
8474
8475 if (isR6) {
8476 __ MulR6(dst, lhs, rhs);
8477 } else {
8478 __ MulR2(dst, lhs, rhs);
8479 }
8480 break;
8481 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008482 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008483 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8484 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8485 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8486 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8487 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8488 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8489
8490 // Extra checks to protect caused by the existance of A1_A2.
8491 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8492 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8493 DCHECK_NE(dst_high, lhs_low);
8494 DCHECK_NE(dst_high, rhs_low);
8495
8496 // A_B * C_D
8497 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8498 // dst_lo: [ low(B*D) ]
8499 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8500
8501 if (isR6) {
8502 __ MulR6(TMP, lhs_high, rhs_low);
8503 __ MulR6(dst_high, lhs_low, rhs_high);
8504 __ Addu(dst_high, dst_high, TMP);
8505 __ MuhuR6(TMP, lhs_low, rhs_low);
8506 __ Addu(dst_high, dst_high, TMP);
8507 __ MulR6(dst_low, lhs_low, rhs_low);
8508 } else {
8509 __ MulR2(TMP, lhs_high, rhs_low);
8510 __ MulR2(dst_high, lhs_low, rhs_high);
8511 __ Addu(dst_high, dst_high, TMP);
8512 __ MultuR2(lhs_low, rhs_low);
8513 __ Mfhi(TMP);
8514 __ Addu(dst_high, dst_high, TMP);
8515 __ Mflo(dst_low);
8516 }
8517 break;
8518 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008519 case DataType::Type::kFloat32:
8520 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008521 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8522 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8523 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008524 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008525 __ MulS(dst, lhs, rhs);
8526 } else {
8527 __ MulD(dst, lhs, rhs);
8528 }
8529 break;
8530 }
8531 default:
8532 LOG(FATAL) << "Unexpected mul type " << type;
8533 }
8534}
8535
8536void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8537 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008538 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008539 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008540 case DataType::Type::kInt32:
8541 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008542 locations->SetInAt(0, Location::RequiresRegister());
8543 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8544 break;
8545
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008546 case DataType::Type::kFloat32:
8547 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008548 locations->SetInAt(0, Location::RequiresFpuRegister());
8549 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8550 break;
8551
8552 default:
8553 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8554 }
8555}
8556
8557void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008558 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008559 LocationSummary* locations = instruction->GetLocations();
8560
8561 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008562 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008563 Register dst = locations->Out().AsRegister<Register>();
8564 Register src = locations->InAt(0).AsRegister<Register>();
8565 __ Subu(dst, ZERO, src);
8566 break;
8567 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008568 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008569 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8570 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8571 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8572 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8573 __ Subu(dst_low, ZERO, src_low);
8574 __ Sltu(TMP, ZERO, dst_low);
8575 __ Subu(dst_high, ZERO, src_high);
8576 __ Subu(dst_high, dst_high, TMP);
8577 break;
8578 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008579 case DataType::Type::kFloat32:
8580 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008581 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8582 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008583 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008584 __ NegS(dst, src);
8585 } else {
8586 __ NegD(dst, src);
8587 }
8588 break;
8589 }
8590 default:
8591 LOG(FATAL) << "Unexpected neg type " << type;
8592 }
8593}
8594
8595void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008596 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8597 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008598 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008599 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008600 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8601 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008602}
8603
8604void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008605 // Note: if heap poisoning is enabled, the entry point takes care
8606 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008607 QuickEntrypointEnum entrypoint =
8608 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8609 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008610 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008611 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008612}
8613
8614void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008615 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8616 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008617 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008618 if (instruction->IsStringAlloc()) {
8619 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8620 } else {
8621 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008622 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008623 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008624}
8625
8626void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008627 // Note: if heap poisoning is enabled, the entry point takes care
8628 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008629 if (instruction->IsStringAlloc()) {
8630 // String is allocated through StringFactory. Call NewEmptyString entry point.
8631 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008632 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008633 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8634 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8635 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008636 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008637 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8638 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008639 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008640 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008641 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008642}
8643
8644void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008645 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008646 locations->SetInAt(0, Location::RequiresRegister());
8647 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8648}
8649
8650void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008651 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008652 LocationSummary* locations = instruction->GetLocations();
8653
8654 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008655 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008656 Register dst = locations->Out().AsRegister<Register>();
8657 Register src = locations->InAt(0).AsRegister<Register>();
8658 __ Nor(dst, src, ZERO);
8659 break;
8660 }
8661
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008662 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008663 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8664 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8665 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8666 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8667 __ Nor(dst_high, src_high, ZERO);
8668 __ Nor(dst_low, src_low, ZERO);
8669 break;
8670 }
8671
8672 default:
8673 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8674 }
8675}
8676
8677void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008678 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008679 locations->SetInAt(0, Location::RequiresRegister());
8680 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8681}
8682
8683void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8684 LocationSummary* locations = instruction->GetLocations();
8685 __ Xori(locations->Out().AsRegister<Register>(),
8686 locations->InAt(0).AsRegister<Register>(),
8687 1);
8688}
8689
8690void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008691 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8692 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008693}
8694
Calin Juravle2ae48182016-03-16 14:05:09 +00008695void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8696 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008697 return;
8698 }
8699 Location obj = instruction->GetLocations()->InAt(0);
8700
8701 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008702 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008703}
8704
Calin Juravle2ae48182016-03-16 14:05:09 +00008705void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008706 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008707 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008708
8709 Location obj = instruction->GetLocations()->InAt(0);
8710
8711 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8712}
8713
8714void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008715 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008716}
8717
8718void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8719 HandleBinaryOp(instruction);
8720}
8721
8722void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8723 HandleBinaryOp(instruction);
8724}
8725
8726void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8727 LOG(FATAL) << "Unreachable";
8728}
8729
8730void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008731 if (instruction->GetNext()->IsSuspendCheck() &&
8732 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8733 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8734 // The back edge will generate the suspend check.
8735 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8736 }
8737
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008738 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8739}
8740
8741void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008742 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008743 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8744 if (location.IsStackSlot()) {
8745 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8746 } else if (location.IsDoubleStackSlot()) {
8747 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8748 }
8749 locations->SetOut(location);
8750}
8751
8752void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8753 ATTRIBUTE_UNUSED) {
8754 // Nothing to do, the parameter is already at its location.
8755}
8756
8757void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8758 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008759 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008760 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8761}
8762
8763void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8764 ATTRIBUTE_UNUSED) {
8765 // Nothing to do, the method is already at its location.
8766}
8767
8768void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008769 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008770 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008771 locations->SetInAt(i, Location::Any());
8772 }
8773 locations->SetOut(Location::Any());
8774}
8775
8776void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8777 LOG(FATAL) << "Unreachable";
8778}
8779
8780void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008781 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008782 bool call_rem;
8783 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8784 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8785 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8786 } else {
8787 call_rem = (type != DataType::Type::kInt32);
8788 }
8789 LocationSummary::CallKind call_kind = call_rem
8790 ? LocationSummary::kCallOnMainOnly
8791 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008792 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008793
8794 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008795 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008796 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008797 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008798 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8799 break;
8800
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008801 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008802 if (call_rem) {
8803 InvokeRuntimeCallingConvention calling_convention;
8804 locations->SetInAt(0, Location::RegisterPairLocation(
8805 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8806 locations->SetInAt(1, Location::RegisterPairLocation(
8807 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8808 locations->SetOut(calling_convention.GetReturnLocation(type));
8809 } else {
8810 locations->SetInAt(0, Location::RequiresRegister());
8811 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8812 locations->SetOut(Location::RequiresRegister());
8813 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008814 break;
8815 }
8816
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008817 case DataType::Type::kFloat32:
8818 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008819 InvokeRuntimeCallingConvention calling_convention;
8820 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8821 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8822 locations->SetOut(calling_convention.GetReturnLocation(type));
8823 break;
8824 }
8825
8826 default:
8827 LOG(FATAL) << "Unexpected rem type " << type;
8828 }
8829}
8830
8831void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008832 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008833 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008834
8835 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008836 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008837 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008838 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008839 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008840 if (locations->InAt(1).IsConstant()) {
8841 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8842 if (imm == 0) {
8843 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8844 } else if (imm == 1 || imm == -1) {
8845 DivRemOneOrMinusOne(instruction);
8846 } else {
8847 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8848 DivRemByPowerOfTwo(instruction);
8849 }
8850 } else {
8851 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8852 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8853 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008854 break;
8855 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008856 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008857 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008858 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008859 break;
8860 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008861 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008862 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008863 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008864 break;
8865 }
8866 default:
8867 LOG(FATAL) << "Unexpected rem type " << type;
8868 }
8869}
8870
Aart Bik1f8d51b2018-02-15 10:42:37 -08008871static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
8872 LocationSummary* locations = new (allocator) LocationSummary(minmax);
8873 switch (minmax->GetResultType()) {
8874 case DataType::Type::kInt32:
8875 case DataType::Type::kInt64:
8876 locations->SetInAt(0, Location::RequiresRegister());
8877 locations->SetInAt(1, Location::RequiresRegister());
8878 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8879 break;
8880 case DataType::Type::kFloat32:
8881 case DataType::Type::kFloat64:
8882 locations->SetInAt(0, Location::RequiresFpuRegister());
8883 locations->SetInAt(1, Location::RequiresFpuRegister());
8884 locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap);
8885 break;
8886 default:
8887 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
8888 }
8889}
8890
Aart Bik351df3e2018-03-07 11:54:57 -08008891void InstructionCodeGeneratorMIPS::GenerateMinMaxInt(LocationSummary* locations,
8892 bool is_min,
8893 bool isR6,
8894 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08008895 if (isR6) {
8896 // Some architectures, such as ARM and MIPS (prior to r6), have a
8897 // conditional move instruction which only changes the target
8898 // (output) register if the condition is true (MIPS prior to r6 had
8899 // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions
8900 // always change the target (output) register. If the condition is
8901 // true the output register gets the contents of the "rs" register;
8902 // otherwise, the output register is set to zero. One consequence
8903 // of this is that to implement something like "rd = c==0 ? rs : rt"
8904 // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions.
8905 // After executing this pair of instructions one of the output
8906 // registers from the pair will necessarily contain zero. Then the
8907 // code ORs the output registers from the SELEQZ/SELNEZ instructions
8908 // to get the final result.
8909 //
8910 // The initial test to see if the output register is same as the
8911 // first input register is needed to make sure that value in the
8912 // first input register isn't clobbered before we've finished
8913 // computing the output value. The logic in the corresponding else
8914 // clause performs the same task but makes sure the second input
8915 // register isn't clobbered in the event that it's the same register
8916 // as the output register; the else clause also handles the case
8917 // where the output register is distinct from both the first, and the
8918 // second input registers.
8919 if (type == DataType::Type::kInt64) {
8920 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8921 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8922 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
8923 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
8924 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8925 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8926
8927 MipsLabel compare_done;
8928
8929 if (a_lo == b_lo) {
8930 if (out_lo != a_lo) {
8931 __ Move(out_lo, a_lo);
8932 __ Move(out_hi, a_hi);
8933 }
8934 } else {
8935 __ Slt(TMP, b_hi, a_hi);
8936 __ Bne(b_hi, a_hi, &compare_done);
8937
8938 __ Sltu(TMP, b_lo, a_lo);
8939
8940 __ Bind(&compare_done);
8941
8942 if (is_min) {
8943 __ Seleqz(AT, a_lo, TMP);
8944 __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo
8945 // because at this point we're
8946 // done using a_lo/b_lo.
8947 } else {
8948 __ Selnez(AT, a_lo, TMP);
8949 __ Seleqz(out_lo, b_lo, TMP); // ditto
8950 }
8951 __ Or(out_lo, out_lo, AT);
8952 if (is_min) {
8953 __ Seleqz(AT, a_hi, TMP);
8954 __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
8955 } else {
8956 __ Selnez(AT, a_hi, TMP);
8957 __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
8958 }
8959 __ Or(out_hi, out_hi, AT);
8960 }
8961 } else {
8962 DCHECK_EQ(type, DataType::Type::kInt32);
8963 Register a = locations->InAt(0).AsRegister<Register>();
8964 Register b = locations->InAt(1).AsRegister<Register>();
8965 Register out = locations->Out().AsRegister<Register>();
8966
8967 if (a == b) {
8968 if (out != a) {
8969 __ Move(out, a);
8970 }
8971 } else {
8972 __ Slt(AT, b, a);
8973 if (is_min) {
8974 __ Seleqz(TMP, a, AT);
8975 __ Selnez(AT, b, AT);
8976 } else {
8977 __ Selnez(TMP, a, AT);
8978 __ Seleqz(AT, b, AT);
8979 }
8980 __ Or(out, TMP, AT);
8981 }
8982 }
8983 } else { // !isR6
8984 if (type == DataType::Type::kInt64) {
8985 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8986 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8987 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
8988 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
8989 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8990 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8991
8992 MipsLabel compare_done;
8993
8994 if (a_lo == b_lo) {
8995 if (out_lo != a_lo) {
8996 __ Move(out_lo, a_lo);
8997 __ Move(out_hi, a_hi);
8998 }
8999 } else {
9000 __ Slt(TMP, a_hi, b_hi);
9001 __ Bne(a_hi, b_hi, &compare_done);
9002
9003 __ Sltu(TMP, a_lo, b_lo);
9004
9005 __ Bind(&compare_done);
9006
9007 if (is_min) {
9008 if (out_lo != a_lo) {
9009 __ Movn(out_hi, a_hi, TMP);
9010 __ Movn(out_lo, a_lo, TMP);
9011 }
9012 if (out_lo != b_lo) {
9013 __ Movz(out_hi, b_hi, TMP);
9014 __ Movz(out_lo, b_lo, TMP);
9015 }
9016 } else {
9017 if (out_lo != a_lo) {
9018 __ Movz(out_hi, a_hi, TMP);
9019 __ Movz(out_lo, a_lo, TMP);
9020 }
9021 if (out_lo != b_lo) {
9022 __ Movn(out_hi, b_hi, TMP);
9023 __ Movn(out_lo, b_lo, TMP);
9024 }
9025 }
9026 }
9027 } else {
9028 DCHECK_EQ(type, DataType::Type::kInt32);
9029 Register a = locations->InAt(0).AsRegister<Register>();
9030 Register b = locations->InAt(1).AsRegister<Register>();
9031 Register out = locations->Out().AsRegister<Register>();
9032
9033 if (a == b) {
9034 if (out != a) {
9035 __ Move(out, a);
9036 }
9037 } else {
9038 __ Slt(AT, a, b);
9039 if (is_min) {
9040 if (out != a) {
9041 __ Movn(out, a, AT);
9042 }
9043 if (out != b) {
9044 __ Movz(out, b, AT);
9045 }
9046 } else {
9047 if (out != a) {
9048 __ Movz(out, a, AT);
9049 }
9050 if (out != b) {
9051 __ Movn(out, b, AT);
9052 }
9053 }
9054 }
9055 }
9056 }
9057}
9058
9059void InstructionCodeGeneratorMIPS::GenerateMinMaxFP(LocationSummary* locations,
9060 bool is_min,
9061 bool isR6,
9062 DataType::Type type) {
9063 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9064 FRegister a = locations->InAt(0).AsFpuRegister<FRegister>();
9065 FRegister b = locations->InAt(1).AsFpuRegister<FRegister>();
9066
9067 if (isR6) {
9068 MipsLabel noNaNs;
9069 MipsLabel done;
9070 FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
9071
9072 // When Java computes min/max it prefers a NaN to a number; the
9073 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
9074 // the inputs is a NaN and the other is a valid number, the MIPS
9075 // instruction will return the number; Java wants the NaN value
9076 // returned. This is why there is extra logic preceding the use of
9077 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
9078 // NaN, return the NaN, otherwise return the min/max.
9079 if (type == DataType::Type::kFloat64) {
9080 __ CmpUnD(FTMP, a, b);
9081 __ Bc1eqz(FTMP, &noNaNs);
9082
9083 // One of the inputs is a NaN
9084 __ CmpEqD(ftmp, a, a);
9085 // If a == a then b is the NaN, otherwise a is the NaN.
9086 __ SelD(ftmp, a, b);
9087
9088 if (ftmp != out) {
9089 __ MovD(out, ftmp);
9090 }
9091
9092 __ B(&done);
9093
9094 __ Bind(&noNaNs);
9095
9096 if (is_min) {
9097 __ MinD(out, a, b);
9098 } else {
9099 __ MaxD(out, a, b);
9100 }
9101 } else {
9102 DCHECK_EQ(type, DataType::Type::kFloat32);
9103 __ CmpUnS(FTMP, a, b);
9104 __ Bc1eqz(FTMP, &noNaNs);
9105
9106 // One of the inputs is a NaN
9107 __ CmpEqS(ftmp, a, a);
9108 // If a == a then b is the NaN, otherwise a is the NaN.
9109 __ SelS(ftmp, a, b);
9110
9111 if (ftmp != out) {
9112 __ MovS(out, ftmp);
9113 }
9114
9115 __ B(&done);
9116
9117 __ Bind(&noNaNs);
9118
9119 if (is_min) {
9120 __ MinS(out, a, b);
9121 } else {
9122 __ MaxS(out, a, b);
9123 }
9124 }
9125
9126 __ Bind(&done);
9127
9128 } else { // !isR6
9129 MipsLabel ordered;
9130 MipsLabel compare;
9131 MipsLabel select;
9132 MipsLabel done;
9133
9134 if (type == DataType::Type::kFloat64) {
9135 __ CunD(a, b);
9136 } else {
9137 DCHECK_EQ(type, DataType::Type::kFloat32);
9138 __ CunS(a, b);
9139 }
9140 __ Bc1f(&ordered);
9141
9142 // a or b (or both) is a NaN. Return one, which is a NaN.
9143 if (type == DataType::Type::kFloat64) {
9144 __ CeqD(b, b);
9145 } else {
9146 __ CeqS(b, b);
9147 }
9148 __ B(&select);
9149
9150 __ Bind(&ordered);
9151
9152 // Neither is a NaN.
9153 // a == b? (-0.0 compares equal with +0.0)
9154 // If equal, handle zeroes, else compare further.
9155 if (type == DataType::Type::kFloat64) {
9156 __ CeqD(a, b);
9157 } else {
9158 __ CeqS(a, b);
9159 }
9160 __ Bc1f(&compare);
9161
9162 // a == b either bit for bit or one is -0.0 and the other is +0.0.
9163 if (type == DataType::Type::kFloat64) {
9164 __ MoveFromFpuHigh(TMP, a);
9165 __ MoveFromFpuHigh(AT, b);
9166 } else {
9167 __ Mfc1(TMP, a);
9168 __ Mfc1(AT, b);
9169 }
9170
9171 if (is_min) {
9172 // -0.0 prevails over +0.0.
9173 __ Or(TMP, TMP, AT);
9174 } else {
9175 // +0.0 prevails over -0.0.
9176 __ And(TMP, TMP, AT);
9177 }
9178
9179 if (type == DataType::Type::kFloat64) {
9180 __ Mfc1(AT, a);
9181 __ Mtc1(AT, out);
9182 __ MoveToFpuHigh(TMP, out);
9183 } else {
9184 __ Mtc1(TMP, out);
9185 }
9186 __ B(&done);
9187
9188 __ Bind(&compare);
9189
9190 if (type == DataType::Type::kFloat64) {
9191 if (is_min) {
9192 // return (a <= b) ? a : b;
9193 __ ColeD(a, b);
9194 } else {
9195 // return (a >= b) ? a : b;
9196 __ ColeD(b, a); // b <= a
9197 }
9198 } else {
9199 if (is_min) {
9200 // return (a <= b) ? a : b;
9201 __ ColeS(a, b);
9202 } else {
9203 // return (a >= b) ? a : b;
9204 __ ColeS(b, a); // b <= a
9205 }
9206 }
9207
9208 __ Bind(&select);
9209
9210 if (type == DataType::Type::kFloat64) {
9211 __ MovtD(out, a);
9212 __ MovfD(out, b);
9213 } else {
9214 __ MovtS(out, a);
9215 __ MovfS(out, b);
9216 }
9217
9218 __ Bind(&done);
9219 }
9220}
9221
Aart Bik351df3e2018-03-07 11:54:57 -08009222void InstructionCodeGeneratorMIPS::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
9223 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9224 DataType::Type type = minmax->GetResultType();
9225 switch (type) {
9226 case DataType::Type::kInt32:
9227 case DataType::Type::kInt64:
9228 GenerateMinMaxInt(minmax->GetLocations(), is_min, isR6, type);
9229 break;
9230 case DataType::Type::kFloat32:
9231 case DataType::Type::kFloat64:
9232 GenerateMinMaxFP(minmax->GetLocations(), is_min, isR6, type);
9233 break;
9234 default:
9235 LOG(FATAL) << "Unexpected type for HMinMax " << type;
9236 }
9237}
9238
Aart Bik1f8d51b2018-02-15 10:42:37 -08009239void LocationsBuilderMIPS::VisitMin(HMin* min) {
9240 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
9241}
9242
9243void InstructionCodeGeneratorMIPS::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08009244 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009245}
9246
9247void LocationsBuilderMIPS::VisitMax(HMax* max) {
9248 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
9249}
9250
9251void InstructionCodeGeneratorMIPS::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08009252 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009253}
9254
Aart Bik3dad3412018-02-28 12:01:46 -08009255void LocationsBuilderMIPS::VisitAbs(HAbs* abs) {
9256 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
9257 switch (abs->GetResultType()) {
9258 case DataType::Type::kInt32:
9259 case DataType::Type::kInt64:
9260 locations->SetInAt(0, Location::RequiresRegister());
9261 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9262 break;
9263 case DataType::Type::kFloat32:
9264 case DataType::Type::kFloat64:
9265 locations->SetInAt(0, Location::RequiresFpuRegister());
9266 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9267 break;
9268 default:
9269 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9270 }
9271}
9272
9273void InstructionCodeGeneratorMIPS::GenerateAbsFP(LocationSummary* locations,
9274 DataType::Type type,
9275 bool isR2OrNewer,
9276 bool isR6) {
9277 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
9278 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9279
9280 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
9281 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
9282 // (signaling NaN may become quiet though).
9283 //
9284 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
9285 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
9286 // affected by this instruction.
9287 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
9288 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
9289 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
9290 if (isR6) {
9291 if (type == DataType::Type::kFloat64) {
9292 __ AbsD(out, in);
9293 } else {
9294 DCHECK_EQ(type, DataType::Type::kFloat32);
9295 __ AbsS(out, in);
9296 }
9297 } else {
9298 if (type == DataType::Type::kFloat64) {
9299 if (in != out) {
9300 __ MovD(out, in);
9301 }
9302 __ MoveFromFpuHigh(TMP, in);
9303 // ins instruction is not available for R1.
9304 if (isR2OrNewer) {
9305 __ Ins(TMP, ZERO, 31, 1);
9306 } else {
9307 __ Sll(TMP, TMP, 1);
9308 __ Srl(TMP, TMP, 1);
9309 }
9310 __ MoveToFpuHigh(TMP, out);
9311 } else {
9312 DCHECK_EQ(type, DataType::Type::kFloat32);
9313 __ Mfc1(TMP, in);
9314 // ins instruction is not available for R1.
9315 if (isR2OrNewer) {
9316 __ Ins(TMP, ZERO, 31, 1);
9317 } else {
9318 __ Sll(TMP, TMP, 1);
9319 __ Srl(TMP, TMP, 1);
9320 }
9321 __ Mtc1(TMP, out);
9322 }
9323 }
9324}
9325
9326void InstructionCodeGeneratorMIPS::VisitAbs(HAbs* abs) {
9327 LocationSummary* locations = abs->GetLocations();
9328 bool isR2OrNewer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
9329 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9330 switch (abs->GetResultType()) {
9331 case DataType::Type::kInt32: {
9332 Register in = locations->InAt(0).AsRegister<Register>();
9333 Register out = locations->Out().AsRegister<Register>();
9334 __ Sra(AT, in, 31);
9335 __ Xor(out, in, AT);
9336 __ Subu(out, out, AT);
9337 break;
9338 }
9339 case DataType::Type::kInt64: {
9340 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9341 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9342 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9343 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9344 // The comments in this section show the analogous operations which would
9345 // be performed if we had 64-bit registers "in", and "out".
9346 // __ Dsra32(AT, in, 31);
9347 __ Sra(AT, in_hi, 31);
9348 // __ Xor(out, in, AT);
9349 __ Xor(TMP, in_lo, AT);
9350 __ Xor(out_hi, in_hi, AT);
9351 // __ Dsubu(out, out, AT);
9352 __ Subu(out_lo, TMP, AT);
9353 __ Sltu(TMP, out_lo, TMP);
9354 __ Addu(out_hi, out_hi, TMP);
9355 break;
9356 }
9357 case DataType::Type::kFloat32:
9358 case DataType::Type::kFloat64:
9359 GenerateAbsFP(locations, abs->GetResultType(), isR2OrNewer, isR6);
9360 break;
9361 default:
9362 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9363 }
9364}
9365
Igor Murashkind01745e2017-04-05 16:40:31 -07009366void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
9367 constructor_fence->SetLocations(nullptr);
9368}
9369
9370void InstructionCodeGeneratorMIPS::VisitConstructorFence(
9371 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
9372 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
9373}
9374
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009375void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9376 memory_barrier->SetLocations(nullptr);
9377}
9378
9379void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9380 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
9381}
9382
9383void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009384 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009385 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009386 locations->SetInAt(0, MipsReturnLocation(return_type));
9387}
9388
9389void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
9390 codegen_->GenerateFrameExit();
9391}
9392
9393void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
9394 ret->SetLocations(nullptr);
9395}
9396
9397void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
9398 codegen_->GenerateFrameExit();
9399}
9400
Alexey Frunze92d90602015-12-18 18:16:36 -08009401void LocationsBuilderMIPS::VisitRor(HRor* ror) {
9402 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009403}
9404
Alexey Frunze92d90602015-12-18 18:16:36 -08009405void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
9406 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009407}
9408
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009409void LocationsBuilderMIPS::VisitShl(HShl* shl) {
9410 HandleShift(shl);
9411}
9412
9413void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
9414 HandleShift(shl);
9415}
9416
9417void LocationsBuilderMIPS::VisitShr(HShr* shr) {
9418 HandleShift(shr);
9419}
9420
9421void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
9422 HandleShift(shr);
9423}
9424
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009425void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
9426 HandleBinaryOp(instruction);
9427}
9428
9429void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
9430 HandleBinaryOp(instruction);
9431}
9432
9433void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9434 HandleFieldGet(instruction, instruction->GetFieldInfo());
9435}
9436
9437void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9438 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
9439}
9440
9441void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
9442 HandleFieldSet(instruction, instruction->GetFieldInfo());
9443}
9444
9445void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01009446 HandleFieldSet(instruction,
9447 instruction->GetFieldInfo(),
9448 instruction->GetDexPc(),
9449 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009450}
9451
9452void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
9453 HUnresolvedInstanceFieldGet* instruction) {
9454 FieldAccessCallingConventionMIPS calling_convention;
9455 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9456 instruction->GetFieldType(),
9457 calling_convention);
9458}
9459
9460void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
9461 HUnresolvedInstanceFieldGet* instruction) {
9462 FieldAccessCallingConventionMIPS calling_convention;
9463 codegen_->GenerateUnresolvedFieldAccess(instruction,
9464 instruction->GetFieldType(),
9465 instruction->GetFieldIndex(),
9466 instruction->GetDexPc(),
9467 calling_convention);
9468}
9469
9470void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
9471 HUnresolvedInstanceFieldSet* instruction) {
9472 FieldAccessCallingConventionMIPS calling_convention;
9473 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9474 instruction->GetFieldType(),
9475 calling_convention);
9476}
9477
9478void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
9479 HUnresolvedInstanceFieldSet* instruction) {
9480 FieldAccessCallingConventionMIPS calling_convention;
9481 codegen_->GenerateUnresolvedFieldAccess(instruction,
9482 instruction->GetFieldType(),
9483 instruction->GetFieldIndex(),
9484 instruction->GetDexPc(),
9485 calling_convention);
9486}
9487
9488void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
9489 HUnresolvedStaticFieldGet* instruction) {
9490 FieldAccessCallingConventionMIPS calling_convention;
9491 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9492 instruction->GetFieldType(),
9493 calling_convention);
9494}
9495
9496void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
9497 HUnresolvedStaticFieldGet* instruction) {
9498 FieldAccessCallingConventionMIPS calling_convention;
9499 codegen_->GenerateUnresolvedFieldAccess(instruction,
9500 instruction->GetFieldType(),
9501 instruction->GetFieldIndex(),
9502 instruction->GetDexPc(),
9503 calling_convention);
9504}
9505
9506void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
9507 HUnresolvedStaticFieldSet* instruction) {
9508 FieldAccessCallingConventionMIPS calling_convention;
9509 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9510 instruction->GetFieldType(),
9511 calling_convention);
9512}
9513
9514void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9515 HUnresolvedStaticFieldSet* instruction) {
9516 FieldAccessCallingConventionMIPS calling_convention;
9517 codegen_->GenerateUnresolvedFieldAccess(instruction,
9518 instruction->GetFieldType(),
9519 instruction->GetFieldIndex(),
9520 instruction->GetDexPc(),
9521 calling_convention);
9522}
9523
9524void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009525 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9526 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009527 // In suspend check slow path, usually there are no caller-save registers at all.
9528 // If SIMD instructions are present, however, we force spilling all live SIMD
9529 // registers in full width (since the runtime only saves/restores lower part).
9530 locations->SetCustomSlowPathCallerSaves(
9531 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009532}
9533
9534void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9535 HBasicBlock* block = instruction->GetBlock();
9536 if (block->GetLoopInformation() != nullptr) {
9537 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9538 // The back edge will generate the suspend check.
9539 return;
9540 }
9541 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9542 // The goto will generate the suspend check.
9543 return;
9544 }
9545 GenerateSuspendCheck(instruction, nullptr);
9546}
9547
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009548void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009549 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9550 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009551 InvokeRuntimeCallingConvention calling_convention;
9552 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9553}
9554
9555void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009556 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009557 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9558}
9559
9560void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009561 DataType::Type input_type = conversion->GetInputType();
9562 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009563 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9564 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009565 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009566
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009567 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9568 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009569 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9570 }
9571
9572 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009573 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009574 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9575 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009576 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009577 }
9578
Vladimir Markoca6fff82017-10-03 14:49:14 +01009579 LocationSummary* locations =
9580 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009581
9582 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009583 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009584 locations->SetInAt(0, Location::RequiresFpuRegister());
9585 } else {
9586 locations->SetInAt(0, Location::RequiresRegister());
9587 }
9588
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009589 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009590 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9591 } else {
9592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9593 }
9594 } else {
9595 InvokeRuntimeCallingConvention calling_convention;
9596
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009597 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009598 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9599 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009600 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009601 locations->SetInAt(0, Location::RegisterPairLocation(
9602 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9603 }
9604
9605 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9606 }
9607}
9608
9609void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9610 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009611 DataType::Type result_type = conversion->GetResultType();
9612 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009613 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009614 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009615
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009616 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9617 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009618
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009619 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009620 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9621 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9622 Register src = locations->InAt(0).AsRegister<Register>();
9623
Alexey Frunzea871ef12016-06-27 15:20:11 -07009624 if (dst_low != src) {
9625 __ Move(dst_low, src);
9626 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009627 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009628 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009629 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009630 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009631 ? locations->InAt(0).AsRegisterPairLow<Register>()
9632 : locations->InAt(0).AsRegister<Register>();
9633
9634 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009635 case DataType::Type::kUint8:
9636 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009637 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009638 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009639 if (has_sign_extension) {
9640 __ Seb(dst, src);
9641 } else {
9642 __ Sll(dst, src, 24);
9643 __ Sra(dst, dst, 24);
9644 }
9645 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009646 case DataType::Type::kUint16:
9647 __ Andi(dst, src, 0xFFFF);
9648 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009649 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009650 if (has_sign_extension) {
9651 __ Seh(dst, src);
9652 } else {
9653 __ Sll(dst, src, 16);
9654 __ Sra(dst, dst, 16);
9655 }
9656 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009657 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009658 if (dst != src) {
9659 __ Move(dst, src);
9660 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009661 break;
9662
9663 default:
9664 LOG(FATAL) << "Unexpected type conversion from " << input_type
9665 << " to " << result_type;
9666 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009667 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9668 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009669 if (isR6) {
9670 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9671 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9672 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9673 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9674 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9675 __ Mtc1(src_low, FTMP);
9676 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009677 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009678 __ Cvtsl(dst, FTMP);
9679 } else {
9680 __ Cvtdl(dst, FTMP);
9681 }
9682 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009683 QuickEntrypointEnum entrypoint =
9684 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009685 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009686 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009687 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9688 } else {
9689 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9690 }
9691 }
9692 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009693 Register src = locations->InAt(0).AsRegister<Register>();
9694 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9695 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009696 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009697 __ Cvtsw(dst, FTMP);
9698 } else {
9699 __ Cvtdw(dst, FTMP);
9700 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009701 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009702 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9703 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009704
9705 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9706 // value of the output type if the input is outside of the range after the truncation or
9707 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9708 // results. This matches the desired float/double-to-int/long conversion exactly.
9709 //
9710 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9711 // value when the input is either a NaN or is outside of the range of the output type
9712 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9713 // the same result.
9714 //
9715 // The code takes care of the different behaviors by first comparing the input to the
9716 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9717 // If the input is greater than or equal to the minimum, it procedes to the truncate
9718 // instruction, which will handle such an input the same way irrespective of NAN2008.
9719 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9720 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009721 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009722 if (isR6) {
9723 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9724 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9725 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9726 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9727 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009729 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009730 __ TruncLS(FTMP, src);
9731 } else {
9732 __ TruncLD(FTMP, src);
9733 }
9734 __ Mfc1(dst_low, FTMP);
9735 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009736 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009737 QuickEntrypointEnum entrypoint =
9738 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009739 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009740 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009741 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9742 } else {
9743 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9744 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009745 }
9746 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009747 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9748 Register dst = locations->Out().AsRegister<Register>();
9749 MipsLabel truncate;
9750 MipsLabel done;
9751
Lena Djokicf4e23a82017-05-09 15:43:45 +02009752 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009753 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009754 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9755 __ LoadConst32(TMP, min_val);
9756 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009757 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009758 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9759 __ LoadConst32(TMP, High32Bits(min_val));
9760 __ Mtc1(ZERO, FTMP);
9761 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009762 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009763
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009764 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009765 __ ColeS(0, FTMP, src);
9766 } else {
9767 __ ColeD(0, FTMP, src);
9768 }
9769 __ Bc1t(0, &truncate);
9770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009771 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009772 __ CeqS(0, src, src);
9773 } else {
9774 __ CeqD(0, src, src);
9775 }
9776 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9777 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009778
9779 __ B(&done);
9780
9781 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009782 }
9783
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009784 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009785 __ TruncWS(FTMP, src);
9786 } else {
9787 __ TruncWD(FTMP, src);
9788 }
9789 __ Mfc1(dst, FTMP);
9790
Lena Djokicf4e23a82017-05-09 15:43:45 +02009791 if (!isR6) {
9792 __ Bind(&done);
9793 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009794 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009795 } else if (DataType::IsFloatingPointType(result_type) &&
9796 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009797 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9798 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009799 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009800 __ Cvtsd(dst, src);
9801 } else {
9802 __ Cvtds(dst, src);
9803 }
9804 } else {
9805 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9806 << " to " << result_type;
9807 }
9808}
9809
9810void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9811 HandleShift(ushr);
9812}
9813
9814void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9815 HandleShift(ushr);
9816}
9817
9818void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9819 HandleBinaryOp(instruction);
9820}
9821
9822void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9823 HandleBinaryOp(instruction);
9824}
9825
9826void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9827 // Nothing to do, this should be removed during prepare for register allocator.
9828 LOG(FATAL) << "Unreachable";
9829}
9830
9831void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9832 // Nothing to do, this should be removed during prepare for register allocator.
9833 LOG(FATAL) << "Unreachable";
9834}
9835
9836void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009837 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009838}
9839
9840void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009841 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009842}
9843
9844void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009845 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009846}
9847
9848void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009849 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009850}
9851
9852void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009853 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009854}
9855
9856void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009857 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009858}
9859
9860void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009861 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009862}
9863
9864void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009865 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009866}
9867
9868void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009869 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009870}
9871
9872void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009873 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009874}
9875
9876void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009877 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009878}
9879
9880void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009881 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009882}
9883
9884void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009885 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009886}
9887
9888void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009889 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009890}
9891
9892void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009893 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009894}
9895
9896void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009897 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009898}
9899
9900void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009901 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009902}
9903
9904void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009905 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009906}
9907
9908void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009909 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009910}
9911
9912void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009913 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009914}
9915
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009916void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9917 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009918 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009919 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009920 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9921 uint32_t num_entries = switch_instr->GetNumEntries();
9922 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9923 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9924 // instruction to simulate PC-relative addressing when accessing the jump table.
9925 // NAL clobbers RA. Make sure RA is preserved.
9926 codegen_->ClobberRA();
9927 }
9928 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009929}
9930
Alexey Frunze96b66822016-09-10 02:32:44 -07009931void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9932 int32_t lower_bound,
9933 uint32_t num_entries,
9934 HBasicBlock* switch_block,
9935 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009936 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009937 Register temp_reg = TMP;
9938 __ Addiu32(temp_reg, value_reg, -lower_bound);
9939 // Jump to default if index is negative
9940 // Note: We don't check the case that index is positive while value < lower_bound, because in
9941 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9942 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9943
Alexey Frunze96b66822016-09-10 02:32:44 -07009944 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009945 // Jump to successors[0] if value == lower_bound.
9946 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9947 int32_t last_index = 0;
9948 for (; num_entries - last_index > 2; last_index += 2) {
9949 __ Addiu(temp_reg, temp_reg, -2);
9950 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9951 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9952 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9953 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9954 }
9955 if (num_entries - last_index == 2) {
9956 // The last missing case_value.
9957 __ Addiu(temp_reg, temp_reg, -1);
9958 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009959 }
9960
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009961 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009962 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009963 __ B(codegen_->GetLabelOf(default_block));
9964 }
9965}
9966
Alexey Frunze96b66822016-09-10 02:32:44 -07009967void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9968 Register constant_area,
9969 int32_t lower_bound,
9970 uint32_t num_entries,
9971 HBasicBlock* switch_block,
9972 HBasicBlock* default_block) {
9973 // Create a jump table.
9974 std::vector<MipsLabel*> labels(num_entries);
9975 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9976 for (uint32_t i = 0; i < num_entries; i++) {
9977 labels[i] = codegen_->GetLabelOf(successors[i]);
9978 }
9979 JumpTable* table = __ CreateJumpTable(std::move(labels));
9980
9981 // Is the value in range?
9982 __ Addiu32(TMP, value_reg, -lower_bound);
9983 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9984 __ Sltiu(AT, TMP, num_entries);
9985 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9986 } else {
9987 __ LoadConst32(AT, num_entries);
9988 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9989 }
9990
9991 // We are in the range of the table.
9992 // Load the target address from the jump table, indexing by the value.
9993 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009994 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009995 __ Lw(TMP, TMP, 0);
9996 // Compute the absolute target address by adding the table start address
9997 // (the table contains offsets to targets relative to its start).
9998 __ Addu(TMP, TMP, AT);
9999 // And jump.
10000 __ Jr(TMP);
10001 __ NopIfNoReordering();
10002}
10003
10004void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
10005 int32_t lower_bound = switch_instr->GetStartValue();
10006 uint32_t num_entries = switch_instr->GetNumEntries();
10007 LocationSummary* locations = switch_instr->GetLocations();
10008 Register value_reg = locations->InAt(0).AsRegister<Register>();
10009 HBasicBlock* switch_block = switch_instr->GetBlock();
10010 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10011
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010012 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -070010013 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -070010014 //
10015 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
10016 // to access the jump table and it is implemented by changing HPackedSwitch to
10017 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
10018 // VisitMipsPackedSwitch()).
10019 //
10020 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
10021 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
10022 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -070010023 GenTableBasedPackedSwitch(value_reg,
10024 ZERO,
10025 lower_bound,
10026 num_entries,
10027 switch_block,
10028 default_block);
10029 } else {
10030 GenPackedSwitchWithCompares(value_reg,
10031 lower_bound,
10032 num_entries,
10033 switch_block,
10034 default_block);
10035 }
10036}
10037
10038void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10039 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010040 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -070010041 locations->SetInAt(0, Location::RequiresRegister());
10042 // Constant area pointer (HMipsComputeBaseMethodAddress).
10043 locations->SetInAt(1, Location::RequiresRegister());
10044}
10045
10046void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
10047 int32_t lower_bound = switch_instr->GetStartValue();
10048 uint32_t num_entries = switch_instr->GetNumEntries();
10049 LocationSummary* locations = switch_instr->GetLocations();
10050 Register value_reg = locations->InAt(0).AsRegister<Register>();
10051 Register constant_area = locations->InAt(1).AsRegister<Register>();
10052 HBasicBlock* switch_block = switch_instr->GetBlock();
10053 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
10054
10055 // This is an R2-only path. HPackedSwitch has been changed to
10056 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
10057 // required to address the jump table relative to PC.
10058 GenTableBasedPackedSwitch(value_reg,
10059 constant_area,
10060 lower_bound,
10061 num_entries,
10062 switch_block,
10063 default_block);
10064}
10065
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010066void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
10067 HMipsComputeBaseMethodAddress* insn) {
10068 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010069 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010070 locations->SetOut(Location::RequiresRegister());
10071}
10072
10073void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
10074 HMipsComputeBaseMethodAddress* insn) {
10075 LocationSummary* locations = insn->GetLocations();
10076 Register reg = locations->Out().AsRegister<Register>();
10077
10078 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
10079
10080 // Generate a dummy PC-relative call to obtain PC.
10081 __ Nal();
10082 // Grab the return address off RA.
10083 __ Move(reg, RA);
10084
10085 // Remember this offset (the obtained PC value) for later use with constant area.
10086 __ BindPcRelBaseLabel();
10087}
10088
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010089void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10090 // The trampoline uses the same calling convention as dex calling conventions,
10091 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
10092 // the method_idx.
10093 HandleInvoke(invoke);
10094}
10095
10096void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10097 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
10098}
10099
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010100void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10101 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010102 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010103 locations->SetInAt(0, Location::RequiresRegister());
10104 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010105}
10106
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010107void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10108 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +000010109 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010110 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010111 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010112 __ LoadFromOffset(kLoadWord,
10113 locations->Out().AsRegister<Register>(),
10114 locations->InAt(0).AsRegister<Register>(),
10115 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010116 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010117 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +000010118 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000010119 __ LoadFromOffset(kLoadWord,
10120 locations->Out().AsRegister<Register>(),
10121 locations->InAt(0).AsRegister<Register>(),
10122 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010123 __ LoadFromOffset(kLoadWord,
10124 locations->Out().AsRegister<Register>(),
10125 locations->Out().AsRegister<Register>(),
10126 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010127 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010128}
10129
xueliang.zhonge0eb4832017-10-30 13:43:14 +000010130void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10131 ATTRIBUTE_UNUSED) {
10132 LOG(FATAL) << "Unreachable";
10133}
10134
10135void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10136 ATTRIBUTE_UNUSED) {
10137 LOG(FATAL) << "Unreachable";
10138}
10139
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010140#undef __
10141#undef QUICK_ENTRY_POINT
10142
10143} // namespace mips
10144} // namespace art