blob: ae42bbcc70b2b833a7a8c969e0c7614686e1bbf7 [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 Markod8dbc8d2017-09-20 13:37:47 +01001600void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001601 DCHECK(linker_patches->empty());
1602 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001603 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001604 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001605 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001606 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001607 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001608 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001609 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001610 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001611 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001612 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001613 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001614 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001615 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001616 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001617 } else {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001618 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001619 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001620 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001621 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001622 boot_image_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001623 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001624 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1625 method_bss_entry_patches_, linker_patches);
1626 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1627 type_bss_entry_patches_, linker_patches);
1628 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1629 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001630 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001631}
1632
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001633CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001634 MethodReference target_method,
1635 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001636 return NewPcRelativePatch(
1637 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001638}
1639
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001640CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001641 MethodReference target_method,
1642 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001643 return NewPcRelativePatch(
1644 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001645}
1646
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001647CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001648 const DexFile& dex_file,
1649 dex::TypeIndex type_index,
1650 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001651 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001652}
1653
Vladimir Marko1998cd02017-01-13 13:02:58 +00001654CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001655 const DexFile& dex_file,
1656 dex::TypeIndex type_index,
1657 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001658 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001659}
1660
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001661CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001662 const DexFile& dex_file,
1663 dex::StringIndex string_index,
1664 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001665 return NewPcRelativePatch(
1666 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001667}
1668
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001669CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1670 const DexFile& dex_file,
1671 dex::StringIndex string_index,
1672 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001673 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001674}
1675
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001676CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001677 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001678 uint32_t offset_or_index,
1679 const PcRelativePatchInfo* info_high,
1680 ArenaDeque<PcRelativePatchInfo>* patches) {
1681 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001682 return &patches->back();
1683}
1684
Alexey Frunze06a46c42016-07-19 15:00:40 -07001685Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1686 return map->GetOrCreate(
1687 value,
1688 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1689}
1690
Alexey Frunze06a46c42016-07-19 15:00:40 -07001691Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001692 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001693}
1694
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001695void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001696 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001697 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001698 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001699 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001700 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001701 if (GetInstructionSetFeatures().IsR6()) {
1702 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001703 __ Bind(&info_high->label);
1704 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001705 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001706 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001707 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001708 } else {
1709 // If base is ZERO, emit NAL to obtain the actual base.
1710 if (base == ZERO) {
1711 // Generate a dummy PC-relative call to obtain PC.
1712 __ Nal();
1713 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001714 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001715 __ Lui(out, /* placeholder */ 0x1234);
1716 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1717 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1718 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001719 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001720 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001721 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001722 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001723 __ Addu(out, out, (base == ZERO) ? RA : base);
1724 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001725 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001726 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001727}
1728
Alexey Frunze627c1a02017-01-30 19:28:14 -08001729CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1730 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001731 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001732 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001733 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1734 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001735 return &jit_string_patches_.back();
1736}
1737
1738CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1739 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001740 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001741 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001742 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1743 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001744 return &jit_class_patches_.back();
1745}
1746
1747void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1748 const uint8_t* roots_data,
1749 const CodeGeneratorMIPS::JitPatchInfo& info,
1750 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001751 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1752 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001753 uintptr_t address =
1754 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1755 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1756 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001757 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1758 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1759 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1760 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001761 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001762 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1763 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001764 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001765 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001766 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1767 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001768 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001769 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1770 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001771}
1772
1773void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1774 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001775 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1776 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001777 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001778 }
1779 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001780 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1781 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001782 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001783 }
1784}
1785
Goran Jakovljevice114da22016-12-26 14:21:43 +01001786void CodeGeneratorMIPS::MarkGCCard(Register object,
1787 Register value,
1788 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001789 MipsLabel done;
1790 Register card = AT;
1791 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001792 if (value_can_be_null) {
1793 __ Beqz(value, &done);
1794 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001795 __ LoadFromOffset(kLoadWord,
1796 card,
1797 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001798 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001799 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1800 __ Addu(temp, card, temp);
1801 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001802 if (value_can_be_null) {
1803 __ Bind(&done);
1804 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001805}
1806
David Brazdil58282f42016-01-14 12:45:10 +00001807void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001808 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1809 blocked_core_registers_[ZERO] = true;
1810 blocked_core_registers_[K0] = true;
1811 blocked_core_registers_[K1] = true;
1812 blocked_core_registers_[GP] = true;
1813 blocked_core_registers_[SP] = true;
1814 blocked_core_registers_[RA] = true;
1815
1816 // AT and TMP(T8) are used as temporary/scratch registers
1817 // (similar to how AT is used by MIPS assemblers).
1818 blocked_core_registers_[AT] = true;
1819 blocked_core_registers_[TMP] = true;
1820 blocked_fpu_registers_[FTMP] = true;
1821
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001822 if (GetInstructionSetFeatures().HasMsa()) {
1823 // To be used just for MSA instructions.
1824 blocked_fpu_registers_[FTMP2] = true;
1825 }
1826
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001827 // Reserve suspend and thread registers.
1828 blocked_core_registers_[S0] = true;
1829 blocked_core_registers_[TR] = true;
1830
1831 // Reserve T9 for function calls
1832 blocked_core_registers_[T9] = true;
1833
1834 // Reserve odd-numbered FPU registers.
1835 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1836 blocked_fpu_registers_[i] = true;
1837 }
1838
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001839 if (GetGraph()->IsDebuggable()) {
1840 // Stubs do not save callee-save floating point registers. If the graph
1841 // is debuggable, we need to deal with these registers differently. For
1842 // now, just block them.
1843 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1844 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1845 }
1846 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001847}
1848
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001849size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1850 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1851 return kMipsWordSize;
1852}
1853
1854size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1855 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1856 return kMipsWordSize;
1857}
1858
1859size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001860 if (GetGraph()->HasSIMD()) {
1861 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1862 } else {
1863 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1864 }
1865 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866}
1867
1868size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001869 if (GetGraph()->HasSIMD()) {
1870 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1871 } else {
1872 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1873 }
1874 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001875}
1876
1877void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001878 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879}
1880
1881void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001882 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001883}
1884
Serban Constantinescufca16662016-07-14 09:21:59 +01001885constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1886
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1888 HInstruction* instruction,
1889 uint32_t dex_pc,
1890 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001891 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001892 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1893 IsDirectEntrypoint(entrypoint));
1894 if (EntrypointRequiresStackMap(entrypoint)) {
1895 RecordPcInfo(instruction, dex_pc, slow_path);
1896 }
1897}
1898
1899void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1900 HInstruction* instruction,
1901 SlowPathCode* slow_path,
1902 bool direct) {
1903 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1904 GenerateInvokeRuntime(entry_point_offset, direct);
1905}
1906
1907void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001908 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001909 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001910 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001911 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912 // Reserve argument space on stack (for $a0-$a3) for
1913 // entrypoints that directly reference native implementations.
1914 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001915 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001916 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001917 } else {
1918 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001920 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921}
1922
1923void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1924 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001925 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1926 const size_t status_byte_offset =
1927 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1928 constexpr uint32_t shifted_initialized_value =
1929 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1930
1931 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01001932 __ Sltiu(TMP, TMP, shifted_initialized_value);
1933 __ Bnez(TMP, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1935 __ Sync(0);
1936 __ Bind(slow_path->GetExitLabel());
1937}
1938
1939void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1940 __ Sync(0); // Only stype 0 is supported.
1941}
1942
1943void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1944 HBasicBlock* successor) {
1945 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001946 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1947
1948 if (slow_path == nullptr) {
1949 slow_path =
1950 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1951 instruction->SetSlowPath(slow_path);
1952 codegen_->AddSlowPath(slow_path);
1953 if (successor != nullptr) {
1954 DCHECK(successor->IsLoopHeader());
1955 }
1956 } else {
1957 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1958 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959
1960 __ LoadFromOffset(kLoadUnsignedHalfword,
1961 TMP,
1962 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001963 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001964 if (successor == nullptr) {
1965 __ Bnez(TMP, slow_path->GetEntryLabel());
1966 __ Bind(slow_path->GetReturnLabel());
1967 } else {
1968 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1969 __ B(slow_path->GetEntryLabel());
1970 // slow_path will return to GetLabelOf(successor).
1971 }
1972}
1973
1974InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1975 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001976 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001977 assembler_(codegen->GetAssembler()),
1978 codegen_(codegen) {}
1979
1980void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1981 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001982 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001983 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01001984 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001985 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001986 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001987 locations->SetInAt(0, Location::RequiresRegister());
1988 HInstruction* right = instruction->InputAt(1);
1989 bool can_use_imm = false;
1990 if (right->IsConstant()) {
1991 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1992 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1993 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001994 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001995 DCHECK(instruction->IsSub() || instruction->IsAdd());
1996 if (instruction->IsSub()) {
1997 imm = -imm;
1998 }
1999 if (isR6) {
2000 bool single_use = right->GetUses().HasExactlyOneElement();
2001 int16_t imm_high = High16Bits(imm);
2002 int16_t imm_low = Low16Bits(imm);
2003 if (imm_low < 0) {
2004 imm_high += 1;
2005 }
2006 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2007 } else {
2008 can_use_imm = IsInt<16>(imm);
2009 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002010 }
2011 }
2012 if (can_use_imm)
2013 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2014 else
2015 locations->SetInAt(1, Location::RequiresRegister());
2016 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2017 break;
2018 }
2019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002020 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002021 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002022 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2023 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 break;
2025 }
2026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 case DataType::Type::kFloat32:
2028 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002029 DCHECK(instruction->IsAdd() || instruction->IsSub());
2030 locations->SetInAt(0, Location::RequiresFpuRegister());
2031 locations->SetInAt(1, Location::RequiresFpuRegister());
2032 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2033 break;
2034
2035 default:
2036 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2037 }
2038}
2039
2040void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002042 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002043 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044
2045 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002046 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002047 Register dst = locations->Out().AsRegister<Register>();
2048 Register lhs = locations->InAt(0).AsRegister<Register>();
2049 Location rhs_location = locations->InAt(1);
2050
2051 Register rhs_reg = ZERO;
2052 int32_t rhs_imm = 0;
2053 bool use_imm = rhs_location.IsConstant();
2054 if (use_imm) {
2055 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2056 } else {
2057 rhs_reg = rhs_location.AsRegister<Register>();
2058 }
2059
2060 if (instruction->IsAnd()) {
2061 if (use_imm)
2062 __ Andi(dst, lhs, rhs_imm);
2063 else
2064 __ And(dst, lhs, rhs_reg);
2065 } else if (instruction->IsOr()) {
2066 if (use_imm)
2067 __ Ori(dst, lhs, rhs_imm);
2068 else
2069 __ Or(dst, lhs, rhs_reg);
2070 } else if (instruction->IsXor()) {
2071 if (use_imm)
2072 __ Xori(dst, lhs, rhs_imm);
2073 else
2074 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002075 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002076 DCHECK(instruction->IsAdd() || instruction->IsSub());
2077 if (use_imm) {
2078 if (instruction->IsSub()) {
2079 rhs_imm = -rhs_imm;
2080 }
2081 if (IsInt<16>(rhs_imm)) {
2082 __ Addiu(dst, lhs, rhs_imm);
2083 } else {
2084 DCHECK(isR6);
2085 int16_t rhs_imm_high = High16Bits(rhs_imm);
2086 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2087 if (rhs_imm_low < 0) {
2088 rhs_imm_high += 1;
2089 }
2090 __ Aui(dst, lhs, rhs_imm_high);
2091 if (rhs_imm_low != 0) {
2092 __ Addiu(dst, dst, rhs_imm_low);
2093 }
2094 }
2095 } else if (instruction->IsAdd()) {
2096 __ Addu(dst, lhs, rhs_reg);
2097 } else {
2098 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002099 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002100 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002101 }
2102 break;
2103 }
2104
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002105 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002106 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2107 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2108 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2109 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002110 Location rhs_location = locations->InAt(1);
2111 bool use_imm = rhs_location.IsConstant();
2112 if (!use_imm) {
2113 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2114 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2115 if (instruction->IsAnd()) {
2116 __ And(dst_low, lhs_low, rhs_low);
2117 __ And(dst_high, lhs_high, rhs_high);
2118 } else if (instruction->IsOr()) {
2119 __ Or(dst_low, lhs_low, rhs_low);
2120 __ Or(dst_high, lhs_high, rhs_high);
2121 } else if (instruction->IsXor()) {
2122 __ Xor(dst_low, lhs_low, rhs_low);
2123 __ Xor(dst_high, lhs_high, rhs_high);
2124 } else if (instruction->IsAdd()) {
2125 if (lhs_low == rhs_low) {
2126 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2127 __ Slt(TMP, lhs_low, ZERO);
2128 __ Addu(dst_low, lhs_low, rhs_low);
2129 } else {
2130 __ Addu(dst_low, lhs_low, rhs_low);
2131 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2132 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2133 }
2134 __ Addu(dst_high, lhs_high, rhs_high);
2135 __ Addu(dst_high, dst_high, TMP);
2136 } else {
2137 DCHECK(instruction->IsSub());
2138 __ Sltu(TMP, lhs_low, rhs_low);
2139 __ Subu(dst_low, lhs_low, rhs_low);
2140 __ Subu(dst_high, lhs_high, rhs_high);
2141 __ Subu(dst_high, dst_high, TMP);
2142 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002143 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002144 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2145 if (instruction->IsOr()) {
2146 uint32_t low = Low32Bits(value);
2147 uint32_t high = High32Bits(value);
2148 if (IsUint<16>(low)) {
2149 if (dst_low != lhs_low || low != 0) {
2150 __ Ori(dst_low, lhs_low, low);
2151 }
2152 } else {
2153 __ LoadConst32(TMP, low);
2154 __ Or(dst_low, lhs_low, TMP);
2155 }
2156 if (IsUint<16>(high)) {
2157 if (dst_high != lhs_high || high != 0) {
2158 __ Ori(dst_high, lhs_high, high);
2159 }
2160 } else {
2161 if (high != low) {
2162 __ LoadConst32(TMP, high);
2163 }
2164 __ Or(dst_high, lhs_high, TMP);
2165 }
2166 } else if (instruction->IsXor()) {
2167 uint32_t low = Low32Bits(value);
2168 uint32_t high = High32Bits(value);
2169 if (IsUint<16>(low)) {
2170 if (dst_low != lhs_low || low != 0) {
2171 __ Xori(dst_low, lhs_low, low);
2172 }
2173 } else {
2174 __ LoadConst32(TMP, low);
2175 __ Xor(dst_low, lhs_low, TMP);
2176 }
2177 if (IsUint<16>(high)) {
2178 if (dst_high != lhs_high || high != 0) {
2179 __ Xori(dst_high, lhs_high, high);
2180 }
2181 } else {
2182 if (high != low) {
2183 __ LoadConst32(TMP, high);
2184 }
2185 __ Xor(dst_high, lhs_high, TMP);
2186 }
2187 } else if (instruction->IsAnd()) {
2188 uint32_t low = Low32Bits(value);
2189 uint32_t high = High32Bits(value);
2190 if (IsUint<16>(low)) {
2191 __ Andi(dst_low, lhs_low, low);
2192 } else if (low != 0xFFFFFFFF) {
2193 __ LoadConst32(TMP, low);
2194 __ And(dst_low, lhs_low, TMP);
2195 } else if (dst_low != lhs_low) {
2196 __ Move(dst_low, lhs_low);
2197 }
2198 if (IsUint<16>(high)) {
2199 __ Andi(dst_high, lhs_high, high);
2200 } else if (high != 0xFFFFFFFF) {
2201 if (high != low) {
2202 __ LoadConst32(TMP, high);
2203 }
2204 __ And(dst_high, lhs_high, TMP);
2205 } else if (dst_high != lhs_high) {
2206 __ Move(dst_high, lhs_high);
2207 }
2208 } else {
2209 if (instruction->IsSub()) {
2210 value = -value;
2211 } else {
2212 DCHECK(instruction->IsAdd());
2213 }
2214 int32_t low = Low32Bits(value);
2215 int32_t high = High32Bits(value);
2216 if (IsInt<16>(low)) {
2217 if (dst_low != lhs_low || low != 0) {
2218 __ Addiu(dst_low, lhs_low, low);
2219 }
2220 if (low != 0) {
2221 __ Sltiu(AT, dst_low, low);
2222 }
2223 } else {
2224 __ LoadConst32(TMP, low);
2225 __ Addu(dst_low, lhs_low, TMP);
2226 __ Sltu(AT, dst_low, TMP);
2227 }
2228 if (IsInt<16>(high)) {
2229 if (dst_high != lhs_high || high != 0) {
2230 __ Addiu(dst_high, lhs_high, high);
2231 }
2232 } else {
2233 if (high != low) {
2234 __ LoadConst32(TMP, high);
2235 }
2236 __ Addu(dst_high, lhs_high, TMP);
2237 }
2238 if (low != 0) {
2239 __ Addu(dst_high, dst_high, AT);
2240 }
2241 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002242 }
2243 break;
2244 }
2245
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002246 case DataType::Type::kFloat32:
2247 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002248 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2249 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2250 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2251 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002252 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002253 __ AddS(dst, lhs, rhs);
2254 } else {
2255 __ AddD(dst, lhs, rhs);
2256 }
2257 } else {
2258 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002259 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 __ SubS(dst, lhs, rhs);
2261 } else {
2262 __ SubD(dst, lhs, rhs);
2263 }
2264 }
2265 break;
2266 }
2267
2268 default:
2269 LOG(FATAL) << "Unexpected binary operation type " << type;
2270 }
2271}
2272
2273void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002274 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002275
Vladimir Markoca6fff82017-10-03 14:49:14 +01002276 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002278 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002279 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002280 locations->SetInAt(0, Location::RequiresRegister());
2281 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2283 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002284 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002285 locations->SetInAt(0, Location::RequiresRegister());
2286 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2287 locations->SetOut(Location::RequiresRegister());
2288 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002289 default:
2290 LOG(FATAL) << "Unexpected shift type " << type;
2291 }
2292}
2293
2294static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2295
2296void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002297 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002298 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002300
2301 Location rhs_location = locations->InAt(1);
2302 bool use_imm = rhs_location.IsConstant();
2303 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2304 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002305 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002307 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002308 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2309 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002310
2311 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002313 Register dst = locations->Out().AsRegister<Register>();
2314 Register lhs = locations->InAt(0).AsRegister<Register>();
2315 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002316 if (shift_value == 0) {
2317 if (dst != lhs) {
2318 __ Move(dst, lhs);
2319 }
2320 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002321 __ Sll(dst, lhs, shift_value);
2322 } else if (instr->IsShr()) {
2323 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002324 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002325 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002326 } else {
2327 if (has_ins_rotr) {
2328 __ Rotr(dst, lhs, shift_value);
2329 } else {
2330 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2331 __ Srl(dst, lhs, shift_value);
2332 __ Or(dst, dst, TMP);
2333 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002334 }
2335 } else {
2336 if (instr->IsShl()) {
2337 __ Sllv(dst, lhs, rhs_reg);
2338 } else if (instr->IsShr()) {
2339 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002340 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002341 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002342 } else {
2343 if (has_ins_rotr) {
2344 __ Rotrv(dst, lhs, rhs_reg);
2345 } else {
2346 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002347 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2348 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2349 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2350 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2351 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002352 __ Sllv(TMP, lhs, TMP);
2353 __ Srlv(dst, lhs, rhs_reg);
2354 __ Or(dst, dst, TMP);
2355 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002356 }
2357 }
2358 break;
2359 }
2360
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002362 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2363 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2364 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2365 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2366 if (use_imm) {
2367 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002368 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002369 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002370 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002371 if (instr->IsShl()) {
2372 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2373 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2374 __ Sll(dst_low, lhs_low, shift_value);
2375 } else if (instr->IsShr()) {
2376 __ Srl(dst_low, lhs_low, shift_value);
2377 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2378 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002379 } else if (instr->IsUShr()) {
2380 __ Srl(dst_low, lhs_low, shift_value);
2381 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2382 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002383 } else {
2384 __ Srl(dst_low, lhs_low, shift_value);
2385 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2386 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002387 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002388 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002390 if (instr->IsShl()) {
2391 __ Sll(dst_low, lhs_low, shift_value);
2392 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2393 __ Sll(dst_high, lhs_high, shift_value);
2394 __ Or(dst_high, dst_high, TMP);
2395 } else if (instr->IsShr()) {
2396 __ Sra(dst_high, lhs_high, shift_value);
2397 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2398 __ Srl(dst_low, lhs_low, shift_value);
2399 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002400 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002401 __ Srl(dst_high, lhs_high, shift_value);
2402 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2403 __ Srl(dst_low, lhs_low, shift_value);
2404 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002405 } else {
2406 __ Srl(TMP, lhs_low, shift_value);
2407 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2408 __ Or(dst_low, dst_low, TMP);
2409 __ Srl(TMP, lhs_high, shift_value);
2410 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2411 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002412 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002413 }
2414 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002415 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002416 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002417 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418 __ Move(dst_low, ZERO);
2419 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002420 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002422 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002423 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002424 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002425 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002426 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002427 // 64-bit rotation by 32 is just a swap.
2428 __ Move(dst_low, lhs_high);
2429 __ Move(dst_high, lhs_low);
2430 } else {
2431 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002432 __ Srl(dst_low, lhs_high, shift_value_high);
2433 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2434 __ Srl(dst_high, lhs_low, shift_value_high);
2435 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002436 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002437 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2438 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002439 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002440 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2441 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002442 __ Or(dst_high, dst_high, TMP);
2443 }
2444 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002445 }
2446 }
2447 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002448 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002449 MipsLabel done;
2450 if (instr->IsShl()) {
2451 __ Sllv(dst_low, lhs_low, rhs_reg);
2452 __ Nor(AT, ZERO, rhs_reg);
2453 __ Srl(TMP, lhs_low, 1);
2454 __ Srlv(TMP, TMP, AT);
2455 __ Sllv(dst_high, lhs_high, rhs_reg);
2456 __ Or(dst_high, dst_high, TMP);
2457 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002458 if (isR6) {
2459 __ Beqzc(TMP, &done, /* is_bare */ true);
2460 __ Move(dst_high, dst_low);
2461 __ Move(dst_low, ZERO);
2462 } else {
2463 __ Movn(dst_high, dst_low, TMP);
2464 __ Movn(dst_low, ZERO, TMP);
2465 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002466 } else if (instr->IsShr()) {
2467 __ Srav(dst_high, lhs_high, rhs_reg);
2468 __ Nor(AT, ZERO, rhs_reg);
2469 __ Sll(TMP, lhs_high, 1);
2470 __ Sllv(TMP, TMP, AT);
2471 __ Srlv(dst_low, lhs_low, rhs_reg);
2472 __ Or(dst_low, dst_low, TMP);
2473 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002474 if (isR6) {
2475 __ Beqzc(TMP, &done, /* is_bare */ true);
2476 __ Move(dst_low, dst_high);
2477 __ Sra(dst_high, dst_high, 31);
2478 } else {
2479 __ Sra(AT, dst_high, 31);
2480 __ Movn(dst_low, dst_high, TMP);
2481 __ Movn(dst_high, AT, TMP);
2482 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002483 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002484 __ Srlv(dst_high, lhs_high, rhs_reg);
2485 __ Nor(AT, ZERO, rhs_reg);
2486 __ Sll(TMP, lhs_high, 1);
2487 __ Sllv(TMP, TMP, AT);
2488 __ Srlv(dst_low, lhs_low, rhs_reg);
2489 __ Or(dst_low, dst_low, TMP);
2490 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002491 if (isR6) {
2492 __ Beqzc(TMP, &done, /* is_bare */ true);
2493 __ Move(dst_low, dst_high);
2494 __ Move(dst_high, ZERO);
2495 } else {
2496 __ Movn(dst_low, dst_high, TMP);
2497 __ Movn(dst_high, ZERO, TMP);
2498 }
2499 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002500 __ Nor(AT, ZERO, rhs_reg);
2501 __ Srlv(TMP, lhs_low, rhs_reg);
2502 __ Sll(dst_low, lhs_high, 1);
2503 __ Sllv(dst_low, dst_low, AT);
2504 __ Or(dst_low, dst_low, TMP);
2505 __ Srlv(TMP, lhs_high, rhs_reg);
2506 __ Sll(dst_high, lhs_low, 1);
2507 __ Sllv(dst_high, dst_high, AT);
2508 __ Or(dst_high, dst_high, TMP);
2509 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002510 if (isR6) {
2511 __ Beqzc(TMP, &done, /* is_bare */ true);
2512 __ Move(TMP, dst_high);
2513 __ Move(dst_high, dst_low);
2514 __ Move(dst_low, TMP);
2515 } else {
2516 __ Movn(AT, dst_high, TMP);
2517 __ Movn(dst_high, dst_low, TMP);
2518 __ Movn(dst_low, AT, TMP);
2519 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002520 }
2521 __ Bind(&done);
2522 }
2523 break;
2524 }
2525
2526 default:
2527 LOG(FATAL) << "Unexpected shift operation type " << type;
2528 }
2529}
2530
2531void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2532 HandleBinaryOp(instruction);
2533}
2534
2535void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2536 HandleBinaryOp(instruction);
2537}
2538
2539void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2540 HandleBinaryOp(instruction);
2541}
2542
2543void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2544 HandleBinaryOp(instruction);
2545}
2546
2547void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002549 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002551 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002552 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2553 object_array_get_with_read_barrier
2554 ? LocationSummary::kCallOnSlowPath
2555 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002556 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2557 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2558 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002559 locations->SetInAt(0, Location::RequiresRegister());
2560 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002562 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2563 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002564 // The output overlaps in the case of an object array get with
2565 // read barriers enabled: we do not want the move to overwrite the
2566 // array's location, as we need it to emit the read barrier.
2567 locations->SetOut(Location::RequiresRegister(),
2568 object_array_get_with_read_barrier
2569 ? Location::kOutputOverlap
2570 : Location::kNoOutputOverlap);
2571 }
2572 // We need a temporary register for the read barrier marking slow
2573 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2574 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002575 bool temp_needed = instruction->GetIndex()->IsConstant()
2576 ? !kBakerReadBarrierThunksEnableForFields
2577 : !kBakerReadBarrierThunksEnableForArrays;
2578 if (temp_needed) {
2579 locations->AddTemp(Location::RequiresRegister());
2580 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002581 }
2582}
2583
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002584static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2585 auto null_checker = [codegen, instruction]() {
2586 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002587 };
2588 return null_checker;
2589}
2590
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002591void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2592 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002593 Location obj_loc = locations->InAt(0);
2594 Register obj = obj_loc.AsRegister<Register>();
2595 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002597 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002598 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002599
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002600 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002601 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2602 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002603 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002604 case DataType::Type::kBool:
2605 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002606 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 if (index.IsConstant()) {
2608 size_t offset =
2609 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002610 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002611 } else {
2612 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002613 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002614 }
2615 break;
2616 }
2617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002618 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002619 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 if (index.IsConstant()) {
2621 size_t offset =
2622 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002623 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624 } else {
2625 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002626 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002627 }
2628 break;
2629 }
2630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002631 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002632 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002633 if (maybe_compressed_char_at) {
2634 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2635 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2636 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2637 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2638 "Expecting 0=compressed, 1=uncompressed");
2639 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002640 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002641 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2642 if (maybe_compressed_char_at) {
2643 MipsLabel uncompressed_load, done;
2644 __ Bnez(TMP, &uncompressed_load);
2645 __ LoadFromOffset(kLoadUnsignedByte,
2646 out,
2647 obj,
2648 data_offset + (const_index << TIMES_1));
2649 __ B(&done);
2650 __ Bind(&uncompressed_load);
2651 __ LoadFromOffset(kLoadUnsignedHalfword,
2652 out,
2653 obj,
2654 data_offset + (const_index << TIMES_2));
2655 __ Bind(&done);
2656 } else {
2657 __ LoadFromOffset(kLoadUnsignedHalfword,
2658 out,
2659 obj,
2660 data_offset + (const_index << TIMES_2),
2661 null_checker);
2662 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002663 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002664 Register index_reg = index.AsRegister<Register>();
2665 if (maybe_compressed_char_at) {
2666 MipsLabel uncompressed_load, done;
2667 __ Bnez(TMP, &uncompressed_load);
2668 __ Addu(TMP, obj, index_reg);
2669 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2670 __ B(&done);
2671 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002672 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002673 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2674 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002675 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2676 __ Addu(TMP, index_reg, obj);
2677 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002678 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002679 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002680 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2681 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002682 }
2683 break;
2684 }
2685
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002686 case DataType::Type::kInt16: {
2687 Register out = out_loc.AsRegister<Register>();
2688 if (index.IsConstant()) {
2689 size_t offset =
2690 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2691 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002692 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2693 __ Addu(TMP, index.AsRegister<Register>(), obj);
2694 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002695 } else {
2696 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2697 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2698 }
2699 break;
2700 }
2701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002702 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002703 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002704 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002705 if (index.IsConstant()) {
2706 size_t offset =
2707 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002708 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002709 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2710 __ Addu(TMP, index.AsRegister<Register>(), obj);
2711 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002712 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002713 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002714 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002715 }
2716 break;
2717 }
2718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002720 static_assert(
2721 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2722 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2723 // /* HeapReference<Object> */ out =
2724 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2725 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002726 bool temp_needed = index.IsConstant()
2727 ? !kBakerReadBarrierThunksEnableForFields
2728 : !kBakerReadBarrierThunksEnableForArrays;
2729 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002730 // Note that a potential implicit null check is handled in this
2731 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002732 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2733 if (index.IsConstant()) {
2734 // Array load with a constant index can be treated as a field load.
2735 size_t offset =
2736 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2737 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2738 out_loc,
2739 obj,
2740 offset,
2741 temp,
2742 /* needs_null_check */ false);
2743 } else {
2744 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2745 out_loc,
2746 obj,
2747 data_offset,
2748 index,
2749 temp,
2750 /* needs_null_check */ false);
2751 }
Alexey Frunze15958152017-02-09 19:08:30 -08002752 } else {
2753 Register out = out_loc.AsRegister<Register>();
2754 if (index.IsConstant()) {
2755 size_t offset =
2756 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2757 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2758 // If read barriers are enabled, emit read barriers other than
2759 // Baker's using a slow path (and also unpoison the loaded
2760 // reference, if heap poisoning is enabled).
2761 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2762 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002763 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002764 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2765 // If read barriers are enabled, emit read barriers other than
2766 // Baker's using a slow path (and also unpoison the loaded
2767 // reference, if heap poisoning is enabled).
2768 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2769 out_loc,
2770 out_loc,
2771 obj_loc,
2772 data_offset,
2773 index);
2774 }
2775 }
2776 break;
2777 }
2778
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002779 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002780 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002781 if (index.IsConstant()) {
2782 size_t offset =
2783 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002784 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002785 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2786 __ Addu(TMP, index.AsRegister<Register>(), obj);
2787 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002788 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002789 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002790 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791 }
2792 break;
2793 }
2794
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002795 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002796 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002797 if (index.IsConstant()) {
2798 size_t offset =
2799 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002800 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002801 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2802 __ Addu(TMP, index.AsRegister<Register>(), obj);
2803 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002804 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002805 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002806 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002807 }
2808 break;
2809 }
2810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002812 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002813 if (index.IsConstant()) {
2814 size_t offset =
2815 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002816 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002817 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2818 __ Addu(TMP, index.AsRegister<Register>(), obj);
2819 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002821 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002822 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002823 }
2824 break;
2825 }
2826
Aart Bik66c158e2018-01-31 12:55:04 -08002827 case DataType::Type::kUint32:
2828 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002830 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2831 UNREACHABLE();
2832 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002833}
2834
2835void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002836 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002837 locations->SetInAt(0, Location::RequiresRegister());
2838 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2839}
2840
2841void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2842 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002843 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002844 Register obj = locations->InAt(0).AsRegister<Register>();
2845 Register out = locations->Out().AsRegister<Register>();
2846 __ LoadFromOffset(kLoadWord, out, obj, offset);
2847 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002848 // Mask out compression flag from String's array length.
2849 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2850 __ Srl(out, out, 1u);
2851 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002852}
2853
Alexey Frunzef58b2482016-09-02 22:14:06 -07002854Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2855 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2856 ? Location::ConstantLocation(instruction->AsConstant())
2857 : Location::RequiresRegister();
2858}
2859
2860Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2861 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2862 // We can store a non-zero float or double constant without first loading it into the FPU,
2863 // but we should only prefer this if the constant has a single use.
2864 if (instruction->IsConstant() &&
2865 (instruction->AsConstant()->IsZeroBitPattern() ||
2866 instruction->GetUses().HasExactlyOneElement())) {
2867 return Location::ConstantLocation(instruction->AsConstant());
2868 // Otherwise fall through and require an FPU register for the constant.
2869 }
2870 return Location::RequiresFpuRegister();
2871}
2872
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002873void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002875
2876 bool needs_write_barrier =
2877 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2878 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2879
Vladimir Markoca6fff82017-10-03 14:49:14 +01002880 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002881 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002882 may_need_runtime_call_for_type_check ?
2883 LocationSummary::kCallOnSlowPath :
2884 LocationSummary::kNoCall);
2885
2886 locations->SetInAt(0, Location::RequiresRegister());
2887 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002889 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002890 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002891 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2892 }
2893 if (needs_write_barrier) {
2894 // Temporary register for the write barrier.
2895 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002896 }
2897}
2898
2899void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2900 LocationSummary* locations = instruction->GetLocations();
2901 Register obj = locations->InAt(0).AsRegister<Register>();
2902 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002903 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002904 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002905 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002906 bool needs_write_barrier =
2907 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002908 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002909 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002910
2911 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002913 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002914 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002915 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002916 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002917 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002918 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002919 __ Addu(base_reg, obj, index.AsRegister<Register>());
2920 }
2921 if (value_location.IsConstant()) {
2922 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2923 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2924 } else {
2925 Register value = value_location.AsRegister<Register>();
2926 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002927 }
2928 break;
2929 }
2930
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002931 case DataType::Type::kUint16:
2932 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002933 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002934 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002935 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002936 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2937 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002938 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002939 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002940 }
2941 if (value_location.IsConstant()) {
2942 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2943 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2944 } else {
2945 Register value = value_location.AsRegister<Register>();
2946 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002947 }
2948 break;
2949 }
2950
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002951 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002952 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2953 if (index.IsConstant()) {
2954 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002955 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2956 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002957 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002958 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002959 }
2960 if (value_location.IsConstant()) {
2961 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2962 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2963 } else {
2964 Register value = value_location.AsRegister<Register>();
2965 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2966 }
2967 break;
2968 }
2969
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002971 if (value_location.IsConstant()) {
2972 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002973 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002974 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002975 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002976 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002977 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002978 }
Alexey Frunze15958152017-02-09 19:08:30 -08002979 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2980 DCHECK_EQ(value, 0);
2981 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2982 DCHECK(!needs_write_barrier);
2983 DCHECK(!may_need_runtime_call_for_type_check);
2984 break;
2985 }
2986
2987 DCHECK(needs_write_barrier);
2988 Register value = value_location.AsRegister<Register>();
2989 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2990 Register temp2 = TMP; // Doesn't need to survive slow path.
2991 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2992 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2993 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2994 MipsLabel done;
2995 SlowPathCodeMIPS* slow_path = nullptr;
2996
2997 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002998 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002999 codegen_->AddSlowPath(slow_path);
3000 if (instruction->GetValueCanBeNull()) {
3001 MipsLabel non_zero;
3002 __ Bnez(value, &non_zero);
3003 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3004 if (index.IsConstant()) {
3005 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003006 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3007 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003008 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003009 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003010 }
Alexey Frunze15958152017-02-09 19:08:30 -08003011 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3012 __ B(&done);
3013 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003014 }
Alexey Frunze15958152017-02-09 19:08:30 -08003015
3016 // Note that when read barriers are enabled, the type checks
3017 // are performed without read barriers. This is fine, even in
3018 // the case where a class object is in the from-space after
3019 // the flip, as a comparison involving such a type would not
3020 // produce a false positive; it may of course produce a false
3021 // negative, in which case we would take the ArraySet slow
3022 // path.
3023
3024 // /* HeapReference<Class> */ temp1 = obj->klass_
3025 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3026 __ MaybeUnpoisonHeapReference(temp1);
3027
3028 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3029 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3030 // /* HeapReference<Class> */ temp2 = value->klass_
3031 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3032 // If heap poisoning is enabled, no need to unpoison `temp1`
3033 // nor `temp2`, as we are comparing two poisoned references.
3034
3035 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3036 MipsLabel do_put;
3037 __ Beq(temp1, temp2, &do_put);
3038 // If heap poisoning is enabled, the `temp1` reference has
3039 // not been unpoisoned yet; unpoison it now.
3040 __ MaybeUnpoisonHeapReference(temp1);
3041
3042 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3043 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3044 // If heap poisoning is enabled, no need to unpoison
3045 // `temp1`, as we are comparing against null below.
3046 __ Bnez(temp1, slow_path->GetEntryLabel());
3047 __ Bind(&do_put);
3048 } else {
3049 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3050 }
3051 }
3052
3053 Register source = value;
3054 if (kPoisonHeapReferences) {
3055 // Note that in the case where `value` is a null reference,
3056 // we do not enter this block, as a null reference does not
3057 // need poisoning.
3058 __ Move(temp1, value);
3059 __ PoisonHeapReference(temp1);
3060 source = temp1;
3061 }
3062
3063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3064 if (index.IsConstant()) {
3065 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003066 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003067 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003068 }
3069 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3070
3071 if (!may_need_runtime_call_for_type_check) {
3072 codegen_->MaybeRecordImplicitNullCheck(instruction);
3073 }
3074
3075 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3076
3077 if (done.IsLinked()) {
3078 __ Bind(&done);
3079 }
3080
3081 if (slow_path != nullptr) {
3082 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003083 }
3084 break;
3085 }
3086
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003087 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003088 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003089 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003090 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003091 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3092 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003094 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003095 }
3096 if (value_location.IsConstant()) {
3097 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3098 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3099 } else {
3100 Register value = value_location.AsRegisterPairLow<Register>();
3101 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003102 }
3103 break;
3104 }
3105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003106 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003107 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003108 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003109 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003110 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3111 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003112 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003113 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003114 }
3115 if (value_location.IsConstant()) {
3116 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3117 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3118 } else {
3119 FRegister value = value_location.AsFpuRegister<FRegister>();
3120 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003121 }
3122 break;
3123 }
3124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003126 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003127 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003128 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003129 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3130 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003131 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003132 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003133 }
3134 if (value_location.IsConstant()) {
3135 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3136 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3137 } else {
3138 FRegister value = value_location.AsFpuRegister<FRegister>();
3139 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003140 }
3141 break;
3142 }
3143
Aart Bik66c158e2018-01-31 12:55:04 -08003144 case DataType::Type::kUint32:
3145 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003147 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3148 UNREACHABLE();
3149 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003150}
3151
Lena Djokica2901602017-09-21 13:50:52 +02003152void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3153 HIntermediateArrayAddressIndex* instruction) {
3154 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003155 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003156
3157 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3158
3159 locations->SetInAt(0, Location::RequiresRegister());
3160 locations->SetInAt(1, Location::ConstantLocation(shift));
3161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3162}
3163
3164void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3165 HIntermediateArrayAddressIndex* instruction) {
3166 LocationSummary* locations = instruction->GetLocations();
3167 Register index_reg = locations->InAt(0).AsRegister<Register>();
3168 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3169 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3170}
3171
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003172void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003173 RegisterSet caller_saves = RegisterSet::Empty();
3174 InvokeRuntimeCallingConvention calling_convention;
3175 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3176 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3177 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003178
3179 HInstruction* index = instruction->InputAt(0);
3180 HInstruction* length = instruction->InputAt(1);
3181
3182 bool const_index = false;
3183 bool const_length = false;
3184
3185 if (index->IsConstant()) {
3186 if (length->IsConstant()) {
3187 const_index = true;
3188 const_length = true;
3189 } else {
3190 int32_t index_value = index->AsIntConstant()->GetValue();
3191 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3192 const_index = true;
3193 }
3194 }
3195 } else if (length->IsConstant()) {
3196 int32_t length_value = length->AsIntConstant()->GetValue();
3197 if (IsUint<15>(length_value)) {
3198 const_length = true;
3199 }
3200 }
3201
3202 locations->SetInAt(0, const_index
3203 ? Location::ConstantLocation(index->AsConstant())
3204 : Location::RequiresRegister());
3205 locations->SetInAt(1, const_length
3206 ? Location::ConstantLocation(length->AsConstant())
3207 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003208}
3209
3210void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3211 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003212 Location index_loc = locations->InAt(0);
3213 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003214
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003215 if (length_loc.IsConstant()) {
3216 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3217 if (index_loc.IsConstant()) {
3218 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3219 if (index < 0 || index >= length) {
3220 BoundsCheckSlowPathMIPS* slow_path =
3221 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3222 codegen_->AddSlowPath(slow_path);
3223 __ B(slow_path->GetEntryLabel());
3224 } else {
3225 // Nothing to be done.
3226 }
3227 return;
3228 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003229
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003230 BoundsCheckSlowPathMIPS* slow_path =
3231 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3232 codegen_->AddSlowPath(slow_path);
3233 Register index = index_loc.AsRegister<Register>();
3234 if (length == 0) {
3235 __ B(slow_path->GetEntryLabel());
3236 } else if (length == 1) {
3237 __ Bnez(index, slow_path->GetEntryLabel());
3238 } else {
3239 DCHECK(IsUint<15>(length)) << length;
3240 __ Sltiu(TMP, index, length);
3241 __ Beqz(TMP, slow_path->GetEntryLabel());
3242 }
3243 } else {
3244 Register length = length_loc.AsRegister<Register>();
3245 BoundsCheckSlowPathMIPS* slow_path =
3246 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3247 codegen_->AddSlowPath(slow_path);
3248 if (index_loc.IsConstant()) {
3249 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3250 if (index < 0) {
3251 __ B(slow_path->GetEntryLabel());
3252 } else if (index == 0) {
3253 __ Blez(length, slow_path->GetEntryLabel());
3254 } else {
3255 DCHECK(IsInt<16>(index + 1)) << index;
3256 __ Sltiu(TMP, length, index + 1);
3257 __ Bnez(TMP, slow_path->GetEntryLabel());
3258 }
3259 } else {
3260 Register index = index_loc.AsRegister<Register>();
3261 __ Bgeu(index, length, slow_path->GetEntryLabel());
3262 }
3263 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003264}
3265
Alexey Frunze15958152017-02-09 19:08:30 -08003266// Temp is used for read barrier.
3267static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3268 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003269 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003270 (kUseBakerReadBarrier ||
3271 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3272 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3273 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3274 return 1;
3275 }
3276 return 0;
3277}
3278
3279// Extra temp is used for read barrier.
3280static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3281 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3282}
3283
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003284void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003285 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003286 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003287 LocationSummary* locations =
3288 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003289 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003290 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003291 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003292}
3293
3294void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003295 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003296 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003297 Location obj_loc = locations->InAt(0);
3298 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003299 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003300 Location temp_loc = locations->GetTemp(0);
3301 Register temp = temp_loc.AsRegister<Register>();
3302 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3303 DCHECK_LE(num_temps, 2u);
3304 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003305 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3306 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3307 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3308 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3309 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3310 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3311 const uint32_t object_array_data_offset =
3312 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3313 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003314
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003315 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003316 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003317 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3318 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003319 codegen_->AddSlowPath(slow_path);
3320
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003321 // Avoid this check if we know `obj` is not null.
3322 if (instruction->MustDoNullCheck()) {
3323 __ Beqz(obj, &done);
3324 }
3325
3326 switch (type_check_kind) {
3327 case TypeCheckKind::kExactCheck:
3328 case TypeCheckKind::kArrayCheck: {
3329 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003330 GenerateReferenceLoadTwoRegisters(instruction,
3331 temp_loc,
3332 obj_loc,
3333 class_offset,
3334 maybe_temp2_loc,
3335 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003336 // Jump to slow path for throwing the exception or doing a
3337 // more involved array check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003338 __ Bne(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003339 break;
3340 }
3341
3342 case TypeCheckKind::kAbstractClassCheck: {
3343 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003344 GenerateReferenceLoadTwoRegisters(instruction,
3345 temp_loc,
3346 obj_loc,
3347 class_offset,
3348 maybe_temp2_loc,
3349 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003350 // If the class is abstract, we eagerly fetch the super class of the
3351 // object to avoid doing a comparison we know will fail.
3352 MipsLabel loop;
3353 __ Bind(&loop);
3354 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003355 GenerateReferenceLoadOneRegister(instruction,
3356 temp_loc,
3357 super_offset,
3358 maybe_temp2_loc,
3359 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003360 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3361 // exception.
3362 __ Beqz(temp, slow_path->GetEntryLabel());
3363 // Otherwise, compare the classes.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003364 __ Bne(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003365 break;
3366 }
3367
3368 case TypeCheckKind::kClassHierarchyCheck: {
3369 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003370 GenerateReferenceLoadTwoRegisters(instruction,
3371 temp_loc,
3372 obj_loc,
3373 class_offset,
3374 maybe_temp2_loc,
3375 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003376 // Walk over the class hierarchy to find a match.
3377 MipsLabel loop;
3378 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003379 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003380 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003381 GenerateReferenceLoadOneRegister(instruction,
3382 temp_loc,
3383 super_offset,
3384 maybe_temp2_loc,
3385 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003386 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3387 // exception. Otherwise, jump to the beginning of the loop.
3388 __ Bnez(temp, &loop);
3389 __ B(slow_path->GetEntryLabel());
3390 break;
3391 }
3392
3393 case TypeCheckKind::kArrayObjectCheck: {
3394 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003395 GenerateReferenceLoadTwoRegisters(instruction,
3396 temp_loc,
3397 obj_loc,
3398 class_offset,
3399 maybe_temp2_loc,
3400 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003401 // Do an exact check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003402 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003403 // Otherwise, we need to check that the object's class is a non-primitive array.
3404 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003405 GenerateReferenceLoadOneRegister(instruction,
3406 temp_loc,
3407 component_offset,
3408 maybe_temp2_loc,
3409 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003410 // If the component type is null, jump to the slow path to throw the exception.
3411 __ Beqz(temp, slow_path->GetEntryLabel());
3412 // Otherwise, the object is indeed an array, further check that this component
3413 // type is not a primitive type.
3414 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3415 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3416 __ Bnez(temp, slow_path->GetEntryLabel());
3417 break;
3418 }
3419
3420 case TypeCheckKind::kUnresolvedCheck:
3421 // We always go into the type check slow path for the unresolved check case.
3422 // We cannot directly call the CheckCast runtime entry point
3423 // without resorting to a type checking slow path here (i.e. by
3424 // calling InvokeRuntime directly), as it would require to
3425 // assign fixed registers for the inputs of this HInstanceOf
3426 // instruction (following the runtime calling convention), which
3427 // might be cluttered by the potential first read barrier
3428 // emission at the beginning of this method.
3429 __ B(slow_path->GetEntryLabel());
3430 break;
3431
3432 case TypeCheckKind::kInterfaceCheck: {
3433 // Avoid read barriers to improve performance of the fast path. We can not get false
3434 // positives by doing this.
3435 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003436 GenerateReferenceLoadTwoRegisters(instruction,
3437 temp_loc,
3438 obj_loc,
3439 class_offset,
3440 maybe_temp2_loc,
3441 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003442 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003443 GenerateReferenceLoadTwoRegisters(instruction,
3444 temp_loc,
3445 temp_loc,
3446 iftable_offset,
3447 maybe_temp2_loc,
3448 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003449 // Iftable is never null.
3450 __ Lw(TMP, temp, array_length_offset);
3451 // Loop through the iftable and check if any class matches.
3452 MipsLabel loop;
3453 __ Bind(&loop);
3454 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3455 __ Beqz(TMP, slow_path->GetEntryLabel());
3456 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3457 __ MaybeUnpoisonHeapReference(AT);
3458 // Go to next interface.
3459 __ Addiu(TMP, TMP, -2);
3460 // Compare the classes and continue the loop if they do not match.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003461 __ Bne(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003462 break;
3463 }
3464 }
3465
3466 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003467 __ Bind(slow_path->GetExitLabel());
3468}
3469
3470void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3471 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003472 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003473 locations->SetInAt(0, Location::RequiresRegister());
3474 if (check->HasUses()) {
3475 locations->SetOut(Location::SameAsFirstInput());
3476 }
3477}
3478
3479void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3480 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003481 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003482 check->GetLoadClass(),
3483 check,
3484 check->GetDexPc(),
3485 true);
3486 codegen_->AddSlowPath(slow_path);
3487 GenerateClassInitializationCheck(slow_path,
3488 check->GetLocations()->InAt(0).AsRegister<Register>());
3489}
3490
3491void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003492 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003493
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003494 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003495 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003496
3497 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003498 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003499 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003500 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003501 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003502 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003503 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003504 locations->SetInAt(0, Location::RequiresRegister());
3505 locations->SetInAt(1, Location::RequiresRegister());
3506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3507 break;
3508
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003509 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003510 locations->SetInAt(0, Location::RequiresRegister());
3511 locations->SetInAt(1, Location::RequiresRegister());
3512 // Output overlaps because it is written before doing the low comparison.
3513 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3514 break;
3515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003516 case DataType::Type::kFloat32:
3517 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003518 locations->SetInAt(0, Location::RequiresFpuRegister());
3519 locations->SetInAt(1, Location::RequiresFpuRegister());
3520 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003521 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003522
3523 default:
3524 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3525 }
3526}
3527
3528void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3529 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003530 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003531 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003532 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003533
3534 // 0 if: left == right
3535 // 1 if: left > right
3536 // -1 if: left < right
3537 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003538 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003539 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003540 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003541 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003542 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003543 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003544 Register lhs = locations->InAt(0).AsRegister<Register>();
3545 Register rhs = locations->InAt(1).AsRegister<Register>();
3546 __ Slt(TMP, lhs, rhs);
3547 __ Slt(res, rhs, lhs);
3548 __ Subu(res, res, TMP);
3549 break;
3550 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003551 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003552 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003553 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3554 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3555 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3556 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3557 // TODO: more efficient (direct) comparison with a constant.
3558 __ Slt(TMP, lhs_high, rhs_high);
3559 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3560 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3561 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3562 __ Sltu(TMP, lhs_low, rhs_low);
3563 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3564 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3565 __ Bind(&done);
3566 break;
3567 }
3568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003570 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003571 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3572 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3573 MipsLabel done;
3574 if (isR6) {
3575 __ CmpEqS(FTMP, lhs, rhs);
3576 __ LoadConst32(res, 0);
3577 __ Bc1nez(FTMP, &done);
3578 if (gt_bias) {
3579 __ CmpLtS(FTMP, lhs, rhs);
3580 __ LoadConst32(res, -1);
3581 __ Bc1nez(FTMP, &done);
3582 __ LoadConst32(res, 1);
3583 } else {
3584 __ CmpLtS(FTMP, rhs, lhs);
3585 __ LoadConst32(res, 1);
3586 __ Bc1nez(FTMP, &done);
3587 __ LoadConst32(res, -1);
3588 }
3589 } else {
3590 if (gt_bias) {
3591 __ ColtS(0, lhs, rhs);
3592 __ LoadConst32(res, -1);
3593 __ Bc1t(0, &done);
3594 __ CeqS(0, lhs, rhs);
3595 __ LoadConst32(res, 1);
3596 __ Movt(res, ZERO, 0);
3597 } else {
3598 __ ColtS(0, rhs, lhs);
3599 __ LoadConst32(res, 1);
3600 __ Bc1t(0, &done);
3601 __ CeqS(0, lhs, rhs);
3602 __ LoadConst32(res, -1);
3603 __ Movt(res, ZERO, 0);
3604 }
3605 }
3606 __ Bind(&done);
3607 break;
3608 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003609 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003610 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003611 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3612 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3613 MipsLabel done;
3614 if (isR6) {
3615 __ CmpEqD(FTMP, lhs, rhs);
3616 __ LoadConst32(res, 0);
3617 __ Bc1nez(FTMP, &done);
3618 if (gt_bias) {
3619 __ CmpLtD(FTMP, lhs, rhs);
3620 __ LoadConst32(res, -1);
3621 __ Bc1nez(FTMP, &done);
3622 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003623 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003624 __ CmpLtD(FTMP, rhs, lhs);
3625 __ LoadConst32(res, 1);
3626 __ Bc1nez(FTMP, &done);
3627 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003628 }
3629 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003630 if (gt_bias) {
3631 __ ColtD(0, lhs, rhs);
3632 __ LoadConst32(res, -1);
3633 __ Bc1t(0, &done);
3634 __ CeqD(0, lhs, rhs);
3635 __ LoadConst32(res, 1);
3636 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003637 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003638 __ ColtD(0, rhs, lhs);
3639 __ LoadConst32(res, 1);
3640 __ Bc1t(0, &done);
3641 __ CeqD(0, lhs, rhs);
3642 __ LoadConst32(res, -1);
3643 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003644 }
3645 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003646 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003647 break;
3648 }
3649
3650 default:
3651 LOG(FATAL) << "Unimplemented compare type " << in_type;
3652 }
3653}
3654
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003655void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003656 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003657 switch (instruction->InputAt(0)->GetType()) {
3658 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003659 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003660 locations->SetInAt(0, Location::RequiresRegister());
3661 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3662 break;
3663
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003664 case DataType::Type::kFloat32:
3665 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003666 locations->SetInAt(0, Location::RequiresFpuRegister());
3667 locations->SetInAt(1, Location::RequiresFpuRegister());
3668 break;
3669 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003670 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003671 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3672 }
3673}
3674
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003675void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003676 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003677 return;
3678 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003680 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003681 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003682
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003683 switch (type) {
3684 default:
3685 // Integer case.
3686 GenerateIntCompare(instruction->GetCondition(), locations);
3687 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003689 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003690 GenerateLongCompare(instruction->GetCondition(), locations);
3691 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003693 case DataType::Type::kFloat32:
3694 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003695 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3696 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003697 }
3698}
3699
Alexey Frunze7e99e052015-11-24 19:28:01 -08003700void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3701 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003702
3703 LocationSummary* locations = instruction->GetLocations();
3704 Location second = locations->InAt(1);
3705 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003706 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003707 DCHECK(imm == 1 || imm == -1);
3708
Lena Djokic4b8025c2017-12-21 16:15:50 +01003709 if (instruction->GetResultType() == DataType::Type::kInt32) {
3710 Register out = locations->Out().AsRegister<Register>();
3711 Register dividend = locations->InAt(0).AsRegister<Register>();
3712
3713 if (instruction->IsRem()) {
3714 __ Move(out, ZERO);
3715 } else {
3716 if (imm == -1) {
3717 __ Subu(out, ZERO, dividend);
3718 } else if (out != dividend) {
3719 __ Move(out, dividend);
3720 }
3721 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003722 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003723 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3724 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3725 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3726 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3727 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3728
3729 if (instruction->IsRem()) {
3730 __ Move(out_high, ZERO);
3731 __ Move(out_low, ZERO);
3732 } else {
3733 if (imm == -1) {
3734 __ Subu(out_low, ZERO, in_low);
3735 __ Sltu(AT, ZERO, out_low);
3736 __ Subu(out_high, ZERO, in_high);
3737 __ Subu(out_high, out_high, AT);
3738 } else {
3739 __ Move(out_low, in_low);
3740 __ Move(out_high, in_high);
3741 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003742 }
3743 }
3744}
3745
3746void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3747 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003748
3749 LocationSummary* locations = instruction->GetLocations();
3750 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003751 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3752 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003753 DCHECK(second.IsConstant());
3754
Lena Djokic4b8025c2017-12-21 16:15:50 +01003755 if (instruction->GetResultType() == DataType::Type::kInt32) {
3756 Register out = locations->Out().AsRegister<Register>();
3757 Register dividend = locations->InAt(0).AsRegister<Register>();
3758 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3759 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3760 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003761
Lena Djokic4b8025c2017-12-21 16:15:50 +01003762 if (instruction->IsDiv()) {
3763 if (ctz_imm == 1) {
3764 // Fast path for division by +/-2, which is very common.
3765 __ Srl(TMP, dividend, 31);
3766 } else {
3767 __ Sra(TMP, dividend, 31);
3768 __ Srl(TMP, TMP, 32 - ctz_imm);
3769 }
3770 __ Addu(out, dividend, TMP);
3771 __ Sra(out, out, ctz_imm);
3772 if (imm < 0) {
3773 __ Subu(out, ZERO, out);
3774 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003775 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003776 if (ctz_imm == 1) {
3777 // Fast path for modulo +/-2, which is very common.
3778 __ Sra(TMP, dividend, 31);
3779 __ Subu(out, dividend, TMP);
3780 __ Andi(out, out, 1);
3781 __ Addu(out, out, TMP);
3782 } else {
3783 __ Sra(TMP, dividend, 31);
3784 __ Srl(TMP, TMP, 32 - ctz_imm);
3785 __ Addu(out, dividend, TMP);
3786 if (IsUint<16>(abs_imm - 1)) {
3787 __ Andi(out, out, abs_imm - 1);
3788 } else {
3789 if (is_r2_or_newer) {
3790 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3791 } else {
3792 __ Sll(out, out, 32 - ctz_imm);
3793 __ Srl(out, out, 32 - ctz_imm);
3794 }
3795 }
3796 __ Subu(out, out, TMP);
3797 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003798 }
3799 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003800 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3801 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3802 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3803 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3804 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3805 int64_t imm = Int64FromConstant(second.GetConstant());
3806 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3807 int ctz_imm = CTZ(abs_imm);
3808
3809 if (instruction->IsDiv()) {
3810 if (ctz_imm < 32) {
3811 if (ctz_imm == 1) {
3812 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003813 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003814 __ Sra(AT, in_high, 31);
3815 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003816 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003817 __ Addu(AT, AT, in_low);
3818 __ Sltu(TMP, AT, in_low);
3819 __ Addu(out_high, in_high, TMP);
3820 __ Srl(out_low, AT, ctz_imm);
3821 if (is_r2_or_newer) {
3822 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3823 __ Sra(out_high, out_high, ctz_imm);
3824 } else {
3825 __ Sll(AT, out_high, 32 - ctz_imm);
3826 __ Sra(out_high, out_high, ctz_imm);
3827 __ Or(out_low, out_low, AT);
3828 }
3829 if (imm < 0) {
3830 __ Subu(out_low, ZERO, out_low);
3831 __ Sltu(AT, ZERO, out_low);
3832 __ Subu(out_high, ZERO, out_high);
3833 __ Subu(out_high, out_high, AT);
3834 }
3835 } else if (ctz_imm == 32) {
3836 __ Sra(AT, in_high, 31);
3837 __ Addu(AT, AT, in_low);
3838 __ Sltu(AT, AT, in_low);
3839 __ Addu(out_low, in_high, AT);
3840 if (imm < 0) {
3841 __ Srl(TMP, out_low, 31);
3842 __ Subu(out_low, ZERO, out_low);
3843 __ Sltu(AT, ZERO, out_low);
3844 __ Subu(out_high, TMP, AT);
3845 } else {
3846 __ Sra(out_high, out_low, 31);
3847 }
3848 } else if (ctz_imm < 63) {
3849 __ Sra(AT, in_high, 31);
3850 __ Srl(TMP, AT, 64 - ctz_imm);
3851 __ Addu(AT, AT, in_low);
3852 __ Sltu(AT, AT, in_low);
3853 __ Addu(out_low, in_high, AT);
3854 __ Addu(out_low, out_low, TMP);
3855 __ Sra(out_low, out_low, ctz_imm - 32);
3856 if (imm < 0) {
3857 __ Subu(out_low, ZERO, out_low);
3858 }
3859 __ Sra(out_high, out_low, 31);
3860 } else {
3861 DCHECK_LT(imm, 0);
3862 if (is_r6) {
3863 __ Aui(AT, in_high, 0x8000);
3864 } else {
3865 __ Lui(AT, 0x8000);
3866 __ Xor(AT, AT, in_high);
3867 }
3868 __ Or(AT, AT, in_low);
3869 __ Sltiu(out_low, AT, 1);
3870 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003871 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003872 } else {
3873 if ((ctz_imm == 1) && !is_r6) {
3874 __ Andi(AT, in_low, 1);
3875 __ Sll(TMP, in_low, 31);
3876 __ And(TMP, in_high, TMP);
3877 __ Sra(out_high, TMP, 31);
3878 __ Or(out_low, out_high, AT);
3879 } else if (ctz_imm < 32) {
3880 __ Sra(AT, in_high, 31);
3881 if (ctz_imm <= 16) {
3882 __ Andi(out_low, in_low, abs_imm - 1);
3883 } else if (is_r2_or_newer) {
3884 __ Ext(out_low, in_low, 0, ctz_imm);
3885 } else {
3886 __ Sll(out_low, in_low, 32 - ctz_imm);
3887 __ Srl(out_low, out_low, 32 - ctz_imm);
3888 }
3889 if (is_r6) {
3890 __ Selnez(out_high, AT, out_low);
3891 } else {
3892 __ Movz(AT, ZERO, out_low);
3893 __ Move(out_high, AT);
3894 }
3895 if (is_r2_or_newer) {
3896 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
3897 } else {
3898 __ Sll(AT, out_high, ctz_imm);
3899 __ Or(out_low, out_low, AT);
3900 }
3901 } else if (ctz_imm == 32) {
3902 __ Sra(AT, in_high, 31);
3903 __ Move(out_low, in_low);
3904 if (is_r6) {
3905 __ Selnez(out_high, AT, out_low);
3906 } else {
3907 __ Movz(AT, ZERO, out_low);
3908 __ Move(out_high, AT);
3909 }
3910 } else if (ctz_imm < 63) {
3911 __ Sra(AT, in_high, 31);
3912 __ Move(TMP, in_low);
3913 if (ctz_imm - 32 <= 16) {
3914 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
3915 } else if (is_r2_or_newer) {
3916 __ Ext(out_high, in_high, 0, ctz_imm - 32);
3917 } else {
3918 __ Sll(out_high, in_high, 64 - ctz_imm);
3919 __ Srl(out_high, out_high, 64 - ctz_imm);
3920 }
3921 __ Move(out_low, TMP);
3922 __ Or(TMP, TMP, out_high);
3923 if (is_r6) {
3924 __ Selnez(AT, AT, TMP);
3925 } else {
3926 __ Movz(AT, ZERO, TMP);
3927 }
3928 if (is_r2_or_newer) {
3929 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
3930 } else {
3931 __ Sll(AT, AT, ctz_imm - 32);
3932 __ Or(out_high, out_high, AT);
3933 }
3934 } else {
3935 if (is_r6) {
3936 __ Aui(AT, in_high, 0x8000);
3937 } else {
3938 __ Lui(AT, 0x8000);
3939 __ Xor(AT, AT, in_high);
3940 }
3941 __ Or(AT, AT, in_low);
3942 __ Sltiu(AT, AT, 1);
3943 __ Sll(AT, AT, 31);
3944 __ Move(out_low, in_low);
3945 __ Xor(out_high, in_high, AT);
3946 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003947 }
3948 }
3949}
3950
3951void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3952 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003953 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003954
3955 LocationSummary* locations = instruction->GetLocations();
3956 Location second = locations->InAt(1);
3957 DCHECK(second.IsConstant());
3958
3959 Register out = locations->Out().AsRegister<Register>();
3960 Register dividend = locations->InAt(0).AsRegister<Register>();
3961 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3962
3963 int64_t magic;
3964 int shift;
3965 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3966
3967 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3968
3969 __ LoadConst32(TMP, magic);
3970 if (isR6) {
3971 __ MuhR6(TMP, dividend, TMP);
3972 } else {
3973 __ MultR2(dividend, TMP);
3974 __ Mfhi(TMP);
3975 }
3976 if (imm > 0 && magic < 0) {
3977 __ Addu(TMP, TMP, dividend);
3978 } else if (imm < 0 && magic > 0) {
3979 __ Subu(TMP, TMP, dividend);
3980 }
3981
3982 if (shift != 0) {
3983 __ Sra(TMP, TMP, shift);
3984 }
3985
3986 if (instruction->IsDiv()) {
3987 __ Sra(out, TMP, 31);
3988 __ Subu(out, TMP, out);
3989 } else {
3990 __ Sra(AT, TMP, 31);
3991 __ Subu(AT, TMP, AT);
3992 __ LoadConst32(TMP, imm);
3993 if (isR6) {
3994 __ MulR6(TMP, AT, TMP);
3995 } else {
3996 __ MulR2(TMP, AT, TMP);
3997 }
3998 __ Subu(out, dividend, TMP);
3999 }
4000}
4001
4002void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4003 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004004 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004005
4006 LocationSummary* locations = instruction->GetLocations();
4007 Register out = locations->Out().AsRegister<Register>();
4008 Location second = locations->InAt(1);
4009
4010 if (second.IsConstant()) {
4011 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4012 if (imm == 0) {
4013 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4014 } else if (imm == 1 || imm == -1) {
4015 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004016 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004017 DivRemByPowerOfTwo(instruction);
4018 } else {
4019 DCHECK(imm <= -2 || imm >= 2);
4020 GenerateDivRemWithAnyConstant(instruction);
4021 }
4022 } else {
4023 Register dividend = locations->InAt(0).AsRegister<Register>();
4024 Register divisor = second.AsRegister<Register>();
4025 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4026 if (instruction->IsDiv()) {
4027 if (isR6) {
4028 __ DivR6(out, dividend, divisor);
4029 } else {
4030 __ DivR2(out, dividend, divisor);
4031 }
4032 } else {
4033 if (isR6) {
4034 __ ModR6(out, dividend, divisor);
4035 } else {
4036 __ ModR2(out, dividend, divisor);
4037 }
4038 }
4039 }
4040}
4041
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004042void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004043 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004044 bool call_long_div = false;
4045 if (type == DataType::Type::kInt64) {
4046 if (div->InputAt(1)->IsConstant()) {
4047 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4048 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4049 } else {
4050 call_long_div = true;
4051 }
4052 }
4053 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004054 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004055 : LocationSummary::kNoCall;
4056
Vladimir Markoca6fff82017-10-03 14:49:14 +01004057 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004058
4059 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004060 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004061 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004062 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4064 break;
4065
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004067 if (call_long_div) {
4068 InvokeRuntimeCallingConvention calling_convention;
4069 locations->SetInAt(0, Location::RegisterPairLocation(
4070 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4071 locations->SetInAt(1, Location::RegisterPairLocation(
4072 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4073 locations->SetOut(calling_convention.GetReturnLocation(type));
4074 } else {
4075 locations->SetInAt(0, Location::RequiresRegister());
4076 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4077 locations->SetOut(Location::RequiresRegister());
4078 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004079 break;
4080 }
4081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004082 case DataType::Type::kFloat32:
4083 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004084 locations->SetInAt(0, Location::RequiresFpuRegister());
4085 locations->SetInAt(1, Location::RequiresFpuRegister());
4086 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4087 break;
4088
4089 default:
4090 LOG(FATAL) << "Unexpected div type " << type;
4091 }
4092}
4093
4094void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004095 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004096 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004097
4098 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004099 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004100 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004101 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004102 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004103 if (locations->InAt(1).IsConstant()) {
4104 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4105 if (imm == 0) {
4106 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4107 } else if (imm == 1 || imm == -1) {
4108 DivRemOneOrMinusOne(instruction);
4109 } else {
4110 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4111 DivRemByPowerOfTwo(instruction);
4112 }
4113 } else {
4114 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4115 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4116 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004117 break;
4118 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004119 case DataType::Type::kFloat32:
4120 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004121 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4122 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4123 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004124 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004125 __ DivS(dst, lhs, rhs);
4126 } else {
4127 __ DivD(dst, lhs, rhs);
4128 }
4129 break;
4130 }
4131 default:
4132 LOG(FATAL) << "Unexpected div type " << type;
4133 }
4134}
4135
4136void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004137 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004138 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004139}
4140
4141void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004142 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004143 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004144 codegen_->AddSlowPath(slow_path);
4145 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004146 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004147
4148 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004150 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004151 case DataType::Type::kInt8:
4152 case DataType::Type::kUint16:
4153 case DataType::Type::kInt16:
4154 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004155 if (value.IsConstant()) {
4156 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4157 __ B(slow_path->GetEntryLabel());
4158 } else {
4159 // A division by a non-null constant is valid. We don't need to perform
4160 // any check, so simply fall through.
4161 }
4162 } else {
4163 DCHECK(value.IsRegister()) << value;
4164 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4165 }
4166 break;
4167 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004168 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004169 if (value.IsConstant()) {
4170 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4171 __ B(slow_path->GetEntryLabel());
4172 } else {
4173 // A division by a non-null constant is valid. We don't need to perform
4174 // any check, so simply fall through.
4175 }
4176 } else {
4177 DCHECK(value.IsRegisterPair()) << value;
4178 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4179 __ Beqz(TMP, slow_path->GetEntryLabel());
4180 }
4181 break;
4182 }
4183 default:
4184 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4185 }
4186}
4187
4188void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4189 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004190 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004191 locations->SetOut(Location::ConstantLocation(constant));
4192}
4193
4194void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4195 // Will be generated at use site.
4196}
4197
4198void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4199 exit->SetLocations(nullptr);
4200}
4201
4202void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4203}
4204
4205void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4206 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004207 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004208 locations->SetOut(Location::ConstantLocation(constant));
4209}
4210
4211void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4212 // Will be generated at use site.
4213}
4214
4215void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4216 got->SetLocations(nullptr);
4217}
4218
4219void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004220 if (successor->IsExitBlock()) {
4221 DCHECK(got->GetPrevious()->AlwaysThrows());
4222 return; // no code needed
4223 }
4224
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004225 HBasicBlock* block = got->GetBlock();
4226 HInstruction* previous = got->GetPrevious();
4227 HLoopInformation* info = block->GetLoopInformation();
4228
4229 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004230 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4231 __ Lw(AT, SP, kCurrentMethodStackOffset);
4232 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4233 __ Addiu(TMP, TMP, 1);
4234 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4235 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004236 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4237 return;
4238 }
4239 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4240 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4241 }
4242 if (!codegen_->GoesToNextBlock(block, successor)) {
4243 __ B(codegen_->GetLabelOf(successor));
4244 }
4245}
4246
4247void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4248 HandleGoto(got, got->GetSuccessor());
4249}
4250
4251void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4252 try_boundary->SetLocations(nullptr);
4253}
4254
4255void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4256 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4257 if (!successor->IsExitBlock()) {
4258 HandleGoto(try_boundary, successor);
4259 }
4260}
4261
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004262void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4263 LocationSummary* locations) {
4264 Register dst = locations->Out().AsRegister<Register>();
4265 Register lhs = locations->InAt(0).AsRegister<Register>();
4266 Location rhs_location = locations->InAt(1);
4267 Register rhs_reg = ZERO;
4268 int64_t rhs_imm = 0;
4269 bool use_imm = rhs_location.IsConstant();
4270 if (use_imm) {
4271 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4272 } else {
4273 rhs_reg = rhs_location.AsRegister<Register>();
4274 }
4275
4276 switch (cond) {
4277 case kCondEQ:
4278 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004279 if (use_imm && IsInt<16>(-rhs_imm)) {
4280 if (rhs_imm == 0) {
4281 if (cond == kCondEQ) {
4282 __ Sltiu(dst, lhs, 1);
4283 } else {
4284 __ Sltu(dst, ZERO, lhs);
4285 }
4286 } else {
4287 __ Addiu(dst, lhs, -rhs_imm);
4288 if (cond == kCondEQ) {
4289 __ Sltiu(dst, dst, 1);
4290 } else {
4291 __ Sltu(dst, ZERO, dst);
4292 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004293 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004294 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004295 if (use_imm && IsUint<16>(rhs_imm)) {
4296 __ Xori(dst, lhs, rhs_imm);
4297 } else {
4298 if (use_imm) {
4299 rhs_reg = TMP;
4300 __ LoadConst32(rhs_reg, rhs_imm);
4301 }
4302 __ Xor(dst, lhs, rhs_reg);
4303 }
4304 if (cond == kCondEQ) {
4305 __ Sltiu(dst, dst, 1);
4306 } else {
4307 __ Sltu(dst, ZERO, dst);
4308 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004309 }
4310 break;
4311
4312 case kCondLT:
4313 case kCondGE:
4314 if (use_imm && IsInt<16>(rhs_imm)) {
4315 __ Slti(dst, lhs, rhs_imm);
4316 } else {
4317 if (use_imm) {
4318 rhs_reg = TMP;
4319 __ LoadConst32(rhs_reg, rhs_imm);
4320 }
4321 __ Slt(dst, lhs, rhs_reg);
4322 }
4323 if (cond == kCondGE) {
4324 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4325 // only the slt instruction but no sge.
4326 __ Xori(dst, dst, 1);
4327 }
4328 break;
4329
4330 case kCondLE:
4331 case kCondGT:
4332 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4333 // Simulate lhs <= rhs via lhs < rhs + 1.
4334 __ Slti(dst, lhs, rhs_imm + 1);
4335 if (cond == kCondGT) {
4336 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4337 // only the slti instruction but no sgti.
4338 __ Xori(dst, dst, 1);
4339 }
4340 } else {
4341 if (use_imm) {
4342 rhs_reg = TMP;
4343 __ LoadConst32(rhs_reg, rhs_imm);
4344 }
4345 __ Slt(dst, rhs_reg, lhs);
4346 if (cond == kCondLE) {
4347 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4348 // only the slt instruction but no sle.
4349 __ Xori(dst, dst, 1);
4350 }
4351 }
4352 break;
4353
4354 case kCondB:
4355 case kCondAE:
4356 if (use_imm && IsInt<16>(rhs_imm)) {
4357 // Sltiu sign-extends its 16-bit immediate operand before
4358 // the comparison and thus lets us compare directly with
4359 // unsigned values in the ranges [0, 0x7fff] and
4360 // [0xffff8000, 0xffffffff].
4361 __ Sltiu(dst, lhs, rhs_imm);
4362 } else {
4363 if (use_imm) {
4364 rhs_reg = TMP;
4365 __ LoadConst32(rhs_reg, rhs_imm);
4366 }
4367 __ Sltu(dst, lhs, rhs_reg);
4368 }
4369 if (cond == kCondAE) {
4370 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4371 // only the sltu instruction but no sgeu.
4372 __ Xori(dst, dst, 1);
4373 }
4374 break;
4375
4376 case kCondBE:
4377 case kCondA:
4378 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4379 // Simulate lhs <= rhs via lhs < rhs + 1.
4380 // Note that this only works if rhs + 1 does not overflow
4381 // to 0, hence the check above.
4382 // Sltiu sign-extends its 16-bit immediate operand before
4383 // the comparison and thus lets us compare directly with
4384 // unsigned values in the ranges [0, 0x7fff] and
4385 // [0xffff8000, 0xffffffff].
4386 __ Sltiu(dst, lhs, rhs_imm + 1);
4387 if (cond == kCondA) {
4388 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4389 // only the sltiu instruction but no sgtiu.
4390 __ Xori(dst, dst, 1);
4391 }
4392 } else {
4393 if (use_imm) {
4394 rhs_reg = TMP;
4395 __ LoadConst32(rhs_reg, rhs_imm);
4396 }
4397 __ Sltu(dst, rhs_reg, lhs);
4398 if (cond == kCondBE) {
4399 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4400 // only the sltu instruction but no sleu.
4401 __ Xori(dst, dst, 1);
4402 }
4403 }
4404 break;
4405 }
4406}
4407
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004408bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4409 LocationSummary* input_locations,
4410 Register dst) {
4411 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4412 Location rhs_location = input_locations->InAt(1);
4413 Register rhs_reg = ZERO;
4414 int64_t rhs_imm = 0;
4415 bool use_imm = rhs_location.IsConstant();
4416 if (use_imm) {
4417 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4418 } else {
4419 rhs_reg = rhs_location.AsRegister<Register>();
4420 }
4421
4422 switch (cond) {
4423 case kCondEQ:
4424 case kCondNE:
4425 if (use_imm && IsInt<16>(-rhs_imm)) {
4426 __ Addiu(dst, lhs, -rhs_imm);
4427 } else if (use_imm && IsUint<16>(rhs_imm)) {
4428 __ Xori(dst, lhs, rhs_imm);
4429 } else {
4430 if (use_imm) {
4431 rhs_reg = TMP;
4432 __ LoadConst32(rhs_reg, rhs_imm);
4433 }
4434 __ Xor(dst, lhs, rhs_reg);
4435 }
4436 return (cond == kCondEQ);
4437
4438 case kCondLT:
4439 case kCondGE:
4440 if (use_imm && IsInt<16>(rhs_imm)) {
4441 __ Slti(dst, lhs, rhs_imm);
4442 } else {
4443 if (use_imm) {
4444 rhs_reg = TMP;
4445 __ LoadConst32(rhs_reg, rhs_imm);
4446 }
4447 __ Slt(dst, lhs, rhs_reg);
4448 }
4449 return (cond == kCondGE);
4450
4451 case kCondLE:
4452 case kCondGT:
4453 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4454 // Simulate lhs <= rhs via lhs < rhs + 1.
4455 __ Slti(dst, lhs, rhs_imm + 1);
4456 return (cond == kCondGT);
4457 } else {
4458 if (use_imm) {
4459 rhs_reg = TMP;
4460 __ LoadConst32(rhs_reg, rhs_imm);
4461 }
4462 __ Slt(dst, rhs_reg, lhs);
4463 return (cond == kCondLE);
4464 }
4465
4466 case kCondB:
4467 case kCondAE:
4468 if (use_imm && IsInt<16>(rhs_imm)) {
4469 // Sltiu sign-extends its 16-bit immediate operand before
4470 // the comparison and thus lets us compare directly with
4471 // unsigned values in the ranges [0, 0x7fff] and
4472 // [0xffff8000, 0xffffffff].
4473 __ Sltiu(dst, lhs, rhs_imm);
4474 } else {
4475 if (use_imm) {
4476 rhs_reg = TMP;
4477 __ LoadConst32(rhs_reg, rhs_imm);
4478 }
4479 __ Sltu(dst, lhs, rhs_reg);
4480 }
4481 return (cond == kCondAE);
4482
4483 case kCondBE:
4484 case kCondA:
4485 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4486 // Simulate lhs <= rhs via lhs < rhs + 1.
4487 // Note that this only works if rhs + 1 does not overflow
4488 // to 0, hence the check above.
4489 // Sltiu sign-extends its 16-bit immediate operand before
4490 // the comparison and thus lets us compare directly with
4491 // unsigned values in the ranges [0, 0x7fff] and
4492 // [0xffff8000, 0xffffffff].
4493 __ Sltiu(dst, lhs, rhs_imm + 1);
4494 return (cond == kCondA);
4495 } else {
4496 if (use_imm) {
4497 rhs_reg = TMP;
4498 __ LoadConst32(rhs_reg, rhs_imm);
4499 }
4500 __ Sltu(dst, rhs_reg, lhs);
4501 return (cond == kCondBE);
4502 }
4503 }
4504}
4505
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004506void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4507 LocationSummary* locations,
4508 MipsLabel* label) {
4509 Register lhs = locations->InAt(0).AsRegister<Register>();
4510 Location rhs_location = locations->InAt(1);
4511 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004512 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004513 bool use_imm = rhs_location.IsConstant();
4514 if (use_imm) {
4515 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4516 } else {
4517 rhs_reg = rhs_location.AsRegister<Register>();
4518 }
4519
4520 if (use_imm && rhs_imm == 0) {
4521 switch (cond) {
4522 case kCondEQ:
4523 case kCondBE: // <= 0 if zero
4524 __ Beqz(lhs, label);
4525 break;
4526 case kCondNE:
4527 case kCondA: // > 0 if non-zero
4528 __ Bnez(lhs, label);
4529 break;
4530 case kCondLT:
4531 __ Bltz(lhs, label);
4532 break;
4533 case kCondGE:
4534 __ Bgez(lhs, label);
4535 break;
4536 case kCondLE:
4537 __ Blez(lhs, label);
4538 break;
4539 case kCondGT:
4540 __ Bgtz(lhs, label);
4541 break;
4542 case kCondB: // always false
4543 break;
4544 case kCondAE: // always true
4545 __ B(label);
4546 break;
4547 }
4548 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004549 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4550 if (isR6 || !use_imm) {
4551 if (use_imm) {
4552 rhs_reg = TMP;
4553 __ LoadConst32(rhs_reg, rhs_imm);
4554 }
4555 switch (cond) {
4556 case kCondEQ:
4557 __ Beq(lhs, rhs_reg, label);
4558 break;
4559 case kCondNE:
4560 __ Bne(lhs, rhs_reg, label);
4561 break;
4562 case kCondLT:
4563 __ Blt(lhs, rhs_reg, label);
4564 break;
4565 case kCondGE:
4566 __ Bge(lhs, rhs_reg, label);
4567 break;
4568 case kCondLE:
4569 __ Bge(rhs_reg, lhs, label);
4570 break;
4571 case kCondGT:
4572 __ Blt(rhs_reg, lhs, label);
4573 break;
4574 case kCondB:
4575 __ Bltu(lhs, rhs_reg, label);
4576 break;
4577 case kCondAE:
4578 __ Bgeu(lhs, rhs_reg, label);
4579 break;
4580 case kCondBE:
4581 __ Bgeu(rhs_reg, lhs, label);
4582 break;
4583 case kCondA:
4584 __ Bltu(rhs_reg, lhs, label);
4585 break;
4586 }
4587 } else {
4588 // Special cases for more efficient comparison with constants on R2.
4589 switch (cond) {
4590 case kCondEQ:
4591 __ LoadConst32(TMP, rhs_imm);
4592 __ Beq(lhs, TMP, label);
4593 break;
4594 case kCondNE:
4595 __ LoadConst32(TMP, rhs_imm);
4596 __ Bne(lhs, TMP, label);
4597 break;
4598 case kCondLT:
4599 if (IsInt<16>(rhs_imm)) {
4600 __ Slti(TMP, lhs, rhs_imm);
4601 __ Bnez(TMP, label);
4602 } else {
4603 __ LoadConst32(TMP, rhs_imm);
4604 __ Blt(lhs, TMP, label);
4605 }
4606 break;
4607 case kCondGE:
4608 if (IsInt<16>(rhs_imm)) {
4609 __ Slti(TMP, lhs, rhs_imm);
4610 __ Beqz(TMP, label);
4611 } else {
4612 __ LoadConst32(TMP, rhs_imm);
4613 __ Bge(lhs, TMP, label);
4614 }
4615 break;
4616 case kCondLE:
4617 if (IsInt<16>(rhs_imm + 1)) {
4618 // Simulate lhs <= rhs via lhs < rhs + 1.
4619 __ Slti(TMP, lhs, rhs_imm + 1);
4620 __ Bnez(TMP, label);
4621 } else {
4622 __ LoadConst32(TMP, rhs_imm);
4623 __ Bge(TMP, lhs, label);
4624 }
4625 break;
4626 case kCondGT:
4627 if (IsInt<16>(rhs_imm + 1)) {
4628 // Simulate lhs > rhs via !(lhs < rhs + 1).
4629 __ Slti(TMP, lhs, rhs_imm + 1);
4630 __ Beqz(TMP, label);
4631 } else {
4632 __ LoadConst32(TMP, rhs_imm);
4633 __ Blt(TMP, lhs, label);
4634 }
4635 break;
4636 case kCondB:
4637 if (IsInt<16>(rhs_imm)) {
4638 __ Sltiu(TMP, lhs, rhs_imm);
4639 __ Bnez(TMP, label);
4640 } else {
4641 __ LoadConst32(TMP, rhs_imm);
4642 __ Bltu(lhs, TMP, label);
4643 }
4644 break;
4645 case kCondAE:
4646 if (IsInt<16>(rhs_imm)) {
4647 __ Sltiu(TMP, lhs, rhs_imm);
4648 __ Beqz(TMP, label);
4649 } else {
4650 __ LoadConst32(TMP, rhs_imm);
4651 __ Bgeu(lhs, TMP, label);
4652 }
4653 break;
4654 case kCondBE:
4655 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4656 // Simulate lhs <= rhs via lhs < rhs + 1.
4657 // Note that this only works if rhs + 1 does not overflow
4658 // to 0, hence the check above.
4659 __ Sltiu(TMP, lhs, rhs_imm + 1);
4660 __ Bnez(TMP, label);
4661 } else {
4662 __ LoadConst32(TMP, rhs_imm);
4663 __ Bgeu(TMP, lhs, label);
4664 }
4665 break;
4666 case kCondA:
4667 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4668 // Simulate lhs > rhs via !(lhs < rhs + 1).
4669 // Note that this only works if rhs + 1 does not overflow
4670 // to 0, hence the check above.
4671 __ Sltiu(TMP, lhs, rhs_imm + 1);
4672 __ Beqz(TMP, label);
4673 } else {
4674 __ LoadConst32(TMP, rhs_imm);
4675 __ Bltu(TMP, lhs, label);
4676 }
4677 break;
4678 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004679 }
4680 }
4681}
4682
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004683void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4684 LocationSummary* locations) {
4685 Register dst = locations->Out().AsRegister<Register>();
4686 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4687 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4688 Location rhs_location = locations->InAt(1);
4689 Register rhs_high = ZERO;
4690 Register rhs_low = ZERO;
4691 int64_t imm = 0;
4692 uint32_t imm_high = 0;
4693 uint32_t imm_low = 0;
4694 bool use_imm = rhs_location.IsConstant();
4695 if (use_imm) {
4696 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4697 imm_high = High32Bits(imm);
4698 imm_low = Low32Bits(imm);
4699 } else {
4700 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4701 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4702 }
4703 if (use_imm && imm == 0) {
4704 switch (cond) {
4705 case kCondEQ:
4706 case kCondBE: // <= 0 if zero
4707 __ Or(dst, lhs_high, lhs_low);
4708 __ Sltiu(dst, dst, 1);
4709 break;
4710 case kCondNE:
4711 case kCondA: // > 0 if non-zero
4712 __ Or(dst, lhs_high, lhs_low);
4713 __ Sltu(dst, ZERO, dst);
4714 break;
4715 case kCondLT:
4716 __ Slt(dst, lhs_high, ZERO);
4717 break;
4718 case kCondGE:
4719 __ Slt(dst, lhs_high, ZERO);
4720 __ Xori(dst, dst, 1);
4721 break;
4722 case kCondLE:
4723 __ Or(TMP, lhs_high, lhs_low);
4724 __ Sra(AT, lhs_high, 31);
4725 __ Sltu(dst, AT, TMP);
4726 __ Xori(dst, dst, 1);
4727 break;
4728 case kCondGT:
4729 __ Or(TMP, lhs_high, lhs_low);
4730 __ Sra(AT, lhs_high, 31);
4731 __ Sltu(dst, AT, TMP);
4732 break;
4733 case kCondB: // always false
4734 __ Andi(dst, dst, 0);
4735 break;
4736 case kCondAE: // always true
4737 __ Ori(dst, ZERO, 1);
4738 break;
4739 }
4740 } else if (use_imm) {
4741 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4742 switch (cond) {
4743 case kCondEQ:
4744 __ LoadConst32(TMP, imm_high);
4745 __ Xor(TMP, TMP, lhs_high);
4746 __ LoadConst32(AT, imm_low);
4747 __ Xor(AT, AT, lhs_low);
4748 __ Or(dst, TMP, AT);
4749 __ Sltiu(dst, dst, 1);
4750 break;
4751 case kCondNE:
4752 __ LoadConst32(TMP, imm_high);
4753 __ Xor(TMP, TMP, lhs_high);
4754 __ LoadConst32(AT, imm_low);
4755 __ Xor(AT, AT, lhs_low);
4756 __ Or(dst, TMP, AT);
4757 __ Sltu(dst, ZERO, dst);
4758 break;
4759 case kCondLT:
4760 case kCondGE:
4761 if (dst == lhs_low) {
4762 __ LoadConst32(TMP, imm_low);
4763 __ Sltu(dst, lhs_low, TMP);
4764 }
4765 __ LoadConst32(TMP, imm_high);
4766 __ Slt(AT, lhs_high, TMP);
4767 __ Slt(TMP, TMP, lhs_high);
4768 if (dst != lhs_low) {
4769 __ LoadConst32(dst, imm_low);
4770 __ Sltu(dst, lhs_low, dst);
4771 }
4772 __ Slt(dst, TMP, dst);
4773 __ Or(dst, dst, AT);
4774 if (cond == kCondGE) {
4775 __ Xori(dst, dst, 1);
4776 }
4777 break;
4778 case kCondGT:
4779 case kCondLE:
4780 if (dst == lhs_low) {
4781 __ LoadConst32(TMP, imm_low);
4782 __ Sltu(dst, TMP, lhs_low);
4783 }
4784 __ LoadConst32(TMP, imm_high);
4785 __ Slt(AT, TMP, lhs_high);
4786 __ Slt(TMP, lhs_high, TMP);
4787 if (dst != lhs_low) {
4788 __ LoadConst32(dst, imm_low);
4789 __ Sltu(dst, dst, lhs_low);
4790 }
4791 __ Slt(dst, TMP, dst);
4792 __ Or(dst, dst, AT);
4793 if (cond == kCondLE) {
4794 __ Xori(dst, dst, 1);
4795 }
4796 break;
4797 case kCondB:
4798 case kCondAE:
4799 if (dst == lhs_low) {
4800 __ LoadConst32(TMP, imm_low);
4801 __ Sltu(dst, lhs_low, TMP);
4802 }
4803 __ LoadConst32(TMP, imm_high);
4804 __ Sltu(AT, lhs_high, TMP);
4805 __ Sltu(TMP, TMP, lhs_high);
4806 if (dst != lhs_low) {
4807 __ LoadConst32(dst, imm_low);
4808 __ Sltu(dst, lhs_low, dst);
4809 }
4810 __ Slt(dst, TMP, dst);
4811 __ Or(dst, dst, AT);
4812 if (cond == kCondAE) {
4813 __ Xori(dst, dst, 1);
4814 }
4815 break;
4816 case kCondA:
4817 case kCondBE:
4818 if (dst == lhs_low) {
4819 __ LoadConst32(TMP, imm_low);
4820 __ Sltu(dst, TMP, lhs_low);
4821 }
4822 __ LoadConst32(TMP, imm_high);
4823 __ Sltu(AT, TMP, lhs_high);
4824 __ Sltu(TMP, lhs_high, TMP);
4825 if (dst != lhs_low) {
4826 __ LoadConst32(dst, imm_low);
4827 __ Sltu(dst, dst, lhs_low);
4828 }
4829 __ Slt(dst, TMP, dst);
4830 __ Or(dst, dst, AT);
4831 if (cond == kCondBE) {
4832 __ Xori(dst, dst, 1);
4833 }
4834 break;
4835 }
4836 } else {
4837 switch (cond) {
4838 case kCondEQ:
4839 __ Xor(TMP, lhs_high, rhs_high);
4840 __ Xor(AT, lhs_low, rhs_low);
4841 __ Or(dst, TMP, AT);
4842 __ Sltiu(dst, dst, 1);
4843 break;
4844 case kCondNE:
4845 __ Xor(TMP, lhs_high, rhs_high);
4846 __ Xor(AT, lhs_low, rhs_low);
4847 __ Or(dst, TMP, AT);
4848 __ Sltu(dst, ZERO, dst);
4849 break;
4850 case kCondLT:
4851 case kCondGE:
4852 __ Slt(TMP, rhs_high, lhs_high);
4853 __ Sltu(AT, lhs_low, rhs_low);
4854 __ Slt(TMP, TMP, AT);
4855 __ Slt(AT, lhs_high, rhs_high);
4856 __ Or(dst, AT, TMP);
4857 if (cond == kCondGE) {
4858 __ Xori(dst, dst, 1);
4859 }
4860 break;
4861 case kCondGT:
4862 case kCondLE:
4863 __ Slt(TMP, lhs_high, rhs_high);
4864 __ Sltu(AT, rhs_low, lhs_low);
4865 __ Slt(TMP, TMP, AT);
4866 __ Slt(AT, rhs_high, lhs_high);
4867 __ Or(dst, AT, TMP);
4868 if (cond == kCondLE) {
4869 __ Xori(dst, dst, 1);
4870 }
4871 break;
4872 case kCondB:
4873 case kCondAE:
4874 __ Sltu(TMP, rhs_high, lhs_high);
4875 __ Sltu(AT, lhs_low, rhs_low);
4876 __ Slt(TMP, TMP, AT);
4877 __ Sltu(AT, lhs_high, rhs_high);
4878 __ Or(dst, AT, TMP);
4879 if (cond == kCondAE) {
4880 __ Xori(dst, dst, 1);
4881 }
4882 break;
4883 case kCondA:
4884 case kCondBE:
4885 __ Sltu(TMP, lhs_high, rhs_high);
4886 __ Sltu(AT, rhs_low, lhs_low);
4887 __ Slt(TMP, TMP, AT);
4888 __ Sltu(AT, rhs_high, lhs_high);
4889 __ Or(dst, AT, TMP);
4890 if (cond == kCondBE) {
4891 __ Xori(dst, dst, 1);
4892 }
4893 break;
4894 }
4895 }
4896}
4897
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004898void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4899 LocationSummary* locations,
4900 MipsLabel* label) {
4901 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4902 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4903 Location rhs_location = locations->InAt(1);
4904 Register rhs_high = ZERO;
4905 Register rhs_low = ZERO;
4906 int64_t imm = 0;
4907 uint32_t imm_high = 0;
4908 uint32_t imm_low = 0;
4909 bool use_imm = rhs_location.IsConstant();
4910 if (use_imm) {
4911 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4912 imm_high = High32Bits(imm);
4913 imm_low = Low32Bits(imm);
4914 } else {
4915 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4916 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4917 }
4918
4919 if (use_imm && imm == 0) {
4920 switch (cond) {
4921 case kCondEQ:
4922 case kCondBE: // <= 0 if zero
4923 __ Or(TMP, lhs_high, lhs_low);
4924 __ Beqz(TMP, label);
4925 break;
4926 case kCondNE:
4927 case kCondA: // > 0 if non-zero
4928 __ Or(TMP, lhs_high, lhs_low);
4929 __ Bnez(TMP, label);
4930 break;
4931 case kCondLT:
4932 __ Bltz(lhs_high, label);
4933 break;
4934 case kCondGE:
4935 __ Bgez(lhs_high, label);
4936 break;
4937 case kCondLE:
4938 __ Or(TMP, lhs_high, lhs_low);
4939 __ Sra(AT, lhs_high, 31);
4940 __ Bgeu(AT, TMP, label);
4941 break;
4942 case kCondGT:
4943 __ Or(TMP, lhs_high, lhs_low);
4944 __ Sra(AT, lhs_high, 31);
4945 __ Bltu(AT, TMP, label);
4946 break;
4947 case kCondB: // always false
4948 break;
4949 case kCondAE: // always true
4950 __ B(label);
4951 break;
4952 }
4953 } else if (use_imm) {
4954 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4955 switch (cond) {
4956 case kCondEQ:
4957 __ LoadConst32(TMP, imm_high);
4958 __ Xor(TMP, TMP, lhs_high);
4959 __ LoadConst32(AT, imm_low);
4960 __ Xor(AT, AT, lhs_low);
4961 __ Or(TMP, TMP, AT);
4962 __ Beqz(TMP, label);
4963 break;
4964 case kCondNE:
4965 __ LoadConst32(TMP, imm_high);
4966 __ Xor(TMP, TMP, lhs_high);
4967 __ LoadConst32(AT, imm_low);
4968 __ Xor(AT, AT, lhs_low);
4969 __ Or(TMP, TMP, AT);
4970 __ Bnez(TMP, label);
4971 break;
4972 case kCondLT:
4973 __ LoadConst32(TMP, imm_high);
4974 __ Blt(lhs_high, TMP, label);
4975 __ Slt(TMP, TMP, lhs_high);
4976 __ LoadConst32(AT, imm_low);
4977 __ Sltu(AT, lhs_low, AT);
4978 __ Blt(TMP, AT, label);
4979 break;
4980 case kCondGE:
4981 __ LoadConst32(TMP, imm_high);
4982 __ Blt(TMP, lhs_high, label);
4983 __ Slt(TMP, lhs_high, TMP);
4984 __ LoadConst32(AT, imm_low);
4985 __ Sltu(AT, lhs_low, AT);
4986 __ Or(TMP, TMP, AT);
4987 __ Beqz(TMP, label);
4988 break;
4989 case kCondLE:
4990 __ LoadConst32(TMP, imm_high);
4991 __ Blt(lhs_high, TMP, label);
4992 __ Slt(TMP, TMP, lhs_high);
4993 __ LoadConst32(AT, imm_low);
4994 __ Sltu(AT, AT, lhs_low);
4995 __ Or(TMP, TMP, AT);
4996 __ Beqz(TMP, label);
4997 break;
4998 case kCondGT:
4999 __ LoadConst32(TMP, imm_high);
5000 __ Blt(TMP, lhs_high, label);
5001 __ Slt(TMP, lhs_high, TMP);
5002 __ LoadConst32(AT, imm_low);
5003 __ Sltu(AT, AT, lhs_low);
5004 __ Blt(TMP, AT, label);
5005 break;
5006 case kCondB:
5007 __ LoadConst32(TMP, imm_high);
5008 __ Bltu(lhs_high, TMP, label);
5009 __ Sltu(TMP, TMP, lhs_high);
5010 __ LoadConst32(AT, imm_low);
5011 __ Sltu(AT, lhs_low, AT);
5012 __ Blt(TMP, AT, label);
5013 break;
5014 case kCondAE:
5015 __ LoadConst32(TMP, imm_high);
5016 __ Bltu(TMP, lhs_high, label);
5017 __ Sltu(TMP, lhs_high, TMP);
5018 __ LoadConst32(AT, imm_low);
5019 __ Sltu(AT, lhs_low, AT);
5020 __ Or(TMP, TMP, AT);
5021 __ Beqz(TMP, label);
5022 break;
5023 case kCondBE:
5024 __ LoadConst32(TMP, imm_high);
5025 __ Bltu(lhs_high, TMP, label);
5026 __ Sltu(TMP, TMP, lhs_high);
5027 __ LoadConst32(AT, imm_low);
5028 __ Sltu(AT, AT, lhs_low);
5029 __ Or(TMP, TMP, AT);
5030 __ Beqz(TMP, label);
5031 break;
5032 case kCondA:
5033 __ LoadConst32(TMP, imm_high);
5034 __ Bltu(TMP, lhs_high, label);
5035 __ Sltu(TMP, lhs_high, TMP);
5036 __ LoadConst32(AT, imm_low);
5037 __ Sltu(AT, AT, lhs_low);
5038 __ Blt(TMP, AT, label);
5039 break;
5040 }
5041 } else {
5042 switch (cond) {
5043 case kCondEQ:
5044 __ Xor(TMP, lhs_high, rhs_high);
5045 __ Xor(AT, lhs_low, rhs_low);
5046 __ Or(TMP, TMP, AT);
5047 __ Beqz(TMP, label);
5048 break;
5049 case kCondNE:
5050 __ Xor(TMP, lhs_high, rhs_high);
5051 __ Xor(AT, lhs_low, rhs_low);
5052 __ Or(TMP, TMP, AT);
5053 __ Bnez(TMP, label);
5054 break;
5055 case kCondLT:
5056 __ Blt(lhs_high, rhs_high, label);
5057 __ Slt(TMP, rhs_high, lhs_high);
5058 __ Sltu(AT, lhs_low, rhs_low);
5059 __ Blt(TMP, AT, label);
5060 break;
5061 case kCondGE:
5062 __ Blt(rhs_high, lhs_high, label);
5063 __ Slt(TMP, lhs_high, rhs_high);
5064 __ Sltu(AT, lhs_low, rhs_low);
5065 __ Or(TMP, TMP, AT);
5066 __ Beqz(TMP, label);
5067 break;
5068 case kCondLE:
5069 __ Blt(lhs_high, rhs_high, label);
5070 __ Slt(TMP, rhs_high, lhs_high);
5071 __ Sltu(AT, rhs_low, lhs_low);
5072 __ Or(TMP, TMP, AT);
5073 __ Beqz(TMP, label);
5074 break;
5075 case kCondGT:
5076 __ Blt(rhs_high, lhs_high, label);
5077 __ Slt(TMP, lhs_high, rhs_high);
5078 __ Sltu(AT, rhs_low, lhs_low);
5079 __ Blt(TMP, AT, label);
5080 break;
5081 case kCondB:
5082 __ Bltu(lhs_high, rhs_high, label);
5083 __ Sltu(TMP, rhs_high, lhs_high);
5084 __ Sltu(AT, lhs_low, rhs_low);
5085 __ Blt(TMP, AT, label);
5086 break;
5087 case kCondAE:
5088 __ Bltu(rhs_high, lhs_high, label);
5089 __ Sltu(TMP, lhs_high, rhs_high);
5090 __ Sltu(AT, lhs_low, rhs_low);
5091 __ Or(TMP, TMP, AT);
5092 __ Beqz(TMP, label);
5093 break;
5094 case kCondBE:
5095 __ Bltu(lhs_high, rhs_high, label);
5096 __ Sltu(TMP, rhs_high, lhs_high);
5097 __ Sltu(AT, rhs_low, lhs_low);
5098 __ Or(TMP, TMP, AT);
5099 __ Beqz(TMP, label);
5100 break;
5101 case kCondA:
5102 __ Bltu(rhs_high, lhs_high, label);
5103 __ Sltu(TMP, lhs_high, rhs_high);
5104 __ Sltu(AT, rhs_low, lhs_low);
5105 __ Blt(TMP, AT, label);
5106 break;
5107 }
5108 }
5109}
5110
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005111void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5112 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005113 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005114 LocationSummary* locations) {
5115 Register dst = locations->Out().AsRegister<Register>();
5116 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5117 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5118 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005119 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005120 if (isR6) {
5121 switch (cond) {
5122 case kCondEQ:
5123 __ CmpEqS(FTMP, lhs, rhs);
5124 __ Mfc1(dst, FTMP);
5125 __ Andi(dst, dst, 1);
5126 break;
5127 case kCondNE:
5128 __ CmpEqS(FTMP, lhs, rhs);
5129 __ Mfc1(dst, FTMP);
5130 __ Addiu(dst, dst, 1);
5131 break;
5132 case kCondLT:
5133 if (gt_bias) {
5134 __ CmpLtS(FTMP, lhs, rhs);
5135 } else {
5136 __ CmpUltS(FTMP, lhs, rhs);
5137 }
5138 __ Mfc1(dst, FTMP);
5139 __ Andi(dst, dst, 1);
5140 break;
5141 case kCondLE:
5142 if (gt_bias) {
5143 __ CmpLeS(FTMP, lhs, rhs);
5144 } else {
5145 __ CmpUleS(FTMP, lhs, rhs);
5146 }
5147 __ Mfc1(dst, FTMP);
5148 __ Andi(dst, dst, 1);
5149 break;
5150 case kCondGT:
5151 if (gt_bias) {
5152 __ CmpUltS(FTMP, rhs, lhs);
5153 } else {
5154 __ CmpLtS(FTMP, rhs, lhs);
5155 }
5156 __ Mfc1(dst, FTMP);
5157 __ Andi(dst, dst, 1);
5158 break;
5159 case kCondGE:
5160 if (gt_bias) {
5161 __ CmpUleS(FTMP, rhs, lhs);
5162 } else {
5163 __ CmpLeS(FTMP, rhs, lhs);
5164 }
5165 __ Mfc1(dst, FTMP);
5166 __ Andi(dst, dst, 1);
5167 break;
5168 default:
5169 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5170 UNREACHABLE();
5171 }
5172 } else {
5173 switch (cond) {
5174 case kCondEQ:
5175 __ CeqS(0, lhs, rhs);
5176 __ LoadConst32(dst, 1);
5177 __ Movf(dst, ZERO, 0);
5178 break;
5179 case kCondNE:
5180 __ CeqS(0, lhs, rhs);
5181 __ LoadConst32(dst, 1);
5182 __ Movt(dst, ZERO, 0);
5183 break;
5184 case kCondLT:
5185 if (gt_bias) {
5186 __ ColtS(0, lhs, rhs);
5187 } else {
5188 __ CultS(0, lhs, rhs);
5189 }
5190 __ LoadConst32(dst, 1);
5191 __ Movf(dst, ZERO, 0);
5192 break;
5193 case kCondLE:
5194 if (gt_bias) {
5195 __ ColeS(0, lhs, rhs);
5196 } else {
5197 __ CuleS(0, lhs, rhs);
5198 }
5199 __ LoadConst32(dst, 1);
5200 __ Movf(dst, ZERO, 0);
5201 break;
5202 case kCondGT:
5203 if (gt_bias) {
5204 __ CultS(0, rhs, lhs);
5205 } else {
5206 __ ColtS(0, rhs, lhs);
5207 }
5208 __ LoadConst32(dst, 1);
5209 __ Movf(dst, ZERO, 0);
5210 break;
5211 case kCondGE:
5212 if (gt_bias) {
5213 __ CuleS(0, rhs, lhs);
5214 } else {
5215 __ ColeS(0, rhs, lhs);
5216 }
5217 __ LoadConst32(dst, 1);
5218 __ Movf(dst, ZERO, 0);
5219 break;
5220 default:
5221 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5222 UNREACHABLE();
5223 }
5224 }
5225 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005226 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005227 if (isR6) {
5228 switch (cond) {
5229 case kCondEQ:
5230 __ CmpEqD(FTMP, lhs, rhs);
5231 __ Mfc1(dst, FTMP);
5232 __ Andi(dst, dst, 1);
5233 break;
5234 case kCondNE:
5235 __ CmpEqD(FTMP, lhs, rhs);
5236 __ Mfc1(dst, FTMP);
5237 __ Addiu(dst, dst, 1);
5238 break;
5239 case kCondLT:
5240 if (gt_bias) {
5241 __ CmpLtD(FTMP, lhs, rhs);
5242 } else {
5243 __ CmpUltD(FTMP, lhs, rhs);
5244 }
5245 __ Mfc1(dst, FTMP);
5246 __ Andi(dst, dst, 1);
5247 break;
5248 case kCondLE:
5249 if (gt_bias) {
5250 __ CmpLeD(FTMP, lhs, rhs);
5251 } else {
5252 __ CmpUleD(FTMP, lhs, rhs);
5253 }
5254 __ Mfc1(dst, FTMP);
5255 __ Andi(dst, dst, 1);
5256 break;
5257 case kCondGT:
5258 if (gt_bias) {
5259 __ CmpUltD(FTMP, rhs, lhs);
5260 } else {
5261 __ CmpLtD(FTMP, rhs, lhs);
5262 }
5263 __ Mfc1(dst, FTMP);
5264 __ Andi(dst, dst, 1);
5265 break;
5266 case kCondGE:
5267 if (gt_bias) {
5268 __ CmpUleD(FTMP, rhs, lhs);
5269 } else {
5270 __ CmpLeD(FTMP, rhs, lhs);
5271 }
5272 __ Mfc1(dst, FTMP);
5273 __ Andi(dst, dst, 1);
5274 break;
5275 default:
5276 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5277 UNREACHABLE();
5278 }
5279 } else {
5280 switch (cond) {
5281 case kCondEQ:
5282 __ CeqD(0, lhs, rhs);
5283 __ LoadConst32(dst, 1);
5284 __ Movf(dst, ZERO, 0);
5285 break;
5286 case kCondNE:
5287 __ CeqD(0, lhs, rhs);
5288 __ LoadConst32(dst, 1);
5289 __ Movt(dst, ZERO, 0);
5290 break;
5291 case kCondLT:
5292 if (gt_bias) {
5293 __ ColtD(0, lhs, rhs);
5294 } else {
5295 __ CultD(0, lhs, rhs);
5296 }
5297 __ LoadConst32(dst, 1);
5298 __ Movf(dst, ZERO, 0);
5299 break;
5300 case kCondLE:
5301 if (gt_bias) {
5302 __ ColeD(0, lhs, rhs);
5303 } else {
5304 __ CuleD(0, lhs, rhs);
5305 }
5306 __ LoadConst32(dst, 1);
5307 __ Movf(dst, ZERO, 0);
5308 break;
5309 case kCondGT:
5310 if (gt_bias) {
5311 __ CultD(0, rhs, lhs);
5312 } else {
5313 __ ColtD(0, rhs, lhs);
5314 }
5315 __ LoadConst32(dst, 1);
5316 __ Movf(dst, ZERO, 0);
5317 break;
5318 case kCondGE:
5319 if (gt_bias) {
5320 __ CuleD(0, rhs, lhs);
5321 } else {
5322 __ ColeD(0, rhs, lhs);
5323 }
5324 __ LoadConst32(dst, 1);
5325 __ Movf(dst, ZERO, 0);
5326 break;
5327 default:
5328 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5329 UNREACHABLE();
5330 }
5331 }
5332 }
5333}
5334
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005335bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5336 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005337 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005338 LocationSummary* input_locations,
5339 int cc) {
5340 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5341 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5342 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005343 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005344 switch (cond) {
5345 case kCondEQ:
5346 __ CeqS(cc, lhs, rhs);
5347 return false;
5348 case kCondNE:
5349 __ CeqS(cc, lhs, rhs);
5350 return true;
5351 case kCondLT:
5352 if (gt_bias) {
5353 __ ColtS(cc, lhs, rhs);
5354 } else {
5355 __ CultS(cc, lhs, rhs);
5356 }
5357 return false;
5358 case kCondLE:
5359 if (gt_bias) {
5360 __ ColeS(cc, lhs, rhs);
5361 } else {
5362 __ CuleS(cc, lhs, rhs);
5363 }
5364 return false;
5365 case kCondGT:
5366 if (gt_bias) {
5367 __ CultS(cc, rhs, lhs);
5368 } else {
5369 __ ColtS(cc, rhs, lhs);
5370 }
5371 return false;
5372 case kCondGE:
5373 if (gt_bias) {
5374 __ CuleS(cc, rhs, lhs);
5375 } else {
5376 __ ColeS(cc, rhs, lhs);
5377 }
5378 return false;
5379 default:
5380 LOG(FATAL) << "Unexpected non-floating-point condition";
5381 UNREACHABLE();
5382 }
5383 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005384 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005385 switch (cond) {
5386 case kCondEQ:
5387 __ CeqD(cc, lhs, rhs);
5388 return false;
5389 case kCondNE:
5390 __ CeqD(cc, lhs, rhs);
5391 return true;
5392 case kCondLT:
5393 if (gt_bias) {
5394 __ ColtD(cc, lhs, rhs);
5395 } else {
5396 __ CultD(cc, lhs, rhs);
5397 }
5398 return false;
5399 case kCondLE:
5400 if (gt_bias) {
5401 __ ColeD(cc, lhs, rhs);
5402 } else {
5403 __ CuleD(cc, lhs, rhs);
5404 }
5405 return false;
5406 case kCondGT:
5407 if (gt_bias) {
5408 __ CultD(cc, rhs, lhs);
5409 } else {
5410 __ ColtD(cc, rhs, lhs);
5411 }
5412 return false;
5413 case kCondGE:
5414 if (gt_bias) {
5415 __ CuleD(cc, rhs, lhs);
5416 } else {
5417 __ ColeD(cc, rhs, lhs);
5418 }
5419 return false;
5420 default:
5421 LOG(FATAL) << "Unexpected non-floating-point condition";
5422 UNREACHABLE();
5423 }
5424 }
5425}
5426
5427bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5428 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005429 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005430 LocationSummary* input_locations,
5431 FRegister dst) {
5432 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5433 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5434 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005435 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005436 switch (cond) {
5437 case kCondEQ:
5438 __ CmpEqS(dst, lhs, rhs);
5439 return false;
5440 case kCondNE:
5441 __ CmpEqS(dst, lhs, rhs);
5442 return true;
5443 case kCondLT:
5444 if (gt_bias) {
5445 __ CmpLtS(dst, lhs, rhs);
5446 } else {
5447 __ CmpUltS(dst, lhs, rhs);
5448 }
5449 return false;
5450 case kCondLE:
5451 if (gt_bias) {
5452 __ CmpLeS(dst, lhs, rhs);
5453 } else {
5454 __ CmpUleS(dst, lhs, rhs);
5455 }
5456 return false;
5457 case kCondGT:
5458 if (gt_bias) {
5459 __ CmpUltS(dst, rhs, lhs);
5460 } else {
5461 __ CmpLtS(dst, rhs, lhs);
5462 }
5463 return false;
5464 case kCondGE:
5465 if (gt_bias) {
5466 __ CmpUleS(dst, rhs, lhs);
5467 } else {
5468 __ CmpLeS(dst, rhs, lhs);
5469 }
5470 return false;
5471 default:
5472 LOG(FATAL) << "Unexpected non-floating-point condition";
5473 UNREACHABLE();
5474 }
5475 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005476 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005477 switch (cond) {
5478 case kCondEQ:
5479 __ CmpEqD(dst, lhs, rhs);
5480 return false;
5481 case kCondNE:
5482 __ CmpEqD(dst, lhs, rhs);
5483 return true;
5484 case kCondLT:
5485 if (gt_bias) {
5486 __ CmpLtD(dst, lhs, rhs);
5487 } else {
5488 __ CmpUltD(dst, lhs, rhs);
5489 }
5490 return false;
5491 case kCondLE:
5492 if (gt_bias) {
5493 __ CmpLeD(dst, lhs, rhs);
5494 } else {
5495 __ CmpUleD(dst, lhs, rhs);
5496 }
5497 return false;
5498 case kCondGT:
5499 if (gt_bias) {
5500 __ CmpUltD(dst, rhs, lhs);
5501 } else {
5502 __ CmpLtD(dst, rhs, lhs);
5503 }
5504 return false;
5505 case kCondGE:
5506 if (gt_bias) {
5507 __ CmpUleD(dst, rhs, lhs);
5508 } else {
5509 __ CmpLeD(dst, rhs, lhs);
5510 }
5511 return false;
5512 default:
5513 LOG(FATAL) << "Unexpected non-floating-point condition";
5514 UNREACHABLE();
5515 }
5516 }
5517}
5518
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005519void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5520 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005521 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005522 LocationSummary* locations,
5523 MipsLabel* label) {
5524 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5525 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5526 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005527 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005528 if (isR6) {
5529 switch (cond) {
5530 case kCondEQ:
5531 __ CmpEqS(FTMP, lhs, rhs);
5532 __ Bc1nez(FTMP, label);
5533 break;
5534 case kCondNE:
5535 __ CmpEqS(FTMP, lhs, rhs);
5536 __ Bc1eqz(FTMP, label);
5537 break;
5538 case kCondLT:
5539 if (gt_bias) {
5540 __ CmpLtS(FTMP, lhs, rhs);
5541 } else {
5542 __ CmpUltS(FTMP, lhs, rhs);
5543 }
5544 __ Bc1nez(FTMP, label);
5545 break;
5546 case kCondLE:
5547 if (gt_bias) {
5548 __ CmpLeS(FTMP, lhs, rhs);
5549 } else {
5550 __ CmpUleS(FTMP, lhs, rhs);
5551 }
5552 __ Bc1nez(FTMP, label);
5553 break;
5554 case kCondGT:
5555 if (gt_bias) {
5556 __ CmpUltS(FTMP, rhs, lhs);
5557 } else {
5558 __ CmpLtS(FTMP, rhs, lhs);
5559 }
5560 __ Bc1nez(FTMP, label);
5561 break;
5562 case kCondGE:
5563 if (gt_bias) {
5564 __ CmpUleS(FTMP, rhs, lhs);
5565 } else {
5566 __ CmpLeS(FTMP, rhs, lhs);
5567 }
5568 __ Bc1nez(FTMP, label);
5569 break;
5570 default:
5571 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005572 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005573 }
5574 } else {
5575 switch (cond) {
5576 case kCondEQ:
5577 __ CeqS(0, lhs, rhs);
5578 __ Bc1t(0, label);
5579 break;
5580 case kCondNE:
5581 __ CeqS(0, lhs, rhs);
5582 __ Bc1f(0, label);
5583 break;
5584 case kCondLT:
5585 if (gt_bias) {
5586 __ ColtS(0, lhs, rhs);
5587 } else {
5588 __ CultS(0, lhs, rhs);
5589 }
5590 __ Bc1t(0, label);
5591 break;
5592 case kCondLE:
5593 if (gt_bias) {
5594 __ ColeS(0, lhs, rhs);
5595 } else {
5596 __ CuleS(0, lhs, rhs);
5597 }
5598 __ Bc1t(0, label);
5599 break;
5600 case kCondGT:
5601 if (gt_bias) {
5602 __ CultS(0, rhs, lhs);
5603 } else {
5604 __ ColtS(0, rhs, lhs);
5605 }
5606 __ Bc1t(0, label);
5607 break;
5608 case kCondGE:
5609 if (gt_bias) {
5610 __ CuleS(0, rhs, lhs);
5611 } else {
5612 __ ColeS(0, rhs, lhs);
5613 }
5614 __ Bc1t(0, label);
5615 break;
5616 default:
5617 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005618 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005619 }
5620 }
5621 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005622 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005623 if (isR6) {
5624 switch (cond) {
5625 case kCondEQ:
5626 __ CmpEqD(FTMP, lhs, rhs);
5627 __ Bc1nez(FTMP, label);
5628 break;
5629 case kCondNE:
5630 __ CmpEqD(FTMP, lhs, rhs);
5631 __ Bc1eqz(FTMP, label);
5632 break;
5633 case kCondLT:
5634 if (gt_bias) {
5635 __ CmpLtD(FTMP, lhs, rhs);
5636 } else {
5637 __ CmpUltD(FTMP, lhs, rhs);
5638 }
5639 __ Bc1nez(FTMP, label);
5640 break;
5641 case kCondLE:
5642 if (gt_bias) {
5643 __ CmpLeD(FTMP, lhs, rhs);
5644 } else {
5645 __ CmpUleD(FTMP, lhs, rhs);
5646 }
5647 __ Bc1nez(FTMP, label);
5648 break;
5649 case kCondGT:
5650 if (gt_bias) {
5651 __ CmpUltD(FTMP, rhs, lhs);
5652 } else {
5653 __ CmpLtD(FTMP, rhs, lhs);
5654 }
5655 __ Bc1nez(FTMP, label);
5656 break;
5657 case kCondGE:
5658 if (gt_bias) {
5659 __ CmpUleD(FTMP, rhs, lhs);
5660 } else {
5661 __ CmpLeD(FTMP, rhs, lhs);
5662 }
5663 __ Bc1nez(FTMP, label);
5664 break;
5665 default:
5666 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005667 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005668 }
5669 } else {
5670 switch (cond) {
5671 case kCondEQ:
5672 __ CeqD(0, lhs, rhs);
5673 __ Bc1t(0, label);
5674 break;
5675 case kCondNE:
5676 __ CeqD(0, lhs, rhs);
5677 __ Bc1f(0, label);
5678 break;
5679 case kCondLT:
5680 if (gt_bias) {
5681 __ ColtD(0, lhs, rhs);
5682 } else {
5683 __ CultD(0, lhs, rhs);
5684 }
5685 __ Bc1t(0, label);
5686 break;
5687 case kCondLE:
5688 if (gt_bias) {
5689 __ ColeD(0, lhs, rhs);
5690 } else {
5691 __ CuleD(0, lhs, rhs);
5692 }
5693 __ Bc1t(0, label);
5694 break;
5695 case kCondGT:
5696 if (gt_bias) {
5697 __ CultD(0, rhs, lhs);
5698 } else {
5699 __ ColtD(0, rhs, lhs);
5700 }
5701 __ Bc1t(0, label);
5702 break;
5703 case kCondGE:
5704 if (gt_bias) {
5705 __ CuleD(0, rhs, lhs);
5706 } else {
5707 __ ColeD(0, rhs, lhs);
5708 }
5709 __ Bc1t(0, label);
5710 break;
5711 default:
5712 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005713 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005714 }
5715 }
5716 }
5717}
5718
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005719void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005720 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005721 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005722 MipsLabel* false_target) {
5723 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005724
David Brazdil0debae72015-11-12 18:37:00 +00005725 if (true_target == nullptr && false_target == nullptr) {
5726 // Nothing to do. The code always falls through.
5727 return;
5728 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005729 // Constant condition, statically compared against "true" (integer value 1).
5730 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005731 if (true_target != nullptr) {
5732 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005733 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005734 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005735 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005736 if (false_target != nullptr) {
5737 __ B(false_target);
5738 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005739 }
David Brazdil0debae72015-11-12 18:37:00 +00005740 return;
5741 }
5742
5743 // The following code generates these patterns:
5744 // (1) true_target == nullptr && false_target != nullptr
5745 // - opposite condition true => branch to false_target
5746 // (2) true_target != nullptr && false_target == nullptr
5747 // - condition true => branch to true_target
5748 // (3) true_target != nullptr && false_target != nullptr
5749 // - condition true => branch to true_target
5750 // - branch to false_target
5751 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005752 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005753 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005754 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005755 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005756 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5757 } else {
5758 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5759 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005760 } else {
5761 // The condition instruction has not been materialized, use its inputs as
5762 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005763 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005764 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005765 LocationSummary* locations = cond->GetLocations();
5766 IfCondition if_cond = condition->GetCondition();
5767 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005768
David Brazdil0debae72015-11-12 18:37:00 +00005769 if (true_target == nullptr) {
5770 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005771 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005772 }
5773
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005774 switch (type) {
5775 default:
5776 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5777 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005778 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005779 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5780 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005781 case DataType::Type::kFloat32:
5782 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005783 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5784 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005785 }
5786 }
David Brazdil0debae72015-11-12 18:37:00 +00005787
5788 // If neither branch falls through (case 3), the conditional branch to `true_target`
5789 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5790 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005791 __ B(false_target);
5792 }
5793}
5794
5795void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005796 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005797 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005798 locations->SetInAt(0, Location::RequiresRegister());
5799 }
5800}
5801
5802void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005803 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5804 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5805 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5806 nullptr : codegen_->GetLabelOf(true_successor);
5807 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5808 nullptr : codegen_->GetLabelOf(false_successor);
5809 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005810}
5811
5812void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005813 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005814 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005815 InvokeRuntimeCallingConvention calling_convention;
5816 RegisterSet caller_saves = RegisterSet::Empty();
5817 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5818 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005819 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005820 locations->SetInAt(0, Location::RequiresRegister());
5821 }
5822}
5823
5824void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005825 SlowPathCodeMIPS* slow_path =
5826 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005827 GenerateTestAndBranch(deoptimize,
5828 /* condition_input_index */ 0,
5829 slow_path->GetEntryLabel(),
5830 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005831}
5832
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005833// This function returns true if a conditional move can be generated for HSelect.
5834// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5835// branches and regular moves.
5836//
5837// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5838//
5839// While determining feasibility of a conditional move and setting inputs/outputs
5840// are two distinct tasks, this function does both because they share quite a bit
5841// of common logic.
5842static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5843 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5844 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5845 HCondition* condition = cond->AsCondition();
5846
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005847 DataType::Type cond_type =
5848 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5849 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005850
5851 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5852 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5853 bool is_true_value_zero_constant =
5854 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5855 bool is_false_value_zero_constant =
5856 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5857
5858 bool can_move_conditionally = false;
5859 bool use_const_for_false_in = false;
5860 bool use_const_for_true_in = false;
5861
5862 if (!cond->IsConstant()) {
5863 switch (cond_type) {
5864 default:
5865 switch (dst_type) {
5866 default:
5867 // Moving int on int condition.
5868 if (is_r6) {
5869 if (is_true_value_zero_constant) {
5870 // seleqz out_reg, false_reg, cond_reg
5871 can_move_conditionally = true;
5872 use_const_for_true_in = true;
5873 } else if (is_false_value_zero_constant) {
5874 // selnez out_reg, true_reg, cond_reg
5875 can_move_conditionally = true;
5876 use_const_for_false_in = true;
5877 } else if (materialized) {
5878 // Not materializing unmaterialized int conditions
5879 // to keep the instruction count low.
5880 // selnez AT, true_reg, cond_reg
5881 // seleqz TMP, false_reg, cond_reg
5882 // or out_reg, AT, TMP
5883 can_move_conditionally = true;
5884 }
5885 } else {
5886 // movn out_reg, true_reg/ZERO, cond_reg
5887 can_move_conditionally = true;
5888 use_const_for_true_in = is_true_value_zero_constant;
5889 }
5890 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005891 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005892 // Moving long on int condition.
5893 if (is_r6) {
5894 if (is_true_value_zero_constant) {
5895 // seleqz out_reg_lo, false_reg_lo, cond_reg
5896 // seleqz out_reg_hi, false_reg_hi, cond_reg
5897 can_move_conditionally = true;
5898 use_const_for_true_in = true;
5899 } else if (is_false_value_zero_constant) {
5900 // selnez out_reg_lo, true_reg_lo, cond_reg
5901 // selnez out_reg_hi, true_reg_hi, cond_reg
5902 can_move_conditionally = true;
5903 use_const_for_false_in = true;
5904 }
5905 // Other long conditional moves would generate 6+ instructions,
5906 // which is too many.
5907 } else {
5908 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5909 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5910 can_move_conditionally = true;
5911 use_const_for_true_in = is_true_value_zero_constant;
5912 }
5913 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005914 case DataType::Type::kFloat32:
5915 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005916 // Moving float/double on int condition.
5917 if (is_r6) {
5918 if (materialized) {
5919 // Not materializing unmaterialized int conditions
5920 // to keep the instruction count low.
5921 can_move_conditionally = true;
5922 if (is_true_value_zero_constant) {
5923 // sltu TMP, ZERO, cond_reg
5924 // mtc1 TMP, temp_cond_reg
5925 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5926 use_const_for_true_in = true;
5927 } else if (is_false_value_zero_constant) {
5928 // sltu TMP, ZERO, cond_reg
5929 // mtc1 TMP, temp_cond_reg
5930 // selnez.fmt out_reg, true_reg, temp_cond_reg
5931 use_const_for_false_in = true;
5932 } else {
5933 // sltu TMP, ZERO, cond_reg
5934 // mtc1 TMP, temp_cond_reg
5935 // sel.fmt temp_cond_reg, false_reg, true_reg
5936 // mov.fmt out_reg, temp_cond_reg
5937 }
5938 }
5939 } else {
5940 // movn.fmt out_reg, true_reg, cond_reg
5941 can_move_conditionally = true;
5942 }
5943 break;
5944 }
5945 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005946 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005947 // We don't materialize long comparison now
5948 // and use conditional branches instead.
5949 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005950 case DataType::Type::kFloat32:
5951 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005952 switch (dst_type) {
5953 default:
5954 // Moving int on float/double condition.
5955 if (is_r6) {
5956 if (is_true_value_zero_constant) {
5957 // mfc1 TMP, temp_cond_reg
5958 // seleqz out_reg, false_reg, TMP
5959 can_move_conditionally = true;
5960 use_const_for_true_in = true;
5961 } else if (is_false_value_zero_constant) {
5962 // mfc1 TMP, temp_cond_reg
5963 // selnez out_reg, true_reg, TMP
5964 can_move_conditionally = true;
5965 use_const_for_false_in = true;
5966 } else {
5967 // mfc1 TMP, temp_cond_reg
5968 // selnez AT, true_reg, TMP
5969 // seleqz TMP, false_reg, TMP
5970 // or out_reg, AT, TMP
5971 can_move_conditionally = true;
5972 }
5973 } else {
5974 // movt out_reg, true_reg/ZERO, cc
5975 can_move_conditionally = true;
5976 use_const_for_true_in = is_true_value_zero_constant;
5977 }
5978 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005979 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005980 // Moving long on float/double condition.
5981 if (is_r6) {
5982 if (is_true_value_zero_constant) {
5983 // mfc1 TMP, temp_cond_reg
5984 // seleqz out_reg_lo, false_reg_lo, TMP
5985 // seleqz out_reg_hi, false_reg_hi, TMP
5986 can_move_conditionally = true;
5987 use_const_for_true_in = true;
5988 } else if (is_false_value_zero_constant) {
5989 // mfc1 TMP, temp_cond_reg
5990 // selnez out_reg_lo, true_reg_lo, TMP
5991 // selnez out_reg_hi, true_reg_hi, TMP
5992 can_move_conditionally = true;
5993 use_const_for_false_in = true;
5994 }
5995 // Other long conditional moves would generate 6+ instructions,
5996 // which is too many.
5997 } else {
5998 // movt out_reg_lo, true_reg_lo/ZERO, cc
5999 // movt out_reg_hi, true_reg_hi/ZERO, cc
6000 can_move_conditionally = true;
6001 use_const_for_true_in = is_true_value_zero_constant;
6002 }
6003 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006004 case DataType::Type::kFloat32:
6005 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006006 // Moving float/double on float/double condition.
6007 if (is_r6) {
6008 can_move_conditionally = true;
6009 if (is_true_value_zero_constant) {
6010 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6011 use_const_for_true_in = true;
6012 } else if (is_false_value_zero_constant) {
6013 // selnez.fmt out_reg, true_reg, temp_cond_reg
6014 use_const_for_false_in = true;
6015 } else {
6016 // sel.fmt temp_cond_reg, false_reg, true_reg
6017 // mov.fmt out_reg, temp_cond_reg
6018 }
6019 } else {
6020 // movt.fmt out_reg, true_reg, cc
6021 can_move_conditionally = true;
6022 }
6023 break;
6024 }
6025 break;
6026 }
6027 }
6028
6029 if (can_move_conditionally) {
6030 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6031 } else {
6032 DCHECK(!use_const_for_false_in);
6033 DCHECK(!use_const_for_true_in);
6034 }
6035
6036 if (locations_to_set != nullptr) {
6037 if (use_const_for_false_in) {
6038 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6039 } else {
6040 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006041 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006042 ? Location::RequiresFpuRegister()
6043 : Location::RequiresRegister());
6044 }
6045 if (use_const_for_true_in) {
6046 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6047 } else {
6048 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006049 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006050 ? Location::RequiresFpuRegister()
6051 : Location::RequiresRegister());
6052 }
6053 if (materialized) {
6054 locations_to_set->SetInAt(2, Location::RequiresRegister());
6055 }
6056 // On R6 we don't require the output to be the same as the
6057 // first input for conditional moves unlike on R2.
6058 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6059 if (is_out_same_as_first_in) {
6060 locations_to_set->SetOut(Location::SameAsFirstInput());
6061 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006062 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006063 ? Location::RequiresFpuRegister()
6064 : Location::RequiresRegister());
6065 }
6066 }
6067
6068 return can_move_conditionally;
6069}
6070
6071void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6072 LocationSummary* locations = select->GetLocations();
6073 Location dst = locations->Out();
6074 Location src = locations->InAt(1);
6075 Register src_reg = ZERO;
6076 Register src_reg_high = ZERO;
6077 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6078 Register cond_reg = TMP;
6079 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006080 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006081 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006082 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006083
6084 if (IsBooleanValueOrMaterializedCondition(cond)) {
6085 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6086 } else {
6087 HCondition* condition = cond->AsCondition();
6088 LocationSummary* cond_locations = cond->GetLocations();
6089 IfCondition if_cond = condition->GetCondition();
6090 cond_type = condition->InputAt(0)->GetType();
6091 switch (cond_type) {
6092 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006093 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006094 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6095 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006096 case DataType::Type::kFloat32:
6097 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006098 cond_inverted = MaterializeFpCompareR2(if_cond,
6099 condition->IsGtBias(),
6100 cond_type,
6101 cond_locations,
6102 cond_cc);
6103 break;
6104 }
6105 }
6106
6107 DCHECK(dst.Equals(locations->InAt(0)));
6108 if (src.IsRegister()) {
6109 src_reg = src.AsRegister<Register>();
6110 } else if (src.IsRegisterPair()) {
6111 src_reg = src.AsRegisterPairLow<Register>();
6112 src_reg_high = src.AsRegisterPairHigh<Register>();
6113 } else if (src.IsConstant()) {
6114 DCHECK(src.GetConstant()->IsZeroBitPattern());
6115 }
6116
6117 switch (cond_type) {
6118 default:
6119 switch (dst_type) {
6120 default:
6121 if (cond_inverted) {
6122 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6123 } else {
6124 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6125 }
6126 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006127 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006128 if (cond_inverted) {
6129 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6130 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6131 } else {
6132 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6133 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6134 }
6135 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006136 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006137 if (cond_inverted) {
6138 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6139 } else {
6140 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6141 }
6142 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006143 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006144 if (cond_inverted) {
6145 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6146 } else {
6147 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6148 }
6149 break;
6150 }
6151 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006152 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006153 LOG(FATAL) << "Unreachable";
6154 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006155 case DataType::Type::kFloat32:
6156 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006157 switch (dst_type) {
6158 default:
6159 if (cond_inverted) {
6160 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6161 } else {
6162 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6163 }
6164 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006165 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006166 if (cond_inverted) {
6167 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6168 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6169 } else {
6170 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6171 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6172 }
6173 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006174 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006175 if (cond_inverted) {
6176 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6177 } else {
6178 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6179 }
6180 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006181 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006182 if (cond_inverted) {
6183 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6184 } else {
6185 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6186 }
6187 break;
6188 }
6189 break;
6190 }
6191}
6192
6193void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6194 LocationSummary* locations = select->GetLocations();
6195 Location dst = locations->Out();
6196 Location false_src = locations->InAt(0);
6197 Location true_src = locations->InAt(1);
6198 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6199 Register cond_reg = TMP;
6200 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006201 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006202 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006203 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006204
6205 if (IsBooleanValueOrMaterializedCondition(cond)) {
6206 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6207 } else {
6208 HCondition* condition = cond->AsCondition();
6209 LocationSummary* cond_locations = cond->GetLocations();
6210 IfCondition if_cond = condition->GetCondition();
6211 cond_type = condition->InputAt(0)->GetType();
6212 switch (cond_type) {
6213 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006214 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006215 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6216 break;
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 cond_inverted = MaterializeFpCompareR6(if_cond,
6220 condition->IsGtBias(),
6221 cond_type,
6222 cond_locations,
6223 fcond_reg);
6224 break;
6225 }
6226 }
6227
6228 if (true_src.IsConstant()) {
6229 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6230 }
6231 if (false_src.IsConstant()) {
6232 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6233 }
6234
6235 switch (dst_type) {
6236 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006237 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006238 __ Mfc1(cond_reg, fcond_reg);
6239 }
6240 if (true_src.IsConstant()) {
6241 if (cond_inverted) {
6242 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6243 } else {
6244 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6245 }
6246 } else if (false_src.IsConstant()) {
6247 if (cond_inverted) {
6248 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6249 } else {
6250 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6251 }
6252 } else {
6253 DCHECK_NE(cond_reg, AT);
6254 if (cond_inverted) {
6255 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6256 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6257 } else {
6258 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6259 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6260 }
6261 __ Or(dst.AsRegister<Register>(), AT, TMP);
6262 }
6263 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006264 case DataType::Type::kInt64: {
6265 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006266 __ Mfc1(cond_reg, fcond_reg);
6267 }
6268 Register dst_lo = dst.AsRegisterPairLow<Register>();
6269 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6270 if (true_src.IsConstant()) {
6271 Register src_lo = false_src.AsRegisterPairLow<Register>();
6272 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6273 if (cond_inverted) {
6274 __ Selnez(dst_lo, src_lo, cond_reg);
6275 __ Selnez(dst_hi, src_hi, cond_reg);
6276 } else {
6277 __ Seleqz(dst_lo, src_lo, cond_reg);
6278 __ Seleqz(dst_hi, src_hi, cond_reg);
6279 }
6280 } else {
6281 DCHECK(false_src.IsConstant());
6282 Register src_lo = true_src.AsRegisterPairLow<Register>();
6283 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6284 if (cond_inverted) {
6285 __ Seleqz(dst_lo, src_lo, cond_reg);
6286 __ Seleqz(dst_hi, src_hi, cond_reg);
6287 } else {
6288 __ Selnez(dst_lo, src_lo, cond_reg);
6289 __ Selnez(dst_hi, src_hi, cond_reg);
6290 }
6291 }
6292 break;
6293 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006294 case DataType::Type::kFloat32: {
6295 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006296 // sel*.fmt tests bit 0 of the condition register, account for that.
6297 __ Sltu(TMP, ZERO, cond_reg);
6298 __ Mtc1(TMP, fcond_reg);
6299 }
6300 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6301 if (true_src.IsConstant()) {
6302 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6303 if (cond_inverted) {
6304 __ SelnezS(dst_reg, src_reg, fcond_reg);
6305 } else {
6306 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6307 }
6308 } else if (false_src.IsConstant()) {
6309 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6310 if (cond_inverted) {
6311 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6312 } else {
6313 __ SelnezS(dst_reg, src_reg, fcond_reg);
6314 }
6315 } else {
6316 if (cond_inverted) {
6317 __ SelS(fcond_reg,
6318 true_src.AsFpuRegister<FRegister>(),
6319 false_src.AsFpuRegister<FRegister>());
6320 } else {
6321 __ SelS(fcond_reg,
6322 false_src.AsFpuRegister<FRegister>(),
6323 true_src.AsFpuRegister<FRegister>());
6324 }
6325 __ MovS(dst_reg, fcond_reg);
6326 }
6327 break;
6328 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006329 case DataType::Type::kFloat64: {
6330 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006331 // sel*.fmt tests bit 0 of the condition register, account for that.
6332 __ Sltu(TMP, ZERO, cond_reg);
6333 __ Mtc1(TMP, fcond_reg);
6334 }
6335 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6336 if (true_src.IsConstant()) {
6337 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6338 if (cond_inverted) {
6339 __ SelnezD(dst_reg, src_reg, fcond_reg);
6340 } else {
6341 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6342 }
6343 } else if (false_src.IsConstant()) {
6344 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6345 if (cond_inverted) {
6346 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6347 } else {
6348 __ SelnezD(dst_reg, src_reg, fcond_reg);
6349 }
6350 } else {
6351 if (cond_inverted) {
6352 __ SelD(fcond_reg,
6353 true_src.AsFpuRegister<FRegister>(),
6354 false_src.AsFpuRegister<FRegister>());
6355 } else {
6356 __ SelD(fcond_reg,
6357 false_src.AsFpuRegister<FRegister>(),
6358 true_src.AsFpuRegister<FRegister>());
6359 }
6360 __ MovD(dst_reg, fcond_reg);
6361 }
6362 break;
6363 }
6364 }
6365}
6366
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006367void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006368 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006369 LocationSummary(flag, LocationSummary::kNoCall);
6370 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006371}
6372
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006373void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6374 __ LoadFromOffset(kLoadWord,
6375 flag->GetLocations()->Out().AsRegister<Register>(),
6376 SP,
6377 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006378}
6379
David Brazdil74eb1b22015-12-14 11:44:01 +00006380void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006381 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006382 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006383}
6384
6385void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006386 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6387 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6388 if (is_r6) {
6389 GenConditionalMoveR6(select);
6390 } else {
6391 GenConditionalMoveR2(select);
6392 }
6393 } else {
6394 LocationSummary* locations = select->GetLocations();
6395 MipsLabel false_target;
6396 GenerateTestAndBranch(select,
6397 /* condition_input_index */ 2,
6398 /* true_target */ nullptr,
6399 &false_target);
6400 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6401 __ Bind(&false_target);
6402 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006403}
6404
David Srbecky0cf44932015-12-09 14:09:59 +00006405void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006406 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006407}
6408
David Srbeckyd28f4a02016-03-14 17:14:24 +00006409void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6410 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006411}
6412
6413void CodeGeneratorMIPS::GenerateNop() {
6414 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006415}
6416
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006417void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006418 DataType::Type field_type = field_info.GetFieldType();
6419 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006420 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006421 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006422 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006423 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006424 instruction,
6425 generate_volatile
6426 ? LocationSummary::kCallOnMainOnly
6427 : (object_field_get_with_read_barrier
6428 ? LocationSummary::kCallOnSlowPath
6429 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006430
Alexey Frunzec61c0762017-04-10 13:54:23 -07006431 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6432 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6433 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006434 locations->SetInAt(0, Location::RequiresRegister());
6435 if (generate_volatile) {
6436 InvokeRuntimeCallingConvention calling_convention;
6437 // need A0 to hold base + offset
6438 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006439 if (field_type == DataType::Type::kInt64) {
6440 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006441 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006442 // Use Location::Any() to prevent situations when running out of available fp registers.
6443 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006444 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006445 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006446 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6447 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6448 }
6449 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006450 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006451 locations->SetOut(Location::RequiresFpuRegister());
6452 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006453 // The output overlaps in the case of an object field get with
6454 // read barriers enabled: we do not want the move to overwrite the
6455 // object's location, as we need it to emit the read barrier.
6456 locations->SetOut(Location::RequiresRegister(),
6457 object_field_get_with_read_barrier
6458 ? Location::kOutputOverlap
6459 : Location::kNoOutputOverlap);
6460 }
6461 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6462 // We need a temporary register for the read barrier marking slow
6463 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006464 if (!kBakerReadBarrierThunksEnableForFields) {
6465 locations->AddTemp(Location::RequiresRegister());
6466 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006467 }
6468 }
6469}
6470
6471void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6472 const FieldInfo& field_info,
6473 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006474 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6475 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006476 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006477 Location obj_loc = locations->InAt(0);
6478 Register obj = obj_loc.AsRegister<Register>();
6479 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006480 LoadOperandType load_type = kLoadUnsignedByte;
6481 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006482 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006483 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006484
6485 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006486 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006487 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006488 load_type = kLoadUnsignedByte;
6489 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006490 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006491 load_type = kLoadSignedByte;
6492 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006493 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006494 load_type = kLoadUnsignedHalfword;
6495 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006496 case DataType::Type::kInt16:
6497 load_type = kLoadSignedHalfword;
6498 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006499 case DataType::Type::kInt32:
6500 case DataType::Type::kFloat32:
6501 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006502 load_type = kLoadWord;
6503 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006504 case DataType::Type::kInt64:
6505 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006506 load_type = kLoadDoubleword;
6507 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006508 case DataType::Type::kUint32:
6509 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006510 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006511 LOG(FATAL) << "Unreachable type " << type;
6512 UNREACHABLE();
6513 }
6514
6515 if (is_volatile && load_type == kLoadDoubleword) {
6516 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006517 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006518 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006519 __ LoadFromOffset(kLoadWord,
6520 ZERO,
6521 locations->GetTemp(0).AsRegister<Register>(),
6522 0,
6523 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006524 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006525 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006526 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006527 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006528 if (dst_loc.IsFpuRegister()) {
6529 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006530 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006531 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006532 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006533 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006534 __ StoreToOffset(kStoreWord,
6535 locations->GetTemp(1).AsRegister<Register>(),
6536 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006537 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006538 __ StoreToOffset(kStoreWord,
6539 locations->GetTemp(2).AsRegister<Register>(),
6540 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006541 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006542 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006543 }
6544 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006545 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006546 // /* HeapReference<Object> */ dst = *(obj + offset)
6547 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006548 Location temp_loc =
6549 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006550 // Note that a potential implicit null check is handled in this
6551 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6552 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6553 dst_loc,
6554 obj,
6555 offset,
6556 temp_loc,
6557 /* needs_null_check */ true);
6558 if (is_volatile) {
6559 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6560 }
6561 } else {
6562 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6563 if (is_volatile) {
6564 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6565 }
6566 // If read barriers are enabled, emit read barriers other than
6567 // Baker's using a slow path (and also unpoison the loaded
6568 // reference, if heap poisoning is enabled).
6569 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6570 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006571 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006572 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006573 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006574 DCHECK(dst_loc.IsRegisterPair());
6575 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006576 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006577 DCHECK(dst_loc.IsRegister());
6578 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006579 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006580 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006581 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006582 DCHECK(dst_loc.IsFpuRegister());
6583 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006584 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006585 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006586 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006587 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006588 }
6589 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006590 }
6591
Alexey Frunze15958152017-02-09 19:08:30 -08006592 // Memory barriers, in the case of references, are handled in the
6593 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006594 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006595 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6596 }
6597}
6598
6599void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006600 DataType::Type field_type = field_info.GetFieldType();
6601 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006602 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006603 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006604 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006605
6606 locations->SetInAt(0, Location::RequiresRegister());
6607 if (generate_volatile) {
6608 InvokeRuntimeCallingConvention calling_convention;
6609 // need A0 to hold base + offset
6610 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006611 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006612 locations->SetInAt(1, Location::RegisterPairLocation(
6613 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6614 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006615 // Use Location::Any() to prevent situations when running out of available fp registers.
6616 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006617 // Pass FP parameters in core registers.
6618 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6619 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6620 }
6621 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006622 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006623 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006624 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006625 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006626 }
6627 }
6628}
6629
6630void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6631 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006632 uint32_t dex_pc,
6633 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006634 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006635 LocationSummary* locations = instruction->GetLocations();
6636 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006637 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006638 StoreOperandType store_type = kStoreByte;
6639 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006640 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006641 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006642 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006643
6644 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006645 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006646 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006647 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006648 store_type = kStoreByte;
6649 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006650 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006651 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006652 store_type = kStoreHalfword;
6653 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006654 case DataType::Type::kInt32:
6655 case DataType::Type::kFloat32:
6656 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006657 store_type = kStoreWord;
6658 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006659 case DataType::Type::kInt64:
6660 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006661 store_type = kStoreDoubleword;
6662 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006663 case DataType::Type::kUint32:
6664 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006665 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006666 LOG(FATAL) << "Unreachable type " << type;
6667 UNREACHABLE();
6668 }
6669
6670 if (is_volatile) {
6671 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6672 }
6673
6674 if (is_volatile && store_type == kStoreDoubleword) {
6675 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006676 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006677 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006678 __ LoadFromOffset(kLoadWord,
6679 ZERO,
6680 locations->GetTemp(0).AsRegister<Register>(),
6681 0,
6682 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006683 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006684 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006685 if (value_location.IsFpuRegister()) {
6686 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6687 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006688 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006689 value_location.AsFpuRegister<FRegister>());
6690 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006691 __ LoadFromOffset(kLoadWord,
6692 locations->GetTemp(1).AsRegister<Register>(),
6693 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006694 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006695 __ LoadFromOffset(kLoadWord,
6696 locations->GetTemp(2).AsRegister<Register>(),
6697 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006698 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006699 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006700 DCHECK(value_location.IsConstant());
6701 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6702 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006703 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6704 locations->GetTemp(1).AsRegister<Register>(),
6705 value);
6706 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006707 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006708 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006709 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6710 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006711 if (value_location.IsConstant()) {
6712 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6713 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006714 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006715 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006716 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006717 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006718 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006719 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006720 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006721 if (kPoisonHeapReferences && needs_write_barrier) {
6722 // Note that in the case where `value` is a null reference,
6723 // we do not enter this block, as a null reference does not
6724 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006725 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006726 __ PoisonHeapReference(TMP, src);
6727 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6728 } else {
6729 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6730 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006731 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006732 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006733 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006734 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006735 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006736 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006737 }
6738 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006739 }
6740
Alexey Frunzec061de12017-02-14 13:27:23 -08006741 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006742 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006743 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006744 }
6745
6746 if (is_volatile) {
6747 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6748 }
6749}
6750
6751void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6752 HandleFieldGet(instruction, instruction->GetFieldInfo());
6753}
6754
6755void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6756 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6757}
6758
6759void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6760 HandleFieldSet(instruction, instruction->GetFieldInfo());
6761}
6762
6763void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006764 HandleFieldSet(instruction,
6765 instruction->GetFieldInfo(),
6766 instruction->GetDexPc(),
6767 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006768}
6769
Alexey Frunze15958152017-02-09 19:08:30 -08006770void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6771 HInstruction* instruction,
6772 Location out,
6773 uint32_t offset,
6774 Location maybe_temp,
6775 ReadBarrierOption read_barrier_option) {
6776 Register out_reg = out.AsRegister<Register>();
6777 if (read_barrier_option == kWithReadBarrier) {
6778 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006779 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6780 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6781 }
Alexey Frunze15958152017-02-09 19:08:30 -08006782 if (kUseBakerReadBarrier) {
6783 // Load with fast path based Baker's read barrier.
6784 // /* HeapReference<Object> */ out = *(out + offset)
6785 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6786 out,
6787 out_reg,
6788 offset,
6789 maybe_temp,
6790 /* needs_null_check */ false);
6791 } else {
6792 // Load with slow path based read barrier.
6793 // Save the value of `out` into `maybe_temp` before overwriting it
6794 // in the following move operation, as we will need it for the
6795 // read barrier below.
6796 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6797 // /* HeapReference<Object> */ out = *(out + offset)
6798 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6799 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6800 }
6801 } else {
6802 // Plain load with no read barrier.
6803 // /* HeapReference<Object> */ out = *(out + offset)
6804 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6805 __ MaybeUnpoisonHeapReference(out_reg);
6806 }
6807}
6808
6809void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6810 HInstruction* instruction,
6811 Location out,
6812 Location obj,
6813 uint32_t offset,
6814 Location maybe_temp,
6815 ReadBarrierOption read_barrier_option) {
6816 Register out_reg = out.AsRegister<Register>();
6817 Register obj_reg = obj.AsRegister<Register>();
6818 if (read_barrier_option == kWithReadBarrier) {
6819 CHECK(kEmitCompilerReadBarrier);
6820 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006821 if (!kBakerReadBarrierThunksEnableForFields) {
6822 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6823 }
Alexey Frunze15958152017-02-09 19:08:30 -08006824 // Load with fast path based Baker's read barrier.
6825 // /* HeapReference<Object> */ out = *(obj + offset)
6826 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6827 out,
6828 obj_reg,
6829 offset,
6830 maybe_temp,
6831 /* needs_null_check */ false);
6832 } else {
6833 // Load with slow path based read barrier.
6834 // /* HeapReference<Object> */ out = *(obj + offset)
6835 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6836 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6837 }
6838 } else {
6839 // Plain load with no read barrier.
6840 // /* HeapReference<Object> */ out = *(obj + offset)
6841 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6842 __ MaybeUnpoisonHeapReference(out_reg);
6843 }
6844}
6845
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006846static inline int GetBakerMarkThunkNumber(Register reg) {
6847 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6848 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6849 return reg - V0;
6850 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6851 return 14 + (reg - S2);
6852 } else if (reg == FP) { // One more.
6853 return 20;
6854 }
6855 LOG(FATAL) << "Unexpected register " << reg;
6856 UNREACHABLE();
6857}
6858
6859static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6860 int num = GetBakerMarkThunkNumber(reg) +
6861 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6862 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6863}
6864
6865static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6866 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6867 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6868}
6869
Alexey Frunze15958152017-02-09 19:08:30 -08006870void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6871 Location root,
6872 Register obj,
6873 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006874 ReadBarrierOption read_barrier_option,
6875 MipsLabel* label_low) {
6876 bool reordering;
6877 if (label_low != nullptr) {
6878 DCHECK_EQ(offset, 0x5678u);
6879 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006880 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006881 if (read_barrier_option == kWithReadBarrier) {
6882 DCHECK(kEmitCompilerReadBarrier);
6883 if (kUseBakerReadBarrier) {
6884 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6885 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006886 if (kBakerReadBarrierThunksEnableForGcRoots) {
6887 // Note that we do not actually check the value of `GetIsGcMarking()`
6888 // to decide whether to mark the loaded GC root or not. Instead, we
6889 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6890 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6891 // vice versa.
6892 //
6893 // We use thunks for the slow path. That thunk checks the reference
6894 // and jumps to the entrypoint if needed.
6895 //
6896 // temp = Thread::Current()->pReadBarrierMarkReg00
6897 // // AKA &art_quick_read_barrier_mark_introspection.
6898 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6899 // if (temp != nullptr) {
6900 // temp = &gc_root_thunk<root_reg>
6901 // root = temp(root)
6902 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006903
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006904 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6905 const int32_t entry_point_offset =
6906 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6907 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6908 int16_t offset_low = Low16Bits(offset);
6909 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6910 // extension in lw.
6911 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6912 Register base = short_offset ? obj : TMP;
6913 // Loading the entrypoint does not require a load acquire since it is only changed when
6914 // threads are suspended or running a checkpoint.
6915 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6916 reordering = __ SetReorder(false);
6917 if (!short_offset) {
6918 DCHECK(!label_low);
6919 __ AddUpper(base, obj, offset_high);
6920 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006921 MipsLabel skip_call;
6922 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006923 if (label_low != nullptr) {
6924 DCHECK(short_offset);
6925 __ Bind(label_low);
6926 }
6927 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6928 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6929 // in delay slot.
6930 if (isR6) {
6931 __ Jialc(T9, thunk_disp);
6932 } else {
6933 __ Addiu(T9, T9, thunk_disp);
6934 __ Jalr(T9);
6935 __ Nop();
6936 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006937 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006938 __ SetReorder(reordering);
6939 } else {
6940 // Note that we do not actually check the value of `GetIsGcMarking()`
6941 // to decide whether to mark the loaded GC root or not. Instead, we
6942 // load into `temp` (T9) the read barrier mark entry point corresponding
6943 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6944 // is false, and vice versa.
6945 //
6946 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6947 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6948 // if (temp != null) {
6949 // root = temp(root)
6950 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006951
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006952 if (label_low != nullptr) {
6953 reordering = __ SetReorder(false);
6954 __ Bind(label_low);
6955 }
6956 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6957 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6958 if (label_low != nullptr) {
6959 __ SetReorder(reordering);
6960 }
6961 static_assert(
6962 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6963 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6964 "have different sizes.");
6965 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6966 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6967 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006968
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006969 // Slow path marking the GC root `root`.
6970 Location temp = Location::RegisterLocation(T9);
6971 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006972 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006973 instruction,
6974 root,
6975 /*entrypoint*/ temp);
6976 codegen_->AddSlowPath(slow_path);
6977
6978 const int32_t entry_point_offset =
6979 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6980 // Loading the entrypoint does not require a load acquire since it is only changed when
6981 // threads are suspended or running a checkpoint.
6982 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6983 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6984 __ Bind(slow_path->GetExitLabel());
6985 }
Alexey Frunze15958152017-02-09 19:08:30 -08006986 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006987 if (label_low != nullptr) {
6988 reordering = __ SetReorder(false);
6989 __ Bind(label_low);
6990 }
Alexey Frunze15958152017-02-09 19:08:30 -08006991 // GC root loaded through a slow path for read barriers other
6992 // than Baker's.
6993 // /* GcRoot<mirror::Object>* */ root = obj + offset
6994 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006995 if (label_low != nullptr) {
6996 __ SetReorder(reordering);
6997 }
Alexey Frunze15958152017-02-09 19:08:30 -08006998 // /* mirror::Object* */ root = root->Read()
6999 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7000 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007001 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007002 if (label_low != nullptr) {
7003 reordering = __ SetReorder(false);
7004 __ Bind(label_low);
7005 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007006 // Plain GC root load with no read barrier.
7007 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7008 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7009 // Note that GC roots are not affected by heap poisoning, thus we
7010 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007011 if (label_low != nullptr) {
7012 __ SetReorder(reordering);
7013 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007014 }
7015}
7016
Alexey Frunze15958152017-02-09 19:08:30 -08007017void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7018 Location ref,
7019 Register obj,
7020 uint32_t offset,
7021 Location temp,
7022 bool needs_null_check) {
7023 DCHECK(kEmitCompilerReadBarrier);
7024 DCHECK(kUseBakerReadBarrier);
7025
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007026 if (kBakerReadBarrierThunksEnableForFields) {
7027 // Note that we do not actually check the value of `GetIsGcMarking()`
7028 // to decide whether to mark the loaded reference or not. Instead, we
7029 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7030 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7031 // vice versa.
7032 //
7033 // We use thunks for the slow path. That thunk checks the reference
7034 // and jumps to the entrypoint if needed. If the holder is not gray,
7035 // it issues a load-load memory barrier and returns to the original
7036 // reference load.
7037 //
7038 // temp = Thread::Current()->pReadBarrierMarkReg00
7039 // // AKA &art_quick_read_barrier_mark_introspection.
7040 // if (temp != nullptr) {
7041 // temp = &field_array_thunk<holder_reg>
7042 // temp()
7043 // }
7044 // not_gray_return_address:
7045 // // If the offset is too large to fit into the lw instruction, we
7046 // // use an adjusted base register (TMP) here. This register
7047 // // receives bits 16 ... 31 of the offset before the thunk invocation
7048 // // and the thunk benefits from it.
7049 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7050 // gray_return_address:
7051
7052 DCHECK(temp.IsInvalid());
7053 bool isR6 = GetInstructionSetFeatures().IsR6();
7054 int16_t offset_low = Low16Bits(offset);
7055 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7056 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7057 bool reordering = __ SetReorder(false);
7058 const int32_t entry_point_offset =
7059 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7060 // There may have or may have not been a null check if the field offset is smaller than
7061 // the page size.
7062 // There must've been a null check in case it's actually a load from an array.
7063 // We will, however, perform an explicit null check in the thunk as it's easier to
7064 // do it than not.
7065 if (instruction->IsArrayGet()) {
7066 DCHECK(!needs_null_check);
7067 }
7068 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7069 // Loading the entrypoint does not require a load acquire since it is only changed when
7070 // threads are suspended or running a checkpoint.
7071 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7072 Register ref_reg = ref.AsRegister<Register>();
7073 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007074 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007075 if (short_offset) {
7076 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007077 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007078 __ Nop(); // In forbidden slot.
7079 __ Jialc(T9, thunk_disp);
7080 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007081 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007082 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7083 __ Jalr(T9);
7084 __ Nop(); // In delay slot.
7085 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007086 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007087 } else {
7088 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007089 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007090 __ Aui(base, obj, offset_high); // In delay slot.
7091 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007092 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007093 } else {
7094 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007095 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007096 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7097 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007098 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007099 __ Addu(base, base, obj); // In delay slot.
7100 }
7101 }
7102 // /* HeapReference<Object> */ ref = *(obj + offset)
7103 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7104 if (needs_null_check) {
7105 MaybeRecordImplicitNullCheck(instruction);
7106 }
7107 __ MaybeUnpoisonHeapReference(ref_reg);
7108 __ SetReorder(reordering);
7109 return;
7110 }
7111
Alexey Frunze15958152017-02-09 19:08:30 -08007112 // /* HeapReference<Object> */ ref = *(obj + offset)
7113 Location no_index = Location::NoLocation();
7114 ScaleFactor no_scale_factor = TIMES_1;
7115 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7116 ref,
7117 obj,
7118 offset,
7119 no_index,
7120 no_scale_factor,
7121 temp,
7122 needs_null_check);
7123}
7124
7125void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7126 Location ref,
7127 Register obj,
7128 uint32_t data_offset,
7129 Location index,
7130 Location temp,
7131 bool needs_null_check) {
7132 DCHECK(kEmitCompilerReadBarrier);
7133 DCHECK(kUseBakerReadBarrier);
7134
7135 static_assert(
7136 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7137 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007138 ScaleFactor scale_factor = TIMES_4;
7139
7140 if (kBakerReadBarrierThunksEnableForArrays) {
7141 // Note that we do not actually check the value of `GetIsGcMarking()`
7142 // to decide whether to mark the loaded reference or not. Instead, we
7143 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7144 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7145 // vice versa.
7146 //
7147 // We use thunks for the slow path. That thunk checks the reference
7148 // and jumps to the entrypoint if needed. If the holder is not gray,
7149 // it issues a load-load memory barrier and returns to the original
7150 // reference load.
7151 //
7152 // temp = Thread::Current()->pReadBarrierMarkReg00
7153 // // AKA &art_quick_read_barrier_mark_introspection.
7154 // if (temp != nullptr) {
7155 // temp = &field_array_thunk<holder_reg>
7156 // temp()
7157 // }
7158 // not_gray_return_address:
7159 // // The element address is pre-calculated in the TMP register before the
7160 // // thunk invocation and the thunk benefits from it.
7161 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7162 // gray_return_address:
7163
7164 DCHECK(temp.IsInvalid());
7165 DCHECK(index.IsValid());
7166 bool reordering = __ SetReorder(false);
7167 const int32_t entry_point_offset =
7168 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7169 // We will not do the explicit null check in the thunk as some form of a null check
7170 // must've been done earlier.
7171 DCHECK(!needs_null_check);
7172 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7173 // Loading the entrypoint does not require a load acquire since it is only changed when
7174 // threads are suspended or running a checkpoint.
7175 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7176 Register ref_reg = ref.AsRegister<Register>();
7177 Register index_reg = index.IsRegisterPair()
7178 ? index.AsRegisterPairLow<Register>()
7179 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007180 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007181 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007182 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007183 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7184 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007185 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007186 } else {
7187 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007188 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007189 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7190 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007191 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007192 __ Addu(TMP, TMP, obj); // In delay slot.
7193 }
7194 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7195 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7196 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7197 __ MaybeUnpoisonHeapReference(ref_reg);
7198 __ SetReorder(reordering);
7199 return;
7200 }
7201
Alexey Frunze15958152017-02-09 19:08:30 -08007202 // /* HeapReference<Object> */ ref =
7203 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007204 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7205 ref,
7206 obj,
7207 data_offset,
7208 index,
7209 scale_factor,
7210 temp,
7211 needs_null_check);
7212}
7213
7214void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7215 Location ref,
7216 Register obj,
7217 uint32_t offset,
7218 Location index,
7219 ScaleFactor scale_factor,
7220 Location temp,
7221 bool needs_null_check,
7222 bool always_update_field) {
7223 DCHECK(kEmitCompilerReadBarrier);
7224 DCHECK(kUseBakerReadBarrier);
7225
7226 // In slow path based read barriers, the read barrier call is
7227 // inserted after the original load. However, in fast path based
7228 // Baker's read barriers, we need to perform the load of
7229 // mirror::Object::monitor_ *before* the original reference load.
7230 // This load-load ordering is required by the read barrier.
7231 // The fast path/slow path (for Baker's algorithm) should look like:
7232 //
7233 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7234 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7235 // HeapReference<Object> ref = *src; // Original reference load.
7236 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7237 // if (is_gray) {
7238 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7239 // }
7240 //
7241 // Note: the original implementation in ReadBarrier::Barrier is
7242 // slightly more complex as it performs additional checks that we do
7243 // not do here for performance reasons.
7244
7245 Register ref_reg = ref.AsRegister<Register>();
7246 Register temp_reg = temp.AsRegister<Register>();
7247 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7248
7249 // /* int32_t */ monitor = obj->monitor_
7250 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7251 if (needs_null_check) {
7252 MaybeRecordImplicitNullCheck(instruction);
7253 }
7254 // /* LockWord */ lock_word = LockWord(monitor)
7255 static_assert(sizeof(LockWord) == sizeof(int32_t),
7256 "art::LockWord and int32_t have different sizes.");
7257
7258 __ Sync(0); // Barrier to prevent load-load reordering.
7259
7260 // The actual reference load.
7261 if (index.IsValid()) {
7262 // Load types involving an "index": ArrayGet,
7263 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7264 // intrinsics.
7265 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7266 if (index.IsConstant()) {
7267 size_t computed_offset =
7268 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7269 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7270 } else {
7271 // Handle the special case of the
7272 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7273 // intrinsics, which use a register pair as index ("long
7274 // offset"), of which only the low part contains data.
7275 Register index_reg = index.IsRegisterPair()
7276 ? index.AsRegisterPairLow<Register>()
7277 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007278 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007279 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7280 }
7281 } else {
7282 // /* HeapReference<Object> */ ref = *(obj + offset)
7283 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7284 }
7285
7286 // Object* ref = ref_addr->AsMirrorPtr()
7287 __ MaybeUnpoisonHeapReference(ref_reg);
7288
7289 // Slow path marking the object `ref` when it is gray.
7290 SlowPathCodeMIPS* slow_path;
7291 if (always_update_field) {
7292 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7293 // of the form `obj + field_offset`, where `obj` is a register and
7294 // `field_offset` is a register pair (of which only the lower half
7295 // is used). Thus `offset` and `scale_factor` above are expected
7296 // to be null in this code path.
7297 DCHECK_EQ(offset, 0u);
7298 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007299 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007300 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7301 ref,
7302 obj,
7303 /* field_offset */ index,
7304 temp_reg);
7305 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007306 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007307 }
7308 AddSlowPath(slow_path);
7309
7310 // if (rb_state == ReadBarrier::GrayState())
7311 // ref = ReadBarrier::Mark(ref);
7312 // Given the numeric representation, it's enough to check the low bit of the
7313 // rb_state. We do that by shifting the bit into the sign bit (31) and
7314 // performing a branch on less than zero.
7315 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7316 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7317 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7318 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7319 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7320 __ Bind(slow_path->GetExitLabel());
7321}
7322
7323void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7324 Location out,
7325 Location ref,
7326 Location obj,
7327 uint32_t offset,
7328 Location index) {
7329 DCHECK(kEmitCompilerReadBarrier);
7330
7331 // Insert a slow path based read barrier *after* the reference load.
7332 //
7333 // If heap poisoning is enabled, the unpoisoning of the loaded
7334 // reference will be carried out by the runtime within the slow
7335 // path.
7336 //
7337 // Note that `ref` currently does not get unpoisoned (when heap
7338 // poisoning is enabled), which is alright as the `ref` argument is
7339 // not used by the artReadBarrierSlow entry point.
7340 //
7341 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007342 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007343 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7344 AddSlowPath(slow_path);
7345
7346 __ B(slow_path->GetEntryLabel());
7347 __ Bind(slow_path->GetExitLabel());
7348}
7349
7350void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7351 Location out,
7352 Location ref,
7353 Location obj,
7354 uint32_t offset,
7355 Location index) {
7356 if (kEmitCompilerReadBarrier) {
7357 // Baker's read barriers shall be handled by the fast path
7358 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7359 DCHECK(!kUseBakerReadBarrier);
7360 // If heap poisoning is enabled, unpoisoning will be taken care of
7361 // by the runtime within the slow path.
7362 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7363 } else if (kPoisonHeapReferences) {
7364 __ UnpoisonHeapReference(out.AsRegister<Register>());
7365 }
7366}
7367
7368void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7369 Location out,
7370 Location root) {
7371 DCHECK(kEmitCompilerReadBarrier);
7372
7373 // Insert a slow path based read barrier *after* the GC root load.
7374 //
7375 // Note that GC roots are not affected by heap poisoning, so we do
7376 // not need to do anything special for this here.
7377 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007378 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007379 AddSlowPath(slow_path);
7380
7381 __ B(slow_path->GetEntryLabel());
7382 __ Bind(slow_path->GetExitLabel());
7383}
7384
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007385void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007386 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7387 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007388 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007389 switch (type_check_kind) {
7390 case TypeCheckKind::kExactCheck:
7391 case TypeCheckKind::kAbstractClassCheck:
7392 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007393 case TypeCheckKind::kArrayObjectCheck: {
7394 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7395 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7396 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007397 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007398 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007399 case TypeCheckKind::kArrayCheck:
7400 case TypeCheckKind::kUnresolvedCheck:
7401 case TypeCheckKind::kInterfaceCheck:
7402 call_kind = LocationSummary::kCallOnSlowPath;
7403 break;
7404 }
7405
Vladimir Markoca6fff82017-10-03 14:49:14 +01007406 LocationSummary* locations =
7407 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007408 if (baker_read_barrier_slow_path) {
7409 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7410 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007411 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007412 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007413 // The output does overlap inputs.
7414 // Note that TypeCheckSlowPathMIPS uses this register too.
7415 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007416 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007417}
7418
7419void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007420 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007421 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007422 Location obj_loc = locations->InAt(0);
7423 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007424 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007425 Location out_loc = locations->Out();
7426 Register out = out_loc.AsRegister<Register>();
7427 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7428 DCHECK_LE(num_temps, 1u);
7429 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007430 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7431 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7432 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7433 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007434 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007435 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007436
7437 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007438 // Avoid this check if we know `obj` is not null.
7439 if (instruction->MustDoNullCheck()) {
7440 __ Move(out, ZERO);
7441 __ Beqz(obj, &done);
7442 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007443
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007444 switch (type_check_kind) {
7445 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007446 ReadBarrierOption read_barrier_option =
7447 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007448 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007449 GenerateReferenceLoadTwoRegisters(instruction,
7450 out_loc,
7451 obj_loc,
7452 class_offset,
7453 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007454 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007455 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007456 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007457 __ Sltiu(out, out, 1);
7458 break;
7459 }
7460
7461 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007462 ReadBarrierOption read_barrier_option =
7463 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007464 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007465 GenerateReferenceLoadTwoRegisters(instruction,
7466 out_loc,
7467 obj_loc,
7468 class_offset,
7469 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007470 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007471 // If the class is abstract, we eagerly fetch the super class of the
7472 // object to avoid doing a comparison we know will fail.
7473 MipsLabel loop;
7474 __ Bind(&loop);
7475 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007476 GenerateReferenceLoadOneRegister(instruction,
7477 out_loc,
7478 super_offset,
7479 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007480 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007481 // If `out` is null, we use it for the result, and jump to `done`.
7482 __ Beqz(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007483 __ Bne(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007484 __ LoadConst32(out, 1);
7485 break;
7486 }
7487
7488 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007489 ReadBarrierOption read_barrier_option =
7490 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007491 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007492 GenerateReferenceLoadTwoRegisters(instruction,
7493 out_loc,
7494 obj_loc,
7495 class_offset,
7496 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007497 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007498 // Walk over the class hierarchy to find a match.
7499 MipsLabel loop, success;
7500 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007501 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007502 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007503 GenerateReferenceLoadOneRegister(instruction,
7504 out_loc,
7505 super_offset,
7506 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007507 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007508 __ Bnez(out, &loop);
7509 // If `out` is null, we use it for the result, and jump to `done`.
7510 __ B(&done);
7511 __ Bind(&success);
7512 __ LoadConst32(out, 1);
7513 break;
7514 }
7515
7516 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007517 ReadBarrierOption read_barrier_option =
7518 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007519 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007520 GenerateReferenceLoadTwoRegisters(instruction,
7521 out_loc,
7522 obj_loc,
7523 class_offset,
7524 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007525 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007526 // Do an exact check.
7527 MipsLabel success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007528 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007529 // Otherwise, we need to check that the object's class is a non-primitive array.
7530 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007531 GenerateReferenceLoadOneRegister(instruction,
7532 out_loc,
7533 component_offset,
7534 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007535 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007536 // If `out` is null, we use it for the result, and jump to `done`.
7537 __ Beqz(out, &done);
7538 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7539 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7540 __ Sltiu(out, out, 1);
7541 __ B(&done);
7542 __ Bind(&success);
7543 __ LoadConst32(out, 1);
7544 break;
7545 }
7546
7547 case TypeCheckKind::kArrayCheck: {
7548 // No read barrier since the slow path will retry upon failure.
7549 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007550 GenerateReferenceLoadTwoRegisters(instruction,
7551 out_loc,
7552 obj_loc,
7553 class_offset,
7554 maybe_temp_loc,
7555 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007556 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007557 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7558 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007559 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007560 __ Bne(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007561 __ LoadConst32(out, 1);
7562 break;
7563 }
7564
7565 case TypeCheckKind::kUnresolvedCheck:
7566 case TypeCheckKind::kInterfaceCheck: {
7567 // Note that we indeed only call on slow path, but we always go
7568 // into the slow path for the unresolved and interface check
7569 // cases.
7570 //
7571 // We cannot directly call the InstanceofNonTrivial runtime
7572 // entry point without resorting to a type checking slow path
7573 // here (i.e. by calling InvokeRuntime directly), as it would
7574 // require to assign fixed registers for the inputs of this
7575 // HInstanceOf instruction (following the runtime calling
7576 // convention), which might be cluttered by the potential first
7577 // read barrier emission at the beginning of this method.
7578 //
7579 // TODO: Introduce a new runtime entry point taking the object
7580 // to test (instead of its class) as argument, and let it deal
7581 // with the read barrier issues. This will let us refactor this
7582 // case of the `switch` code as it was previously (with a direct
7583 // call to the runtime not using a type checking slow path).
7584 // This should also be beneficial for the other cases above.
7585 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007586 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7587 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007588 codegen_->AddSlowPath(slow_path);
7589 __ B(slow_path->GetEntryLabel());
7590 break;
7591 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007592 }
7593
7594 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007595
7596 if (slow_path != nullptr) {
7597 __ Bind(slow_path->GetExitLabel());
7598 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007599}
7600
7601void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007602 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007603 locations->SetOut(Location::ConstantLocation(constant));
7604}
7605
7606void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7607 // Will be generated at use site.
7608}
7609
7610void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007611 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007612 locations->SetOut(Location::ConstantLocation(constant));
7613}
7614
7615void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7616 // Will be generated at use site.
7617}
7618
7619void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7620 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7621 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7622}
7623
7624void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7625 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007626 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007627 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007628 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007629}
7630
7631void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7632 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7633 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007634 Location receiver = invoke->GetLocations()->InAt(0);
7635 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007636 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007637
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007638 // temp = object->GetClass();
7639 if (receiver.IsStackSlot()) {
7640 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7641 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7642 } else {
7643 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7644 }
7645 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007646 // Instead of simply (possibly) unpoisoning `temp` here, we should
7647 // emit a read barrier for the previous class reference load.
7648 // However this is not required in practice, as this is an
7649 // intermediate/temporary reference and because the current
7650 // concurrent copying collector keeps the from-space memory
7651 // intact/accessible until the end of the marking phase (the
7652 // concurrent copying collector may not in the future).
7653 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007654 __ LoadFromOffset(kLoadWord, temp, temp,
7655 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7656 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007657 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007658 // temp = temp->GetImtEntryAt(method_offset);
7659 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7660 // T9 = temp->GetEntryPoint();
7661 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
Lena Djokic3177e102018-02-28 11:32:40 +01007662 // Set the hidden argument.
7663 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7664 invoke->GetDexMethodIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007665 // T9();
7666 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007667 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007668 DCHECK(!codegen_->IsLeafMethod());
7669 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7670}
7671
7672void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007673 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7674 if (intrinsic.TryDispatch(invoke)) {
7675 return;
7676 }
7677
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007678 HandleInvoke(invoke);
7679}
7680
7681void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007682 // Explicit clinit checks triggered by static invokes must have been pruned by
7683 // art::PrepareForRegisterAllocation.
7684 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007685
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007686 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007687 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7688 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007689
Chris Larsen701566a2015-10-27 15:29:13 -07007690 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7691 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007692 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7693 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7694 }
Chris Larsen701566a2015-10-27 15:29:13 -07007695 return;
7696 }
7697
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007698 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007699
7700 // Add the extra input register if either the dex cache array base register
7701 // or the PC-relative base register for accessing literals is needed.
7702 if (has_extra_input) {
7703 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7704 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007705}
7706
Orion Hodsonac141392017-01-13 11:53:47 +00007707void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7708 HandleInvoke(invoke);
7709}
7710
7711void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7712 codegen_->GenerateInvokePolymorphicCall(invoke);
7713}
7714
Chris Larsen701566a2015-10-27 15:29:13 -07007715static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007716 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007717 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7718 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007719 return true;
7720 }
7721 return false;
7722}
7723
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007724HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007725 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007726 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007727 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007728 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007729 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007730 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007731 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007732 case HLoadString::LoadKind::kJitTableAddress:
7733 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007734 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007735 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007736 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007737 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007738 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007739 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007740}
7741
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007742HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7743 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007744 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007745 case HLoadClass::LoadKind::kInvalid:
7746 LOG(FATAL) << "UNREACHABLE";
7747 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007748 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007749 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007750 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007751 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007752 case HLoadClass::LoadKind::kBssEntry:
7753 DCHECK(!Runtime::Current()->UseJitCompilation());
7754 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007755 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007756 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007757 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007758 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007759 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007760 break;
7761 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007762 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007763}
7764
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007765Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7766 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007767 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007768 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007769 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7770 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7771 if (!invoke->GetLocations()->Intrinsified()) {
7772 return location.AsRegister<Register>();
7773 }
7774 // For intrinsics we allow any location, so it may be on the stack.
7775 if (!location.IsRegister()) {
7776 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7777 return temp;
7778 }
7779 // For register locations, check if the register was saved. If so, get it from the stack.
7780 // Note: There is a chance that the register was saved but not overwritten, so we could
7781 // save one load. However, since this is just an intrinsic slow path we prefer this
7782 // simple and more robust approach rather that trying to determine if that's the case.
7783 SlowPathCode* slow_path = GetCurrentSlowPath();
7784 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7785 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7786 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7787 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7788 return temp;
7789 }
7790 return location.AsRegister<Register>();
7791}
7792
Vladimir Markodc151b22015-10-15 18:02:30 +01007793HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7794 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007795 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007796 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007797}
7798
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007799void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7800 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007801 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007802 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007803 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7804 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007805 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007806 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7807 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007808 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7809 : ZERO;
7810
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007811 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007812 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007813 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007814 uint32_t offset =
7815 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007816 __ LoadFromOffset(kLoadWord,
7817 temp.AsRegister<Register>(),
7818 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007819 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007820 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007821 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007822 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007823 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007824 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007825 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7826 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007827 PcRelativePatchInfo* info_high = NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007828 PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007829 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007830 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007831 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7832 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007833 break;
7834 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007835 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7836 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7837 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007838 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007839 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007840 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007841 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7842 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007843 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007844 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7845 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007846 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007847 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007848 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7849 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7850 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007851 }
7852 }
7853
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007854 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007855 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007856 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007857 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007858 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7859 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007860 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007861 T9,
7862 callee_method.AsRegister<Register>(),
7863 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007864 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007865 // T9()
7866 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007867 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007868 break;
7869 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007870 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7871
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007872 DCHECK(!IsLeafMethod());
7873}
7874
7875void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007876 // Explicit clinit checks triggered by static invokes must have been pruned by
7877 // art::PrepareForRegisterAllocation.
7878 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007879
7880 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7881 return;
7882 }
7883
7884 LocationSummary* locations = invoke->GetLocations();
7885 codegen_->GenerateStaticOrDirectCall(invoke,
7886 locations->HasTemps()
7887 ? locations->GetTemp(0)
7888 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007889}
7890
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007891void CodeGeneratorMIPS::GenerateVirtualCall(
7892 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007893 // Use the calling convention instead of the location of the receiver, as
7894 // intrinsics may have put the receiver in a different register. In the intrinsics
7895 // slow path, the arguments have been moved to the right place, so here we are
7896 // guaranteed that the receiver is the first register of the calling convention.
7897 InvokeDexCallingConvention calling_convention;
7898 Register receiver = calling_convention.GetRegisterAt(0);
7899
Chris Larsen3acee732015-11-18 13:31:08 -08007900 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007901 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7902 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7903 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007904 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007905
7906 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007907 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007908 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007909 // Instead of simply (possibly) unpoisoning `temp` here, we should
7910 // emit a read barrier for the previous class reference load.
7911 // However this is not required in practice, as this is an
7912 // intermediate/temporary reference and because the current
7913 // concurrent copying collector keeps the from-space memory
7914 // intact/accessible until the end of the marking phase (the
7915 // concurrent copying collector may not in the future).
7916 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007917 // temp = temp->GetMethodAt(method_offset);
7918 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7919 // T9 = temp->GetEntryPoint();
7920 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7921 // T9();
7922 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007923 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007924 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007925}
7926
7927void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7928 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7929 return;
7930 }
7931
7932 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007933 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007934}
7935
7936void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007937 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007938 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007939 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007940 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7941 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007942 return;
7943 }
Vladimir Marko41559982017-01-06 14:04:23 +00007944 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007945 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007946 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007947 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7948 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007949 ? LocationSummary::kCallOnSlowPath
7950 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007951 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007952 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7953 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7954 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007955 switch (load_kind) {
7956 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007957 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007958 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007959 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007960 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007961 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007962 break;
7963 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007964 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007965 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7966 codegen_->ClobberRA();
7967 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007968 break;
7969 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007970 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007971 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007972 locations->SetInAt(0, Location::RequiresRegister());
7973 break;
7974 default:
7975 break;
7976 }
7977 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007978 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7979 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7980 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007981 RegisterSet caller_saves = RegisterSet::Empty();
7982 InvokeRuntimeCallingConvention calling_convention;
7983 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7984 locations->SetCustomSlowPathCallerSaves(caller_saves);
7985 } else {
7986 // For non-Baker read barriers we have a temp-clobbering call.
7987 }
7988 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007989}
7990
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007991// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7992// move.
7993void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007994 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007995 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007996 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007997 return;
7998 }
Vladimir Marko41559982017-01-06 14:04:23 +00007999 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008000
Vladimir Marko41559982017-01-06 14:04:23 +00008001 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008002 Location out_loc = locations->Out();
8003 Register out = out_loc.AsRegister<Register>();
8004 Register base_or_current_method_reg;
8005 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008006 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008007 switch (load_kind) {
8008 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008009 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008010 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008011 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008012 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008013 base_or_current_method_reg =
8014 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008015 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008016 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008017 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008018 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8019 break;
8020 default:
8021 base_or_current_method_reg = ZERO;
8022 break;
8023 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008024
Alexey Frunze15958152017-02-09 19:08:30 -08008025 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8026 ? kWithoutReadBarrier
8027 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008028 bool generate_null_check = false;
8029 switch (load_kind) {
8030 case HLoadClass::LoadKind::kReferrersClass: {
8031 DCHECK(!cls->CanCallRuntime());
8032 DCHECK(!cls->MustGenerateClinitCheck());
8033 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8034 GenerateGcRootFieldLoad(cls,
8035 out_loc,
8036 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008037 ArtMethod::DeclaringClassOffset().Int32Value(),
8038 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008039 break;
8040 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008041 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008042 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008043 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008044 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008045 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008046 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008047 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008048 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8049 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008050 base_or_current_method_reg);
8051 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008052 break;
8053 }
8054 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08008055 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008056 uint32_t address = dchecked_integral_cast<uint32_t>(
8057 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
8058 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008059 if (isR6 || !has_irreducible_loops) {
8060 __ LoadLiteral(out,
8061 base_or_current_method_reg,
8062 codegen_->DeduplicateBootImageAddressLiteral(address));
8063 } else {
8064 __ LoadConst32(out, address);
8065 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008066 break;
8067 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008068 case HLoadClass::LoadKind::kBootImageClassTable: {
8069 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8070 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008071 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008072 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008073 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008074 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8075 out,
8076 base_or_current_method_reg);
8077 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8078 // Extract the reference from the slot data, i.e. clear the hash bits.
8079 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
8080 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
8081 if (masked_hash != 0) {
8082 __ Addiu(out, out, -masked_hash);
8083 }
8084 break;
8085 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008086 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008087 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8088 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008089 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8090 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008091 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008092 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008093 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008094 GenerateGcRootFieldLoad(cls,
8095 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008096 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008097 /* placeholder */ 0x5678,
8098 read_barrier_option,
8099 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008100 generate_null_check = true;
8101 break;
8102 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008103 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008104 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8105 cls->GetTypeIndex(),
8106 cls->GetClass());
8107 bool reordering = __ SetReorder(false);
8108 __ Bind(&info->high_label);
8109 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008110 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008111 GenerateGcRootFieldLoad(cls,
8112 out_loc,
8113 out,
8114 /* placeholder */ 0x5678,
8115 read_barrier_option,
8116 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008117 break;
8118 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008119 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008120 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008121 LOG(FATAL) << "UNREACHABLE";
8122 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008123 }
8124
8125 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8126 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008127 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00008128 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07008129 codegen_->AddSlowPath(slow_path);
8130 if (generate_null_check) {
8131 __ Beqz(out, slow_path->GetEntryLabel());
8132 }
8133 if (cls->MustGenerateClinitCheck()) {
8134 GenerateClassInitializationCheck(slow_path, out);
8135 } else {
8136 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008137 }
8138 }
8139}
8140
8141static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008142 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008143}
8144
8145void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8146 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008147 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008148 locations->SetOut(Location::RequiresRegister());
8149}
8150
8151void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8152 Register out = load->GetLocations()->Out().AsRegister<Register>();
8153 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8154}
8155
8156void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008157 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008158}
8159
8160void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8161 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8162}
8163
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008164void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008165 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008166 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008167 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008168 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008169 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008170 switch (load_kind) {
8171 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008172 case HLoadString::LoadKind::kBootImageAddress:
8173 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008174 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008175 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008176 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008177 break;
8178 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008179 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008180 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
8181 codegen_->ClobberRA();
8182 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008183 break;
8184 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008185 FALLTHROUGH_INTENDED;
8186 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008187 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008188 locations->SetInAt(0, Location::RequiresRegister());
8189 break;
8190 default:
8191 break;
8192 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008193 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008194 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008195 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008196 } else {
8197 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008198 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8199 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8200 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008201 RegisterSet caller_saves = RegisterSet::Empty();
8202 InvokeRuntimeCallingConvention calling_convention;
8203 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8204 locations->SetCustomSlowPathCallerSaves(caller_saves);
8205 } else {
8206 // For non-Baker read barriers we have a temp-clobbering call.
8207 }
8208 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008209 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008210}
8211
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008212// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8213// move.
8214void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008215 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008216 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008217 Location out_loc = locations->Out();
8218 Register out = out_loc.AsRegister<Register>();
8219 Register base_or_current_method_reg;
8220 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008221 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008222 switch (load_kind) {
8223 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008224 case HLoadString::LoadKind::kBootImageAddress:
8225 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008226 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008227 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008228 base_or_current_method_reg =
8229 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008230 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008231 default:
8232 base_or_current_method_reg = ZERO;
8233 break;
8234 }
8235
8236 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008237 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008238 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008239 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008240 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008241 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008242 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008243 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8244 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008245 base_or_current_method_reg);
8246 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008247 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008248 }
8249 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008250 uint32_t address = dchecked_integral_cast<uint32_t>(
8251 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8252 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008253 if (isR6 || !has_irreducible_loops) {
8254 __ LoadLiteral(out,
8255 base_or_current_method_reg,
8256 codegen_->DeduplicateBootImageAddressLiteral(address));
8257 } else {
8258 __ LoadConst32(out, address);
8259 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008260 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008261 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008262 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008263 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008264 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008265 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008266 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008267 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008268 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8269 out,
8270 base_or_current_method_reg);
8271 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8272 return;
8273 }
8274 case HLoadString::LoadKind::kBssEntry: {
8275 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8276 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8277 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8278 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8279 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008280 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008281 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008282 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008283 GenerateGcRootFieldLoad(load,
8284 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008285 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008286 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008287 kCompilerReadBarrierOption,
8288 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008289 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008290 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008291 codegen_->AddSlowPath(slow_path);
8292 __ Beqz(out, slow_path->GetEntryLabel());
8293 __ Bind(slow_path->GetExitLabel());
8294 return;
8295 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008296 case HLoadString::LoadKind::kJitTableAddress: {
8297 CodeGeneratorMIPS::JitPatchInfo* info =
8298 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8299 load->GetStringIndex(),
8300 load->GetString());
8301 bool reordering = __ SetReorder(false);
8302 __ Bind(&info->high_label);
8303 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008304 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008305 GenerateGcRootFieldLoad(load,
8306 out_loc,
8307 out,
8308 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008309 kCompilerReadBarrierOption,
8310 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008311 return;
8312 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008313 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008314 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008315 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008316
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008317 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008318 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008319 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008320 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008321 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008322 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8323 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008324}
8325
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008326void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008327 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008328 locations->SetOut(Location::ConstantLocation(constant));
8329}
8330
8331void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8332 // Will be generated at use site.
8333}
8334
8335void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008336 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8337 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008338 InvokeRuntimeCallingConvention calling_convention;
8339 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8340}
8341
8342void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8343 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008344 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008345 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8346 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008347 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008348 }
8349 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8350}
8351
8352void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8353 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008354 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008355 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008356 case DataType::Type::kInt32:
8357 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008358 locations->SetInAt(0, Location::RequiresRegister());
8359 locations->SetInAt(1, Location::RequiresRegister());
8360 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8361 break;
8362
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008363 case DataType::Type::kFloat32:
8364 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008365 locations->SetInAt(0, Location::RequiresFpuRegister());
8366 locations->SetInAt(1, Location::RequiresFpuRegister());
8367 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8368 break;
8369
8370 default:
8371 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8372 }
8373}
8374
8375void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008376 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008377 LocationSummary* locations = instruction->GetLocations();
8378 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8379
8380 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008381 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008382 Register dst = locations->Out().AsRegister<Register>();
8383 Register lhs = locations->InAt(0).AsRegister<Register>();
8384 Register rhs = locations->InAt(1).AsRegister<Register>();
8385
8386 if (isR6) {
8387 __ MulR6(dst, lhs, rhs);
8388 } else {
8389 __ MulR2(dst, lhs, rhs);
8390 }
8391 break;
8392 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008393 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008394 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8395 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8396 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8397 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8398 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8399 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8400
8401 // Extra checks to protect caused by the existance of A1_A2.
8402 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8403 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8404 DCHECK_NE(dst_high, lhs_low);
8405 DCHECK_NE(dst_high, rhs_low);
8406
8407 // A_B * C_D
8408 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8409 // dst_lo: [ low(B*D) ]
8410 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8411
8412 if (isR6) {
8413 __ MulR6(TMP, lhs_high, rhs_low);
8414 __ MulR6(dst_high, lhs_low, rhs_high);
8415 __ Addu(dst_high, dst_high, TMP);
8416 __ MuhuR6(TMP, lhs_low, rhs_low);
8417 __ Addu(dst_high, dst_high, TMP);
8418 __ MulR6(dst_low, lhs_low, rhs_low);
8419 } else {
8420 __ MulR2(TMP, lhs_high, rhs_low);
8421 __ MulR2(dst_high, lhs_low, rhs_high);
8422 __ Addu(dst_high, dst_high, TMP);
8423 __ MultuR2(lhs_low, rhs_low);
8424 __ Mfhi(TMP);
8425 __ Addu(dst_high, dst_high, TMP);
8426 __ Mflo(dst_low);
8427 }
8428 break;
8429 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008430 case DataType::Type::kFloat32:
8431 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008432 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8433 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8434 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008435 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008436 __ MulS(dst, lhs, rhs);
8437 } else {
8438 __ MulD(dst, lhs, rhs);
8439 }
8440 break;
8441 }
8442 default:
8443 LOG(FATAL) << "Unexpected mul type " << type;
8444 }
8445}
8446
8447void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8448 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008449 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008450 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008451 case DataType::Type::kInt32:
8452 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008453 locations->SetInAt(0, Location::RequiresRegister());
8454 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8455 break;
8456
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008457 case DataType::Type::kFloat32:
8458 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008459 locations->SetInAt(0, Location::RequiresFpuRegister());
8460 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8461 break;
8462
8463 default:
8464 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8465 }
8466}
8467
8468void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008469 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008470 LocationSummary* locations = instruction->GetLocations();
8471
8472 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008473 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008474 Register dst = locations->Out().AsRegister<Register>();
8475 Register src = locations->InAt(0).AsRegister<Register>();
8476 __ Subu(dst, ZERO, src);
8477 break;
8478 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008479 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008480 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8481 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8482 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8483 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8484 __ Subu(dst_low, ZERO, src_low);
8485 __ Sltu(TMP, ZERO, dst_low);
8486 __ Subu(dst_high, ZERO, src_high);
8487 __ Subu(dst_high, dst_high, TMP);
8488 break;
8489 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008490 case DataType::Type::kFloat32:
8491 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008492 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8493 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008494 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008495 __ NegS(dst, src);
8496 } else {
8497 __ NegD(dst, src);
8498 }
8499 break;
8500 }
8501 default:
8502 LOG(FATAL) << "Unexpected neg type " << type;
8503 }
8504}
8505
8506void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008507 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8508 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008509 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008510 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008511 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8512 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008513}
8514
8515void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008516 // Note: if heap poisoning is enabled, the entry point takes care
8517 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008518 QuickEntrypointEnum entrypoint =
8519 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8520 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008521 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008522 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008523}
8524
8525void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008526 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8527 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008528 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008529 if (instruction->IsStringAlloc()) {
8530 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8531 } else {
8532 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008533 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008534 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008535}
8536
8537void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008538 // Note: if heap poisoning is enabled, the entry point takes care
8539 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008540 if (instruction->IsStringAlloc()) {
8541 // String is allocated through StringFactory. Call NewEmptyString entry point.
8542 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008543 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008544 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8545 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8546 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008547 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008548 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8549 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008550 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008551 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008552 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008553}
8554
8555void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008556 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008557 locations->SetInAt(0, Location::RequiresRegister());
8558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8559}
8560
8561void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008562 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008563 LocationSummary* locations = instruction->GetLocations();
8564
8565 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008566 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008567 Register dst = locations->Out().AsRegister<Register>();
8568 Register src = locations->InAt(0).AsRegister<Register>();
8569 __ Nor(dst, src, ZERO);
8570 break;
8571 }
8572
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008573 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008574 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8575 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8576 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8577 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8578 __ Nor(dst_high, src_high, ZERO);
8579 __ Nor(dst_low, src_low, ZERO);
8580 break;
8581 }
8582
8583 default:
8584 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8585 }
8586}
8587
8588void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008589 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008590 locations->SetInAt(0, Location::RequiresRegister());
8591 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8592}
8593
8594void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8595 LocationSummary* locations = instruction->GetLocations();
8596 __ Xori(locations->Out().AsRegister<Register>(),
8597 locations->InAt(0).AsRegister<Register>(),
8598 1);
8599}
8600
8601void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008602 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8603 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008604}
8605
Calin Juravle2ae48182016-03-16 14:05:09 +00008606void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8607 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008608 return;
8609 }
8610 Location obj = instruction->GetLocations()->InAt(0);
8611
8612 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008613 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008614}
8615
Calin Juravle2ae48182016-03-16 14:05:09 +00008616void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008617 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008618 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008619
8620 Location obj = instruction->GetLocations()->InAt(0);
8621
8622 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8623}
8624
8625void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008626 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008627}
8628
8629void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8630 HandleBinaryOp(instruction);
8631}
8632
8633void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8634 HandleBinaryOp(instruction);
8635}
8636
8637void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8638 LOG(FATAL) << "Unreachable";
8639}
8640
8641void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008642 if (instruction->GetNext()->IsSuspendCheck() &&
8643 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8644 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8645 // The back edge will generate the suspend check.
8646 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8647 }
8648
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008649 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8650}
8651
8652void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008653 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008654 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8655 if (location.IsStackSlot()) {
8656 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8657 } else if (location.IsDoubleStackSlot()) {
8658 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8659 }
8660 locations->SetOut(location);
8661}
8662
8663void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8664 ATTRIBUTE_UNUSED) {
8665 // Nothing to do, the parameter is already at its location.
8666}
8667
8668void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8669 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008670 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008671 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8672}
8673
8674void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8675 ATTRIBUTE_UNUSED) {
8676 // Nothing to do, the method is already at its location.
8677}
8678
8679void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008680 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008681 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008682 locations->SetInAt(i, Location::Any());
8683 }
8684 locations->SetOut(Location::Any());
8685}
8686
8687void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8688 LOG(FATAL) << "Unreachable";
8689}
8690
8691void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008692 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008693 bool call_rem;
8694 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8695 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8696 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8697 } else {
8698 call_rem = (type != DataType::Type::kInt32);
8699 }
8700 LocationSummary::CallKind call_kind = call_rem
8701 ? LocationSummary::kCallOnMainOnly
8702 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008703 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008704
8705 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008706 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008707 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008708 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008709 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8710 break;
8711
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008712 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008713 if (call_rem) {
8714 InvokeRuntimeCallingConvention calling_convention;
8715 locations->SetInAt(0, Location::RegisterPairLocation(
8716 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8717 locations->SetInAt(1, Location::RegisterPairLocation(
8718 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8719 locations->SetOut(calling_convention.GetReturnLocation(type));
8720 } else {
8721 locations->SetInAt(0, Location::RequiresRegister());
8722 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8723 locations->SetOut(Location::RequiresRegister());
8724 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008725 break;
8726 }
8727
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008728 case DataType::Type::kFloat32:
8729 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008730 InvokeRuntimeCallingConvention calling_convention;
8731 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8732 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8733 locations->SetOut(calling_convention.GetReturnLocation(type));
8734 break;
8735 }
8736
8737 default:
8738 LOG(FATAL) << "Unexpected rem type " << type;
8739 }
8740}
8741
8742void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008743 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008744 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008745
8746 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008747 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008748 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008749 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008750 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008751 if (locations->InAt(1).IsConstant()) {
8752 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8753 if (imm == 0) {
8754 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8755 } else if (imm == 1 || imm == -1) {
8756 DivRemOneOrMinusOne(instruction);
8757 } else {
8758 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8759 DivRemByPowerOfTwo(instruction);
8760 }
8761 } else {
8762 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8763 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8764 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008765 break;
8766 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008767 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008768 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008769 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008770 break;
8771 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008772 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008773 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008774 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008775 break;
8776 }
8777 default:
8778 LOG(FATAL) << "Unexpected rem type " << type;
8779 }
8780}
8781
Aart Bik1f8d51b2018-02-15 10:42:37 -08008782static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
8783 LocationSummary* locations = new (allocator) LocationSummary(minmax);
8784 switch (minmax->GetResultType()) {
8785 case DataType::Type::kInt32:
8786 case DataType::Type::kInt64:
8787 locations->SetInAt(0, Location::RequiresRegister());
8788 locations->SetInAt(1, Location::RequiresRegister());
8789 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8790 break;
8791 case DataType::Type::kFloat32:
8792 case DataType::Type::kFloat64:
8793 locations->SetInAt(0, Location::RequiresFpuRegister());
8794 locations->SetInAt(1, Location::RequiresFpuRegister());
8795 locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap);
8796 break;
8797 default:
8798 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
8799 }
8800}
8801
8802void InstructionCodeGeneratorMIPS::GenerateMinMax(LocationSummary* locations,
8803 bool is_min,
8804 bool isR6,
8805 DataType::Type type) {
8806 if (isR6) {
8807 // Some architectures, such as ARM and MIPS (prior to r6), have a
8808 // conditional move instruction which only changes the target
8809 // (output) register if the condition is true (MIPS prior to r6 had
8810 // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions
8811 // always change the target (output) register. If the condition is
8812 // true the output register gets the contents of the "rs" register;
8813 // otherwise, the output register is set to zero. One consequence
8814 // of this is that to implement something like "rd = c==0 ? rs : rt"
8815 // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions.
8816 // After executing this pair of instructions one of the output
8817 // registers from the pair will necessarily contain zero. Then the
8818 // code ORs the output registers from the SELEQZ/SELNEZ instructions
8819 // to get the final result.
8820 //
8821 // The initial test to see if the output register is same as the
8822 // first input register is needed to make sure that value in the
8823 // first input register isn't clobbered before we've finished
8824 // computing the output value. The logic in the corresponding else
8825 // clause performs the same task but makes sure the second input
8826 // register isn't clobbered in the event that it's the same register
8827 // as the output register; the else clause also handles the case
8828 // where the output register is distinct from both the first, and the
8829 // second input registers.
8830 if (type == DataType::Type::kInt64) {
8831 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8832 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8833 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
8834 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
8835 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8836 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8837
8838 MipsLabel compare_done;
8839
8840 if (a_lo == b_lo) {
8841 if (out_lo != a_lo) {
8842 __ Move(out_lo, a_lo);
8843 __ Move(out_hi, a_hi);
8844 }
8845 } else {
8846 __ Slt(TMP, b_hi, a_hi);
8847 __ Bne(b_hi, a_hi, &compare_done);
8848
8849 __ Sltu(TMP, b_lo, a_lo);
8850
8851 __ Bind(&compare_done);
8852
8853 if (is_min) {
8854 __ Seleqz(AT, a_lo, TMP);
8855 __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo
8856 // because at this point we're
8857 // done using a_lo/b_lo.
8858 } else {
8859 __ Selnez(AT, a_lo, TMP);
8860 __ Seleqz(out_lo, b_lo, TMP); // ditto
8861 }
8862 __ Or(out_lo, out_lo, AT);
8863 if (is_min) {
8864 __ Seleqz(AT, a_hi, TMP);
8865 __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
8866 } else {
8867 __ Selnez(AT, a_hi, TMP);
8868 __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
8869 }
8870 __ Or(out_hi, out_hi, AT);
8871 }
8872 } else {
8873 DCHECK_EQ(type, DataType::Type::kInt32);
8874 Register a = locations->InAt(0).AsRegister<Register>();
8875 Register b = locations->InAt(1).AsRegister<Register>();
8876 Register out = locations->Out().AsRegister<Register>();
8877
8878 if (a == b) {
8879 if (out != a) {
8880 __ Move(out, a);
8881 }
8882 } else {
8883 __ Slt(AT, b, a);
8884 if (is_min) {
8885 __ Seleqz(TMP, a, AT);
8886 __ Selnez(AT, b, AT);
8887 } else {
8888 __ Selnez(TMP, a, AT);
8889 __ Seleqz(AT, b, AT);
8890 }
8891 __ Or(out, TMP, AT);
8892 }
8893 }
8894 } else { // !isR6
8895 if (type == DataType::Type::kInt64) {
8896 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8897 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8898 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
8899 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
8900 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8901 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8902
8903 MipsLabel compare_done;
8904
8905 if (a_lo == b_lo) {
8906 if (out_lo != a_lo) {
8907 __ Move(out_lo, a_lo);
8908 __ Move(out_hi, a_hi);
8909 }
8910 } else {
8911 __ Slt(TMP, a_hi, b_hi);
8912 __ Bne(a_hi, b_hi, &compare_done);
8913
8914 __ Sltu(TMP, a_lo, b_lo);
8915
8916 __ Bind(&compare_done);
8917
8918 if (is_min) {
8919 if (out_lo != a_lo) {
8920 __ Movn(out_hi, a_hi, TMP);
8921 __ Movn(out_lo, a_lo, TMP);
8922 }
8923 if (out_lo != b_lo) {
8924 __ Movz(out_hi, b_hi, TMP);
8925 __ Movz(out_lo, b_lo, TMP);
8926 }
8927 } else {
8928 if (out_lo != a_lo) {
8929 __ Movz(out_hi, a_hi, TMP);
8930 __ Movz(out_lo, a_lo, TMP);
8931 }
8932 if (out_lo != b_lo) {
8933 __ Movn(out_hi, b_hi, TMP);
8934 __ Movn(out_lo, b_lo, TMP);
8935 }
8936 }
8937 }
8938 } else {
8939 DCHECK_EQ(type, DataType::Type::kInt32);
8940 Register a = locations->InAt(0).AsRegister<Register>();
8941 Register b = locations->InAt(1).AsRegister<Register>();
8942 Register out = locations->Out().AsRegister<Register>();
8943
8944 if (a == b) {
8945 if (out != a) {
8946 __ Move(out, a);
8947 }
8948 } else {
8949 __ Slt(AT, a, b);
8950 if (is_min) {
8951 if (out != a) {
8952 __ Movn(out, a, AT);
8953 }
8954 if (out != b) {
8955 __ Movz(out, b, AT);
8956 }
8957 } else {
8958 if (out != a) {
8959 __ Movz(out, a, AT);
8960 }
8961 if (out != b) {
8962 __ Movn(out, b, AT);
8963 }
8964 }
8965 }
8966 }
8967 }
8968}
8969
8970void InstructionCodeGeneratorMIPS::GenerateMinMaxFP(LocationSummary* locations,
8971 bool is_min,
8972 bool isR6,
8973 DataType::Type type) {
8974 FRegister out = locations->Out().AsFpuRegister<FRegister>();
8975 FRegister a = locations->InAt(0).AsFpuRegister<FRegister>();
8976 FRegister b = locations->InAt(1).AsFpuRegister<FRegister>();
8977
8978 if (isR6) {
8979 MipsLabel noNaNs;
8980 MipsLabel done;
8981 FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
8982
8983 // When Java computes min/max it prefers a NaN to a number; the
8984 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
8985 // the inputs is a NaN and the other is a valid number, the MIPS
8986 // instruction will return the number; Java wants the NaN value
8987 // returned. This is why there is extra logic preceding the use of
8988 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
8989 // NaN, return the NaN, otherwise return the min/max.
8990 if (type == DataType::Type::kFloat64) {
8991 __ CmpUnD(FTMP, a, b);
8992 __ Bc1eqz(FTMP, &noNaNs);
8993
8994 // One of the inputs is a NaN
8995 __ CmpEqD(ftmp, a, a);
8996 // If a == a then b is the NaN, otherwise a is the NaN.
8997 __ SelD(ftmp, a, b);
8998
8999 if (ftmp != out) {
9000 __ MovD(out, ftmp);
9001 }
9002
9003 __ B(&done);
9004
9005 __ Bind(&noNaNs);
9006
9007 if (is_min) {
9008 __ MinD(out, a, b);
9009 } else {
9010 __ MaxD(out, a, b);
9011 }
9012 } else {
9013 DCHECK_EQ(type, DataType::Type::kFloat32);
9014 __ CmpUnS(FTMP, a, b);
9015 __ Bc1eqz(FTMP, &noNaNs);
9016
9017 // One of the inputs is a NaN
9018 __ CmpEqS(ftmp, a, a);
9019 // If a == a then b is the NaN, otherwise a is the NaN.
9020 __ SelS(ftmp, a, b);
9021
9022 if (ftmp != out) {
9023 __ MovS(out, ftmp);
9024 }
9025
9026 __ B(&done);
9027
9028 __ Bind(&noNaNs);
9029
9030 if (is_min) {
9031 __ MinS(out, a, b);
9032 } else {
9033 __ MaxS(out, a, b);
9034 }
9035 }
9036
9037 __ Bind(&done);
9038
9039 } else { // !isR6
9040 MipsLabel ordered;
9041 MipsLabel compare;
9042 MipsLabel select;
9043 MipsLabel done;
9044
9045 if (type == DataType::Type::kFloat64) {
9046 __ CunD(a, b);
9047 } else {
9048 DCHECK_EQ(type, DataType::Type::kFloat32);
9049 __ CunS(a, b);
9050 }
9051 __ Bc1f(&ordered);
9052
9053 // a or b (or both) is a NaN. Return one, which is a NaN.
9054 if (type == DataType::Type::kFloat64) {
9055 __ CeqD(b, b);
9056 } else {
9057 __ CeqS(b, b);
9058 }
9059 __ B(&select);
9060
9061 __ Bind(&ordered);
9062
9063 // Neither is a NaN.
9064 // a == b? (-0.0 compares equal with +0.0)
9065 // If equal, handle zeroes, else compare further.
9066 if (type == DataType::Type::kFloat64) {
9067 __ CeqD(a, b);
9068 } else {
9069 __ CeqS(a, b);
9070 }
9071 __ Bc1f(&compare);
9072
9073 // a == b either bit for bit or one is -0.0 and the other is +0.0.
9074 if (type == DataType::Type::kFloat64) {
9075 __ MoveFromFpuHigh(TMP, a);
9076 __ MoveFromFpuHigh(AT, b);
9077 } else {
9078 __ Mfc1(TMP, a);
9079 __ Mfc1(AT, b);
9080 }
9081
9082 if (is_min) {
9083 // -0.0 prevails over +0.0.
9084 __ Or(TMP, TMP, AT);
9085 } else {
9086 // +0.0 prevails over -0.0.
9087 __ And(TMP, TMP, AT);
9088 }
9089
9090 if (type == DataType::Type::kFloat64) {
9091 __ Mfc1(AT, a);
9092 __ Mtc1(AT, out);
9093 __ MoveToFpuHigh(TMP, out);
9094 } else {
9095 __ Mtc1(TMP, out);
9096 }
9097 __ B(&done);
9098
9099 __ Bind(&compare);
9100
9101 if (type == DataType::Type::kFloat64) {
9102 if (is_min) {
9103 // return (a <= b) ? a : b;
9104 __ ColeD(a, b);
9105 } else {
9106 // return (a >= b) ? a : b;
9107 __ ColeD(b, a); // b <= a
9108 }
9109 } else {
9110 if (is_min) {
9111 // return (a <= b) ? a : b;
9112 __ ColeS(a, b);
9113 } else {
9114 // return (a >= b) ? a : b;
9115 __ ColeS(b, a); // b <= a
9116 }
9117 }
9118
9119 __ Bind(&select);
9120
9121 if (type == DataType::Type::kFloat64) {
9122 __ MovtD(out, a);
9123 __ MovfD(out, b);
9124 } else {
9125 __ MovtS(out, a);
9126 __ MovfS(out, b);
9127 }
9128
9129 __ Bind(&done);
9130 }
9131}
9132
9133void LocationsBuilderMIPS::VisitMin(HMin* min) {
9134 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
9135}
9136
9137void InstructionCodeGeneratorMIPS::VisitMin(HMin* min) {
9138 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9139 switch (min->GetResultType()) {
9140 case DataType::Type::kInt32:
9141 case DataType::Type::kInt64:
9142 GenerateMinMax(min->GetLocations(), /*is_min*/ true, isR6, min->GetResultType());
9143 break;
9144 case DataType::Type::kFloat32:
9145 case DataType::Type::kFloat64:
9146 GenerateMinMaxFP(min->GetLocations(), /*is_min*/ true, isR6, min->GetResultType());
9147 break;
9148 default:
9149 LOG(FATAL) << "Unexpected type for HMin " << min->GetResultType();
9150 }
9151}
9152
9153void LocationsBuilderMIPS::VisitMax(HMax* max) {
9154 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
9155}
9156
9157void InstructionCodeGeneratorMIPS::VisitMax(HMax* max) {
9158 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9159 switch (max->GetResultType()) {
9160 case DataType::Type::kInt32:
9161 case DataType::Type::kInt64:
9162 GenerateMinMax(max->GetLocations(), /*is_min*/ false, isR6, max->GetResultType());
9163 break;
9164 case DataType::Type::kFloat32:
9165 case DataType::Type::kFloat64:
9166 GenerateMinMaxFP(max->GetLocations(), /*is_min*/ false, isR6, max->GetResultType());
9167 break;
9168 default:
9169 LOG(FATAL) << "Unexpected type for HMax " << max->GetResultType();
9170 }
9171}
9172
Aart Bik3dad3412018-02-28 12:01:46 -08009173void LocationsBuilderMIPS::VisitAbs(HAbs* abs) {
9174 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
9175 switch (abs->GetResultType()) {
9176 case DataType::Type::kInt32:
9177 case DataType::Type::kInt64:
9178 locations->SetInAt(0, Location::RequiresRegister());
9179 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9180 break;
9181 case DataType::Type::kFloat32:
9182 case DataType::Type::kFloat64:
9183 locations->SetInAt(0, Location::RequiresFpuRegister());
9184 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9185 break;
9186 default:
9187 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9188 }
9189}
9190
9191void InstructionCodeGeneratorMIPS::GenerateAbsFP(LocationSummary* locations,
9192 DataType::Type type,
9193 bool isR2OrNewer,
9194 bool isR6) {
9195 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
9196 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9197
9198 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
9199 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
9200 // (signaling NaN may become quiet though).
9201 //
9202 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
9203 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
9204 // affected by this instruction.
9205 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
9206 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
9207 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
9208 if (isR6) {
9209 if (type == DataType::Type::kFloat64) {
9210 __ AbsD(out, in);
9211 } else {
9212 DCHECK_EQ(type, DataType::Type::kFloat32);
9213 __ AbsS(out, in);
9214 }
9215 } else {
9216 if (type == DataType::Type::kFloat64) {
9217 if (in != out) {
9218 __ MovD(out, in);
9219 }
9220 __ MoveFromFpuHigh(TMP, in);
9221 // ins instruction is not available for R1.
9222 if (isR2OrNewer) {
9223 __ Ins(TMP, ZERO, 31, 1);
9224 } else {
9225 __ Sll(TMP, TMP, 1);
9226 __ Srl(TMP, TMP, 1);
9227 }
9228 __ MoveToFpuHigh(TMP, out);
9229 } else {
9230 DCHECK_EQ(type, DataType::Type::kFloat32);
9231 __ Mfc1(TMP, in);
9232 // ins instruction is not available for R1.
9233 if (isR2OrNewer) {
9234 __ Ins(TMP, ZERO, 31, 1);
9235 } else {
9236 __ Sll(TMP, TMP, 1);
9237 __ Srl(TMP, TMP, 1);
9238 }
9239 __ Mtc1(TMP, out);
9240 }
9241 }
9242}
9243
9244void InstructionCodeGeneratorMIPS::VisitAbs(HAbs* abs) {
9245 LocationSummary* locations = abs->GetLocations();
9246 bool isR2OrNewer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
9247 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9248 switch (abs->GetResultType()) {
9249 case DataType::Type::kInt32: {
9250 Register in = locations->InAt(0).AsRegister<Register>();
9251 Register out = locations->Out().AsRegister<Register>();
9252 __ Sra(AT, in, 31);
9253 __ Xor(out, in, AT);
9254 __ Subu(out, out, AT);
9255 break;
9256 }
9257 case DataType::Type::kInt64: {
9258 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9259 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9260 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9261 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9262 // The comments in this section show the analogous operations which would
9263 // be performed if we had 64-bit registers "in", and "out".
9264 // __ Dsra32(AT, in, 31);
9265 __ Sra(AT, in_hi, 31);
9266 // __ Xor(out, in, AT);
9267 __ Xor(TMP, in_lo, AT);
9268 __ Xor(out_hi, in_hi, AT);
9269 // __ Dsubu(out, out, AT);
9270 __ Subu(out_lo, TMP, AT);
9271 __ Sltu(TMP, out_lo, TMP);
9272 __ Addu(out_hi, out_hi, TMP);
9273 break;
9274 }
9275 case DataType::Type::kFloat32:
9276 case DataType::Type::kFloat64:
9277 GenerateAbsFP(locations, abs->GetResultType(), isR2OrNewer, isR6);
9278 break;
9279 default:
9280 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9281 }
9282}
9283
Igor Murashkind01745e2017-04-05 16:40:31 -07009284void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
9285 constructor_fence->SetLocations(nullptr);
9286}
9287
9288void InstructionCodeGeneratorMIPS::VisitConstructorFence(
9289 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
9290 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
9291}
9292
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009293void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9294 memory_barrier->SetLocations(nullptr);
9295}
9296
9297void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9298 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
9299}
9300
9301void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009302 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009303 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009304 locations->SetInAt(0, MipsReturnLocation(return_type));
9305}
9306
9307void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
9308 codegen_->GenerateFrameExit();
9309}
9310
9311void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
9312 ret->SetLocations(nullptr);
9313}
9314
9315void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
9316 codegen_->GenerateFrameExit();
9317}
9318
Alexey Frunze92d90602015-12-18 18:16:36 -08009319void LocationsBuilderMIPS::VisitRor(HRor* ror) {
9320 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009321}
9322
Alexey Frunze92d90602015-12-18 18:16:36 -08009323void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
9324 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009325}
9326
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009327void LocationsBuilderMIPS::VisitShl(HShl* shl) {
9328 HandleShift(shl);
9329}
9330
9331void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
9332 HandleShift(shl);
9333}
9334
9335void LocationsBuilderMIPS::VisitShr(HShr* shr) {
9336 HandleShift(shr);
9337}
9338
9339void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
9340 HandleShift(shr);
9341}
9342
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009343void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
9344 HandleBinaryOp(instruction);
9345}
9346
9347void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
9348 HandleBinaryOp(instruction);
9349}
9350
9351void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9352 HandleFieldGet(instruction, instruction->GetFieldInfo());
9353}
9354
9355void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9356 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
9357}
9358
9359void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
9360 HandleFieldSet(instruction, instruction->GetFieldInfo());
9361}
9362
9363void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01009364 HandleFieldSet(instruction,
9365 instruction->GetFieldInfo(),
9366 instruction->GetDexPc(),
9367 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009368}
9369
9370void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
9371 HUnresolvedInstanceFieldGet* instruction) {
9372 FieldAccessCallingConventionMIPS calling_convention;
9373 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9374 instruction->GetFieldType(),
9375 calling_convention);
9376}
9377
9378void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
9379 HUnresolvedInstanceFieldGet* instruction) {
9380 FieldAccessCallingConventionMIPS calling_convention;
9381 codegen_->GenerateUnresolvedFieldAccess(instruction,
9382 instruction->GetFieldType(),
9383 instruction->GetFieldIndex(),
9384 instruction->GetDexPc(),
9385 calling_convention);
9386}
9387
9388void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
9389 HUnresolvedInstanceFieldSet* instruction) {
9390 FieldAccessCallingConventionMIPS calling_convention;
9391 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9392 instruction->GetFieldType(),
9393 calling_convention);
9394}
9395
9396void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
9397 HUnresolvedInstanceFieldSet* instruction) {
9398 FieldAccessCallingConventionMIPS calling_convention;
9399 codegen_->GenerateUnresolvedFieldAccess(instruction,
9400 instruction->GetFieldType(),
9401 instruction->GetFieldIndex(),
9402 instruction->GetDexPc(),
9403 calling_convention);
9404}
9405
9406void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
9407 HUnresolvedStaticFieldGet* instruction) {
9408 FieldAccessCallingConventionMIPS calling_convention;
9409 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9410 instruction->GetFieldType(),
9411 calling_convention);
9412}
9413
9414void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
9415 HUnresolvedStaticFieldGet* instruction) {
9416 FieldAccessCallingConventionMIPS calling_convention;
9417 codegen_->GenerateUnresolvedFieldAccess(instruction,
9418 instruction->GetFieldType(),
9419 instruction->GetFieldIndex(),
9420 instruction->GetDexPc(),
9421 calling_convention);
9422}
9423
9424void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
9425 HUnresolvedStaticFieldSet* instruction) {
9426 FieldAccessCallingConventionMIPS calling_convention;
9427 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9428 instruction->GetFieldType(),
9429 calling_convention);
9430}
9431
9432void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9433 HUnresolvedStaticFieldSet* instruction) {
9434 FieldAccessCallingConventionMIPS calling_convention;
9435 codegen_->GenerateUnresolvedFieldAccess(instruction,
9436 instruction->GetFieldType(),
9437 instruction->GetFieldIndex(),
9438 instruction->GetDexPc(),
9439 calling_convention);
9440}
9441
9442void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009443 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9444 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009445 // In suspend check slow path, usually there are no caller-save registers at all.
9446 // If SIMD instructions are present, however, we force spilling all live SIMD
9447 // registers in full width (since the runtime only saves/restores lower part).
9448 locations->SetCustomSlowPathCallerSaves(
9449 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009450}
9451
9452void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9453 HBasicBlock* block = instruction->GetBlock();
9454 if (block->GetLoopInformation() != nullptr) {
9455 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9456 // The back edge will generate the suspend check.
9457 return;
9458 }
9459 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9460 // The goto will generate the suspend check.
9461 return;
9462 }
9463 GenerateSuspendCheck(instruction, nullptr);
9464}
9465
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009466void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009467 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9468 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009469 InvokeRuntimeCallingConvention calling_convention;
9470 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9471}
9472
9473void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009474 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009475 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9476}
9477
9478void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009479 DataType::Type input_type = conversion->GetInputType();
9480 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009481 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9482 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009483 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009484
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009485 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9486 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009487 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9488 }
9489
9490 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009491 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009492 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9493 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009494 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009495 }
9496
Vladimir Markoca6fff82017-10-03 14:49:14 +01009497 LocationSummary* locations =
9498 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009499
9500 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009501 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009502 locations->SetInAt(0, Location::RequiresFpuRegister());
9503 } else {
9504 locations->SetInAt(0, Location::RequiresRegister());
9505 }
9506
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009507 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009508 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9509 } else {
9510 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9511 }
9512 } else {
9513 InvokeRuntimeCallingConvention calling_convention;
9514
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009515 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009516 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9517 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009518 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009519 locations->SetInAt(0, Location::RegisterPairLocation(
9520 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9521 }
9522
9523 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9524 }
9525}
9526
9527void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9528 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009529 DataType::Type result_type = conversion->GetResultType();
9530 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009531 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009532 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009533
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009534 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9535 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009536
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009537 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009538 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9539 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9540 Register src = locations->InAt(0).AsRegister<Register>();
9541
Alexey Frunzea871ef12016-06-27 15:20:11 -07009542 if (dst_low != src) {
9543 __ Move(dst_low, src);
9544 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009545 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009546 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009547 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009548 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009549 ? locations->InAt(0).AsRegisterPairLow<Register>()
9550 : locations->InAt(0).AsRegister<Register>();
9551
9552 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009553 case DataType::Type::kUint8:
9554 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009555 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009556 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009557 if (has_sign_extension) {
9558 __ Seb(dst, src);
9559 } else {
9560 __ Sll(dst, src, 24);
9561 __ Sra(dst, dst, 24);
9562 }
9563 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009564 case DataType::Type::kUint16:
9565 __ Andi(dst, src, 0xFFFF);
9566 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009567 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009568 if (has_sign_extension) {
9569 __ Seh(dst, src);
9570 } else {
9571 __ Sll(dst, src, 16);
9572 __ Sra(dst, dst, 16);
9573 }
9574 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009575 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009576 if (dst != src) {
9577 __ Move(dst, src);
9578 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009579 break;
9580
9581 default:
9582 LOG(FATAL) << "Unexpected type conversion from " << input_type
9583 << " to " << result_type;
9584 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009585 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9586 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009587 if (isR6) {
9588 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9589 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9590 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9591 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9592 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9593 __ Mtc1(src_low, FTMP);
9594 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009595 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009596 __ Cvtsl(dst, FTMP);
9597 } else {
9598 __ Cvtdl(dst, FTMP);
9599 }
9600 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009601 QuickEntrypointEnum entrypoint =
9602 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009603 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009604 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009605 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9606 } else {
9607 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9608 }
9609 }
9610 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009611 Register src = locations->InAt(0).AsRegister<Register>();
9612 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9613 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009614 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009615 __ Cvtsw(dst, FTMP);
9616 } else {
9617 __ Cvtdw(dst, FTMP);
9618 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009619 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009620 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9621 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009622
9623 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9624 // value of the output type if the input is outside of the range after the truncation or
9625 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9626 // results. This matches the desired float/double-to-int/long conversion exactly.
9627 //
9628 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9629 // value when the input is either a NaN or is outside of the range of the output type
9630 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9631 // the same result.
9632 //
9633 // The code takes care of the different behaviors by first comparing the input to the
9634 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9635 // If the input is greater than or equal to the minimum, it procedes to the truncate
9636 // instruction, which will handle such an input the same way irrespective of NAN2008.
9637 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9638 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009639 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009640 if (isR6) {
9641 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9642 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9643 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9644 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9645 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009646
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009647 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009648 __ TruncLS(FTMP, src);
9649 } else {
9650 __ TruncLD(FTMP, src);
9651 }
9652 __ Mfc1(dst_low, FTMP);
9653 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009654 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009655 QuickEntrypointEnum entrypoint =
9656 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009657 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009658 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009659 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9660 } else {
9661 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9662 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009663 }
9664 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009665 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9666 Register dst = locations->Out().AsRegister<Register>();
9667 MipsLabel truncate;
9668 MipsLabel done;
9669
Lena Djokicf4e23a82017-05-09 15:43:45 +02009670 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009671 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009672 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9673 __ LoadConst32(TMP, min_val);
9674 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009675 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009676 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9677 __ LoadConst32(TMP, High32Bits(min_val));
9678 __ Mtc1(ZERO, FTMP);
9679 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009680 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009681
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009682 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009683 __ ColeS(0, FTMP, src);
9684 } else {
9685 __ ColeD(0, FTMP, src);
9686 }
9687 __ Bc1t(0, &truncate);
9688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009689 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009690 __ CeqS(0, src, src);
9691 } else {
9692 __ CeqD(0, src, src);
9693 }
9694 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9695 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009696
9697 __ B(&done);
9698
9699 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009700 }
9701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009702 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009703 __ TruncWS(FTMP, src);
9704 } else {
9705 __ TruncWD(FTMP, src);
9706 }
9707 __ Mfc1(dst, FTMP);
9708
Lena Djokicf4e23a82017-05-09 15:43:45 +02009709 if (!isR6) {
9710 __ Bind(&done);
9711 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009712 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009713 } else if (DataType::IsFloatingPointType(result_type) &&
9714 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009715 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9716 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009717 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009718 __ Cvtsd(dst, src);
9719 } else {
9720 __ Cvtds(dst, src);
9721 }
9722 } else {
9723 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9724 << " to " << result_type;
9725 }
9726}
9727
9728void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9729 HandleShift(ushr);
9730}
9731
9732void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9733 HandleShift(ushr);
9734}
9735
9736void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9737 HandleBinaryOp(instruction);
9738}
9739
9740void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9741 HandleBinaryOp(instruction);
9742}
9743
9744void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9745 // Nothing to do, this should be removed during prepare for register allocator.
9746 LOG(FATAL) << "Unreachable";
9747}
9748
9749void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9750 // Nothing to do, this should be removed during prepare for register allocator.
9751 LOG(FATAL) << "Unreachable";
9752}
9753
9754void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009755 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009756}
9757
9758void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009759 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009760}
9761
9762void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009763 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009764}
9765
9766void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009767 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009768}
9769
9770void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009771 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009772}
9773
9774void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009775 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009776}
9777
9778void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009779 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009780}
9781
9782void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009783 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009784}
9785
9786void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009787 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009788}
9789
9790void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009791 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009792}
9793
9794void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009795 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009796}
9797
9798void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009799 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009800}
9801
9802void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009803 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009804}
9805
9806void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009807 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009808}
9809
9810void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009811 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009812}
9813
9814void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009815 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009816}
9817
9818void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009819 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009820}
9821
9822void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009823 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009824}
9825
9826void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009827 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009828}
9829
9830void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009831 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009832}
9833
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009834void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9835 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009836 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009837 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009838 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9839 uint32_t num_entries = switch_instr->GetNumEntries();
9840 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9841 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9842 // instruction to simulate PC-relative addressing when accessing the jump table.
9843 // NAL clobbers RA. Make sure RA is preserved.
9844 codegen_->ClobberRA();
9845 }
9846 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009847}
9848
Alexey Frunze96b66822016-09-10 02:32:44 -07009849void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9850 int32_t lower_bound,
9851 uint32_t num_entries,
9852 HBasicBlock* switch_block,
9853 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009854 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009855 Register temp_reg = TMP;
9856 __ Addiu32(temp_reg, value_reg, -lower_bound);
9857 // Jump to default if index is negative
9858 // Note: We don't check the case that index is positive while value < lower_bound, because in
9859 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9860 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9861
Alexey Frunze96b66822016-09-10 02:32:44 -07009862 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009863 // Jump to successors[0] if value == lower_bound.
9864 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9865 int32_t last_index = 0;
9866 for (; num_entries - last_index > 2; last_index += 2) {
9867 __ Addiu(temp_reg, temp_reg, -2);
9868 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9869 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9870 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9871 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9872 }
9873 if (num_entries - last_index == 2) {
9874 // The last missing case_value.
9875 __ Addiu(temp_reg, temp_reg, -1);
9876 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009877 }
9878
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009879 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009880 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009881 __ B(codegen_->GetLabelOf(default_block));
9882 }
9883}
9884
Alexey Frunze96b66822016-09-10 02:32:44 -07009885void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9886 Register constant_area,
9887 int32_t lower_bound,
9888 uint32_t num_entries,
9889 HBasicBlock* switch_block,
9890 HBasicBlock* default_block) {
9891 // Create a jump table.
9892 std::vector<MipsLabel*> labels(num_entries);
9893 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9894 for (uint32_t i = 0; i < num_entries; i++) {
9895 labels[i] = codegen_->GetLabelOf(successors[i]);
9896 }
9897 JumpTable* table = __ CreateJumpTable(std::move(labels));
9898
9899 // Is the value in range?
9900 __ Addiu32(TMP, value_reg, -lower_bound);
9901 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9902 __ Sltiu(AT, TMP, num_entries);
9903 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9904 } else {
9905 __ LoadConst32(AT, num_entries);
9906 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9907 }
9908
9909 // We are in the range of the table.
9910 // Load the target address from the jump table, indexing by the value.
9911 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009912 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009913 __ Lw(TMP, TMP, 0);
9914 // Compute the absolute target address by adding the table start address
9915 // (the table contains offsets to targets relative to its start).
9916 __ Addu(TMP, TMP, AT);
9917 // And jump.
9918 __ Jr(TMP);
9919 __ NopIfNoReordering();
9920}
9921
9922void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9923 int32_t lower_bound = switch_instr->GetStartValue();
9924 uint32_t num_entries = switch_instr->GetNumEntries();
9925 LocationSummary* locations = switch_instr->GetLocations();
9926 Register value_reg = locations->InAt(0).AsRegister<Register>();
9927 HBasicBlock* switch_block = switch_instr->GetBlock();
9928 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9929
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009930 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009931 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009932 //
9933 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9934 // to access the jump table and it is implemented by changing HPackedSwitch to
9935 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9936 // VisitMipsPackedSwitch()).
9937 //
9938 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9939 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9940 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009941 GenTableBasedPackedSwitch(value_reg,
9942 ZERO,
9943 lower_bound,
9944 num_entries,
9945 switch_block,
9946 default_block);
9947 } else {
9948 GenPackedSwitchWithCompares(value_reg,
9949 lower_bound,
9950 num_entries,
9951 switch_block,
9952 default_block);
9953 }
9954}
9955
9956void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9957 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009958 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009959 locations->SetInAt(0, Location::RequiresRegister());
9960 // Constant area pointer (HMipsComputeBaseMethodAddress).
9961 locations->SetInAt(1, Location::RequiresRegister());
9962}
9963
9964void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9965 int32_t lower_bound = switch_instr->GetStartValue();
9966 uint32_t num_entries = switch_instr->GetNumEntries();
9967 LocationSummary* locations = switch_instr->GetLocations();
9968 Register value_reg = locations->InAt(0).AsRegister<Register>();
9969 Register constant_area = locations->InAt(1).AsRegister<Register>();
9970 HBasicBlock* switch_block = switch_instr->GetBlock();
9971 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9972
9973 // This is an R2-only path. HPackedSwitch has been changed to
9974 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9975 // required to address the jump table relative to PC.
9976 GenTableBasedPackedSwitch(value_reg,
9977 constant_area,
9978 lower_bound,
9979 num_entries,
9980 switch_block,
9981 default_block);
9982}
9983
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009984void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9985 HMipsComputeBaseMethodAddress* insn) {
9986 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009987 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009988 locations->SetOut(Location::RequiresRegister());
9989}
9990
9991void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9992 HMipsComputeBaseMethodAddress* insn) {
9993 LocationSummary* locations = insn->GetLocations();
9994 Register reg = locations->Out().AsRegister<Register>();
9995
9996 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9997
9998 // Generate a dummy PC-relative call to obtain PC.
9999 __ Nal();
10000 // Grab the return address off RA.
10001 __ Move(reg, RA);
10002
10003 // Remember this offset (the obtained PC value) for later use with constant area.
10004 __ BindPcRelBaseLabel();
10005}
10006
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010007void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10008 // The trampoline uses the same calling convention as dex calling conventions,
10009 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
10010 // the method_idx.
10011 HandleInvoke(invoke);
10012}
10013
10014void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10015 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
10016}
10017
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010018void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10019 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010020 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010021 locations->SetInAt(0, Location::RequiresRegister());
10022 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010023}
10024
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010025void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10026 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +000010027 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010028 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010029 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010030 __ LoadFromOffset(kLoadWord,
10031 locations->Out().AsRegister<Register>(),
10032 locations->InAt(0).AsRegister<Register>(),
10033 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010034 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010035 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +000010036 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000010037 __ LoadFromOffset(kLoadWord,
10038 locations->Out().AsRegister<Register>(),
10039 locations->InAt(0).AsRegister<Register>(),
10040 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010041 __ LoadFromOffset(kLoadWord,
10042 locations->Out().AsRegister<Register>(),
10043 locations->Out().AsRegister<Register>(),
10044 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010045 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010046}
10047
xueliang.zhonge0eb4832017-10-30 13:43:14 +000010048void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10049 ATTRIBUTE_UNUSED) {
10050 LOG(FATAL) << "Unreachable";
10051}
10052
10053void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10054 ATTRIBUTE_UNUSED) {
10055 LOG(FATAL) << "Unreachable";
10056}
10057
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010058#undef __
10059#undef QUICK_ENTRY_POINT
10060
10061} // namespace mips
10062} // namespace art