blob: be9ff48a6bac956200762603257e2b09b8d470cc [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010036#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020037#include "thread.h"
38#include "utils/assembler.h"
39#include "utils/mips/assembler_mips.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace mips {
44
45static constexpr int kCurrentMethodStackOffset = 0;
46static constexpr Register kMethodRegisterArgument = A0;
47
Alexey Frunze4147fcc2017-06-17 19:57:27 -070048// Flags controlling the use of thunks for Baker read barriers.
49constexpr bool kBakerReadBarrierThunksEnableForFields = true;
50constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
51constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
52
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020054 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010055 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010057 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010058 case DataType::Type::kInt8:
59 case DataType::Type::kUint16:
60 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -080061 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010062 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020063 return Location::RegisterLocation(V0);
64
Aart Bik66c158e2018-01-31 12:55:04 -080065 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020067 return Location::RegisterPairLocation(V0, V1);
68
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010069 case DataType::Type::kFloat32:
70 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020071 return Location::FpuRegisterLocation(F0);
72
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020074 return Location();
75 }
76 UNREACHABLE();
77}
78
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010079Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020080 return MipsReturnLocation(type);
81}
82
83Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
84 return Location::RegisterLocation(kMethodRegisterArgument);
85}
86
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020088 Location next_location;
89
90 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010091 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010093 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010094 case DataType::Type::kInt8:
95 case DataType::Type::kUint16:
96 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010097 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020098 uint32_t gp_index = gp_index_++;
99 if (gp_index < calling_convention.GetNumberOfRegisters()) {
100 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
101 } else {
102 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
103 next_location = Location::StackSlot(stack_offset);
104 }
105 break;
106 }
107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100108 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200109 uint32_t gp_index = gp_index_;
110 gp_index_ += 2;
111 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800112 Register reg = calling_convention.GetRegisterAt(gp_index);
113 if (reg == A1 || reg == A3) {
114 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200115 gp_index++;
116 }
117 Register low_even = calling_convention.GetRegisterAt(gp_index);
118 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
119 DCHECK_EQ(low_even + 1, high_odd);
120 next_location = Location::RegisterPairLocation(low_even, high_odd);
121 } else {
122 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
123 next_location = Location::DoubleStackSlot(stack_offset);
124 }
125 break;
126 }
127
128 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
129 // will take up the even/odd pair, while floats are stored in even regs only.
130 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100131 case DataType::Type::kFloat32:
132 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200133 uint32_t float_index = float_index_++;
134 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
135 next_location = Location::FpuRegisterLocation(
136 calling_convention.GetFpuRegisterAt(float_index));
137 } else {
138 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100139 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
140 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200141 }
142 break;
143 }
144
Aart Bik66c158e2018-01-31 12:55:04 -0800145 case DataType::Type::kUint32:
146 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200148 LOG(FATAL) << "Unexpected parameter type " << type;
149 break;
150 }
151
152 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200154
155 return next_location;
156}
157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100158Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200159 return MipsReturnLocation(type);
160}
161
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100162// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
163#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700164#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200165
166class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
167 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000168 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200169
170 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
171 LocationSummary* locations = instruction_->GetLocations();
172 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
173 __ Bind(GetEntryLabel());
174 if (instruction_->CanThrowIntoCatchBlock()) {
175 // Live registers will be restored in the catch block if caught.
176 SaveLiveRegisters(codegen, instruction_->GetLocations());
177 }
178 // We're moving two locations to locations that could overlap, so we need a parallel
179 // move resolver.
180 InvokeRuntimeCallingConvention calling_convention;
181 codegen->EmitParallelMoves(locations->InAt(0),
182 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100183 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200184 locations->InAt(1),
185 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100186 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100187 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
188 ? kQuickThrowStringBounds
189 : kQuickThrowArrayBounds;
190 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100191 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200192 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
193 }
194
195 bool IsFatal() const OVERRIDE { return true; }
196
197 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
198
199 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200200 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
201};
202
203class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
204 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000205 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206
207 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
208 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
209 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100210 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200211 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
212 }
213
214 bool IsFatal() const OVERRIDE { return true; }
215
216 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
217
218 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200219 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
220};
221
222class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
223 public:
224 LoadClassSlowPathMIPS(HLoadClass* cls,
225 HInstruction* at,
226 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000227 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700228 : SlowPathCodeMIPS(at),
229 cls_(cls),
230 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000231 do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
233 }
234
235 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000236 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700237 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200238 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700239 InvokeRuntimeCallingConvention calling_convention;
240 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200241 __ Bind(GetEntryLabel());
242 SaveLiveRegisters(codegen, locations);
243
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 dex::TypeIndex type_index = cls_->GetTypeIndex();
245 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100246 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
247 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000248 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200249 if (do_clinit_) {
250 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
251 } else {
252 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
253 }
254
255 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200256 if (out.IsValid()) {
257 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100258 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700259 mips_codegen->MoveLocation(out,
260 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
261 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200262 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200263 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700264
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200265 __ B(GetExitLabel());
266 }
267
268 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
269
270 private:
271 // The class this slow path will load.
272 HLoadClass* const cls_;
273
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
281};
282
283class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
284 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000285 explicit LoadStringSlowPathMIPS(HLoadString* instruction)
286 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700289 DCHECK(instruction_->IsLoadString());
290 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200291 LocationSummary* locations = instruction_->GetLocations();
292 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000293 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200294 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700295 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200296 __ Bind(GetEntryLabel());
297 SaveLiveRegisters(codegen, locations);
298
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000299 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100300 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200301 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700302
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100303 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200304 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000308
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200309 __ B(GetExitLabel());
310 }
311
312 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
313
314 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200315 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
316};
317
318class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
319 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000320 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200321
322 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
323 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
324 __ Bind(GetEntryLabel());
325 if (instruction_->CanThrowIntoCatchBlock()) {
326 // Live registers will be restored in the catch block if caught.
327 SaveLiveRegisters(codegen, instruction_->GetLocations());
328 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100329 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330 instruction_,
331 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100332 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200333 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
334 }
335
336 bool IsFatal() const OVERRIDE { return true; }
337
338 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
339
340 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
342};
343
344class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
345 public:
346 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000347 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200348
349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200350 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
352 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200353 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100354 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200355 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200356 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200357 if (successor_ == nullptr) {
358 __ B(GetReturnLabel());
359 } else {
360 __ B(mips_codegen->GetLabelOf(successor_));
361 }
362 }
363
364 MipsLabel* GetReturnLabel() {
365 DCHECK(successor_ == nullptr);
366 return &return_label_;
367 }
368
369 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
370
Chris Larsena2045912017-11-02 12:39:54 -0700371 HBasicBlock* GetSuccessor() const {
372 return successor_;
373 }
374
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200375 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200376 // If not null, the block to branch to after the suspend check.
377 HBasicBlock* const successor_;
378
379 // If `successor_` is null, the label to branch to after the suspend check.
380 MipsLabel return_label_;
381
382 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
383};
384
385class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
386 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800387 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
388 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200392 uint32_t dex_pc = instruction_->GetDexPc();
393 DCHECK(instruction_->IsCheckCast()
394 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
395 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
396
397 __ Bind(GetEntryLabel());
Alexey Frunzedfc30af2018-01-24 16:25:10 -0800398 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800399 SaveLiveRegisters(codegen, locations);
400 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200401
402 // We're moving two locations to locations that could overlap, so we need a parallel
403 // move resolver.
404 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800405 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200406 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100407 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800408 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200409 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100410 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200411 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100412 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800413 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100414 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200415 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
416 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 } else {
418 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800419 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
420 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 }
422
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800423 if (!is_fatal_) {
424 RestoreLiveRegisters(codegen, locations);
425 __ B(GetExitLabel());
426 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200427 }
428
429 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
430
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800431 bool IsFatal() const OVERRIDE { return is_fatal_; }
432
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800434 const bool is_fatal_;
435
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200436 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
437};
438
439class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
440 public:
Aart Bik42249c32016-01-07 15:33:50 -0800441 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000442 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443
444 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800445 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100447 LocationSummary* locations = instruction_->GetLocations();
448 SaveLiveRegisters(codegen, locations);
449 InvokeRuntimeCallingConvention calling_convention;
450 __ LoadConst32(calling_convention.GetRegisterAt(0),
451 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100452 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100453 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 }
455
456 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
457
458 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200459 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
460};
461
Alexey Frunze15958152017-02-09 19:08:30 -0800462class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
463 public:
464 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
465
466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 LocationSummary* locations = instruction_->GetLocations();
468 __ Bind(GetEntryLabel());
469 SaveLiveRegisters(codegen, locations);
470
471 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100472 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800473 parallel_move.AddMove(
474 locations->InAt(0),
475 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100476 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800477 nullptr);
478 parallel_move.AddMove(
479 locations->InAt(1),
480 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100481 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800482 nullptr);
483 parallel_move.AddMove(
484 locations->InAt(2),
485 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100486 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800487 nullptr);
488 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
489
490 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
491 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
492 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
493 RestoreLiveRegisters(codegen, locations);
494 __ B(GetExitLabel());
495 }
496
497 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
498
499 private:
500 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
501};
502
503// Slow path marking an object reference `ref` during a read
504// barrier. The field `obj.field` in the object `obj` holding this
505// reference does not get updated by this slow path after marking (see
506// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
507//
508// This means that after the execution of this slow path, `ref` will
509// always be up-to-date, but `obj.field` may not; i.e., after the
510// flip, `ref` will be a to-space reference, but `obj.field` will
511// probably still be a from-space reference (unless it gets updated by
512// another thread, or if another thread installed another object
513// reference (different from `ref`) in `obj.field`).
514//
515// If `entrypoint` is a valid location it is assumed to already be
516// holding the entrypoint. The case where the entrypoint is passed in
517// is for the GcRoot read barrier.
518class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
519 public:
520 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
521 Location ref,
522 Location entrypoint = Location::NoLocation())
523 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
524 DCHECK(kEmitCompilerReadBarrier);
525 }
526
527 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
528
529 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
530 LocationSummary* locations = instruction_->GetLocations();
531 Register ref_reg = ref_.AsRegister<Register>();
532 DCHECK(locations->CanCall());
533 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
534 DCHECK(instruction_->IsInstanceFieldGet() ||
535 instruction_->IsStaticFieldGet() ||
536 instruction_->IsArrayGet() ||
537 instruction_->IsArraySet() ||
538 instruction_->IsLoadClass() ||
539 instruction_->IsLoadString() ||
540 instruction_->IsInstanceOf() ||
541 instruction_->IsCheckCast() ||
542 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
543 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
544 << "Unexpected instruction in read barrier marking slow path: "
545 << instruction_->DebugName();
546
547 __ Bind(GetEntryLabel());
548 // No need to save live registers; it's taken care of by the
549 // entrypoint. Also, there is no need to update the stack mask,
550 // as this runtime call will not trigger a garbage collection.
551 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
552 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
553 (S2 <= ref_reg && ref_reg <= S7) ||
554 (ref_reg == FP)) << ref_reg;
555 // "Compact" slow path, saving two moves.
556 //
557 // Instead of using the standard runtime calling convention (input
558 // and output in A0 and V0 respectively):
559 //
560 // A0 <- ref
561 // V0 <- ReadBarrierMark(A0)
562 // ref <- V0
563 //
564 // we just use rX (the register containing `ref`) as input and output
565 // of a dedicated entrypoint:
566 //
567 // rX <- ReadBarrierMarkRegX(rX)
568 //
569 if (entrypoint_.IsValid()) {
570 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
571 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
572 __ Jalr(entrypoint_.AsRegister<Register>());
573 __ NopIfNoReordering();
574 } else {
575 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100576 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800577 // This runtime call does not require a stack map.
578 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
579 instruction_,
580 this,
581 /* direct */ false);
582 }
583 __ B(GetExitLabel());
584 }
585
586 private:
587 // The location (register) of the marked object reference.
588 const Location ref_;
589
590 // The location of the entrypoint if already loaded.
591 const Location entrypoint_;
592
593 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
594};
595
596// Slow path marking an object reference `ref` during a read barrier,
597// and if needed, atomically updating the field `obj.field` in the
598// object `obj` holding this reference after marking (contrary to
599// ReadBarrierMarkSlowPathMIPS above, which never tries to update
600// `obj.field`).
601//
602// This means that after the execution of this slow path, both `ref`
603// and `obj.field` will be up-to-date; i.e., after the flip, both will
604// hold the same to-space reference (unless another thread installed
605// another object reference (different from `ref`) in `obj.field`).
606class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
607 public:
608 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
609 Location ref,
610 Register obj,
611 Location field_offset,
612 Register temp1)
613 : SlowPathCodeMIPS(instruction),
614 ref_(ref),
615 obj_(obj),
616 field_offset_(field_offset),
617 temp1_(temp1) {
618 DCHECK(kEmitCompilerReadBarrier);
619 }
620
621 const char* GetDescription() const OVERRIDE {
622 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
623 }
624
625 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
626 LocationSummary* locations = instruction_->GetLocations();
627 Register ref_reg = ref_.AsRegister<Register>();
628 DCHECK(locations->CanCall());
629 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
630 // This slow path is only used by the UnsafeCASObject intrinsic.
631 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
632 << "Unexpected instruction in read barrier marking and field updating slow path: "
633 << instruction_->DebugName();
634 DCHECK(instruction_->GetLocations()->Intrinsified());
635 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
636 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
637
638 __ Bind(GetEntryLabel());
639
640 // Save the old reference.
641 // Note that we cannot use AT or TMP to save the old reference, as those
642 // are used by the code that follows, but we need the old reference after
643 // the call to the ReadBarrierMarkRegX entry point.
644 DCHECK_NE(temp1_, AT);
645 DCHECK_NE(temp1_, TMP);
646 __ Move(temp1_, ref_reg);
647
648 // No need to save live registers; it's taken care of by the
649 // entrypoint. Also, there is no need to update the stack mask,
650 // as this runtime call will not trigger a garbage collection.
651 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
652 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
653 (S2 <= ref_reg && ref_reg <= S7) ||
654 (ref_reg == FP)) << ref_reg;
655 // "Compact" slow path, saving two moves.
656 //
657 // Instead of using the standard runtime calling convention (input
658 // and output in A0 and V0 respectively):
659 //
660 // A0 <- ref
661 // V0 <- ReadBarrierMark(A0)
662 // ref <- V0
663 //
664 // we just use rX (the register containing `ref`) as input and output
665 // of a dedicated entrypoint:
666 //
667 // rX <- ReadBarrierMarkRegX(rX)
668 //
669 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100670 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800671 // This runtime call does not require a stack map.
672 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
673 instruction_,
674 this,
675 /* direct */ false);
676
677 // If the new reference is different from the old reference,
678 // update the field in the holder (`*(obj_ + field_offset_)`).
679 //
680 // Note that this field could also hold a different object, if
681 // another thread had concurrently changed it. In that case, the
682 // the compare-and-set (CAS) loop below would abort, leaving the
683 // field as-is.
684 MipsLabel done;
685 __ Beq(temp1_, ref_reg, &done);
686
687 // Update the the holder's field atomically. This may fail if
688 // mutator updates before us, but it's OK. This is achieved
689 // using a strong compare-and-set (CAS) operation with relaxed
690 // memory synchronization ordering, where the expected value is
691 // the old reference and the desired value is the new reference.
692
693 // Convenience aliases.
694 Register base = obj_;
695 // The UnsafeCASObject intrinsic uses a register pair as field
696 // offset ("long offset"), of which only the low part contains
697 // data.
698 Register offset = field_offset_.AsRegisterPairLow<Register>();
699 Register expected = temp1_;
700 Register value = ref_reg;
701 Register tmp_ptr = TMP; // Pointer to actual memory.
702 Register tmp = AT; // Value in memory.
703
704 __ Addu(tmp_ptr, base, offset);
705
706 if (kPoisonHeapReferences) {
707 __ PoisonHeapReference(expected);
708 // Do not poison `value` if it is the same register as
709 // `expected`, which has just been poisoned.
710 if (value != expected) {
711 __ PoisonHeapReference(value);
712 }
713 }
714
715 // do {
716 // tmp = [r_ptr] - expected;
717 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
718
719 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
720 MipsLabel loop_head, exit_loop;
721 __ Bind(&loop_head);
722 if (is_r6) {
723 __ LlR6(tmp, tmp_ptr);
724 } else {
725 __ LlR2(tmp, tmp_ptr);
726 }
727 __ Bne(tmp, expected, &exit_loop);
728 __ Move(tmp, value);
729 if (is_r6) {
730 __ ScR6(tmp, tmp_ptr);
731 } else {
732 __ ScR2(tmp, tmp_ptr);
733 }
734 __ Beqz(tmp, &loop_head);
735 __ Bind(&exit_loop);
736
737 if (kPoisonHeapReferences) {
738 __ UnpoisonHeapReference(expected);
739 // Do not unpoison `value` if it is the same register as
740 // `expected`, which has just been unpoisoned.
741 if (value != expected) {
742 __ UnpoisonHeapReference(value);
743 }
744 }
745
746 __ Bind(&done);
747 __ B(GetExitLabel());
748 }
749
750 private:
751 // The location (register) of the marked object reference.
752 const Location ref_;
753 // The register containing the object holding the marked object reference field.
754 const Register obj_;
755 // The location of the offset of the marked reference field within `obj_`.
756 Location field_offset_;
757
758 const Register temp1_;
759
760 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
761};
762
763// Slow path generating a read barrier for a heap reference.
764class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
765 public:
766 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
767 Location out,
768 Location ref,
769 Location obj,
770 uint32_t offset,
771 Location index)
772 : SlowPathCodeMIPS(instruction),
773 out_(out),
774 ref_(ref),
775 obj_(obj),
776 offset_(offset),
777 index_(index) {
778 DCHECK(kEmitCompilerReadBarrier);
779 // If `obj` is equal to `out` or `ref`, it means the initial object
780 // has been overwritten by (or after) the heap object reference load
781 // to be instrumented, e.g.:
782 //
783 // __ LoadFromOffset(kLoadWord, out, out, offset);
784 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
785 //
786 // In that case, we have lost the information about the original
787 // object, and the emitted read barrier cannot work properly.
788 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
789 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
790 }
791
792 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
793 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
794 LocationSummary* locations = instruction_->GetLocations();
795 Register reg_out = out_.AsRegister<Register>();
796 DCHECK(locations->CanCall());
797 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
798 DCHECK(instruction_->IsInstanceFieldGet() ||
799 instruction_->IsStaticFieldGet() ||
800 instruction_->IsArrayGet() ||
801 instruction_->IsInstanceOf() ||
802 instruction_->IsCheckCast() ||
803 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
804 << "Unexpected instruction in read barrier for heap reference slow path: "
805 << instruction_->DebugName();
806
807 __ Bind(GetEntryLabel());
808 SaveLiveRegisters(codegen, locations);
809
810 // We may have to change the index's value, but as `index_` is a
811 // constant member (like other "inputs" of this slow path),
812 // introduce a copy of it, `index`.
813 Location index = index_;
814 if (index_.IsValid()) {
815 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
816 if (instruction_->IsArrayGet()) {
817 // Compute the actual memory offset and store it in `index`.
818 Register index_reg = index_.AsRegister<Register>();
819 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
820 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
821 // We are about to change the value of `index_reg` (see the
822 // calls to art::mips::MipsAssembler::Sll and
823 // art::mips::MipsAssembler::Addiu32 below), but it has
824 // not been saved by the previous call to
825 // art::SlowPathCode::SaveLiveRegisters, as it is a
826 // callee-save register --
827 // art::SlowPathCode::SaveLiveRegisters does not consider
828 // callee-save registers, as it has been designed with the
829 // assumption that callee-save registers are supposed to be
830 // handled by the called function. So, as a callee-save
831 // register, `index_reg` _would_ eventually be saved onto
832 // the stack, but it would be too late: we would have
833 // changed its value earlier. Therefore, we manually save
834 // it here into another freely available register,
835 // `free_reg`, chosen of course among the caller-save
836 // registers (as a callee-save `free_reg` register would
837 // exhibit the same problem).
838 //
839 // Note we could have requested a temporary register from
840 // the register allocator instead; but we prefer not to, as
841 // this is a slow path, and we know we can find a
842 // caller-save register that is available.
843 Register free_reg = FindAvailableCallerSaveRegister(codegen);
844 __ Move(free_reg, index_reg);
845 index_reg = free_reg;
846 index = Location::RegisterLocation(index_reg);
847 } else {
848 // The initial register stored in `index_` has already been
849 // saved in the call to art::SlowPathCode::SaveLiveRegisters
850 // (as it is not a callee-save register), so we can freely
851 // use it.
852 }
853 // Shifting the index value contained in `index_reg` by the scale
854 // factor (2) cannot overflow in practice, as the runtime is
855 // unable to allocate object arrays with a size larger than
856 // 2^26 - 1 (that is, 2^28 - 4 bytes).
857 __ Sll(index_reg, index_reg, TIMES_4);
858 static_assert(
859 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
860 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
861 __ Addiu32(index_reg, index_reg, offset_);
862 } else {
863 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
864 // intrinsics, `index_` is not shifted by a scale factor of 2
865 // (as in the case of ArrayGet), as it is actually an offset
866 // to an object field within an object.
867 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
868 DCHECK(instruction_->GetLocations()->Intrinsified());
869 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
870 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
871 << instruction_->AsInvoke()->GetIntrinsic();
872 DCHECK_EQ(offset_, 0U);
873 DCHECK(index_.IsRegisterPair());
874 // UnsafeGet's offset location is a register pair, the low
875 // part contains the correct offset.
876 index = index_.ToLow();
877 }
878 }
879
880 // We're moving two or three locations to locations that could
881 // overlap, so we need a parallel move resolver.
882 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100883 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800884 parallel_move.AddMove(ref_,
885 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100886 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800887 nullptr);
888 parallel_move.AddMove(obj_,
889 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100890 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800891 nullptr);
892 if (index.IsValid()) {
893 parallel_move.AddMove(index,
894 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100895 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800896 nullptr);
897 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
898 } else {
899 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
900 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
901 }
902 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
903 instruction_,
904 instruction_->GetDexPc(),
905 this);
906 CheckEntrypointTypes<
907 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200908 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100909 calling_convention.GetReturnLocation(DataType::Type::kReference),
910 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800911
912 RestoreLiveRegisters(codegen, locations);
913 __ B(GetExitLabel());
914 }
915
916 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
917
918 private:
919 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
920 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
921 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
922 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
923 if (i != ref &&
924 i != obj &&
925 !codegen->IsCoreCalleeSaveRegister(i) &&
926 !codegen->IsBlockedCoreRegister(i)) {
927 return static_cast<Register>(i);
928 }
929 }
930 // We shall never fail to find a free caller-save register, as
931 // there are more than two core caller-save registers on MIPS
932 // (meaning it is possible to find one which is different from
933 // `ref` and `obj`).
934 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
935 LOG(FATAL) << "Could not find a free caller-save register";
936 UNREACHABLE();
937 }
938
939 const Location out_;
940 const Location ref_;
941 const Location obj_;
942 const uint32_t offset_;
943 // An additional location containing an index to an array.
944 // Only used for HArrayGet and the UnsafeGetObject &
945 // UnsafeGetObjectVolatile intrinsics.
946 const Location index_;
947
948 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
949};
950
951// Slow path generating a read barrier for a GC root.
952class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
953 public:
954 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
955 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
956 DCHECK(kEmitCompilerReadBarrier);
957 }
958
959 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
960 LocationSummary* locations = instruction_->GetLocations();
961 Register reg_out = out_.AsRegister<Register>();
962 DCHECK(locations->CanCall());
963 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
964 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
965 << "Unexpected instruction in read barrier for GC root slow path: "
966 << instruction_->DebugName();
967
968 __ Bind(GetEntryLabel());
969 SaveLiveRegisters(codegen, locations);
970
971 InvokeRuntimeCallingConvention calling_convention;
972 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +0200973 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
974 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100975 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800976 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
977 instruction_,
978 instruction_->GetDexPc(),
979 this);
980 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +0200981 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100982 calling_convention.GetReturnLocation(DataType::Type::kReference),
983 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800984
985 RestoreLiveRegisters(codegen, locations);
986 __ B(GetExitLabel());
987 }
988
989 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
990
991 private:
992 const Location out_;
993 const Location root_;
994
995 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
996};
997
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200998CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
999 const MipsInstructionSetFeatures& isa_features,
1000 const CompilerOptions& compiler_options,
1001 OptimizingCompilerStats* stats)
1002 : CodeGenerator(graph,
1003 kNumberOfCoreRegisters,
1004 kNumberOfFRegisters,
1005 kNumberOfRegisterPairs,
1006 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1007 arraysize(kCoreCalleeSaves)),
1008 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1009 arraysize(kFpuCalleeSaves)),
1010 compiler_options,
1011 stats),
1012 block_labels_(nullptr),
1013 location_builder_(graph, this),
1014 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001015 move_resolver_(graph->GetAllocator(), this),
1016 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001017 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001018 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001019 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001020 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001021 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001022 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001023 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001024 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001025 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1026 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1027 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001028 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001029 // Save RA (containing the return address) to mimic Quick.
1030 AddAllocatedRegister(Location::RegisterLocation(RA));
1031}
1032
1033#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001034// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1035#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001036#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001037
1038void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1039 // Ensure that we fix up branches.
1040 __ FinalizeCode();
1041
1042 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001043 StackMapStream* stack_map_stream = GetStackMapStream();
1044 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001045 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +00001046 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001047 uint32_t new_position = __ GetAdjustedPosition(old_position);
1048 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001049 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001050 }
1051
1052 // Adjust pc offsets for the disassembly information.
1053 if (disasm_info_ != nullptr) {
1054 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1055 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1056 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1057 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1058 it.second.start = __ GetAdjustedPosition(it.second.start);
1059 it.second.end = __ GetAdjustedPosition(it.second.end);
1060 }
1061 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1062 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1063 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1064 }
1065 }
1066
1067 CodeGenerator::Finalize(allocator);
1068}
1069
1070MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1071 return codegen_->GetAssembler();
1072}
1073
1074void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1075 DCHECK_LT(index, moves_.size());
1076 MoveOperands* move = moves_[index];
1077 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1078}
1079
1080void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1081 DCHECK_LT(index, moves_.size());
1082 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001083 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001084 Location loc1 = move->GetDestination();
1085 Location loc2 = move->GetSource();
1086
1087 DCHECK(!loc1.IsConstant());
1088 DCHECK(!loc2.IsConstant());
1089
1090 if (loc1.Equals(loc2)) {
1091 return;
1092 }
1093
1094 if (loc1.IsRegister() && loc2.IsRegister()) {
1095 // Swap 2 GPRs.
1096 Register r1 = loc1.AsRegister<Register>();
1097 Register r2 = loc2.AsRegister<Register>();
1098 __ Move(TMP, r2);
1099 __ Move(r2, r1);
1100 __ Move(r1, TMP);
1101 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001102 if (codegen_->GetGraph()->HasSIMD()) {
1103 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1104 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1105 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001106 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001107 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1108 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1109 if (type == DataType::Type::kFloat32) {
1110 __ MovS(FTMP, f2);
1111 __ MovS(f2, f1);
1112 __ MovS(f1, FTMP);
1113 } else {
1114 DCHECK_EQ(type, DataType::Type::kFloat64);
1115 __ MovD(FTMP, f2);
1116 __ MovD(f2, f1);
1117 __ MovD(f1, FTMP);
1118 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001119 }
1120 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1121 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1122 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001123 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001124 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1125 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001126 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001127 __ Move(TMP, r2);
1128 __ Mfc1(r2, f1);
1129 __ Mtc1(TMP, f1);
1130 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1131 // Swap 2 GPR register pairs.
1132 Register r1 = loc1.AsRegisterPairLow<Register>();
1133 Register r2 = loc2.AsRegisterPairLow<Register>();
1134 __ Move(TMP, r2);
1135 __ Move(r2, r1);
1136 __ Move(r1, TMP);
1137 r1 = loc1.AsRegisterPairHigh<Register>();
1138 r2 = loc2.AsRegisterPairHigh<Register>();
1139 __ Move(TMP, r2);
1140 __ Move(r2, r1);
1141 __ Move(r1, TMP);
1142 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1143 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1144 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001146 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1147 : loc2.AsFpuRegister<FRegister>();
1148 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1149 : loc2.AsRegisterPairLow<Register>();
1150 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1151 : loc2.AsRegisterPairHigh<Register>();
1152 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1153 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1154 // unpredictable and the following mfch1 will fail.
1155 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001156 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001157 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001158 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001159 __ Move(r2_l, TMP);
1160 __ Move(r2_h, AT);
1161 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1162 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1163 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1164 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001165 } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) {
1166 ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
David Brazdilcc0f3112016-01-28 17:14:52 +00001167 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1168 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001169 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1170 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001171 __ Move(TMP, reg);
1172 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1173 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1174 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1175 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1176 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1177 : loc2.AsRegisterPairLow<Register>();
1178 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1179 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001180 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001181 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1182 : loc2.GetHighStackIndex(kMipsWordSize);
1183 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001184 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001185 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001186 __ Move(TMP, reg_h);
1187 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1188 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001189 } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) ||
1190 (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) {
1191 Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2;
1192 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1193 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(fp_loc));
1194 __ LoadQFromOffset(fp_loc.AsFpuRegister<FRegister>(), SP, offset);
1195 __ StoreQToOffset(FTMP, SP, offset);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001196 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1197 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1198 : loc2.AsFpuRegister<FRegister>();
1199 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001200 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001201 __ MovS(FTMP, reg);
1202 __ LoadSFromOffset(reg, SP, offset);
1203 __ StoreSToOffset(FTMP, SP, offset);
1204 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001205 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001206 __ MovD(FTMP, reg);
1207 __ LoadDFromOffset(reg, SP, offset);
1208 __ StoreDToOffset(FTMP, SP, offset);
1209 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210 } else {
1211 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1212 }
1213}
1214
1215void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1216 __ Pop(static_cast<Register>(reg));
1217}
1218
1219void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1220 __ Push(static_cast<Register>(reg));
1221}
1222
1223void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1224 // Allocate a scratch register other than TMP, if available.
1225 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1226 // automatically unspilled when the scratch scope object is destroyed).
1227 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1228 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001229 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001230 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1231 __ LoadFromOffset(kLoadWord,
1232 Register(ensure_scratch.GetRegister()),
1233 SP,
1234 index1 + stack_offset);
1235 __ LoadFromOffset(kLoadWord,
1236 TMP,
1237 SP,
1238 index2 + stack_offset);
1239 __ StoreToOffset(kStoreWord,
1240 Register(ensure_scratch.GetRegister()),
1241 SP,
1242 index2 + stack_offset);
1243 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1244 }
1245}
1246
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001247void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) {
1248 __ LoadQFromOffset(FTMP, SP, index1);
1249 __ LoadQFromOffset(FTMP2, SP, index2);
1250 __ StoreQToOffset(FTMP, SP, index2);
1251 __ StoreQToOffset(FTMP2, SP, index1);
1252}
1253
Alexey Frunze73296a72016-06-03 22:51:46 -07001254void CodeGeneratorMIPS::ComputeSpillMask() {
1255 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1256 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1257 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1258 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1259 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1260 // within the stack frame.
1261 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1262 core_spill_mask_ |= (1 << ZERO);
1263 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001264}
1265
1266bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001267 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001268 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1269 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1270 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001271 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001272}
1273
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001274static dwarf::Reg DWARFReg(Register reg) {
1275 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1276}
1277
1278// TODO: mapping of floating-point registers to DWARF.
1279
1280void CodeGeneratorMIPS::GenerateFrameEntry() {
1281 __ Bind(&frame_entry_label_);
1282
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001283 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01001284 __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
1285 __ Addiu(TMP, TMP, 1);
1286 __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001287 }
1288
Vladimir Marko33bff252017-11-01 14:35:42 +00001289 bool do_overflow_check =
1290 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001291
1292 if (do_overflow_check) {
1293 __ LoadFromOffset(kLoadWord,
1294 ZERO,
1295 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001296 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 RecordPcInfo(nullptr, 0);
1298 }
1299
1300 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001301 CHECK_EQ(fpu_spill_mask_, 0u);
1302 CHECK_EQ(core_spill_mask_, 1u << RA);
1303 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304 return;
1305 }
1306
1307 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001308 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1309 LOG(FATAL) << "Stack frame larger than "
1310 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311 }
1312
1313 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001314
Alexey Frunze73296a72016-06-03 22:51:46 -07001315 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316 __ IncreaseFrameSize(ofs);
1317
Alexey Frunze73296a72016-06-03 22:51:46 -07001318 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1319 Register reg = static_cast<Register>(MostSignificantBit(mask));
1320 mask ^= 1u << reg;
1321 ofs -= kMipsWordSize;
1322 // The ZERO register is only included for alignment.
1323 if (reg != ZERO) {
1324 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001325 __ cfi().RelOffset(DWARFReg(reg), ofs);
1326 }
1327 }
1328
Alexey Frunze73296a72016-06-03 22:51:46 -07001329 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1330 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1331 mask ^= 1u << reg;
1332 ofs -= kMipsDoublewordSize;
1333 __ StoreDToOffset(reg, SP, ofs);
1334 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001335 }
1336
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001337 // Save the current method if we need it. Note that we do not
1338 // do this in HCurrentMethod, as the instruction might have been removed
1339 // in the SSA graph.
1340 if (RequiresCurrentMethod()) {
1341 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1342 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001343
1344 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1345 // Initialize should deoptimize flag to 0.
1346 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1347 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001348}
1349
1350void CodeGeneratorMIPS::GenerateFrameExit() {
1351 __ cfi().RememberState();
1352
1353 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001354 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355
Alexey Frunze73296a72016-06-03 22:51:46 -07001356 // For better instruction scheduling restore RA before other registers.
1357 uint32_t ofs = GetFrameSize();
1358 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1359 Register reg = static_cast<Register>(MostSignificantBit(mask));
1360 mask ^= 1u << reg;
1361 ofs -= kMipsWordSize;
1362 // The ZERO register is only included for alignment.
1363 if (reg != ZERO) {
1364 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001365 __ cfi().Restore(DWARFReg(reg));
1366 }
1367 }
1368
Alexey Frunze73296a72016-06-03 22:51:46 -07001369 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1370 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1371 mask ^= 1u << reg;
1372 ofs -= kMipsDoublewordSize;
1373 __ LoadDFromOffset(reg, SP, ofs);
1374 // TODO: __ cfi().Restore(DWARFReg(reg));
1375 }
1376
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001377 size_t frame_size = GetFrameSize();
1378 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1379 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1380 bool reordering = __ SetReorder(false);
1381 if (exchange) {
1382 __ Jr(RA);
1383 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1384 } else {
1385 __ DecreaseFrameSize(frame_size);
1386 __ Jr(RA);
1387 __ Nop(); // In delay slot.
1388 }
1389 __ SetReorder(reordering);
1390 } else {
1391 __ Jr(RA);
1392 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001393 }
1394
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001395 __ cfi().RestoreState();
1396 __ cfi().DefCFAOffset(GetFrameSize());
1397}
1398
1399void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1400 __ Bind(GetLabelOf(block));
1401}
1402
Lena Djokicca8c2952017-05-29 11:31:46 +02001403VectorRegister VectorRegisterFrom(Location location) {
1404 DCHECK(location.IsFpuRegister());
1405 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1406}
1407
Lena Djokic8098da92017-06-28 12:07:50 +02001408void CodeGeneratorMIPS::MoveLocation(Location destination,
1409 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001410 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 if (source.Equals(destination)) {
1412 return;
1413 }
1414
Lena Djokic8098da92017-06-28 12:07:50 +02001415 if (source.IsConstant()) {
1416 MoveConstant(destination, source.GetConstant());
1417 } else {
1418 if (destination.IsRegister()) {
1419 if (source.IsRegister()) {
1420 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1421 } else if (source.IsFpuRegister()) {
1422 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1423 } else {
1424 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001425 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001426 }
1427 } else if (destination.IsRegisterPair()) {
1428 if (source.IsRegisterPair()) {
1429 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1430 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1431 } else if (source.IsFpuRegister()) {
1432 Register dst_high = destination.AsRegisterPairHigh<Register>();
1433 Register dst_low = destination.AsRegisterPairLow<Register>();
1434 FRegister src = source.AsFpuRegister<FRegister>();
1435 __ Mfc1(dst_low, src);
1436 __ MoveFromFpuHigh(dst_high, src);
1437 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001438 DCHECK(source.IsDoubleStackSlot())
1439 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001440 int32_t off = source.GetStackIndex();
1441 Register r = destination.AsRegisterPairLow<Register>();
1442 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1443 }
1444 } else if (destination.IsFpuRegister()) {
1445 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001446 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001447 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1448 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001450 FRegister dst = destination.AsFpuRegister<FRegister>();
1451 Register src_high = source.AsRegisterPairHigh<Register>();
1452 Register src_low = source.AsRegisterPairLow<Register>();
1453 __ Mtc1(src_low, dst);
1454 __ MoveToFpuHigh(src_high, dst);
1455 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001456 if (GetGraph()->HasSIMD()) {
1457 __ MoveV(VectorRegisterFrom(destination),
1458 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001459 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001461 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1462 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001464 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1465 }
Lena Djokic8098da92017-06-28 12:07:50 +02001466 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001467 } else if (source.IsSIMDStackSlot()) {
1468 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001469 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001470 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001471 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1472 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001474 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1475 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1476 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001477 } else if (destination.IsSIMDStackSlot()) {
1478 if (source.IsFpuRegister()) {
1479 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1480 } else {
1481 DCHECK(source.IsSIMDStackSlot());
1482 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1483 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1484 }
Lena Djokic8098da92017-06-28 12:07:50 +02001485 } else if (destination.IsDoubleStackSlot()) {
1486 int32_t dst_offset = destination.GetStackIndex();
1487 if (source.IsRegisterPair()) {
1488 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1489 } else if (source.IsFpuRegister()) {
1490 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1491 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001492 DCHECK(source.IsDoubleStackSlot())
1493 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001494 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1495 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1496 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1497 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1498 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001499 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001500 DCHECK(destination.IsStackSlot()) << destination;
1501 int32_t dst_offset = destination.GetStackIndex();
1502 if (source.IsRegister()) {
1503 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1504 } else if (source.IsFpuRegister()) {
1505 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1506 } else {
1507 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1508 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1509 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1510 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001511 }
1512 }
1513}
1514
1515void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1516 if (c->IsIntConstant() || c->IsNullConstant()) {
1517 // Move 32 bit constant.
1518 int32_t value = GetInt32ValueOf(c);
1519 if (destination.IsRegister()) {
1520 Register dst = destination.AsRegister<Register>();
1521 __ LoadConst32(dst, value);
1522 } else {
1523 DCHECK(destination.IsStackSlot())
1524 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001525 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001526 }
1527 } else if (c->IsLongConstant()) {
1528 // Move 64 bit constant.
1529 int64_t value = GetInt64ValueOf(c);
1530 if (destination.IsRegisterPair()) {
1531 Register r_h = destination.AsRegisterPairHigh<Register>();
1532 Register r_l = destination.AsRegisterPairLow<Register>();
1533 __ LoadConst64(r_h, r_l, value);
1534 } else {
1535 DCHECK(destination.IsDoubleStackSlot())
1536 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001537 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001538 }
1539 } else if (c->IsFloatConstant()) {
1540 // Move 32 bit float constant.
1541 int32_t value = GetInt32ValueOf(c);
1542 if (destination.IsFpuRegister()) {
1543 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1544 } else {
1545 DCHECK(destination.IsStackSlot())
1546 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001547 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548 }
1549 } else {
1550 // Move 64 bit double constant.
1551 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1552 int64_t value = GetInt64ValueOf(c);
1553 if (destination.IsFpuRegister()) {
1554 FRegister fd = destination.AsFpuRegister<FRegister>();
1555 __ LoadDConst64(fd, value, TMP);
1556 } else {
1557 DCHECK(destination.IsDoubleStackSlot())
1558 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001559 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001560 }
1561 }
1562}
1563
1564void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1565 DCHECK(destination.IsRegister());
1566 Register dst = destination.AsRegister<Register>();
1567 __ LoadConst32(dst, value);
1568}
1569
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001570void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1571 if (location.IsRegister()) {
1572 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001573 } else if (location.IsRegisterPair()) {
1574 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1575 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576 } else {
1577 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1578 }
1579}
1580
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001581template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001582inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1583 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001584 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001585 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001586 const DexFile* dex_file = info.target_dex_file;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001587 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001588 DCHECK(info.label.IsBound());
1589 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001590 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1591 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001592 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1593 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1594 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001595 : __ GetPcRelBaseLabelLocation();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001596 linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001597 }
1598}
1599
Vladimir Markob066d432018-01-03 13:14:37 +00001600linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
1601 const DexFile* target_dex_file,
1602 uint32_t pc_insn_offset,
1603 uint32_t boot_image_offset) {
1604 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
1605 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
1606}
1607
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001608void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001609 DCHECK(linker_patches->empty());
1610 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001611 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001612 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001613 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001614 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001615 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001616 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001617 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001618 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001619 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001620 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001621 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001622 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001623 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001624 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001625 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00001626 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
1627 boot_image_method_patches_, linker_patches);
Vladimir Markoe47f60c2018-02-21 13:43:28 +00001628 DCHECK(boot_image_type_patches_.empty());
1629 DCHECK(boot_image_string_patches_.empty());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001630 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001631 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1632 method_bss_entry_patches_, linker_patches);
1633 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1634 type_bss_entry_patches_, linker_patches);
1635 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1636 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001637 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001638}
1639
Vladimir Markob066d432018-01-03 13:14:37 +00001640CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageRelRoPatch(
1641 uint32_t boot_image_offset,
1642 const PcRelativePatchInfo* info_high) {
1643 return NewPcRelativePatch(
1644 /* dex_file */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_);
1645}
1646
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001647CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001648 MethodReference target_method,
1649 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001650 return NewPcRelativePatch(
1651 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001652}
1653
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001654CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001655 MethodReference target_method,
1656 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001657 return NewPcRelativePatch(
1658 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001659}
1660
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001661CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001662 const DexFile& dex_file,
1663 dex::TypeIndex type_index,
1664 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001665 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001666}
1667
Vladimir Marko1998cd02017-01-13 13:02:58 +00001668CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001669 const DexFile& dex_file,
1670 dex::TypeIndex type_index,
1671 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001672 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001673}
1674
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001675CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001676 const DexFile& dex_file,
1677 dex::StringIndex string_index,
1678 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001679 return NewPcRelativePatch(
1680 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001681}
1682
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001683CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1684 const DexFile& dex_file,
1685 dex::StringIndex string_index,
1686 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001687 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001688}
1689
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001690CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001691 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001692 uint32_t offset_or_index,
1693 const PcRelativePatchInfo* info_high,
1694 ArenaDeque<PcRelativePatchInfo>* patches) {
1695 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001696 return &patches->back();
1697}
1698
Alexey Frunze06a46c42016-07-19 15:00:40 -07001699Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1700 return map->GetOrCreate(
1701 value,
1702 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1703}
1704
Alexey Frunze06a46c42016-07-19 15:00:40 -07001705Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001706 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001707}
1708
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001709void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001710 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001711 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001712 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001713 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001714 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001715 if (GetInstructionSetFeatures().IsR6()) {
1716 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001717 __ Bind(&info_high->label);
1718 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001719 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001720 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001721 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001722 } else {
1723 // If base is ZERO, emit NAL to obtain the actual base.
1724 if (base == ZERO) {
1725 // Generate a dummy PC-relative call to obtain PC.
1726 __ Nal();
1727 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001728 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001729 __ Lui(out, /* placeholder */ 0x1234);
1730 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1731 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1732 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001733 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001734 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001735 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001736 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001737 __ Addu(out, out, (base == ZERO) ? RA : base);
1738 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001739 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001740 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001741}
1742
Alexey Frunze627c1a02017-01-30 19:28:14 -08001743CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1744 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001745 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001746 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001747 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1748 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001749 return &jit_string_patches_.back();
1750}
1751
1752CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1753 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001754 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001755 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001756 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1757 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001758 return &jit_class_patches_.back();
1759}
1760
1761void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1762 const uint8_t* roots_data,
1763 const CodeGeneratorMIPS::JitPatchInfo& info,
1764 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001765 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1766 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001767 uintptr_t address =
1768 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1769 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1770 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001771 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1772 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1773 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1774 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001775 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001776 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1777 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001778 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001779 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001780 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1781 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001782 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001783 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1784 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001785}
1786
1787void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1788 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001789 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1790 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001791 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001792 }
1793 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001794 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1795 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001796 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001797 }
1798}
1799
Goran Jakovljevice114da22016-12-26 14:21:43 +01001800void CodeGeneratorMIPS::MarkGCCard(Register object,
1801 Register value,
1802 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001803 MipsLabel done;
1804 Register card = AT;
1805 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001806 if (value_can_be_null) {
1807 __ Beqz(value, &done);
1808 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001809 __ LoadFromOffset(kLoadWord,
1810 card,
1811 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001812 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001813 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1814 __ Addu(temp, card, temp);
1815 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001816 if (value_can_be_null) {
1817 __ Bind(&done);
1818 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001819}
1820
David Brazdil58282f42016-01-14 12:45:10 +00001821void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001822 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1823 blocked_core_registers_[ZERO] = true;
1824 blocked_core_registers_[K0] = true;
1825 blocked_core_registers_[K1] = true;
1826 blocked_core_registers_[GP] = true;
1827 blocked_core_registers_[SP] = true;
1828 blocked_core_registers_[RA] = true;
1829
1830 // AT and TMP(T8) are used as temporary/scratch registers
1831 // (similar to how AT is used by MIPS assemblers).
1832 blocked_core_registers_[AT] = true;
1833 blocked_core_registers_[TMP] = true;
1834 blocked_fpu_registers_[FTMP] = true;
1835
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001836 if (GetInstructionSetFeatures().HasMsa()) {
1837 // To be used just for MSA instructions.
1838 blocked_fpu_registers_[FTMP2] = true;
1839 }
1840
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001841 // Reserve suspend and thread registers.
1842 blocked_core_registers_[S0] = true;
1843 blocked_core_registers_[TR] = true;
1844
1845 // Reserve T9 for function calls
1846 blocked_core_registers_[T9] = true;
1847
1848 // Reserve odd-numbered FPU registers.
1849 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1850 blocked_fpu_registers_[i] = true;
1851 }
1852
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001853 if (GetGraph()->IsDebuggable()) {
1854 // Stubs do not save callee-save floating point registers. If the graph
1855 // is debuggable, we need to deal with these registers differently. For
1856 // now, just block them.
1857 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1858 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1859 }
1860 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001861}
1862
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1864 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1865 return kMipsWordSize;
1866}
1867
1868size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1869 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1870 return kMipsWordSize;
1871}
1872
1873size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001874 if (GetGraph()->HasSIMD()) {
1875 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1876 } else {
1877 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1878 }
1879 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001880}
1881
1882size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001883 if (GetGraph()->HasSIMD()) {
1884 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1885 } else {
1886 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1887 }
1888 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001889}
1890
1891void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001892 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001893}
1894
1895void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001896 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001897}
1898
Serban Constantinescufca16662016-07-14 09:21:59 +01001899constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1900
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1902 HInstruction* instruction,
1903 uint32_t dex_pc,
1904 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001905 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001906 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1907 IsDirectEntrypoint(entrypoint));
1908 if (EntrypointRequiresStackMap(entrypoint)) {
1909 RecordPcInfo(instruction, dex_pc, slow_path);
1910 }
1911}
1912
1913void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1914 HInstruction* instruction,
1915 SlowPathCode* slow_path,
1916 bool direct) {
1917 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1918 GenerateInvokeRuntime(entry_point_offset, direct);
1919}
1920
1921void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001922 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001923 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001924 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001925 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001926 // Reserve argument space on stack (for $a0-$a3) for
1927 // entrypoints that directly reference native implementations.
1928 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001929 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001930 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001931 } else {
1932 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001934 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001935}
1936
1937void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1938 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001939 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1940 const size_t status_byte_offset =
1941 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1942 constexpr uint32_t shifted_initialized_value =
1943 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1944
1945 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01001946 __ Sltiu(TMP, TMP, shifted_initialized_value);
1947 __ Bnez(TMP, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001948 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1949 __ Sync(0);
1950 __ Bind(slow_path->GetExitLabel());
1951}
1952
1953void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1954 __ Sync(0); // Only stype 0 is supported.
1955}
1956
1957void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1958 HBasicBlock* successor) {
1959 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001960 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1961
1962 if (slow_path == nullptr) {
1963 slow_path =
1964 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1965 instruction->SetSlowPath(slow_path);
1966 codegen_->AddSlowPath(slow_path);
1967 if (successor != nullptr) {
1968 DCHECK(successor->IsLoopHeader());
1969 }
1970 } else {
1971 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1972 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973
1974 __ LoadFromOffset(kLoadUnsignedHalfword,
1975 TMP,
1976 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001977 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001978 if (successor == nullptr) {
1979 __ Bnez(TMP, slow_path->GetEntryLabel());
1980 __ Bind(slow_path->GetReturnLabel());
1981 } else {
1982 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1983 __ B(slow_path->GetEntryLabel());
1984 // slow_path will return to GetLabelOf(successor).
1985 }
1986}
1987
1988InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1989 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001990 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001991 assembler_(codegen->GetAssembler()),
1992 codegen_(codegen) {}
1993
1994void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1995 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001996 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001997 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01001998 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001999 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002000 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002001 locations->SetInAt(0, Location::RequiresRegister());
2002 HInstruction* right = instruction->InputAt(1);
2003 bool can_use_imm = false;
2004 if (right->IsConstant()) {
2005 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2006 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2007 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002008 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002009 DCHECK(instruction->IsSub() || instruction->IsAdd());
2010 if (instruction->IsSub()) {
2011 imm = -imm;
2012 }
2013 if (isR6) {
2014 bool single_use = right->GetUses().HasExactlyOneElement();
2015 int16_t imm_high = High16Bits(imm);
2016 int16_t imm_low = Low16Bits(imm);
2017 if (imm_low < 0) {
2018 imm_high += 1;
2019 }
2020 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2021 } else {
2022 can_use_imm = IsInt<16>(imm);
2023 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 }
2025 }
2026 if (can_use_imm)
2027 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2028 else
2029 locations->SetInAt(1, Location::RequiresRegister());
2030 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2031 break;
2032 }
2033
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002034 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002035 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002036 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2037 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002038 break;
2039 }
2040
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 case DataType::Type::kFloat32:
2042 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002043 DCHECK(instruction->IsAdd() || instruction->IsSub());
2044 locations->SetInAt(0, Location::RequiresFpuRegister());
2045 locations->SetInAt(1, Location::RequiresFpuRegister());
2046 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2047 break;
2048
2049 default:
2050 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2051 }
2052}
2053
2054void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002055 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002056 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002057 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002058
2059 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002060 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002061 Register dst = locations->Out().AsRegister<Register>();
2062 Register lhs = locations->InAt(0).AsRegister<Register>();
2063 Location rhs_location = locations->InAt(1);
2064
2065 Register rhs_reg = ZERO;
2066 int32_t rhs_imm = 0;
2067 bool use_imm = rhs_location.IsConstant();
2068 if (use_imm) {
2069 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2070 } else {
2071 rhs_reg = rhs_location.AsRegister<Register>();
2072 }
2073
2074 if (instruction->IsAnd()) {
2075 if (use_imm)
2076 __ Andi(dst, lhs, rhs_imm);
2077 else
2078 __ And(dst, lhs, rhs_reg);
2079 } else if (instruction->IsOr()) {
2080 if (use_imm)
2081 __ Ori(dst, lhs, rhs_imm);
2082 else
2083 __ Or(dst, lhs, rhs_reg);
2084 } else if (instruction->IsXor()) {
2085 if (use_imm)
2086 __ Xori(dst, lhs, rhs_imm);
2087 else
2088 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002089 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002090 DCHECK(instruction->IsAdd() || instruction->IsSub());
2091 if (use_imm) {
2092 if (instruction->IsSub()) {
2093 rhs_imm = -rhs_imm;
2094 }
2095 if (IsInt<16>(rhs_imm)) {
2096 __ Addiu(dst, lhs, rhs_imm);
2097 } else {
2098 DCHECK(isR6);
2099 int16_t rhs_imm_high = High16Bits(rhs_imm);
2100 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2101 if (rhs_imm_low < 0) {
2102 rhs_imm_high += 1;
2103 }
2104 __ Aui(dst, lhs, rhs_imm_high);
2105 if (rhs_imm_low != 0) {
2106 __ Addiu(dst, dst, rhs_imm_low);
2107 }
2108 }
2109 } else if (instruction->IsAdd()) {
2110 __ Addu(dst, lhs, rhs_reg);
2111 } else {
2112 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002113 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002114 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002115 }
2116 break;
2117 }
2118
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002120 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2121 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2122 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2123 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002124 Location rhs_location = locations->InAt(1);
2125 bool use_imm = rhs_location.IsConstant();
2126 if (!use_imm) {
2127 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2128 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2129 if (instruction->IsAnd()) {
2130 __ And(dst_low, lhs_low, rhs_low);
2131 __ And(dst_high, lhs_high, rhs_high);
2132 } else if (instruction->IsOr()) {
2133 __ Or(dst_low, lhs_low, rhs_low);
2134 __ Or(dst_high, lhs_high, rhs_high);
2135 } else if (instruction->IsXor()) {
2136 __ Xor(dst_low, lhs_low, rhs_low);
2137 __ Xor(dst_high, lhs_high, rhs_high);
2138 } else if (instruction->IsAdd()) {
2139 if (lhs_low == rhs_low) {
2140 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2141 __ Slt(TMP, lhs_low, ZERO);
2142 __ Addu(dst_low, lhs_low, rhs_low);
2143 } else {
2144 __ Addu(dst_low, lhs_low, rhs_low);
2145 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2146 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2147 }
2148 __ Addu(dst_high, lhs_high, rhs_high);
2149 __ Addu(dst_high, dst_high, TMP);
2150 } else {
2151 DCHECK(instruction->IsSub());
2152 __ Sltu(TMP, lhs_low, rhs_low);
2153 __ Subu(dst_low, lhs_low, rhs_low);
2154 __ Subu(dst_high, lhs_high, rhs_high);
2155 __ Subu(dst_high, dst_high, TMP);
2156 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002157 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002158 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2159 if (instruction->IsOr()) {
2160 uint32_t low = Low32Bits(value);
2161 uint32_t high = High32Bits(value);
2162 if (IsUint<16>(low)) {
2163 if (dst_low != lhs_low || low != 0) {
2164 __ Ori(dst_low, lhs_low, low);
2165 }
2166 } else {
2167 __ LoadConst32(TMP, low);
2168 __ Or(dst_low, lhs_low, TMP);
2169 }
2170 if (IsUint<16>(high)) {
2171 if (dst_high != lhs_high || high != 0) {
2172 __ Ori(dst_high, lhs_high, high);
2173 }
2174 } else {
2175 if (high != low) {
2176 __ LoadConst32(TMP, high);
2177 }
2178 __ Or(dst_high, lhs_high, TMP);
2179 }
2180 } else if (instruction->IsXor()) {
2181 uint32_t low = Low32Bits(value);
2182 uint32_t high = High32Bits(value);
2183 if (IsUint<16>(low)) {
2184 if (dst_low != lhs_low || low != 0) {
2185 __ Xori(dst_low, lhs_low, low);
2186 }
2187 } else {
2188 __ LoadConst32(TMP, low);
2189 __ Xor(dst_low, lhs_low, TMP);
2190 }
2191 if (IsUint<16>(high)) {
2192 if (dst_high != lhs_high || high != 0) {
2193 __ Xori(dst_high, lhs_high, high);
2194 }
2195 } else {
2196 if (high != low) {
2197 __ LoadConst32(TMP, high);
2198 }
2199 __ Xor(dst_high, lhs_high, TMP);
2200 }
2201 } else if (instruction->IsAnd()) {
2202 uint32_t low = Low32Bits(value);
2203 uint32_t high = High32Bits(value);
2204 if (IsUint<16>(low)) {
2205 __ Andi(dst_low, lhs_low, low);
2206 } else if (low != 0xFFFFFFFF) {
2207 __ LoadConst32(TMP, low);
2208 __ And(dst_low, lhs_low, TMP);
2209 } else if (dst_low != lhs_low) {
2210 __ Move(dst_low, lhs_low);
2211 }
2212 if (IsUint<16>(high)) {
2213 __ Andi(dst_high, lhs_high, high);
2214 } else if (high != 0xFFFFFFFF) {
2215 if (high != low) {
2216 __ LoadConst32(TMP, high);
2217 }
2218 __ And(dst_high, lhs_high, TMP);
2219 } else if (dst_high != lhs_high) {
2220 __ Move(dst_high, lhs_high);
2221 }
2222 } else {
2223 if (instruction->IsSub()) {
2224 value = -value;
2225 } else {
2226 DCHECK(instruction->IsAdd());
2227 }
2228 int32_t low = Low32Bits(value);
2229 int32_t high = High32Bits(value);
2230 if (IsInt<16>(low)) {
2231 if (dst_low != lhs_low || low != 0) {
2232 __ Addiu(dst_low, lhs_low, low);
2233 }
2234 if (low != 0) {
2235 __ Sltiu(AT, dst_low, low);
2236 }
2237 } else {
2238 __ LoadConst32(TMP, low);
2239 __ Addu(dst_low, lhs_low, TMP);
2240 __ Sltu(AT, dst_low, TMP);
2241 }
2242 if (IsInt<16>(high)) {
2243 if (dst_high != lhs_high || high != 0) {
2244 __ Addiu(dst_high, lhs_high, high);
2245 }
2246 } else {
2247 if (high != low) {
2248 __ LoadConst32(TMP, high);
2249 }
2250 __ Addu(dst_high, lhs_high, TMP);
2251 }
2252 if (low != 0) {
2253 __ Addu(dst_high, dst_high, AT);
2254 }
2255 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002256 }
2257 break;
2258 }
2259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002260 case DataType::Type::kFloat32:
2261 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2263 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2264 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2265 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002267 __ AddS(dst, lhs, rhs);
2268 } else {
2269 __ AddD(dst, lhs, rhs);
2270 }
2271 } else {
2272 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002274 __ SubS(dst, lhs, rhs);
2275 } else {
2276 __ SubD(dst, lhs, rhs);
2277 }
2278 }
2279 break;
2280 }
2281
2282 default:
2283 LOG(FATAL) << "Unexpected binary operation type " << type;
2284 }
2285}
2286
2287void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002288 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002289
Vladimir Markoca6fff82017-10-03 14:49:14 +01002290 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002292 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002294 locations->SetInAt(0, Location::RequiresRegister());
2295 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2296 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2297 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002299 locations->SetInAt(0, Location::RequiresRegister());
2300 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2301 locations->SetOut(Location::RequiresRegister());
2302 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303 default:
2304 LOG(FATAL) << "Unexpected shift type " << type;
2305 }
2306}
2307
2308static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2309
2310void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002311 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002312 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002314
2315 Location rhs_location = locations->InAt(1);
2316 bool use_imm = rhs_location.IsConstant();
2317 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2318 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002319 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002321 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002322 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2323 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002324
2325 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002326 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002327 Register dst = locations->Out().AsRegister<Register>();
2328 Register lhs = locations->InAt(0).AsRegister<Register>();
2329 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002330 if (shift_value == 0) {
2331 if (dst != lhs) {
2332 __ Move(dst, lhs);
2333 }
2334 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002335 __ Sll(dst, lhs, shift_value);
2336 } else if (instr->IsShr()) {
2337 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002338 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002339 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002340 } else {
2341 if (has_ins_rotr) {
2342 __ Rotr(dst, lhs, shift_value);
2343 } else {
2344 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2345 __ Srl(dst, lhs, shift_value);
2346 __ Or(dst, dst, TMP);
2347 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002348 }
2349 } else {
2350 if (instr->IsShl()) {
2351 __ Sllv(dst, lhs, rhs_reg);
2352 } else if (instr->IsShr()) {
2353 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002354 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002355 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002356 } else {
2357 if (has_ins_rotr) {
2358 __ Rotrv(dst, lhs, rhs_reg);
2359 } else {
2360 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002361 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2362 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2363 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2364 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2365 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002366 __ Sllv(TMP, lhs, TMP);
2367 __ Srlv(dst, lhs, rhs_reg);
2368 __ Or(dst, dst, TMP);
2369 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002370 }
2371 }
2372 break;
2373 }
2374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002375 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2377 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2378 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2379 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2380 if (use_imm) {
2381 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002382 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002383 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002384 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002385 if (instr->IsShl()) {
2386 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2387 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2388 __ Sll(dst_low, lhs_low, shift_value);
2389 } else if (instr->IsShr()) {
2390 __ Srl(dst_low, lhs_low, shift_value);
2391 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2392 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002393 } else if (instr->IsUShr()) {
2394 __ Srl(dst_low, lhs_low, shift_value);
2395 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2396 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002397 } else {
2398 __ Srl(dst_low, lhs_low, shift_value);
2399 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2400 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002401 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002402 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002403 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002404 if (instr->IsShl()) {
2405 __ Sll(dst_low, lhs_low, shift_value);
2406 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2407 __ Sll(dst_high, lhs_high, shift_value);
2408 __ Or(dst_high, dst_high, TMP);
2409 } else if (instr->IsShr()) {
2410 __ Sra(dst_high, lhs_high, shift_value);
2411 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2412 __ Srl(dst_low, lhs_low, shift_value);
2413 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002414 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002415 __ Srl(dst_high, lhs_high, shift_value);
2416 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2417 __ Srl(dst_low, lhs_low, shift_value);
2418 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002419 } else {
2420 __ Srl(TMP, lhs_low, shift_value);
2421 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2422 __ Or(dst_low, dst_low, TMP);
2423 __ Srl(TMP, lhs_high, shift_value);
2424 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2425 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002426 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002427 }
2428 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002429 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002430 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002431 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432 __ Move(dst_low, ZERO);
2433 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002434 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002435 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002436 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002437 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002438 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002439 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002440 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002441 // 64-bit rotation by 32 is just a swap.
2442 __ Move(dst_low, lhs_high);
2443 __ Move(dst_high, lhs_low);
2444 } else {
2445 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002446 __ Srl(dst_low, lhs_high, shift_value_high);
2447 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2448 __ Srl(dst_high, lhs_low, shift_value_high);
2449 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002450 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002451 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2452 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002453 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002454 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2455 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002456 __ Or(dst_high, dst_high, TMP);
2457 }
2458 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002459 }
2460 }
2461 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002462 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002463 MipsLabel done;
2464 if (instr->IsShl()) {
2465 __ Sllv(dst_low, lhs_low, rhs_reg);
2466 __ Nor(AT, ZERO, rhs_reg);
2467 __ Srl(TMP, lhs_low, 1);
2468 __ Srlv(TMP, TMP, AT);
2469 __ Sllv(dst_high, lhs_high, rhs_reg);
2470 __ Or(dst_high, dst_high, TMP);
2471 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002472 if (isR6) {
2473 __ Beqzc(TMP, &done, /* is_bare */ true);
2474 __ Move(dst_high, dst_low);
2475 __ Move(dst_low, ZERO);
2476 } else {
2477 __ Movn(dst_high, dst_low, TMP);
2478 __ Movn(dst_low, ZERO, TMP);
2479 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002480 } else if (instr->IsShr()) {
2481 __ Srav(dst_high, lhs_high, rhs_reg);
2482 __ Nor(AT, ZERO, rhs_reg);
2483 __ Sll(TMP, lhs_high, 1);
2484 __ Sllv(TMP, TMP, AT);
2485 __ Srlv(dst_low, lhs_low, rhs_reg);
2486 __ Or(dst_low, dst_low, TMP);
2487 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002488 if (isR6) {
2489 __ Beqzc(TMP, &done, /* is_bare */ true);
2490 __ Move(dst_low, dst_high);
2491 __ Sra(dst_high, dst_high, 31);
2492 } else {
2493 __ Sra(AT, dst_high, 31);
2494 __ Movn(dst_low, dst_high, TMP);
2495 __ Movn(dst_high, AT, TMP);
2496 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002497 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002498 __ Srlv(dst_high, lhs_high, rhs_reg);
2499 __ Nor(AT, ZERO, rhs_reg);
2500 __ Sll(TMP, lhs_high, 1);
2501 __ Sllv(TMP, TMP, AT);
2502 __ Srlv(dst_low, lhs_low, rhs_reg);
2503 __ Or(dst_low, dst_low, TMP);
2504 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002505 if (isR6) {
2506 __ Beqzc(TMP, &done, /* is_bare */ true);
2507 __ Move(dst_low, dst_high);
2508 __ Move(dst_high, ZERO);
2509 } else {
2510 __ Movn(dst_low, dst_high, TMP);
2511 __ Movn(dst_high, ZERO, TMP);
2512 }
2513 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002514 __ Nor(AT, ZERO, rhs_reg);
2515 __ Srlv(TMP, lhs_low, rhs_reg);
2516 __ Sll(dst_low, lhs_high, 1);
2517 __ Sllv(dst_low, dst_low, AT);
2518 __ Or(dst_low, dst_low, TMP);
2519 __ Srlv(TMP, lhs_high, rhs_reg);
2520 __ Sll(dst_high, lhs_low, 1);
2521 __ Sllv(dst_high, dst_high, AT);
2522 __ Or(dst_high, dst_high, TMP);
2523 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002524 if (isR6) {
2525 __ Beqzc(TMP, &done, /* is_bare */ true);
2526 __ Move(TMP, dst_high);
2527 __ Move(dst_high, dst_low);
2528 __ Move(dst_low, TMP);
2529 } else {
2530 __ Movn(AT, dst_high, TMP);
2531 __ Movn(dst_high, dst_low, TMP);
2532 __ Movn(dst_low, AT, TMP);
2533 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002534 }
2535 __ Bind(&done);
2536 }
2537 break;
2538 }
2539
2540 default:
2541 LOG(FATAL) << "Unexpected shift operation type " << type;
2542 }
2543}
2544
2545void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2546 HandleBinaryOp(instruction);
2547}
2548
2549void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2550 HandleBinaryOp(instruction);
2551}
2552
2553void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2554 HandleBinaryOp(instruction);
2555}
2556
2557void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2558 HandleBinaryOp(instruction);
2559}
2560
2561void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002562 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002563 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002565 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002566 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2567 object_array_get_with_read_barrier
2568 ? LocationSummary::kCallOnSlowPath
2569 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002570 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2571 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2572 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002573 locations->SetInAt(0, Location::RequiresRegister());
2574 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002575 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002576 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2577 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002578 // The output overlaps in the case of an object array get with
2579 // read barriers enabled: we do not want the move to overwrite the
2580 // array's location, as we need it to emit the read barrier.
2581 locations->SetOut(Location::RequiresRegister(),
2582 object_array_get_with_read_barrier
2583 ? Location::kOutputOverlap
2584 : Location::kNoOutputOverlap);
2585 }
2586 // We need a temporary register for the read barrier marking slow
2587 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2588 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002589 bool temp_needed = instruction->GetIndex()->IsConstant()
2590 ? !kBakerReadBarrierThunksEnableForFields
2591 : !kBakerReadBarrierThunksEnableForArrays;
2592 if (temp_needed) {
2593 locations->AddTemp(Location::RequiresRegister());
2594 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002595 }
2596}
2597
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002598static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2599 auto null_checker = [codegen, instruction]() {
2600 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002601 };
2602 return null_checker;
2603}
2604
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002605void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2606 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002607 Location obj_loc = locations->InAt(0);
2608 Register obj = obj_loc.AsRegister<Register>();
2609 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002610 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002611 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002612 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002613
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002614 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002615 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2616 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002618 case DataType::Type::kBool:
2619 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002620 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002621 if (index.IsConstant()) {
2622 size_t offset =
2623 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002624 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002625 } else {
2626 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002627 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002628 }
2629 break;
2630 }
2631
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002632 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002633 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002634 if (index.IsConstant()) {
2635 size_t offset =
2636 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002637 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002638 } else {
2639 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002640 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002641 }
2642 break;
2643 }
2644
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002645 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002646 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002647 if (maybe_compressed_char_at) {
2648 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2649 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2650 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2651 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2652 "Expecting 0=compressed, 1=uncompressed");
2653 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002654 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002655 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2656 if (maybe_compressed_char_at) {
2657 MipsLabel uncompressed_load, done;
2658 __ Bnez(TMP, &uncompressed_load);
2659 __ LoadFromOffset(kLoadUnsignedByte,
2660 out,
2661 obj,
2662 data_offset + (const_index << TIMES_1));
2663 __ B(&done);
2664 __ Bind(&uncompressed_load);
2665 __ LoadFromOffset(kLoadUnsignedHalfword,
2666 out,
2667 obj,
2668 data_offset + (const_index << TIMES_2));
2669 __ Bind(&done);
2670 } else {
2671 __ LoadFromOffset(kLoadUnsignedHalfword,
2672 out,
2673 obj,
2674 data_offset + (const_index << TIMES_2),
2675 null_checker);
2676 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002677 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002678 Register index_reg = index.AsRegister<Register>();
2679 if (maybe_compressed_char_at) {
2680 MipsLabel uncompressed_load, done;
2681 __ Bnez(TMP, &uncompressed_load);
2682 __ Addu(TMP, obj, index_reg);
2683 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2684 __ B(&done);
2685 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002686 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002687 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2688 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002689 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2690 __ Addu(TMP, index_reg, obj);
2691 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002692 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002693 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002694 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2695 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 }
2697 break;
2698 }
2699
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002700 case DataType::Type::kInt16: {
2701 Register out = out_loc.AsRegister<Register>();
2702 if (index.IsConstant()) {
2703 size_t offset =
2704 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2705 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002706 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2707 __ Addu(TMP, index.AsRegister<Register>(), obj);
2708 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002709 } else {
2710 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2711 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2712 }
2713 break;
2714 }
2715
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002716 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002717 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002718 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002719 if (index.IsConstant()) {
2720 size_t offset =
2721 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002722 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002723 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2724 __ Addu(TMP, index.AsRegister<Register>(), obj);
2725 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002726 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002727 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002728 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002729 }
2730 break;
2731 }
2732
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002733 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002734 static_assert(
2735 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2736 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2737 // /* HeapReference<Object> */ out =
2738 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2739 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002740 bool temp_needed = index.IsConstant()
2741 ? !kBakerReadBarrierThunksEnableForFields
2742 : !kBakerReadBarrierThunksEnableForArrays;
2743 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002744 // Note that a potential implicit null check is handled in this
2745 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002746 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2747 if (index.IsConstant()) {
2748 // Array load with a constant index can be treated as a field load.
2749 size_t offset =
2750 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2751 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2752 out_loc,
2753 obj,
2754 offset,
2755 temp,
2756 /* needs_null_check */ false);
2757 } else {
2758 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2759 out_loc,
2760 obj,
2761 data_offset,
2762 index,
2763 temp,
2764 /* needs_null_check */ false);
2765 }
Alexey Frunze15958152017-02-09 19:08:30 -08002766 } else {
2767 Register out = out_loc.AsRegister<Register>();
2768 if (index.IsConstant()) {
2769 size_t offset =
2770 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2771 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2772 // If read barriers are enabled, emit read barriers other than
2773 // Baker's using a slow path (and also unpoison the loaded
2774 // reference, if heap poisoning is enabled).
2775 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2776 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002777 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002778 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2779 // If read barriers are enabled, emit read barriers other than
2780 // Baker's using a slow path (and also unpoison the loaded
2781 // reference, if heap poisoning is enabled).
2782 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2783 out_loc,
2784 out_loc,
2785 obj_loc,
2786 data_offset,
2787 index);
2788 }
2789 }
2790 break;
2791 }
2792
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002793 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002794 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002795 if (index.IsConstant()) {
2796 size_t offset =
2797 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002798 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002799 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2800 __ Addu(TMP, index.AsRegister<Register>(), obj);
2801 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002802 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002803 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002804 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002805 }
2806 break;
2807 }
2808
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002809 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002810 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002811 if (index.IsConstant()) {
2812 size_t offset =
2813 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002814 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002815 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2816 __ Addu(TMP, index.AsRegister<Register>(), obj);
2817 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002818 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002819 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002820 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002821 }
2822 break;
2823 }
2824
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002825 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002826 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002827 if (index.IsConstant()) {
2828 size_t offset =
2829 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002830 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002831 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2832 __ Addu(TMP, index.AsRegister<Register>(), obj);
2833 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002834 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002835 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002836 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002837 }
2838 break;
2839 }
2840
Aart Bik66c158e2018-01-31 12:55:04 -08002841 case DataType::Type::kUint32:
2842 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002843 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002844 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2845 UNREACHABLE();
2846 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002847}
2848
2849void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002850 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002851 locations->SetInAt(0, Location::RequiresRegister());
2852 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2853}
2854
2855void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2856 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002857 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002858 Register obj = locations->InAt(0).AsRegister<Register>();
2859 Register out = locations->Out().AsRegister<Register>();
2860 __ LoadFromOffset(kLoadWord, out, obj, offset);
2861 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002862 // Mask out compression flag from String's array length.
2863 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2864 __ Srl(out, out, 1u);
2865 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002866}
2867
Alexey Frunzef58b2482016-09-02 22:14:06 -07002868Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2869 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2870 ? Location::ConstantLocation(instruction->AsConstant())
2871 : Location::RequiresRegister();
2872}
2873
2874Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2875 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2876 // We can store a non-zero float or double constant without first loading it into the FPU,
2877 // but we should only prefer this if the constant has a single use.
2878 if (instruction->IsConstant() &&
2879 (instruction->AsConstant()->IsZeroBitPattern() ||
2880 instruction->GetUses().HasExactlyOneElement())) {
2881 return Location::ConstantLocation(instruction->AsConstant());
2882 // Otherwise fall through and require an FPU register for the constant.
2883 }
2884 return Location::RequiresFpuRegister();
2885}
2886
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002887void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002889
2890 bool needs_write_barrier =
2891 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2892 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2893
Vladimir Markoca6fff82017-10-03 14:49:14 +01002894 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002895 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002896 may_need_runtime_call_for_type_check ?
2897 LocationSummary::kCallOnSlowPath :
2898 LocationSummary::kNoCall);
2899
2900 locations->SetInAt(0, Location::RequiresRegister());
2901 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002902 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002903 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002904 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002905 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2906 }
2907 if (needs_write_barrier) {
2908 // Temporary register for the write barrier.
2909 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002910 }
2911}
2912
2913void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2914 LocationSummary* locations = instruction->GetLocations();
2915 Register obj = locations->InAt(0).AsRegister<Register>();
2916 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002917 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002919 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002920 bool needs_write_barrier =
2921 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002922 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002923 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002924
2925 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002926 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002927 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002929 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002930 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002931 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002932 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002933 __ Addu(base_reg, obj, index.AsRegister<Register>());
2934 }
2935 if (value_location.IsConstant()) {
2936 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2937 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2938 } else {
2939 Register value = value_location.AsRegister<Register>();
2940 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002941 }
2942 break;
2943 }
2944
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002945 case DataType::Type::kUint16:
2946 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002947 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002948 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002949 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002950 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2951 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002952 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002953 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002954 }
2955 if (value_location.IsConstant()) {
2956 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2957 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2958 } else {
2959 Register value = value_location.AsRegister<Register>();
2960 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002961 }
2962 break;
2963 }
2964
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002965 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002966 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2967 if (index.IsConstant()) {
2968 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002969 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2970 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002971 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002972 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002973 }
2974 if (value_location.IsConstant()) {
2975 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2976 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2977 } else {
2978 Register value = value_location.AsRegister<Register>();
2979 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2980 }
2981 break;
2982 }
2983
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002984 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002985 if (value_location.IsConstant()) {
2986 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002987 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002988 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002989 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002990 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002991 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002992 }
Alexey Frunze15958152017-02-09 19:08:30 -08002993 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2994 DCHECK_EQ(value, 0);
2995 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2996 DCHECK(!needs_write_barrier);
2997 DCHECK(!may_need_runtime_call_for_type_check);
2998 break;
2999 }
3000
3001 DCHECK(needs_write_barrier);
3002 Register value = value_location.AsRegister<Register>();
3003 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3004 Register temp2 = TMP; // Doesn't need to survive slow path.
3005 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3006 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3007 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3008 MipsLabel done;
3009 SlowPathCodeMIPS* slow_path = nullptr;
3010
3011 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003012 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003013 codegen_->AddSlowPath(slow_path);
3014 if (instruction->GetValueCanBeNull()) {
3015 MipsLabel non_zero;
3016 __ Bnez(value, &non_zero);
3017 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3018 if (index.IsConstant()) {
3019 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003020 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3021 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003022 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003023 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003024 }
Alexey Frunze15958152017-02-09 19:08:30 -08003025 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3026 __ B(&done);
3027 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003028 }
Alexey Frunze15958152017-02-09 19:08:30 -08003029
3030 // Note that when read barriers are enabled, the type checks
3031 // are performed without read barriers. This is fine, even in
3032 // the case where a class object is in the from-space after
3033 // the flip, as a comparison involving such a type would not
3034 // produce a false positive; it may of course produce a false
3035 // negative, in which case we would take the ArraySet slow
3036 // path.
3037
3038 // /* HeapReference<Class> */ temp1 = obj->klass_
3039 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3040 __ MaybeUnpoisonHeapReference(temp1);
3041
3042 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3043 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3044 // /* HeapReference<Class> */ temp2 = value->klass_
3045 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3046 // If heap poisoning is enabled, no need to unpoison `temp1`
3047 // nor `temp2`, as we are comparing two poisoned references.
3048
3049 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3050 MipsLabel do_put;
3051 __ Beq(temp1, temp2, &do_put);
3052 // If heap poisoning is enabled, the `temp1` reference has
3053 // not been unpoisoned yet; unpoison it now.
3054 __ MaybeUnpoisonHeapReference(temp1);
3055
3056 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3057 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3058 // If heap poisoning is enabled, no need to unpoison
3059 // `temp1`, as we are comparing against null below.
3060 __ Bnez(temp1, slow_path->GetEntryLabel());
3061 __ Bind(&do_put);
3062 } else {
3063 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3064 }
3065 }
3066
3067 Register source = value;
3068 if (kPoisonHeapReferences) {
3069 // Note that in the case where `value` is a null reference,
3070 // we do not enter this block, as a null reference does not
3071 // need poisoning.
3072 __ Move(temp1, value);
3073 __ PoisonHeapReference(temp1);
3074 source = temp1;
3075 }
3076
3077 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3078 if (index.IsConstant()) {
3079 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003080 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003081 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003082 }
3083 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3084
3085 if (!may_need_runtime_call_for_type_check) {
3086 codegen_->MaybeRecordImplicitNullCheck(instruction);
3087 }
3088
3089 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3090
3091 if (done.IsLinked()) {
3092 __ Bind(&done);
3093 }
3094
3095 if (slow_path != nullptr) {
3096 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003097 }
3098 break;
3099 }
3100
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003101 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003102 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003103 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003104 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003105 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3106 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003107 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003108 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003109 }
3110 if (value_location.IsConstant()) {
3111 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3112 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3113 } else {
3114 Register value = value_location.AsRegisterPairLow<Register>();
3115 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003116 }
3117 break;
3118 }
3119
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003120 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003121 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003122 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003123 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003124 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3125 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003126 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003127 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003128 }
3129 if (value_location.IsConstant()) {
3130 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3131 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3132 } else {
3133 FRegister value = value_location.AsFpuRegister<FRegister>();
3134 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003135 }
3136 break;
3137 }
3138
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003139 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003140 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003141 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003142 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003143 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3144 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003145 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003146 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003147 }
3148 if (value_location.IsConstant()) {
3149 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3150 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3151 } else {
3152 FRegister value = value_location.AsFpuRegister<FRegister>();
3153 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003154 }
3155 break;
3156 }
3157
Aart Bik66c158e2018-01-31 12:55:04 -08003158 case DataType::Type::kUint32:
3159 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003160 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003161 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3162 UNREACHABLE();
3163 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003164}
3165
Lena Djokica2901602017-09-21 13:50:52 +02003166void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3167 HIntermediateArrayAddressIndex* instruction) {
3168 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003169 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003170
3171 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3172
3173 locations->SetInAt(0, Location::RequiresRegister());
3174 locations->SetInAt(1, Location::ConstantLocation(shift));
3175 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3176}
3177
3178void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3179 HIntermediateArrayAddressIndex* instruction) {
3180 LocationSummary* locations = instruction->GetLocations();
3181 Register index_reg = locations->InAt(0).AsRegister<Register>();
3182 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3183 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3184}
3185
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003186void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003187 RegisterSet caller_saves = RegisterSet::Empty();
3188 InvokeRuntimeCallingConvention calling_convention;
3189 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3190 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3191 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003192
3193 HInstruction* index = instruction->InputAt(0);
3194 HInstruction* length = instruction->InputAt(1);
3195
3196 bool const_index = false;
3197 bool const_length = false;
3198
3199 if (index->IsConstant()) {
3200 if (length->IsConstant()) {
3201 const_index = true;
3202 const_length = true;
3203 } else {
3204 int32_t index_value = index->AsIntConstant()->GetValue();
3205 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3206 const_index = true;
3207 }
3208 }
3209 } else if (length->IsConstant()) {
3210 int32_t length_value = length->AsIntConstant()->GetValue();
3211 if (IsUint<15>(length_value)) {
3212 const_length = true;
3213 }
3214 }
3215
3216 locations->SetInAt(0, const_index
3217 ? Location::ConstantLocation(index->AsConstant())
3218 : Location::RequiresRegister());
3219 locations->SetInAt(1, const_length
3220 ? Location::ConstantLocation(length->AsConstant())
3221 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003222}
3223
3224void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3225 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003226 Location index_loc = locations->InAt(0);
3227 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003228
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003229 if (length_loc.IsConstant()) {
3230 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3231 if (index_loc.IsConstant()) {
3232 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3233 if (index < 0 || index >= length) {
3234 BoundsCheckSlowPathMIPS* slow_path =
3235 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3236 codegen_->AddSlowPath(slow_path);
3237 __ B(slow_path->GetEntryLabel());
3238 } else {
3239 // Nothing to be done.
3240 }
3241 return;
3242 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003243
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003244 BoundsCheckSlowPathMIPS* slow_path =
3245 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3246 codegen_->AddSlowPath(slow_path);
3247 Register index = index_loc.AsRegister<Register>();
3248 if (length == 0) {
3249 __ B(slow_path->GetEntryLabel());
3250 } else if (length == 1) {
3251 __ Bnez(index, slow_path->GetEntryLabel());
3252 } else {
3253 DCHECK(IsUint<15>(length)) << length;
3254 __ Sltiu(TMP, index, length);
3255 __ Beqz(TMP, slow_path->GetEntryLabel());
3256 }
3257 } else {
3258 Register length = length_loc.AsRegister<Register>();
3259 BoundsCheckSlowPathMIPS* slow_path =
3260 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3261 codegen_->AddSlowPath(slow_path);
3262 if (index_loc.IsConstant()) {
3263 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3264 if (index < 0) {
3265 __ B(slow_path->GetEntryLabel());
3266 } else if (index == 0) {
3267 __ Blez(length, slow_path->GetEntryLabel());
3268 } else {
3269 DCHECK(IsInt<16>(index + 1)) << index;
3270 __ Sltiu(TMP, length, index + 1);
3271 __ Bnez(TMP, slow_path->GetEntryLabel());
3272 }
3273 } else {
3274 Register index = index_loc.AsRegister<Register>();
3275 __ Bgeu(index, length, slow_path->GetEntryLabel());
3276 }
3277 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003278}
3279
Alexey Frunze15958152017-02-09 19:08:30 -08003280// Temp is used for read barrier.
3281static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3282 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003283 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003284 (kUseBakerReadBarrier ||
3285 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3286 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3287 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3288 return 1;
3289 }
3290 return 0;
3291}
3292
3293// Extra temp is used for read barrier.
3294static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3295 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3296}
3297
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003298void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003299 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003300 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003301 LocationSummary* locations =
3302 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003303 locations->SetInAt(0, Location::RequiresRegister());
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00003304 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003305 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003306}
3307
3308void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003309 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003310 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003311 Location obj_loc = locations->InAt(0);
3312 Register obj = obj_loc.AsRegister<Register>();
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00003313 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003314 Location temp_loc = locations->GetTemp(0);
3315 Register temp = temp_loc.AsRegister<Register>();
3316 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3317 DCHECK_LE(num_temps, 2u);
3318 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003319 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3320 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3321 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3322 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3323 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3324 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3325 const uint32_t object_array_data_offset =
3326 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3327 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003328
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003329 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003330 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003331 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3332 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003333 codegen_->AddSlowPath(slow_path);
3334
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003335 // Avoid this check if we know `obj` is not null.
3336 if (instruction->MustDoNullCheck()) {
3337 __ Beqz(obj, &done);
3338 }
3339
3340 switch (type_check_kind) {
3341 case TypeCheckKind::kExactCheck:
3342 case TypeCheckKind::kArrayCheck: {
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 // Jump to slow path for throwing the exception or doing a
3351 // more involved array check.
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00003352 __ Bne(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003353 break;
3354 }
3355
3356 case TypeCheckKind::kAbstractClassCheck: {
3357 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003358 GenerateReferenceLoadTwoRegisters(instruction,
3359 temp_loc,
3360 obj_loc,
3361 class_offset,
3362 maybe_temp2_loc,
3363 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003364 // If the class is abstract, we eagerly fetch the super class of the
3365 // object to avoid doing a comparison we know will fail.
3366 MipsLabel loop;
3367 __ Bind(&loop);
3368 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003369 GenerateReferenceLoadOneRegister(instruction,
3370 temp_loc,
3371 super_offset,
3372 maybe_temp2_loc,
3373 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003374 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3375 // exception.
3376 __ Beqz(temp, slow_path->GetEntryLabel());
3377 // Otherwise, compare the classes.
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00003378 __ Bne(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003379 break;
3380 }
3381
3382 case TypeCheckKind::kClassHierarchyCheck: {
3383 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003384 GenerateReferenceLoadTwoRegisters(instruction,
3385 temp_loc,
3386 obj_loc,
3387 class_offset,
3388 maybe_temp2_loc,
3389 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003390 // Walk over the class hierarchy to find a match.
3391 MipsLabel loop;
3392 __ Bind(&loop);
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00003393 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003394 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003395 GenerateReferenceLoadOneRegister(instruction,
3396 temp_loc,
3397 super_offset,
3398 maybe_temp2_loc,
3399 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003400 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3401 // exception. Otherwise, jump to the beginning of the loop.
3402 __ Bnez(temp, &loop);
3403 __ B(slow_path->GetEntryLabel());
3404 break;
3405 }
3406
3407 case TypeCheckKind::kArrayObjectCheck: {
3408 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003409 GenerateReferenceLoadTwoRegisters(instruction,
3410 temp_loc,
3411 obj_loc,
3412 class_offset,
3413 maybe_temp2_loc,
3414 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003415 // Do an exact check.
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00003416 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003417 // Otherwise, we need to check that the object's class is a non-primitive array.
3418 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003419 GenerateReferenceLoadOneRegister(instruction,
3420 temp_loc,
3421 component_offset,
3422 maybe_temp2_loc,
3423 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003424 // If the component type is null, jump to the slow path to throw the exception.
3425 __ Beqz(temp, slow_path->GetEntryLabel());
3426 // Otherwise, the object is indeed an array, further check that this component
3427 // type is not a primitive type.
3428 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3429 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3430 __ Bnez(temp, slow_path->GetEntryLabel());
3431 break;
3432 }
3433
3434 case TypeCheckKind::kUnresolvedCheck:
3435 // We always go into the type check slow path for the unresolved check case.
3436 // We cannot directly call the CheckCast runtime entry point
3437 // without resorting to a type checking slow path here (i.e. by
3438 // calling InvokeRuntime directly), as it would require to
3439 // assign fixed registers for the inputs of this HInstanceOf
3440 // instruction (following the runtime calling convention), which
3441 // might be cluttered by the potential first read barrier
3442 // emission at the beginning of this method.
3443 __ B(slow_path->GetEntryLabel());
3444 break;
3445
3446 case TypeCheckKind::kInterfaceCheck: {
3447 // Avoid read barriers to improve performance of the fast path. We can not get false
3448 // positives by doing this.
3449 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003450 GenerateReferenceLoadTwoRegisters(instruction,
3451 temp_loc,
3452 obj_loc,
3453 class_offset,
3454 maybe_temp2_loc,
3455 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003456 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003457 GenerateReferenceLoadTwoRegisters(instruction,
3458 temp_loc,
3459 temp_loc,
3460 iftable_offset,
3461 maybe_temp2_loc,
3462 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003463 // Iftable is never null.
3464 __ Lw(TMP, temp, array_length_offset);
3465 // Loop through the iftable and check if any class matches.
3466 MipsLabel loop;
3467 __ Bind(&loop);
3468 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3469 __ Beqz(TMP, slow_path->GetEntryLabel());
3470 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3471 __ MaybeUnpoisonHeapReference(AT);
3472 // Go to next interface.
3473 __ Addiu(TMP, TMP, -2);
3474 // Compare the classes and continue the loop if they do not match.
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00003475 __ Bne(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003476 break;
3477 }
3478 }
3479
3480 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003481 __ Bind(slow_path->GetExitLabel());
3482}
3483
3484void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3485 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003486 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003487 locations->SetInAt(0, Location::RequiresRegister());
3488 if (check->HasUses()) {
3489 locations->SetOut(Location::SameAsFirstInput());
3490 }
3491}
3492
3493void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3494 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003495 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003496 check->GetLoadClass(),
3497 check,
3498 check->GetDexPc(),
3499 true);
3500 codegen_->AddSlowPath(slow_path);
3501 GenerateClassInitializationCheck(slow_path,
3502 check->GetLocations()->InAt(0).AsRegister<Register>());
3503}
3504
3505void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003506 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003507
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003508 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003509 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003510
3511 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003513 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003514 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003515 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003516 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003517 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003518 locations->SetInAt(0, Location::RequiresRegister());
3519 locations->SetInAt(1, Location::RequiresRegister());
3520 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3521 break;
3522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003523 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003524 locations->SetInAt(0, Location::RequiresRegister());
3525 locations->SetInAt(1, Location::RequiresRegister());
3526 // Output overlaps because it is written before doing the low comparison.
3527 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3528 break;
3529
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003530 case DataType::Type::kFloat32:
3531 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003532 locations->SetInAt(0, Location::RequiresFpuRegister());
3533 locations->SetInAt(1, Location::RequiresFpuRegister());
3534 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003535 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003536
3537 default:
3538 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3539 }
3540}
3541
3542void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3543 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003544 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003545 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003546 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003547
3548 // 0 if: left == right
3549 // 1 if: left > right
3550 // -1 if: left < right
3551 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003552 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003553 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003554 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003555 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003556 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003557 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003558 Register lhs = locations->InAt(0).AsRegister<Register>();
3559 Register rhs = locations->InAt(1).AsRegister<Register>();
3560 __ Slt(TMP, lhs, rhs);
3561 __ Slt(res, rhs, lhs);
3562 __ Subu(res, res, TMP);
3563 break;
3564 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003565 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003566 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003567 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3568 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3569 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3570 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3571 // TODO: more efficient (direct) comparison with a constant.
3572 __ Slt(TMP, lhs_high, rhs_high);
3573 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3574 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3575 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3576 __ Sltu(TMP, lhs_low, rhs_low);
3577 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3578 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3579 __ Bind(&done);
3580 break;
3581 }
3582
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003583 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003584 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003585 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3586 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3587 MipsLabel done;
3588 if (isR6) {
3589 __ CmpEqS(FTMP, lhs, rhs);
3590 __ LoadConst32(res, 0);
3591 __ Bc1nez(FTMP, &done);
3592 if (gt_bias) {
3593 __ CmpLtS(FTMP, lhs, rhs);
3594 __ LoadConst32(res, -1);
3595 __ Bc1nez(FTMP, &done);
3596 __ LoadConst32(res, 1);
3597 } else {
3598 __ CmpLtS(FTMP, rhs, lhs);
3599 __ LoadConst32(res, 1);
3600 __ Bc1nez(FTMP, &done);
3601 __ LoadConst32(res, -1);
3602 }
3603 } else {
3604 if (gt_bias) {
3605 __ ColtS(0, lhs, rhs);
3606 __ LoadConst32(res, -1);
3607 __ Bc1t(0, &done);
3608 __ CeqS(0, lhs, rhs);
3609 __ LoadConst32(res, 1);
3610 __ Movt(res, ZERO, 0);
3611 } else {
3612 __ ColtS(0, rhs, lhs);
3613 __ LoadConst32(res, 1);
3614 __ Bc1t(0, &done);
3615 __ CeqS(0, lhs, rhs);
3616 __ LoadConst32(res, -1);
3617 __ Movt(res, ZERO, 0);
3618 }
3619 }
3620 __ Bind(&done);
3621 break;
3622 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003623 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003624 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003625 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3626 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3627 MipsLabel done;
3628 if (isR6) {
3629 __ CmpEqD(FTMP, lhs, rhs);
3630 __ LoadConst32(res, 0);
3631 __ Bc1nez(FTMP, &done);
3632 if (gt_bias) {
3633 __ CmpLtD(FTMP, lhs, rhs);
3634 __ LoadConst32(res, -1);
3635 __ Bc1nez(FTMP, &done);
3636 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003637 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003638 __ CmpLtD(FTMP, rhs, lhs);
3639 __ LoadConst32(res, 1);
3640 __ Bc1nez(FTMP, &done);
3641 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003642 }
3643 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003644 if (gt_bias) {
3645 __ ColtD(0, lhs, rhs);
3646 __ LoadConst32(res, -1);
3647 __ Bc1t(0, &done);
3648 __ CeqD(0, lhs, rhs);
3649 __ LoadConst32(res, 1);
3650 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003651 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003652 __ ColtD(0, rhs, lhs);
3653 __ LoadConst32(res, 1);
3654 __ Bc1t(0, &done);
3655 __ CeqD(0, lhs, rhs);
3656 __ LoadConst32(res, -1);
3657 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003658 }
3659 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003660 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003661 break;
3662 }
3663
3664 default:
3665 LOG(FATAL) << "Unimplemented compare type " << in_type;
3666 }
3667}
3668
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003669void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003670 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003671 switch (instruction->InputAt(0)->GetType()) {
3672 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003673 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003674 locations->SetInAt(0, Location::RequiresRegister());
3675 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3676 break;
3677
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003678 case DataType::Type::kFloat32:
3679 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003680 locations->SetInAt(0, Location::RequiresFpuRegister());
3681 locations->SetInAt(1, Location::RequiresFpuRegister());
3682 break;
3683 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003684 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003685 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3686 }
3687}
3688
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003689void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003690 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003691 return;
3692 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003694 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003695 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003696
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003697 switch (type) {
3698 default:
3699 // Integer case.
3700 GenerateIntCompare(instruction->GetCondition(), locations);
3701 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003702
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003703 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003704 GenerateLongCompare(instruction->GetCondition(), locations);
3705 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003706
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003707 case DataType::Type::kFloat32:
3708 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003709 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3710 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003711 }
3712}
3713
Alexey Frunze7e99e052015-11-24 19:28:01 -08003714void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3715 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003716
3717 LocationSummary* locations = instruction->GetLocations();
3718 Location second = locations->InAt(1);
3719 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003720 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003721 DCHECK(imm == 1 || imm == -1);
3722
Lena Djokic4b8025c2017-12-21 16:15:50 +01003723 if (instruction->GetResultType() == DataType::Type::kInt32) {
3724 Register out = locations->Out().AsRegister<Register>();
3725 Register dividend = locations->InAt(0).AsRegister<Register>();
3726
3727 if (instruction->IsRem()) {
3728 __ Move(out, ZERO);
3729 } else {
3730 if (imm == -1) {
3731 __ Subu(out, ZERO, dividend);
3732 } else if (out != dividend) {
3733 __ Move(out, dividend);
3734 }
3735 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003736 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003737 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3738 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3739 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3740 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3741 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3742
3743 if (instruction->IsRem()) {
3744 __ Move(out_high, ZERO);
3745 __ Move(out_low, ZERO);
3746 } else {
3747 if (imm == -1) {
3748 __ Subu(out_low, ZERO, in_low);
3749 __ Sltu(AT, ZERO, out_low);
3750 __ Subu(out_high, ZERO, in_high);
3751 __ Subu(out_high, out_high, AT);
3752 } else {
3753 __ Move(out_low, in_low);
3754 __ Move(out_high, in_high);
3755 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003756 }
3757 }
3758}
3759
3760void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3761 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003762
3763 LocationSummary* locations = instruction->GetLocations();
3764 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003765 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3766 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003767 DCHECK(second.IsConstant());
3768
Lena Djokic4b8025c2017-12-21 16:15:50 +01003769 if (instruction->GetResultType() == DataType::Type::kInt32) {
3770 Register out = locations->Out().AsRegister<Register>();
3771 Register dividend = locations->InAt(0).AsRegister<Register>();
3772 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3773 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3774 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003775
Lena Djokic4b8025c2017-12-21 16:15:50 +01003776 if (instruction->IsDiv()) {
3777 if (ctz_imm == 1) {
3778 // Fast path for division by +/-2, which is very common.
3779 __ Srl(TMP, dividend, 31);
3780 } else {
3781 __ Sra(TMP, dividend, 31);
3782 __ Srl(TMP, TMP, 32 - ctz_imm);
3783 }
3784 __ Addu(out, dividend, TMP);
3785 __ Sra(out, out, ctz_imm);
3786 if (imm < 0) {
3787 __ Subu(out, ZERO, out);
3788 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003789 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003790 if (ctz_imm == 1) {
3791 // Fast path for modulo +/-2, which is very common.
3792 __ Sra(TMP, dividend, 31);
3793 __ Subu(out, dividend, TMP);
3794 __ Andi(out, out, 1);
3795 __ Addu(out, out, TMP);
3796 } else {
3797 __ Sra(TMP, dividend, 31);
3798 __ Srl(TMP, TMP, 32 - ctz_imm);
3799 __ Addu(out, dividend, TMP);
3800 if (IsUint<16>(abs_imm - 1)) {
3801 __ Andi(out, out, abs_imm - 1);
3802 } else {
3803 if (is_r2_or_newer) {
3804 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3805 } else {
3806 __ Sll(out, out, 32 - ctz_imm);
3807 __ Srl(out, out, 32 - ctz_imm);
3808 }
3809 }
3810 __ Subu(out, out, TMP);
3811 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003812 }
3813 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003814 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3815 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3816 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3817 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3818 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3819 int64_t imm = Int64FromConstant(second.GetConstant());
3820 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3821 int ctz_imm = CTZ(abs_imm);
3822
3823 if (instruction->IsDiv()) {
3824 if (ctz_imm < 32) {
3825 if (ctz_imm == 1) {
3826 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003827 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003828 __ Sra(AT, in_high, 31);
3829 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003830 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003831 __ Addu(AT, AT, in_low);
3832 __ Sltu(TMP, AT, in_low);
3833 __ Addu(out_high, in_high, TMP);
3834 __ Srl(out_low, AT, ctz_imm);
3835 if (is_r2_or_newer) {
3836 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3837 __ Sra(out_high, out_high, ctz_imm);
3838 } else {
3839 __ Sll(AT, out_high, 32 - ctz_imm);
3840 __ Sra(out_high, out_high, ctz_imm);
3841 __ Or(out_low, out_low, AT);
3842 }
3843 if (imm < 0) {
3844 __ Subu(out_low, ZERO, out_low);
3845 __ Sltu(AT, ZERO, out_low);
3846 __ Subu(out_high, ZERO, out_high);
3847 __ Subu(out_high, out_high, AT);
3848 }
3849 } else if (ctz_imm == 32) {
3850 __ Sra(AT, in_high, 31);
3851 __ Addu(AT, AT, in_low);
3852 __ Sltu(AT, AT, in_low);
3853 __ Addu(out_low, in_high, AT);
3854 if (imm < 0) {
3855 __ Srl(TMP, out_low, 31);
3856 __ Subu(out_low, ZERO, out_low);
3857 __ Sltu(AT, ZERO, out_low);
3858 __ Subu(out_high, TMP, AT);
3859 } else {
3860 __ Sra(out_high, out_low, 31);
3861 }
3862 } else if (ctz_imm < 63) {
3863 __ Sra(AT, in_high, 31);
3864 __ Srl(TMP, AT, 64 - ctz_imm);
3865 __ Addu(AT, AT, in_low);
3866 __ Sltu(AT, AT, in_low);
3867 __ Addu(out_low, in_high, AT);
3868 __ Addu(out_low, out_low, TMP);
3869 __ Sra(out_low, out_low, ctz_imm - 32);
3870 if (imm < 0) {
3871 __ Subu(out_low, ZERO, out_low);
3872 }
3873 __ Sra(out_high, out_low, 31);
3874 } else {
3875 DCHECK_LT(imm, 0);
3876 if (is_r6) {
3877 __ Aui(AT, in_high, 0x8000);
3878 } else {
3879 __ Lui(AT, 0x8000);
3880 __ Xor(AT, AT, in_high);
3881 }
3882 __ Or(AT, AT, in_low);
3883 __ Sltiu(out_low, AT, 1);
3884 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003885 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003886 } else {
3887 if ((ctz_imm == 1) && !is_r6) {
3888 __ Andi(AT, in_low, 1);
3889 __ Sll(TMP, in_low, 31);
3890 __ And(TMP, in_high, TMP);
3891 __ Sra(out_high, TMP, 31);
3892 __ Or(out_low, out_high, AT);
3893 } else if (ctz_imm < 32) {
3894 __ Sra(AT, in_high, 31);
3895 if (ctz_imm <= 16) {
3896 __ Andi(out_low, in_low, abs_imm - 1);
3897 } else if (is_r2_or_newer) {
3898 __ Ext(out_low, in_low, 0, ctz_imm);
3899 } else {
3900 __ Sll(out_low, in_low, 32 - ctz_imm);
3901 __ Srl(out_low, out_low, 32 - ctz_imm);
3902 }
3903 if (is_r6) {
3904 __ Selnez(out_high, AT, out_low);
3905 } else {
3906 __ Movz(AT, ZERO, out_low);
3907 __ Move(out_high, AT);
3908 }
3909 if (is_r2_or_newer) {
3910 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
3911 } else {
3912 __ Sll(AT, out_high, ctz_imm);
3913 __ Or(out_low, out_low, AT);
3914 }
3915 } else if (ctz_imm == 32) {
3916 __ Sra(AT, in_high, 31);
3917 __ Move(out_low, in_low);
3918 if (is_r6) {
3919 __ Selnez(out_high, AT, out_low);
3920 } else {
3921 __ Movz(AT, ZERO, out_low);
3922 __ Move(out_high, AT);
3923 }
3924 } else if (ctz_imm < 63) {
3925 __ Sra(AT, in_high, 31);
3926 __ Move(TMP, in_low);
3927 if (ctz_imm - 32 <= 16) {
3928 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
3929 } else if (is_r2_or_newer) {
3930 __ Ext(out_high, in_high, 0, ctz_imm - 32);
3931 } else {
3932 __ Sll(out_high, in_high, 64 - ctz_imm);
3933 __ Srl(out_high, out_high, 64 - ctz_imm);
3934 }
3935 __ Move(out_low, TMP);
3936 __ Or(TMP, TMP, out_high);
3937 if (is_r6) {
3938 __ Selnez(AT, AT, TMP);
3939 } else {
3940 __ Movz(AT, ZERO, TMP);
3941 }
3942 if (is_r2_or_newer) {
3943 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
3944 } else {
3945 __ Sll(AT, AT, ctz_imm - 32);
3946 __ Or(out_high, out_high, AT);
3947 }
3948 } else {
3949 if (is_r6) {
3950 __ Aui(AT, in_high, 0x8000);
3951 } else {
3952 __ Lui(AT, 0x8000);
3953 __ Xor(AT, AT, in_high);
3954 }
3955 __ Or(AT, AT, in_low);
3956 __ Sltiu(AT, AT, 1);
3957 __ Sll(AT, AT, 31);
3958 __ Move(out_low, in_low);
3959 __ Xor(out_high, in_high, AT);
3960 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003961 }
3962 }
3963}
3964
3965void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3966 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003967 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003968
3969 LocationSummary* locations = instruction->GetLocations();
3970 Location second = locations->InAt(1);
3971 DCHECK(second.IsConstant());
3972
3973 Register out = locations->Out().AsRegister<Register>();
3974 Register dividend = locations->InAt(0).AsRegister<Register>();
3975 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3976
3977 int64_t magic;
3978 int shift;
3979 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3980
3981 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3982
3983 __ LoadConst32(TMP, magic);
3984 if (isR6) {
3985 __ MuhR6(TMP, dividend, TMP);
3986 } else {
3987 __ MultR2(dividend, TMP);
3988 __ Mfhi(TMP);
3989 }
3990 if (imm > 0 && magic < 0) {
3991 __ Addu(TMP, TMP, dividend);
3992 } else if (imm < 0 && magic > 0) {
3993 __ Subu(TMP, TMP, dividend);
3994 }
3995
3996 if (shift != 0) {
3997 __ Sra(TMP, TMP, shift);
3998 }
3999
4000 if (instruction->IsDiv()) {
4001 __ Sra(out, TMP, 31);
4002 __ Subu(out, TMP, out);
4003 } else {
4004 __ Sra(AT, TMP, 31);
4005 __ Subu(AT, TMP, AT);
4006 __ LoadConst32(TMP, imm);
4007 if (isR6) {
4008 __ MulR6(TMP, AT, TMP);
4009 } else {
4010 __ MulR2(TMP, AT, TMP);
4011 }
4012 __ Subu(out, dividend, TMP);
4013 }
4014}
4015
4016void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4017 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004018 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004019
4020 LocationSummary* locations = instruction->GetLocations();
4021 Register out = locations->Out().AsRegister<Register>();
4022 Location second = locations->InAt(1);
4023
4024 if (second.IsConstant()) {
4025 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4026 if (imm == 0) {
4027 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4028 } else if (imm == 1 || imm == -1) {
4029 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004030 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004031 DivRemByPowerOfTwo(instruction);
4032 } else {
4033 DCHECK(imm <= -2 || imm >= 2);
4034 GenerateDivRemWithAnyConstant(instruction);
4035 }
4036 } else {
4037 Register dividend = locations->InAt(0).AsRegister<Register>();
4038 Register divisor = second.AsRegister<Register>();
4039 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4040 if (instruction->IsDiv()) {
4041 if (isR6) {
4042 __ DivR6(out, dividend, divisor);
4043 } else {
4044 __ DivR2(out, dividend, divisor);
4045 }
4046 } else {
4047 if (isR6) {
4048 __ ModR6(out, dividend, divisor);
4049 } else {
4050 __ ModR2(out, dividend, divisor);
4051 }
4052 }
4053 }
4054}
4055
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004056void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004057 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004058 bool call_long_div = false;
4059 if (type == DataType::Type::kInt64) {
4060 if (div->InputAt(1)->IsConstant()) {
4061 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4062 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4063 } else {
4064 call_long_div = true;
4065 }
4066 }
4067 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004068 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004069 : LocationSummary::kNoCall;
4070
Vladimir Markoca6fff82017-10-03 14:49:14 +01004071 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004072
4073 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004074 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004075 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004076 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004077 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4078 break;
4079
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004080 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004081 if (call_long_div) {
4082 InvokeRuntimeCallingConvention calling_convention;
4083 locations->SetInAt(0, Location::RegisterPairLocation(
4084 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4085 locations->SetInAt(1, Location::RegisterPairLocation(
4086 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4087 locations->SetOut(calling_convention.GetReturnLocation(type));
4088 } else {
4089 locations->SetInAt(0, Location::RequiresRegister());
4090 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4091 locations->SetOut(Location::RequiresRegister());
4092 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004093 break;
4094 }
4095
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004096 case DataType::Type::kFloat32:
4097 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004098 locations->SetInAt(0, Location::RequiresFpuRegister());
4099 locations->SetInAt(1, Location::RequiresFpuRegister());
4100 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4101 break;
4102
4103 default:
4104 LOG(FATAL) << "Unexpected div type " << type;
4105 }
4106}
4107
4108void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004109 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004110 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004111
4112 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004113 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004114 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004115 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004116 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004117 if (locations->InAt(1).IsConstant()) {
4118 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4119 if (imm == 0) {
4120 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4121 } else if (imm == 1 || imm == -1) {
4122 DivRemOneOrMinusOne(instruction);
4123 } else {
4124 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4125 DivRemByPowerOfTwo(instruction);
4126 }
4127 } else {
4128 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4129 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4130 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004131 break;
4132 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004133 case DataType::Type::kFloat32:
4134 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004135 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4136 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4137 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004138 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004139 __ DivS(dst, lhs, rhs);
4140 } else {
4141 __ DivD(dst, lhs, rhs);
4142 }
4143 break;
4144 }
4145 default:
4146 LOG(FATAL) << "Unexpected div type " << type;
4147 }
4148}
4149
4150void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004151 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004152 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004153}
4154
4155void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004156 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004157 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004158 codegen_->AddSlowPath(slow_path);
4159 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004160 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004161
4162 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004163 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004164 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004165 case DataType::Type::kInt8:
4166 case DataType::Type::kUint16:
4167 case DataType::Type::kInt16:
4168 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004169 if (value.IsConstant()) {
4170 if (value.GetConstant()->AsIntConstant()->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.IsRegister()) << value;
4178 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4179 }
4180 break;
4181 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004182 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004183 if (value.IsConstant()) {
4184 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4185 __ B(slow_path->GetEntryLabel());
4186 } else {
4187 // A division by a non-null constant is valid. We don't need to perform
4188 // any check, so simply fall through.
4189 }
4190 } else {
4191 DCHECK(value.IsRegisterPair()) << value;
4192 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4193 __ Beqz(TMP, slow_path->GetEntryLabel());
4194 }
4195 break;
4196 }
4197 default:
4198 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4199 }
4200}
4201
4202void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4203 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004204 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004205 locations->SetOut(Location::ConstantLocation(constant));
4206}
4207
4208void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4209 // Will be generated at use site.
4210}
4211
4212void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4213 exit->SetLocations(nullptr);
4214}
4215
4216void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4217}
4218
4219void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4220 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004221 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004222 locations->SetOut(Location::ConstantLocation(constant));
4223}
4224
4225void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4226 // Will be generated at use site.
4227}
4228
4229void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4230 got->SetLocations(nullptr);
4231}
4232
4233void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004234 if (successor->IsExitBlock()) {
4235 DCHECK(got->GetPrevious()->AlwaysThrows());
4236 return; // no code needed
4237 }
4238
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004239 HBasicBlock* block = got->GetBlock();
4240 HInstruction* previous = got->GetPrevious();
4241 HLoopInformation* info = block->GetLoopInformation();
4242
4243 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004244 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4245 __ Lw(AT, SP, kCurrentMethodStackOffset);
4246 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4247 __ Addiu(TMP, TMP, 1);
4248 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4249 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004250 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4251 return;
4252 }
4253 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4254 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4255 }
4256 if (!codegen_->GoesToNextBlock(block, successor)) {
4257 __ B(codegen_->GetLabelOf(successor));
4258 }
4259}
4260
4261void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4262 HandleGoto(got, got->GetSuccessor());
4263}
4264
4265void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4266 try_boundary->SetLocations(nullptr);
4267}
4268
4269void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4270 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4271 if (!successor->IsExitBlock()) {
4272 HandleGoto(try_boundary, successor);
4273 }
4274}
4275
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004276void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4277 LocationSummary* locations) {
4278 Register dst = locations->Out().AsRegister<Register>();
4279 Register lhs = locations->InAt(0).AsRegister<Register>();
4280 Location rhs_location = locations->InAt(1);
4281 Register rhs_reg = ZERO;
4282 int64_t rhs_imm = 0;
4283 bool use_imm = rhs_location.IsConstant();
4284 if (use_imm) {
4285 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4286 } else {
4287 rhs_reg = rhs_location.AsRegister<Register>();
4288 }
4289
4290 switch (cond) {
4291 case kCondEQ:
4292 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004293 if (use_imm && IsInt<16>(-rhs_imm)) {
4294 if (rhs_imm == 0) {
4295 if (cond == kCondEQ) {
4296 __ Sltiu(dst, lhs, 1);
4297 } else {
4298 __ Sltu(dst, ZERO, lhs);
4299 }
4300 } else {
4301 __ Addiu(dst, lhs, -rhs_imm);
4302 if (cond == kCondEQ) {
4303 __ Sltiu(dst, dst, 1);
4304 } else {
4305 __ Sltu(dst, ZERO, dst);
4306 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004307 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004308 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004309 if (use_imm && IsUint<16>(rhs_imm)) {
4310 __ Xori(dst, lhs, rhs_imm);
4311 } else {
4312 if (use_imm) {
4313 rhs_reg = TMP;
4314 __ LoadConst32(rhs_reg, rhs_imm);
4315 }
4316 __ Xor(dst, lhs, rhs_reg);
4317 }
4318 if (cond == kCondEQ) {
4319 __ Sltiu(dst, dst, 1);
4320 } else {
4321 __ Sltu(dst, ZERO, dst);
4322 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004323 }
4324 break;
4325
4326 case kCondLT:
4327 case kCondGE:
4328 if (use_imm && IsInt<16>(rhs_imm)) {
4329 __ Slti(dst, lhs, rhs_imm);
4330 } else {
4331 if (use_imm) {
4332 rhs_reg = TMP;
4333 __ LoadConst32(rhs_reg, rhs_imm);
4334 }
4335 __ Slt(dst, lhs, rhs_reg);
4336 }
4337 if (cond == kCondGE) {
4338 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4339 // only the slt instruction but no sge.
4340 __ Xori(dst, dst, 1);
4341 }
4342 break;
4343
4344 case kCondLE:
4345 case kCondGT:
4346 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4347 // Simulate lhs <= rhs via lhs < rhs + 1.
4348 __ Slti(dst, lhs, rhs_imm + 1);
4349 if (cond == kCondGT) {
4350 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4351 // only the slti instruction but no sgti.
4352 __ Xori(dst, dst, 1);
4353 }
4354 } else {
4355 if (use_imm) {
4356 rhs_reg = TMP;
4357 __ LoadConst32(rhs_reg, rhs_imm);
4358 }
4359 __ Slt(dst, rhs_reg, lhs);
4360 if (cond == kCondLE) {
4361 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4362 // only the slt instruction but no sle.
4363 __ Xori(dst, dst, 1);
4364 }
4365 }
4366 break;
4367
4368 case kCondB:
4369 case kCondAE:
4370 if (use_imm && IsInt<16>(rhs_imm)) {
4371 // Sltiu sign-extends its 16-bit immediate operand before
4372 // the comparison and thus lets us compare directly with
4373 // unsigned values in the ranges [0, 0x7fff] and
4374 // [0xffff8000, 0xffffffff].
4375 __ Sltiu(dst, lhs, rhs_imm);
4376 } else {
4377 if (use_imm) {
4378 rhs_reg = TMP;
4379 __ LoadConst32(rhs_reg, rhs_imm);
4380 }
4381 __ Sltu(dst, lhs, rhs_reg);
4382 }
4383 if (cond == kCondAE) {
4384 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4385 // only the sltu instruction but no sgeu.
4386 __ Xori(dst, dst, 1);
4387 }
4388 break;
4389
4390 case kCondBE:
4391 case kCondA:
4392 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4393 // Simulate lhs <= rhs via lhs < rhs + 1.
4394 // Note that this only works if rhs + 1 does not overflow
4395 // to 0, hence the check above.
4396 // Sltiu sign-extends its 16-bit immediate operand before
4397 // the comparison and thus lets us compare directly with
4398 // unsigned values in the ranges [0, 0x7fff] and
4399 // [0xffff8000, 0xffffffff].
4400 __ Sltiu(dst, lhs, rhs_imm + 1);
4401 if (cond == kCondA) {
4402 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4403 // only the sltiu instruction but no sgtiu.
4404 __ Xori(dst, dst, 1);
4405 }
4406 } else {
4407 if (use_imm) {
4408 rhs_reg = TMP;
4409 __ LoadConst32(rhs_reg, rhs_imm);
4410 }
4411 __ Sltu(dst, rhs_reg, lhs);
4412 if (cond == kCondBE) {
4413 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4414 // only the sltu instruction but no sleu.
4415 __ Xori(dst, dst, 1);
4416 }
4417 }
4418 break;
4419 }
4420}
4421
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004422bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4423 LocationSummary* input_locations,
4424 Register dst) {
4425 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4426 Location rhs_location = input_locations->InAt(1);
4427 Register rhs_reg = ZERO;
4428 int64_t rhs_imm = 0;
4429 bool use_imm = rhs_location.IsConstant();
4430 if (use_imm) {
4431 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4432 } else {
4433 rhs_reg = rhs_location.AsRegister<Register>();
4434 }
4435
4436 switch (cond) {
4437 case kCondEQ:
4438 case kCondNE:
4439 if (use_imm && IsInt<16>(-rhs_imm)) {
4440 __ Addiu(dst, lhs, -rhs_imm);
4441 } else if (use_imm && IsUint<16>(rhs_imm)) {
4442 __ Xori(dst, lhs, rhs_imm);
4443 } else {
4444 if (use_imm) {
4445 rhs_reg = TMP;
4446 __ LoadConst32(rhs_reg, rhs_imm);
4447 }
4448 __ Xor(dst, lhs, rhs_reg);
4449 }
4450 return (cond == kCondEQ);
4451
4452 case kCondLT:
4453 case kCondGE:
4454 if (use_imm && IsInt<16>(rhs_imm)) {
4455 __ Slti(dst, lhs, rhs_imm);
4456 } else {
4457 if (use_imm) {
4458 rhs_reg = TMP;
4459 __ LoadConst32(rhs_reg, rhs_imm);
4460 }
4461 __ Slt(dst, lhs, rhs_reg);
4462 }
4463 return (cond == kCondGE);
4464
4465 case kCondLE:
4466 case kCondGT:
4467 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4468 // Simulate lhs <= rhs via lhs < rhs + 1.
4469 __ Slti(dst, lhs, rhs_imm + 1);
4470 return (cond == kCondGT);
4471 } else {
4472 if (use_imm) {
4473 rhs_reg = TMP;
4474 __ LoadConst32(rhs_reg, rhs_imm);
4475 }
4476 __ Slt(dst, rhs_reg, lhs);
4477 return (cond == kCondLE);
4478 }
4479
4480 case kCondB:
4481 case kCondAE:
4482 if (use_imm && IsInt<16>(rhs_imm)) {
4483 // Sltiu sign-extends its 16-bit immediate operand before
4484 // the comparison and thus lets us compare directly with
4485 // unsigned values in the ranges [0, 0x7fff] and
4486 // [0xffff8000, 0xffffffff].
4487 __ Sltiu(dst, lhs, rhs_imm);
4488 } else {
4489 if (use_imm) {
4490 rhs_reg = TMP;
4491 __ LoadConst32(rhs_reg, rhs_imm);
4492 }
4493 __ Sltu(dst, lhs, rhs_reg);
4494 }
4495 return (cond == kCondAE);
4496
4497 case kCondBE:
4498 case kCondA:
4499 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4500 // Simulate lhs <= rhs via lhs < rhs + 1.
4501 // Note that this only works if rhs + 1 does not overflow
4502 // to 0, hence the check above.
4503 // Sltiu sign-extends its 16-bit immediate operand before
4504 // the comparison and thus lets us compare directly with
4505 // unsigned values in the ranges [0, 0x7fff] and
4506 // [0xffff8000, 0xffffffff].
4507 __ Sltiu(dst, lhs, rhs_imm + 1);
4508 return (cond == kCondA);
4509 } else {
4510 if (use_imm) {
4511 rhs_reg = TMP;
4512 __ LoadConst32(rhs_reg, rhs_imm);
4513 }
4514 __ Sltu(dst, rhs_reg, lhs);
4515 return (cond == kCondBE);
4516 }
4517 }
4518}
4519
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004520void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4521 LocationSummary* locations,
4522 MipsLabel* label) {
4523 Register lhs = locations->InAt(0).AsRegister<Register>();
4524 Location rhs_location = locations->InAt(1);
4525 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004526 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004527 bool use_imm = rhs_location.IsConstant();
4528 if (use_imm) {
4529 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4530 } else {
4531 rhs_reg = rhs_location.AsRegister<Register>();
4532 }
4533
4534 if (use_imm && rhs_imm == 0) {
4535 switch (cond) {
4536 case kCondEQ:
4537 case kCondBE: // <= 0 if zero
4538 __ Beqz(lhs, label);
4539 break;
4540 case kCondNE:
4541 case kCondA: // > 0 if non-zero
4542 __ Bnez(lhs, label);
4543 break;
4544 case kCondLT:
4545 __ Bltz(lhs, label);
4546 break;
4547 case kCondGE:
4548 __ Bgez(lhs, label);
4549 break;
4550 case kCondLE:
4551 __ Blez(lhs, label);
4552 break;
4553 case kCondGT:
4554 __ Bgtz(lhs, label);
4555 break;
4556 case kCondB: // always false
4557 break;
4558 case kCondAE: // always true
4559 __ B(label);
4560 break;
4561 }
4562 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004563 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4564 if (isR6 || !use_imm) {
4565 if (use_imm) {
4566 rhs_reg = TMP;
4567 __ LoadConst32(rhs_reg, rhs_imm);
4568 }
4569 switch (cond) {
4570 case kCondEQ:
4571 __ Beq(lhs, rhs_reg, label);
4572 break;
4573 case kCondNE:
4574 __ Bne(lhs, rhs_reg, label);
4575 break;
4576 case kCondLT:
4577 __ Blt(lhs, rhs_reg, label);
4578 break;
4579 case kCondGE:
4580 __ Bge(lhs, rhs_reg, label);
4581 break;
4582 case kCondLE:
4583 __ Bge(rhs_reg, lhs, label);
4584 break;
4585 case kCondGT:
4586 __ Blt(rhs_reg, lhs, label);
4587 break;
4588 case kCondB:
4589 __ Bltu(lhs, rhs_reg, label);
4590 break;
4591 case kCondAE:
4592 __ Bgeu(lhs, rhs_reg, label);
4593 break;
4594 case kCondBE:
4595 __ Bgeu(rhs_reg, lhs, label);
4596 break;
4597 case kCondA:
4598 __ Bltu(rhs_reg, lhs, label);
4599 break;
4600 }
4601 } else {
4602 // Special cases for more efficient comparison with constants on R2.
4603 switch (cond) {
4604 case kCondEQ:
4605 __ LoadConst32(TMP, rhs_imm);
4606 __ Beq(lhs, TMP, label);
4607 break;
4608 case kCondNE:
4609 __ LoadConst32(TMP, rhs_imm);
4610 __ Bne(lhs, TMP, label);
4611 break;
4612 case kCondLT:
4613 if (IsInt<16>(rhs_imm)) {
4614 __ Slti(TMP, lhs, rhs_imm);
4615 __ Bnez(TMP, label);
4616 } else {
4617 __ LoadConst32(TMP, rhs_imm);
4618 __ Blt(lhs, TMP, label);
4619 }
4620 break;
4621 case kCondGE:
4622 if (IsInt<16>(rhs_imm)) {
4623 __ Slti(TMP, lhs, rhs_imm);
4624 __ Beqz(TMP, label);
4625 } else {
4626 __ LoadConst32(TMP, rhs_imm);
4627 __ Bge(lhs, TMP, label);
4628 }
4629 break;
4630 case kCondLE:
4631 if (IsInt<16>(rhs_imm + 1)) {
4632 // Simulate lhs <= rhs via lhs < rhs + 1.
4633 __ Slti(TMP, lhs, rhs_imm + 1);
4634 __ Bnez(TMP, label);
4635 } else {
4636 __ LoadConst32(TMP, rhs_imm);
4637 __ Bge(TMP, lhs, label);
4638 }
4639 break;
4640 case kCondGT:
4641 if (IsInt<16>(rhs_imm + 1)) {
4642 // Simulate lhs > rhs via !(lhs < rhs + 1).
4643 __ Slti(TMP, lhs, rhs_imm + 1);
4644 __ Beqz(TMP, label);
4645 } else {
4646 __ LoadConst32(TMP, rhs_imm);
4647 __ Blt(TMP, lhs, label);
4648 }
4649 break;
4650 case kCondB:
4651 if (IsInt<16>(rhs_imm)) {
4652 __ Sltiu(TMP, lhs, rhs_imm);
4653 __ Bnez(TMP, label);
4654 } else {
4655 __ LoadConst32(TMP, rhs_imm);
4656 __ Bltu(lhs, TMP, label);
4657 }
4658 break;
4659 case kCondAE:
4660 if (IsInt<16>(rhs_imm)) {
4661 __ Sltiu(TMP, lhs, rhs_imm);
4662 __ Beqz(TMP, label);
4663 } else {
4664 __ LoadConst32(TMP, rhs_imm);
4665 __ Bgeu(lhs, TMP, label);
4666 }
4667 break;
4668 case kCondBE:
4669 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4670 // Simulate lhs <= rhs via lhs < rhs + 1.
4671 // Note that this only works if rhs + 1 does not overflow
4672 // to 0, hence the check above.
4673 __ Sltiu(TMP, lhs, rhs_imm + 1);
4674 __ Bnez(TMP, label);
4675 } else {
4676 __ LoadConst32(TMP, rhs_imm);
4677 __ Bgeu(TMP, lhs, label);
4678 }
4679 break;
4680 case kCondA:
4681 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4682 // Simulate lhs > rhs via !(lhs < rhs + 1).
4683 // Note that this only works if rhs + 1 does not overflow
4684 // to 0, hence the check above.
4685 __ Sltiu(TMP, lhs, rhs_imm + 1);
4686 __ Beqz(TMP, label);
4687 } else {
4688 __ LoadConst32(TMP, rhs_imm);
4689 __ Bltu(TMP, lhs, label);
4690 }
4691 break;
4692 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004693 }
4694 }
4695}
4696
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004697void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4698 LocationSummary* locations) {
4699 Register dst = locations->Out().AsRegister<Register>();
4700 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4701 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4702 Location rhs_location = locations->InAt(1);
4703 Register rhs_high = ZERO;
4704 Register rhs_low = ZERO;
4705 int64_t imm = 0;
4706 uint32_t imm_high = 0;
4707 uint32_t imm_low = 0;
4708 bool use_imm = rhs_location.IsConstant();
4709 if (use_imm) {
4710 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4711 imm_high = High32Bits(imm);
4712 imm_low = Low32Bits(imm);
4713 } else {
4714 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4715 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4716 }
4717 if (use_imm && imm == 0) {
4718 switch (cond) {
4719 case kCondEQ:
4720 case kCondBE: // <= 0 if zero
4721 __ Or(dst, lhs_high, lhs_low);
4722 __ Sltiu(dst, dst, 1);
4723 break;
4724 case kCondNE:
4725 case kCondA: // > 0 if non-zero
4726 __ Or(dst, lhs_high, lhs_low);
4727 __ Sltu(dst, ZERO, dst);
4728 break;
4729 case kCondLT:
4730 __ Slt(dst, lhs_high, ZERO);
4731 break;
4732 case kCondGE:
4733 __ Slt(dst, lhs_high, ZERO);
4734 __ Xori(dst, dst, 1);
4735 break;
4736 case kCondLE:
4737 __ Or(TMP, lhs_high, lhs_low);
4738 __ Sra(AT, lhs_high, 31);
4739 __ Sltu(dst, AT, TMP);
4740 __ Xori(dst, dst, 1);
4741 break;
4742 case kCondGT:
4743 __ Or(TMP, lhs_high, lhs_low);
4744 __ Sra(AT, lhs_high, 31);
4745 __ Sltu(dst, AT, TMP);
4746 break;
4747 case kCondB: // always false
4748 __ Andi(dst, dst, 0);
4749 break;
4750 case kCondAE: // always true
4751 __ Ori(dst, ZERO, 1);
4752 break;
4753 }
4754 } else if (use_imm) {
4755 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4756 switch (cond) {
4757 case kCondEQ:
4758 __ LoadConst32(TMP, imm_high);
4759 __ Xor(TMP, TMP, lhs_high);
4760 __ LoadConst32(AT, imm_low);
4761 __ Xor(AT, AT, lhs_low);
4762 __ Or(dst, TMP, AT);
4763 __ Sltiu(dst, dst, 1);
4764 break;
4765 case kCondNE:
4766 __ LoadConst32(TMP, imm_high);
4767 __ Xor(TMP, TMP, lhs_high);
4768 __ LoadConst32(AT, imm_low);
4769 __ Xor(AT, AT, lhs_low);
4770 __ Or(dst, TMP, AT);
4771 __ Sltu(dst, ZERO, dst);
4772 break;
4773 case kCondLT:
4774 case kCondGE:
4775 if (dst == lhs_low) {
4776 __ LoadConst32(TMP, imm_low);
4777 __ Sltu(dst, lhs_low, TMP);
4778 }
4779 __ LoadConst32(TMP, imm_high);
4780 __ Slt(AT, lhs_high, TMP);
4781 __ Slt(TMP, TMP, lhs_high);
4782 if (dst != lhs_low) {
4783 __ LoadConst32(dst, imm_low);
4784 __ Sltu(dst, lhs_low, dst);
4785 }
4786 __ Slt(dst, TMP, dst);
4787 __ Or(dst, dst, AT);
4788 if (cond == kCondGE) {
4789 __ Xori(dst, dst, 1);
4790 }
4791 break;
4792 case kCondGT:
4793 case kCondLE:
4794 if (dst == lhs_low) {
4795 __ LoadConst32(TMP, imm_low);
4796 __ Sltu(dst, TMP, lhs_low);
4797 }
4798 __ LoadConst32(TMP, imm_high);
4799 __ Slt(AT, TMP, lhs_high);
4800 __ Slt(TMP, lhs_high, TMP);
4801 if (dst != lhs_low) {
4802 __ LoadConst32(dst, imm_low);
4803 __ Sltu(dst, dst, lhs_low);
4804 }
4805 __ Slt(dst, TMP, dst);
4806 __ Or(dst, dst, AT);
4807 if (cond == kCondLE) {
4808 __ Xori(dst, dst, 1);
4809 }
4810 break;
4811 case kCondB:
4812 case kCondAE:
4813 if (dst == lhs_low) {
4814 __ LoadConst32(TMP, imm_low);
4815 __ Sltu(dst, lhs_low, TMP);
4816 }
4817 __ LoadConst32(TMP, imm_high);
4818 __ Sltu(AT, lhs_high, TMP);
4819 __ Sltu(TMP, TMP, lhs_high);
4820 if (dst != lhs_low) {
4821 __ LoadConst32(dst, imm_low);
4822 __ Sltu(dst, lhs_low, dst);
4823 }
4824 __ Slt(dst, TMP, dst);
4825 __ Or(dst, dst, AT);
4826 if (cond == kCondAE) {
4827 __ Xori(dst, dst, 1);
4828 }
4829 break;
4830 case kCondA:
4831 case kCondBE:
4832 if (dst == lhs_low) {
4833 __ LoadConst32(TMP, imm_low);
4834 __ Sltu(dst, TMP, lhs_low);
4835 }
4836 __ LoadConst32(TMP, imm_high);
4837 __ Sltu(AT, TMP, lhs_high);
4838 __ Sltu(TMP, lhs_high, TMP);
4839 if (dst != lhs_low) {
4840 __ LoadConst32(dst, imm_low);
4841 __ Sltu(dst, dst, lhs_low);
4842 }
4843 __ Slt(dst, TMP, dst);
4844 __ Or(dst, dst, AT);
4845 if (cond == kCondBE) {
4846 __ Xori(dst, dst, 1);
4847 }
4848 break;
4849 }
4850 } else {
4851 switch (cond) {
4852 case kCondEQ:
4853 __ Xor(TMP, lhs_high, rhs_high);
4854 __ Xor(AT, lhs_low, rhs_low);
4855 __ Or(dst, TMP, AT);
4856 __ Sltiu(dst, dst, 1);
4857 break;
4858 case kCondNE:
4859 __ Xor(TMP, lhs_high, rhs_high);
4860 __ Xor(AT, lhs_low, rhs_low);
4861 __ Or(dst, TMP, AT);
4862 __ Sltu(dst, ZERO, dst);
4863 break;
4864 case kCondLT:
4865 case kCondGE:
4866 __ Slt(TMP, rhs_high, lhs_high);
4867 __ Sltu(AT, lhs_low, rhs_low);
4868 __ Slt(TMP, TMP, AT);
4869 __ Slt(AT, lhs_high, rhs_high);
4870 __ Or(dst, AT, TMP);
4871 if (cond == kCondGE) {
4872 __ Xori(dst, dst, 1);
4873 }
4874 break;
4875 case kCondGT:
4876 case kCondLE:
4877 __ Slt(TMP, lhs_high, rhs_high);
4878 __ Sltu(AT, rhs_low, lhs_low);
4879 __ Slt(TMP, TMP, AT);
4880 __ Slt(AT, rhs_high, lhs_high);
4881 __ Or(dst, AT, TMP);
4882 if (cond == kCondLE) {
4883 __ Xori(dst, dst, 1);
4884 }
4885 break;
4886 case kCondB:
4887 case kCondAE:
4888 __ Sltu(TMP, rhs_high, lhs_high);
4889 __ Sltu(AT, lhs_low, rhs_low);
4890 __ Slt(TMP, TMP, AT);
4891 __ Sltu(AT, lhs_high, rhs_high);
4892 __ Or(dst, AT, TMP);
4893 if (cond == kCondAE) {
4894 __ Xori(dst, dst, 1);
4895 }
4896 break;
4897 case kCondA:
4898 case kCondBE:
4899 __ Sltu(TMP, lhs_high, rhs_high);
4900 __ Sltu(AT, rhs_low, lhs_low);
4901 __ Slt(TMP, TMP, AT);
4902 __ Sltu(AT, rhs_high, lhs_high);
4903 __ Or(dst, AT, TMP);
4904 if (cond == kCondBE) {
4905 __ Xori(dst, dst, 1);
4906 }
4907 break;
4908 }
4909 }
4910}
4911
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004912void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4913 LocationSummary* locations,
4914 MipsLabel* label) {
4915 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4916 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4917 Location rhs_location = locations->InAt(1);
4918 Register rhs_high = ZERO;
4919 Register rhs_low = ZERO;
4920 int64_t imm = 0;
4921 uint32_t imm_high = 0;
4922 uint32_t imm_low = 0;
4923 bool use_imm = rhs_location.IsConstant();
4924 if (use_imm) {
4925 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4926 imm_high = High32Bits(imm);
4927 imm_low = Low32Bits(imm);
4928 } else {
4929 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4930 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4931 }
4932
4933 if (use_imm && imm == 0) {
4934 switch (cond) {
4935 case kCondEQ:
4936 case kCondBE: // <= 0 if zero
4937 __ Or(TMP, lhs_high, lhs_low);
4938 __ Beqz(TMP, label);
4939 break;
4940 case kCondNE:
4941 case kCondA: // > 0 if non-zero
4942 __ Or(TMP, lhs_high, lhs_low);
4943 __ Bnez(TMP, label);
4944 break;
4945 case kCondLT:
4946 __ Bltz(lhs_high, label);
4947 break;
4948 case kCondGE:
4949 __ Bgez(lhs_high, label);
4950 break;
4951 case kCondLE:
4952 __ Or(TMP, lhs_high, lhs_low);
4953 __ Sra(AT, lhs_high, 31);
4954 __ Bgeu(AT, TMP, label);
4955 break;
4956 case kCondGT:
4957 __ Or(TMP, lhs_high, lhs_low);
4958 __ Sra(AT, lhs_high, 31);
4959 __ Bltu(AT, TMP, label);
4960 break;
4961 case kCondB: // always false
4962 break;
4963 case kCondAE: // always true
4964 __ B(label);
4965 break;
4966 }
4967 } else if (use_imm) {
4968 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4969 switch (cond) {
4970 case kCondEQ:
4971 __ LoadConst32(TMP, imm_high);
4972 __ Xor(TMP, TMP, lhs_high);
4973 __ LoadConst32(AT, imm_low);
4974 __ Xor(AT, AT, lhs_low);
4975 __ Or(TMP, TMP, AT);
4976 __ Beqz(TMP, label);
4977 break;
4978 case kCondNE:
4979 __ LoadConst32(TMP, imm_high);
4980 __ Xor(TMP, TMP, lhs_high);
4981 __ LoadConst32(AT, imm_low);
4982 __ Xor(AT, AT, lhs_low);
4983 __ Or(TMP, TMP, AT);
4984 __ Bnez(TMP, label);
4985 break;
4986 case kCondLT:
4987 __ LoadConst32(TMP, imm_high);
4988 __ Blt(lhs_high, TMP, label);
4989 __ Slt(TMP, TMP, lhs_high);
4990 __ LoadConst32(AT, imm_low);
4991 __ Sltu(AT, lhs_low, AT);
4992 __ Blt(TMP, AT, label);
4993 break;
4994 case kCondGE:
4995 __ LoadConst32(TMP, imm_high);
4996 __ Blt(TMP, lhs_high, label);
4997 __ Slt(TMP, lhs_high, TMP);
4998 __ LoadConst32(AT, imm_low);
4999 __ Sltu(AT, lhs_low, AT);
5000 __ Or(TMP, TMP, AT);
5001 __ Beqz(TMP, label);
5002 break;
5003 case kCondLE:
5004 __ LoadConst32(TMP, imm_high);
5005 __ Blt(lhs_high, TMP, label);
5006 __ Slt(TMP, TMP, lhs_high);
5007 __ LoadConst32(AT, imm_low);
5008 __ Sltu(AT, AT, lhs_low);
5009 __ Or(TMP, TMP, AT);
5010 __ Beqz(TMP, label);
5011 break;
5012 case kCondGT:
5013 __ LoadConst32(TMP, imm_high);
5014 __ Blt(TMP, lhs_high, label);
5015 __ Slt(TMP, lhs_high, TMP);
5016 __ LoadConst32(AT, imm_low);
5017 __ Sltu(AT, AT, lhs_low);
5018 __ Blt(TMP, AT, label);
5019 break;
5020 case kCondB:
5021 __ LoadConst32(TMP, imm_high);
5022 __ Bltu(lhs_high, TMP, label);
5023 __ Sltu(TMP, TMP, lhs_high);
5024 __ LoadConst32(AT, imm_low);
5025 __ Sltu(AT, lhs_low, AT);
5026 __ Blt(TMP, AT, label);
5027 break;
5028 case kCondAE:
5029 __ LoadConst32(TMP, imm_high);
5030 __ Bltu(TMP, lhs_high, label);
5031 __ Sltu(TMP, lhs_high, TMP);
5032 __ LoadConst32(AT, imm_low);
5033 __ Sltu(AT, lhs_low, AT);
5034 __ Or(TMP, TMP, AT);
5035 __ Beqz(TMP, label);
5036 break;
5037 case kCondBE:
5038 __ LoadConst32(TMP, imm_high);
5039 __ Bltu(lhs_high, TMP, label);
5040 __ Sltu(TMP, TMP, lhs_high);
5041 __ LoadConst32(AT, imm_low);
5042 __ Sltu(AT, AT, lhs_low);
5043 __ Or(TMP, TMP, AT);
5044 __ Beqz(TMP, label);
5045 break;
5046 case kCondA:
5047 __ LoadConst32(TMP, imm_high);
5048 __ Bltu(TMP, lhs_high, label);
5049 __ Sltu(TMP, lhs_high, TMP);
5050 __ LoadConst32(AT, imm_low);
5051 __ Sltu(AT, AT, lhs_low);
5052 __ Blt(TMP, AT, label);
5053 break;
5054 }
5055 } else {
5056 switch (cond) {
5057 case kCondEQ:
5058 __ Xor(TMP, lhs_high, rhs_high);
5059 __ Xor(AT, lhs_low, rhs_low);
5060 __ Or(TMP, TMP, AT);
5061 __ Beqz(TMP, label);
5062 break;
5063 case kCondNE:
5064 __ Xor(TMP, lhs_high, rhs_high);
5065 __ Xor(AT, lhs_low, rhs_low);
5066 __ Or(TMP, TMP, AT);
5067 __ Bnez(TMP, label);
5068 break;
5069 case kCondLT:
5070 __ Blt(lhs_high, rhs_high, label);
5071 __ Slt(TMP, rhs_high, lhs_high);
5072 __ Sltu(AT, lhs_low, rhs_low);
5073 __ Blt(TMP, AT, label);
5074 break;
5075 case kCondGE:
5076 __ Blt(rhs_high, lhs_high, label);
5077 __ Slt(TMP, lhs_high, rhs_high);
5078 __ Sltu(AT, lhs_low, rhs_low);
5079 __ Or(TMP, TMP, AT);
5080 __ Beqz(TMP, label);
5081 break;
5082 case kCondLE:
5083 __ Blt(lhs_high, rhs_high, label);
5084 __ Slt(TMP, rhs_high, lhs_high);
5085 __ Sltu(AT, rhs_low, lhs_low);
5086 __ Or(TMP, TMP, AT);
5087 __ Beqz(TMP, label);
5088 break;
5089 case kCondGT:
5090 __ Blt(rhs_high, lhs_high, label);
5091 __ Slt(TMP, lhs_high, rhs_high);
5092 __ Sltu(AT, rhs_low, lhs_low);
5093 __ Blt(TMP, AT, label);
5094 break;
5095 case kCondB:
5096 __ Bltu(lhs_high, rhs_high, label);
5097 __ Sltu(TMP, rhs_high, lhs_high);
5098 __ Sltu(AT, lhs_low, rhs_low);
5099 __ Blt(TMP, AT, label);
5100 break;
5101 case kCondAE:
5102 __ Bltu(rhs_high, lhs_high, label);
5103 __ Sltu(TMP, lhs_high, rhs_high);
5104 __ Sltu(AT, lhs_low, rhs_low);
5105 __ Or(TMP, TMP, AT);
5106 __ Beqz(TMP, label);
5107 break;
5108 case kCondBE:
5109 __ Bltu(lhs_high, rhs_high, label);
5110 __ Sltu(TMP, rhs_high, lhs_high);
5111 __ Sltu(AT, rhs_low, lhs_low);
5112 __ Or(TMP, TMP, AT);
5113 __ Beqz(TMP, label);
5114 break;
5115 case kCondA:
5116 __ Bltu(rhs_high, lhs_high, label);
5117 __ Sltu(TMP, lhs_high, rhs_high);
5118 __ Sltu(AT, rhs_low, lhs_low);
5119 __ Blt(TMP, AT, label);
5120 break;
5121 }
5122 }
5123}
5124
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005125void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5126 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005127 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005128 LocationSummary* locations) {
5129 Register dst = locations->Out().AsRegister<Register>();
5130 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5131 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5132 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005133 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005134 if (isR6) {
5135 switch (cond) {
5136 case kCondEQ:
5137 __ CmpEqS(FTMP, lhs, rhs);
5138 __ Mfc1(dst, FTMP);
5139 __ Andi(dst, dst, 1);
5140 break;
5141 case kCondNE:
5142 __ CmpEqS(FTMP, lhs, rhs);
5143 __ Mfc1(dst, FTMP);
5144 __ Addiu(dst, dst, 1);
5145 break;
5146 case kCondLT:
5147 if (gt_bias) {
5148 __ CmpLtS(FTMP, lhs, rhs);
5149 } else {
5150 __ CmpUltS(FTMP, lhs, rhs);
5151 }
5152 __ Mfc1(dst, FTMP);
5153 __ Andi(dst, dst, 1);
5154 break;
5155 case kCondLE:
5156 if (gt_bias) {
5157 __ CmpLeS(FTMP, lhs, rhs);
5158 } else {
5159 __ CmpUleS(FTMP, lhs, rhs);
5160 }
5161 __ Mfc1(dst, FTMP);
5162 __ Andi(dst, dst, 1);
5163 break;
5164 case kCondGT:
5165 if (gt_bias) {
5166 __ CmpUltS(FTMP, rhs, lhs);
5167 } else {
5168 __ CmpLtS(FTMP, rhs, lhs);
5169 }
5170 __ Mfc1(dst, FTMP);
5171 __ Andi(dst, dst, 1);
5172 break;
5173 case kCondGE:
5174 if (gt_bias) {
5175 __ CmpUleS(FTMP, rhs, lhs);
5176 } else {
5177 __ CmpLeS(FTMP, rhs, lhs);
5178 }
5179 __ Mfc1(dst, FTMP);
5180 __ Andi(dst, dst, 1);
5181 break;
5182 default:
5183 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5184 UNREACHABLE();
5185 }
5186 } else {
5187 switch (cond) {
5188 case kCondEQ:
5189 __ CeqS(0, lhs, rhs);
5190 __ LoadConst32(dst, 1);
5191 __ Movf(dst, ZERO, 0);
5192 break;
5193 case kCondNE:
5194 __ CeqS(0, lhs, rhs);
5195 __ LoadConst32(dst, 1);
5196 __ Movt(dst, ZERO, 0);
5197 break;
5198 case kCondLT:
5199 if (gt_bias) {
5200 __ ColtS(0, lhs, rhs);
5201 } else {
5202 __ CultS(0, lhs, rhs);
5203 }
5204 __ LoadConst32(dst, 1);
5205 __ Movf(dst, ZERO, 0);
5206 break;
5207 case kCondLE:
5208 if (gt_bias) {
5209 __ ColeS(0, lhs, rhs);
5210 } else {
5211 __ CuleS(0, lhs, rhs);
5212 }
5213 __ LoadConst32(dst, 1);
5214 __ Movf(dst, ZERO, 0);
5215 break;
5216 case kCondGT:
5217 if (gt_bias) {
5218 __ CultS(0, rhs, lhs);
5219 } else {
5220 __ ColtS(0, rhs, lhs);
5221 }
5222 __ LoadConst32(dst, 1);
5223 __ Movf(dst, ZERO, 0);
5224 break;
5225 case kCondGE:
5226 if (gt_bias) {
5227 __ CuleS(0, rhs, lhs);
5228 } else {
5229 __ ColeS(0, rhs, lhs);
5230 }
5231 __ LoadConst32(dst, 1);
5232 __ Movf(dst, ZERO, 0);
5233 break;
5234 default:
5235 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5236 UNREACHABLE();
5237 }
5238 }
5239 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005240 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005241 if (isR6) {
5242 switch (cond) {
5243 case kCondEQ:
5244 __ CmpEqD(FTMP, lhs, rhs);
5245 __ Mfc1(dst, FTMP);
5246 __ Andi(dst, dst, 1);
5247 break;
5248 case kCondNE:
5249 __ CmpEqD(FTMP, lhs, rhs);
5250 __ Mfc1(dst, FTMP);
5251 __ Addiu(dst, dst, 1);
5252 break;
5253 case kCondLT:
5254 if (gt_bias) {
5255 __ CmpLtD(FTMP, lhs, rhs);
5256 } else {
5257 __ CmpUltD(FTMP, lhs, rhs);
5258 }
5259 __ Mfc1(dst, FTMP);
5260 __ Andi(dst, dst, 1);
5261 break;
5262 case kCondLE:
5263 if (gt_bias) {
5264 __ CmpLeD(FTMP, lhs, rhs);
5265 } else {
5266 __ CmpUleD(FTMP, lhs, rhs);
5267 }
5268 __ Mfc1(dst, FTMP);
5269 __ Andi(dst, dst, 1);
5270 break;
5271 case kCondGT:
5272 if (gt_bias) {
5273 __ CmpUltD(FTMP, rhs, lhs);
5274 } else {
5275 __ CmpLtD(FTMP, rhs, lhs);
5276 }
5277 __ Mfc1(dst, FTMP);
5278 __ Andi(dst, dst, 1);
5279 break;
5280 case kCondGE:
5281 if (gt_bias) {
5282 __ CmpUleD(FTMP, rhs, lhs);
5283 } else {
5284 __ CmpLeD(FTMP, rhs, lhs);
5285 }
5286 __ Mfc1(dst, FTMP);
5287 __ Andi(dst, dst, 1);
5288 break;
5289 default:
5290 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5291 UNREACHABLE();
5292 }
5293 } else {
5294 switch (cond) {
5295 case kCondEQ:
5296 __ CeqD(0, lhs, rhs);
5297 __ LoadConst32(dst, 1);
5298 __ Movf(dst, ZERO, 0);
5299 break;
5300 case kCondNE:
5301 __ CeqD(0, lhs, rhs);
5302 __ LoadConst32(dst, 1);
5303 __ Movt(dst, ZERO, 0);
5304 break;
5305 case kCondLT:
5306 if (gt_bias) {
5307 __ ColtD(0, lhs, rhs);
5308 } else {
5309 __ CultD(0, lhs, rhs);
5310 }
5311 __ LoadConst32(dst, 1);
5312 __ Movf(dst, ZERO, 0);
5313 break;
5314 case kCondLE:
5315 if (gt_bias) {
5316 __ ColeD(0, lhs, rhs);
5317 } else {
5318 __ CuleD(0, lhs, rhs);
5319 }
5320 __ LoadConst32(dst, 1);
5321 __ Movf(dst, ZERO, 0);
5322 break;
5323 case kCondGT:
5324 if (gt_bias) {
5325 __ CultD(0, rhs, lhs);
5326 } else {
5327 __ ColtD(0, rhs, lhs);
5328 }
5329 __ LoadConst32(dst, 1);
5330 __ Movf(dst, ZERO, 0);
5331 break;
5332 case kCondGE:
5333 if (gt_bias) {
5334 __ CuleD(0, rhs, lhs);
5335 } else {
5336 __ ColeD(0, rhs, lhs);
5337 }
5338 __ LoadConst32(dst, 1);
5339 __ Movf(dst, ZERO, 0);
5340 break;
5341 default:
5342 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5343 UNREACHABLE();
5344 }
5345 }
5346 }
5347}
5348
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005349bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5350 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005351 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005352 LocationSummary* input_locations,
5353 int cc) {
5354 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5355 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5356 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005357 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005358 switch (cond) {
5359 case kCondEQ:
5360 __ CeqS(cc, lhs, rhs);
5361 return false;
5362 case kCondNE:
5363 __ CeqS(cc, lhs, rhs);
5364 return true;
5365 case kCondLT:
5366 if (gt_bias) {
5367 __ ColtS(cc, lhs, rhs);
5368 } else {
5369 __ CultS(cc, lhs, rhs);
5370 }
5371 return false;
5372 case kCondLE:
5373 if (gt_bias) {
5374 __ ColeS(cc, lhs, rhs);
5375 } else {
5376 __ CuleS(cc, lhs, rhs);
5377 }
5378 return false;
5379 case kCondGT:
5380 if (gt_bias) {
5381 __ CultS(cc, rhs, lhs);
5382 } else {
5383 __ ColtS(cc, rhs, lhs);
5384 }
5385 return false;
5386 case kCondGE:
5387 if (gt_bias) {
5388 __ CuleS(cc, rhs, lhs);
5389 } else {
5390 __ ColeS(cc, rhs, lhs);
5391 }
5392 return false;
5393 default:
5394 LOG(FATAL) << "Unexpected non-floating-point condition";
5395 UNREACHABLE();
5396 }
5397 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005398 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005399 switch (cond) {
5400 case kCondEQ:
5401 __ CeqD(cc, lhs, rhs);
5402 return false;
5403 case kCondNE:
5404 __ CeqD(cc, lhs, rhs);
5405 return true;
5406 case kCondLT:
5407 if (gt_bias) {
5408 __ ColtD(cc, lhs, rhs);
5409 } else {
5410 __ CultD(cc, lhs, rhs);
5411 }
5412 return false;
5413 case kCondLE:
5414 if (gt_bias) {
5415 __ ColeD(cc, lhs, rhs);
5416 } else {
5417 __ CuleD(cc, lhs, rhs);
5418 }
5419 return false;
5420 case kCondGT:
5421 if (gt_bias) {
5422 __ CultD(cc, rhs, lhs);
5423 } else {
5424 __ ColtD(cc, rhs, lhs);
5425 }
5426 return false;
5427 case kCondGE:
5428 if (gt_bias) {
5429 __ CuleD(cc, rhs, lhs);
5430 } else {
5431 __ ColeD(cc, rhs, lhs);
5432 }
5433 return false;
5434 default:
5435 LOG(FATAL) << "Unexpected non-floating-point condition";
5436 UNREACHABLE();
5437 }
5438 }
5439}
5440
5441bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5442 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005443 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005444 LocationSummary* input_locations,
5445 FRegister dst) {
5446 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5447 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5448 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005449 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005450 switch (cond) {
5451 case kCondEQ:
5452 __ CmpEqS(dst, lhs, rhs);
5453 return false;
5454 case kCondNE:
5455 __ CmpEqS(dst, lhs, rhs);
5456 return true;
5457 case kCondLT:
5458 if (gt_bias) {
5459 __ CmpLtS(dst, lhs, rhs);
5460 } else {
5461 __ CmpUltS(dst, lhs, rhs);
5462 }
5463 return false;
5464 case kCondLE:
5465 if (gt_bias) {
5466 __ CmpLeS(dst, lhs, rhs);
5467 } else {
5468 __ CmpUleS(dst, lhs, rhs);
5469 }
5470 return false;
5471 case kCondGT:
5472 if (gt_bias) {
5473 __ CmpUltS(dst, rhs, lhs);
5474 } else {
5475 __ CmpLtS(dst, rhs, lhs);
5476 }
5477 return false;
5478 case kCondGE:
5479 if (gt_bias) {
5480 __ CmpUleS(dst, rhs, lhs);
5481 } else {
5482 __ CmpLeS(dst, rhs, lhs);
5483 }
5484 return false;
5485 default:
5486 LOG(FATAL) << "Unexpected non-floating-point condition";
5487 UNREACHABLE();
5488 }
5489 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005490 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005491 switch (cond) {
5492 case kCondEQ:
5493 __ CmpEqD(dst, lhs, rhs);
5494 return false;
5495 case kCondNE:
5496 __ CmpEqD(dst, lhs, rhs);
5497 return true;
5498 case kCondLT:
5499 if (gt_bias) {
5500 __ CmpLtD(dst, lhs, rhs);
5501 } else {
5502 __ CmpUltD(dst, lhs, rhs);
5503 }
5504 return false;
5505 case kCondLE:
5506 if (gt_bias) {
5507 __ CmpLeD(dst, lhs, rhs);
5508 } else {
5509 __ CmpUleD(dst, lhs, rhs);
5510 }
5511 return false;
5512 case kCondGT:
5513 if (gt_bias) {
5514 __ CmpUltD(dst, rhs, lhs);
5515 } else {
5516 __ CmpLtD(dst, rhs, lhs);
5517 }
5518 return false;
5519 case kCondGE:
5520 if (gt_bias) {
5521 __ CmpUleD(dst, rhs, lhs);
5522 } else {
5523 __ CmpLeD(dst, rhs, lhs);
5524 }
5525 return false;
5526 default:
5527 LOG(FATAL) << "Unexpected non-floating-point condition";
5528 UNREACHABLE();
5529 }
5530 }
5531}
5532
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005533void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5534 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005535 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005536 LocationSummary* locations,
5537 MipsLabel* label) {
5538 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5539 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5540 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005541 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005542 if (isR6) {
5543 switch (cond) {
5544 case kCondEQ:
5545 __ CmpEqS(FTMP, lhs, rhs);
5546 __ Bc1nez(FTMP, label);
5547 break;
5548 case kCondNE:
5549 __ CmpEqS(FTMP, lhs, rhs);
5550 __ Bc1eqz(FTMP, label);
5551 break;
5552 case kCondLT:
5553 if (gt_bias) {
5554 __ CmpLtS(FTMP, lhs, rhs);
5555 } else {
5556 __ CmpUltS(FTMP, lhs, rhs);
5557 }
5558 __ Bc1nez(FTMP, label);
5559 break;
5560 case kCondLE:
5561 if (gt_bias) {
5562 __ CmpLeS(FTMP, lhs, rhs);
5563 } else {
5564 __ CmpUleS(FTMP, lhs, rhs);
5565 }
5566 __ Bc1nez(FTMP, label);
5567 break;
5568 case kCondGT:
5569 if (gt_bias) {
5570 __ CmpUltS(FTMP, rhs, lhs);
5571 } else {
5572 __ CmpLtS(FTMP, rhs, lhs);
5573 }
5574 __ Bc1nez(FTMP, label);
5575 break;
5576 case kCondGE:
5577 if (gt_bias) {
5578 __ CmpUleS(FTMP, rhs, lhs);
5579 } else {
5580 __ CmpLeS(FTMP, rhs, lhs);
5581 }
5582 __ Bc1nez(FTMP, label);
5583 break;
5584 default:
5585 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005586 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005587 }
5588 } else {
5589 switch (cond) {
5590 case kCondEQ:
5591 __ CeqS(0, lhs, rhs);
5592 __ Bc1t(0, label);
5593 break;
5594 case kCondNE:
5595 __ CeqS(0, lhs, rhs);
5596 __ Bc1f(0, label);
5597 break;
5598 case kCondLT:
5599 if (gt_bias) {
5600 __ ColtS(0, lhs, rhs);
5601 } else {
5602 __ CultS(0, lhs, rhs);
5603 }
5604 __ Bc1t(0, label);
5605 break;
5606 case kCondLE:
5607 if (gt_bias) {
5608 __ ColeS(0, lhs, rhs);
5609 } else {
5610 __ CuleS(0, lhs, rhs);
5611 }
5612 __ Bc1t(0, label);
5613 break;
5614 case kCondGT:
5615 if (gt_bias) {
5616 __ CultS(0, rhs, lhs);
5617 } else {
5618 __ ColtS(0, rhs, lhs);
5619 }
5620 __ Bc1t(0, label);
5621 break;
5622 case kCondGE:
5623 if (gt_bias) {
5624 __ CuleS(0, rhs, lhs);
5625 } else {
5626 __ ColeS(0, rhs, lhs);
5627 }
5628 __ Bc1t(0, label);
5629 break;
5630 default:
5631 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005632 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005633 }
5634 }
5635 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005636 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005637 if (isR6) {
5638 switch (cond) {
5639 case kCondEQ:
5640 __ CmpEqD(FTMP, lhs, rhs);
5641 __ Bc1nez(FTMP, label);
5642 break;
5643 case kCondNE:
5644 __ CmpEqD(FTMP, lhs, rhs);
5645 __ Bc1eqz(FTMP, label);
5646 break;
5647 case kCondLT:
5648 if (gt_bias) {
5649 __ CmpLtD(FTMP, lhs, rhs);
5650 } else {
5651 __ CmpUltD(FTMP, lhs, rhs);
5652 }
5653 __ Bc1nez(FTMP, label);
5654 break;
5655 case kCondLE:
5656 if (gt_bias) {
5657 __ CmpLeD(FTMP, lhs, rhs);
5658 } else {
5659 __ CmpUleD(FTMP, lhs, rhs);
5660 }
5661 __ Bc1nez(FTMP, label);
5662 break;
5663 case kCondGT:
5664 if (gt_bias) {
5665 __ CmpUltD(FTMP, rhs, lhs);
5666 } else {
5667 __ CmpLtD(FTMP, rhs, lhs);
5668 }
5669 __ Bc1nez(FTMP, label);
5670 break;
5671 case kCondGE:
5672 if (gt_bias) {
5673 __ CmpUleD(FTMP, rhs, lhs);
5674 } else {
5675 __ CmpLeD(FTMP, rhs, lhs);
5676 }
5677 __ Bc1nez(FTMP, label);
5678 break;
5679 default:
5680 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005681 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005682 }
5683 } else {
5684 switch (cond) {
5685 case kCondEQ:
5686 __ CeqD(0, lhs, rhs);
5687 __ Bc1t(0, label);
5688 break;
5689 case kCondNE:
5690 __ CeqD(0, lhs, rhs);
5691 __ Bc1f(0, label);
5692 break;
5693 case kCondLT:
5694 if (gt_bias) {
5695 __ ColtD(0, lhs, rhs);
5696 } else {
5697 __ CultD(0, lhs, rhs);
5698 }
5699 __ Bc1t(0, label);
5700 break;
5701 case kCondLE:
5702 if (gt_bias) {
5703 __ ColeD(0, lhs, rhs);
5704 } else {
5705 __ CuleD(0, lhs, rhs);
5706 }
5707 __ Bc1t(0, label);
5708 break;
5709 case kCondGT:
5710 if (gt_bias) {
5711 __ CultD(0, rhs, lhs);
5712 } else {
5713 __ ColtD(0, rhs, lhs);
5714 }
5715 __ Bc1t(0, label);
5716 break;
5717 case kCondGE:
5718 if (gt_bias) {
5719 __ CuleD(0, rhs, lhs);
5720 } else {
5721 __ ColeD(0, rhs, lhs);
5722 }
5723 __ Bc1t(0, label);
5724 break;
5725 default:
5726 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005727 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005728 }
5729 }
5730 }
5731}
5732
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005733void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005734 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005735 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005736 MipsLabel* false_target) {
5737 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005738
David Brazdil0debae72015-11-12 18:37:00 +00005739 if (true_target == nullptr && false_target == nullptr) {
5740 // Nothing to do. The code always falls through.
5741 return;
5742 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005743 // Constant condition, statically compared against "true" (integer value 1).
5744 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005745 if (true_target != nullptr) {
5746 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005747 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005748 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005749 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005750 if (false_target != nullptr) {
5751 __ B(false_target);
5752 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005753 }
David Brazdil0debae72015-11-12 18:37:00 +00005754 return;
5755 }
5756
5757 // The following code generates these patterns:
5758 // (1) true_target == nullptr && false_target != nullptr
5759 // - opposite condition true => branch to false_target
5760 // (2) true_target != nullptr && false_target == nullptr
5761 // - condition true => branch to true_target
5762 // (3) true_target != nullptr && false_target != nullptr
5763 // - condition true => branch to true_target
5764 // - branch to false_target
5765 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005766 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005767 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005768 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005769 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005770 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5771 } else {
5772 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5773 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005774 } else {
5775 // The condition instruction has not been materialized, use its inputs as
5776 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005777 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005778 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005779 LocationSummary* locations = cond->GetLocations();
5780 IfCondition if_cond = condition->GetCondition();
5781 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005782
David Brazdil0debae72015-11-12 18:37:00 +00005783 if (true_target == nullptr) {
5784 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005785 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005786 }
5787
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005788 switch (type) {
5789 default:
5790 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5791 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005792 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005793 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5794 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005795 case DataType::Type::kFloat32:
5796 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005797 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5798 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005799 }
5800 }
David Brazdil0debae72015-11-12 18:37:00 +00005801
5802 // If neither branch falls through (case 3), the conditional branch to `true_target`
5803 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5804 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005805 __ B(false_target);
5806 }
5807}
5808
5809void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005810 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005811 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005812 locations->SetInAt(0, Location::RequiresRegister());
5813 }
5814}
5815
5816void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005817 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5818 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5819 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5820 nullptr : codegen_->GetLabelOf(true_successor);
5821 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5822 nullptr : codegen_->GetLabelOf(false_successor);
5823 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005824}
5825
5826void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005827 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005828 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005829 InvokeRuntimeCallingConvention calling_convention;
5830 RegisterSet caller_saves = RegisterSet::Empty();
5831 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5832 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005833 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005834 locations->SetInAt(0, Location::RequiresRegister());
5835 }
5836}
5837
5838void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005839 SlowPathCodeMIPS* slow_path =
5840 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005841 GenerateTestAndBranch(deoptimize,
5842 /* condition_input_index */ 0,
5843 slow_path->GetEntryLabel(),
5844 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005845}
5846
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005847// This function returns true if a conditional move can be generated for HSelect.
5848// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5849// branches and regular moves.
5850//
5851// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5852//
5853// While determining feasibility of a conditional move and setting inputs/outputs
5854// are two distinct tasks, this function does both because they share quite a bit
5855// of common logic.
5856static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5857 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5858 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5859 HCondition* condition = cond->AsCondition();
5860
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005861 DataType::Type cond_type =
5862 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5863 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005864
5865 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5866 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5867 bool is_true_value_zero_constant =
5868 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5869 bool is_false_value_zero_constant =
5870 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5871
5872 bool can_move_conditionally = false;
5873 bool use_const_for_false_in = false;
5874 bool use_const_for_true_in = false;
5875
5876 if (!cond->IsConstant()) {
5877 switch (cond_type) {
5878 default:
5879 switch (dst_type) {
5880 default:
5881 // Moving int on int condition.
5882 if (is_r6) {
5883 if (is_true_value_zero_constant) {
5884 // seleqz out_reg, false_reg, cond_reg
5885 can_move_conditionally = true;
5886 use_const_for_true_in = true;
5887 } else if (is_false_value_zero_constant) {
5888 // selnez out_reg, true_reg, cond_reg
5889 can_move_conditionally = true;
5890 use_const_for_false_in = true;
5891 } else if (materialized) {
5892 // Not materializing unmaterialized int conditions
5893 // to keep the instruction count low.
5894 // selnez AT, true_reg, cond_reg
5895 // seleqz TMP, false_reg, cond_reg
5896 // or out_reg, AT, TMP
5897 can_move_conditionally = true;
5898 }
5899 } else {
5900 // movn out_reg, true_reg/ZERO, cond_reg
5901 can_move_conditionally = true;
5902 use_const_for_true_in = is_true_value_zero_constant;
5903 }
5904 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005905 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005906 // Moving long on int condition.
5907 if (is_r6) {
5908 if (is_true_value_zero_constant) {
5909 // seleqz out_reg_lo, false_reg_lo, cond_reg
5910 // seleqz out_reg_hi, false_reg_hi, cond_reg
5911 can_move_conditionally = true;
5912 use_const_for_true_in = true;
5913 } else if (is_false_value_zero_constant) {
5914 // selnez out_reg_lo, true_reg_lo, cond_reg
5915 // selnez out_reg_hi, true_reg_hi, cond_reg
5916 can_move_conditionally = true;
5917 use_const_for_false_in = true;
5918 }
5919 // Other long conditional moves would generate 6+ instructions,
5920 // which is too many.
5921 } else {
5922 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5923 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5924 can_move_conditionally = true;
5925 use_const_for_true_in = is_true_value_zero_constant;
5926 }
5927 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005928 case DataType::Type::kFloat32:
5929 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005930 // Moving float/double on int condition.
5931 if (is_r6) {
5932 if (materialized) {
5933 // Not materializing unmaterialized int conditions
5934 // to keep the instruction count low.
5935 can_move_conditionally = true;
5936 if (is_true_value_zero_constant) {
5937 // sltu TMP, ZERO, cond_reg
5938 // mtc1 TMP, temp_cond_reg
5939 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5940 use_const_for_true_in = true;
5941 } else if (is_false_value_zero_constant) {
5942 // sltu TMP, ZERO, cond_reg
5943 // mtc1 TMP, temp_cond_reg
5944 // selnez.fmt out_reg, true_reg, temp_cond_reg
5945 use_const_for_false_in = true;
5946 } else {
5947 // sltu TMP, ZERO, cond_reg
5948 // mtc1 TMP, temp_cond_reg
5949 // sel.fmt temp_cond_reg, false_reg, true_reg
5950 // mov.fmt out_reg, temp_cond_reg
5951 }
5952 }
5953 } else {
5954 // movn.fmt out_reg, true_reg, cond_reg
5955 can_move_conditionally = true;
5956 }
5957 break;
5958 }
5959 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005960 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005961 // We don't materialize long comparison now
5962 // and use conditional branches instead.
5963 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005964 case DataType::Type::kFloat32:
5965 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005966 switch (dst_type) {
5967 default:
5968 // Moving int on float/double condition.
5969 if (is_r6) {
5970 if (is_true_value_zero_constant) {
5971 // mfc1 TMP, temp_cond_reg
5972 // seleqz out_reg, false_reg, TMP
5973 can_move_conditionally = true;
5974 use_const_for_true_in = true;
5975 } else if (is_false_value_zero_constant) {
5976 // mfc1 TMP, temp_cond_reg
5977 // selnez out_reg, true_reg, TMP
5978 can_move_conditionally = true;
5979 use_const_for_false_in = true;
5980 } else {
5981 // mfc1 TMP, temp_cond_reg
5982 // selnez AT, true_reg, TMP
5983 // seleqz TMP, false_reg, TMP
5984 // or out_reg, AT, TMP
5985 can_move_conditionally = true;
5986 }
5987 } else {
5988 // movt out_reg, true_reg/ZERO, cc
5989 can_move_conditionally = true;
5990 use_const_for_true_in = is_true_value_zero_constant;
5991 }
5992 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005993 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005994 // Moving long on float/double condition.
5995 if (is_r6) {
5996 if (is_true_value_zero_constant) {
5997 // mfc1 TMP, temp_cond_reg
5998 // seleqz out_reg_lo, false_reg_lo, TMP
5999 // seleqz out_reg_hi, false_reg_hi, TMP
6000 can_move_conditionally = true;
6001 use_const_for_true_in = true;
6002 } else if (is_false_value_zero_constant) {
6003 // mfc1 TMP, temp_cond_reg
6004 // selnez out_reg_lo, true_reg_lo, TMP
6005 // selnez out_reg_hi, true_reg_hi, TMP
6006 can_move_conditionally = true;
6007 use_const_for_false_in = true;
6008 }
6009 // Other long conditional moves would generate 6+ instructions,
6010 // which is too many.
6011 } else {
6012 // movt out_reg_lo, true_reg_lo/ZERO, cc
6013 // movt out_reg_hi, true_reg_hi/ZERO, cc
6014 can_move_conditionally = true;
6015 use_const_for_true_in = is_true_value_zero_constant;
6016 }
6017 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006018 case DataType::Type::kFloat32:
6019 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006020 // Moving float/double on float/double condition.
6021 if (is_r6) {
6022 can_move_conditionally = true;
6023 if (is_true_value_zero_constant) {
6024 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6025 use_const_for_true_in = true;
6026 } else if (is_false_value_zero_constant) {
6027 // selnez.fmt out_reg, true_reg, temp_cond_reg
6028 use_const_for_false_in = true;
6029 } else {
6030 // sel.fmt temp_cond_reg, false_reg, true_reg
6031 // mov.fmt out_reg, temp_cond_reg
6032 }
6033 } else {
6034 // movt.fmt out_reg, true_reg, cc
6035 can_move_conditionally = true;
6036 }
6037 break;
6038 }
6039 break;
6040 }
6041 }
6042
6043 if (can_move_conditionally) {
6044 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6045 } else {
6046 DCHECK(!use_const_for_false_in);
6047 DCHECK(!use_const_for_true_in);
6048 }
6049
6050 if (locations_to_set != nullptr) {
6051 if (use_const_for_false_in) {
6052 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6053 } else {
6054 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006055 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006056 ? Location::RequiresFpuRegister()
6057 : Location::RequiresRegister());
6058 }
6059 if (use_const_for_true_in) {
6060 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6061 } else {
6062 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006063 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006064 ? Location::RequiresFpuRegister()
6065 : Location::RequiresRegister());
6066 }
6067 if (materialized) {
6068 locations_to_set->SetInAt(2, Location::RequiresRegister());
6069 }
6070 // On R6 we don't require the output to be the same as the
6071 // first input for conditional moves unlike on R2.
6072 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6073 if (is_out_same_as_first_in) {
6074 locations_to_set->SetOut(Location::SameAsFirstInput());
6075 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006076 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006077 ? Location::RequiresFpuRegister()
6078 : Location::RequiresRegister());
6079 }
6080 }
6081
6082 return can_move_conditionally;
6083}
6084
6085void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6086 LocationSummary* locations = select->GetLocations();
6087 Location dst = locations->Out();
6088 Location src = locations->InAt(1);
6089 Register src_reg = ZERO;
6090 Register src_reg_high = ZERO;
6091 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6092 Register cond_reg = TMP;
6093 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006094 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006095 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006096 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006097
6098 if (IsBooleanValueOrMaterializedCondition(cond)) {
6099 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6100 } else {
6101 HCondition* condition = cond->AsCondition();
6102 LocationSummary* cond_locations = cond->GetLocations();
6103 IfCondition if_cond = condition->GetCondition();
6104 cond_type = condition->InputAt(0)->GetType();
6105 switch (cond_type) {
6106 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006107 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006108 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6109 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006110 case DataType::Type::kFloat32:
6111 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006112 cond_inverted = MaterializeFpCompareR2(if_cond,
6113 condition->IsGtBias(),
6114 cond_type,
6115 cond_locations,
6116 cond_cc);
6117 break;
6118 }
6119 }
6120
6121 DCHECK(dst.Equals(locations->InAt(0)));
6122 if (src.IsRegister()) {
6123 src_reg = src.AsRegister<Register>();
6124 } else if (src.IsRegisterPair()) {
6125 src_reg = src.AsRegisterPairLow<Register>();
6126 src_reg_high = src.AsRegisterPairHigh<Register>();
6127 } else if (src.IsConstant()) {
6128 DCHECK(src.GetConstant()->IsZeroBitPattern());
6129 }
6130
6131 switch (cond_type) {
6132 default:
6133 switch (dst_type) {
6134 default:
6135 if (cond_inverted) {
6136 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6137 } else {
6138 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6139 }
6140 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006141 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006142 if (cond_inverted) {
6143 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6144 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6145 } else {
6146 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6147 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6148 }
6149 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006150 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006151 if (cond_inverted) {
6152 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6153 } else {
6154 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6155 }
6156 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006157 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006158 if (cond_inverted) {
6159 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6160 } else {
6161 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6162 }
6163 break;
6164 }
6165 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006166 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006167 LOG(FATAL) << "Unreachable";
6168 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006169 case DataType::Type::kFloat32:
6170 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006171 switch (dst_type) {
6172 default:
6173 if (cond_inverted) {
6174 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6175 } else {
6176 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6177 }
6178 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006179 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006180 if (cond_inverted) {
6181 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6182 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6183 } else {
6184 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6185 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6186 }
6187 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006188 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006189 if (cond_inverted) {
6190 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6191 } else {
6192 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6193 }
6194 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006195 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006196 if (cond_inverted) {
6197 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6198 } else {
6199 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6200 }
6201 break;
6202 }
6203 break;
6204 }
6205}
6206
6207void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6208 LocationSummary* locations = select->GetLocations();
6209 Location dst = locations->Out();
6210 Location false_src = locations->InAt(0);
6211 Location true_src = locations->InAt(1);
6212 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6213 Register cond_reg = TMP;
6214 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006215 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006216 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006217 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006218
6219 if (IsBooleanValueOrMaterializedCondition(cond)) {
6220 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6221 } else {
6222 HCondition* condition = cond->AsCondition();
6223 LocationSummary* cond_locations = cond->GetLocations();
6224 IfCondition if_cond = condition->GetCondition();
6225 cond_type = condition->InputAt(0)->GetType();
6226 switch (cond_type) {
6227 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006228 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006229 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6230 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006231 case DataType::Type::kFloat32:
6232 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006233 cond_inverted = MaterializeFpCompareR6(if_cond,
6234 condition->IsGtBias(),
6235 cond_type,
6236 cond_locations,
6237 fcond_reg);
6238 break;
6239 }
6240 }
6241
6242 if (true_src.IsConstant()) {
6243 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6244 }
6245 if (false_src.IsConstant()) {
6246 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6247 }
6248
6249 switch (dst_type) {
6250 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006251 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006252 __ Mfc1(cond_reg, fcond_reg);
6253 }
6254 if (true_src.IsConstant()) {
6255 if (cond_inverted) {
6256 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6257 } else {
6258 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6259 }
6260 } else if (false_src.IsConstant()) {
6261 if (cond_inverted) {
6262 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6263 } else {
6264 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6265 }
6266 } else {
6267 DCHECK_NE(cond_reg, AT);
6268 if (cond_inverted) {
6269 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6270 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6271 } else {
6272 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6273 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6274 }
6275 __ Or(dst.AsRegister<Register>(), AT, TMP);
6276 }
6277 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006278 case DataType::Type::kInt64: {
6279 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006280 __ Mfc1(cond_reg, fcond_reg);
6281 }
6282 Register dst_lo = dst.AsRegisterPairLow<Register>();
6283 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6284 if (true_src.IsConstant()) {
6285 Register src_lo = false_src.AsRegisterPairLow<Register>();
6286 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6287 if (cond_inverted) {
6288 __ Selnez(dst_lo, src_lo, cond_reg);
6289 __ Selnez(dst_hi, src_hi, cond_reg);
6290 } else {
6291 __ Seleqz(dst_lo, src_lo, cond_reg);
6292 __ Seleqz(dst_hi, src_hi, cond_reg);
6293 }
6294 } else {
6295 DCHECK(false_src.IsConstant());
6296 Register src_lo = true_src.AsRegisterPairLow<Register>();
6297 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6298 if (cond_inverted) {
6299 __ Seleqz(dst_lo, src_lo, cond_reg);
6300 __ Seleqz(dst_hi, src_hi, cond_reg);
6301 } else {
6302 __ Selnez(dst_lo, src_lo, cond_reg);
6303 __ Selnez(dst_hi, src_hi, cond_reg);
6304 }
6305 }
6306 break;
6307 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006308 case DataType::Type::kFloat32: {
6309 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006310 // sel*.fmt tests bit 0 of the condition register, account for that.
6311 __ Sltu(TMP, ZERO, cond_reg);
6312 __ Mtc1(TMP, fcond_reg);
6313 }
6314 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6315 if (true_src.IsConstant()) {
6316 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6317 if (cond_inverted) {
6318 __ SelnezS(dst_reg, src_reg, fcond_reg);
6319 } else {
6320 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6321 }
6322 } else if (false_src.IsConstant()) {
6323 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6324 if (cond_inverted) {
6325 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6326 } else {
6327 __ SelnezS(dst_reg, src_reg, fcond_reg);
6328 }
6329 } else {
6330 if (cond_inverted) {
6331 __ SelS(fcond_reg,
6332 true_src.AsFpuRegister<FRegister>(),
6333 false_src.AsFpuRegister<FRegister>());
6334 } else {
6335 __ SelS(fcond_reg,
6336 false_src.AsFpuRegister<FRegister>(),
6337 true_src.AsFpuRegister<FRegister>());
6338 }
6339 __ MovS(dst_reg, fcond_reg);
6340 }
6341 break;
6342 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006343 case DataType::Type::kFloat64: {
6344 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006345 // sel*.fmt tests bit 0 of the condition register, account for that.
6346 __ Sltu(TMP, ZERO, cond_reg);
6347 __ Mtc1(TMP, fcond_reg);
6348 }
6349 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6350 if (true_src.IsConstant()) {
6351 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6352 if (cond_inverted) {
6353 __ SelnezD(dst_reg, src_reg, fcond_reg);
6354 } else {
6355 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6356 }
6357 } else if (false_src.IsConstant()) {
6358 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6359 if (cond_inverted) {
6360 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6361 } else {
6362 __ SelnezD(dst_reg, src_reg, fcond_reg);
6363 }
6364 } else {
6365 if (cond_inverted) {
6366 __ SelD(fcond_reg,
6367 true_src.AsFpuRegister<FRegister>(),
6368 false_src.AsFpuRegister<FRegister>());
6369 } else {
6370 __ SelD(fcond_reg,
6371 false_src.AsFpuRegister<FRegister>(),
6372 true_src.AsFpuRegister<FRegister>());
6373 }
6374 __ MovD(dst_reg, fcond_reg);
6375 }
6376 break;
6377 }
6378 }
6379}
6380
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006381void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006382 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006383 LocationSummary(flag, LocationSummary::kNoCall);
6384 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006385}
6386
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006387void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6388 __ LoadFromOffset(kLoadWord,
6389 flag->GetLocations()->Out().AsRegister<Register>(),
6390 SP,
6391 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006392}
6393
David Brazdil74eb1b22015-12-14 11:44:01 +00006394void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006395 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006396 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006397}
6398
6399void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006400 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6401 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6402 if (is_r6) {
6403 GenConditionalMoveR6(select);
6404 } else {
6405 GenConditionalMoveR2(select);
6406 }
6407 } else {
6408 LocationSummary* locations = select->GetLocations();
6409 MipsLabel false_target;
6410 GenerateTestAndBranch(select,
6411 /* condition_input_index */ 2,
6412 /* true_target */ nullptr,
6413 &false_target);
6414 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6415 __ Bind(&false_target);
6416 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006417}
6418
David Srbecky0cf44932015-12-09 14:09:59 +00006419void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006420 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006421}
6422
David Srbeckyd28f4a02016-03-14 17:14:24 +00006423void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6424 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006425}
6426
6427void CodeGeneratorMIPS::GenerateNop() {
6428 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006429}
6430
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006431void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006432 DataType::Type field_type = field_info.GetFieldType();
6433 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006434 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006435 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006436 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006437 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006438 instruction,
6439 generate_volatile
6440 ? LocationSummary::kCallOnMainOnly
6441 : (object_field_get_with_read_barrier
6442 ? LocationSummary::kCallOnSlowPath
6443 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006444
Alexey Frunzec61c0762017-04-10 13:54:23 -07006445 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6446 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006448 locations->SetInAt(0, Location::RequiresRegister());
6449 if (generate_volatile) {
6450 InvokeRuntimeCallingConvention calling_convention;
6451 // need A0 to hold base + offset
6452 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006453 if (field_type == DataType::Type::kInt64) {
6454 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006455 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006456 // Use Location::Any() to prevent situations when running out of available fp registers.
6457 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006458 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006459 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006460 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6461 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6462 }
6463 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006464 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006465 locations->SetOut(Location::RequiresFpuRegister());
6466 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006467 // The output overlaps in the case of an object field get with
6468 // read barriers enabled: we do not want the move to overwrite the
6469 // object's location, as we need it to emit the read barrier.
6470 locations->SetOut(Location::RequiresRegister(),
6471 object_field_get_with_read_barrier
6472 ? Location::kOutputOverlap
6473 : Location::kNoOutputOverlap);
6474 }
6475 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6476 // We need a temporary register for the read barrier marking slow
6477 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006478 if (!kBakerReadBarrierThunksEnableForFields) {
6479 locations->AddTemp(Location::RequiresRegister());
6480 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006481 }
6482 }
6483}
6484
6485void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6486 const FieldInfo& field_info,
6487 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006488 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6489 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006490 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006491 Location obj_loc = locations->InAt(0);
6492 Register obj = obj_loc.AsRegister<Register>();
6493 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006494 LoadOperandType load_type = kLoadUnsignedByte;
6495 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006496 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006497 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006498
6499 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006500 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006501 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006502 load_type = kLoadUnsignedByte;
6503 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006504 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006505 load_type = kLoadSignedByte;
6506 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006507 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006508 load_type = kLoadUnsignedHalfword;
6509 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006510 case DataType::Type::kInt16:
6511 load_type = kLoadSignedHalfword;
6512 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006513 case DataType::Type::kInt32:
6514 case DataType::Type::kFloat32:
6515 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006516 load_type = kLoadWord;
6517 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006518 case DataType::Type::kInt64:
6519 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006520 load_type = kLoadDoubleword;
6521 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006522 case DataType::Type::kUint32:
6523 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006524 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006525 LOG(FATAL) << "Unreachable type " << type;
6526 UNREACHABLE();
6527 }
6528
6529 if (is_volatile && load_type == kLoadDoubleword) {
6530 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006531 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006532 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006533 __ LoadFromOffset(kLoadWord,
6534 ZERO,
6535 locations->GetTemp(0).AsRegister<Register>(),
6536 0,
6537 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006538 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006539 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006540 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006541 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006542 if (dst_loc.IsFpuRegister()) {
6543 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006544 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006545 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006546 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006547 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006548 __ StoreToOffset(kStoreWord,
6549 locations->GetTemp(1).AsRegister<Register>(),
6550 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006551 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006552 __ StoreToOffset(kStoreWord,
6553 locations->GetTemp(2).AsRegister<Register>(),
6554 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006555 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006556 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006557 }
6558 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006559 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006560 // /* HeapReference<Object> */ dst = *(obj + offset)
6561 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006562 Location temp_loc =
6563 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006564 // Note that a potential implicit null check is handled in this
6565 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6566 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6567 dst_loc,
6568 obj,
6569 offset,
6570 temp_loc,
6571 /* needs_null_check */ true);
6572 if (is_volatile) {
6573 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6574 }
6575 } else {
6576 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6577 if (is_volatile) {
6578 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6579 }
6580 // If read barriers are enabled, emit read barriers other than
6581 // Baker's using a slow path (and also unpoison the loaded
6582 // reference, if heap poisoning is enabled).
6583 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6584 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006585 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006586 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006587 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006588 DCHECK(dst_loc.IsRegisterPair());
6589 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006590 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006591 DCHECK(dst_loc.IsRegister());
6592 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006593 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006594 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006595 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006596 DCHECK(dst_loc.IsFpuRegister());
6597 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006598 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006599 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006600 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006601 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006602 }
6603 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006604 }
6605
Alexey Frunze15958152017-02-09 19:08:30 -08006606 // Memory barriers, in the case of references, are handled in the
6607 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006608 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006609 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6610 }
6611}
6612
6613void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006614 DataType::Type field_type = field_info.GetFieldType();
6615 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006616 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006617 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006618 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006619
6620 locations->SetInAt(0, Location::RequiresRegister());
6621 if (generate_volatile) {
6622 InvokeRuntimeCallingConvention calling_convention;
6623 // need A0 to hold base + offset
6624 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006625 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006626 locations->SetInAt(1, Location::RegisterPairLocation(
6627 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6628 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006629 // Use Location::Any() to prevent situations when running out of available fp registers.
6630 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006631 // Pass FP parameters in core registers.
6632 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6633 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6634 }
6635 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006636 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006637 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006638 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006639 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006640 }
6641 }
6642}
6643
6644void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6645 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006646 uint32_t dex_pc,
6647 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006648 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006649 LocationSummary* locations = instruction->GetLocations();
6650 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006651 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006652 StoreOperandType store_type = kStoreByte;
6653 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006654 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006655 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006656 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006657
6658 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006659 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006660 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006661 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006662 store_type = kStoreByte;
6663 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006664 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006665 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006666 store_type = kStoreHalfword;
6667 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006668 case DataType::Type::kInt32:
6669 case DataType::Type::kFloat32:
6670 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006671 store_type = kStoreWord;
6672 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006673 case DataType::Type::kInt64:
6674 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006675 store_type = kStoreDoubleword;
6676 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006677 case DataType::Type::kUint32:
6678 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006679 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006680 LOG(FATAL) << "Unreachable type " << type;
6681 UNREACHABLE();
6682 }
6683
6684 if (is_volatile) {
6685 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6686 }
6687
6688 if (is_volatile && store_type == kStoreDoubleword) {
6689 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006690 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006691 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006692 __ LoadFromOffset(kLoadWord,
6693 ZERO,
6694 locations->GetTemp(0).AsRegister<Register>(),
6695 0,
6696 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006697 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006698 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006699 if (value_location.IsFpuRegister()) {
6700 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6701 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006702 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006703 value_location.AsFpuRegister<FRegister>());
6704 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006705 __ LoadFromOffset(kLoadWord,
6706 locations->GetTemp(1).AsRegister<Register>(),
6707 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006708 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006709 __ LoadFromOffset(kLoadWord,
6710 locations->GetTemp(2).AsRegister<Register>(),
6711 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006712 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006713 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006714 DCHECK(value_location.IsConstant());
6715 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6716 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006717 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6718 locations->GetTemp(1).AsRegister<Register>(),
6719 value);
6720 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006721 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006722 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006723 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6724 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006725 if (value_location.IsConstant()) {
6726 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6727 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006728 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006729 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006730 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006731 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006732 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006733 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006734 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006735 if (kPoisonHeapReferences && needs_write_barrier) {
6736 // Note that in the case where `value` is a null reference,
6737 // we do not enter this block, as a null reference does not
6738 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006739 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006740 __ PoisonHeapReference(TMP, src);
6741 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6742 } else {
6743 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6744 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006745 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006746 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006747 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006748 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006749 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006750 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006751 }
6752 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006753 }
6754
Alexey Frunzec061de12017-02-14 13:27:23 -08006755 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006756 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006757 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006758 }
6759
6760 if (is_volatile) {
6761 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6762 }
6763}
6764
6765void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6766 HandleFieldGet(instruction, instruction->GetFieldInfo());
6767}
6768
6769void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6770 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6771}
6772
6773void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6774 HandleFieldSet(instruction, instruction->GetFieldInfo());
6775}
6776
6777void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006778 HandleFieldSet(instruction,
6779 instruction->GetFieldInfo(),
6780 instruction->GetDexPc(),
6781 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006782}
6783
Alexey Frunze15958152017-02-09 19:08:30 -08006784void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6785 HInstruction* instruction,
6786 Location out,
6787 uint32_t offset,
6788 Location maybe_temp,
6789 ReadBarrierOption read_barrier_option) {
6790 Register out_reg = out.AsRegister<Register>();
6791 if (read_barrier_option == kWithReadBarrier) {
6792 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006793 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6794 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6795 }
Alexey Frunze15958152017-02-09 19:08:30 -08006796 if (kUseBakerReadBarrier) {
6797 // Load with fast path based Baker's read barrier.
6798 // /* HeapReference<Object> */ out = *(out + offset)
6799 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6800 out,
6801 out_reg,
6802 offset,
6803 maybe_temp,
6804 /* needs_null_check */ false);
6805 } else {
6806 // Load with slow path based read barrier.
6807 // Save the value of `out` into `maybe_temp` before overwriting it
6808 // in the following move operation, as we will need it for the
6809 // read barrier below.
6810 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6811 // /* HeapReference<Object> */ out = *(out + offset)
6812 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6813 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6814 }
6815 } else {
6816 // Plain load with no read barrier.
6817 // /* HeapReference<Object> */ out = *(out + offset)
6818 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6819 __ MaybeUnpoisonHeapReference(out_reg);
6820 }
6821}
6822
6823void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6824 HInstruction* instruction,
6825 Location out,
6826 Location obj,
6827 uint32_t offset,
6828 Location maybe_temp,
6829 ReadBarrierOption read_barrier_option) {
6830 Register out_reg = out.AsRegister<Register>();
6831 Register obj_reg = obj.AsRegister<Register>();
6832 if (read_barrier_option == kWithReadBarrier) {
6833 CHECK(kEmitCompilerReadBarrier);
6834 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006835 if (!kBakerReadBarrierThunksEnableForFields) {
6836 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6837 }
Alexey Frunze15958152017-02-09 19:08:30 -08006838 // Load with fast path based Baker's read barrier.
6839 // /* HeapReference<Object> */ out = *(obj + offset)
6840 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6841 out,
6842 obj_reg,
6843 offset,
6844 maybe_temp,
6845 /* needs_null_check */ false);
6846 } else {
6847 // Load with slow path based read barrier.
6848 // /* HeapReference<Object> */ out = *(obj + offset)
6849 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6850 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6851 }
6852 } else {
6853 // Plain load with no read barrier.
6854 // /* HeapReference<Object> */ out = *(obj + offset)
6855 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6856 __ MaybeUnpoisonHeapReference(out_reg);
6857 }
6858}
6859
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006860static inline int GetBakerMarkThunkNumber(Register reg) {
6861 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6862 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6863 return reg - V0;
6864 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6865 return 14 + (reg - S2);
6866 } else if (reg == FP) { // One more.
6867 return 20;
6868 }
6869 LOG(FATAL) << "Unexpected register " << reg;
6870 UNREACHABLE();
6871}
6872
6873static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6874 int num = GetBakerMarkThunkNumber(reg) +
6875 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6876 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6877}
6878
6879static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6880 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6881 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6882}
6883
Alexey Frunze15958152017-02-09 19:08:30 -08006884void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6885 Location root,
6886 Register obj,
6887 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006888 ReadBarrierOption read_barrier_option,
6889 MipsLabel* label_low) {
6890 bool reordering;
6891 if (label_low != nullptr) {
6892 DCHECK_EQ(offset, 0x5678u);
6893 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006894 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006895 if (read_barrier_option == kWithReadBarrier) {
6896 DCHECK(kEmitCompilerReadBarrier);
6897 if (kUseBakerReadBarrier) {
6898 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6899 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006900 if (kBakerReadBarrierThunksEnableForGcRoots) {
6901 // Note that we do not actually check the value of `GetIsGcMarking()`
6902 // to decide whether to mark the loaded GC root or not. Instead, we
6903 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6904 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6905 // vice versa.
6906 //
6907 // We use thunks for the slow path. That thunk checks the reference
6908 // and jumps to the entrypoint if needed.
6909 //
6910 // temp = Thread::Current()->pReadBarrierMarkReg00
6911 // // AKA &art_quick_read_barrier_mark_introspection.
6912 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6913 // if (temp != nullptr) {
6914 // temp = &gc_root_thunk<root_reg>
6915 // root = temp(root)
6916 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006917
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006918 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6919 const int32_t entry_point_offset =
6920 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6921 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6922 int16_t offset_low = Low16Bits(offset);
6923 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6924 // extension in lw.
6925 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6926 Register base = short_offset ? obj : TMP;
6927 // Loading the entrypoint does not require a load acquire since it is only changed when
6928 // threads are suspended or running a checkpoint.
6929 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6930 reordering = __ SetReorder(false);
6931 if (!short_offset) {
6932 DCHECK(!label_low);
6933 __ AddUpper(base, obj, offset_high);
6934 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006935 MipsLabel skip_call;
6936 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006937 if (label_low != nullptr) {
6938 DCHECK(short_offset);
6939 __ Bind(label_low);
6940 }
6941 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6942 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6943 // in delay slot.
6944 if (isR6) {
6945 __ Jialc(T9, thunk_disp);
6946 } else {
6947 __ Addiu(T9, T9, thunk_disp);
6948 __ Jalr(T9);
6949 __ Nop();
6950 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006951 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006952 __ SetReorder(reordering);
6953 } else {
6954 // Note that we do not actually check the value of `GetIsGcMarking()`
6955 // to decide whether to mark the loaded GC root or not. Instead, we
6956 // load into `temp` (T9) the read barrier mark entry point corresponding
6957 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6958 // is false, and vice versa.
6959 //
6960 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6961 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6962 // if (temp != null) {
6963 // root = temp(root)
6964 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006965
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006966 if (label_low != nullptr) {
6967 reordering = __ SetReorder(false);
6968 __ Bind(label_low);
6969 }
6970 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6971 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6972 if (label_low != nullptr) {
6973 __ SetReorder(reordering);
6974 }
6975 static_assert(
6976 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6977 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6978 "have different sizes.");
6979 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6980 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6981 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006982
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006983 // Slow path marking the GC root `root`.
6984 Location temp = Location::RegisterLocation(T9);
6985 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006986 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006987 instruction,
6988 root,
6989 /*entrypoint*/ temp);
6990 codegen_->AddSlowPath(slow_path);
6991
6992 const int32_t entry_point_offset =
6993 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6994 // Loading the entrypoint does not require a load acquire since it is only changed when
6995 // threads are suspended or running a checkpoint.
6996 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6997 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6998 __ Bind(slow_path->GetExitLabel());
6999 }
Alexey Frunze15958152017-02-09 19:08:30 -08007000 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007001 if (label_low != nullptr) {
7002 reordering = __ SetReorder(false);
7003 __ Bind(label_low);
7004 }
Alexey Frunze15958152017-02-09 19:08:30 -08007005 // GC root loaded through a slow path for read barriers other
7006 // than Baker's.
7007 // /* GcRoot<mirror::Object>* */ root = obj + offset
7008 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007009 if (label_low != nullptr) {
7010 __ SetReorder(reordering);
7011 }
Alexey Frunze15958152017-02-09 19:08:30 -08007012 // /* mirror::Object* */ root = root->Read()
7013 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7014 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007015 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007016 if (label_low != nullptr) {
7017 reordering = __ SetReorder(false);
7018 __ Bind(label_low);
7019 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007020 // Plain GC root load with no read barrier.
7021 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7022 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7023 // Note that GC roots are not affected by heap poisoning, thus we
7024 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007025 if (label_low != nullptr) {
7026 __ SetReorder(reordering);
7027 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007028 }
7029}
7030
Alexey Frunze15958152017-02-09 19:08:30 -08007031void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7032 Location ref,
7033 Register obj,
7034 uint32_t offset,
7035 Location temp,
7036 bool needs_null_check) {
7037 DCHECK(kEmitCompilerReadBarrier);
7038 DCHECK(kUseBakerReadBarrier);
7039
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007040 if (kBakerReadBarrierThunksEnableForFields) {
7041 // Note that we do not actually check the value of `GetIsGcMarking()`
7042 // to decide whether to mark the loaded reference or not. Instead, we
7043 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7044 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7045 // vice versa.
7046 //
7047 // We use thunks for the slow path. That thunk checks the reference
7048 // and jumps to the entrypoint if needed. If the holder is not gray,
7049 // it issues a load-load memory barrier and returns to the original
7050 // reference load.
7051 //
7052 // temp = Thread::Current()->pReadBarrierMarkReg00
7053 // // AKA &art_quick_read_barrier_mark_introspection.
7054 // if (temp != nullptr) {
7055 // temp = &field_array_thunk<holder_reg>
7056 // temp()
7057 // }
7058 // not_gray_return_address:
7059 // // If the offset is too large to fit into the lw instruction, we
7060 // // use an adjusted base register (TMP) here. This register
7061 // // receives bits 16 ... 31 of the offset before the thunk invocation
7062 // // and the thunk benefits from it.
7063 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7064 // gray_return_address:
7065
7066 DCHECK(temp.IsInvalid());
7067 bool isR6 = GetInstructionSetFeatures().IsR6();
7068 int16_t offset_low = Low16Bits(offset);
7069 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7070 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7071 bool reordering = __ SetReorder(false);
7072 const int32_t entry_point_offset =
7073 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7074 // There may have or may have not been a null check if the field offset is smaller than
7075 // the page size.
7076 // There must've been a null check in case it's actually a load from an array.
7077 // We will, however, perform an explicit null check in the thunk as it's easier to
7078 // do it than not.
7079 if (instruction->IsArrayGet()) {
7080 DCHECK(!needs_null_check);
7081 }
7082 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7083 // Loading the entrypoint does not require a load acquire since it is only changed when
7084 // threads are suspended or running a checkpoint.
7085 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7086 Register ref_reg = ref.AsRegister<Register>();
7087 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007088 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007089 if (short_offset) {
7090 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007091 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007092 __ Nop(); // In forbidden slot.
7093 __ Jialc(T9, thunk_disp);
7094 } else {
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);
7098 __ Nop(); // In delay slot.
7099 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007100 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007101 } else {
7102 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007103 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007104 __ Aui(base, obj, offset_high); // In delay slot.
7105 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007106 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007107 } else {
7108 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007109 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007110 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7111 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007112 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007113 __ Addu(base, base, obj); // In delay slot.
7114 }
7115 }
7116 // /* HeapReference<Object> */ ref = *(obj + offset)
7117 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7118 if (needs_null_check) {
7119 MaybeRecordImplicitNullCheck(instruction);
7120 }
7121 __ MaybeUnpoisonHeapReference(ref_reg);
7122 __ SetReorder(reordering);
7123 return;
7124 }
7125
Alexey Frunze15958152017-02-09 19:08:30 -08007126 // /* HeapReference<Object> */ ref = *(obj + offset)
7127 Location no_index = Location::NoLocation();
7128 ScaleFactor no_scale_factor = TIMES_1;
7129 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7130 ref,
7131 obj,
7132 offset,
7133 no_index,
7134 no_scale_factor,
7135 temp,
7136 needs_null_check);
7137}
7138
7139void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7140 Location ref,
7141 Register obj,
7142 uint32_t data_offset,
7143 Location index,
7144 Location temp,
7145 bool needs_null_check) {
7146 DCHECK(kEmitCompilerReadBarrier);
7147 DCHECK(kUseBakerReadBarrier);
7148
7149 static_assert(
7150 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7151 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007152 ScaleFactor scale_factor = TIMES_4;
7153
7154 if (kBakerReadBarrierThunksEnableForArrays) {
7155 // Note that we do not actually check the value of `GetIsGcMarking()`
7156 // to decide whether to mark the loaded reference or not. Instead, we
7157 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7158 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7159 // vice versa.
7160 //
7161 // We use thunks for the slow path. That thunk checks the reference
7162 // and jumps to the entrypoint if needed. If the holder is not gray,
7163 // it issues a load-load memory barrier and returns to the original
7164 // reference load.
7165 //
7166 // temp = Thread::Current()->pReadBarrierMarkReg00
7167 // // AKA &art_quick_read_barrier_mark_introspection.
7168 // if (temp != nullptr) {
7169 // temp = &field_array_thunk<holder_reg>
7170 // temp()
7171 // }
7172 // not_gray_return_address:
7173 // // The element address is pre-calculated in the TMP register before the
7174 // // thunk invocation and the thunk benefits from it.
7175 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7176 // gray_return_address:
7177
7178 DCHECK(temp.IsInvalid());
7179 DCHECK(index.IsValid());
7180 bool reordering = __ SetReorder(false);
7181 const int32_t entry_point_offset =
7182 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7183 // We will not do the explicit null check in the thunk as some form of a null check
7184 // must've been done earlier.
7185 DCHECK(!needs_null_check);
7186 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7187 // Loading the entrypoint does not require a load acquire since it is only changed when
7188 // threads are suspended or running a checkpoint.
7189 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7190 Register ref_reg = ref.AsRegister<Register>();
7191 Register index_reg = index.IsRegisterPair()
7192 ? index.AsRegisterPairLow<Register>()
7193 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007194 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007195 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007196 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007197 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7198 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007199 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007200 } else {
7201 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007202 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007203 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7204 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007205 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007206 __ Addu(TMP, TMP, obj); // In delay slot.
7207 }
7208 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7209 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7210 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7211 __ MaybeUnpoisonHeapReference(ref_reg);
7212 __ SetReorder(reordering);
7213 return;
7214 }
7215
Alexey Frunze15958152017-02-09 19:08:30 -08007216 // /* HeapReference<Object> */ ref =
7217 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007218 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7219 ref,
7220 obj,
7221 data_offset,
7222 index,
7223 scale_factor,
7224 temp,
7225 needs_null_check);
7226}
7227
7228void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7229 Location ref,
7230 Register obj,
7231 uint32_t offset,
7232 Location index,
7233 ScaleFactor scale_factor,
7234 Location temp,
7235 bool needs_null_check,
7236 bool always_update_field) {
7237 DCHECK(kEmitCompilerReadBarrier);
7238 DCHECK(kUseBakerReadBarrier);
7239
7240 // In slow path based read barriers, the read barrier call is
7241 // inserted after the original load. However, in fast path based
7242 // Baker's read barriers, we need to perform the load of
7243 // mirror::Object::monitor_ *before* the original reference load.
7244 // This load-load ordering is required by the read barrier.
7245 // The fast path/slow path (for Baker's algorithm) should look like:
7246 //
7247 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7248 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7249 // HeapReference<Object> ref = *src; // Original reference load.
7250 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7251 // if (is_gray) {
7252 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7253 // }
7254 //
7255 // Note: the original implementation in ReadBarrier::Barrier is
7256 // slightly more complex as it performs additional checks that we do
7257 // not do here for performance reasons.
7258
7259 Register ref_reg = ref.AsRegister<Register>();
7260 Register temp_reg = temp.AsRegister<Register>();
7261 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7262
7263 // /* int32_t */ monitor = obj->monitor_
7264 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7265 if (needs_null_check) {
7266 MaybeRecordImplicitNullCheck(instruction);
7267 }
7268 // /* LockWord */ lock_word = LockWord(monitor)
7269 static_assert(sizeof(LockWord) == sizeof(int32_t),
7270 "art::LockWord and int32_t have different sizes.");
7271
7272 __ Sync(0); // Barrier to prevent load-load reordering.
7273
7274 // The actual reference load.
7275 if (index.IsValid()) {
7276 // Load types involving an "index": ArrayGet,
7277 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7278 // intrinsics.
7279 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7280 if (index.IsConstant()) {
7281 size_t computed_offset =
7282 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7283 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7284 } else {
7285 // Handle the special case of the
7286 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7287 // intrinsics, which use a register pair as index ("long
7288 // offset"), of which only the low part contains data.
7289 Register index_reg = index.IsRegisterPair()
7290 ? index.AsRegisterPairLow<Register>()
7291 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007292 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007293 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7294 }
7295 } else {
7296 // /* HeapReference<Object> */ ref = *(obj + offset)
7297 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7298 }
7299
7300 // Object* ref = ref_addr->AsMirrorPtr()
7301 __ MaybeUnpoisonHeapReference(ref_reg);
7302
7303 // Slow path marking the object `ref` when it is gray.
7304 SlowPathCodeMIPS* slow_path;
7305 if (always_update_field) {
7306 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7307 // of the form `obj + field_offset`, where `obj` is a register and
7308 // `field_offset` is a register pair (of which only the lower half
7309 // is used). Thus `offset` and `scale_factor` above are expected
7310 // to be null in this code path.
7311 DCHECK_EQ(offset, 0u);
7312 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007313 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007314 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7315 ref,
7316 obj,
7317 /* field_offset */ index,
7318 temp_reg);
7319 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007320 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007321 }
7322 AddSlowPath(slow_path);
7323
7324 // if (rb_state == ReadBarrier::GrayState())
7325 // ref = ReadBarrier::Mark(ref);
7326 // Given the numeric representation, it's enough to check the low bit of the
7327 // rb_state. We do that by shifting the bit into the sign bit (31) and
7328 // performing a branch on less than zero.
7329 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7330 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7331 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7332 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7333 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7334 __ Bind(slow_path->GetExitLabel());
7335}
7336
7337void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7338 Location out,
7339 Location ref,
7340 Location obj,
7341 uint32_t offset,
7342 Location index) {
7343 DCHECK(kEmitCompilerReadBarrier);
7344
7345 // Insert a slow path based read barrier *after* the reference load.
7346 //
7347 // If heap poisoning is enabled, the unpoisoning of the loaded
7348 // reference will be carried out by the runtime within the slow
7349 // path.
7350 //
7351 // Note that `ref` currently does not get unpoisoned (when heap
7352 // poisoning is enabled), which is alright as the `ref` argument is
7353 // not used by the artReadBarrierSlow entry point.
7354 //
7355 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007356 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007357 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7358 AddSlowPath(slow_path);
7359
7360 __ B(slow_path->GetEntryLabel());
7361 __ Bind(slow_path->GetExitLabel());
7362}
7363
7364void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7365 Location out,
7366 Location ref,
7367 Location obj,
7368 uint32_t offset,
7369 Location index) {
7370 if (kEmitCompilerReadBarrier) {
7371 // Baker's read barriers shall be handled by the fast path
7372 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7373 DCHECK(!kUseBakerReadBarrier);
7374 // If heap poisoning is enabled, unpoisoning will be taken care of
7375 // by the runtime within the slow path.
7376 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7377 } else if (kPoisonHeapReferences) {
7378 __ UnpoisonHeapReference(out.AsRegister<Register>());
7379 }
7380}
7381
7382void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7383 Location out,
7384 Location root) {
7385 DCHECK(kEmitCompilerReadBarrier);
7386
7387 // Insert a slow path based read barrier *after* the GC root load.
7388 //
7389 // Note that GC roots are not affected by heap poisoning, so we do
7390 // not need to do anything special for this here.
7391 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007392 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007393 AddSlowPath(slow_path);
7394
7395 __ B(slow_path->GetEntryLabel());
7396 __ Bind(slow_path->GetExitLabel());
7397}
7398
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007399void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007400 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7401 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007402 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007403 switch (type_check_kind) {
7404 case TypeCheckKind::kExactCheck:
7405 case TypeCheckKind::kAbstractClassCheck:
7406 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007407 case TypeCheckKind::kArrayObjectCheck: {
7408 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7409 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7410 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007411 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007412 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007413 case TypeCheckKind::kArrayCheck:
7414 case TypeCheckKind::kUnresolvedCheck:
7415 case TypeCheckKind::kInterfaceCheck:
7416 call_kind = LocationSummary::kCallOnSlowPath;
7417 break;
7418 }
7419
Vladimir Markoca6fff82017-10-03 14:49:14 +01007420 LocationSummary* locations =
7421 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007422 if (baker_read_barrier_slow_path) {
7423 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7424 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007425 locations->SetInAt(0, Location::RequiresRegister());
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007426 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007427 // The output does overlap inputs.
7428 // Note that TypeCheckSlowPathMIPS uses this register too.
7429 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007430 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007431}
7432
7433void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007434 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007435 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007436 Location obj_loc = locations->InAt(0);
7437 Register obj = obj_loc.AsRegister<Register>();
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007438 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007439 Location out_loc = locations->Out();
7440 Register out = out_loc.AsRegister<Register>();
7441 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7442 DCHECK_LE(num_temps, 1u);
7443 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007444 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7445 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7446 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7447 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007448 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007449 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007450
7451 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007452 // Avoid this check if we know `obj` is not null.
7453 if (instruction->MustDoNullCheck()) {
7454 __ Move(out, ZERO);
7455 __ Beqz(obj, &done);
7456 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007457
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007458 switch (type_check_kind) {
7459 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007460 ReadBarrierOption read_barrier_option =
7461 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007462 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007463 GenerateReferenceLoadTwoRegisters(instruction,
7464 out_loc,
7465 obj_loc,
7466 class_offset,
7467 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007468 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007469 // Classes must be equal for the instanceof to succeed.
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007470 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007471 __ Sltiu(out, out, 1);
7472 break;
7473 }
7474
7475 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007476 ReadBarrierOption read_barrier_option =
7477 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007478 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007479 GenerateReferenceLoadTwoRegisters(instruction,
7480 out_loc,
7481 obj_loc,
7482 class_offset,
7483 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007484 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007485 // If the class is abstract, we eagerly fetch the super class of the
7486 // object to avoid doing a comparison we know will fail.
7487 MipsLabel loop;
7488 __ Bind(&loop);
7489 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007490 GenerateReferenceLoadOneRegister(instruction,
7491 out_loc,
7492 super_offset,
7493 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007494 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007495 // If `out` is null, we use it for the result, and jump to `done`.
7496 __ Beqz(out, &done);
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007497 __ Bne(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007498 __ LoadConst32(out, 1);
7499 break;
7500 }
7501
7502 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007503 ReadBarrierOption read_barrier_option =
7504 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007505 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007506 GenerateReferenceLoadTwoRegisters(instruction,
7507 out_loc,
7508 obj_loc,
7509 class_offset,
7510 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007511 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007512 // Walk over the class hierarchy to find a match.
7513 MipsLabel loop, success;
7514 __ Bind(&loop);
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007515 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007516 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007517 GenerateReferenceLoadOneRegister(instruction,
7518 out_loc,
7519 super_offset,
7520 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007521 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007522 __ Bnez(out, &loop);
7523 // If `out` is null, we use it for the result, and jump to `done`.
7524 __ B(&done);
7525 __ Bind(&success);
7526 __ LoadConst32(out, 1);
7527 break;
7528 }
7529
7530 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007531 ReadBarrierOption read_barrier_option =
7532 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007533 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007534 GenerateReferenceLoadTwoRegisters(instruction,
7535 out_loc,
7536 obj_loc,
7537 class_offset,
7538 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007539 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007540 // Do an exact check.
7541 MipsLabel success;
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007542 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007543 // Otherwise, we need to check that the object's class is a non-primitive array.
7544 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007545 GenerateReferenceLoadOneRegister(instruction,
7546 out_loc,
7547 component_offset,
7548 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007549 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007550 // If `out` is null, we use it for the result, and jump to `done`.
7551 __ Beqz(out, &done);
7552 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7553 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7554 __ Sltiu(out, out, 1);
7555 __ B(&done);
7556 __ Bind(&success);
7557 __ LoadConst32(out, 1);
7558 break;
7559 }
7560
7561 case TypeCheckKind::kArrayCheck: {
7562 // No read barrier since the slow path will retry upon failure.
7563 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007564 GenerateReferenceLoadTwoRegisters(instruction,
7565 out_loc,
7566 obj_loc,
7567 class_offset,
7568 maybe_temp_loc,
7569 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007570 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007571 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7572 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007573 codegen_->AddSlowPath(slow_path);
Andreas Gampe3fbd3ad2018-03-26 21:14:46 +00007574 __ Bne(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007575 __ LoadConst32(out, 1);
7576 break;
7577 }
7578
7579 case TypeCheckKind::kUnresolvedCheck:
7580 case TypeCheckKind::kInterfaceCheck: {
7581 // Note that we indeed only call on slow path, but we always go
7582 // into the slow path for the unresolved and interface check
7583 // cases.
7584 //
7585 // We cannot directly call the InstanceofNonTrivial runtime
7586 // entry point without resorting to a type checking slow path
7587 // here (i.e. by calling InvokeRuntime directly), as it would
7588 // require to assign fixed registers for the inputs of this
7589 // HInstanceOf instruction (following the runtime calling
7590 // convention), which might be cluttered by the potential first
7591 // read barrier emission at the beginning of this method.
7592 //
7593 // TODO: Introduce a new runtime entry point taking the object
7594 // to test (instead of its class) as argument, and let it deal
7595 // with the read barrier issues. This will let us refactor this
7596 // case of the `switch` code as it was previously (with a direct
7597 // call to the runtime not using a type checking slow path).
7598 // This should also be beneficial for the other cases above.
7599 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007600 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7601 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007602 codegen_->AddSlowPath(slow_path);
7603 __ B(slow_path->GetEntryLabel());
7604 break;
7605 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007606 }
7607
7608 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007609
7610 if (slow_path != nullptr) {
7611 __ Bind(slow_path->GetExitLabel());
7612 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007613}
7614
7615void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007616 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007617 locations->SetOut(Location::ConstantLocation(constant));
7618}
7619
7620void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7621 // Will be generated at use site.
7622}
7623
7624void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007625 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007626 locations->SetOut(Location::ConstantLocation(constant));
7627}
7628
7629void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7630 // Will be generated at use site.
7631}
7632
7633void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7634 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7635 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7636}
7637
7638void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7639 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007640 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007641 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007642 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007643}
7644
7645void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7646 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7647 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007648 Location receiver = invoke->GetLocations()->InAt(0);
7649 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007650 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007651
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007652 // temp = object->GetClass();
7653 if (receiver.IsStackSlot()) {
7654 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7655 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7656 } else {
7657 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7658 }
7659 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007660 // Instead of simply (possibly) unpoisoning `temp` here, we should
7661 // emit a read barrier for the previous class reference load.
7662 // However this is not required in practice, as this is an
7663 // intermediate/temporary reference and because the current
7664 // concurrent copying collector keeps the from-space memory
7665 // intact/accessible until the end of the marking phase (the
7666 // concurrent copying collector may not in the future).
7667 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007668 __ LoadFromOffset(kLoadWord, temp, temp,
7669 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7670 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007671 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007672 // temp = temp->GetImtEntryAt(method_offset);
7673 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7674 // T9 = temp->GetEntryPoint();
7675 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
Lena Djokic3177e102018-02-28 11:32:40 +01007676 // Set the hidden argument.
7677 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7678 invoke->GetDexMethodIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007679 // T9();
7680 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007681 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007682 DCHECK(!codegen_->IsLeafMethod());
7683 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7684}
7685
7686void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007687 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7688 if (intrinsic.TryDispatch(invoke)) {
7689 return;
7690 }
7691
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007692 HandleInvoke(invoke);
7693}
7694
7695void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007696 // Explicit clinit checks triggered by static invokes must have been pruned by
7697 // art::PrepareForRegisterAllocation.
7698 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007699
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007700 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007701 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7702 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007703
Chris Larsen701566a2015-10-27 15:29:13 -07007704 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7705 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007706 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7707 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7708 }
Chris Larsen701566a2015-10-27 15:29:13 -07007709 return;
7710 }
7711
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007712 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007713
7714 // Add the extra input register if either the dex cache array base register
7715 // or the PC-relative base register for accessing literals is needed.
7716 if (has_extra_input) {
7717 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7718 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007719}
7720
Orion Hodsonac141392017-01-13 11:53:47 +00007721void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7722 HandleInvoke(invoke);
7723}
7724
7725void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7726 codegen_->GenerateInvokePolymorphicCall(invoke);
7727}
7728
Chris Larsen701566a2015-10-27 15:29:13 -07007729static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007730 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007731 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7732 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007733 return true;
7734 }
7735 return false;
7736}
7737
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007738HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007739 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007740 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007741 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007742 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007743 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007744 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007745 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007746 case HLoadString::LoadKind::kJitTableAddress:
7747 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007748 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007749 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007750 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007751 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007752 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007753 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007754}
7755
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007756HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7757 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007758 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007759 case HLoadClass::LoadKind::kInvalid:
7760 LOG(FATAL) << "UNREACHABLE";
7761 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007762 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007763 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007764 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007765 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007766 case HLoadClass::LoadKind::kBssEntry:
7767 DCHECK(!Runtime::Current()->UseJitCompilation());
7768 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007769 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007770 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007771 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007772 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007773 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007774 break;
7775 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007776 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007777}
7778
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007779Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7780 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007781 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007782 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007783 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7784 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7785 if (!invoke->GetLocations()->Intrinsified()) {
7786 return location.AsRegister<Register>();
7787 }
7788 // For intrinsics we allow any location, so it may be on the stack.
7789 if (!location.IsRegister()) {
7790 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7791 return temp;
7792 }
7793 // For register locations, check if the register was saved. If so, get it from the stack.
7794 // Note: There is a chance that the register was saved but not overwritten, so we could
7795 // save one load. However, since this is just an intrinsic slow path we prefer this
7796 // simple and more robust approach rather that trying to determine if that's the case.
7797 SlowPathCode* slow_path = GetCurrentSlowPath();
7798 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7799 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7800 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7801 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7802 return temp;
7803 }
7804 return location.AsRegister<Register>();
7805}
7806
Vladimir Markodc151b22015-10-15 18:02:30 +01007807HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7808 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007809 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007810 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007811}
7812
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007813void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7814 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007815 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007816 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007817 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7818 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007819 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007820 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7821 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007822 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7823 : ZERO;
7824
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007825 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007826 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007827 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007828 uint32_t offset =
7829 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007830 __ LoadFromOffset(kLoadWord,
7831 temp.AsRegister<Register>(),
7832 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007833 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007834 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007835 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007836 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007837 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007838 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007839 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7840 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007841 PcRelativePatchInfo* info_high = NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007842 PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007843 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007844 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007845 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7846 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007847 break;
7848 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007849 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7850 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7851 break;
Vladimir Markob066d432018-01-03 13:14:37 +00007852 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007853 uint32_t boot_image_offset = GetBootImageOffset(invoke);
Vladimir Markob066d432018-01-03 13:14:37 +00007854 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
7855 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
7856 Register temp_reg = temp.AsRegister<Register>();
7857 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7858 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
7859 break;
7860 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007861 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007862 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007863 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007864 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7865 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007866 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007867 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7868 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007869 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007870 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007871 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7872 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7873 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007874 }
7875 }
7876
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007877 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007878 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007879 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007880 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007881 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7882 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007883 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007884 T9,
7885 callee_method.AsRegister<Register>(),
7886 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007887 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007888 // T9()
7889 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007890 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007891 break;
7892 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007893 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7894
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007895 DCHECK(!IsLeafMethod());
7896}
7897
7898void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007899 // Explicit clinit checks triggered by static invokes must have been pruned by
7900 // art::PrepareForRegisterAllocation.
7901 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007902
7903 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7904 return;
7905 }
7906
7907 LocationSummary* locations = invoke->GetLocations();
7908 codegen_->GenerateStaticOrDirectCall(invoke,
7909 locations->HasTemps()
7910 ? locations->GetTemp(0)
7911 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007912}
7913
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007914void CodeGeneratorMIPS::GenerateVirtualCall(
7915 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007916 // Use the calling convention instead of the location of the receiver, as
7917 // intrinsics may have put the receiver in a different register. In the intrinsics
7918 // slow path, the arguments have been moved to the right place, so here we are
7919 // guaranteed that the receiver is the first register of the calling convention.
7920 InvokeDexCallingConvention calling_convention;
7921 Register receiver = calling_convention.GetRegisterAt(0);
7922
Chris Larsen3acee732015-11-18 13:31:08 -08007923 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007924 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7925 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7926 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007927 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007928
7929 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007930 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007931 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007932 // Instead of simply (possibly) unpoisoning `temp` here, we should
7933 // emit a read barrier for the previous class reference load.
7934 // However this is not required in practice, as this is an
7935 // intermediate/temporary reference and because the current
7936 // concurrent copying collector keeps the from-space memory
7937 // intact/accessible until the end of the marking phase (the
7938 // concurrent copying collector may not in the future).
7939 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007940 // temp = temp->GetMethodAt(method_offset);
7941 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7942 // T9 = temp->GetEntryPoint();
7943 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7944 // T9();
7945 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007946 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007947 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007948}
7949
7950void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7951 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7952 return;
7953 }
7954
7955 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007956 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007957}
7958
7959void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007960 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007961 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007962 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007963 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7964 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007965 return;
7966 }
Vladimir Marko41559982017-01-06 14:04:23 +00007967 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007968 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007969 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007970 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7971 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007972 ? LocationSummary::kCallOnSlowPath
7973 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007974 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007975 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7976 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7977 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007978 switch (load_kind) {
7979 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007980 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007981 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00007982 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007983 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007984 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007985 break;
7986 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007987 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007988 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7989 codegen_->ClobberRA();
7990 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007991 break;
7992 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007993 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007994 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007995 locations->SetInAt(0, Location::RequiresRegister());
7996 break;
7997 default:
7998 break;
7999 }
8000 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008001 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
8002 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8003 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008004 RegisterSet caller_saves = RegisterSet::Empty();
8005 InvokeRuntimeCallingConvention calling_convention;
8006 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8007 locations->SetCustomSlowPathCallerSaves(caller_saves);
8008 } else {
8009 // For non-Baker read barriers we have a temp-clobbering call.
8010 }
8011 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008012}
8013
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008014// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8015// move.
8016void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00008017 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008018 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00008019 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01008020 return;
8021 }
Vladimir Marko41559982017-01-06 14:04:23 +00008022 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008023
Vladimir Marko41559982017-01-06 14:04:23 +00008024 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008025 Location out_loc = locations->Out();
8026 Register out = out_loc.AsRegister<Register>();
8027 Register base_or_current_method_reg;
8028 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008029 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008030 switch (load_kind) {
8031 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008032 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008033 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008034 case HLoadClass::LoadKind::kBootImageRelRo:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008035 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008036 base_or_current_method_reg =
8037 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008038 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008039 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008040 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008041 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8042 break;
8043 default:
8044 base_or_current_method_reg = ZERO;
8045 break;
8046 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008047
Alexey Frunze15958152017-02-09 19:08:30 -08008048 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8049 ? kWithoutReadBarrier
8050 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008051 bool generate_null_check = false;
8052 switch (load_kind) {
8053 case HLoadClass::LoadKind::kReferrersClass: {
8054 DCHECK(!cls->CanCallRuntime());
8055 DCHECK(!cls->MustGenerateClinitCheck());
8056 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8057 GenerateGcRootFieldLoad(cls,
8058 out_loc,
8059 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008060 ArtMethod::DeclaringClassOffset().Int32Value(),
8061 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008062 break;
8063 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008064 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008065 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008066 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008067 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008068 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008069 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008070 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008071 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8072 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008073 base_or_current_method_reg);
8074 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008075 break;
8076 }
8077 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08008078 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008079 uint32_t address = dchecked_integral_cast<uint32_t>(
8080 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
8081 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008082 if (isR6 || !has_irreducible_loops) {
8083 __ LoadLiteral(out,
8084 base_or_current_method_reg,
8085 codegen_->DeduplicateBootImageAddressLiteral(address));
8086 } else {
8087 __ LoadConst32(out, address);
8088 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008089 break;
8090 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008091 case HLoadClass::LoadKind::kBootImageRelRo: {
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008092 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008093 uint32_t boot_image_offset = codegen_->GetBootImageOffset(cls);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008094 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008095 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008096 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008097 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008098 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8099 out,
8100 base_or_current_method_reg);
8101 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008102 break;
8103 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008104 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008105 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8106 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008107 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8108 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008109 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008110 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008111 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008112 GenerateGcRootFieldLoad(cls,
8113 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008114 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008115 /* placeholder */ 0x5678,
8116 read_barrier_option,
8117 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008118 generate_null_check = true;
8119 break;
8120 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008121 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008122 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8123 cls->GetTypeIndex(),
8124 cls->GetClass());
8125 bool reordering = __ SetReorder(false);
8126 __ Bind(&info->high_label);
8127 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008128 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008129 GenerateGcRootFieldLoad(cls,
8130 out_loc,
8131 out,
8132 /* placeholder */ 0x5678,
8133 read_barrier_option,
8134 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008135 break;
8136 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008137 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008138 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008139 LOG(FATAL) << "UNREACHABLE";
8140 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008141 }
8142
8143 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8144 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008145 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00008146 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07008147 codegen_->AddSlowPath(slow_path);
8148 if (generate_null_check) {
8149 __ Beqz(out, slow_path->GetEntryLabel());
8150 }
8151 if (cls->MustGenerateClinitCheck()) {
8152 GenerateClassInitializationCheck(slow_path, out);
8153 } else {
8154 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008155 }
8156 }
8157}
8158
8159static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008160 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008161}
8162
8163void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8164 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008165 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008166 locations->SetOut(Location::RequiresRegister());
8167}
8168
8169void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8170 Register out = load->GetLocations()->Out().AsRegister<Register>();
8171 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8172}
8173
8174void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008175 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008176}
8177
8178void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8179 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8180}
8181
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008182void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008183 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008184 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008185 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008186 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008187 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008188 switch (load_kind) {
8189 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008190 case HLoadString::LoadKind::kBootImageAddress:
8191 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008192 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008193 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008194 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008195 break;
8196 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008197 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008198 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
8199 codegen_->ClobberRA();
8200 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008201 break;
8202 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008203 FALLTHROUGH_INTENDED;
8204 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008205 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008206 locations->SetInAt(0, Location::RequiresRegister());
8207 break;
8208 default:
8209 break;
8210 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008211 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008212 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008213 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008214 } else {
8215 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008216 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8217 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8218 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008219 RegisterSet caller_saves = RegisterSet::Empty();
8220 InvokeRuntimeCallingConvention calling_convention;
8221 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8222 locations->SetCustomSlowPathCallerSaves(caller_saves);
8223 } else {
8224 // For non-Baker read barriers we have a temp-clobbering call.
8225 }
8226 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008227 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008228}
8229
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008230// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8231// move.
8232void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008233 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008234 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008235 Location out_loc = locations->Out();
8236 Register out = out_loc.AsRegister<Register>();
8237 Register base_or_current_method_reg;
8238 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008239 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008240 switch (load_kind) {
8241 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008242 case HLoadString::LoadKind::kBootImageAddress:
8243 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008244 case HLoadString::LoadKind::kBootImageRelRo:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008245 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008246 base_or_current_method_reg =
8247 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008248 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008249 default:
8250 base_or_current_method_reg = ZERO;
8251 break;
8252 }
8253
8254 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008255 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008256 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008257 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008258 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008259 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008260 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008261 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8262 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008263 base_or_current_method_reg);
8264 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008265 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008266 }
8267 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008268 uint32_t address = dchecked_integral_cast<uint32_t>(
8269 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8270 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008271 if (isR6 || !has_irreducible_loops) {
8272 __ LoadLiteral(out,
8273 base_or_current_method_reg,
8274 codegen_->DeduplicateBootImageAddressLiteral(address));
8275 } else {
8276 __ LoadConst32(out, address);
8277 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008278 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008279 }
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008280 case HLoadString::LoadKind::kBootImageRelRo: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008281 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008282 uint32_t boot_image_offset = codegen_->GetBootImageOffset(load);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008283 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008284 codegen_->NewBootImageRelRoPatch(boot_image_offset);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008285 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Markoe47f60c2018-02-21 13:43:28 +00008286 codegen_->NewBootImageRelRoPatch(boot_image_offset, info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008287 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8288 out,
8289 base_or_current_method_reg);
8290 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8291 return;
8292 }
8293 case HLoadString::LoadKind::kBssEntry: {
8294 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8295 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8296 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8297 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8298 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008299 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008300 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008301 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008302 GenerateGcRootFieldLoad(load,
8303 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008304 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008305 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008306 kCompilerReadBarrierOption,
8307 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008308 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008309 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008310 codegen_->AddSlowPath(slow_path);
8311 __ Beqz(out, slow_path->GetEntryLabel());
8312 __ Bind(slow_path->GetExitLabel());
8313 return;
8314 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008315 case HLoadString::LoadKind::kJitTableAddress: {
8316 CodeGeneratorMIPS::JitPatchInfo* info =
8317 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8318 load->GetStringIndex(),
8319 load->GetString());
8320 bool reordering = __ SetReorder(false);
8321 __ Bind(&info->high_label);
8322 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008323 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008324 GenerateGcRootFieldLoad(load,
8325 out_loc,
8326 out,
8327 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008328 kCompilerReadBarrierOption,
8329 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008330 return;
8331 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008332 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008333 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008334 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008335
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008336 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008337 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008338 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008339 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008340 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008341 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8342 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008343}
8344
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008345void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008346 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008347 locations->SetOut(Location::ConstantLocation(constant));
8348}
8349
8350void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8351 // Will be generated at use site.
8352}
8353
8354void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008355 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8356 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008357 InvokeRuntimeCallingConvention calling_convention;
8358 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8359}
8360
8361void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8362 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008363 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008364 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8365 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008366 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008367 }
8368 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8369}
8370
8371void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8372 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008373 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008374 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008375 case DataType::Type::kInt32:
8376 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008377 locations->SetInAt(0, Location::RequiresRegister());
8378 locations->SetInAt(1, Location::RequiresRegister());
8379 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8380 break;
8381
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008382 case DataType::Type::kFloat32:
8383 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008384 locations->SetInAt(0, Location::RequiresFpuRegister());
8385 locations->SetInAt(1, Location::RequiresFpuRegister());
8386 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8387 break;
8388
8389 default:
8390 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8391 }
8392}
8393
8394void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008395 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008396 LocationSummary* locations = instruction->GetLocations();
8397 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8398
8399 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008400 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008401 Register dst = locations->Out().AsRegister<Register>();
8402 Register lhs = locations->InAt(0).AsRegister<Register>();
8403 Register rhs = locations->InAt(1).AsRegister<Register>();
8404
8405 if (isR6) {
8406 __ MulR6(dst, lhs, rhs);
8407 } else {
8408 __ MulR2(dst, lhs, rhs);
8409 }
8410 break;
8411 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008412 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008413 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8414 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8415 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8416 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8417 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8418 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8419
8420 // Extra checks to protect caused by the existance of A1_A2.
8421 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8422 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8423 DCHECK_NE(dst_high, lhs_low);
8424 DCHECK_NE(dst_high, rhs_low);
8425
8426 // A_B * C_D
8427 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8428 // dst_lo: [ low(B*D) ]
8429 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8430
8431 if (isR6) {
8432 __ MulR6(TMP, lhs_high, rhs_low);
8433 __ MulR6(dst_high, lhs_low, rhs_high);
8434 __ Addu(dst_high, dst_high, TMP);
8435 __ MuhuR6(TMP, lhs_low, rhs_low);
8436 __ Addu(dst_high, dst_high, TMP);
8437 __ MulR6(dst_low, lhs_low, rhs_low);
8438 } else {
8439 __ MulR2(TMP, lhs_high, rhs_low);
8440 __ MulR2(dst_high, lhs_low, rhs_high);
8441 __ Addu(dst_high, dst_high, TMP);
8442 __ MultuR2(lhs_low, rhs_low);
8443 __ Mfhi(TMP);
8444 __ Addu(dst_high, dst_high, TMP);
8445 __ Mflo(dst_low);
8446 }
8447 break;
8448 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008449 case DataType::Type::kFloat32:
8450 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008451 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8452 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8453 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008454 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008455 __ MulS(dst, lhs, rhs);
8456 } else {
8457 __ MulD(dst, lhs, rhs);
8458 }
8459 break;
8460 }
8461 default:
8462 LOG(FATAL) << "Unexpected mul type " << type;
8463 }
8464}
8465
8466void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8467 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008468 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008469 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008470 case DataType::Type::kInt32:
8471 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008472 locations->SetInAt(0, Location::RequiresRegister());
8473 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8474 break;
8475
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008476 case DataType::Type::kFloat32:
8477 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008478 locations->SetInAt(0, Location::RequiresFpuRegister());
8479 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8480 break;
8481
8482 default:
8483 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8484 }
8485}
8486
8487void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008488 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008489 LocationSummary* locations = instruction->GetLocations();
8490
8491 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008492 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008493 Register dst = locations->Out().AsRegister<Register>();
8494 Register src = locations->InAt(0).AsRegister<Register>();
8495 __ Subu(dst, ZERO, src);
8496 break;
8497 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008498 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008499 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8500 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8501 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8502 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8503 __ Subu(dst_low, ZERO, src_low);
8504 __ Sltu(TMP, ZERO, dst_low);
8505 __ Subu(dst_high, ZERO, src_high);
8506 __ Subu(dst_high, dst_high, TMP);
8507 break;
8508 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008509 case DataType::Type::kFloat32:
8510 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008511 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8512 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008513 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008514 __ NegS(dst, src);
8515 } else {
8516 __ NegD(dst, src);
8517 }
8518 break;
8519 }
8520 default:
8521 LOG(FATAL) << "Unexpected neg type " << type;
8522 }
8523}
8524
8525void LocationsBuilderMIPS::VisitNewArray(HNewArray* 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;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008529 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008530 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8531 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008532}
8533
8534void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008535 // Note: if heap poisoning is enabled, the entry point takes care
8536 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008537 QuickEntrypointEnum entrypoint =
8538 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8539 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008540 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008541 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008542}
8543
8544void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008545 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8546 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008547 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008548 if (instruction->IsStringAlloc()) {
8549 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8550 } else {
8551 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008552 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008553 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008554}
8555
8556void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008557 // Note: if heap poisoning is enabled, the entry point takes care
8558 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008559 if (instruction->IsStringAlloc()) {
8560 // String is allocated through StringFactory. Call NewEmptyString entry point.
8561 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008562 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008563 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8564 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8565 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008566 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008567 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8568 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008569 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008570 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008571 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008572}
8573
8574void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008575 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008576 locations->SetInAt(0, Location::RequiresRegister());
8577 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8578}
8579
8580void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008581 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008582 LocationSummary* locations = instruction->GetLocations();
8583
8584 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008585 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008586 Register dst = locations->Out().AsRegister<Register>();
8587 Register src = locations->InAt(0).AsRegister<Register>();
8588 __ Nor(dst, src, ZERO);
8589 break;
8590 }
8591
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008592 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008593 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8594 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8595 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8596 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8597 __ Nor(dst_high, src_high, ZERO);
8598 __ Nor(dst_low, src_low, ZERO);
8599 break;
8600 }
8601
8602 default:
8603 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8604 }
8605}
8606
8607void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008608 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008609 locations->SetInAt(0, Location::RequiresRegister());
8610 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8611}
8612
8613void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8614 LocationSummary* locations = instruction->GetLocations();
8615 __ Xori(locations->Out().AsRegister<Register>(),
8616 locations->InAt(0).AsRegister<Register>(),
8617 1);
8618}
8619
8620void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008621 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8622 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008623}
8624
Calin Juravle2ae48182016-03-16 14:05:09 +00008625void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8626 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008627 return;
8628 }
8629 Location obj = instruction->GetLocations()->InAt(0);
8630
8631 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008632 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008633}
8634
Calin Juravle2ae48182016-03-16 14:05:09 +00008635void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008636 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008637 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008638
8639 Location obj = instruction->GetLocations()->InAt(0);
8640
8641 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8642}
8643
8644void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008645 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008646}
8647
8648void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8649 HandleBinaryOp(instruction);
8650}
8651
8652void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8653 HandleBinaryOp(instruction);
8654}
8655
8656void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8657 LOG(FATAL) << "Unreachable";
8658}
8659
8660void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008661 if (instruction->GetNext()->IsSuspendCheck() &&
8662 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8663 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8664 // The back edge will generate the suspend check.
8665 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8666 }
8667
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008668 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8669}
8670
8671void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008672 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008673 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8674 if (location.IsStackSlot()) {
8675 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8676 } else if (location.IsDoubleStackSlot()) {
8677 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8678 }
8679 locations->SetOut(location);
8680}
8681
8682void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8683 ATTRIBUTE_UNUSED) {
8684 // Nothing to do, the parameter is already at its location.
8685}
8686
8687void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8688 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008689 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008690 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8691}
8692
8693void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8694 ATTRIBUTE_UNUSED) {
8695 // Nothing to do, the method is already at its location.
8696}
8697
8698void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008699 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008700 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008701 locations->SetInAt(i, Location::Any());
8702 }
8703 locations->SetOut(Location::Any());
8704}
8705
8706void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8707 LOG(FATAL) << "Unreachable";
8708}
8709
8710void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008711 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008712 bool call_rem;
8713 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8714 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8715 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8716 } else {
8717 call_rem = (type != DataType::Type::kInt32);
8718 }
8719 LocationSummary::CallKind call_kind = call_rem
8720 ? LocationSummary::kCallOnMainOnly
8721 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008722 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008723
8724 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008725 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008726 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008727 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008728 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8729 break;
8730
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008731 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008732 if (call_rem) {
8733 InvokeRuntimeCallingConvention calling_convention;
8734 locations->SetInAt(0, Location::RegisterPairLocation(
8735 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8736 locations->SetInAt(1, Location::RegisterPairLocation(
8737 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8738 locations->SetOut(calling_convention.GetReturnLocation(type));
8739 } else {
8740 locations->SetInAt(0, Location::RequiresRegister());
8741 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8742 locations->SetOut(Location::RequiresRegister());
8743 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008744 break;
8745 }
8746
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008747 case DataType::Type::kFloat32:
8748 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008749 InvokeRuntimeCallingConvention calling_convention;
8750 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8751 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8752 locations->SetOut(calling_convention.GetReturnLocation(type));
8753 break;
8754 }
8755
8756 default:
8757 LOG(FATAL) << "Unexpected rem type " << type;
8758 }
8759}
8760
8761void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008762 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008763 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008764
8765 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008766 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008767 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008768 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008769 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008770 if (locations->InAt(1).IsConstant()) {
8771 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8772 if (imm == 0) {
8773 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8774 } else if (imm == 1 || imm == -1) {
8775 DivRemOneOrMinusOne(instruction);
8776 } else {
8777 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8778 DivRemByPowerOfTwo(instruction);
8779 }
8780 } else {
8781 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8782 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8783 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008784 break;
8785 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008786 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008787 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008788 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008789 break;
8790 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008791 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008792 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008793 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008794 break;
8795 }
8796 default:
8797 LOG(FATAL) << "Unexpected rem type " << type;
8798 }
8799}
8800
Aart Bik1f8d51b2018-02-15 10:42:37 -08008801static void CreateMinMaxLocations(ArenaAllocator* allocator, HBinaryOperation* minmax) {
8802 LocationSummary* locations = new (allocator) LocationSummary(minmax);
8803 switch (minmax->GetResultType()) {
8804 case DataType::Type::kInt32:
8805 case DataType::Type::kInt64:
8806 locations->SetInAt(0, Location::RequiresRegister());
8807 locations->SetInAt(1, Location::RequiresRegister());
8808 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8809 break;
8810 case DataType::Type::kFloat32:
8811 case DataType::Type::kFloat64:
8812 locations->SetInAt(0, Location::RequiresFpuRegister());
8813 locations->SetInAt(1, Location::RequiresFpuRegister());
8814 locations->SetOut(Location::RequiresFpuRegister(), Location::kOutputOverlap);
8815 break;
8816 default:
8817 LOG(FATAL) << "Unexpected type for HMinMax " << minmax->GetResultType();
8818 }
8819}
8820
Aart Bik351df3e2018-03-07 11:54:57 -08008821void InstructionCodeGeneratorMIPS::GenerateMinMaxInt(LocationSummary* locations,
8822 bool is_min,
8823 bool isR6,
8824 DataType::Type type) {
Aart Bik1f8d51b2018-02-15 10:42:37 -08008825 if (isR6) {
8826 // Some architectures, such as ARM and MIPS (prior to r6), have a
8827 // conditional move instruction which only changes the target
8828 // (output) register if the condition is true (MIPS prior to r6 had
8829 // MOVF, MOVT, MOVN, and MOVZ). The SELEQZ and SELNEZ instructions
8830 // always change the target (output) register. If the condition is
8831 // true the output register gets the contents of the "rs" register;
8832 // otherwise, the output register is set to zero. One consequence
8833 // of this is that to implement something like "rd = c==0 ? rs : rt"
8834 // MIPS64r6 needs to use a pair of SELEQZ/SELNEZ instructions.
8835 // After executing this pair of instructions one of the output
8836 // registers from the pair will necessarily contain zero. Then the
8837 // code ORs the output registers from the SELEQZ/SELNEZ instructions
8838 // to get the final result.
8839 //
8840 // The initial test to see if the output register is same as the
8841 // first input register is needed to make sure that value in the
8842 // first input register isn't clobbered before we've finished
8843 // computing the output value. The logic in the corresponding else
8844 // clause performs the same task but makes sure the second input
8845 // register isn't clobbered in the event that it's the same register
8846 // as the output register; the else clause also handles the case
8847 // where the output register is distinct from both the first, and the
8848 // second input registers.
8849 if (type == DataType::Type::kInt64) {
8850 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8851 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8852 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
8853 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
8854 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8855 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8856
8857 MipsLabel compare_done;
8858
8859 if (a_lo == b_lo) {
8860 if (out_lo != a_lo) {
8861 __ Move(out_lo, a_lo);
8862 __ Move(out_hi, a_hi);
8863 }
8864 } else {
8865 __ Slt(TMP, b_hi, a_hi);
8866 __ Bne(b_hi, a_hi, &compare_done);
8867
8868 __ Sltu(TMP, b_lo, a_lo);
8869
8870 __ Bind(&compare_done);
8871
8872 if (is_min) {
8873 __ Seleqz(AT, a_lo, TMP);
8874 __ Selnez(out_lo, b_lo, TMP); // Safe even if out_lo == a_lo/b_lo
8875 // because at this point we're
8876 // done using a_lo/b_lo.
8877 } else {
8878 __ Selnez(AT, a_lo, TMP);
8879 __ Seleqz(out_lo, b_lo, TMP); // ditto
8880 }
8881 __ Or(out_lo, out_lo, AT);
8882 if (is_min) {
8883 __ Seleqz(AT, a_hi, TMP);
8884 __ Selnez(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
8885 } else {
8886 __ Selnez(AT, a_hi, TMP);
8887 __ Seleqz(out_hi, b_hi, TMP); // ditto but for out_hi & a_hi/b_hi
8888 }
8889 __ Or(out_hi, out_hi, AT);
8890 }
8891 } else {
8892 DCHECK_EQ(type, DataType::Type::kInt32);
8893 Register a = locations->InAt(0).AsRegister<Register>();
8894 Register b = locations->InAt(1).AsRegister<Register>();
8895 Register out = locations->Out().AsRegister<Register>();
8896
8897 if (a == b) {
8898 if (out != a) {
8899 __ Move(out, a);
8900 }
8901 } else {
8902 __ Slt(AT, b, a);
8903 if (is_min) {
8904 __ Seleqz(TMP, a, AT);
8905 __ Selnez(AT, b, AT);
8906 } else {
8907 __ Selnez(TMP, a, AT);
8908 __ Seleqz(AT, b, AT);
8909 }
8910 __ Or(out, TMP, AT);
8911 }
8912 }
8913 } else { // !isR6
8914 if (type == DataType::Type::kInt64) {
8915 Register a_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8916 Register a_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8917 Register b_lo = locations->InAt(1).AsRegisterPairLow<Register>();
8918 Register b_hi = locations->InAt(1).AsRegisterPairHigh<Register>();
8919 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8920 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8921
8922 MipsLabel compare_done;
8923
8924 if (a_lo == b_lo) {
8925 if (out_lo != a_lo) {
8926 __ Move(out_lo, a_lo);
8927 __ Move(out_hi, a_hi);
8928 }
8929 } else {
8930 __ Slt(TMP, a_hi, b_hi);
8931 __ Bne(a_hi, b_hi, &compare_done);
8932
8933 __ Sltu(TMP, a_lo, b_lo);
8934
8935 __ Bind(&compare_done);
8936
8937 if (is_min) {
8938 if (out_lo != a_lo) {
8939 __ Movn(out_hi, a_hi, TMP);
8940 __ Movn(out_lo, a_lo, TMP);
8941 }
8942 if (out_lo != b_lo) {
8943 __ Movz(out_hi, b_hi, TMP);
8944 __ Movz(out_lo, b_lo, TMP);
8945 }
8946 } else {
8947 if (out_lo != a_lo) {
8948 __ Movz(out_hi, a_hi, TMP);
8949 __ Movz(out_lo, a_lo, TMP);
8950 }
8951 if (out_lo != b_lo) {
8952 __ Movn(out_hi, b_hi, TMP);
8953 __ Movn(out_lo, b_lo, TMP);
8954 }
8955 }
8956 }
8957 } else {
8958 DCHECK_EQ(type, DataType::Type::kInt32);
8959 Register a = locations->InAt(0).AsRegister<Register>();
8960 Register b = locations->InAt(1).AsRegister<Register>();
8961 Register out = locations->Out().AsRegister<Register>();
8962
8963 if (a == b) {
8964 if (out != a) {
8965 __ Move(out, a);
8966 }
8967 } else {
8968 __ Slt(AT, a, b);
8969 if (is_min) {
8970 if (out != a) {
8971 __ Movn(out, a, AT);
8972 }
8973 if (out != b) {
8974 __ Movz(out, b, AT);
8975 }
8976 } else {
8977 if (out != a) {
8978 __ Movz(out, a, AT);
8979 }
8980 if (out != b) {
8981 __ Movn(out, b, AT);
8982 }
8983 }
8984 }
8985 }
8986 }
8987}
8988
8989void InstructionCodeGeneratorMIPS::GenerateMinMaxFP(LocationSummary* locations,
8990 bool is_min,
8991 bool isR6,
8992 DataType::Type type) {
8993 FRegister out = locations->Out().AsFpuRegister<FRegister>();
8994 FRegister a = locations->InAt(0).AsFpuRegister<FRegister>();
8995 FRegister b = locations->InAt(1).AsFpuRegister<FRegister>();
8996
8997 if (isR6) {
8998 MipsLabel noNaNs;
8999 MipsLabel done;
9000 FRegister ftmp = ((out != a) && (out != b)) ? out : FTMP;
9001
9002 // When Java computes min/max it prefers a NaN to a number; the
9003 // behavior of MIPSR6 is to prefer numbers to NaNs, i.e., if one of
9004 // the inputs is a NaN and the other is a valid number, the MIPS
9005 // instruction will return the number; Java wants the NaN value
9006 // returned. This is why there is extra logic preceding the use of
9007 // the MIPS min.fmt/max.fmt instructions. If either a, or b holds a
9008 // NaN, return the NaN, otherwise return the min/max.
9009 if (type == DataType::Type::kFloat64) {
9010 __ CmpUnD(FTMP, a, b);
9011 __ Bc1eqz(FTMP, &noNaNs);
9012
9013 // One of the inputs is a NaN
9014 __ CmpEqD(ftmp, a, a);
9015 // If a == a then b is the NaN, otherwise a is the NaN.
9016 __ SelD(ftmp, a, b);
9017
9018 if (ftmp != out) {
9019 __ MovD(out, ftmp);
9020 }
9021
9022 __ B(&done);
9023
9024 __ Bind(&noNaNs);
9025
9026 if (is_min) {
9027 __ MinD(out, a, b);
9028 } else {
9029 __ MaxD(out, a, b);
9030 }
9031 } else {
9032 DCHECK_EQ(type, DataType::Type::kFloat32);
9033 __ CmpUnS(FTMP, a, b);
9034 __ Bc1eqz(FTMP, &noNaNs);
9035
9036 // One of the inputs is a NaN
9037 __ CmpEqS(ftmp, a, a);
9038 // If a == a then b is the NaN, otherwise a is the NaN.
9039 __ SelS(ftmp, a, b);
9040
9041 if (ftmp != out) {
9042 __ MovS(out, ftmp);
9043 }
9044
9045 __ B(&done);
9046
9047 __ Bind(&noNaNs);
9048
9049 if (is_min) {
9050 __ MinS(out, a, b);
9051 } else {
9052 __ MaxS(out, a, b);
9053 }
9054 }
9055
9056 __ Bind(&done);
9057
9058 } else { // !isR6
9059 MipsLabel ordered;
9060 MipsLabel compare;
9061 MipsLabel select;
9062 MipsLabel done;
9063
9064 if (type == DataType::Type::kFloat64) {
9065 __ CunD(a, b);
9066 } else {
9067 DCHECK_EQ(type, DataType::Type::kFloat32);
9068 __ CunS(a, b);
9069 }
9070 __ Bc1f(&ordered);
9071
9072 // a or b (or both) is a NaN. Return one, which is a NaN.
9073 if (type == DataType::Type::kFloat64) {
9074 __ CeqD(b, b);
9075 } else {
9076 __ CeqS(b, b);
9077 }
9078 __ B(&select);
9079
9080 __ Bind(&ordered);
9081
9082 // Neither is a NaN.
9083 // a == b? (-0.0 compares equal with +0.0)
9084 // If equal, handle zeroes, else compare further.
9085 if (type == DataType::Type::kFloat64) {
9086 __ CeqD(a, b);
9087 } else {
9088 __ CeqS(a, b);
9089 }
9090 __ Bc1f(&compare);
9091
9092 // a == b either bit for bit or one is -0.0 and the other is +0.0.
9093 if (type == DataType::Type::kFloat64) {
9094 __ MoveFromFpuHigh(TMP, a);
9095 __ MoveFromFpuHigh(AT, b);
9096 } else {
9097 __ Mfc1(TMP, a);
9098 __ Mfc1(AT, b);
9099 }
9100
9101 if (is_min) {
9102 // -0.0 prevails over +0.0.
9103 __ Or(TMP, TMP, AT);
9104 } else {
9105 // +0.0 prevails over -0.0.
9106 __ And(TMP, TMP, AT);
9107 }
9108
9109 if (type == DataType::Type::kFloat64) {
9110 __ Mfc1(AT, a);
9111 __ Mtc1(AT, out);
9112 __ MoveToFpuHigh(TMP, out);
9113 } else {
9114 __ Mtc1(TMP, out);
9115 }
9116 __ B(&done);
9117
9118 __ Bind(&compare);
9119
9120 if (type == DataType::Type::kFloat64) {
9121 if (is_min) {
9122 // return (a <= b) ? a : b;
9123 __ ColeD(a, b);
9124 } else {
9125 // return (a >= b) ? a : b;
9126 __ ColeD(b, a); // b <= a
9127 }
9128 } else {
9129 if (is_min) {
9130 // return (a <= b) ? a : b;
9131 __ ColeS(a, b);
9132 } else {
9133 // return (a >= b) ? a : b;
9134 __ ColeS(b, a); // b <= a
9135 }
9136 }
9137
9138 __ Bind(&select);
9139
9140 if (type == DataType::Type::kFloat64) {
9141 __ MovtD(out, a);
9142 __ MovfD(out, b);
9143 } else {
9144 __ MovtS(out, a);
9145 __ MovfS(out, b);
9146 }
9147
9148 __ Bind(&done);
9149 }
9150}
9151
Aart Bik351df3e2018-03-07 11:54:57 -08009152void InstructionCodeGeneratorMIPS::GenerateMinMax(HBinaryOperation* minmax, bool is_min) {
9153 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9154 DataType::Type type = minmax->GetResultType();
9155 switch (type) {
9156 case DataType::Type::kInt32:
9157 case DataType::Type::kInt64:
9158 GenerateMinMaxInt(minmax->GetLocations(), is_min, isR6, type);
9159 break;
9160 case DataType::Type::kFloat32:
9161 case DataType::Type::kFloat64:
9162 GenerateMinMaxFP(minmax->GetLocations(), is_min, isR6, type);
9163 break;
9164 default:
9165 LOG(FATAL) << "Unexpected type for HMinMax " << type;
9166 }
9167}
9168
Aart Bik1f8d51b2018-02-15 10:42:37 -08009169void LocationsBuilderMIPS::VisitMin(HMin* min) {
9170 CreateMinMaxLocations(GetGraph()->GetAllocator(), min);
9171}
9172
9173void InstructionCodeGeneratorMIPS::VisitMin(HMin* min) {
Aart Bik351df3e2018-03-07 11:54:57 -08009174 GenerateMinMax(min, /*is_min*/ true);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009175}
9176
9177void LocationsBuilderMIPS::VisitMax(HMax* max) {
9178 CreateMinMaxLocations(GetGraph()->GetAllocator(), max);
9179}
9180
9181void InstructionCodeGeneratorMIPS::VisitMax(HMax* max) {
Aart Bik351df3e2018-03-07 11:54:57 -08009182 GenerateMinMax(max, /*is_min*/ false);
Aart Bik1f8d51b2018-02-15 10:42:37 -08009183}
9184
Aart Bik3dad3412018-02-28 12:01:46 -08009185void LocationsBuilderMIPS::VisitAbs(HAbs* abs) {
9186 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
9187 switch (abs->GetResultType()) {
9188 case DataType::Type::kInt32:
9189 case DataType::Type::kInt64:
9190 locations->SetInAt(0, Location::RequiresRegister());
9191 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9192 break;
9193 case DataType::Type::kFloat32:
9194 case DataType::Type::kFloat64:
9195 locations->SetInAt(0, Location::RequiresFpuRegister());
9196 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9197 break;
9198 default:
9199 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9200 }
9201}
9202
9203void InstructionCodeGeneratorMIPS::GenerateAbsFP(LocationSummary* locations,
9204 DataType::Type type,
9205 bool isR2OrNewer,
9206 bool isR6) {
9207 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
9208 FRegister out = locations->Out().AsFpuRegister<FRegister>();
9209
9210 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
9211 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
9212 // (signaling NaN may become quiet though).
9213 //
9214 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
9215 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
9216 // affected by this instruction.
9217 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
9218 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
9219 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
9220 if (isR6) {
9221 if (type == DataType::Type::kFloat64) {
9222 __ AbsD(out, in);
9223 } else {
9224 DCHECK_EQ(type, DataType::Type::kFloat32);
9225 __ AbsS(out, in);
9226 }
9227 } else {
9228 if (type == DataType::Type::kFloat64) {
9229 if (in != out) {
9230 __ MovD(out, in);
9231 }
9232 __ MoveFromFpuHigh(TMP, in);
9233 // ins instruction is not available for R1.
9234 if (isR2OrNewer) {
9235 __ Ins(TMP, ZERO, 31, 1);
9236 } else {
9237 __ Sll(TMP, TMP, 1);
9238 __ Srl(TMP, TMP, 1);
9239 }
9240 __ MoveToFpuHigh(TMP, out);
9241 } else {
9242 DCHECK_EQ(type, DataType::Type::kFloat32);
9243 __ Mfc1(TMP, in);
9244 // ins instruction is not available for R1.
9245 if (isR2OrNewer) {
9246 __ Ins(TMP, ZERO, 31, 1);
9247 } else {
9248 __ Sll(TMP, TMP, 1);
9249 __ Srl(TMP, TMP, 1);
9250 }
9251 __ Mtc1(TMP, out);
9252 }
9253 }
9254}
9255
9256void InstructionCodeGeneratorMIPS::VisitAbs(HAbs* abs) {
9257 LocationSummary* locations = abs->GetLocations();
9258 bool isR2OrNewer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
9259 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
9260 switch (abs->GetResultType()) {
9261 case DataType::Type::kInt32: {
9262 Register in = locations->InAt(0).AsRegister<Register>();
9263 Register out = locations->Out().AsRegister<Register>();
9264 __ Sra(AT, in, 31);
9265 __ Xor(out, in, AT);
9266 __ Subu(out, out, AT);
9267 break;
9268 }
9269 case DataType::Type::kInt64: {
9270 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
9271 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
9272 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
9273 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
9274 // The comments in this section show the analogous operations which would
9275 // be performed if we had 64-bit registers "in", and "out".
9276 // __ Dsra32(AT, in, 31);
9277 __ Sra(AT, in_hi, 31);
9278 // __ Xor(out, in, AT);
9279 __ Xor(TMP, in_lo, AT);
9280 __ Xor(out_hi, in_hi, AT);
9281 // __ Dsubu(out, out, AT);
9282 __ Subu(out_lo, TMP, AT);
9283 __ Sltu(TMP, out_lo, TMP);
9284 __ Addu(out_hi, out_hi, TMP);
9285 break;
9286 }
9287 case DataType::Type::kFloat32:
9288 case DataType::Type::kFloat64:
9289 GenerateAbsFP(locations, abs->GetResultType(), isR2OrNewer, isR6);
9290 break;
9291 default:
9292 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
9293 }
9294}
9295
Igor Murashkind01745e2017-04-05 16:40:31 -07009296void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
9297 constructor_fence->SetLocations(nullptr);
9298}
9299
9300void InstructionCodeGeneratorMIPS::VisitConstructorFence(
9301 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
9302 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
9303}
9304
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009305void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9306 memory_barrier->SetLocations(nullptr);
9307}
9308
9309void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
9310 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
9311}
9312
9313void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009314 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009315 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009316 locations->SetInAt(0, MipsReturnLocation(return_type));
9317}
9318
9319void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
9320 codegen_->GenerateFrameExit();
9321}
9322
9323void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
9324 ret->SetLocations(nullptr);
9325}
9326
9327void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
9328 codegen_->GenerateFrameExit();
9329}
9330
Alexey Frunze92d90602015-12-18 18:16:36 -08009331void LocationsBuilderMIPS::VisitRor(HRor* ror) {
9332 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009333}
9334
Alexey Frunze92d90602015-12-18 18:16:36 -08009335void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
9336 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00009337}
9338
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009339void LocationsBuilderMIPS::VisitShl(HShl* shl) {
9340 HandleShift(shl);
9341}
9342
9343void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
9344 HandleShift(shl);
9345}
9346
9347void LocationsBuilderMIPS::VisitShr(HShr* shr) {
9348 HandleShift(shr);
9349}
9350
9351void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
9352 HandleShift(shr);
9353}
9354
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009355void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
9356 HandleBinaryOp(instruction);
9357}
9358
9359void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
9360 HandleBinaryOp(instruction);
9361}
9362
9363void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9364 HandleFieldGet(instruction, instruction->GetFieldInfo());
9365}
9366
9367void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
9368 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
9369}
9370
9371void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
9372 HandleFieldSet(instruction, instruction->GetFieldInfo());
9373}
9374
9375void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01009376 HandleFieldSet(instruction,
9377 instruction->GetFieldInfo(),
9378 instruction->GetDexPc(),
9379 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009380}
9381
9382void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
9383 HUnresolvedInstanceFieldGet* instruction) {
9384 FieldAccessCallingConventionMIPS calling_convention;
9385 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9386 instruction->GetFieldType(),
9387 calling_convention);
9388}
9389
9390void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
9391 HUnresolvedInstanceFieldGet* instruction) {
9392 FieldAccessCallingConventionMIPS calling_convention;
9393 codegen_->GenerateUnresolvedFieldAccess(instruction,
9394 instruction->GetFieldType(),
9395 instruction->GetFieldIndex(),
9396 instruction->GetDexPc(),
9397 calling_convention);
9398}
9399
9400void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
9401 HUnresolvedInstanceFieldSet* instruction) {
9402 FieldAccessCallingConventionMIPS calling_convention;
9403 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9404 instruction->GetFieldType(),
9405 calling_convention);
9406}
9407
9408void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
9409 HUnresolvedInstanceFieldSet* instruction) {
9410 FieldAccessCallingConventionMIPS calling_convention;
9411 codegen_->GenerateUnresolvedFieldAccess(instruction,
9412 instruction->GetFieldType(),
9413 instruction->GetFieldIndex(),
9414 instruction->GetDexPc(),
9415 calling_convention);
9416}
9417
9418void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
9419 HUnresolvedStaticFieldGet* instruction) {
9420 FieldAccessCallingConventionMIPS calling_convention;
9421 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9422 instruction->GetFieldType(),
9423 calling_convention);
9424}
9425
9426void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
9427 HUnresolvedStaticFieldGet* instruction) {
9428 FieldAccessCallingConventionMIPS calling_convention;
9429 codegen_->GenerateUnresolvedFieldAccess(instruction,
9430 instruction->GetFieldType(),
9431 instruction->GetFieldIndex(),
9432 instruction->GetDexPc(),
9433 calling_convention);
9434}
9435
9436void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
9437 HUnresolvedStaticFieldSet* instruction) {
9438 FieldAccessCallingConventionMIPS calling_convention;
9439 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9440 instruction->GetFieldType(),
9441 calling_convention);
9442}
9443
9444void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9445 HUnresolvedStaticFieldSet* instruction) {
9446 FieldAccessCallingConventionMIPS calling_convention;
9447 codegen_->GenerateUnresolvedFieldAccess(instruction,
9448 instruction->GetFieldType(),
9449 instruction->GetFieldIndex(),
9450 instruction->GetDexPc(),
9451 calling_convention);
9452}
9453
9454void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009455 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9456 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009457 // In suspend check slow path, usually there are no caller-save registers at all.
9458 // If SIMD instructions are present, however, we force spilling all live SIMD
9459 // registers in full width (since the runtime only saves/restores lower part).
9460 locations->SetCustomSlowPathCallerSaves(
9461 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009462}
9463
9464void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9465 HBasicBlock* block = instruction->GetBlock();
9466 if (block->GetLoopInformation() != nullptr) {
9467 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9468 // The back edge will generate the suspend check.
9469 return;
9470 }
9471 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9472 // The goto will generate the suspend check.
9473 return;
9474 }
9475 GenerateSuspendCheck(instruction, nullptr);
9476}
9477
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009478void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009479 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9480 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009481 InvokeRuntimeCallingConvention calling_convention;
9482 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9483}
9484
9485void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009486 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009487 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9488}
9489
9490void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009491 DataType::Type input_type = conversion->GetInputType();
9492 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009493 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9494 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009495 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009496
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009497 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9498 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009499 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9500 }
9501
9502 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009503 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009504 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9505 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009506 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009507 }
9508
Vladimir Markoca6fff82017-10-03 14:49:14 +01009509 LocationSummary* locations =
9510 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009511
9512 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009513 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009514 locations->SetInAt(0, Location::RequiresFpuRegister());
9515 } else {
9516 locations->SetInAt(0, Location::RequiresRegister());
9517 }
9518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009519 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009520 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9521 } else {
9522 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9523 }
9524 } else {
9525 InvokeRuntimeCallingConvention calling_convention;
9526
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009527 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009528 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9529 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009530 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009531 locations->SetInAt(0, Location::RegisterPairLocation(
9532 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9533 }
9534
9535 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9536 }
9537}
9538
9539void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9540 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009541 DataType::Type result_type = conversion->GetResultType();
9542 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009543 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009544 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009545
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009546 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9547 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009548
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009549 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009550 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9551 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9552 Register src = locations->InAt(0).AsRegister<Register>();
9553
Alexey Frunzea871ef12016-06-27 15:20:11 -07009554 if (dst_low != src) {
9555 __ Move(dst_low, src);
9556 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009557 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009558 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009559 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009560 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009561 ? locations->InAt(0).AsRegisterPairLow<Register>()
9562 : locations->InAt(0).AsRegister<Register>();
9563
9564 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009565 case DataType::Type::kUint8:
9566 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009567 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009568 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009569 if (has_sign_extension) {
9570 __ Seb(dst, src);
9571 } else {
9572 __ Sll(dst, src, 24);
9573 __ Sra(dst, dst, 24);
9574 }
9575 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009576 case DataType::Type::kUint16:
9577 __ Andi(dst, src, 0xFFFF);
9578 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009579 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009580 if (has_sign_extension) {
9581 __ Seh(dst, src);
9582 } else {
9583 __ Sll(dst, src, 16);
9584 __ Sra(dst, dst, 16);
9585 }
9586 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009587 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009588 if (dst != src) {
9589 __ Move(dst, src);
9590 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009591 break;
9592
9593 default:
9594 LOG(FATAL) << "Unexpected type conversion from " << input_type
9595 << " to " << result_type;
9596 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009597 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9598 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009599 if (isR6) {
9600 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9601 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9602 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9603 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9604 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9605 __ Mtc1(src_low, FTMP);
9606 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009607 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009608 __ Cvtsl(dst, FTMP);
9609 } else {
9610 __ Cvtdl(dst, FTMP);
9611 }
9612 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009613 QuickEntrypointEnum entrypoint =
9614 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009615 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009616 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009617 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9618 } else {
9619 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9620 }
9621 }
9622 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009623 Register src = locations->InAt(0).AsRegister<Register>();
9624 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9625 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009626 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009627 __ Cvtsw(dst, FTMP);
9628 } else {
9629 __ Cvtdw(dst, FTMP);
9630 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009631 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009632 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9633 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009634
9635 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9636 // value of the output type if the input is outside of the range after the truncation or
9637 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9638 // results. This matches the desired float/double-to-int/long conversion exactly.
9639 //
9640 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9641 // value when the input is either a NaN or is outside of the range of the output type
9642 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9643 // the same result.
9644 //
9645 // The code takes care of the different behaviors by first comparing the input to the
9646 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9647 // If the input is greater than or equal to the minimum, it procedes to the truncate
9648 // instruction, which will handle such an input the same way irrespective of NAN2008.
9649 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9650 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009651 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009652 if (isR6) {
9653 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9654 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9655 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9656 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9657 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009658
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009659 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009660 __ TruncLS(FTMP, src);
9661 } else {
9662 __ TruncLD(FTMP, src);
9663 }
9664 __ Mfc1(dst_low, FTMP);
9665 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009666 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009667 QuickEntrypointEnum entrypoint =
9668 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009669 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009670 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009671 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9672 } else {
9673 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9674 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009675 }
9676 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009677 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9678 Register dst = locations->Out().AsRegister<Register>();
9679 MipsLabel truncate;
9680 MipsLabel done;
9681
Lena Djokicf4e23a82017-05-09 15:43:45 +02009682 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009683 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009684 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9685 __ LoadConst32(TMP, min_val);
9686 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009687 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009688 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9689 __ LoadConst32(TMP, High32Bits(min_val));
9690 __ Mtc1(ZERO, FTMP);
9691 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009692 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009693
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009694 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009695 __ ColeS(0, FTMP, src);
9696 } else {
9697 __ ColeD(0, FTMP, src);
9698 }
9699 __ Bc1t(0, &truncate);
9700
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009701 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009702 __ CeqS(0, src, src);
9703 } else {
9704 __ CeqD(0, src, src);
9705 }
9706 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9707 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009708
9709 __ B(&done);
9710
9711 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009712 }
9713
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009714 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009715 __ TruncWS(FTMP, src);
9716 } else {
9717 __ TruncWD(FTMP, src);
9718 }
9719 __ Mfc1(dst, FTMP);
9720
Lena Djokicf4e23a82017-05-09 15:43:45 +02009721 if (!isR6) {
9722 __ Bind(&done);
9723 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009724 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009725 } else if (DataType::IsFloatingPointType(result_type) &&
9726 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009727 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9728 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009729 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009730 __ Cvtsd(dst, src);
9731 } else {
9732 __ Cvtds(dst, src);
9733 }
9734 } else {
9735 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9736 << " to " << result_type;
9737 }
9738}
9739
9740void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9741 HandleShift(ushr);
9742}
9743
9744void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9745 HandleShift(ushr);
9746}
9747
9748void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9749 HandleBinaryOp(instruction);
9750}
9751
9752void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9753 HandleBinaryOp(instruction);
9754}
9755
9756void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9757 // Nothing to do, this should be removed during prepare for register allocator.
9758 LOG(FATAL) << "Unreachable";
9759}
9760
9761void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9762 // Nothing to do, this should be removed during prepare for register allocator.
9763 LOG(FATAL) << "Unreachable";
9764}
9765
9766void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009767 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009768}
9769
9770void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009771 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009772}
9773
9774void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009775 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009776}
9777
9778void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009779 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009780}
9781
9782void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009783 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009784}
9785
9786void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009787 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009788}
9789
9790void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009791 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009792}
9793
9794void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009795 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009796}
9797
9798void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009799 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009800}
9801
9802void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009803 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009804}
9805
9806void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009807 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009808}
9809
9810void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009811 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009812}
9813
9814void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009815 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009816}
9817
9818void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009819 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009820}
9821
9822void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009823 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009824}
9825
9826void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009827 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009828}
9829
9830void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009831 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009832}
9833
9834void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009835 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009836}
9837
9838void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009839 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009840}
9841
9842void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009843 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009844}
9845
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009846void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9847 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009848 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009849 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009850 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9851 uint32_t num_entries = switch_instr->GetNumEntries();
9852 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9853 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9854 // instruction to simulate PC-relative addressing when accessing the jump table.
9855 // NAL clobbers RA. Make sure RA is preserved.
9856 codegen_->ClobberRA();
9857 }
9858 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009859}
9860
Alexey Frunze96b66822016-09-10 02:32:44 -07009861void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9862 int32_t lower_bound,
9863 uint32_t num_entries,
9864 HBasicBlock* switch_block,
9865 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009866 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009867 Register temp_reg = TMP;
9868 __ Addiu32(temp_reg, value_reg, -lower_bound);
9869 // Jump to default if index is negative
9870 // Note: We don't check the case that index is positive while value < lower_bound, because in
9871 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9872 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9873
Alexey Frunze96b66822016-09-10 02:32:44 -07009874 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009875 // Jump to successors[0] if value == lower_bound.
9876 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9877 int32_t last_index = 0;
9878 for (; num_entries - last_index > 2; last_index += 2) {
9879 __ Addiu(temp_reg, temp_reg, -2);
9880 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9881 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9882 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9883 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9884 }
9885 if (num_entries - last_index == 2) {
9886 // The last missing case_value.
9887 __ Addiu(temp_reg, temp_reg, -1);
9888 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009889 }
9890
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009891 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009892 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009893 __ B(codegen_->GetLabelOf(default_block));
9894 }
9895}
9896
Alexey Frunze96b66822016-09-10 02:32:44 -07009897void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9898 Register constant_area,
9899 int32_t lower_bound,
9900 uint32_t num_entries,
9901 HBasicBlock* switch_block,
9902 HBasicBlock* default_block) {
9903 // Create a jump table.
9904 std::vector<MipsLabel*> labels(num_entries);
9905 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9906 for (uint32_t i = 0; i < num_entries; i++) {
9907 labels[i] = codegen_->GetLabelOf(successors[i]);
9908 }
9909 JumpTable* table = __ CreateJumpTable(std::move(labels));
9910
9911 // Is the value in range?
9912 __ Addiu32(TMP, value_reg, -lower_bound);
9913 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9914 __ Sltiu(AT, TMP, num_entries);
9915 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9916 } else {
9917 __ LoadConst32(AT, num_entries);
9918 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9919 }
9920
9921 // We are in the range of the table.
9922 // Load the target address from the jump table, indexing by the value.
9923 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009924 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009925 __ Lw(TMP, TMP, 0);
9926 // Compute the absolute target address by adding the table start address
9927 // (the table contains offsets to targets relative to its start).
9928 __ Addu(TMP, TMP, AT);
9929 // And jump.
9930 __ Jr(TMP);
9931 __ NopIfNoReordering();
9932}
9933
9934void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9935 int32_t lower_bound = switch_instr->GetStartValue();
9936 uint32_t num_entries = switch_instr->GetNumEntries();
9937 LocationSummary* locations = switch_instr->GetLocations();
9938 Register value_reg = locations->InAt(0).AsRegister<Register>();
9939 HBasicBlock* switch_block = switch_instr->GetBlock();
9940 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9941
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009942 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009943 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009944 //
9945 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9946 // to access the jump table and it is implemented by changing HPackedSwitch to
9947 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9948 // VisitMipsPackedSwitch()).
9949 //
9950 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9951 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9952 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009953 GenTableBasedPackedSwitch(value_reg,
9954 ZERO,
9955 lower_bound,
9956 num_entries,
9957 switch_block,
9958 default_block);
9959 } else {
9960 GenPackedSwitchWithCompares(value_reg,
9961 lower_bound,
9962 num_entries,
9963 switch_block,
9964 default_block);
9965 }
9966}
9967
9968void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9969 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009970 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009971 locations->SetInAt(0, Location::RequiresRegister());
9972 // Constant area pointer (HMipsComputeBaseMethodAddress).
9973 locations->SetInAt(1, Location::RequiresRegister());
9974}
9975
9976void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9977 int32_t lower_bound = switch_instr->GetStartValue();
9978 uint32_t num_entries = switch_instr->GetNumEntries();
9979 LocationSummary* locations = switch_instr->GetLocations();
9980 Register value_reg = locations->InAt(0).AsRegister<Register>();
9981 Register constant_area = locations->InAt(1).AsRegister<Register>();
9982 HBasicBlock* switch_block = switch_instr->GetBlock();
9983 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9984
9985 // This is an R2-only path. HPackedSwitch has been changed to
9986 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9987 // required to address the jump table relative to PC.
9988 GenTableBasedPackedSwitch(value_reg,
9989 constant_area,
9990 lower_bound,
9991 num_entries,
9992 switch_block,
9993 default_block);
9994}
9995
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009996void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9997 HMipsComputeBaseMethodAddress* insn) {
9998 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009999 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -070010000 locations->SetOut(Location::RequiresRegister());
10001}
10002
10003void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
10004 HMipsComputeBaseMethodAddress* insn) {
10005 LocationSummary* locations = insn->GetLocations();
10006 Register reg = locations->Out().AsRegister<Register>();
10007
10008 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
10009
10010 // Generate a dummy PC-relative call to obtain PC.
10011 __ Nal();
10012 // Grab the return address off RA.
10013 __ Move(reg, RA);
10014
10015 // Remember this offset (the obtained PC value) for later use with constant area.
10016 __ BindPcRelBaseLabel();
10017}
10018
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010019void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10020 // The trampoline uses the same calling convention as dex calling conventions,
10021 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
10022 // the method_idx.
10023 HandleInvoke(invoke);
10024}
10025
10026void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
10027 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
10028}
10029
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010030void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10031 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +010010032 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010033 locations->SetInAt(0, Location::RequiresRegister());
10034 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010035}
10036
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010037void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
10038 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +000010039 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010040 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010041 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010042 __ LoadFromOffset(kLoadWord,
10043 locations->Out().AsRegister<Register>(),
10044 locations->InAt(0).AsRegister<Register>(),
10045 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010046 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010047 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +000010048 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +000010049 __ LoadFromOffset(kLoadWord,
10050 locations->Out().AsRegister<Register>(),
10051 locations->InAt(0).AsRegister<Register>(),
10052 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +010010053 __ LoadFromOffset(kLoadWord,
10054 locations->Out().AsRegister<Register>(),
10055 locations->Out().AsRegister<Register>(),
10056 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +000010057 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +000010058}
10059
xueliang.zhonge0eb4832017-10-30 13:43:14 +000010060void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10061 ATTRIBUTE_UNUSED) {
10062 LOG(FATAL) << "Unreachable";
10063}
10064
10065void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
10066 ATTRIBUTE_UNUSED) {
10067 LOG(FATAL) << "Unreachable";
10068}
10069
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020010070#undef __
10071#undef QUICK_ENTRY_POINT
10072
10073} // namespace mips
10074} // namespace art