blob: 1c249186983deb43f05fadf649a1eae24dabdd8f [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010036#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020037#include "thread.h"
38#include "utils/assembler.h"
39#include "utils/mips/assembler_mips.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace mips {
44
45static constexpr int kCurrentMethodStackOffset = 0;
46static constexpr Register kMethodRegisterArgument = A0;
47
Alexey Frunze4147fcc2017-06-17 19:57:27 -070048// Flags controlling the use of thunks for Baker read barriers.
49constexpr bool kBakerReadBarrierThunksEnableForFields = true;
50constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
51constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
52
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020054 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010055 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010057 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010058 case DataType::Type::kInt8:
59 case DataType::Type::kUint16:
60 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -080061 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010062 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020063 return Location::RegisterLocation(V0);
64
Aart Bik66c158e2018-01-31 12:55:04 -080065 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020067 return Location::RegisterPairLocation(V0, V1);
68
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010069 case DataType::Type::kFloat32:
70 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020071 return Location::FpuRegisterLocation(F0);
72
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020074 return Location();
75 }
76 UNREACHABLE();
77}
78
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010079Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020080 return MipsReturnLocation(type);
81}
82
83Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
84 return Location::RegisterLocation(kMethodRegisterArgument);
85}
86
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020088 Location next_location;
89
90 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010091 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010093 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010094 case DataType::Type::kInt8:
95 case DataType::Type::kUint16:
96 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010097 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020098 uint32_t gp_index = gp_index_++;
99 if (gp_index < calling_convention.GetNumberOfRegisters()) {
100 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
101 } else {
102 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
103 next_location = Location::StackSlot(stack_offset);
104 }
105 break;
106 }
107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100108 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200109 uint32_t gp_index = gp_index_;
110 gp_index_ += 2;
111 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800112 Register reg = calling_convention.GetRegisterAt(gp_index);
113 if (reg == A1 || reg == A3) {
114 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200115 gp_index++;
116 }
117 Register low_even = calling_convention.GetRegisterAt(gp_index);
118 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
119 DCHECK_EQ(low_even + 1, high_odd);
120 next_location = Location::RegisterPairLocation(low_even, high_odd);
121 } else {
122 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
123 next_location = Location::DoubleStackSlot(stack_offset);
124 }
125 break;
126 }
127
128 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
129 // will take up the even/odd pair, while floats are stored in even regs only.
130 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100131 case DataType::Type::kFloat32:
132 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200133 uint32_t float_index = float_index_++;
134 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
135 next_location = Location::FpuRegisterLocation(
136 calling_convention.GetFpuRegisterAt(float_index));
137 } else {
138 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100139 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
140 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200141 }
142 break;
143 }
144
Aart Bik66c158e2018-01-31 12:55:04 -0800145 case DataType::Type::kUint32:
146 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200148 LOG(FATAL) << "Unexpected parameter type " << type;
149 break;
150 }
151
152 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200154
155 return next_location;
156}
157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100158Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200159 return MipsReturnLocation(type);
160}
161
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100162// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
163#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700164#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200165
166class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
167 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000168 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200169
170 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
171 LocationSummary* locations = instruction_->GetLocations();
172 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
173 __ Bind(GetEntryLabel());
174 if (instruction_->CanThrowIntoCatchBlock()) {
175 // Live registers will be restored in the catch block if caught.
176 SaveLiveRegisters(codegen, instruction_->GetLocations());
177 }
178 // We're moving two locations to locations that could overlap, so we need a parallel
179 // move resolver.
180 InvokeRuntimeCallingConvention calling_convention;
181 codegen->EmitParallelMoves(locations->InAt(0),
182 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100183 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200184 locations->InAt(1),
185 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100186 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100187 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
188 ? kQuickThrowStringBounds
189 : kQuickThrowArrayBounds;
190 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100191 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200192 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
193 }
194
195 bool IsFatal() const OVERRIDE { return true; }
196
197 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
198
199 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200200 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
201};
202
203class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
204 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000205 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206
207 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
208 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
209 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100210 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200211 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
212 }
213
214 bool IsFatal() const OVERRIDE { return true; }
215
216 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
217
218 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200219 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
220};
221
222class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
223 public:
224 LoadClassSlowPathMIPS(HLoadClass* cls,
225 HInstruction* at,
226 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000227 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700228 : SlowPathCodeMIPS(at),
229 cls_(cls),
230 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000231 do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
233 }
234
235 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000236 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700237 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200238 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700239 InvokeRuntimeCallingConvention calling_convention;
240 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200241 __ Bind(GetEntryLabel());
242 SaveLiveRegisters(codegen, locations);
243
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 dex::TypeIndex type_index = cls_->GetTypeIndex();
245 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100246 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
247 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000248 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200249 if (do_clinit_) {
250 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
251 } else {
252 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
253 }
254
255 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200256 if (out.IsValid()) {
257 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100258 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700259 mips_codegen->MoveLocation(out,
260 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
261 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200262 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200263 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700264
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200265 __ B(GetExitLabel());
266 }
267
268 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
269
270 private:
271 // The class this slow path will load.
272 HLoadClass* const cls_;
273
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
281};
282
283class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
284 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000285 explicit LoadStringSlowPathMIPS(HLoadString* instruction)
286 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700289 DCHECK(instruction_->IsLoadString());
290 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200291 LocationSummary* locations = instruction_->GetLocations();
292 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000293 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200294 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700295 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200296 __ Bind(GetEntryLabel());
297 SaveLiveRegisters(codegen, locations);
298
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000299 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100300 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200301 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700302
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100303 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200304 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000308
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200309 __ B(GetExitLabel());
310 }
311
312 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
313
314 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200315 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
316};
317
318class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
319 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000320 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200321
322 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
323 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
324 __ Bind(GetEntryLabel());
325 if (instruction_->CanThrowIntoCatchBlock()) {
326 // Live registers will be restored in the catch block if caught.
327 SaveLiveRegisters(codegen, instruction_->GetLocations());
328 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100329 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330 instruction_,
331 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100332 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200333 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
334 }
335
336 bool IsFatal() const OVERRIDE { return true; }
337
338 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
339
340 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
342};
343
344class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
345 public:
346 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000347 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200348
349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200350 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
352 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200353 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100354 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200355 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200356 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200357 if (successor_ == nullptr) {
358 __ B(GetReturnLabel());
359 } else {
360 __ B(mips_codegen->GetLabelOf(successor_));
361 }
362 }
363
364 MipsLabel* GetReturnLabel() {
365 DCHECK(successor_ == nullptr);
366 return &return_label_;
367 }
368
369 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
370
Chris Larsena2045912017-11-02 12:39:54 -0700371 HBasicBlock* GetSuccessor() const {
372 return successor_;
373 }
374
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200375 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200376 // If not null, the block to branch to after the suspend check.
377 HBasicBlock* const successor_;
378
379 // If `successor_` is null, the label to branch to after the suspend check.
380 MipsLabel return_label_;
381
382 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
383};
384
385class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
386 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800387 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
388 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200392 uint32_t dex_pc = instruction_->GetDexPc();
393 DCHECK(instruction_->IsCheckCast()
394 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
395 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
396
397 __ Bind(GetEntryLabel());
Alexey Frunzedfc30af2018-01-24 16:25:10 -0800398 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800399 SaveLiveRegisters(codegen, locations);
400 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200401
402 // We're moving two locations to locations that could overlap, so we need a parallel
403 // move resolver.
404 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800405 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200406 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100407 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800408 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200409 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100410 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200411 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100412 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800413 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100414 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200415 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
416 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 } else {
418 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800419 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
420 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 }
422
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800423 if (!is_fatal_) {
424 RestoreLiveRegisters(codegen, locations);
425 __ B(GetExitLabel());
426 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200427 }
428
429 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
430
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800431 bool IsFatal() const OVERRIDE { return is_fatal_; }
432
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800434 const bool is_fatal_;
435
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200436 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
437};
438
439class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
440 public:
Aart Bik42249c32016-01-07 15:33:50 -0800441 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000442 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443
444 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800445 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100447 LocationSummary* locations = instruction_->GetLocations();
448 SaveLiveRegisters(codegen, locations);
449 InvokeRuntimeCallingConvention calling_convention;
450 __ LoadConst32(calling_convention.GetRegisterAt(0),
451 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100452 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100453 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 }
455
456 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
457
458 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200459 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
460};
461
Alexey Frunze15958152017-02-09 19:08:30 -0800462class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
463 public:
464 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
465
466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 LocationSummary* locations = instruction_->GetLocations();
468 __ Bind(GetEntryLabel());
469 SaveLiveRegisters(codegen, locations);
470
471 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100472 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800473 parallel_move.AddMove(
474 locations->InAt(0),
475 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100476 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800477 nullptr);
478 parallel_move.AddMove(
479 locations->InAt(1),
480 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100481 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800482 nullptr);
483 parallel_move.AddMove(
484 locations->InAt(2),
485 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100486 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800487 nullptr);
488 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
489
490 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
491 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
492 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
493 RestoreLiveRegisters(codegen, locations);
494 __ B(GetExitLabel());
495 }
496
497 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
498
499 private:
500 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
501};
502
503// Slow path marking an object reference `ref` during a read
504// barrier. The field `obj.field` in the object `obj` holding this
505// reference does not get updated by this slow path after marking (see
506// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
507//
508// This means that after the execution of this slow path, `ref` will
509// always be up-to-date, but `obj.field` may not; i.e., after the
510// flip, `ref` will be a to-space reference, but `obj.field` will
511// probably still be a from-space reference (unless it gets updated by
512// another thread, or if another thread installed another object
513// reference (different from `ref`) in `obj.field`).
514//
515// If `entrypoint` is a valid location it is assumed to already be
516// holding the entrypoint. The case where the entrypoint is passed in
517// is for the GcRoot read barrier.
518class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
519 public:
520 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
521 Location ref,
522 Location entrypoint = Location::NoLocation())
523 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
524 DCHECK(kEmitCompilerReadBarrier);
525 }
526
527 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
528
529 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
530 LocationSummary* locations = instruction_->GetLocations();
531 Register ref_reg = ref_.AsRegister<Register>();
532 DCHECK(locations->CanCall());
533 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
534 DCHECK(instruction_->IsInstanceFieldGet() ||
535 instruction_->IsStaticFieldGet() ||
536 instruction_->IsArrayGet() ||
537 instruction_->IsArraySet() ||
538 instruction_->IsLoadClass() ||
539 instruction_->IsLoadString() ||
540 instruction_->IsInstanceOf() ||
541 instruction_->IsCheckCast() ||
542 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
543 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
544 << "Unexpected instruction in read barrier marking slow path: "
545 << instruction_->DebugName();
546
547 __ Bind(GetEntryLabel());
548 // No need to save live registers; it's taken care of by the
549 // entrypoint. Also, there is no need to update the stack mask,
550 // as this runtime call will not trigger a garbage collection.
551 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
552 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
553 (S2 <= ref_reg && ref_reg <= S7) ||
554 (ref_reg == FP)) << ref_reg;
555 // "Compact" slow path, saving two moves.
556 //
557 // Instead of using the standard runtime calling convention (input
558 // and output in A0 and V0 respectively):
559 //
560 // A0 <- ref
561 // V0 <- ReadBarrierMark(A0)
562 // ref <- V0
563 //
564 // we just use rX (the register containing `ref`) as input and output
565 // of a dedicated entrypoint:
566 //
567 // rX <- ReadBarrierMarkRegX(rX)
568 //
569 if (entrypoint_.IsValid()) {
570 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
571 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
572 __ Jalr(entrypoint_.AsRegister<Register>());
573 __ NopIfNoReordering();
574 } else {
575 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100576 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800577 // This runtime call does not require a stack map.
578 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
579 instruction_,
580 this,
581 /* direct */ false);
582 }
583 __ B(GetExitLabel());
584 }
585
586 private:
587 // The location (register) of the marked object reference.
588 const Location ref_;
589
590 // The location of the entrypoint if already loaded.
591 const Location entrypoint_;
592
593 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
594};
595
596// Slow path marking an object reference `ref` during a read barrier,
597// and if needed, atomically updating the field `obj.field` in the
598// object `obj` holding this reference after marking (contrary to
599// ReadBarrierMarkSlowPathMIPS above, which never tries to update
600// `obj.field`).
601//
602// This means that after the execution of this slow path, both `ref`
603// and `obj.field` will be up-to-date; i.e., after the flip, both will
604// hold the same to-space reference (unless another thread installed
605// another object reference (different from `ref`) in `obj.field`).
606class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
607 public:
608 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
609 Location ref,
610 Register obj,
611 Location field_offset,
612 Register temp1)
613 : SlowPathCodeMIPS(instruction),
614 ref_(ref),
615 obj_(obj),
616 field_offset_(field_offset),
617 temp1_(temp1) {
618 DCHECK(kEmitCompilerReadBarrier);
619 }
620
621 const char* GetDescription() const OVERRIDE {
622 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
623 }
624
625 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
626 LocationSummary* locations = instruction_->GetLocations();
627 Register ref_reg = ref_.AsRegister<Register>();
628 DCHECK(locations->CanCall());
629 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
630 // This slow path is only used by the UnsafeCASObject intrinsic.
631 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
632 << "Unexpected instruction in read barrier marking and field updating slow path: "
633 << instruction_->DebugName();
634 DCHECK(instruction_->GetLocations()->Intrinsified());
635 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
636 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
637
638 __ Bind(GetEntryLabel());
639
640 // Save the old reference.
641 // Note that we cannot use AT or TMP to save the old reference, as those
642 // are used by the code that follows, but we need the old reference after
643 // the call to the ReadBarrierMarkRegX entry point.
644 DCHECK_NE(temp1_, AT);
645 DCHECK_NE(temp1_, TMP);
646 __ Move(temp1_, ref_reg);
647
648 // No need to save live registers; it's taken care of by the
649 // entrypoint. Also, there is no need to update the stack mask,
650 // as this runtime call will not trigger a garbage collection.
651 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
652 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
653 (S2 <= ref_reg && ref_reg <= S7) ||
654 (ref_reg == FP)) << ref_reg;
655 // "Compact" slow path, saving two moves.
656 //
657 // Instead of using the standard runtime calling convention (input
658 // and output in A0 and V0 respectively):
659 //
660 // A0 <- ref
661 // V0 <- ReadBarrierMark(A0)
662 // ref <- V0
663 //
664 // we just use rX (the register containing `ref`) as input and output
665 // of a dedicated entrypoint:
666 //
667 // rX <- ReadBarrierMarkRegX(rX)
668 //
669 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100670 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800671 // This runtime call does not require a stack map.
672 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
673 instruction_,
674 this,
675 /* direct */ false);
676
677 // If the new reference is different from the old reference,
678 // update the field in the holder (`*(obj_ + field_offset_)`).
679 //
680 // Note that this field could also hold a different object, if
681 // another thread had concurrently changed it. In that case, the
682 // the compare-and-set (CAS) loop below would abort, leaving the
683 // field as-is.
684 MipsLabel done;
685 __ Beq(temp1_, ref_reg, &done);
686
687 // Update the the holder's field atomically. This may fail if
688 // mutator updates before us, but it's OK. This is achieved
689 // using a strong compare-and-set (CAS) operation with relaxed
690 // memory synchronization ordering, where the expected value is
691 // the old reference and the desired value is the new reference.
692
693 // Convenience aliases.
694 Register base = obj_;
695 // The UnsafeCASObject intrinsic uses a register pair as field
696 // offset ("long offset"), of which only the low part contains
697 // data.
698 Register offset = field_offset_.AsRegisterPairLow<Register>();
699 Register expected = temp1_;
700 Register value = ref_reg;
701 Register tmp_ptr = TMP; // Pointer to actual memory.
702 Register tmp = AT; // Value in memory.
703
704 __ Addu(tmp_ptr, base, offset);
705
706 if (kPoisonHeapReferences) {
707 __ PoisonHeapReference(expected);
708 // Do not poison `value` if it is the same register as
709 // `expected`, which has just been poisoned.
710 if (value != expected) {
711 __ PoisonHeapReference(value);
712 }
713 }
714
715 // do {
716 // tmp = [r_ptr] - expected;
717 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
718
719 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
720 MipsLabel loop_head, exit_loop;
721 __ Bind(&loop_head);
722 if (is_r6) {
723 __ LlR6(tmp, tmp_ptr);
724 } else {
725 __ LlR2(tmp, tmp_ptr);
726 }
727 __ Bne(tmp, expected, &exit_loop);
728 __ Move(tmp, value);
729 if (is_r6) {
730 __ ScR6(tmp, tmp_ptr);
731 } else {
732 __ ScR2(tmp, tmp_ptr);
733 }
734 __ Beqz(tmp, &loop_head);
735 __ Bind(&exit_loop);
736
737 if (kPoisonHeapReferences) {
738 __ UnpoisonHeapReference(expected);
739 // Do not unpoison `value` if it is the same register as
740 // `expected`, which has just been unpoisoned.
741 if (value != expected) {
742 __ UnpoisonHeapReference(value);
743 }
744 }
745
746 __ Bind(&done);
747 __ B(GetExitLabel());
748 }
749
750 private:
751 // The location (register) of the marked object reference.
752 const Location ref_;
753 // The register containing the object holding the marked object reference field.
754 const Register obj_;
755 // The location of the offset of the marked reference field within `obj_`.
756 Location field_offset_;
757
758 const Register temp1_;
759
760 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
761};
762
763// Slow path generating a read barrier for a heap reference.
764class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
765 public:
766 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
767 Location out,
768 Location ref,
769 Location obj,
770 uint32_t offset,
771 Location index)
772 : SlowPathCodeMIPS(instruction),
773 out_(out),
774 ref_(ref),
775 obj_(obj),
776 offset_(offset),
777 index_(index) {
778 DCHECK(kEmitCompilerReadBarrier);
779 // If `obj` is equal to `out` or `ref`, it means the initial object
780 // has been overwritten by (or after) the heap object reference load
781 // to be instrumented, e.g.:
782 //
783 // __ LoadFromOffset(kLoadWord, out, out, offset);
784 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
785 //
786 // In that case, we have lost the information about the original
787 // object, and the emitted read barrier cannot work properly.
788 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
789 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
790 }
791
792 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
793 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
794 LocationSummary* locations = instruction_->GetLocations();
795 Register reg_out = out_.AsRegister<Register>();
796 DCHECK(locations->CanCall());
797 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
798 DCHECK(instruction_->IsInstanceFieldGet() ||
799 instruction_->IsStaticFieldGet() ||
800 instruction_->IsArrayGet() ||
801 instruction_->IsInstanceOf() ||
802 instruction_->IsCheckCast() ||
803 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
804 << "Unexpected instruction in read barrier for heap reference slow path: "
805 << instruction_->DebugName();
806
807 __ Bind(GetEntryLabel());
808 SaveLiveRegisters(codegen, locations);
809
810 // We may have to change the index's value, but as `index_` is a
811 // constant member (like other "inputs" of this slow path),
812 // introduce a copy of it, `index`.
813 Location index = index_;
814 if (index_.IsValid()) {
815 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
816 if (instruction_->IsArrayGet()) {
817 // Compute the actual memory offset and store it in `index`.
818 Register index_reg = index_.AsRegister<Register>();
819 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
820 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
821 // We are about to change the value of `index_reg` (see the
822 // calls to art::mips::MipsAssembler::Sll and
823 // art::mips::MipsAssembler::Addiu32 below), but it has
824 // not been saved by the previous call to
825 // art::SlowPathCode::SaveLiveRegisters, as it is a
826 // callee-save register --
827 // art::SlowPathCode::SaveLiveRegisters does not consider
828 // callee-save registers, as it has been designed with the
829 // assumption that callee-save registers are supposed to be
830 // handled by the called function. So, as a callee-save
831 // register, `index_reg` _would_ eventually be saved onto
832 // the stack, but it would be too late: we would have
833 // changed its value earlier. Therefore, we manually save
834 // it here into another freely available register,
835 // `free_reg`, chosen of course among the caller-save
836 // registers (as a callee-save `free_reg` register would
837 // exhibit the same problem).
838 //
839 // Note we could have requested a temporary register from
840 // the register allocator instead; but we prefer not to, as
841 // this is a slow path, and we know we can find a
842 // caller-save register that is available.
843 Register free_reg = FindAvailableCallerSaveRegister(codegen);
844 __ Move(free_reg, index_reg);
845 index_reg = free_reg;
846 index = Location::RegisterLocation(index_reg);
847 } else {
848 // The initial register stored in `index_` has already been
849 // saved in the call to art::SlowPathCode::SaveLiveRegisters
850 // (as it is not a callee-save register), so we can freely
851 // use it.
852 }
853 // Shifting the index value contained in `index_reg` by the scale
854 // factor (2) cannot overflow in practice, as the runtime is
855 // unable to allocate object arrays with a size larger than
856 // 2^26 - 1 (that is, 2^28 - 4 bytes).
857 __ Sll(index_reg, index_reg, TIMES_4);
858 static_assert(
859 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
860 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
861 __ Addiu32(index_reg, index_reg, offset_);
862 } else {
863 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
864 // intrinsics, `index_` is not shifted by a scale factor of 2
865 // (as in the case of ArrayGet), as it is actually an offset
866 // to an object field within an object.
867 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
868 DCHECK(instruction_->GetLocations()->Intrinsified());
869 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
870 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
871 << instruction_->AsInvoke()->GetIntrinsic();
872 DCHECK_EQ(offset_, 0U);
873 DCHECK(index_.IsRegisterPair());
874 // UnsafeGet's offset location is a register pair, the low
875 // part contains the correct offset.
876 index = index_.ToLow();
877 }
878 }
879
880 // We're moving two or three locations to locations that could
881 // overlap, so we need a parallel move resolver.
882 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100883 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800884 parallel_move.AddMove(ref_,
885 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100886 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800887 nullptr);
888 parallel_move.AddMove(obj_,
889 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100890 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800891 nullptr);
892 if (index.IsValid()) {
893 parallel_move.AddMove(index,
894 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100895 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800896 nullptr);
897 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
898 } else {
899 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
900 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
901 }
902 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
903 instruction_,
904 instruction_->GetDexPc(),
905 this);
906 CheckEntrypointTypes<
907 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200908 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100909 calling_convention.GetReturnLocation(DataType::Type::kReference),
910 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800911
912 RestoreLiveRegisters(codegen, locations);
913 __ B(GetExitLabel());
914 }
915
916 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
917
918 private:
919 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
920 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
921 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
922 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
923 if (i != ref &&
924 i != obj &&
925 !codegen->IsCoreCalleeSaveRegister(i) &&
926 !codegen->IsBlockedCoreRegister(i)) {
927 return static_cast<Register>(i);
928 }
929 }
930 // We shall never fail to find a free caller-save register, as
931 // there are more than two core caller-save registers on MIPS
932 // (meaning it is possible to find one which is different from
933 // `ref` and `obj`).
934 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
935 LOG(FATAL) << "Could not find a free caller-save register";
936 UNREACHABLE();
937 }
938
939 const Location out_;
940 const Location ref_;
941 const Location obj_;
942 const uint32_t offset_;
943 // An additional location containing an index to an array.
944 // Only used for HArrayGet and the UnsafeGetObject &
945 // UnsafeGetObjectVolatile intrinsics.
946 const Location index_;
947
948 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
949};
950
951// Slow path generating a read barrier for a GC root.
952class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
953 public:
954 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
955 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
956 DCHECK(kEmitCompilerReadBarrier);
957 }
958
959 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
960 LocationSummary* locations = instruction_->GetLocations();
961 Register reg_out = out_.AsRegister<Register>();
962 DCHECK(locations->CanCall());
963 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
964 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
965 << "Unexpected instruction in read barrier for GC root slow path: "
966 << instruction_->DebugName();
967
968 __ Bind(GetEntryLabel());
969 SaveLiveRegisters(codegen, locations);
970
971 InvokeRuntimeCallingConvention calling_convention;
972 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +0200973 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
974 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100975 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800976 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
977 instruction_,
978 instruction_->GetDexPc(),
979 this);
980 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +0200981 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100982 calling_convention.GetReturnLocation(DataType::Type::kReference),
983 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800984
985 RestoreLiveRegisters(codegen, locations);
986 __ B(GetExitLabel());
987 }
988
989 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
990
991 private:
992 const Location out_;
993 const Location root_;
994
995 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
996};
997
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200998CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
999 const MipsInstructionSetFeatures& isa_features,
1000 const CompilerOptions& compiler_options,
1001 OptimizingCompilerStats* stats)
1002 : CodeGenerator(graph,
1003 kNumberOfCoreRegisters,
1004 kNumberOfFRegisters,
1005 kNumberOfRegisterPairs,
1006 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1007 arraysize(kCoreCalleeSaves)),
1008 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1009 arraysize(kFpuCalleeSaves)),
1010 compiler_options,
1011 stats),
1012 block_labels_(nullptr),
1013 location_builder_(graph, this),
1014 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001015 move_resolver_(graph->GetAllocator(), this),
1016 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001017 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001018 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001019 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001020 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001021 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001022 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001023 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001024 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001025 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1026 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1027 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001028 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001029 // Save RA (containing the return address) to mimic Quick.
1030 AddAllocatedRegister(Location::RegisterLocation(RA));
1031}
1032
1033#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001034// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1035#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001036#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001037
1038void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1039 // Ensure that we fix up branches.
1040 __ FinalizeCode();
1041
1042 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001043 StackMapStream* stack_map_stream = GetStackMapStream();
1044 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001045 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +00001046 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001047 uint32_t new_position = __ GetAdjustedPosition(old_position);
1048 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001049 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001050 }
1051
1052 // Adjust pc offsets for the disassembly information.
1053 if (disasm_info_ != nullptr) {
1054 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1055 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1056 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1057 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1058 it.second.start = __ GetAdjustedPosition(it.second.start);
1059 it.second.end = __ GetAdjustedPosition(it.second.end);
1060 }
1061 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1062 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1063 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1064 }
1065 }
1066
1067 CodeGenerator::Finalize(allocator);
1068}
1069
1070MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1071 return codegen_->GetAssembler();
1072}
1073
1074void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1075 DCHECK_LT(index, moves_.size());
1076 MoveOperands* move = moves_[index];
1077 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1078}
1079
1080void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1081 DCHECK_LT(index, moves_.size());
1082 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001083 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001084 Location loc1 = move->GetDestination();
1085 Location loc2 = move->GetSource();
1086
1087 DCHECK(!loc1.IsConstant());
1088 DCHECK(!loc2.IsConstant());
1089
1090 if (loc1.Equals(loc2)) {
1091 return;
1092 }
1093
1094 if (loc1.IsRegister() && loc2.IsRegister()) {
1095 // Swap 2 GPRs.
1096 Register r1 = loc1.AsRegister<Register>();
1097 Register r2 = loc2.AsRegister<Register>();
1098 __ Move(TMP, r2);
1099 __ Move(r2, r1);
1100 __ Move(r1, TMP);
1101 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001102 if (codegen_->GetGraph()->HasSIMD()) {
1103 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1104 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1105 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001106 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001107 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1108 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1109 if (type == DataType::Type::kFloat32) {
1110 __ MovS(FTMP, f2);
1111 __ MovS(f2, f1);
1112 __ MovS(f1, FTMP);
1113 } else {
1114 DCHECK_EQ(type, DataType::Type::kFloat64);
1115 __ MovD(FTMP, f2);
1116 __ MovD(f2, f1);
1117 __ MovD(f1, FTMP);
1118 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001119 }
1120 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1121 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1122 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001123 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001124 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1125 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001126 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001127 __ Move(TMP, r2);
1128 __ Mfc1(r2, f1);
1129 __ Mtc1(TMP, f1);
1130 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1131 // Swap 2 GPR register pairs.
1132 Register r1 = loc1.AsRegisterPairLow<Register>();
1133 Register r2 = loc2.AsRegisterPairLow<Register>();
1134 __ Move(TMP, r2);
1135 __ Move(r2, r1);
1136 __ Move(r1, TMP);
1137 r1 = loc1.AsRegisterPairHigh<Register>();
1138 r2 = loc2.AsRegisterPairHigh<Register>();
1139 __ Move(TMP, r2);
1140 __ Move(r2, r1);
1141 __ Move(r1, TMP);
1142 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1143 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1144 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001146 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1147 : loc2.AsFpuRegister<FRegister>();
1148 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1149 : loc2.AsRegisterPairLow<Register>();
1150 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1151 : loc2.AsRegisterPairHigh<Register>();
1152 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1153 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1154 // unpredictable and the following mfch1 will fail.
1155 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001156 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001157 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001158 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001159 __ Move(r2_l, TMP);
1160 __ Move(r2_h, AT);
1161 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1162 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1163 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1164 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001165 } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) {
1166 ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
David Brazdilcc0f3112016-01-28 17:14:52 +00001167 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1168 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001169 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1170 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001171 __ Move(TMP, reg);
1172 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1173 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1174 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1175 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1176 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1177 : loc2.AsRegisterPairLow<Register>();
1178 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1179 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001180 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001181 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1182 : loc2.GetHighStackIndex(kMipsWordSize);
1183 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001184 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001185 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001186 __ Move(TMP, reg_h);
1187 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1188 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001189 } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) ||
1190 (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) {
1191 Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2;
1192 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1193 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(fp_loc));
1194 __ LoadQFromOffset(fp_loc.AsFpuRegister<FRegister>(), SP, offset);
1195 __ StoreQToOffset(FTMP, SP, offset);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001196 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1197 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1198 : loc2.AsFpuRegister<FRegister>();
1199 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001200 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001201 __ MovS(FTMP, reg);
1202 __ LoadSFromOffset(reg, SP, offset);
1203 __ StoreSToOffset(FTMP, SP, offset);
1204 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001205 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001206 __ MovD(FTMP, reg);
1207 __ LoadDFromOffset(reg, SP, offset);
1208 __ StoreDToOffset(FTMP, SP, offset);
1209 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210 } else {
1211 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1212 }
1213}
1214
1215void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1216 __ Pop(static_cast<Register>(reg));
1217}
1218
1219void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1220 __ Push(static_cast<Register>(reg));
1221}
1222
1223void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1224 // Allocate a scratch register other than TMP, if available.
1225 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1226 // automatically unspilled when the scratch scope object is destroyed).
1227 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1228 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001229 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001230 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1231 __ LoadFromOffset(kLoadWord,
1232 Register(ensure_scratch.GetRegister()),
1233 SP,
1234 index1 + stack_offset);
1235 __ LoadFromOffset(kLoadWord,
1236 TMP,
1237 SP,
1238 index2 + stack_offset);
1239 __ StoreToOffset(kStoreWord,
1240 Register(ensure_scratch.GetRegister()),
1241 SP,
1242 index2 + stack_offset);
1243 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1244 }
1245}
1246
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001247void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) {
1248 __ LoadQFromOffset(FTMP, SP, index1);
1249 __ LoadQFromOffset(FTMP2, SP, index2);
1250 __ StoreQToOffset(FTMP, SP, index2);
1251 __ StoreQToOffset(FTMP2, SP, index1);
1252}
1253
Alexey Frunze73296a72016-06-03 22:51:46 -07001254void CodeGeneratorMIPS::ComputeSpillMask() {
1255 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1256 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1257 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1258 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1259 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1260 // within the stack frame.
1261 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1262 core_spill_mask_ |= (1 << ZERO);
1263 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001264}
1265
1266bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001267 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001268 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1269 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1270 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001271 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001272}
1273
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001274static dwarf::Reg DWARFReg(Register reg) {
1275 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1276}
1277
1278// TODO: mapping of floating-point registers to DWARF.
1279
1280void CodeGeneratorMIPS::GenerateFrameEntry() {
1281 __ Bind(&frame_entry_label_);
1282
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001283 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01001284 __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
1285 __ Addiu(TMP, TMP, 1);
1286 __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001287 }
1288
Vladimir Marko33bff252017-11-01 14:35:42 +00001289 bool do_overflow_check =
1290 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001291
1292 if (do_overflow_check) {
1293 __ LoadFromOffset(kLoadWord,
1294 ZERO,
1295 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001296 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 RecordPcInfo(nullptr, 0);
1298 }
1299
1300 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001301 CHECK_EQ(fpu_spill_mask_, 0u);
1302 CHECK_EQ(core_spill_mask_, 1u << RA);
1303 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304 return;
1305 }
1306
1307 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001308 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1309 LOG(FATAL) << "Stack frame larger than "
1310 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311 }
1312
1313 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001314
Alexey Frunze73296a72016-06-03 22:51:46 -07001315 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316 __ IncreaseFrameSize(ofs);
1317
Alexey Frunze73296a72016-06-03 22:51:46 -07001318 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1319 Register reg = static_cast<Register>(MostSignificantBit(mask));
1320 mask ^= 1u << reg;
1321 ofs -= kMipsWordSize;
1322 // The ZERO register is only included for alignment.
1323 if (reg != ZERO) {
1324 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001325 __ cfi().RelOffset(DWARFReg(reg), ofs);
1326 }
1327 }
1328
Alexey Frunze73296a72016-06-03 22:51:46 -07001329 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1330 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1331 mask ^= 1u << reg;
1332 ofs -= kMipsDoublewordSize;
1333 __ StoreDToOffset(reg, SP, ofs);
1334 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001335 }
1336
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001337 // Save the current method if we need it. Note that we do not
1338 // do this in HCurrentMethod, as the instruction might have been removed
1339 // in the SSA graph.
1340 if (RequiresCurrentMethod()) {
1341 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1342 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001343
1344 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1345 // Initialize should deoptimize flag to 0.
1346 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1347 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001348}
1349
1350void CodeGeneratorMIPS::GenerateFrameExit() {
1351 __ cfi().RememberState();
1352
1353 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001354 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355
Alexey Frunze73296a72016-06-03 22:51:46 -07001356 // For better instruction scheduling restore RA before other registers.
1357 uint32_t ofs = GetFrameSize();
1358 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1359 Register reg = static_cast<Register>(MostSignificantBit(mask));
1360 mask ^= 1u << reg;
1361 ofs -= kMipsWordSize;
1362 // The ZERO register is only included for alignment.
1363 if (reg != ZERO) {
1364 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001365 __ cfi().Restore(DWARFReg(reg));
1366 }
1367 }
1368
Alexey Frunze73296a72016-06-03 22:51:46 -07001369 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1370 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1371 mask ^= 1u << reg;
1372 ofs -= kMipsDoublewordSize;
1373 __ LoadDFromOffset(reg, SP, ofs);
1374 // TODO: __ cfi().Restore(DWARFReg(reg));
1375 }
1376
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001377 size_t frame_size = GetFrameSize();
1378 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1379 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1380 bool reordering = __ SetReorder(false);
1381 if (exchange) {
1382 __ Jr(RA);
1383 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1384 } else {
1385 __ DecreaseFrameSize(frame_size);
1386 __ Jr(RA);
1387 __ Nop(); // In delay slot.
1388 }
1389 __ SetReorder(reordering);
1390 } else {
1391 __ Jr(RA);
1392 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001393 }
1394
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001395 __ cfi().RestoreState();
1396 __ cfi().DefCFAOffset(GetFrameSize());
1397}
1398
1399void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1400 __ Bind(GetLabelOf(block));
1401}
1402
Lena Djokicca8c2952017-05-29 11:31:46 +02001403VectorRegister VectorRegisterFrom(Location location) {
1404 DCHECK(location.IsFpuRegister());
1405 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1406}
1407
Lena Djokic8098da92017-06-28 12:07:50 +02001408void CodeGeneratorMIPS::MoveLocation(Location destination,
1409 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001410 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 if (source.Equals(destination)) {
1412 return;
1413 }
1414
Lena Djokic8098da92017-06-28 12:07:50 +02001415 if (source.IsConstant()) {
1416 MoveConstant(destination, source.GetConstant());
1417 } else {
1418 if (destination.IsRegister()) {
1419 if (source.IsRegister()) {
1420 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1421 } else if (source.IsFpuRegister()) {
1422 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1423 } else {
1424 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001425 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001426 }
1427 } else if (destination.IsRegisterPair()) {
1428 if (source.IsRegisterPair()) {
1429 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1430 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1431 } else if (source.IsFpuRegister()) {
1432 Register dst_high = destination.AsRegisterPairHigh<Register>();
1433 Register dst_low = destination.AsRegisterPairLow<Register>();
1434 FRegister src = source.AsFpuRegister<FRegister>();
1435 __ Mfc1(dst_low, src);
1436 __ MoveFromFpuHigh(dst_high, src);
1437 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001438 DCHECK(source.IsDoubleStackSlot())
1439 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001440 int32_t off = source.GetStackIndex();
1441 Register r = destination.AsRegisterPairLow<Register>();
1442 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1443 }
1444 } else if (destination.IsFpuRegister()) {
1445 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001446 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001447 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1448 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001450 FRegister dst = destination.AsFpuRegister<FRegister>();
1451 Register src_high = source.AsRegisterPairHigh<Register>();
1452 Register src_low = source.AsRegisterPairLow<Register>();
1453 __ Mtc1(src_low, dst);
1454 __ MoveToFpuHigh(src_high, dst);
1455 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001456 if (GetGraph()->HasSIMD()) {
1457 __ MoveV(VectorRegisterFrom(destination),
1458 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001459 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001461 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1462 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001464 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1465 }
Lena Djokic8098da92017-06-28 12:07:50 +02001466 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001467 } else if (source.IsSIMDStackSlot()) {
1468 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001469 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001470 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001471 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1472 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001474 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1475 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1476 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001477 } else if (destination.IsSIMDStackSlot()) {
1478 if (source.IsFpuRegister()) {
1479 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1480 } else {
1481 DCHECK(source.IsSIMDStackSlot());
1482 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1483 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1484 }
Lena Djokic8098da92017-06-28 12:07:50 +02001485 } else if (destination.IsDoubleStackSlot()) {
1486 int32_t dst_offset = destination.GetStackIndex();
1487 if (source.IsRegisterPair()) {
1488 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1489 } else if (source.IsFpuRegister()) {
1490 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1491 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001492 DCHECK(source.IsDoubleStackSlot())
1493 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001494 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1495 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1496 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1497 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1498 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001499 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001500 DCHECK(destination.IsStackSlot()) << destination;
1501 int32_t dst_offset = destination.GetStackIndex();
1502 if (source.IsRegister()) {
1503 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1504 } else if (source.IsFpuRegister()) {
1505 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1506 } else {
1507 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1508 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1509 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1510 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001511 }
1512 }
1513}
1514
1515void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1516 if (c->IsIntConstant() || c->IsNullConstant()) {
1517 // Move 32 bit constant.
1518 int32_t value = GetInt32ValueOf(c);
1519 if (destination.IsRegister()) {
1520 Register dst = destination.AsRegister<Register>();
1521 __ LoadConst32(dst, value);
1522 } else {
1523 DCHECK(destination.IsStackSlot())
1524 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001525 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001526 }
1527 } else if (c->IsLongConstant()) {
1528 // Move 64 bit constant.
1529 int64_t value = GetInt64ValueOf(c);
1530 if (destination.IsRegisterPair()) {
1531 Register r_h = destination.AsRegisterPairHigh<Register>();
1532 Register r_l = destination.AsRegisterPairLow<Register>();
1533 __ LoadConst64(r_h, r_l, value);
1534 } else {
1535 DCHECK(destination.IsDoubleStackSlot())
1536 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001537 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001538 }
1539 } else if (c->IsFloatConstant()) {
1540 // Move 32 bit float constant.
1541 int32_t value = GetInt32ValueOf(c);
1542 if (destination.IsFpuRegister()) {
1543 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1544 } else {
1545 DCHECK(destination.IsStackSlot())
1546 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001547 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548 }
1549 } else {
1550 // Move 64 bit double constant.
1551 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1552 int64_t value = GetInt64ValueOf(c);
1553 if (destination.IsFpuRegister()) {
1554 FRegister fd = destination.AsFpuRegister<FRegister>();
1555 __ LoadDConst64(fd, value, TMP);
1556 } else {
1557 DCHECK(destination.IsDoubleStackSlot())
1558 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001559 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001560 }
1561 }
1562}
1563
1564void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1565 DCHECK(destination.IsRegister());
1566 Register dst = destination.AsRegister<Register>();
1567 __ LoadConst32(dst, value);
1568}
1569
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001570void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1571 if (location.IsRegister()) {
1572 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001573 } else if (location.IsRegisterPair()) {
1574 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1575 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576 } else {
1577 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1578 }
1579}
1580
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001581template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001582inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1583 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001584 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001585 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001586 const DexFile* dex_file = info.target_dex_file;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001587 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001588 DCHECK(info.label.IsBound());
1589 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001590 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1591 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001592 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1593 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1594 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001595 : __ GetPcRelBaseLabelLocation();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001596 linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001597 }
1598}
1599
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001600void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001601 DCHECK(linker_patches->empty());
1602 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001603 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001604 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001605 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001606 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001607 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001608 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001609 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001610 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001611 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001612 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001613 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001614 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001615 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001616 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001617 } else {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001618 DCHECK(boot_image_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001619 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001620 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001621 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001622 boot_image_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001623 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001624 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1625 method_bss_entry_patches_, linker_patches);
1626 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1627 type_bss_entry_patches_, linker_patches);
1628 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1629 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001630 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001631}
1632
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001633CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001634 MethodReference target_method,
1635 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001636 return NewPcRelativePatch(
1637 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001638}
1639
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001640CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001641 MethodReference target_method,
1642 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001643 return NewPcRelativePatch(
1644 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001645}
1646
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001647CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001648 const DexFile& dex_file,
1649 dex::TypeIndex type_index,
1650 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001651 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001652}
1653
Vladimir Marko1998cd02017-01-13 13:02:58 +00001654CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001655 const DexFile& dex_file,
1656 dex::TypeIndex type_index,
1657 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001658 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001659}
1660
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001661CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001662 const DexFile& dex_file,
1663 dex::StringIndex string_index,
1664 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001665 return NewPcRelativePatch(
1666 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001667}
1668
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001669CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1670 const DexFile& dex_file,
1671 dex::StringIndex string_index,
1672 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001673 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001674}
1675
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001676CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001677 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001678 uint32_t offset_or_index,
1679 const PcRelativePatchInfo* info_high,
1680 ArenaDeque<PcRelativePatchInfo>* patches) {
1681 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001682 return &patches->back();
1683}
1684
Alexey Frunze06a46c42016-07-19 15:00:40 -07001685Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1686 return map->GetOrCreate(
1687 value,
1688 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1689}
1690
Alexey Frunze06a46c42016-07-19 15:00:40 -07001691Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001692 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001693}
1694
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001695void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001696 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001697 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001698 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001699 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001700 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001701 if (GetInstructionSetFeatures().IsR6()) {
1702 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001703 __ Bind(&info_high->label);
1704 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001705 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001706 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001707 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001708 } else {
1709 // If base is ZERO, emit NAL to obtain the actual base.
1710 if (base == ZERO) {
1711 // Generate a dummy PC-relative call to obtain PC.
1712 __ Nal();
1713 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001714 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001715 __ Lui(out, /* placeholder */ 0x1234);
1716 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1717 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1718 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001719 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001720 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001721 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001722 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001723 __ Addu(out, out, (base == ZERO) ? RA : base);
1724 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001725 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001726 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001727}
1728
Alexey Frunze627c1a02017-01-30 19:28:14 -08001729CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1730 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001731 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001732 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001733 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1734 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001735 return &jit_string_patches_.back();
1736}
1737
1738CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1739 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001740 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001741 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001742 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1743 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001744 return &jit_class_patches_.back();
1745}
1746
1747void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1748 const uint8_t* roots_data,
1749 const CodeGeneratorMIPS::JitPatchInfo& info,
1750 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001751 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1752 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001753 uintptr_t address =
1754 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1755 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1756 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001757 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1758 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1759 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1760 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001761 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001762 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1763 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001764 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001765 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001766 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1767 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001768 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001769 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1770 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001771}
1772
1773void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1774 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001775 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1776 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001777 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001778 }
1779 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001780 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1781 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001782 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001783 }
1784}
1785
Goran Jakovljevice114da22016-12-26 14:21:43 +01001786void CodeGeneratorMIPS::MarkGCCard(Register object,
1787 Register value,
1788 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001789 MipsLabel done;
1790 Register card = AT;
1791 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001792 if (value_can_be_null) {
1793 __ Beqz(value, &done);
1794 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001795 __ LoadFromOffset(kLoadWord,
1796 card,
1797 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001798 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001799 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1800 __ Addu(temp, card, temp);
1801 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001802 if (value_can_be_null) {
1803 __ Bind(&done);
1804 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001805}
1806
David Brazdil58282f42016-01-14 12:45:10 +00001807void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001808 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1809 blocked_core_registers_[ZERO] = true;
1810 blocked_core_registers_[K0] = true;
1811 blocked_core_registers_[K1] = true;
1812 blocked_core_registers_[GP] = true;
1813 blocked_core_registers_[SP] = true;
1814 blocked_core_registers_[RA] = true;
1815
1816 // AT and TMP(T8) are used as temporary/scratch registers
1817 // (similar to how AT is used by MIPS assemblers).
1818 blocked_core_registers_[AT] = true;
1819 blocked_core_registers_[TMP] = true;
1820 blocked_fpu_registers_[FTMP] = true;
1821
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001822 if (GetInstructionSetFeatures().HasMsa()) {
1823 // To be used just for MSA instructions.
1824 blocked_fpu_registers_[FTMP2] = true;
1825 }
1826
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001827 // Reserve suspend and thread registers.
1828 blocked_core_registers_[S0] = true;
1829 blocked_core_registers_[TR] = true;
1830
1831 // Reserve T9 for function calls
1832 blocked_core_registers_[T9] = true;
1833
1834 // Reserve odd-numbered FPU registers.
1835 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1836 blocked_fpu_registers_[i] = true;
1837 }
1838
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001839 if (GetGraph()->IsDebuggable()) {
1840 // Stubs do not save callee-save floating point registers. If the graph
1841 // is debuggable, we need to deal with these registers differently. For
1842 // now, just block them.
1843 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1844 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1845 }
1846 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001847}
1848
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001849size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1850 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1851 return kMipsWordSize;
1852}
1853
1854size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1855 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1856 return kMipsWordSize;
1857}
1858
1859size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001860 if (GetGraph()->HasSIMD()) {
1861 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1862 } else {
1863 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1864 }
1865 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001866}
1867
1868size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001869 if (GetGraph()->HasSIMD()) {
1870 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1871 } else {
1872 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1873 }
1874 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001875}
1876
1877void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001878 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001879}
1880
1881void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001882 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001883}
1884
Serban Constantinescufca16662016-07-14 09:21:59 +01001885constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1886
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001887void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1888 HInstruction* instruction,
1889 uint32_t dex_pc,
1890 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001891 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001892 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1893 IsDirectEntrypoint(entrypoint));
1894 if (EntrypointRequiresStackMap(entrypoint)) {
1895 RecordPcInfo(instruction, dex_pc, slow_path);
1896 }
1897}
1898
1899void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1900 HInstruction* instruction,
1901 SlowPathCode* slow_path,
1902 bool direct) {
1903 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1904 GenerateInvokeRuntime(entry_point_offset, direct);
1905}
1906
1907void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001908 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001909 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001910 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001911 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912 // Reserve argument space on stack (for $a0-$a3) for
1913 // entrypoints that directly reference native implementations.
1914 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001915 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001916 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001917 } else {
1918 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001920 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001921}
1922
1923void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1924 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001925 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1926 const size_t status_byte_offset =
1927 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1928 constexpr uint32_t shifted_initialized_value =
1929 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1930
1931 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
1932 __ LoadConst32(AT, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001933 __ Bltu(TMP, AT, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1935 __ Sync(0);
1936 __ Bind(slow_path->GetExitLabel());
1937}
1938
1939void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1940 __ Sync(0); // Only stype 0 is supported.
1941}
1942
1943void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1944 HBasicBlock* successor) {
1945 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001946 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1947
1948 if (slow_path == nullptr) {
1949 slow_path =
1950 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1951 instruction->SetSlowPath(slow_path);
1952 codegen_->AddSlowPath(slow_path);
1953 if (successor != nullptr) {
1954 DCHECK(successor->IsLoopHeader());
1955 }
1956 } else {
1957 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1958 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001959
1960 __ LoadFromOffset(kLoadUnsignedHalfword,
1961 TMP,
1962 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001963 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001964 if (successor == nullptr) {
1965 __ Bnez(TMP, slow_path->GetEntryLabel());
1966 __ Bind(slow_path->GetReturnLabel());
1967 } else {
1968 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1969 __ B(slow_path->GetEntryLabel());
1970 // slow_path will return to GetLabelOf(successor).
1971 }
1972}
1973
1974InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1975 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001976 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001977 assembler_(codegen->GetAssembler()),
1978 codegen_(codegen) {}
1979
1980void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1981 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001982 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001983 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01001984 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001985 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001986 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001987 locations->SetInAt(0, Location::RequiresRegister());
1988 HInstruction* right = instruction->InputAt(1);
1989 bool can_use_imm = false;
1990 if (right->IsConstant()) {
1991 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1992 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1993 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001994 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001995 DCHECK(instruction->IsSub() || instruction->IsAdd());
1996 if (instruction->IsSub()) {
1997 imm = -imm;
1998 }
1999 if (isR6) {
2000 bool single_use = right->GetUses().HasExactlyOneElement();
2001 int16_t imm_high = High16Bits(imm);
2002 int16_t imm_low = Low16Bits(imm);
2003 if (imm_low < 0) {
2004 imm_high += 1;
2005 }
2006 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2007 } else {
2008 can_use_imm = IsInt<16>(imm);
2009 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002010 }
2011 }
2012 if (can_use_imm)
2013 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2014 else
2015 locations->SetInAt(1, Location::RequiresRegister());
2016 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2017 break;
2018 }
2019
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002020 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002021 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002022 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2023 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 break;
2025 }
2026
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 case DataType::Type::kFloat32:
2028 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002029 DCHECK(instruction->IsAdd() || instruction->IsSub());
2030 locations->SetInAt(0, Location::RequiresFpuRegister());
2031 locations->SetInAt(1, Location::RequiresFpuRegister());
2032 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2033 break;
2034
2035 default:
2036 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2037 }
2038}
2039
2040void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002041 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002042 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002043 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002044
2045 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002046 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002047 Register dst = locations->Out().AsRegister<Register>();
2048 Register lhs = locations->InAt(0).AsRegister<Register>();
2049 Location rhs_location = locations->InAt(1);
2050
2051 Register rhs_reg = ZERO;
2052 int32_t rhs_imm = 0;
2053 bool use_imm = rhs_location.IsConstant();
2054 if (use_imm) {
2055 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2056 } else {
2057 rhs_reg = rhs_location.AsRegister<Register>();
2058 }
2059
2060 if (instruction->IsAnd()) {
2061 if (use_imm)
2062 __ Andi(dst, lhs, rhs_imm);
2063 else
2064 __ And(dst, lhs, rhs_reg);
2065 } else if (instruction->IsOr()) {
2066 if (use_imm)
2067 __ Ori(dst, lhs, rhs_imm);
2068 else
2069 __ Or(dst, lhs, rhs_reg);
2070 } else if (instruction->IsXor()) {
2071 if (use_imm)
2072 __ Xori(dst, lhs, rhs_imm);
2073 else
2074 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002075 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002076 DCHECK(instruction->IsAdd() || instruction->IsSub());
2077 if (use_imm) {
2078 if (instruction->IsSub()) {
2079 rhs_imm = -rhs_imm;
2080 }
2081 if (IsInt<16>(rhs_imm)) {
2082 __ Addiu(dst, lhs, rhs_imm);
2083 } else {
2084 DCHECK(isR6);
2085 int16_t rhs_imm_high = High16Bits(rhs_imm);
2086 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2087 if (rhs_imm_low < 0) {
2088 rhs_imm_high += 1;
2089 }
2090 __ Aui(dst, lhs, rhs_imm_high);
2091 if (rhs_imm_low != 0) {
2092 __ Addiu(dst, dst, rhs_imm_low);
2093 }
2094 }
2095 } else if (instruction->IsAdd()) {
2096 __ Addu(dst, lhs, rhs_reg);
2097 } else {
2098 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002099 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002100 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002101 }
2102 break;
2103 }
2104
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002105 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002106 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2107 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2108 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2109 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002110 Location rhs_location = locations->InAt(1);
2111 bool use_imm = rhs_location.IsConstant();
2112 if (!use_imm) {
2113 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2114 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2115 if (instruction->IsAnd()) {
2116 __ And(dst_low, lhs_low, rhs_low);
2117 __ And(dst_high, lhs_high, rhs_high);
2118 } else if (instruction->IsOr()) {
2119 __ Or(dst_low, lhs_low, rhs_low);
2120 __ Or(dst_high, lhs_high, rhs_high);
2121 } else if (instruction->IsXor()) {
2122 __ Xor(dst_low, lhs_low, rhs_low);
2123 __ Xor(dst_high, lhs_high, rhs_high);
2124 } else if (instruction->IsAdd()) {
2125 if (lhs_low == rhs_low) {
2126 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2127 __ Slt(TMP, lhs_low, ZERO);
2128 __ Addu(dst_low, lhs_low, rhs_low);
2129 } else {
2130 __ Addu(dst_low, lhs_low, rhs_low);
2131 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2132 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2133 }
2134 __ Addu(dst_high, lhs_high, rhs_high);
2135 __ Addu(dst_high, dst_high, TMP);
2136 } else {
2137 DCHECK(instruction->IsSub());
2138 __ Sltu(TMP, lhs_low, rhs_low);
2139 __ Subu(dst_low, lhs_low, rhs_low);
2140 __ Subu(dst_high, lhs_high, rhs_high);
2141 __ Subu(dst_high, dst_high, TMP);
2142 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002143 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002144 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2145 if (instruction->IsOr()) {
2146 uint32_t low = Low32Bits(value);
2147 uint32_t high = High32Bits(value);
2148 if (IsUint<16>(low)) {
2149 if (dst_low != lhs_low || low != 0) {
2150 __ Ori(dst_low, lhs_low, low);
2151 }
2152 } else {
2153 __ LoadConst32(TMP, low);
2154 __ Or(dst_low, lhs_low, TMP);
2155 }
2156 if (IsUint<16>(high)) {
2157 if (dst_high != lhs_high || high != 0) {
2158 __ Ori(dst_high, lhs_high, high);
2159 }
2160 } else {
2161 if (high != low) {
2162 __ LoadConst32(TMP, high);
2163 }
2164 __ Or(dst_high, lhs_high, TMP);
2165 }
2166 } else if (instruction->IsXor()) {
2167 uint32_t low = Low32Bits(value);
2168 uint32_t high = High32Bits(value);
2169 if (IsUint<16>(low)) {
2170 if (dst_low != lhs_low || low != 0) {
2171 __ Xori(dst_low, lhs_low, low);
2172 }
2173 } else {
2174 __ LoadConst32(TMP, low);
2175 __ Xor(dst_low, lhs_low, TMP);
2176 }
2177 if (IsUint<16>(high)) {
2178 if (dst_high != lhs_high || high != 0) {
2179 __ Xori(dst_high, lhs_high, high);
2180 }
2181 } else {
2182 if (high != low) {
2183 __ LoadConst32(TMP, high);
2184 }
2185 __ Xor(dst_high, lhs_high, TMP);
2186 }
2187 } else if (instruction->IsAnd()) {
2188 uint32_t low = Low32Bits(value);
2189 uint32_t high = High32Bits(value);
2190 if (IsUint<16>(low)) {
2191 __ Andi(dst_low, lhs_low, low);
2192 } else if (low != 0xFFFFFFFF) {
2193 __ LoadConst32(TMP, low);
2194 __ And(dst_low, lhs_low, TMP);
2195 } else if (dst_low != lhs_low) {
2196 __ Move(dst_low, lhs_low);
2197 }
2198 if (IsUint<16>(high)) {
2199 __ Andi(dst_high, lhs_high, high);
2200 } else if (high != 0xFFFFFFFF) {
2201 if (high != low) {
2202 __ LoadConst32(TMP, high);
2203 }
2204 __ And(dst_high, lhs_high, TMP);
2205 } else if (dst_high != lhs_high) {
2206 __ Move(dst_high, lhs_high);
2207 }
2208 } else {
2209 if (instruction->IsSub()) {
2210 value = -value;
2211 } else {
2212 DCHECK(instruction->IsAdd());
2213 }
2214 int32_t low = Low32Bits(value);
2215 int32_t high = High32Bits(value);
2216 if (IsInt<16>(low)) {
2217 if (dst_low != lhs_low || low != 0) {
2218 __ Addiu(dst_low, lhs_low, low);
2219 }
2220 if (low != 0) {
2221 __ Sltiu(AT, dst_low, low);
2222 }
2223 } else {
2224 __ LoadConst32(TMP, low);
2225 __ Addu(dst_low, lhs_low, TMP);
2226 __ Sltu(AT, dst_low, TMP);
2227 }
2228 if (IsInt<16>(high)) {
2229 if (dst_high != lhs_high || high != 0) {
2230 __ Addiu(dst_high, lhs_high, high);
2231 }
2232 } else {
2233 if (high != low) {
2234 __ LoadConst32(TMP, high);
2235 }
2236 __ Addu(dst_high, lhs_high, TMP);
2237 }
2238 if (low != 0) {
2239 __ Addu(dst_high, dst_high, AT);
2240 }
2241 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002242 }
2243 break;
2244 }
2245
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002246 case DataType::Type::kFloat32:
2247 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002248 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2249 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2250 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2251 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002252 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002253 __ AddS(dst, lhs, rhs);
2254 } else {
2255 __ AddD(dst, lhs, rhs);
2256 }
2257 } else {
2258 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002259 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002260 __ SubS(dst, lhs, rhs);
2261 } else {
2262 __ SubD(dst, lhs, rhs);
2263 }
2264 }
2265 break;
2266 }
2267
2268 default:
2269 LOG(FATAL) << "Unexpected binary operation type " << type;
2270 }
2271}
2272
2273void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002274 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002275
Vladimir Markoca6fff82017-10-03 14:49:14 +01002276 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002277 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002278 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002279 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002280 locations->SetInAt(0, Location::RequiresRegister());
2281 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2282 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2283 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002284 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002285 locations->SetInAt(0, Location::RequiresRegister());
2286 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2287 locations->SetOut(Location::RequiresRegister());
2288 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002289 default:
2290 LOG(FATAL) << "Unexpected shift type " << type;
2291 }
2292}
2293
2294static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2295
2296void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002297 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002298 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002299 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002300
2301 Location rhs_location = locations->InAt(1);
2302 bool use_imm = rhs_location.IsConstant();
2303 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2304 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002305 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002306 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002307 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002308 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2309 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002310
2311 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002312 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002313 Register dst = locations->Out().AsRegister<Register>();
2314 Register lhs = locations->InAt(0).AsRegister<Register>();
2315 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002316 if (shift_value == 0) {
2317 if (dst != lhs) {
2318 __ Move(dst, lhs);
2319 }
2320 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002321 __ Sll(dst, lhs, shift_value);
2322 } else if (instr->IsShr()) {
2323 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002324 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002325 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002326 } else {
2327 if (has_ins_rotr) {
2328 __ Rotr(dst, lhs, shift_value);
2329 } else {
2330 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2331 __ Srl(dst, lhs, shift_value);
2332 __ Or(dst, dst, TMP);
2333 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002334 }
2335 } else {
2336 if (instr->IsShl()) {
2337 __ Sllv(dst, lhs, rhs_reg);
2338 } else if (instr->IsShr()) {
2339 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002340 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002341 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002342 } else {
2343 if (has_ins_rotr) {
2344 __ Rotrv(dst, lhs, rhs_reg);
2345 } else {
2346 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002347 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2348 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2349 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2350 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2351 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002352 __ Sllv(TMP, lhs, TMP);
2353 __ Srlv(dst, lhs, rhs_reg);
2354 __ Or(dst, dst, TMP);
2355 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002356 }
2357 }
2358 break;
2359 }
2360
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002361 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002362 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2363 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2364 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2365 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2366 if (use_imm) {
2367 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002368 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002369 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002370 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002371 if (instr->IsShl()) {
2372 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2373 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2374 __ Sll(dst_low, lhs_low, shift_value);
2375 } else if (instr->IsShr()) {
2376 __ Srl(dst_low, lhs_low, shift_value);
2377 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2378 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002379 } else if (instr->IsUShr()) {
2380 __ Srl(dst_low, lhs_low, shift_value);
2381 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2382 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002383 } else {
2384 __ Srl(dst_low, lhs_low, shift_value);
2385 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2386 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002387 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002388 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002389 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002390 if (instr->IsShl()) {
2391 __ Sll(dst_low, lhs_low, shift_value);
2392 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2393 __ Sll(dst_high, lhs_high, shift_value);
2394 __ Or(dst_high, dst_high, TMP);
2395 } else if (instr->IsShr()) {
2396 __ Sra(dst_high, lhs_high, shift_value);
2397 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2398 __ Srl(dst_low, lhs_low, shift_value);
2399 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002400 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002401 __ Srl(dst_high, lhs_high, shift_value);
2402 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2403 __ Srl(dst_low, lhs_low, shift_value);
2404 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002405 } else {
2406 __ Srl(TMP, lhs_low, shift_value);
2407 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2408 __ Or(dst_low, dst_low, TMP);
2409 __ Srl(TMP, lhs_high, shift_value);
2410 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2411 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002412 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002413 }
2414 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002415 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002416 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002417 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002418 __ Move(dst_low, ZERO);
2419 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002420 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002422 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002423 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002424 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002425 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002426 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002427 // 64-bit rotation by 32 is just a swap.
2428 __ Move(dst_low, lhs_high);
2429 __ Move(dst_high, lhs_low);
2430 } else {
2431 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002432 __ Srl(dst_low, lhs_high, shift_value_high);
2433 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2434 __ Srl(dst_high, lhs_low, shift_value_high);
2435 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002436 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002437 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2438 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002439 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002440 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2441 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002442 __ Or(dst_high, dst_high, TMP);
2443 }
2444 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002445 }
2446 }
2447 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002448 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002449 MipsLabel done;
2450 if (instr->IsShl()) {
2451 __ Sllv(dst_low, lhs_low, rhs_reg);
2452 __ Nor(AT, ZERO, rhs_reg);
2453 __ Srl(TMP, lhs_low, 1);
2454 __ Srlv(TMP, TMP, AT);
2455 __ Sllv(dst_high, lhs_high, rhs_reg);
2456 __ Or(dst_high, dst_high, TMP);
2457 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002458 if (isR6) {
2459 __ Beqzc(TMP, &done, /* is_bare */ true);
2460 __ Move(dst_high, dst_low);
2461 __ Move(dst_low, ZERO);
2462 } else {
2463 __ Movn(dst_high, dst_low, TMP);
2464 __ Movn(dst_low, ZERO, TMP);
2465 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002466 } else if (instr->IsShr()) {
2467 __ Srav(dst_high, lhs_high, rhs_reg);
2468 __ Nor(AT, ZERO, rhs_reg);
2469 __ Sll(TMP, lhs_high, 1);
2470 __ Sllv(TMP, TMP, AT);
2471 __ Srlv(dst_low, lhs_low, rhs_reg);
2472 __ Or(dst_low, dst_low, TMP);
2473 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002474 if (isR6) {
2475 __ Beqzc(TMP, &done, /* is_bare */ true);
2476 __ Move(dst_low, dst_high);
2477 __ Sra(dst_high, dst_high, 31);
2478 } else {
2479 __ Sra(AT, dst_high, 31);
2480 __ Movn(dst_low, dst_high, TMP);
2481 __ Movn(dst_high, AT, TMP);
2482 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002483 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002484 __ Srlv(dst_high, lhs_high, rhs_reg);
2485 __ Nor(AT, ZERO, rhs_reg);
2486 __ Sll(TMP, lhs_high, 1);
2487 __ Sllv(TMP, TMP, AT);
2488 __ Srlv(dst_low, lhs_low, rhs_reg);
2489 __ Or(dst_low, dst_low, TMP);
2490 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002491 if (isR6) {
2492 __ Beqzc(TMP, &done, /* is_bare */ true);
2493 __ Move(dst_low, dst_high);
2494 __ Move(dst_high, ZERO);
2495 } else {
2496 __ Movn(dst_low, dst_high, TMP);
2497 __ Movn(dst_high, ZERO, TMP);
2498 }
2499 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002500 __ Nor(AT, ZERO, rhs_reg);
2501 __ Srlv(TMP, lhs_low, rhs_reg);
2502 __ Sll(dst_low, lhs_high, 1);
2503 __ Sllv(dst_low, dst_low, AT);
2504 __ Or(dst_low, dst_low, TMP);
2505 __ Srlv(TMP, lhs_high, rhs_reg);
2506 __ Sll(dst_high, lhs_low, 1);
2507 __ Sllv(dst_high, dst_high, AT);
2508 __ Or(dst_high, dst_high, TMP);
2509 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002510 if (isR6) {
2511 __ Beqzc(TMP, &done, /* is_bare */ true);
2512 __ Move(TMP, dst_high);
2513 __ Move(dst_high, dst_low);
2514 __ Move(dst_low, TMP);
2515 } else {
2516 __ Movn(AT, dst_high, TMP);
2517 __ Movn(dst_high, dst_low, TMP);
2518 __ Movn(dst_low, AT, TMP);
2519 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002520 }
2521 __ Bind(&done);
2522 }
2523 break;
2524 }
2525
2526 default:
2527 LOG(FATAL) << "Unexpected shift operation type " << type;
2528 }
2529}
2530
2531void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2532 HandleBinaryOp(instruction);
2533}
2534
2535void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2536 HandleBinaryOp(instruction);
2537}
2538
2539void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2540 HandleBinaryOp(instruction);
2541}
2542
2543void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2544 HandleBinaryOp(instruction);
2545}
2546
2547void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002548 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002549 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002550 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002551 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002552 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2553 object_array_get_with_read_barrier
2554 ? LocationSummary::kCallOnSlowPath
2555 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002556 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2557 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2558 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002559 locations->SetInAt(0, Location::RequiresRegister());
2560 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002561 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002562 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2563 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002564 // The output overlaps in the case of an object array get with
2565 // read barriers enabled: we do not want the move to overwrite the
2566 // array's location, as we need it to emit the read barrier.
2567 locations->SetOut(Location::RequiresRegister(),
2568 object_array_get_with_read_barrier
2569 ? Location::kOutputOverlap
2570 : Location::kNoOutputOverlap);
2571 }
2572 // We need a temporary register for the read barrier marking slow
2573 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2574 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002575 bool temp_needed = instruction->GetIndex()->IsConstant()
2576 ? !kBakerReadBarrierThunksEnableForFields
2577 : !kBakerReadBarrierThunksEnableForArrays;
2578 if (temp_needed) {
2579 locations->AddTemp(Location::RequiresRegister());
2580 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002581 }
2582}
2583
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002584static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2585 auto null_checker = [codegen, instruction]() {
2586 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002587 };
2588 return null_checker;
2589}
2590
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002591void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2592 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002593 Location obj_loc = locations->InAt(0);
2594 Register obj = obj_loc.AsRegister<Register>();
2595 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002597 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002598 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002599
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002600 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002601 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2602 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002603 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002604 case DataType::Type::kBool:
2605 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002606 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607 if (index.IsConstant()) {
2608 size_t offset =
2609 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002610 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002611 } else {
2612 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002613 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002614 }
2615 break;
2616 }
2617
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002618 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002619 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002620 if (index.IsConstant()) {
2621 size_t offset =
2622 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002623 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002624 } else {
2625 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002626 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002627 }
2628 break;
2629 }
2630
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002631 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002632 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002633 if (maybe_compressed_char_at) {
2634 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2635 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2636 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2637 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2638 "Expecting 0=compressed, 1=uncompressed");
2639 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002640 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002641 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2642 if (maybe_compressed_char_at) {
2643 MipsLabel uncompressed_load, done;
2644 __ Bnez(TMP, &uncompressed_load);
2645 __ LoadFromOffset(kLoadUnsignedByte,
2646 out,
2647 obj,
2648 data_offset + (const_index << TIMES_1));
2649 __ B(&done);
2650 __ Bind(&uncompressed_load);
2651 __ LoadFromOffset(kLoadUnsignedHalfword,
2652 out,
2653 obj,
2654 data_offset + (const_index << TIMES_2));
2655 __ Bind(&done);
2656 } else {
2657 __ LoadFromOffset(kLoadUnsignedHalfword,
2658 out,
2659 obj,
2660 data_offset + (const_index << TIMES_2),
2661 null_checker);
2662 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002663 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002664 Register index_reg = index.AsRegister<Register>();
2665 if (maybe_compressed_char_at) {
2666 MipsLabel uncompressed_load, done;
2667 __ Bnez(TMP, &uncompressed_load);
2668 __ Addu(TMP, obj, index_reg);
2669 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2670 __ B(&done);
2671 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002672 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002673 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2674 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002675 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2676 __ Addu(TMP, index_reg, obj);
2677 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002678 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002679 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002680 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2681 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002682 }
2683 break;
2684 }
2685
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002686 case DataType::Type::kInt16: {
2687 Register out = out_loc.AsRegister<Register>();
2688 if (index.IsConstant()) {
2689 size_t offset =
2690 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2691 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002692 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2693 __ Addu(TMP, index.AsRegister<Register>(), obj);
2694 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002695 } else {
2696 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2697 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2698 }
2699 break;
2700 }
2701
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002702 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002703 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002704 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002705 if (index.IsConstant()) {
2706 size_t offset =
2707 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002708 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002709 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2710 __ Addu(TMP, index.AsRegister<Register>(), obj);
2711 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002712 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002713 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002714 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002715 }
2716 break;
2717 }
2718
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002719 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002720 static_assert(
2721 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2722 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2723 // /* HeapReference<Object> */ out =
2724 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2725 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002726 bool temp_needed = index.IsConstant()
2727 ? !kBakerReadBarrierThunksEnableForFields
2728 : !kBakerReadBarrierThunksEnableForArrays;
2729 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002730 // Note that a potential implicit null check is handled in this
2731 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002732 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2733 if (index.IsConstant()) {
2734 // Array load with a constant index can be treated as a field load.
2735 size_t offset =
2736 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2737 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2738 out_loc,
2739 obj,
2740 offset,
2741 temp,
2742 /* needs_null_check */ false);
2743 } else {
2744 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2745 out_loc,
2746 obj,
2747 data_offset,
2748 index,
2749 temp,
2750 /* needs_null_check */ false);
2751 }
Alexey Frunze15958152017-02-09 19:08:30 -08002752 } else {
2753 Register out = out_loc.AsRegister<Register>();
2754 if (index.IsConstant()) {
2755 size_t offset =
2756 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2757 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2758 // If read barriers are enabled, emit read barriers other than
2759 // Baker's using a slow path (and also unpoison the loaded
2760 // reference, if heap poisoning is enabled).
2761 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2762 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002763 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002764 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2765 // If read barriers are enabled, emit read barriers other than
2766 // Baker's using a slow path (and also unpoison the loaded
2767 // reference, if heap poisoning is enabled).
2768 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2769 out_loc,
2770 out_loc,
2771 obj_loc,
2772 data_offset,
2773 index);
2774 }
2775 }
2776 break;
2777 }
2778
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002779 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002780 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002781 if (index.IsConstant()) {
2782 size_t offset =
2783 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002784 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002785 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2786 __ Addu(TMP, index.AsRegister<Register>(), obj);
2787 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002788 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002789 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002790 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791 }
2792 break;
2793 }
2794
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002795 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002796 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002797 if (index.IsConstant()) {
2798 size_t offset =
2799 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002800 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002801 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2802 __ Addu(TMP, index.AsRegister<Register>(), obj);
2803 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002804 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002805 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002806 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002807 }
2808 break;
2809 }
2810
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002811 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002812 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002813 if (index.IsConstant()) {
2814 size_t offset =
2815 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002816 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002817 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2818 __ Addu(TMP, index.AsRegister<Register>(), obj);
2819 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002821 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002822 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002823 }
2824 break;
2825 }
2826
Aart Bik66c158e2018-01-31 12:55:04 -08002827 case DataType::Type::kUint32:
2828 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002829 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002830 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2831 UNREACHABLE();
2832 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002833}
2834
2835void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002836 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002837 locations->SetInAt(0, Location::RequiresRegister());
2838 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2839}
2840
2841void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2842 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002843 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002844 Register obj = locations->InAt(0).AsRegister<Register>();
2845 Register out = locations->Out().AsRegister<Register>();
2846 __ LoadFromOffset(kLoadWord, out, obj, offset);
2847 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002848 // Mask out compression flag from String's array length.
2849 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2850 __ Srl(out, out, 1u);
2851 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002852}
2853
Alexey Frunzef58b2482016-09-02 22:14:06 -07002854Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2855 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2856 ? Location::ConstantLocation(instruction->AsConstant())
2857 : Location::RequiresRegister();
2858}
2859
2860Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2861 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2862 // We can store a non-zero float or double constant without first loading it into the FPU,
2863 // but we should only prefer this if the constant has a single use.
2864 if (instruction->IsConstant() &&
2865 (instruction->AsConstant()->IsZeroBitPattern() ||
2866 instruction->GetUses().HasExactlyOneElement())) {
2867 return Location::ConstantLocation(instruction->AsConstant());
2868 // Otherwise fall through and require an FPU register for the constant.
2869 }
2870 return Location::RequiresFpuRegister();
2871}
2872
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002873void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002875
2876 bool needs_write_barrier =
2877 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2878 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2879
Vladimir Markoca6fff82017-10-03 14:49:14 +01002880 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002881 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002882 may_need_runtime_call_for_type_check ?
2883 LocationSummary::kCallOnSlowPath :
2884 LocationSummary::kNoCall);
2885
2886 locations->SetInAt(0, Location::RequiresRegister());
2887 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002888 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002889 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002890 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002891 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2892 }
2893 if (needs_write_barrier) {
2894 // Temporary register for the write barrier.
2895 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002896 }
2897}
2898
2899void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2900 LocationSummary* locations = instruction->GetLocations();
2901 Register obj = locations->InAt(0).AsRegister<Register>();
2902 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002903 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002904 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002905 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002906 bool needs_write_barrier =
2907 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002908 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002909 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002910
2911 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002912 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002913 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002914 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002915 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002916 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002917 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002918 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002919 __ Addu(base_reg, obj, index.AsRegister<Register>());
2920 }
2921 if (value_location.IsConstant()) {
2922 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2923 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2924 } else {
2925 Register value = value_location.AsRegister<Register>();
2926 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002927 }
2928 break;
2929 }
2930
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002931 case DataType::Type::kUint16:
2932 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002933 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002934 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002935 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002936 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2937 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002938 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002939 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002940 }
2941 if (value_location.IsConstant()) {
2942 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2943 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2944 } else {
2945 Register value = value_location.AsRegister<Register>();
2946 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002947 }
2948 break;
2949 }
2950
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002951 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002952 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2953 if (index.IsConstant()) {
2954 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002955 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2956 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002957 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002958 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002959 }
2960 if (value_location.IsConstant()) {
2961 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2962 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2963 } else {
2964 Register value = value_location.AsRegister<Register>();
2965 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2966 }
2967 break;
2968 }
2969
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002970 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002971 if (value_location.IsConstant()) {
2972 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002973 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002974 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002975 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002976 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002977 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002978 }
Alexey Frunze15958152017-02-09 19:08:30 -08002979 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2980 DCHECK_EQ(value, 0);
2981 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2982 DCHECK(!needs_write_barrier);
2983 DCHECK(!may_need_runtime_call_for_type_check);
2984 break;
2985 }
2986
2987 DCHECK(needs_write_barrier);
2988 Register value = value_location.AsRegister<Register>();
2989 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2990 Register temp2 = TMP; // Doesn't need to survive slow path.
2991 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2992 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2993 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2994 MipsLabel done;
2995 SlowPathCodeMIPS* slow_path = nullptr;
2996
2997 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01002998 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08002999 codegen_->AddSlowPath(slow_path);
3000 if (instruction->GetValueCanBeNull()) {
3001 MipsLabel non_zero;
3002 __ Bnez(value, &non_zero);
3003 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3004 if (index.IsConstant()) {
3005 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003006 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3007 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003008 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003009 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003010 }
Alexey Frunze15958152017-02-09 19:08:30 -08003011 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3012 __ B(&done);
3013 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003014 }
Alexey Frunze15958152017-02-09 19:08:30 -08003015
3016 // Note that when read barriers are enabled, the type checks
3017 // are performed without read barriers. This is fine, even in
3018 // the case where a class object is in the from-space after
3019 // the flip, as a comparison involving such a type would not
3020 // produce a false positive; it may of course produce a false
3021 // negative, in which case we would take the ArraySet slow
3022 // path.
3023
3024 // /* HeapReference<Class> */ temp1 = obj->klass_
3025 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3026 __ MaybeUnpoisonHeapReference(temp1);
3027
3028 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3029 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3030 // /* HeapReference<Class> */ temp2 = value->klass_
3031 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3032 // If heap poisoning is enabled, no need to unpoison `temp1`
3033 // nor `temp2`, as we are comparing two poisoned references.
3034
3035 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3036 MipsLabel do_put;
3037 __ Beq(temp1, temp2, &do_put);
3038 // If heap poisoning is enabled, the `temp1` reference has
3039 // not been unpoisoned yet; unpoison it now.
3040 __ MaybeUnpoisonHeapReference(temp1);
3041
3042 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3043 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3044 // If heap poisoning is enabled, no need to unpoison
3045 // `temp1`, as we are comparing against null below.
3046 __ Bnez(temp1, slow_path->GetEntryLabel());
3047 __ Bind(&do_put);
3048 } else {
3049 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3050 }
3051 }
3052
3053 Register source = value;
3054 if (kPoisonHeapReferences) {
3055 // Note that in the case where `value` is a null reference,
3056 // we do not enter this block, as a null reference does not
3057 // need poisoning.
3058 __ Move(temp1, value);
3059 __ PoisonHeapReference(temp1);
3060 source = temp1;
3061 }
3062
3063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3064 if (index.IsConstant()) {
3065 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003066 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003067 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003068 }
3069 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3070
3071 if (!may_need_runtime_call_for_type_check) {
3072 codegen_->MaybeRecordImplicitNullCheck(instruction);
3073 }
3074
3075 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3076
3077 if (done.IsLinked()) {
3078 __ Bind(&done);
3079 }
3080
3081 if (slow_path != nullptr) {
3082 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003083 }
3084 break;
3085 }
3086
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003087 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003088 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003089 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003090 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003091 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3092 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003093 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003094 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003095 }
3096 if (value_location.IsConstant()) {
3097 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3098 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3099 } else {
3100 Register value = value_location.AsRegisterPairLow<Register>();
3101 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003102 }
3103 break;
3104 }
3105
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003106 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003107 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003108 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003109 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003110 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3111 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003112 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003113 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003114 }
3115 if (value_location.IsConstant()) {
3116 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3117 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3118 } else {
3119 FRegister value = value_location.AsFpuRegister<FRegister>();
3120 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003121 }
3122 break;
3123 }
3124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003125 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003126 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003127 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003128 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003129 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3130 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003131 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003132 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003133 }
3134 if (value_location.IsConstant()) {
3135 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3136 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3137 } else {
3138 FRegister value = value_location.AsFpuRegister<FRegister>();
3139 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003140 }
3141 break;
3142 }
3143
Aart Bik66c158e2018-01-31 12:55:04 -08003144 case DataType::Type::kUint32:
3145 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003146 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003147 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3148 UNREACHABLE();
3149 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003150}
3151
Lena Djokica2901602017-09-21 13:50:52 +02003152void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3153 HIntermediateArrayAddressIndex* instruction) {
3154 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003155 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003156
3157 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3158
3159 locations->SetInAt(0, Location::RequiresRegister());
3160 locations->SetInAt(1, Location::ConstantLocation(shift));
3161 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3162}
3163
3164void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3165 HIntermediateArrayAddressIndex* instruction) {
3166 LocationSummary* locations = instruction->GetLocations();
3167 Register index_reg = locations->InAt(0).AsRegister<Register>();
3168 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3169 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3170}
3171
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003172void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003173 RegisterSet caller_saves = RegisterSet::Empty();
3174 InvokeRuntimeCallingConvention calling_convention;
3175 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3176 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3177 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003178
3179 HInstruction* index = instruction->InputAt(0);
3180 HInstruction* length = instruction->InputAt(1);
3181
3182 bool const_index = false;
3183 bool const_length = false;
3184
3185 if (index->IsConstant()) {
3186 if (length->IsConstant()) {
3187 const_index = true;
3188 const_length = true;
3189 } else {
3190 int32_t index_value = index->AsIntConstant()->GetValue();
3191 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3192 const_index = true;
3193 }
3194 }
3195 } else if (length->IsConstant()) {
3196 int32_t length_value = length->AsIntConstant()->GetValue();
3197 if (IsUint<15>(length_value)) {
3198 const_length = true;
3199 }
3200 }
3201
3202 locations->SetInAt(0, const_index
3203 ? Location::ConstantLocation(index->AsConstant())
3204 : Location::RequiresRegister());
3205 locations->SetInAt(1, const_length
3206 ? Location::ConstantLocation(length->AsConstant())
3207 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003208}
3209
3210void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3211 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003212 Location index_loc = locations->InAt(0);
3213 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003214
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003215 if (length_loc.IsConstant()) {
3216 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3217 if (index_loc.IsConstant()) {
3218 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3219 if (index < 0 || index >= length) {
3220 BoundsCheckSlowPathMIPS* slow_path =
3221 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3222 codegen_->AddSlowPath(slow_path);
3223 __ B(slow_path->GetEntryLabel());
3224 } else {
3225 // Nothing to be done.
3226 }
3227 return;
3228 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003229
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003230 BoundsCheckSlowPathMIPS* slow_path =
3231 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3232 codegen_->AddSlowPath(slow_path);
3233 Register index = index_loc.AsRegister<Register>();
3234 if (length == 0) {
3235 __ B(slow_path->GetEntryLabel());
3236 } else if (length == 1) {
3237 __ Bnez(index, slow_path->GetEntryLabel());
3238 } else {
3239 DCHECK(IsUint<15>(length)) << length;
3240 __ Sltiu(TMP, index, length);
3241 __ Beqz(TMP, slow_path->GetEntryLabel());
3242 }
3243 } else {
3244 Register length = length_loc.AsRegister<Register>();
3245 BoundsCheckSlowPathMIPS* slow_path =
3246 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3247 codegen_->AddSlowPath(slow_path);
3248 if (index_loc.IsConstant()) {
3249 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3250 if (index < 0) {
3251 __ B(slow_path->GetEntryLabel());
3252 } else if (index == 0) {
3253 __ Blez(length, slow_path->GetEntryLabel());
3254 } else {
3255 DCHECK(IsInt<16>(index + 1)) << index;
3256 __ Sltiu(TMP, length, index + 1);
3257 __ Bnez(TMP, slow_path->GetEntryLabel());
3258 }
3259 } else {
3260 Register index = index_loc.AsRegister<Register>();
3261 __ Bgeu(index, length, slow_path->GetEntryLabel());
3262 }
3263 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003264}
3265
Alexey Frunze15958152017-02-09 19:08:30 -08003266// Temp is used for read barrier.
3267static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3268 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003269 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003270 (kUseBakerReadBarrier ||
3271 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3272 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3273 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3274 return 1;
3275 }
3276 return 0;
3277}
3278
3279// Extra temp is used for read barrier.
3280static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3281 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3282}
3283
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003284void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003285 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003286 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003287 LocationSummary* locations =
3288 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003289 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003290 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003291 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003292}
3293
3294void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003295 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003296 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003297 Location obj_loc = locations->InAt(0);
3298 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003299 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003300 Location temp_loc = locations->GetTemp(0);
3301 Register temp = temp_loc.AsRegister<Register>();
3302 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3303 DCHECK_LE(num_temps, 2u);
3304 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003305 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3306 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3307 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3308 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3309 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3310 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3311 const uint32_t object_array_data_offset =
3312 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3313 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003314
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003315 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003316 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003317 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3318 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003319 codegen_->AddSlowPath(slow_path);
3320
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003321 // Avoid this check if we know `obj` is not null.
3322 if (instruction->MustDoNullCheck()) {
3323 __ Beqz(obj, &done);
3324 }
3325
3326 switch (type_check_kind) {
3327 case TypeCheckKind::kExactCheck:
3328 case TypeCheckKind::kArrayCheck: {
3329 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003330 GenerateReferenceLoadTwoRegisters(instruction,
3331 temp_loc,
3332 obj_loc,
3333 class_offset,
3334 maybe_temp2_loc,
3335 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003336 // Jump to slow path for throwing the exception or doing a
3337 // more involved array check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003338 __ Bne(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003339 break;
3340 }
3341
3342 case TypeCheckKind::kAbstractClassCheck: {
3343 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003344 GenerateReferenceLoadTwoRegisters(instruction,
3345 temp_loc,
3346 obj_loc,
3347 class_offset,
3348 maybe_temp2_loc,
3349 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003350 // If the class is abstract, we eagerly fetch the super class of the
3351 // object to avoid doing a comparison we know will fail.
3352 MipsLabel loop;
3353 __ Bind(&loop);
3354 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003355 GenerateReferenceLoadOneRegister(instruction,
3356 temp_loc,
3357 super_offset,
3358 maybe_temp2_loc,
3359 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003360 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3361 // exception.
3362 __ Beqz(temp, slow_path->GetEntryLabel());
3363 // Otherwise, compare the classes.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003364 __ Bne(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003365 break;
3366 }
3367
3368 case TypeCheckKind::kClassHierarchyCheck: {
3369 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003370 GenerateReferenceLoadTwoRegisters(instruction,
3371 temp_loc,
3372 obj_loc,
3373 class_offset,
3374 maybe_temp2_loc,
3375 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003376 // Walk over the class hierarchy to find a match.
3377 MipsLabel loop;
3378 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003379 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003380 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003381 GenerateReferenceLoadOneRegister(instruction,
3382 temp_loc,
3383 super_offset,
3384 maybe_temp2_loc,
3385 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003386 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3387 // exception. Otherwise, jump to the beginning of the loop.
3388 __ Bnez(temp, &loop);
3389 __ B(slow_path->GetEntryLabel());
3390 break;
3391 }
3392
3393 case TypeCheckKind::kArrayObjectCheck: {
3394 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003395 GenerateReferenceLoadTwoRegisters(instruction,
3396 temp_loc,
3397 obj_loc,
3398 class_offset,
3399 maybe_temp2_loc,
3400 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003401 // Do an exact check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003402 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003403 // Otherwise, we need to check that the object's class is a non-primitive array.
3404 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003405 GenerateReferenceLoadOneRegister(instruction,
3406 temp_loc,
3407 component_offset,
3408 maybe_temp2_loc,
3409 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003410 // If the component type is null, jump to the slow path to throw the exception.
3411 __ Beqz(temp, slow_path->GetEntryLabel());
3412 // Otherwise, the object is indeed an array, further check that this component
3413 // type is not a primitive type.
3414 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3415 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3416 __ Bnez(temp, slow_path->GetEntryLabel());
3417 break;
3418 }
3419
3420 case TypeCheckKind::kUnresolvedCheck:
3421 // We always go into the type check slow path for the unresolved check case.
3422 // We cannot directly call the CheckCast runtime entry point
3423 // without resorting to a type checking slow path here (i.e. by
3424 // calling InvokeRuntime directly), as it would require to
3425 // assign fixed registers for the inputs of this HInstanceOf
3426 // instruction (following the runtime calling convention), which
3427 // might be cluttered by the potential first read barrier
3428 // emission at the beginning of this method.
3429 __ B(slow_path->GetEntryLabel());
3430 break;
3431
3432 case TypeCheckKind::kInterfaceCheck: {
3433 // Avoid read barriers to improve performance of the fast path. We can not get false
3434 // positives by doing this.
3435 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003436 GenerateReferenceLoadTwoRegisters(instruction,
3437 temp_loc,
3438 obj_loc,
3439 class_offset,
3440 maybe_temp2_loc,
3441 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003442 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003443 GenerateReferenceLoadTwoRegisters(instruction,
3444 temp_loc,
3445 temp_loc,
3446 iftable_offset,
3447 maybe_temp2_loc,
3448 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003449 // Iftable is never null.
3450 __ Lw(TMP, temp, array_length_offset);
3451 // Loop through the iftable and check if any class matches.
3452 MipsLabel loop;
3453 __ Bind(&loop);
3454 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3455 __ Beqz(TMP, slow_path->GetEntryLabel());
3456 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3457 __ MaybeUnpoisonHeapReference(AT);
3458 // Go to next interface.
3459 __ Addiu(TMP, TMP, -2);
3460 // Compare the classes and continue the loop if they do not match.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003461 __ Bne(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003462 break;
3463 }
3464 }
3465
3466 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003467 __ Bind(slow_path->GetExitLabel());
3468}
3469
3470void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3471 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003472 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003473 locations->SetInAt(0, Location::RequiresRegister());
3474 if (check->HasUses()) {
3475 locations->SetOut(Location::SameAsFirstInput());
3476 }
3477}
3478
3479void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3480 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003481 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003482 check->GetLoadClass(),
3483 check,
3484 check->GetDexPc(),
3485 true);
3486 codegen_->AddSlowPath(slow_path);
3487 GenerateClassInitializationCheck(slow_path,
3488 check->GetLocations()->InAt(0).AsRegister<Register>());
3489}
3490
3491void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003492 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003493
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003494 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003495 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003496
3497 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003498 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003499 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003500 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003501 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003502 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003503 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003504 locations->SetInAt(0, Location::RequiresRegister());
3505 locations->SetInAt(1, Location::RequiresRegister());
3506 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3507 break;
3508
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003509 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003510 locations->SetInAt(0, Location::RequiresRegister());
3511 locations->SetInAt(1, Location::RequiresRegister());
3512 // Output overlaps because it is written before doing the low comparison.
3513 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3514 break;
3515
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003516 case DataType::Type::kFloat32:
3517 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003518 locations->SetInAt(0, Location::RequiresFpuRegister());
3519 locations->SetInAt(1, Location::RequiresFpuRegister());
3520 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003521 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003522
3523 default:
3524 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3525 }
3526}
3527
3528void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3529 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003530 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003531 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003532 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003533
3534 // 0 if: left == right
3535 // 1 if: left > right
3536 // -1 if: left < right
3537 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003538 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003539 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003540 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003541 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003542 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003543 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003544 Register lhs = locations->InAt(0).AsRegister<Register>();
3545 Register rhs = locations->InAt(1).AsRegister<Register>();
3546 __ Slt(TMP, lhs, rhs);
3547 __ Slt(res, rhs, lhs);
3548 __ Subu(res, res, TMP);
3549 break;
3550 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003551 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003552 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003553 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3554 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3555 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3556 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3557 // TODO: more efficient (direct) comparison with a constant.
3558 __ Slt(TMP, lhs_high, rhs_high);
3559 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3560 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3561 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3562 __ Sltu(TMP, lhs_low, rhs_low);
3563 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3564 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3565 __ Bind(&done);
3566 break;
3567 }
3568
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003569 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003570 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003571 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3572 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3573 MipsLabel done;
3574 if (isR6) {
3575 __ CmpEqS(FTMP, lhs, rhs);
3576 __ LoadConst32(res, 0);
3577 __ Bc1nez(FTMP, &done);
3578 if (gt_bias) {
3579 __ CmpLtS(FTMP, lhs, rhs);
3580 __ LoadConst32(res, -1);
3581 __ Bc1nez(FTMP, &done);
3582 __ LoadConst32(res, 1);
3583 } else {
3584 __ CmpLtS(FTMP, rhs, lhs);
3585 __ LoadConst32(res, 1);
3586 __ Bc1nez(FTMP, &done);
3587 __ LoadConst32(res, -1);
3588 }
3589 } else {
3590 if (gt_bias) {
3591 __ ColtS(0, lhs, rhs);
3592 __ LoadConst32(res, -1);
3593 __ Bc1t(0, &done);
3594 __ CeqS(0, lhs, rhs);
3595 __ LoadConst32(res, 1);
3596 __ Movt(res, ZERO, 0);
3597 } else {
3598 __ ColtS(0, rhs, lhs);
3599 __ LoadConst32(res, 1);
3600 __ Bc1t(0, &done);
3601 __ CeqS(0, lhs, rhs);
3602 __ LoadConst32(res, -1);
3603 __ Movt(res, ZERO, 0);
3604 }
3605 }
3606 __ Bind(&done);
3607 break;
3608 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003609 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003610 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003611 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3612 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3613 MipsLabel done;
3614 if (isR6) {
3615 __ CmpEqD(FTMP, lhs, rhs);
3616 __ LoadConst32(res, 0);
3617 __ Bc1nez(FTMP, &done);
3618 if (gt_bias) {
3619 __ CmpLtD(FTMP, lhs, rhs);
3620 __ LoadConst32(res, -1);
3621 __ Bc1nez(FTMP, &done);
3622 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003623 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003624 __ CmpLtD(FTMP, rhs, lhs);
3625 __ LoadConst32(res, 1);
3626 __ Bc1nez(FTMP, &done);
3627 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003628 }
3629 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003630 if (gt_bias) {
3631 __ ColtD(0, lhs, rhs);
3632 __ LoadConst32(res, -1);
3633 __ Bc1t(0, &done);
3634 __ CeqD(0, lhs, rhs);
3635 __ LoadConst32(res, 1);
3636 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003637 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003638 __ ColtD(0, rhs, lhs);
3639 __ LoadConst32(res, 1);
3640 __ Bc1t(0, &done);
3641 __ CeqD(0, lhs, rhs);
3642 __ LoadConst32(res, -1);
3643 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003644 }
3645 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003646 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003647 break;
3648 }
3649
3650 default:
3651 LOG(FATAL) << "Unimplemented compare type " << in_type;
3652 }
3653}
3654
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003655void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003656 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003657 switch (instruction->InputAt(0)->GetType()) {
3658 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003659 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003660 locations->SetInAt(0, Location::RequiresRegister());
3661 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3662 break;
3663
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003664 case DataType::Type::kFloat32:
3665 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003666 locations->SetInAt(0, Location::RequiresFpuRegister());
3667 locations->SetInAt(1, Location::RequiresFpuRegister());
3668 break;
3669 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003670 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003671 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3672 }
3673}
3674
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003675void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003676 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003677 return;
3678 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003680 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003681 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003682
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003683 switch (type) {
3684 default:
3685 // Integer case.
3686 GenerateIntCompare(instruction->GetCondition(), locations);
3687 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003688
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003689 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003690 GenerateLongCompare(instruction->GetCondition(), locations);
3691 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003692
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003693 case DataType::Type::kFloat32:
3694 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003695 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3696 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003697 }
3698}
3699
Alexey Frunze7e99e052015-11-24 19:28:01 -08003700void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3701 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003702
3703 LocationSummary* locations = instruction->GetLocations();
3704 Location second = locations->InAt(1);
3705 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003706 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003707 DCHECK(imm == 1 || imm == -1);
3708
Lena Djokic4b8025c2017-12-21 16:15:50 +01003709 if (instruction->GetResultType() == DataType::Type::kInt32) {
3710 Register out = locations->Out().AsRegister<Register>();
3711 Register dividend = locations->InAt(0).AsRegister<Register>();
3712
3713 if (instruction->IsRem()) {
3714 __ Move(out, ZERO);
3715 } else {
3716 if (imm == -1) {
3717 __ Subu(out, ZERO, dividend);
3718 } else if (out != dividend) {
3719 __ Move(out, dividend);
3720 }
3721 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003722 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003723 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3724 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3725 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3726 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3727 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3728
3729 if (instruction->IsRem()) {
3730 __ Move(out_high, ZERO);
3731 __ Move(out_low, ZERO);
3732 } else {
3733 if (imm == -1) {
3734 __ Subu(out_low, ZERO, in_low);
3735 __ Sltu(AT, ZERO, out_low);
3736 __ Subu(out_high, ZERO, in_high);
3737 __ Subu(out_high, out_high, AT);
3738 } else {
3739 __ Move(out_low, in_low);
3740 __ Move(out_high, in_high);
3741 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003742 }
3743 }
3744}
3745
3746void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3747 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003748
3749 LocationSummary* locations = instruction->GetLocations();
3750 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003751 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3752 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003753 DCHECK(second.IsConstant());
3754
Lena Djokic4b8025c2017-12-21 16:15:50 +01003755 if (instruction->GetResultType() == DataType::Type::kInt32) {
3756 Register out = locations->Out().AsRegister<Register>();
3757 Register dividend = locations->InAt(0).AsRegister<Register>();
3758 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3759 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3760 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003761
Lena Djokic4b8025c2017-12-21 16:15:50 +01003762 if (instruction->IsDiv()) {
3763 if (ctz_imm == 1) {
3764 // Fast path for division by +/-2, which is very common.
3765 __ Srl(TMP, dividend, 31);
3766 } else {
3767 __ Sra(TMP, dividend, 31);
3768 __ Srl(TMP, TMP, 32 - ctz_imm);
3769 }
3770 __ Addu(out, dividend, TMP);
3771 __ Sra(out, out, ctz_imm);
3772 if (imm < 0) {
3773 __ Subu(out, ZERO, out);
3774 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003775 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003776 if (ctz_imm == 1) {
3777 // Fast path for modulo +/-2, which is very common.
3778 __ Sra(TMP, dividend, 31);
3779 __ Subu(out, dividend, TMP);
3780 __ Andi(out, out, 1);
3781 __ Addu(out, out, TMP);
3782 } else {
3783 __ Sra(TMP, dividend, 31);
3784 __ Srl(TMP, TMP, 32 - ctz_imm);
3785 __ Addu(out, dividend, TMP);
3786 if (IsUint<16>(abs_imm - 1)) {
3787 __ Andi(out, out, abs_imm - 1);
3788 } else {
3789 if (is_r2_or_newer) {
3790 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3791 } else {
3792 __ Sll(out, out, 32 - ctz_imm);
3793 __ Srl(out, out, 32 - ctz_imm);
3794 }
3795 }
3796 __ Subu(out, out, TMP);
3797 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003798 }
3799 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003800 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3801 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3802 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3803 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3804 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3805 int64_t imm = Int64FromConstant(second.GetConstant());
3806 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3807 int ctz_imm = CTZ(abs_imm);
3808
3809 if (instruction->IsDiv()) {
3810 if (ctz_imm < 32) {
3811 if (ctz_imm == 1) {
3812 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003813 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003814 __ Sra(AT, in_high, 31);
3815 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003816 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003817 __ Addu(AT, AT, in_low);
3818 __ Sltu(TMP, AT, in_low);
3819 __ Addu(out_high, in_high, TMP);
3820 __ Srl(out_low, AT, ctz_imm);
3821 if (is_r2_or_newer) {
3822 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3823 __ Sra(out_high, out_high, ctz_imm);
3824 } else {
3825 __ Sll(AT, out_high, 32 - ctz_imm);
3826 __ Sra(out_high, out_high, ctz_imm);
3827 __ Or(out_low, out_low, AT);
3828 }
3829 if (imm < 0) {
3830 __ Subu(out_low, ZERO, out_low);
3831 __ Sltu(AT, ZERO, out_low);
3832 __ Subu(out_high, ZERO, out_high);
3833 __ Subu(out_high, out_high, AT);
3834 }
3835 } else if (ctz_imm == 32) {
3836 __ Sra(AT, in_high, 31);
3837 __ Addu(AT, AT, in_low);
3838 __ Sltu(AT, AT, in_low);
3839 __ Addu(out_low, in_high, AT);
3840 if (imm < 0) {
3841 __ Srl(TMP, out_low, 31);
3842 __ Subu(out_low, ZERO, out_low);
3843 __ Sltu(AT, ZERO, out_low);
3844 __ Subu(out_high, TMP, AT);
3845 } else {
3846 __ Sra(out_high, out_low, 31);
3847 }
3848 } else if (ctz_imm < 63) {
3849 __ Sra(AT, in_high, 31);
3850 __ Srl(TMP, AT, 64 - ctz_imm);
3851 __ Addu(AT, AT, in_low);
3852 __ Sltu(AT, AT, in_low);
3853 __ Addu(out_low, in_high, AT);
3854 __ Addu(out_low, out_low, TMP);
3855 __ Sra(out_low, out_low, ctz_imm - 32);
3856 if (imm < 0) {
3857 __ Subu(out_low, ZERO, out_low);
3858 }
3859 __ Sra(out_high, out_low, 31);
3860 } else {
3861 DCHECK_LT(imm, 0);
3862 if (is_r6) {
3863 __ Aui(AT, in_high, 0x8000);
3864 } else {
3865 __ Lui(AT, 0x8000);
3866 __ Xor(AT, AT, in_high);
3867 }
3868 __ Or(AT, AT, in_low);
3869 __ Sltiu(out_low, AT, 1);
3870 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003871 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003872 } else {
3873 if ((ctz_imm == 1) && !is_r6) {
3874 __ Andi(AT, in_low, 1);
3875 __ Sll(TMP, in_low, 31);
3876 __ And(TMP, in_high, TMP);
3877 __ Sra(out_high, TMP, 31);
3878 __ Or(out_low, out_high, AT);
3879 } else if (ctz_imm < 32) {
3880 __ Sra(AT, in_high, 31);
3881 if (ctz_imm <= 16) {
3882 __ Andi(out_low, in_low, abs_imm - 1);
3883 } else if (is_r2_or_newer) {
3884 __ Ext(out_low, in_low, 0, ctz_imm);
3885 } else {
3886 __ Sll(out_low, in_low, 32 - ctz_imm);
3887 __ Srl(out_low, out_low, 32 - ctz_imm);
3888 }
3889 if (is_r6) {
3890 __ Selnez(out_high, AT, out_low);
3891 } else {
3892 __ Movz(AT, ZERO, out_low);
3893 __ Move(out_high, AT);
3894 }
3895 if (is_r2_or_newer) {
3896 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
3897 } else {
3898 __ Sll(AT, out_high, ctz_imm);
3899 __ Or(out_low, out_low, AT);
3900 }
3901 } else if (ctz_imm == 32) {
3902 __ Sra(AT, in_high, 31);
3903 __ Move(out_low, in_low);
3904 if (is_r6) {
3905 __ Selnez(out_high, AT, out_low);
3906 } else {
3907 __ Movz(AT, ZERO, out_low);
3908 __ Move(out_high, AT);
3909 }
3910 } else if (ctz_imm < 63) {
3911 __ Sra(AT, in_high, 31);
3912 __ Move(TMP, in_low);
3913 if (ctz_imm - 32 <= 16) {
3914 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
3915 } else if (is_r2_or_newer) {
3916 __ Ext(out_high, in_high, 0, ctz_imm - 32);
3917 } else {
3918 __ Sll(out_high, in_high, 64 - ctz_imm);
3919 __ Srl(out_high, out_high, 64 - ctz_imm);
3920 }
3921 __ Move(out_low, TMP);
3922 __ Or(TMP, TMP, out_high);
3923 if (is_r6) {
3924 __ Selnez(AT, AT, TMP);
3925 } else {
3926 __ Movz(AT, ZERO, TMP);
3927 }
3928 if (is_r2_or_newer) {
3929 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
3930 } else {
3931 __ Sll(AT, AT, ctz_imm - 32);
3932 __ Or(out_high, out_high, AT);
3933 }
3934 } else {
3935 if (is_r6) {
3936 __ Aui(AT, in_high, 0x8000);
3937 } else {
3938 __ Lui(AT, 0x8000);
3939 __ Xor(AT, AT, in_high);
3940 }
3941 __ Or(AT, AT, in_low);
3942 __ Sltiu(AT, AT, 1);
3943 __ Sll(AT, AT, 31);
3944 __ Move(out_low, in_low);
3945 __ Xor(out_high, in_high, AT);
3946 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003947 }
3948 }
3949}
3950
3951void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3952 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003953 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003954
3955 LocationSummary* locations = instruction->GetLocations();
3956 Location second = locations->InAt(1);
3957 DCHECK(second.IsConstant());
3958
3959 Register out = locations->Out().AsRegister<Register>();
3960 Register dividend = locations->InAt(0).AsRegister<Register>();
3961 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3962
3963 int64_t magic;
3964 int shift;
3965 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3966
3967 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3968
3969 __ LoadConst32(TMP, magic);
3970 if (isR6) {
3971 __ MuhR6(TMP, dividend, TMP);
3972 } else {
3973 __ MultR2(dividend, TMP);
3974 __ Mfhi(TMP);
3975 }
3976 if (imm > 0 && magic < 0) {
3977 __ Addu(TMP, TMP, dividend);
3978 } else if (imm < 0 && magic > 0) {
3979 __ Subu(TMP, TMP, dividend);
3980 }
3981
3982 if (shift != 0) {
3983 __ Sra(TMP, TMP, shift);
3984 }
3985
3986 if (instruction->IsDiv()) {
3987 __ Sra(out, TMP, 31);
3988 __ Subu(out, TMP, out);
3989 } else {
3990 __ Sra(AT, TMP, 31);
3991 __ Subu(AT, TMP, AT);
3992 __ LoadConst32(TMP, imm);
3993 if (isR6) {
3994 __ MulR6(TMP, AT, TMP);
3995 } else {
3996 __ MulR2(TMP, AT, TMP);
3997 }
3998 __ Subu(out, dividend, TMP);
3999 }
4000}
4001
4002void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4003 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004004 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004005
4006 LocationSummary* locations = instruction->GetLocations();
4007 Register out = locations->Out().AsRegister<Register>();
4008 Location second = locations->InAt(1);
4009
4010 if (second.IsConstant()) {
4011 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4012 if (imm == 0) {
4013 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4014 } else if (imm == 1 || imm == -1) {
4015 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004016 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004017 DivRemByPowerOfTwo(instruction);
4018 } else {
4019 DCHECK(imm <= -2 || imm >= 2);
4020 GenerateDivRemWithAnyConstant(instruction);
4021 }
4022 } else {
4023 Register dividend = locations->InAt(0).AsRegister<Register>();
4024 Register divisor = second.AsRegister<Register>();
4025 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4026 if (instruction->IsDiv()) {
4027 if (isR6) {
4028 __ DivR6(out, dividend, divisor);
4029 } else {
4030 __ DivR2(out, dividend, divisor);
4031 }
4032 } else {
4033 if (isR6) {
4034 __ ModR6(out, dividend, divisor);
4035 } else {
4036 __ ModR2(out, dividend, divisor);
4037 }
4038 }
4039 }
4040}
4041
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004042void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004043 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004044 bool call_long_div = false;
4045 if (type == DataType::Type::kInt64) {
4046 if (div->InputAt(1)->IsConstant()) {
4047 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4048 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4049 } else {
4050 call_long_div = true;
4051 }
4052 }
4053 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004054 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004055 : LocationSummary::kNoCall;
4056
Vladimir Markoca6fff82017-10-03 14:49:14 +01004057 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004058
4059 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004060 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004061 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004062 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004063 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4064 break;
4065
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004066 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004067 if (call_long_div) {
4068 InvokeRuntimeCallingConvention calling_convention;
4069 locations->SetInAt(0, Location::RegisterPairLocation(
4070 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4071 locations->SetInAt(1, Location::RegisterPairLocation(
4072 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4073 locations->SetOut(calling_convention.GetReturnLocation(type));
4074 } else {
4075 locations->SetInAt(0, Location::RequiresRegister());
4076 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4077 locations->SetOut(Location::RequiresRegister());
4078 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004079 break;
4080 }
4081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004082 case DataType::Type::kFloat32:
4083 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004084 locations->SetInAt(0, Location::RequiresFpuRegister());
4085 locations->SetInAt(1, Location::RequiresFpuRegister());
4086 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4087 break;
4088
4089 default:
4090 LOG(FATAL) << "Unexpected div type " << type;
4091 }
4092}
4093
4094void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004095 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004096 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004097
4098 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004099 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004100 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004101 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004102 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004103 if (locations->InAt(1).IsConstant()) {
4104 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4105 if (imm == 0) {
4106 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4107 } else if (imm == 1 || imm == -1) {
4108 DivRemOneOrMinusOne(instruction);
4109 } else {
4110 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4111 DivRemByPowerOfTwo(instruction);
4112 }
4113 } else {
4114 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4115 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4116 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004117 break;
4118 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004119 case DataType::Type::kFloat32:
4120 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004121 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4122 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4123 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004124 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004125 __ DivS(dst, lhs, rhs);
4126 } else {
4127 __ DivD(dst, lhs, rhs);
4128 }
4129 break;
4130 }
4131 default:
4132 LOG(FATAL) << "Unexpected div type " << type;
4133 }
4134}
4135
4136void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004137 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004138 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004139}
4140
4141void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004142 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004143 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004144 codegen_->AddSlowPath(slow_path);
4145 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004146 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004147
4148 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004150 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004151 case DataType::Type::kInt8:
4152 case DataType::Type::kUint16:
4153 case DataType::Type::kInt16:
4154 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004155 if (value.IsConstant()) {
4156 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4157 __ B(slow_path->GetEntryLabel());
4158 } else {
4159 // A division by a non-null constant is valid. We don't need to perform
4160 // any check, so simply fall through.
4161 }
4162 } else {
4163 DCHECK(value.IsRegister()) << value;
4164 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4165 }
4166 break;
4167 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004168 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004169 if (value.IsConstant()) {
4170 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4171 __ B(slow_path->GetEntryLabel());
4172 } else {
4173 // A division by a non-null constant is valid. We don't need to perform
4174 // any check, so simply fall through.
4175 }
4176 } else {
4177 DCHECK(value.IsRegisterPair()) << value;
4178 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4179 __ Beqz(TMP, slow_path->GetEntryLabel());
4180 }
4181 break;
4182 }
4183 default:
4184 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4185 }
4186}
4187
4188void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4189 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004190 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004191 locations->SetOut(Location::ConstantLocation(constant));
4192}
4193
4194void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4195 // Will be generated at use site.
4196}
4197
4198void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4199 exit->SetLocations(nullptr);
4200}
4201
4202void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4203}
4204
4205void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4206 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004207 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004208 locations->SetOut(Location::ConstantLocation(constant));
4209}
4210
4211void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4212 // Will be generated at use site.
4213}
4214
4215void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4216 got->SetLocations(nullptr);
4217}
4218
4219void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004220 if (successor->IsExitBlock()) {
4221 DCHECK(got->GetPrevious()->AlwaysThrows());
4222 return; // no code needed
4223 }
4224
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004225 HBasicBlock* block = got->GetBlock();
4226 HInstruction* previous = got->GetPrevious();
4227 HLoopInformation* info = block->GetLoopInformation();
4228
4229 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004230 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4231 __ Lw(AT, SP, kCurrentMethodStackOffset);
4232 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4233 __ Addiu(TMP, TMP, 1);
4234 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4235 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004236 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4237 return;
4238 }
4239 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4240 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4241 }
4242 if (!codegen_->GoesToNextBlock(block, successor)) {
4243 __ B(codegen_->GetLabelOf(successor));
4244 }
4245}
4246
4247void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4248 HandleGoto(got, got->GetSuccessor());
4249}
4250
4251void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4252 try_boundary->SetLocations(nullptr);
4253}
4254
4255void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4256 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4257 if (!successor->IsExitBlock()) {
4258 HandleGoto(try_boundary, successor);
4259 }
4260}
4261
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004262void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4263 LocationSummary* locations) {
4264 Register dst = locations->Out().AsRegister<Register>();
4265 Register lhs = locations->InAt(0).AsRegister<Register>();
4266 Location rhs_location = locations->InAt(1);
4267 Register rhs_reg = ZERO;
4268 int64_t rhs_imm = 0;
4269 bool use_imm = rhs_location.IsConstant();
4270 if (use_imm) {
4271 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4272 } else {
4273 rhs_reg = rhs_location.AsRegister<Register>();
4274 }
4275
4276 switch (cond) {
4277 case kCondEQ:
4278 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004279 if (use_imm && IsInt<16>(-rhs_imm)) {
4280 if (rhs_imm == 0) {
4281 if (cond == kCondEQ) {
4282 __ Sltiu(dst, lhs, 1);
4283 } else {
4284 __ Sltu(dst, ZERO, lhs);
4285 }
4286 } else {
4287 __ Addiu(dst, lhs, -rhs_imm);
4288 if (cond == kCondEQ) {
4289 __ Sltiu(dst, dst, 1);
4290 } else {
4291 __ Sltu(dst, ZERO, dst);
4292 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004293 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004294 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004295 if (use_imm && IsUint<16>(rhs_imm)) {
4296 __ Xori(dst, lhs, rhs_imm);
4297 } else {
4298 if (use_imm) {
4299 rhs_reg = TMP;
4300 __ LoadConst32(rhs_reg, rhs_imm);
4301 }
4302 __ Xor(dst, lhs, rhs_reg);
4303 }
4304 if (cond == kCondEQ) {
4305 __ Sltiu(dst, dst, 1);
4306 } else {
4307 __ Sltu(dst, ZERO, dst);
4308 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004309 }
4310 break;
4311
4312 case kCondLT:
4313 case kCondGE:
4314 if (use_imm && IsInt<16>(rhs_imm)) {
4315 __ Slti(dst, lhs, rhs_imm);
4316 } else {
4317 if (use_imm) {
4318 rhs_reg = TMP;
4319 __ LoadConst32(rhs_reg, rhs_imm);
4320 }
4321 __ Slt(dst, lhs, rhs_reg);
4322 }
4323 if (cond == kCondGE) {
4324 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4325 // only the slt instruction but no sge.
4326 __ Xori(dst, dst, 1);
4327 }
4328 break;
4329
4330 case kCondLE:
4331 case kCondGT:
4332 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4333 // Simulate lhs <= rhs via lhs < rhs + 1.
4334 __ Slti(dst, lhs, rhs_imm + 1);
4335 if (cond == kCondGT) {
4336 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4337 // only the slti instruction but no sgti.
4338 __ Xori(dst, dst, 1);
4339 }
4340 } else {
4341 if (use_imm) {
4342 rhs_reg = TMP;
4343 __ LoadConst32(rhs_reg, rhs_imm);
4344 }
4345 __ Slt(dst, rhs_reg, lhs);
4346 if (cond == kCondLE) {
4347 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4348 // only the slt instruction but no sle.
4349 __ Xori(dst, dst, 1);
4350 }
4351 }
4352 break;
4353
4354 case kCondB:
4355 case kCondAE:
4356 if (use_imm && IsInt<16>(rhs_imm)) {
4357 // Sltiu sign-extends its 16-bit immediate operand before
4358 // the comparison and thus lets us compare directly with
4359 // unsigned values in the ranges [0, 0x7fff] and
4360 // [0xffff8000, 0xffffffff].
4361 __ Sltiu(dst, lhs, rhs_imm);
4362 } else {
4363 if (use_imm) {
4364 rhs_reg = TMP;
4365 __ LoadConst32(rhs_reg, rhs_imm);
4366 }
4367 __ Sltu(dst, lhs, rhs_reg);
4368 }
4369 if (cond == kCondAE) {
4370 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4371 // only the sltu instruction but no sgeu.
4372 __ Xori(dst, dst, 1);
4373 }
4374 break;
4375
4376 case kCondBE:
4377 case kCondA:
4378 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4379 // Simulate lhs <= rhs via lhs < rhs + 1.
4380 // Note that this only works if rhs + 1 does not overflow
4381 // to 0, hence the check above.
4382 // Sltiu sign-extends its 16-bit immediate operand before
4383 // the comparison and thus lets us compare directly with
4384 // unsigned values in the ranges [0, 0x7fff] and
4385 // [0xffff8000, 0xffffffff].
4386 __ Sltiu(dst, lhs, rhs_imm + 1);
4387 if (cond == kCondA) {
4388 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4389 // only the sltiu instruction but no sgtiu.
4390 __ Xori(dst, dst, 1);
4391 }
4392 } else {
4393 if (use_imm) {
4394 rhs_reg = TMP;
4395 __ LoadConst32(rhs_reg, rhs_imm);
4396 }
4397 __ Sltu(dst, rhs_reg, lhs);
4398 if (cond == kCondBE) {
4399 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4400 // only the sltu instruction but no sleu.
4401 __ Xori(dst, dst, 1);
4402 }
4403 }
4404 break;
4405 }
4406}
4407
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004408bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4409 LocationSummary* input_locations,
4410 Register dst) {
4411 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4412 Location rhs_location = input_locations->InAt(1);
4413 Register rhs_reg = ZERO;
4414 int64_t rhs_imm = 0;
4415 bool use_imm = rhs_location.IsConstant();
4416 if (use_imm) {
4417 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4418 } else {
4419 rhs_reg = rhs_location.AsRegister<Register>();
4420 }
4421
4422 switch (cond) {
4423 case kCondEQ:
4424 case kCondNE:
4425 if (use_imm && IsInt<16>(-rhs_imm)) {
4426 __ Addiu(dst, lhs, -rhs_imm);
4427 } else if (use_imm && IsUint<16>(rhs_imm)) {
4428 __ Xori(dst, lhs, rhs_imm);
4429 } else {
4430 if (use_imm) {
4431 rhs_reg = TMP;
4432 __ LoadConst32(rhs_reg, rhs_imm);
4433 }
4434 __ Xor(dst, lhs, rhs_reg);
4435 }
4436 return (cond == kCondEQ);
4437
4438 case kCondLT:
4439 case kCondGE:
4440 if (use_imm && IsInt<16>(rhs_imm)) {
4441 __ Slti(dst, lhs, rhs_imm);
4442 } else {
4443 if (use_imm) {
4444 rhs_reg = TMP;
4445 __ LoadConst32(rhs_reg, rhs_imm);
4446 }
4447 __ Slt(dst, lhs, rhs_reg);
4448 }
4449 return (cond == kCondGE);
4450
4451 case kCondLE:
4452 case kCondGT:
4453 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4454 // Simulate lhs <= rhs via lhs < rhs + 1.
4455 __ Slti(dst, lhs, rhs_imm + 1);
4456 return (cond == kCondGT);
4457 } else {
4458 if (use_imm) {
4459 rhs_reg = TMP;
4460 __ LoadConst32(rhs_reg, rhs_imm);
4461 }
4462 __ Slt(dst, rhs_reg, lhs);
4463 return (cond == kCondLE);
4464 }
4465
4466 case kCondB:
4467 case kCondAE:
4468 if (use_imm && IsInt<16>(rhs_imm)) {
4469 // Sltiu sign-extends its 16-bit immediate operand before
4470 // the comparison and thus lets us compare directly with
4471 // unsigned values in the ranges [0, 0x7fff] and
4472 // [0xffff8000, 0xffffffff].
4473 __ Sltiu(dst, lhs, rhs_imm);
4474 } else {
4475 if (use_imm) {
4476 rhs_reg = TMP;
4477 __ LoadConst32(rhs_reg, rhs_imm);
4478 }
4479 __ Sltu(dst, lhs, rhs_reg);
4480 }
4481 return (cond == kCondAE);
4482
4483 case kCondBE:
4484 case kCondA:
4485 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4486 // Simulate lhs <= rhs via lhs < rhs + 1.
4487 // Note that this only works if rhs + 1 does not overflow
4488 // to 0, hence the check above.
4489 // Sltiu sign-extends its 16-bit immediate operand before
4490 // the comparison and thus lets us compare directly with
4491 // unsigned values in the ranges [0, 0x7fff] and
4492 // [0xffff8000, 0xffffffff].
4493 __ Sltiu(dst, lhs, rhs_imm + 1);
4494 return (cond == kCondA);
4495 } else {
4496 if (use_imm) {
4497 rhs_reg = TMP;
4498 __ LoadConst32(rhs_reg, rhs_imm);
4499 }
4500 __ Sltu(dst, rhs_reg, lhs);
4501 return (cond == kCondBE);
4502 }
4503 }
4504}
4505
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004506void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4507 LocationSummary* locations,
4508 MipsLabel* label) {
4509 Register lhs = locations->InAt(0).AsRegister<Register>();
4510 Location rhs_location = locations->InAt(1);
4511 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004512 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004513 bool use_imm = rhs_location.IsConstant();
4514 if (use_imm) {
4515 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4516 } else {
4517 rhs_reg = rhs_location.AsRegister<Register>();
4518 }
4519
4520 if (use_imm && rhs_imm == 0) {
4521 switch (cond) {
4522 case kCondEQ:
4523 case kCondBE: // <= 0 if zero
4524 __ Beqz(lhs, label);
4525 break;
4526 case kCondNE:
4527 case kCondA: // > 0 if non-zero
4528 __ Bnez(lhs, label);
4529 break;
4530 case kCondLT:
4531 __ Bltz(lhs, label);
4532 break;
4533 case kCondGE:
4534 __ Bgez(lhs, label);
4535 break;
4536 case kCondLE:
4537 __ Blez(lhs, label);
4538 break;
4539 case kCondGT:
4540 __ Bgtz(lhs, label);
4541 break;
4542 case kCondB: // always false
4543 break;
4544 case kCondAE: // always true
4545 __ B(label);
4546 break;
4547 }
4548 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004549 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4550 if (isR6 || !use_imm) {
4551 if (use_imm) {
4552 rhs_reg = TMP;
4553 __ LoadConst32(rhs_reg, rhs_imm);
4554 }
4555 switch (cond) {
4556 case kCondEQ:
4557 __ Beq(lhs, rhs_reg, label);
4558 break;
4559 case kCondNE:
4560 __ Bne(lhs, rhs_reg, label);
4561 break;
4562 case kCondLT:
4563 __ Blt(lhs, rhs_reg, label);
4564 break;
4565 case kCondGE:
4566 __ Bge(lhs, rhs_reg, label);
4567 break;
4568 case kCondLE:
4569 __ Bge(rhs_reg, lhs, label);
4570 break;
4571 case kCondGT:
4572 __ Blt(rhs_reg, lhs, label);
4573 break;
4574 case kCondB:
4575 __ Bltu(lhs, rhs_reg, label);
4576 break;
4577 case kCondAE:
4578 __ Bgeu(lhs, rhs_reg, label);
4579 break;
4580 case kCondBE:
4581 __ Bgeu(rhs_reg, lhs, label);
4582 break;
4583 case kCondA:
4584 __ Bltu(rhs_reg, lhs, label);
4585 break;
4586 }
4587 } else {
4588 // Special cases for more efficient comparison with constants on R2.
4589 switch (cond) {
4590 case kCondEQ:
4591 __ LoadConst32(TMP, rhs_imm);
4592 __ Beq(lhs, TMP, label);
4593 break;
4594 case kCondNE:
4595 __ LoadConst32(TMP, rhs_imm);
4596 __ Bne(lhs, TMP, label);
4597 break;
4598 case kCondLT:
4599 if (IsInt<16>(rhs_imm)) {
4600 __ Slti(TMP, lhs, rhs_imm);
4601 __ Bnez(TMP, label);
4602 } else {
4603 __ LoadConst32(TMP, rhs_imm);
4604 __ Blt(lhs, TMP, label);
4605 }
4606 break;
4607 case kCondGE:
4608 if (IsInt<16>(rhs_imm)) {
4609 __ Slti(TMP, lhs, rhs_imm);
4610 __ Beqz(TMP, label);
4611 } else {
4612 __ LoadConst32(TMP, rhs_imm);
4613 __ Bge(lhs, TMP, label);
4614 }
4615 break;
4616 case kCondLE:
4617 if (IsInt<16>(rhs_imm + 1)) {
4618 // Simulate lhs <= rhs via lhs < rhs + 1.
4619 __ Slti(TMP, lhs, rhs_imm + 1);
4620 __ Bnez(TMP, label);
4621 } else {
4622 __ LoadConst32(TMP, rhs_imm);
4623 __ Bge(TMP, lhs, label);
4624 }
4625 break;
4626 case kCondGT:
4627 if (IsInt<16>(rhs_imm + 1)) {
4628 // Simulate lhs > rhs via !(lhs < rhs + 1).
4629 __ Slti(TMP, lhs, rhs_imm + 1);
4630 __ Beqz(TMP, label);
4631 } else {
4632 __ LoadConst32(TMP, rhs_imm);
4633 __ Blt(TMP, lhs, label);
4634 }
4635 break;
4636 case kCondB:
4637 if (IsInt<16>(rhs_imm)) {
4638 __ Sltiu(TMP, lhs, rhs_imm);
4639 __ Bnez(TMP, label);
4640 } else {
4641 __ LoadConst32(TMP, rhs_imm);
4642 __ Bltu(lhs, TMP, label);
4643 }
4644 break;
4645 case kCondAE:
4646 if (IsInt<16>(rhs_imm)) {
4647 __ Sltiu(TMP, lhs, rhs_imm);
4648 __ Beqz(TMP, label);
4649 } else {
4650 __ LoadConst32(TMP, rhs_imm);
4651 __ Bgeu(lhs, TMP, label);
4652 }
4653 break;
4654 case kCondBE:
4655 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4656 // Simulate lhs <= rhs via lhs < rhs + 1.
4657 // Note that this only works if rhs + 1 does not overflow
4658 // to 0, hence the check above.
4659 __ Sltiu(TMP, lhs, rhs_imm + 1);
4660 __ Bnez(TMP, label);
4661 } else {
4662 __ LoadConst32(TMP, rhs_imm);
4663 __ Bgeu(TMP, lhs, label);
4664 }
4665 break;
4666 case kCondA:
4667 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4668 // Simulate lhs > rhs via !(lhs < rhs + 1).
4669 // Note that this only works if rhs + 1 does not overflow
4670 // to 0, hence the check above.
4671 __ Sltiu(TMP, lhs, rhs_imm + 1);
4672 __ Beqz(TMP, label);
4673 } else {
4674 __ LoadConst32(TMP, rhs_imm);
4675 __ Bltu(TMP, lhs, label);
4676 }
4677 break;
4678 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004679 }
4680 }
4681}
4682
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004683void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4684 LocationSummary* locations) {
4685 Register dst = locations->Out().AsRegister<Register>();
4686 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4687 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4688 Location rhs_location = locations->InAt(1);
4689 Register rhs_high = ZERO;
4690 Register rhs_low = ZERO;
4691 int64_t imm = 0;
4692 uint32_t imm_high = 0;
4693 uint32_t imm_low = 0;
4694 bool use_imm = rhs_location.IsConstant();
4695 if (use_imm) {
4696 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4697 imm_high = High32Bits(imm);
4698 imm_low = Low32Bits(imm);
4699 } else {
4700 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4701 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4702 }
4703 if (use_imm && imm == 0) {
4704 switch (cond) {
4705 case kCondEQ:
4706 case kCondBE: // <= 0 if zero
4707 __ Or(dst, lhs_high, lhs_low);
4708 __ Sltiu(dst, dst, 1);
4709 break;
4710 case kCondNE:
4711 case kCondA: // > 0 if non-zero
4712 __ Or(dst, lhs_high, lhs_low);
4713 __ Sltu(dst, ZERO, dst);
4714 break;
4715 case kCondLT:
4716 __ Slt(dst, lhs_high, ZERO);
4717 break;
4718 case kCondGE:
4719 __ Slt(dst, lhs_high, ZERO);
4720 __ Xori(dst, dst, 1);
4721 break;
4722 case kCondLE:
4723 __ Or(TMP, lhs_high, lhs_low);
4724 __ Sra(AT, lhs_high, 31);
4725 __ Sltu(dst, AT, TMP);
4726 __ Xori(dst, dst, 1);
4727 break;
4728 case kCondGT:
4729 __ Or(TMP, lhs_high, lhs_low);
4730 __ Sra(AT, lhs_high, 31);
4731 __ Sltu(dst, AT, TMP);
4732 break;
4733 case kCondB: // always false
4734 __ Andi(dst, dst, 0);
4735 break;
4736 case kCondAE: // always true
4737 __ Ori(dst, ZERO, 1);
4738 break;
4739 }
4740 } else if (use_imm) {
4741 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4742 switch (cond) {
4743 case kCondEQ:
4744 __ LoadConst32(TMP, imm_high);
4745 __ Xor(TMP, TMP, lhs_high);
4746 __ LoadConst32(AT, imm_low);
4747 __ Xor(AT, AT, lhs_low);
4748 __ Or(dst, TMP, AT);
4749 __ Sltiu(dst, dst, 1);
4750 break;
4751 case kCondNE:
4752 __ LoadConst32(TMP, imm_high);
4753 __ Xor(TMP, TMP, lhs_high);
4754 __ LoadConst32(AT, imm_low);
4755 __ Xor(AT, AT, lhs_low);
4756 __ Or(dst, TMP, AT);
4757 __ Sltu(dst, ZERO, dst);
4758 break;
4759 case kCondLT:
4760 case kCondGE:
4761 if (dst == lhs_low) {
4762 __ LoadConst32(TMP, imm_low);
4763 __ Sltu(dst, lhs_low, TMP);
4764 }
4765 __ LoadConst32(TMP, imm_high);
4766 __ Slt(AT, lhs_high, TMP);
4767 __ Slt(TMP, TMP, lhs_high);
4768 if (dst != lhs_low) {
4769 __ LoadConst32(dst, imm_low);
4770 __ Sltu(dst, lhs_low, dst);
4771 }
4772 __ Slt(dst, TMP, dst);
4773 __ Or(dst, dst, AT);
4774 if (cond == kCondGE) {
4775 __ Xori(dst, dst, 1);
4776 }
4777 break;
4778 case kCondGT:
4779 case kCondLE:
4780 if (dst == lhs_low) {
4781 __ LoadConst32(TMP, imm_low);
4782 __ Sltu(dst, TMP, lhs_low);
4783 }
4784 __ LoadConst32(TMP, imm_high);
4785 __ Slt(AT, TMP, lhs_high);
4786 __ Slt(TMP, lhs_high, TMP);
4787 if (dst != lhs_low) {
4788 __ LoadConst32(dst, imm_low);
4789 __ Sltu(dst, dst, lhs_low);
4790 }
4791 __ Slt(dst, TMP, dst);
4792 __ Or(dst, dst, AT);
4793 if (cond == kCondLE) {
4794 __ Xori(dst, dst, 1);
4795 }
4796 break;
4797 case kCondB:
4798 case kCondAE:
4799 if (dst == lhs_low) {
4800 __ LoadConst32(TMP, imm_low);
4801 __ Sltu(dst, lhs_low, TMP);
4802 }
4803 __ LoadConst32(TMP, imm_high);
4804 __ Sltu(AT, lhs_high, TMP);
4805 __ Sltu(TMP, TMP, lhs_high);
4806 if (dst != lhs_low) {
4807 __ LoadConst32(dst, imm_low);
4808 __ Sltu(dst, lhs_low, dst);
4809 }
4810 __ Slt(dst, TMP, dst);
4811 __ Or(dst, dst, AT);
4812 if (cond == kCondAE) {
4813 __ Xori(dst, dst, 1);
4814 }
4815 break;
4816 case kCondA:
4817 case kCondBE:
4818 if (dst == lhs_low) {
4819 __ LoadConst32(TMP, imm_low);
4820 __ Sltu(dst, TMP, lhs_low);
4821 }
4822 __ LoadConst32(TMP, imm_high);
4823 __ Sltu(AT, TMP, lhs_high);
4824 __ Sltu(TMP, lhs_high, TMP);
4825 if (dst != lhs_low) {
4826 __ LoadConst32(dst, imm_low);
4827 __ Sltu(dst, dst, lhs_low);
4828 }
4829 __ Slt(dst, TMP, dst);
4830 __ Or(dst, dst, AT);
4831 if (cond == kCondBE) {
4832 __ Xori(dst, dst, 1);
4833 }
4834 break;
4835 }
4836 } else {
4837 switch (cond) {
4838 case kCondEQ:
4839 __ Xor(TMP, lhs_high, rhs_high);
4840 __ Xor(AT, lhs_low, rhs_low);
4841 __ Or(dst, TMP, AT);
4842 __ Sltiu(dst, dst, 1);
4843 break;
4844 case kCondNE:
4845 __ Xor(TMP, lhs_high, rhs_high);
4846 __ Xor(AT, lhs_low, rhs_low);
4847 __ Or(dst, TMP, AT);
4848 __ Sltu(dst, ZERO, dst);
4849 break;
4850 case kCondLT:
4851 case kCondGE:
4852 __ Slt(TMP, rhs_high, lhs_high);
4853 __ Sltu(AT, lhs_low, rhs_low);
4854 __ Slt(TMP, TMP, AT);
4855 __ Slt(AT, lhs_high, rhs_high);
4856 __ Or(dst, AT, TMP);
4857 if (cond == kCondGE) {
4858 __ Xori(dst, dst, 1);
4859 }
4860 break;
4861 case kCondGT:
4862 case kCondLE:
4863 __ Slt(TMP, lhs_high, rhs_high);
4864 __ Sltu(AT, rhs_low, lhs_low);
4865 __ Slt(TMP, TMP, AT);
4866 __ Slt(AT, rhs_high, lhs_high);
4867 __ Or(dst, AT, TMP);
4868 if (cond == kCondLE) {
4869 __ Xori(dst, dst, 1);
4870 }
4871 break;
4872 case kCondB:
4873 case kCondAE:
4874 __ Sltu(TMP, rhs_high, lhs_high);
4875 __ Sltu(AT, lhs_low, rhs_low);
4876 __ Slt(TMP, TMP, AT);
4877 __ Sltu(AT, lhs_high, rhs_high);
4878 __ Or(dst, AT, TMP);
4879 if (cond == kCondAE) {
4880 __ Xori(dst, dst, 1);
4881 }
4882 break;
4883 case kCondA:
4884 case kCondBE:
4885 __ Sltu(TMP, lhs_high, rhs_high);
4886 __ Sltu(AT, rhs_low, lhs_low);
4887 __ Slt(TMP, TMP, AT);
4888 __ Sltu(AT, rhs_high, lhs_high);
4889 __ Or(dst, AT, TMP);
4890 if (cond == kCondBE) {
4891 __ Xori(dst, dst, 1);
4892 }
4893 break;
4894 }
4895 }
4896}
4897
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004898void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4899 LocationSummary* locations,
4900 MipsLabel* label) {
4901 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4902 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4903 Location rhs_location = locations->InAt(1);
4904 Register rhs_high = ZERO;
4905 Register rhs_low = ZERO;
4906 int64_t imm = 0;
4907 uint32_t imm_high = 0;
4908 uint32_t imm_low = 0;
4909 bool use_imm = rhs_location.IsConstant();
4910 if (use_imm) {
4911 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4912 imm_high = High32Bits(imm);
4913 imm_low = Low32Bits(imm);
4914 } else {
4915 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4916 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4917 }
4918
4919 if (use_imm && imm == 0) {
4920 switch (cond) {
4921 case kCondEQ:
4922 case kCondBE: // <= 0 if zero
4923 __ Or(TMP, lhs_high, lhs_low);
4924 __ Beqz(TMP, label);
4925 break;
4926 case kCondNE:
4927 case kCondA: // > 0 if non-zero
4928 __ Or(TMP, lhs_high, lhs_low);
4929 __ Bnez(TMP, label);
4930 break;
4931 case kCondLT:
4932 __ Bltz(lhs_high, label);
4933 break;
4934 case kCondGE:
4935 __ Bgez(lhs_high, label);
4936 break;
4937 case kCondLE:
4938 __ Or(TMP, lhs_high, lhs_low);
4939 __ Sra(AT, lhs_high, 31);
4940 __ Bgeu(AT, TMP, label);
4941 break;
4942 case kCondGT:
4943 __ Or(TMP, lhs_high, lhs_low);
4944 __ Sra(AT, lhs_high, 31);
4945 __ Bltu(AT, TMP, label);
4946 break;
4947 case kCondB: // always false
4948 break;
4949 case kCondAE: // always true
4950 __ B(label);
4951 break;
4952 }
4953 } else if (use_imm) {
4954 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4955 switch (cond) {
4956 case kCondEQ:
4957 __ LoadConst32(TMP, imm_high);
4958 __ Xor(TMP, TMP, lhs_high);
4959 __ LoadConst32(AT, imm_low);
4960 __ Xor(AT, AT, lhs_low);
4961 __ Or(TMP, TMP, AT);
4962 __ Beqz(TMP, label);
4963 break;
4964 case kCondNE:
4965 __ LoadConst32(TMP, imm_high);
4966 __ Xor(TMP, TMP, lhs_high);
4967 __ LoadConst32(AT, imm_low);
4968 __ Xor(AT, AT, lhs_low);
4969 __ Or(TMP, TMP, AT);
4970 __ Bnez(TMP, label);
4971 break;
4972 case kCondLT:
4973 __ LoadConst32(TMP, imm_high);
4974 __ Blt(lhs_high, TMP, label);
4975 __ Slt(TMP, TMP, lhs_high);
4976 __ LoadConst32(AT, imm_low);
4977 __ Sltu(AT, lhs_low, AT);
4978 __ Blt(TMP, AT, label);
4979 break;
4980 case kCondGE:
4981 __ LoadConst32(TMP, imm_high);
4982 __ Blt(TMP, lhs_high, label);
4983 __ Slt(TMP, lhs_high, TMP);
4984 __ LoadConst32(AT, imm_low);
4985 __ Sltu(AT, lhs_low, AT);
4986 __ Or(TMP, TMP, AT);
4987 __ Beqz(TMP, label);
4988 break;
4989 case kCondLE:
4990 __ LoadConst32(TMP, imm_high);
4991 __ Blt(lhs_high, TMP, label);
4992 __ Slt(TMP, TMP, lhs_high);
4993 __ LoadConst32(AT, imm_low);
4994 __ Sltu(AT, AT, lhs_low);
4995 __ Or(TMP, TMP, AT);
4996 __ Beqz(TMP, label);
4997 break;
4998 case kCondGT:
4999 __ LoadConst32(TMP, imm_high);
5000 __ Blt(TMP, lhs_high, label);
5001 __ Slt(TMP, lhs_high, TMP);
5002 __ LoadConst32(AT, imm_low);
5003 __ Sltu(AT, AT, lhs_low);
5004 __ Blt(TMP, AT, label);
5005 break;
5006 case kCondB:
5007 __ LoadConst32(TMP, imm_high);
5008 __ Bltu(lhs_high, TMP, label);
5009 __ Sltu(TMP, TMP, lhs_high);
5010 __ LoadConst32(AT, imm_low);
5011 __ Sltu(AT, lhs_low, AT);
5012 __ Blt(TMP, AT, label);
5013 break;
5014 case kCondAE:
5015 __ LoadConst32(TMP, imm_high);
5016 __ Bltu(TMP, lhs_high, label);
5017 __ Sltu(TMP, lhs_high, TMP);
5018 __ LoadConst32(AT, imm_low);
5019 __ Sltu(AT, lhs_low, AT);
5020 __ Or(TMP, TMP, AT);
5021 __ Beqz(TMP, label);
5022 break;
5023 case kCondBE:
5024 __ LoadConst32(TMP, imm_high);
5025 __ Bltu(lhs_high, TMP, label);
5026 __ Sltu(TMP, TMP, lhs_high);
5027 __ LoadConst32(AT, imm_low);
5028 __ Sltu(AT, AT, lhs_low);
5029 __ Or(TMP, TMP, AT);
5030 __ Beqz(TMP, label);
5031 break;
5032 case kCondA:
5033 __ LoadConst32(TMP, imm_high);
5034 __ Bltu(TMP, lhs_high, label);
5035 __ Sltu(TMP, lhs_high, TMP);
5036 __ LoadConst32(AT, imm_low);
5037 __ Sltu(AT, AT, lhs_low);
5038 __ Blt(TMP, AT, label);
5039 break;
5040 }
5041 } else {
5042 switch (cond) {
5043 case kCondEQ:
5044 __ Xor(TMP, lhs_high, rhs_high);
5045 __ Xor(AT, lhs_low, rhs_low);
5046 __ Or(TMP, TMP, AT);
5047 __ Beqz(TMP, label);
5048 break;
5049 case kCondNE:
5050 __ Xor(TMP, lhs_high, rhs_high);
5051 __ Xor(AT, lhs_low, rhs_low);
5052 __ Or(TMP, TMP, AT);
5053 __ Bnez(TMP, label);
5054 break;
5055 case kCondLT:
5056 __ Blt(lhs_high, rhs_high, label);
5057 __ Slt(TMP, rhs_high, lhs_high);
5058 __ Sltu(AT, lhs_low, rhs_low);
5059 __ Blt(TMP, AT, label);
5060 break;
5061 case kCondGE:
5062 __ Blt(rhs_high, lhs_high, label);
5063 __ Slt(TMP, lhs_high, rhs_high);
5064 __ Sltu(AT, lhs_low, rhs_low);
5065 __ Or(TMP, TMP, AT);
5066 __ Beqz(TMP, label);
5067 break;
5068 case kCondLE:
5069 __ Blt(lhs_high, rhs_high, label);
5070 __ Slt(TMP, rhs_high, lhs_high);
5071 __ Sltu(AT, rhs_low, lhs_low);
5072 __ Or(TMP, TMP, AT);
5073 __ Beqz(TMP, label);
5074 break;
5075 case kCondGT:
5076 __ Blt(rhs_high, lhs_high, label);
5077 __ Slt(TMP, lhs_high, rhs_high);
5078 __ Sltu(AT, rhs_low, lhs_low);
5079 __ Blt(TMP, AT, label);
5080 break;
5081 case kCondB:
5082 __ Bltu(lhs_high, rhs_high, label);
5083 __ Sltu(TMP, rhs_high, lhs_high);
5084 __ Sltu(AT, lhs_low, rhs_low);
5085 __ Blt(TMP, AT, label);
5086 break;
5087 case kCondAE:
5088 __ Bltu(rhs_high, lhs_high, label);
5089 __ Sltu(TMP, lhs_high, rhs_high);
5090 __ Sltu(AT, lhs_low, rhs_low);
5091 __ Or(TMP, TMP, AT);
5092 __ Beqz(TMP, label);
5093 break;
5094 case kCondBE:
5095 __ Bltu(lhs_high, rhs_high, label);
5096 __ Sltu(TMP, rhs_high, lhs_high);
5097 __ Sltu(AT, rhs_low, lhs_low);
5098 __ Or(TMP, TMP, AT);
5099 __ Beqz(TMP, label);
5100 break;
5101 case kCondA:
5102 __ Bltu(rhs_high, lhs_high, label);
5103 __ Sltu(TMP, lhs_high, rhs_high);
5104 __ Sltu(AT, rhs_low, lhs_low);
5105 __ Blt(TMP, AT, label);
5106 break;
5107 }
5108 }
5109}
5110
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005111void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5112 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005113 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005114 LocationSummary* locations) {
5115 Register dst = locations->Out().AsRegister<Register>();
5116 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5117 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5118 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005119 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005120 if (isR6) {
5121 switch (cond) {
5122 case kCondEQ:
5123 __ CmpEqS(FTMP, lhs, rhs);
5124 __ Mfc1(dst, FTMP);
5125 __ Andi(dst, dst, 1);
5126 break;
5127 case kCondNE:
5128 __ CmpEqS(FTMP, lhs, rhs);
5129 __ Mfc1(dst, FTMP);
5130 __ Addiu(dst, dst, 1);
5131 break;
5132 case kCondLT:
5133 if (gt_bias) {
5134 __ CmpLtS(FTMP, lhs, rhs);
5135 } else {
5136 __ CmpUltS(FTMP, lhs, rhs);
5137 }
5138 __ Mfc1(dst, FTMP);
5139 __ Andi(dst, dst, 1);
5140 break;
5141 case kCondLE:
5142 if (gt_bias) {
5143 __ CmpLeS(FTMP, lhs, rhs);
5144 } else {
5145 __ CmpUleS(FTMP, lhs, rhs);
5146 }
5147 __ Mfc1(dst, FTMP);
5148 __ Andi(dst, dst, 1);
5149 break;
5150 case kCondGT:
5151 if (gt_bias) {
5152 __ CmpUltS(FTMP, rhs, lhs);
5153 } else {
5154 __ CmpLtS(FTMP, rhs, lhs);
5155 }
5156 __ Mfc1(dst, FTMP);
5157 __ Andi(dst, dst, 1);
5158 break;
5159 case kCondGE:
5160 if (gt_bias) {
5161 __ CmpUleS(FTMP, rhs, lhs);
5162 } else {
5163 __ CmpLeS(FTMP, rhs, lhs);
5164 }
5165 __ Mfc1(dst, FTMP);
5166 __ Andi(dst, dst, 1);
5167 break;
5168 default:
5169 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5170 UNREACHABLE();
5171 }
5172 } else {
5173 switch (cond) {
5174 case kCondEQ:
5175 __ CeqS(0, lhs, rhs);
5176 __ LoadConst32(dst, 1);
5177 __ Movf(dst, ZERO, 0);
5178 break;
5179 case kCondNE:
5180 __ CeqS(0, lhs, rhs);
5181 __ LoadConst32(dst, 1);
5182 __ Movt(dst, ZERO, 0);
5183 break;
5184 case kCondLT:
5185 if (gt_bias) {
5186 __ ColtS(0, lhs, rhs);
5187 } else {
5188 __ CultS(0, lhs, rhs);
5189 }
5190 __ LoadConst32(dst, 1);
5191 __ Movf(dst, ZERO, 0);
5192 break;
5193 case kCondLE:
5194 if (gt_bias) {
5195 __ ColeS(0, lhs, rhs);
5196 } else {
5197 __ CuleS(0, lhs, rhs);
5198 }
5199 __ LoadConst32(dst, 1);
5200 __ Movf(dst, ZERO, 0);
5201 break;
5202 case kCondGT:
5203 if (gt_bias) {
5204 __ CultS(0, rhs, lhs);
5205 } else {
5206 __ ColtS(0, rhs, lhs);
5207 }
5208 __ LoadConst32(dst, 1);
5209 __ Movf(dst, ZERO, 0);
5210 break;
5211 case kCondGE:
5212 if (gt_bias) {
5213 __ CuleS(0, rhs, lhs);
5214 } else {
5215 __ ColeS(0, rhs, lhs);
5216 }
5217 __ LoadConst32(dst, 1);
5218 __ Movf(dst, ZERO, 0);
5219 break;
5220 default:
5221 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5222 UNREACHABLE();
5223 }
5224 }
5225 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005226 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005227 if (isR6) {
5228 switch (cond) {
5229 case kCondEQ:
5230 __ CmpEqD(FTMP, lhs, rhs);
5231 __ Mfc1(dst, FTMP);
5232 __ Andi(dst, dst, 1);
5233 break;
5234 case kCondNE:
5235 __ CmpEqD(FTMP, lhs, rhs);
5236 __ Mfc1(dst, FTMP);
5237 __ Addiu(dst, dst, 1);
5238 break;
5239 case kCondLT:
5240 if (gt_bias) {
5241 __ CmpLtD(FTMP, lhs, rhs);
5242 } else {
5243 __ CmpUltD(FTMP, lhs, rhs);
5244 }
5245 __ Mfc1(dst, FTMP);
5246 __ Andi(dst, dst, 1);
5247 break;
5248 case kCondLE:
5249 if (gt_bias) {
5250 __ CmpLeD(FTMP, lhs, rhs);
5251 } else {
5252 __ CmpUleD(FTMP, lhs, rhs);
5253 }
5254 __ Mfc1(dst, FTMP);
5255 __ Andi(dst, dst, 1);
5256 break;
5257 case kCondGT:
5258 if (gt_bias) {
5259 __ CmpUltD(FTMP, rhs, lhs);
5260 } else {
5261 __ CmpLtD(FTMP, rhs, lhs);
5262 }
5263 __ Mfc1(dst, FTMP);
5264 __ Andi(dst, dst, 1);
5265 break;
5266 case kCondGE:
5267 if (gt_bias) {
5268 __ CmpUleD(FTMP, rhs, lhs);
5269 } else {
5270 __ CmpLeD(FTMP, rhs, lhs);
5271 }
5272 __ Mfc1(dst, FTMP);
5273 __ Andi(dst, dst, 1);
5274 break;
5275 default:
5276 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5277 UNREACHABLE();
5278 }
5279 } else {
5280 switch (cond) {
5281 case kCondEQ:
5282 __ CeqD(0, lhs, rhs);
5283 __ LoadConst32(dst, 1);
5284 __ Movf(dst, ZERO, 0);
5285 break;
5286 case kCondNE:
5287 __ CeqD(0, lhs, rhs);
5288 __ LoadConst32(dst, 1);
5289 __ Movt(dst, ZERO, 0);
5290 break;
5291 case kCondLT:
5292 if (gt_bias) {
5293 __ ColtD(0, lhs, rhs);
5294 } else {
5295 __ CultD(0, lhs, rhs);
5296 }
5297 __ LoadConst32(dst, 1);
5298 __ Movf(dst, ZERO, 0);
5299 break;
5300 case kCondLE:
5301 if (gt_bias) {
5302 __ ColeD(0, lhs, rhs);
5303 } else {
5304 __ CuleD(0, lhs, rhs);
5305 }
5306 __ LoadConst32(dst, 1);
5307 __ Movf(dst, ZERO, 0);
5308 break;
5309 case kCondGT:
5310 if (gt_bias) {
5311 __ CultD(0, rhs, lhs);
5312 } else {
5313 __ ColtD(0, rhs, lhs);
5314 }
5315 __ LoadConst32(dst, 1);
5316 __ Movf(dst, ZERO, 0);
5317 break;
5318 case kCondGE:
5319 if (gt_bias) {
5320 __ CuleD(0, rhs, lhs);
5321 } else {
5322 __ ColeD(0, rhs, lhs);
5323 }
5324 __ LoadConst32(dst, 1);
5325 __ Movf(dst, ZERO, 0);
5326 break;
5327 default:
5328 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5329 UNREACHABLE();
5330 }
5331 }
5332 }
5333}
5334
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005335bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5336 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005337 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005338 LocationSummary* input_locations,
5339 int cc) {
5340 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5341 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5342 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005343 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005344 switch (cond) {
5345 case kCondEQ:
5346 __ CeqS(cc, lhs, rhs);
5347 return false;
5348 case kCondNE:
5349 __ CeqS(cc, lhs, rhs);
5350 return true;
5351 case kCondLT:
5352 if (gt_bias) {
5353 __ ColtS(cc, lhs, rhs);
5354 } else {
5355 __ CultS(cc, lhs, rhs);
5356 }
5357 return false;
5358 case kCondLE:
5359 if (gt_bias) {
5360 __ ColeS(cc, lhs, rhs);
5361 } else {
5362 __ CuleS(cc, lhs, rhs);
5363 }
5364 return false;
5365 case kCondGT:
5366 if (gt_bias) {
5367 __ CultS(cc, rhs, lhs);
5368 } else {
5369 __ ColtS(cc, rhs, lhs);
5370 }
5371 return false;
5372 case kCondGE:
5373 if (gt_bias) {
5374 __ CuleS(cc, rhs, lhs);
5375 } else {
5376 __ ColeS(cc, rhs, lhs);
5377 }
5378 return false;
5379 default:
5380 LOG(FATAL) << "Unexpected non-floating-point condition";
5381 UNREACHABLE();
5382 }
5383 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005384 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005385 switch (cond) {
5386 case kCondEQ:
5387 __ CeqD(cc, lhs, rhs);
5388 return false;
5389 case kCondNE:
5390 __ CeqD(cc, lhs, rhs);
5391 return true;
5392 case kCondLT:
5393 if (gt_bias) {
5394 __ ColtD(cc, lhs, rhs);
5395 } else {
5396 __ CultD(cc, lhs, rhs);
5397 }
5398 return false;
5399 case kCondLE:
5400 if (gt_bias) {
5401 __ ColeD(cc, lhs, rhs);
5402 } else {
5403 __ CuleD(cc, lhs, rhs);
5404 }
5405 return false;
5406 case kCondGT:
5407 if (gt_bias) {
5408 __ CultD(cc, rhs, lhs);
5409 } else {
5410 __ ColtD(cc, rhs, lhs);
5411 }
5412 return false;
5413 case kCondGE:
5414 if (gt_bias) {
5415 __ CuleD(cc, rhs, lhs);
5416 } else {
5417 __ ColeD(cc, rhs, lhs);
5418 }
5419 return false;
5420 default:
5421 LOG(FATAL) << "Unexpected non-floating-point condition";
5422 UNREACHABLE();
5423 }
5424 }
5425}
5426
5427bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5428 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005429 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005430 LocationSummary* input_locations,
5431 FRegister dst) {
5432 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5433 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5434 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005435 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005436 switch (cond) {
5437 case kCondEQ:
5438 __ CmpEqS(dst, lhs, rhs);
5439 return false;
5440 case kCondNE:
5441 __ CmpEqS(dst, lhs, rhs);
5442 return true;
5443 case kCondLT:
5444 if (gt_bias) {
5445 __ CmpLtS(dst, lhs, rhs);
5446 } else {
5447 __ CmpUltS(dst, lhs, rhs);
5448 }
5449 return false;
5450 case kCondLE:
5451 if (gt_bias) {
5452 __ CmpLeS(dst, lhs, rhs);
5453 } else {
5454 __ CmpUleS(dst, lhs, rhs);
5455 }
5456 return false;
5457 case kCondGT:
5458 if (gt_bias) {
5459 __ CmpUltS(dst, rhs, lhs);
5460 } else {
5461 __ CmpLtS(dst, rhs, lhs);
5462 }
5463 return false;
5464 case kCondGE:
5465 if (gt_bias) {
5466 __ CmpUleS(dst, rhs, lhs);
5467 } else {
5468 __ CmpLeS(dst, rhs, lhs);
5469 }
5470 return false;
5471 default:
5472 LOG(FATAL) << "Unexpected non-floating-point condition";
5473 UNREACHABLE();
5474 }
5475 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005476 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005477 switch (cond) {
5478 case kCondEQ:
5479 __ CmpEqD(dst, lhs, rhs);
5480 return false;
5481 case kCondNE:
5482 __ CmpEqD(dst, lhs, rhs);
5483 return true;
5484 case kCondLT:
5485 if (gt_bias) {
5486 __ CmpLtD(dst, lhs, rhs);
5487 } else {
5488 __ CmpUltD(dst, lhs, rhs);
5489 }
5490 return false;
5491 case kCondLE:
5492 if (gt_bias) {
5493 __ CmpLeD(dst, lhs, rhs);
5494 } else {
5495 __ CmpUleD(dst, lhs, rhs);
5496 }
5497 return false;
5498 case kCondGT:
5499 if (gt_bias) {
5500 __ CmpUltD(dst, rhs, lhs);
5501 } else {
5502 __ CmpLtD(dst, rhs, lhs);
5503 }
5504 return false;
5505 case kCondGE:
5506 if (gt_bias) {
5507 __ CmpUleD(dst, rhs, lhs);
5508 } else {
5509 __ CmpLeD(dst, rhs, lhs);
5510 }
5511 return false;
5512 default:
5513 LOG(FATAL) << "Unexpected non-floating-point condition";
5514 UNREACHABLE();
5515 }
5516 }
5517}
5518
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005519void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5520 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005521 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005522 LocationSummary* locations,
5523 MipsLabel* label) {
5524 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5525 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5526 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005527 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005528 if (isR6) {
5529 switch (cond) {
5530 case kCondEQ:
5531 __ CmpEqS(FTMP, lhs, rhs);
5532 __ Bc1nez(FTMP, label);
5533 break;
5534 case kCondNE:
5535 __ CmpEqS(FTMP, lhs, rhs);
5536 __ Bc1eqz(FTMP, label);
5537 break;
5538 case kCondLT:
5539 if (gt_bias) {
5540 __ CmpLtS(FTMP, lhs, rhs);
5541 } else {
5542 __ CmpUltS(FTMP, lhs, rhs);
5543 }
5544 __ Bc1nez(FTMP, label);
5545 break;
5546 case kCondLE:
5547 if (gt_bias) {
5548 __ CmpLeS(FTMP, lhs, rhs);
5549 } else {
5550 __ CmpUleS(FTMP, lhs, rhs);
5551 }
5552 __ Bc1nez(FTMP, label);
5553 break;
5554 case kCondGT:
5555 if (gt_bias) {
5556 __ CmpUltS(FTMP, rhs, lhs);
5557 } else {
5558 __ CmpLtS(FTMP, rhs, lhs);
5559 }
5560 __ Bc1nez(FTMP, label);
5561 break;
5562 case kCondGE:
5563 if (gt_bias) {
5564 __ CmpUleS(FTMP, rhs, lhs);
5565 } else {
5566 __ CmpLeS(FTMP, rhs, lhs);
5567 }
5568 __ Bc1nez(FTMP, label);
5569 break;
5570 default:
5571 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005572 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005573 }
5574 } else {
5575 switch (cond) {
5576 case kCondEQ:
5577 __ CeqS(0, lhs, rhs);
5578 __ Bc1t(0, label);
5579 break;
5580 case kCondNE:
5581 __ CeqS(0, lhs, rhs);
5582 __ Bc1f(0, label);
5583 break;
5584 case kCondLT:
5585 if (gt_bias) {
5586 __ ColtS(0, lhs, rhs);
5587 } else {
5588 __ CultS(0, lhs, rhs);
5589 }
5590 __ Bc1t(0, label);
5591 break;
5592 case kCondLE:
5593 if (gt_bias) {
5594 __ ColeS(0, lhs, rhs);
5595 } else {
5596 __ CuleS(0, lhs, rhs);
5597 }
5598 __ Bc1t(0, label);
5599 break;
5600 case kCondGT:
5601 if (gt_bias) {
5602 __ CultS(0, rhs, lhs);
5603 } else {
5604 __ ColtS(0, rhs, lhs);
5605 }
5606 __ Bc1t(0, label);
5607 break;
5608 case kCondGE:
5609 if (gt_bias) {
5610 __ CuleS(0, rhs, lhs);
5611 } else {
5612 __ ColeS(0, rhs, lhs);
5613 }
5614 __ Bc1t(0, label);
5615 break;
5616 default:
5617 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005618 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005619 }
5620 }
5621 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005622 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005623 if (isR6) {
5624 switch (cond) {
5625 case kCondEQ:
5626 __ CmpEqD(FTMP, lhs, rhs);
5627 __ Bc1nez(FTMP, label);
5628 break;
5629 case kCondNE:
5630 __ CmpEqD(FTMP, lhs, rhs);
5631 __ Bc1eqz(FTMP, label);
5632 break;
5633 case kCondLT:
5634 if (gt_bias) {
5635 __ CmpLtD(FTMP, lhs, rhs);
5636 } else {
5637 __ CmpUltD(FTMP, lhs, rhs);
5638 }
5639 __ Bc1nez(FTMP, label);
5640 break;
5641 case kCondLE:
5642 if (gt_bias) {
5643 __ CmpLeD(FTMP, lhs, rhs);
5644 } else {
5645 __ CmpUleD(FTMP, lhs, rhs);
5646 }
5647 __ Bc1nez(FTMP, label);
5648 break;
5649 case kCondGT:
5650 if (gt_bias) {
5651 __ CmpUltD(FTMP, rhs, lhs);
5652 } else {
5653 __ CmpLtD(FTMP, rhs, lhs);
5654 }
5655 __ Bc1nez(FTMP, label);
5656 break;
5657 case kCondGE:
5658 if (gt_bias) {
5659 __ CmpUleD(FTMP, rhs, lhs);
5660 } else {
5661 __ CmpLeD(FTMP, rhs, lhs);
5662 }
5663 __ Bc1nez(FTMP, label);
5664 break;
5665 default:
5666 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005667 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005668 }
5669 } else {
5670 switch (cond) {
5671 case kCondEQ:
5672 __ CeqD(0, lhs, rhs);
5673 __ Bc1t(0, label);
5674 break;
5675 case kCondNE:
5676 __ CeqD(0, lhs, rhs);
5677 __ Bc1f(0, label);
5678 break;
5679 case kCondLT:
5680 if (gt_bias) {
5681 __ ColtD(0, lhs, rhs);
5682 } else {
5683 __ CultD(0, lhs, rhs);
5684 }
5685 __ Bc1t(0, label);
5686 break;
5687 case kCondLE:
5688 if (gt_bias) {
5689 __ ColeD(0, lhs, rhs);
5690 } else {
5691 __ CuleD(0, lhs, rhs);
5692 }
5693 __ Bc1t(0, label);
5694 break;
5695 case kCondGT:
5696 if (gt_bias) {
5697 __ CultD(0, rhs, lhs);
5698 } else {
5699 __ ColtD(0, rhs, lhs);
5700 }
5701 __ Bc1t(0, label);
5702 break;
5703 case kCondGE:
5704 if (gt_bias) {
5705 __ CuleD(0, rhs, lhs);
5706 } else {
5707 __ ColeD(0, rhs, lhs);
5708 }
5709 __ Bc1t(0, label);
5710 break;
5711 default:
5712 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005713 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005714 }
5715 }
5716 }
5717}
5718
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005719void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005720 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005721 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005722 MipsLabel* false_target) {
5723 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005724
David Brazdil0debae72015-11-12 18:37:00 +00005725 if (true_target == nullptr && false_target == nullptr) {
5726 // Nothing to do. The code always falls through.
5727 return;
5728 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005729 // Constant condition, statically compared against "true" (integer value 1).
5730 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005731 if (true_target != nullptr) {
5732 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005733 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005734 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005735 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005736 if (false_target != nullptr) {
5737 __ B(false_target);
5738 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005739 }
David Brazdil0debae72015-11-12 18:37:00 +00005740 return;
5741 }
5742
5743 // The following code generates these patterns:
5744 // (1) true_target == nullptr && false_target != nullptr
5745 // - opposite condition true => branch to false_target
5746 // (2) true_target != nullptr && false_target == nullptr
5747 // - condition true => branch to true_target
5748 // (3) true_target != nullptr && false_target != nullptr
5749 // - condition true => branch to true_target
5750 // - branch to false_target
5751 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005752 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005753 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005754 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005755 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005756 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5757 } else {
5758 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5759 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005760 } else {
5761 // The condition instruction has not been materialized, use its inputs as
5762 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005763 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005764 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005765 LocationSummary* locations = cond->GetLocations();
5766 IfCondition if_cond = condition->GetCondition();
5767 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005768
David Brazdil0debae72015-11-12 18:37:00 +00005769 if (true_target == nullptr) {
5770 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005771 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005772 }
5773
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005774 switch (type) {
5775 default:
5776 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5777 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005778 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005779 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5780 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005781 case DataType::Type::kFloat32:
5782 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005783 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5784 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005785 }
5786 }
David Brazdil0debae72015-11-12 18:37:00 +00005787
5788 // If neither branch falls through (case 3), the conditional branch to `true_target`
5789 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5790 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005791 __ B(false_target);
5792 }
5793}
5794
5795void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005796 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005797 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005798 locations->SetInAt(0, Location::RequiresRegister());
5799 }
5800}
5801
5802void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005803 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5804 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5805 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5806 nullptr : codegen_->GetLabelOf(true_successor);
5807 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5808 nullptr : codegen_->GetLabelOf(false_successor);
5809 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005810}
5811
5812void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005813 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005814 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005815 InvokeRuntimeCallingConvention calling_convention;
5816 RegisterSet caller_saves = RegisterSet::Empty();
5817 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5818 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005819 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005820 locations->SetInAt(0, Location::RequiresRegister());
5821 }
5822}
5823
5824void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005825 SlowPathCodeMIPS* slow_path =
5826 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005827 GenerateTestAndBranch(deoptimize,
5828 /* condition_input_index */ 0,
5829 slow_path->GetEntryLabel(),
5830 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005831}
5832
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005833// This function returns true if a conditional move can be generated for HSelect.
5834// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5835// branches and regular moves.
5836//
5837// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5838//
5839// While determining feasibility of a conditional move and setting inputs/outputs
5840// are two distinct tasks, this function does both because they share quite a bit
5841// of common logic.
5842static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5843 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5844 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5845 HCondition* condition = cond->AsCondition();
5846
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005847 DataType::Type cond_type =
5848 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5849 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005850
5851 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5852 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5853 bool is_true_value_zero_constant =
5854 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5855 bool is_false_value_zero_constant =
5856 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5857
5858 bool can_move_conditionally = false;
5859 bool use_const_for_false_in = false;
5860 bool use_const_for_true_in = false;
5861
5862 if (!cond->IsConstant()) {
5863 switch (cond_type) {
5864 default:
5865 switch (dst_type) {
5866 default:
5867 // Moving int on int condition.
5868 if (is_r6) {
5869 if (is_true_value_zero_constant) {
5870 // seleqz out_reg, false_reg, cond_reg
5871 can_move_conditionally = true;
5872 use_const_for_true_in = true;
5873 } else if (is_false_value_zero_constant) {
5874 // selnez out_reg, true_reg, cond_reg
5875 can_move_conditionally = true;
5876 use_const_for_false_in = true;
5877 } else if (materialized) {
5878 // Not materializing unmaterialized int conditions
5879 // to keep the instruction count low.
5880 // selnez AT, true_reg, cond_reg
5881 // seleqz TMP, false_reg, cond_reg
5882 // or out_reg, AT, TMP
5883 can_move_conditionally = true;
5884 }
5885 } else {
5886 // movn out_reg, true_reg/ZERO, cond_reg
5887 can_move_conditionally = true;
5888 use_const_for_true_in = is_true_value_zero_constant;
5889 }
5890 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005891 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005892 // Moving long on int condition.
5893 if (is_r6) {
5894 if (is_true_value_zero_constant) {
5895 // seleqz out_reg_lo, false_reg_lo, cond_reg
5896 // seleqz out_reg_hi, false_reg_hi, cond_reg
5897 can_move_conditionally = true;
5898 use_const_for_true_in = true;
5899 } else if (is_false_value_zero_constant) {
5900 // selnez out_reg_lo, true_reg_lo, cond_reg
5901 // selnez out_reg_hi, true_reg_hi, cond_reg
5902 can_move_conditionally = true;
5903 use_const_for_false_in = true;
5904 }
5905 // Other long conditional moves would generate 6+ instructions,
5906 // which is too many.
5907 } else {
5908 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5909 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5910 can_move_conditionally = true;
5911 use_const_for_true_in = is_true_value_zero_constant;
5912 }
5913 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005914 case DataType::Type::kFloat32:
5915 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005916 // Moving float/double on int condition.
5917 if (is_r6) {
5918 if (materialized) {
5919 // Not materializing unmaterialized int conditions
5920 // to keep the instruction count low.
5921 can_move_conditionally = true;
5922 if (is_true_value_zero_constant) {
5923 // sltu TMP, ZERO, cond_reg
5924 // mtc1 TMP, temp_cond_reg
5925 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5926 use_const_for_true_in = true;
5927 } else if (is_false_value_zero_constant) {
5928 // sltu TMP, ZERO, cond_reg
5929 // mtc1 TMP, temp_cond_reg
5930 // selnez.fmt out_reg, true_reg, temp_cond_reg
5931 use_const_for_false_in = true;
5932 } else {
5933 // sltu TMP, ZERO, cond_reg
5934 // mtc1 TMP, temp_cond_reg
5935 // sel.fmt temp_cond_reg, false_reg, true_reg
5936 // mov.fmt out_reg, temp_cond_reg
5937 }
5938 }
5939 } else {
5940 // movn.fmt out_reg, true_reg, cond_reg
5941 can_move_conditionally = true;
5942 }
5943 break;
5944 }
5945 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005946 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005947 // We don't materialize long comparison now
5948 // and use conditional branches instead.
5949 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005950 case DataType::Type::kFloat32:
5951 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005952 switch (dst_type) {
5953 default:
5954 // Moving int on float/double condition.
5955 if (is_r6) {
5956 if (is_true_value_zero_constant) {
5957 // mfc1 TMP, temp_cond_reg
5958 // seleqz out_reg, false_reg, TMP
5959 can_move_conditionally = true;
5960 use_const_for_true_in = true;
5961 } else if (is_false_value_zero_constant) {
5962 // mfc1 TMP, temp_cond_reg
5963 // selnez out_reg, true_reg, TMP
5964 can_move_conditionally = true;
5965 use_const_for_false_in = true;
5966 } else {
5967 // mfc1 TMP, temp_cond_reg
5968 // selnez AT, true_reg, TMP
5969 // seleqz TMP, false_reg, TMP
5970 // or out_reg, AT, TMP
5971 can_move_conditionally = true;
5972 }
5973 } else {
5974 // movt out_reg, true_reg/ZERO, cc
5975 can_move_conditionally = true;
5976 use_const_for_true_in = is_true_value_zero_constant;
5977 }
5978 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005979 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005980 // Moving long on float/double condition.
5981 if (is_r6) {
5982 if (is_true_value_zero_constant) {
5983 // mfc1 TMP, temp_cond_reg
5984 // seleqz out_reg_lo, false_reg_lo, TMP
5985 // seleqz out_reg_hi, false_reg_hi, TMP
5986 can_move_conditionally = true;
5987 use_const_for_true_in = true;
5988 } else if (is_false_value_zero_constant) {
5989 // mfc1 TMP, temp_cond_reg
5990 // selnez out_reg_lo, true_reg_lo, TMP
5991 // selnez out_reg_hi, true_reg_hi, TMP
5992 can_move_conditionally = true;
5993 use_const_for_false_in = true;
5994 }
5995 // Other long conditional moves would generate 6+ instructions,
5996 // which is too many.
5997 } else {
5998 // movt out_reg_lo, true_reg_lo/ZERO, cc
5999 // movt out_reg_hi, true_reg_hi/ZERO, cc
6000 can_move_conditionally = true;
6001 use_const_for_true_in = is_true_value_zero_constant;
6002 }
6003 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006004 case DataType::Type::kFloat32:
6005 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006006 // Moving float/double on float/double condition.
6007 if (is_r6) {
6008 can_move_conditionally = true;
6009 if (is_true_value_zero_constant) {
6010 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6011 use_const_for_true_in = true;
6012 } else if (is_false_value_zero_constant) {
6013 // selnez.fmt out_reg, true_reg, temp_cond_reg
6014 use_const_for_false_in = true;
6015 } else {
6016 // sel.fmt temp_cond_reg, false_reg, true_reg
6017 // mov.fmt out_reg, temp_cond_reg
6018 }
6019 } else {
6020 // movt.fmt out_reg, true_reg, cc
6021 can_move_conditionally = true;
6022 }
6023 break;
6024 }
6025 break;
6026 }
6027 }
6028
6029 if (can_move_conditionally) {
6030 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6031 } else {
6032 DCHECK(!use_const_for_false_in);
6033 DCHECK(!use_const_for_true_in);
6034 }
6035
6036 if (locations_to_set != nullptr) {
6037 if (use_const_for_false_in) {
6038 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6039 } else {
6040 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006041 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006042 ? Location::RequiresFpuRegister()
6043 : Location::RequiresRegister());
6044 }
6045 if (use_const_for_true_in) {
6046 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6047 } else {
6048 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006049 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006050 ? Location::RequiresFpuRegister()
6051 : Location::RequiresRegister());
6052 }
6053 if (materialized) {
6054 locations_to_set->SetInAt(2, Location::RequiresRegister());
6055 }
6056 // On R6 we don't require the output to be the same as the
6057 // first input for conditional moves unlike on R2.
6058 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6059 if (is_out_same_as_first_in) {
6060 locations_to_set->SetOut(Location::SameAsFirstInput());
6061 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006062 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006063 ? Location::RequiresFpuRegister()
6064 : Location::RequiresRegister());
6065 }
6066 }
6067
6068 return can_move_conditionally;
6069}
6070
6071void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6072 LocationSummary* locations = select->GetLocations();
6073 Location dst = locations->Out();
6074 Location src = locations->InAt(1);
6075 Register src_reg = ZERO;
6076 Register src_reg_high = ZERO;
6077 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6078 Register cond_reg = TMP;
6079 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006080 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006081 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006082 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006083
6084 if (IsBooleanValueOrMaterializedCondition(cond)) {
6085 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6086 } else {
6087 HCondition* condition = cond->AsCondition();
6088 LocationSummary* cond_locations = cond->GetLocations();
6089 IfCondition if_cond = condition->GetCondition();
6090 cond_type = condition->InputAt(0)->GetType();
6091 switch (cond_type) {
6092 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006093 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006094 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6095 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006096 case DataType::Type::kFloat32:
6097 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006098 cond_inverted = MaterializeFpCompareR2(if_cond,
6099 condition->IsGtBias(),
6100 cond_type,
6101 cond_locations,
6102 cond_cc);
6103 break;
6104 }
6105 }
6106
6107 DCHECK(dst.Equals(locations->InAt(0)));
6108 if (src.IsRegister()) {
6109 src_reg = src.AsRegister<Register>();
6110 } else if (src.IsRegisterPair()) {
6111 src_reg = src.AsRegisterPairLow<Register>();
6112 src_reg_high = src.AsRegisterPairHigh<Register>();
6113 } else if (src.IsConstant()) {
6114 DCHECK(src.GetConstant()->IsZeroBitPattern());
6115 }
6116
6117 switch (cond_type) {
6118 default:
6119 switch (dst_type) {
6120 default:
6121 if (cond_inverted) {
6122 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6123 } else {
6124 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6125 }
6126 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006127 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006128 if (cond_inverted) {
6129 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6130 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6131 } else {
6132 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6133 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6134 }
6135 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006136 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006137 if (cond_inverted) {
6138 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6139 } else {
6140 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6141 }
6142 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006143 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006144 if (cond_inverted) {
6145 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6146 } else {
6147 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6148 }
6149 break;
6150 }
6151 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006152 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006153 LOG(FATAL) << "Unreachable";
6154 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006155 case DataType::Type::kFloat32:
6156 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006157 switch (dst_type) {
6158 default:
6159 if (cond_inverted) {
6160 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6161 } else {
6162 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6163 }
6164 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006165 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006166 if (cond_inverted) {
6167 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6168 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6169 } else {
6170 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6171 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6172 }
6173 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006174 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006175 if (cond_inverted) {
6176 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6177 } else {
6178 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6179 }
6180 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006181 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006182 if (cond_inverted) {
6183 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6184 } else {
6185 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6186 }
6187 break;
6188 }
6189 break;
6190 }
6191}
6192
6193void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6194 LocationSummary* locations = select->GetLocations();
6195 Location dst = locations->Out();
6196 Location false_src = locations->InAt(0);
6197 Location true_src = locations->InAt(1);
6198 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6199 Register cond_reg = TMP;
6200 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006201 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006202 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006203 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006204
6205 if (IsBooleanValueOrMaterializedCondition(cond)) {
6206 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6207 } else {
6208 HCondition* condition = cond->AsCondition();
6209 LocationSummary* cond_locations = cond->GetLocations();
6210 IfCondition if_cond = condition->GetCondition();
6211 cond_type = condition->InputAt(0)->GetType();
6212 switch (cond_type) {
6213 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006214 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006215 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6216 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006217 case DataType::Type::kFloat32:
6218 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006219 cond_inverted = MaterializeFpCompareR6(if_cond,
6220 condition->IsGtBias(),
6221 cond_type,
6222 cond_locations,
6223 fcond_reg);
6224 break;
6225 }
6226 }
6227
6228 if (true_src.IsConstant()) {
6229 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6230 }
6231 if (false_src.IsConstant()) {
6232 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6233 }
6234
6235 switch (dst_type) {
6236 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006237 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006238 __ Mfc1(cond_reg, fcond_reg);
6239 }
6240 if (true_src.IsConstant()) {
6241 if (cond_inverted) {
6242 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6243 } else {
6244 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6245 }
6246 } else if (false_src.IsConstant()) {
6247 if (cond_inverted) {
6248 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6249 } else {
6250 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6251 }
6252 } else {
6253 DCHECK_NE(cond_reg, AT);
6254 if (cond_inverted) {
6255 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6256 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6257 } else {
6258 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6259 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6260 }
6261 __ Or(dst.AsRegister<Register>(), AT, TMP);
6262 }
6263 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006264 case DataType::Type::kInt64: {
6265 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006266 __ Mfc1(cond_reg, fcond_reg);
6267 }
6268 Register dst_lo = dst.AsRegisterPairLow<Register>();
6269 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6270 if (true_src.IsConstant()) {
6271 Register src_lo = false_src.AsRegisterPairLow<Register>();
6272 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6273 if (cond_inverted) {
6274 __ Selnez(dst_lo, src_lo, cond_reg);
6275 __ Selnez(dst_hi, src_hi, cond_reg);
6276 } else {
6277 __ Seleqz(dst_lo, src_lo, cond_reg);
6278 __ Seleqz(dst_hi, src_hi, cond_reg);
6279 }
6280 } else {
6281 DCHECK(false_src.IsConstant());
6282 Register src_lo = true_src.AsRegisterPairLow<Register>();
6283 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6284 if (cond_inverted) {
6285 __ Seleqz(dst_lo, src_lo, cond_reg);
6286 __ Seleqz(dst_hi, src_hi, cond_reg);
6287 } else {
6288 __ Selnez(dst_lo, src_lo, cond_reg);
6289 __ Selnez(dst_hi, src_hi, cond_reg);
6290 }
6291 }
6292 break;
6293 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006294 case DataType::Type::kFloat32: {
6295 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006296 // sel*.fmt tests bit 0 of the condition register, account for that.
6297 __ Sltu(TMP, ZERO, cond_reg);
6298 __ Mtc1(TMP, fcond_reg);
6299 }
6300 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6301 if (true_src.IsConstant()) {
6302 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6303 if (cond_inverted) {
6304 __ SelnezS(dst_reg, src_reg, fcond_reg);
6305 } else {
6306 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6307 }
6308 } else if (false_src.IsConstant()) {
6309 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6310 if (cond_inverted) {
6311 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6312 } else {
6313 __ SelnezS(dst_reg, src_reg, fcond_reg);
6314 }
6315 } else {
6316 if (cond_inverted) {
6317 __ SelS(fcond_reg,
6318 true_src.AsFpuRegister<FRegister>(),
6319 false_src.AsFpuRegister<FRegister>());
6320 } else {
6321 __ SelS(fcond_reg,
6322 false_src.AsFpuRegister<FRegister>(),
6323 true_src.AsFpuRegister<FRegister>());
6324 }
6325 __ MovS(dst_reg, fcond_reg);
6326 }
6327 break;
6328 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006329 case DataType::Type::kFloat64: {
6330 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006331 // sel*.fmt tests bit 0 of the condition register, account for that.
6332 __ Sltu(TMP, ZERO, cond_reg);
6333 __ Mtc1(TMP, fcond_reg);
6334 }
6335 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6336 if (true_src.IsConstant()) {
6337 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6338 if (cond_inverted) {
6339 __ SelnezD(dst_reg, src_reg, fcond_reg);
6340 } else {
6341 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6342 }
6343 } else if (false_src.IsConstant()) {
6344 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6345 if (cond_inverted) {
6346 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6347 } else {
6348 __ SelnezD(dst_reg, src_reg, fcond_reg);
6349 }
6350 } else {
6351 if (cond_inverted) {
6352 __ SelD(fcond_reg,
6353 true_src.AsFpuRegister<FRegister>(),
6354 false_src.AsFpuRegister<FRegister>());
6355 } else {
6356 __ SelD(fcond_reg,
6357 false_src.AsFpuRegister<FRegister>(),
6358 true_src.AsFpuRegister<FRegister>());
6359 }
6360 __ MovD(dst_reg, fcond_reg);
6361 }
6362 break;
6363 }
6364 }
6365}
6366
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006367void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006368 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006369 LocationSummary(flag, LocationSummary::kNoCall);
6370 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006371}
6372
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006373void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6374 __ LoadFromOffset(kLoadWord,
6375 flag->GetLocations()->Out().AsRegister<Register>(),
6376 SP,
6377 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006378}
6379
David Brazdil74eb1b22015-12-14 11:44:01 +00006380void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006381 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006382 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006383}
6384
6385void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006386 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6387 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6388 if (is_r6) {
6389 GenConditionalMoveR6(select);
6390 } else {
6391 GenConditionalMoveR2(select);
6392 }
6393 } else {
6394 LocationSummary* locations = select->GetLocations();
6395 MipsLabel false_target;
6396 GenerateTestAndBranch(select,
6397 /* condition_input_index */ 2,
6398 /* true_target */ nullptr,
6399 &false_target);
6400 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6401 __ Bind(&false_target);
6402 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006403}
6404
David Srbecky0cf44932015-12-09 14:09:59 +00006405void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006406 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006407}
6408
David Srbeckyd28f4a02016-03-14 17:14:24 +00006409void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6410 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006411}
6412
6413void CodeGeneratorMIPS::GenerateNop() {
6414 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006415}
6416
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006417void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006418 DataType::Type field_type = field_info.GetFieldType();
6419 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006420 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006421 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006422 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006423 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006424 instruction,
6425 generate_volatile
6426 ? LocationSummary::kCallOnMainOnly
6427 : (object_field_get_with_read_barrier
6428 ? LocationSummary::kCallOnSlowPath
6429 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006430
Alexey Frunzec61c0762017-04-10 13:54:23 -07006431 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6432 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6433 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006434 locations->SetInAt(0, Location::RequiresRegister());
6435 if (generate_volatile) {
6436 InvokeRuntimeCallingConvention calling_convention;
6437 // need A0 to hold base + offset
6438 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006439 if (field_type == DataType::Type::kInt64) {
6440 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006441 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006442 // Use Location::Any() to prevent situations when running out of available fp registers.
6443 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006444 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006445 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006446 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6447 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6448 }
6449 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006450 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006451 locations->SetOut(Location::RequiresFpuRegister());
6452 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006453 // The output overlaps in the case of an object field get with
6454 // read barriers enabled: we do not want the move to overwrite the
6455 // object's location, as we need it to emit the read barrier.
6456 locations->SetOut(Location::RequiresRegister(),
6457 object_field_get_with_read_barrier
6458 ? Location::kOutputOverlap
6459 : Location::kNoOutputOverlap);
6460 }
6461 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6462 // We need a temporary register for the read barrier marking slow
6463 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006464 if (!kBakerReadBarrierThunksEnableForFields) {
6465 locations->AddTemp(Location::RequiresRegister());
6466 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006467 }
6468 }
6469}
6470
6471void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6472 const FieldInfo& field_info,
6473 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006474 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6475 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006476 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006477 Location obj_loc = locations->InAt(0);
6478 Register obj = obj_loc.AsRegister<Register>();
6479 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006480 LoadOperandType load_type = kLoadUnsignedByte;
6481 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006482 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006483 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006484
6485 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006486 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006487 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006488 load_type = kLoadUnsignedByte;
6489 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006490 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006491 load_type = kLoadSignedByte;
6492 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006493 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006494 load_type = kLoadUnsignedHalfword;
6495 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006496 case DataType::Type::kInt16:
6497 load_type = kLoadSignedHalfword;
6498 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006499 case DataType::Type::kInt32:
6500 case DataType::Type::kFloat32:
6501 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006502 load_type = kLoadWord;
6503 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006504 case DataType::Type::kInt64:
6505 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006506 load_type = kLoadDoubleword;
6507 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006508 case DataType::Type::kUint32:
6509 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006510 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006511 LOG(FATAL) << "Unreachable type " << type;
6512 UNREACHABLE();
6513 }
6514
6515 if (is_volatile && load_type == kLoadDoubleword) {
6516 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006517 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006518 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006519 __ LoadFromOffset(kLoadWord,
6520 ZERO,
6521 locations->GetTemp(0).AsRegister<Register>(),
6522 0,
6523 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006524 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006525 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006526 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006527 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006528 if (dst_loc.IsFpuRegister()) {
6529 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006530 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006531 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006532 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006533 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006534 __ StoreToOffset(kStoreWord,
6535 locations->GetTemp(1).AsRegister<Register>(),
6536 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006537 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006538 __ StoreToOffset(kStoreWord,
6539 locations->GetTemp(2).AsRegister<Register>(),
6540 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006541 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006542 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006543 }
6544 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006545 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006546 // /* HeapReference<Object> */ dst = *(obj + offset)
6547 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006548 Location temp_loc =
6549 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006550 // Note that a potential implicit null check is handled in this
6551 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6552 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6553 dst_loc,
6554 obj,
6555 offset,
6556 temp_loc,
6557 /* needs_null_check */ true);
6558 if (is_volatile) {
6559 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6560 }
6561 } else {
6562 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6563 if (is_volatile) {
6564 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6565 }
6566 // If read barriers are enabled, emit read barriers other than
6567 // Baker's using a slow path (and also unpoison the loaded
6568 // reference, if heap poisoning is enabled).
6569 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6570 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006571 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006572 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006573 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006574 DCHECK(dst_loc.IsRegisterPair());
6575 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006576 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006577 DCHECK(dst_loc.IsRegister());
6578 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006579 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006580 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006581 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006582 DCHECK(dst_loc.IsFpuRegister());
6583 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006584 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006585 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006586 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006587 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006588 }
6589 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006590 }
6591
Alexey Frunze15958152017-02-09 19:08:30 -08006592 // Memory barriers, in the case of references, are handled in the
6593 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006594 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006595 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6596 }
6597}
6598
6599void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006600 DataType::Type field_type = field_info.GetFieldType();
6601 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006602 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006603 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006604 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006605
6606 locations->SetInAt(0, Location::RequiresRegister());
6607 if (generate_volatile) {
6608 InvokeRuntimeCallingConvention calling_convention;
6609 // need A0 to hold base + offset
6610 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006611 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006612 locations->SetInAt(1, Location::RegisterPairLocation(
6613 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6614 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006615 // Use Location::Any() to prevent situations when running out of available fp registers.
6616 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006617 // Pass FP parameters in core registers.
6618 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6619 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6620 }
6621 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006622 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006623 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006624 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006625 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006626 }
6627 }
6628}
6629
6630void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6631 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006632 uint32_t dex_pc,
6633 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006634 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006635 LocationSummary* locations = instruction->GetLocations();
6636 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006637 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006638 StoreOperandType store_type = kStoreByte;
6639 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006640 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006641 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006642 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006643
6644 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006645 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006646 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006647 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006648 store_type = kStoreByte;
6649 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006650 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006651 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006652 store_type = kStoreHalfword;
6653 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006654 case DataType::Type::kInt32:
6655 case DataType::Type::kFloat32:
6656 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006657 store_type = kStoreWord;
6658 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006659 case DataType::Type::kInt64:
6660 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006661 store_type = kStoreDoubleword;
6662 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006663 case DataType::Type::kUint32:
6664 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006665 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006666 LOG(FATAL) << "Unreachable type " << type;
6667 UNREACHABLE();
6668 }
6669
6670 if (is_volatile) {
6671 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6672 }
6673
6674 if (is_volatile && store_type == kStoreDoubleword) {
6675 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006676 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006677 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006678 __ LoadFromOffset(kLoadWord,
6679 ZERO,
6680 locations->GetTemp(0).AsRegister<Register>(),
6681 0,
6682 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006683 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006684 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006685 if (value_location.IsFpuRegister()) {
6686 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6687 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006688 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006689 value_location.AsFpuRegister<FRegister>());
6690 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006691 __ LoadFromOffset(kLoadWord,
6692 locations->GetTemp(1).AsRegister<Register>(),
6693 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006694 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006695 __ LoadFromOffset(kLoadWord,
6696 locations->GetTemp(2).AsRegister<Register>(),
6697 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006698 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006699 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006700 DCHECK(value_location.IsConstant());
6701 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6702 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006703 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6704 locations->GetTemp(1).AsRegister<Register>(),
6705 value);
6706 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006707 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006708 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006709 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6710 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006711 if (value_location.IsConstant()) {
6712 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6713 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006714 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006715 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006716 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006717 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006718 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006719 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006720 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006721 if (kPoisonHeapReferences && needs_write_barrier) {
6722 // Note that in the case where `value` is a null reference,
6723 // we do not enter this block, as a null reference does not
6724 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006725 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006726 __ PoisonHeapReference(TMP, src);
6727 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6728 } else {
6729 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6730 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006731 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006732 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006733 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006734 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006735 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006736 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006737 }
6738 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006739 }
6740
Alexey Frunzec061de12017-02-14 13:27:23 -08006741 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006742 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006743 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006744 }
6745
6746 if (is_volatile) {
6747 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6748 }
6749}
6750
6751void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6752 HandleFieldGet(instruction, instruction->GetFieldInfo());
6753}
6754
6755void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6756 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6757}
6758
6759void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6760 HandleFieldSet(instruction, instruction->GetFieldInfo());
6761}
6762
6763void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006764 HandleFieldSet(instruction,
6765 instruction->GetFieldInfo(),
6766 instruction->GetDexPc(),
6767 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006768}
6769
Alexey Frunze15958152017-02-09 19:08:30 -08006770void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6771 HInstruction* instruction,
6772 Location out,
6773 uint32_t offset,
6774 Location maybe_temp,
6775 ReadBarrierOption read_barrier_option) {
6776 Register out_reg = out.AsRegister<Register>();
6777 if (read_barrier_option == kWithReadBarrier) {
6778 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006779 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6780 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6781 }
Alexey Frunze15958152017-02-09 19:08:30 -08006782 if (kUseBakerReadBarrier) {
6783 // Load with fast path based Baker's read barrier.
6784 // /* HeapReference<Object> */ out = *(out + offset)
6785 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6786 out,
6787 out_reg,
6788 offset,
6789 maybe_temp,
6790 /* needs_null_check */ false);
6791 } else {
6792 // Load with slow path based read barrier.
6793 // Save the value of `out` into `maybe_temp` before overwriting it
6794 // in the following move operation, as we will need it for the
6795 // read barrier below.
6796 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6797 // /* HeapReference<Object> */ out = *(out + offset)
6798 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6799 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6800 }
6801 } else {
6802 // Plain load with no read barrier.
6803 // /* HeapReference<Object> */ out = *(out + offset)
6804 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6805 __ MaybeUnpoisonHeapReference(out_reg);
6806 }
6807}
6808
6809void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6810 HInstruction* instruction,
6811 Location out,
6812 Location obj,
6813 uint32_t offset,
6814 Location maybe_temp,
6815 ReadBarrierOption read_barrier_option) {
6816 Register out_reg = out.AsRegister<Register>();
6817 Register obj_reg = obj.AsRegister<Register>();
6818 if (read_barrier_option == kWithReadBarrier) {
6819 CHECK(kEmitCompilerReadBarrier);
6820 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006821 if (!kBakerReadBarrierThunksEnableForFields) {
6822 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6823 }
Alexey Frunze15958152017-02-09 19:08:30 -08006824 // Load with fast path based Baker's read barrier.
6825 // /* HeapReference<Object> */ out = *(obj + offset)
6826 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6827 out,
6828 obj_reg,
6829 offset,
6830 maybe_temp,
6831 /* needs_null_check */ false);
6832 } else {
6833 // Load with slow path based read barrier.
6834 // /* HeapReference<Object> */ out = *(obj + offset)
6835 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6836 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6837 }
6838 } else {
6839 // Plain load with no read barrier.
6840 // /* HeapReference<Object> */ out = *(obj + offset)
6841 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6842 __ MaybeUnpoisonHeapReference(out_reg);
6843 }
6844}
6845
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006846static inline int GetBakerMarkThunkNumber(Register reg) {
6847 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6848 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6849 return reg - V0;
6850 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6851 return 14 + (reg - S2);
6852 } else if (reg == FP) { // One more.
6853 return 20;
6854 }
6855 LOG(FATAL) << "Unexpected register " << reg;
6856 UNREACHABLE();
6857}
6858
6859static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6860 int num = GetBakerMarkThunkNumber(reg) +
6861 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6862 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6863}
6864
6865static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6866 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6867 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6868}
6869
Alexey Frunze15958152017-02-09 19:08:30 -08006870void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6871 Location root,
6872 Register obj,
6873 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006874 ReadBarrierOption read_barrier_option,
6875 MipsLabel* label_low) {
6876 bool reordering;
6877 if (label_low != nullptr) {
6878 DCHECK_EQ(offset, 0x5678u);
6879 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006880 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006881 if (read_barrier_option == kWithReadBarrier) {
6882 DCHECK(kEmitCompilerReadBarrier);
6883 if (kUseBakerReadBarrier) {
6884 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6885 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006886 if (kBakerReadBarrierThunksEnableForGcRoots) {
6887 // Note that we do not actually check the value of `GetIsGcMarking()`
6888 // to decide whether to mark the loaded GC root or not. Instead, we
6889 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6890 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6891 // vice versa.
6892 //
6893 // We use thunks for the slow path. That thunk checks the reference
6894 // and jumps to the entrypoint if needed.
6895 //
6896 // temp = Thread::Current()->pReadBarrierMarkReg00
6897 // // AKA &art_quick_read_barrier_mark_introspection.
6898 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6899 // if (temp != nullptr) {
6900 // temp = &gc_root_thunk<root_reg>
6901 // root = temp(root)
6902 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006903
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006904 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6905 const int32_t entry_point_offset =
6906 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6907 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6908 int16_t offset_low = Low16Bits(offset);
6909 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6910 // extension in lw.
6911 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6912 Register base = short_offset ? obj : TMP;
6913 // Loading the entrypoint does not require a load acquire since it is only changed when
6914 // threads are suspended or running a checkpoint.
6915 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6916 reordering = __ SetReorder(false);
6917 if (!short_offset) {
6918 DCHECK(!label_low);
6919 __ AddUpper(base, obj, offset_high);
6920 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006921 MipsLabel skip_call;
6922 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006923 if (label_low != nullptr) {
6924 DCHECK(short_offset);
6925 __ Bind(label_low);
6926 }
6927 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6928 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6929 // in delay slot.
6930 if (isR6) {
6931 __ Jialc(T9, thunk_disp);
6932 } else {
6933 __ Addiu(T9, T9, thunk_disp);
6934 __ Jalr(T9);
6935 __ Nop();
6936 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006937 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006938 __ SetReorder(reordering);
6939 } else {
6940 // Note that we do not actually check the value of `GetIsGcMarking()`
6941 // to decide whether to mark the loaded GC root or not. Instead, we
6942 // load into `temp` (T9) the read barrier mark entry point corresponding
6943 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6944 // is false, and vice versa.
6945 //
6946 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6947 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6948 // if (temp != null) {
6949 // root = temp(root)
6950 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006951
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006952 if (label_low != nullptr) {
6953 reordering = __ SetReorder(false);
6954 __ Bind(label_low);
6955 }
6956 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6957 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6958 if (label_low != nullptr) {
6959 __ SetReorder(reordering);
6960 }
6961 static_assert(
6962 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6963 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6964 "have different sizes.");
6965 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6966 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6967 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006968
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006969 // Slow path marking the GC root `root`.
6970 Location temp = Location::RegisterLocation(T9);
6971 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006972 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006973 instruction,
6974 root,
6975 /*entrypoint*/ temp);
6976 codegen_->AddSlowPath(slow_path);
6977
6978 const int32_t entry_point_offset =
6979 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6980 // Loading the entrypoint does not require a load acquire since it is only changed when
6981 // threads are suspended or running a checkpoint.
6982 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6983 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6984 __ Bind(slow_path->GetExitLabel());
6985 }
Alexey Frunze15958152017-02-09 19:08:30 -08006986 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006987 if (label_low != nullptr) {
6988 reordering = __ SetReorder(false);
6989 __ Bind(label_low);
6990 }
Alexey Frunze15958152017-02-09 19:08:30 -08006991 // GC root loaded through a slow path for read barriers other
6992 // than Baker's.
6993 // /* GcRoot<mirror::Object>* */ root = obj + offset
6994 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006995 if (label_low != nullptr) {
6996 __ SetReorder(reordering);
6997 }
Alexey Frunze15958152017-02-09 19:08:30 -08006998 // /* mirror::Object* */ root = root->Read()
6999 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7000 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007001 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007002 if (label_low != nullptr) {
7003 reordering = __ SetReorder(false);
7004 __ Bind(label_low);
7005 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007006 // Plain GC root load with no read barrier.
7007 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7008 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7009 // Note that GC roots are not affected by heap poisoning, thus we
7010 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007011 if (label_low != nullptr) {
7012 __ SetReorder(reordering);
7013 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007014 }
7015}
7016
Alexey Frunze15958152017-02-09 19:08:30 -08007017void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7018 Location ref,
7019 Register obj,
7020 uint32_t offset,
7021 Location temp,
7022 bool needs_null_check) {
7023 DCHECK(kEmitCompilerReadBarrier);
7024 DCHECK(kUseBakerReadBarrier);
7025
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007026 if (kBakerReadBarrierThunksEnableForFields) {
7027 // Note that we do not actually check the value of `GetIsGcMarking()`
7028 // to decide whether to mark the loaded reference or not. Instead, we
7029 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7030 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7031 // vice versa.
7032 //
7033 // We use thunks for the slow path. That thunk checks the reference
7034 // and jumps to the entrypoint if needed. If the holder is not gray,
7035 // it issues a load-load memory barrier and returns to the original
7036 // reference load.
7037 //
7038 // temp = Thread::Current()->pReadBarrierMarkReg00
7039 // // AKA &art_quick_read_barrier_mark_introspection.
7040 // if (temp != nullptr) {
7041 // temp = &field_array_thunk<holder_reg>
7042 // temp()
7043 // }
7044 // not_gray_return_address:
7045 // // If the offset is too large to fit into the lw instruction, we
7046 // // use an adjusted base register (TMP) here. This register
7047 // // receives bits 16 ... 31 of the offset before the thunk invocation
7048 // // and the thunk benefits from it.
7049 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7050 // gray_return_address:
7051
7052 DCHECK(temp.IsInvalid());
7053 bool isR6 = GetInstructionSetFeatures().IsR6();
7054 int16_t offset_low = Low16Bits(offset);
7055 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7056 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7057 bool reordering = __ SetReorder(false);
7058 const int32_t entry_point_offset =
7059 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7060 // There may have or may have not been a null check if the field offset is smaller than
7061 // the page size.
7062 // There must've been a null check in case it's actually a load from an array.
7063 // We will, however, perform an explicit null check in the thunk as it's easier to
7064 // do it than not.
7065 if (instruction->IsArrayGet()) {
7066 DCHECK(!needs_null_check);
7067 }
7068 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7069 // Loading the entrypoint does not require a load acquire since it is only changed when
7070 // threads are suspended or running a checkpoint.
7071 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7072 Register ref_reg = ref.AsRegister<Register>();
7073 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007074 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007075 if (short_offset) {
7076 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007077 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007078 __ Nop(); // In forbidden slot.
7079 __ Jialc(T9, thunk_disp);
7080 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007081 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007082 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7083 __ Jalr(T9);
7084 __ Nop(); // In delay slot.
7085 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007086 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007087 } else {
7088 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007089 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007090 __ Aui(base, obj, offset_high); // In delay slot.
7091 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007092 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007093 } else {
7094 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007095 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007096 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7097 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007098 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007099 __ Addu(base, base, obj); // In delay slot.
7100 }
7101 }
7102 // /* HeapReference<Object> */ ref = *(obj + offset)
7103 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7104 if (needs_null_check) {
7105 MaybeRecordImplicitNullCheck(instruction);
7106 }
7107 __ MaybeUnpoisonHeapReference(ref_reg);
7108 __ SetReorder(reordering);
7109 return;
7110 }
7111
Alexey Frunze15958152017-02-09 19:08:30 -08007112 // /* HeapReference<Object> */ ref = *(obj + offset)
7113 Location no_index = Location::NoLocation();
7114 ScaleFactor no_scale_factor = TIMES_1;
7115 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7116 ref,
7117 obj,
7118 offset,
7119 no_index,
7120 no_scale_factor,
7121 temp,
7122 needs_null_check);
7123}
7124
7125void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7126 Location ref,
7127 Register obj,
7128 uint32_t data_offset,
7129 Location index,
7130 Location temp,
7131 bool needs_null_check) {
7132 DCHECK(kEmitCompilerReadBarrier);
7133 DCHECK(kUseBakerReadBarrier);
7134
7135 static_assert(
7136 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7137 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007138 ScaleFactor scale_factor = TIMES_4;
7139
7140 if (kBakerReadBarrierThunksEnableForArrays) {
7141 // Note that we do not actually check the value of `GetIsGcMarking()`
7142 // to decide whether to mark the loaded reference or not. Instead, we
7143 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7144 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7145 // vice versa.
7146 //
7147 // We use thunks for the slow path. That thunk checks the reference
7148 // and jumps to the entrypoint if needed. If the holder is not gray,
7149 // it issues a load-load memory barrier and returns to the original
7150 // reference load.
7151 //
7152 // temp = Thread::Current()->pReadBarrierMarkReg00
7153 // // AKA &art_quick_read_barrier_mark_introspection.
7154 // if (temp != nullptr) {
7155 // temp = &field_array_thunk<holder_reg>
7156 // temp()
7157 // }
7158 // not_gray_return_address:
7159 // // The element address is pre-calculated in the TMP register before the
7160 // // thunk invocation and the thunk benefits from it.
7161 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7162 // gray_return_address:
7163
7164 DCHECK(temp.IsInvalid());
7165 DCHECK(index.IsValid());
7166 bool reordering = __ SetReorder(false);
7167 const int32_t entry_point_offset =
7168 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7169 // We will not do the explicit null check in the thunk as some form of a null check
7170 // must've been done earlier.
7171 DCHECK(!needs_null_check);
7172 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7173 // Loading the entrypoint does not require a load acquire since it is only changed when
7174 // threads are suspended or running a checkpoint.
7175 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7176 Register ref_reg = ref.AsRegister<Register>();
7177 Register index_reg = index.IsRegisterPair()
7178 ? index.AsRegisterPairLow<Register>()
7179 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007180 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007181 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007182 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007183 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7184 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007185 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007186 } else {
7187 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007188 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007189 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7190 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007191 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007192 __ Addu(TMP, TMP, obj); // In delay slot.
7193 }
7194 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7195 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7196 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7197 __ MaybeUnpoisonHeapReference(ref_reg);
7198 __ SetReorder(reordering);
7199 return;
7200 }
7201
Alexey Frunze15958152017-02-09 19:08:30 -08007202 // /* HeapReference<Object> */ ref =
7203 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007204 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7205 ref,
7206 obj,
7207 data_offset,
7208 index,
7209 scale_factor,
7210 temp,
7211 needs_null_check);
7212}
7213
7214void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7215 Location ref,
7216 Register obj,
7217 uint32_t offset,
7218 Location index,
7219 ScaleFactor scale_factor,
7220 Location temp,
7221 bool needs_null_check,
7222 bool always_update_field) {
7223 DCHECK(kEmitCompilerReadBarrier);
7224 DCHECK(kUseBakerReadBarrier);
7225
7226 // In slow path based read barriers, the read barrier call is
7227 // inserted after the original load. However, in fast path based
7228 // Baker's read barriers, we need to perform the load of
7229 // mirror::Object::monitor_ *before* the original reference load.
7230 // This load-load ordering is required by the read barrier.
7231 // The fast path/slow path (for Baker's algorithm) should look like:
7232 //
7233 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7234 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7235 // HeapReference<Object> ref = *src; // Original reference load.
7236 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7237 // if (is_gray) {
7238 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7239 // }
7240 //
7241 // Note: the original implementation in ReadBarrier::Barrier is
7242 // slightly more complex as it performs additional checks that we do
7243 // not do here for performance reasons.
7244
7245 Register ref_reg = ref.AsRegister<Register>();
7246 Register temp_reg = temp.AsRegister<Register>();
7247 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7248
7249 // /* int32_t */ monitor = obj->monitor_
7250 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7251 if (needs_null_check) {
7252 MaybeRecordImplicitNullCheck(instruction);
7253 }
7254 // /* LockWord */ lock_word = LockWord(monitor)
7255 static_assert(sizeof(LockWord) == sizeof(int32_t),
7256 "art::LockWord and int32_t have different sizes.");
7257
7258 __ Sync(0); // Barrier to prevent load-load reordering.
7259
7260 // The actual reference load.
7261 if (index.IsValid()) {
7262 // Load types involving an "index": ArrayGet,
7263 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7264 // intrinsics.
7265 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7266 if (index.IsConstant()) {
7267 size_t computed_offset =
7268 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7269 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7270 } else {
7271 // Handle the special case of the
7272 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7273 // intrinsics, which use a register pair as index ("long
7274 // offset"), of which only the low part contains data.
7275 Register index_reg = index.IsRegisterPair()
7276 ? index.AsRegisterPairLow<Register>()
7277 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007278 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007279 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7280 }
7281 } else {
7282 // /* HeapReference<Object> */ ref = *(obj + offset)
7283 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7284 }
7285
7286 // Object* ref = ref_addr->AsMirrorPtr()
7287 __ MaybeUnpoisonHeapReference(ref_reg);
7288
7289 // Slow path marking the object `ref` when it is gray.
7290 SlowPathCodeMIPS* slow_path;
7291 if (always_update_field) {
7292 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7293 // of the form `obj + field_offset`, where `obj` is a register and
7294 // `field_offset` is a register pair (of which only the lower half
7295 // is used). Thus `offset` and `scale_factor` above are expected
7296 // to be null in this code path.
7297 DCHECK_EQ(offset, 0u);
7298 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007299 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007300 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7301 ref,
7302 obj,
7303 /* field_offset */ index,
7304 temp_reg);
7305 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007306 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007307 }
7308 AddSlowPath(slow_path);
7309
7310 // if (rb_state == ReadBarrier::GrayState())
7311 // ref = ReadBarrier::Mark(ref);
7312 // Given the numeric representation, it's enough to check the low bit of the
7313 // rb_state. We do that by shifting the bit into the sign bit (31) and
7314 // performing a branch on less than zero.
7315 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7316 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7317 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7318 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7319 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7320 __ Bind(slow_path->GetExitLabel());
7321}
7322
7323void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7324 Location out,
7325 Location ref,
7326 Location obj,
7327 uint32_t offset,
7328 Location index) {
7329 DCHECK(kEmitCompilerReadBarrier);
7330
7331 // Insert a slow path based read barrier *after* the reference load.
7332 //
7333 // If heap poisoning is enabled, the unpoisoning of the loaded
7334 // reference will be carried out by the runtime within the slow
7335 // path.
7336 //
7337 // Note that `ref` currently does not get unpoisoned (when heap
7338 // poisoning is enabled), which is alright as the `ref` argument is
7339 // not used by the artReadBarrierSlow entry point.
7340 //
7341 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007342 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007343 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7344 AddSlowPath(slow_path);
7345
7346 __ B(slow_path->GetEntryLabel());
7347 __ Bind(slow_path->GetExitLabel());
7348}
7349
7350void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7351 Location out,
7352 Location ref,
7353 Location obj,
7354 uint32_t offset,
7355 Location index) {
7356 if (kEmitCompilerReadBarrier) {
7357 // Baker's read barriers shall be handled by the fast path
7358 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7359 DCHECK(!kUseBakerReadBarrier);
7360 // If heap poisoning is enabled, unpoisoning will be taken care of
7361 // by the runtime within the slow path.
7362 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7363 } else if (kPoisonHeapReferences) {
7364 __ UnpoisonHeapReference(out.AsRegister<Register>());
7365 }
7366}
7367
7368void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7369 Location out,
7370 Location root) {
7371 DCHECK(kEmitCompilerReadBarrier);
7372
7373 // Insert a slow path based read barrier *after* the GC root load.
7374 //
7375 // Note that GC roots are not affected by heap poisoning, so we do
7376 // not need to do anything special for this here.
7377 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007378 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007379 AddSlowPath(slow_path);
7380
7381 __ B(slow_path->GetEntryLabel());
7382 __ Bind(slow_path->GetExitLabel());
7383}
7384
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007385void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007386 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7387 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007388 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007389 switch (type_check_kind) {
7390 case TypeCheckKind::kExactCheck:
7391 case TypeCheckKind::kAbstractClassCheck:
7392 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007393 case TypeCheckKind::kArrayObjectCheck: {
7394 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7395 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7396 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007397 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007398 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007399 case TypeCheckKind::kArrayCheck:
7400 case TypeCheckKind::kUnresolvedCheck:
7401 case TypeCheckKind::kInterfaceCheck:
7402 call_kind = LocationSummary::kCallOnSlowPath;
7403 break;
7404 }
7405
Vladimir Markoca6fff82017-10-03 14:49:14 +01007406 LocationSummary* locations =
7407 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007408 if (baker_read_barrier_slow_path) {
7409 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7410 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007411 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007412 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007413 // The output does overlap inputs.
7414 // Note that TypeCheckSlowPathMIPS uses this register too.
7415 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007416 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007417}
7418
7419void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007420 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007421 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007422 Location obj_loc = locations->InAt(0);
7423 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007424 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007425 Location out_loc = locations->Out();
7426 Register out = out_loc.AsRegister<Register>();
7427 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7428 DCHECK_LE(num_temps, 1u);
7429 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007430 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7431 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7432 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7433 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007434 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007435 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007436
7437 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007438 // Avoid this check if we know `obj` is not null.
7439 if (instruction->MustDoNullCheck()) {
7440 __ Move(out, ZERO);
7441 __ Beqz(obj, &done);
7442 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007443
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007444 switch (type_check_kind) {
7445 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007446 ReadBarrierOption read_barrier_option =
7447 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007448 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007449 GenerateReferenceLoadTwoRegisters(instruction,
7450 out_loc,
7451 obj_loc,
7452 class_offset,
7453 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007454 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007455 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007456 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007457 __ Sltiu(out, out, 1);
7458 break;
7459 }
7460
7461 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007462 ReadBarrierOption read_barrier_option =
7463 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007464 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007465 GenerateReferenceLoadTwoRegisters(instruction,
7466 out_loc,
7467 obj_loc,
7468 class_offset,
7469 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007470 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007471 // If the class is abstract, we eagerly fetch the super class of the
7472 // object to avoid doing a comparison we know will fail.
7473 MipsLabel loop;
7474 __ Bind(&loop);
7475 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007476 GenerateReferenceLoadOneRegister(instruction,
7477 out_loc,
7478 super_offset,
7479 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007480 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007481 // If `out` is null, we use it for the result, and jump to `done`.
7482 __ Beqz(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007483 __ Bne(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007484 __ LoadConst32(out, 1);
7485 break;
7486 }
7487
7488 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007489 ReadBarrierOption read_barrier_option =
7490 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007491 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007492 GenerateReferenceLoadTwoRegisters(instruction,
7493 out_loc,
7494 obj_loc,
7495 class_offset,
7496 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007497 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007498 // Walk over the class hierarchy to find a match.
7499 MipsLabel loop, success;
7500 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007501 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007502 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007503 GenerateReferenceLoadOneRegister(instruction,
7504 out_loc,
7505 super_offset,
7506 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007507 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007508 __ Bnez(out, &loop);
7509 // If `out` is null, we use it for the result, and jump to `done`.
7510 __ B(&done);
7511 __ Bind(&success);
7512 __ LoadConst32(out, 1);
7513 break;
7514 }
7515
7516 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007517 ReadBarrierOption read_barrier_option =
7518 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007519 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007520 GenerateReferenceLoadTwoRegisters(instruction,
7521 out_loc,
7522 obj_loc,
7523 class_offset,
7524 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007525 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007526 // Do an exact check.
7527 MipsLabel success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007528 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007529 // Otherwise, we need to check that the object's class is a non-primitive array.
7530 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007531 GenerateReferenceLoadOneRegister(instruction,
7532 out_loc,
7533 component_offset,
7534 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007535 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007536 // If `out` is null, we use it for the result, and jump to `done`.
7537 __ Beqz(out, &done);
7538 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7539 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7540 __ Sltiu(out, out, 1);
7541 __ B(&done);
7542 __ Bind(&success);
7543 __ LoadConst32(out, 1);
7544 break;
7545 }
7546
7547 case TypeCheckKind::kArrayCheck: {
7548 // No read barrier since the slow path will retry upon failure.
7549 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007550 GenerateReferenceLoadTwoRegisters(instruction,
7551 out_loc,
7552 obj_loc,
7553 class_offset,
7554 maybe_temp_loc,
7555 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007556 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007557 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7558 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007559 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007560 __ Bne(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007561 __ LoadConst32(out, 1);
7562 break;
7563 }
7564
7565 case TypeCheckKind::kUnresolvedCheck:
7566 case TypeCheckKind::kInterfaceCheck: {
7567 // Note that we indeed only call on slow path, but we always go
7568 // into the slow path for the unresolved and interface check
7569 // cases.
7570 //
7571 // We cannot directly call the InstanceofNonTrivial runtime
7572 // entry point without resorting to a type checking slow path
7573 // here (i.e. by calling InvokeRuntime directly), as it would
7574 // require to assign fixed registers for the inputs of this
7575 // HInstanceOf instruction (following the runtime calling
7576 // convention), which might be cluttered by the potential first
7577 // read barrier emission at the beginning of this method.
7578 //
7579 // TODO: Introduce a new runtime entry point taking the object
7580 // to test (instead of its class) as argument, and let it deal
7581 // with the read barrier issues. This will let us refactor this
7582 // case of the `switch` code as it was previously (with a direct
7583 // call to the runtime not using a type checking slow path).
7584 // This should also be beneficial for the other cases above.
7585 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007586 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7587 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007588 codegen_->AddSlowPath(slow_path);
7589 __ B(slow_path->GetEntryLabel());
7590 break;
7591 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007592 }
7593
7594 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007595
7596 if (slow_path != nullptr) {
7597 __ Bind(slow_path->GetExitLabel());
7598 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007599}
7600
7601void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007602 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007603 locations->SetOut(Location::ConstantLocation(constant));
7604}
7605
7606void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7607 // Will be generated at use site.
7608}
7609
7610void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007611 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007612 locations->SetOut(Location::ConstantLocation(constant));
7613}
7614
7615void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7616 // Will be generated at use site.
7617}
7618
7619void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7620 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7621 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7622}
7623
7624void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7625 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007626 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007627 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007628 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007629}
7630
7631void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7632 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7633 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007634 Location receiver = invoke->GetLocations()->InAt(0);
7635 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007636 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007637
7638 // Set the hidden argument.
7639 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7640 invoke->GetDexMethodIndex());
7641
7642 // temp = object->GetClass();
7643 if (receiver.IsStackSlot()) {
7644 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7645 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7646 } else {
7647 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7648 }
7649 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007650 // Instead of simply (possibly) unpoisoning `temp` here, we should
7651 // emit a read barrier for the previous class reference load.
7652 // However this is not required in practice, as this is an
7653 // intermediate/temporary reference and because the current
7654 // concurrent copying collector keeps the from-space memory
7655 // intact/accessible until the end of the marking phase (the
7656 // concurrent copying collector may not in the future).
7657 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007658 __ LoadFromOffset(kLoadWord, temp, temp,
7659 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7660 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007661 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007662 // temp = temp->GetImtEntryAt(method_offset);
7663 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7664 // T9 = temp->GetEntryPoint();
7665 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7666 // T9();
7667 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007668 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007669 DCHECK(!codegen_->IsLeafMethod());
7670 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7671}
7672
7673void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007674 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7675 if (intrinsic.TryDispatch(invoke)) {
7676 return;
7677 }
7678
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007679 HandleInvoke(invoke);
7680}
7681
7682void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007683 // Explicit clinit checks triggered by static invokes must have been pruned by
7684 // art::PrepareForRegisterAllocation.
7685 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007686
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007687 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007688 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7689 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007690
Chris Larsen701566a2015-10-27 15:29:13 -07007691 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7692 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007693 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7694 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7695 }
Chris Larsen701566a2015-10-27 15:29:13 -07007696 return;
7697 }
7698
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007699 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007700
7701 // Add the extra input register if either the dex cache array base register
7702 // or the PC-relative base register for accessing literals is needed.
7703 if (has_extra_input) {
7704 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7705 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007706}
7707
Orion Hodsonac141392017-01-13 11:53:47 +00007708void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7709 HandleInvoke(invoke);
7710}
7711
7712void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7713 codegen_->GenerateInvokePolymorphicCall(invoke);
7714}
7715
Chris Larsen701566a2015-10-27 15:29:13 -07007716static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007717 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007718 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7719 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007720 return true;
7721 }
7722 return false;
7723}
7724
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007725HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007726 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007727 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007728 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007729 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007730 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007731 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007732 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007733 case HLoadString::LoadKind::kJitTableAddress:
7734 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007735 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007736 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007737 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007738 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007739 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007740 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007741}
7742
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007743HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7744 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007745 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007746 case HLoadClass::LoadKind::kInvalid:
7747 LOG(FATAL) << "UNREACHABLE";
7748 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007749 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007750 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007751 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007752 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007753 case HLoadClass::LoadKind::kBssEntry:
7754 DCHECK(!Runtime::Current()->UseJitCompilation());
7755 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007756 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007757 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007758 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007759 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007760 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007761 break;
7762 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007763 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007764}
7765
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007766Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7767 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007768 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007769 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007770 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7771 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7772 if (!invoke->GetLocations()->Intrinsified()) {
7773 return location.AsRegister<Register>();
7774 }
7775 // For intrinsics we allow any location, so it may be on the stack.
7776 if (!location.IsRegister()) {
7777 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7778 return temp;
7779 }
7780 // For register locations, check if the register was saved. If so, get it from the stack.
7781 // Note: There is a chance that the register was saved but not overwritten, so we could
7782 // save one load. However, since this is just an intrinsic slow path we prefer this
7783 // simple and more robust approach rather that trying to determine if that's the case.
7784 SlowPathCode* slow_path = GetCurrentSlowPath();
7785 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7786 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7787 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7788 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7789 return temp;
7790 }
7791 return location.AsRegister<Register>();
7792}
7793
Vladimir Markodc151b22015-10-15 18:02:30 +01007794HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7795 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007796 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007797 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007798}
7799
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007800void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7801 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007802 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007803 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007804 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7805 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007806 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007807 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7808 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007809 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7810 : ZERO;
7811
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007812 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007813 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007814 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007815 uint32_t offset =
7816 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007817 __ LoadFromOffset(kLoadWord,
7818 temp.AsRegister<Register>(),
7819 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007820 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007821 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007822 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007823 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007824 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007825 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007826 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7827 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007828 PcRelativePatchInfo* info_high = NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007829 PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007830 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007831 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007832 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7833 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007834 break;
7835 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007836 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7837 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7838 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007839 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007840 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007841 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007842 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7843 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007844 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007845 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7846 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007847 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007848 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007849 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7850 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7851 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007852 }
7853 }
7854
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007855 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007856 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007857 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007858 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007859 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7860 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007861 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007862 T9,
7863 callee_method.AsRegister<Register>(),
7864 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007865 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007866 // T9()
7867 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007868 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007869 break;
7870 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007871 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7872
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007873 DCHECK(!IsLeafMethod());
7874}
7875
7876void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007877 // Explicit clinit checks triggered by static invokes must have been pruned by
7878 // art::PrepareForRegisterAllocation.
7879 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007880
7881 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7882 return;
7883 }
7884
7885 LocationSummary* locations = invoke->GetLocations();
7886 codegen_->GenerateStaticOrDirectCall(invoke,
7887 locations->HasTemps()
7888 ? locations->GetTemp(0)
7889 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007890}
7891
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007892void CodeGeneratorMIPS::GenerateVirtualCall(
7893 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007894 // Use the calling convention instead of the location of the receiver, as
7895 // intrinsics may have put the receiver in a different register. In the intrinsics
7896 // slow path, the arguments have been moved to the right place, so here we are
7897 // guaranteed that the receiver is the first register of the calling convention.
7898 InvokeDexCallingConvention calling_convention;
7899 Register receiver = calling_convention.GetRegisterAt(0);
7900
Chris Larsen3acee732015-11-18 13:31:08 -08007901 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007902 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7903 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7904 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007905 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007906
7907 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007908 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007909 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007910 // Instead of simply (possibly) unpoisoning `temp` here, we should
7911 // emit a read barrier for the previous class reference load.
7912 // However this is not required in practice, as this is an
7913 // intermediate/temporary reference and because the current
7914 // concurrent copying collector keeps the from-space memory
7915 // intact/accessible until the end of the marking phase (the
7916 // concurrent copying collector may not in the future).
7917 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007918 // temp = temp->GetMethodAt(method_offset);
7919 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7920 // T9 = temp->GetEntryPoint();
7921 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7922 // T9();
7923 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007924 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007925 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007926}
7927
7928void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7929 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7930 return;
7931 }
7932
7933 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007934 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007935}
7936
7937void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007938 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007939 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007940 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007941 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7942 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007943 return;
7944 }
Vladimir Marko41559982017-01-06 14:04:23 +00007945 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007946 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007947 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007948 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7949 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007950 ? LocationSummary::kCallOnSlowPath
7951 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007952 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007953 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7954 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7955 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007956 switch (load_kind) {
7957 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007958 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007959 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007960 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007961 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007962 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007963 break;
7964 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007965 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007966 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7967 codegen_->ClobberRA();
7968 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007969 break;
7970 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007971 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007972 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007973 locations->SetInAt(0, Location::RequiresRegister());
7974 break;
7975 default:
7976 break;
7977 }
7978 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007979 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7980 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7981 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007982 RegisterSet caller_saves = RegisterSet::Empty();
7983 InvokeRuntimeCallingConvention calling_convention;
7984 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7985 locations->SetCustomSlowPathCallerSaves(caller_saves);
7986 } else {
7987 // For non-Baker read barriers we have a temp-clobbering call.
7988 }
7989 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007990}
7991
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007992// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7993// move.
7994void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007995 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007996 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007997 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007998 return;
7999 }
Vladimir Marko41559982017-01-06 14:04:23 +00008000 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008001
Vladimir Marko41559982017-01-06 14:04:23 +00008002 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008003 Location out_loc = locations->Out();
8004 Register out = out_loc.AsRegister<Register>();
8005 Register base_or_current_method_reg;
8006 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008007 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008008 switch (load_kind) {
8009 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008010 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008011 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008012 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008013 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008014 base_or_current_method_reg =
8015 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008016 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008017 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008018 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008019 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8020 break;
8021 default:
8022 base_or_current_method_reg = ZERO;
8023 break;
8024 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008025
Alexey Frunze15958152017-02-09 19:08:30 -08008026 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8027 ? kWithoutReadBarrier
8028 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008029 bool generate_null_check = false;
8030 switch (load_kind) {
8031 case HLoadClass::LoadKind::kReferrersClass: {
8032 DCHECK(!cls->CanCallRuntime());
8033 DCHECK(!cls->MustGenerateClinitCheck());
8034 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8035 GenerateGcRootFieldLoad(cls,
8036 out_loc,
8037 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008038 ArtMethod::DeclaringClassOffset().Int32Value(),
8039 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008040 break;
8041 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008042 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008043 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008044 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008045 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008046 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008047 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008048 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008049 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8050 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008051 base_or_current_method_reg);
8052 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008053 break;
8054 }
8055 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08008056 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008057 uint32_t address = dchecked_integral_cast<uint32_t>(
8058 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
8059 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008060 if (isR6 || !has_irreducible_loops) {
8061 __ LoadLiteral(out,
8062 base_or_current_method_reg,
8063 codegen_->DeduplicateBootImageAddressLiteral(address));
8064 } else {
8065 __ LoadConst32(out, address);
8066 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008067 break;
8068 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008069 case HLoadClass::LoadKind::kBootImageClassTable: {
8070 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8071 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008072 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008073 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008074 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008075 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8076 out,
8077 base_or_current_method_reg);
8078 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8079 // Extract the reference from the slot data, i.e. clear the hash bits.
8080 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
8081 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
8082 if (masked_hash != 0) {
8083 __ Addiu(out, out, -masked_hash);
8084 }
8085 break;
8086 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008087 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008088 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8089 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008090 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8091 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008092 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008093 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008094 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008095 GenerateGcRootFieldLoad(cls,
8096 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008097 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008098 /* placeholder */ 0x5678,
8099 read_barrier_option,
8100 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008101 generate_null_check = true;
8102 break;
8103 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008104 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008105 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8106 cls->GetTypeIndex(),
8107 cls->GetClass());
8108 bool reordering = __ SetReorder(false);
8109 __ Bind(&info->high_label);
8110 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008111 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008112 GenerateGcRootFieldLoad(cls,
8113 out_loc,
8114 out,
8115 /* placeholder */ 0x5678,
8116 read_barrier_option,
8117 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008118 break;
8119 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008120 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008121 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008122 LOG(FATAL) << "UNREACHABLE";
8123 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008124 }
8125
8126 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8127 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008128 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00008129 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07008130 codegen_->AddSlowPath(slow_path);
8131 if (generate_null_check) {
8132 __ Beqz(out, slow_path->GetEntryLabel());
8133 }
8134 if (cls->MustGenerateClinitCheck()) {
8135 GenerateClassInitializationCheck(slow_path, out);
8136 } else {
8137 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008138 }
8139 }
8140}
8141
8142static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008143 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008144}
8145
8146void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8147 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008148 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008149 locations->SetOut(Location::RequiresRegister());
8150}
8151
8152void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8153 Register out = load->GetLocations()->Out().AsRegister<Register>();
8154 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8155}
8156
8157void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008158 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008159}
8160
8161void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8162 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8163}
8164
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008165void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008166 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008167 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008168 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008169 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008170 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008171 switch (load_kind) {
8172 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008173 case HLoadString::LoadKind::kBootImageAddress:
8174 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008175 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008176 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008177 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008178 break;
8179 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008180 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008181 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
8182 codegen_->ClobberRA();
8183 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008184 break;
8185 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008186 FALLTHROUGH_INTENDED;
8187 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008188 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008189 locations->SetInAt(0, Location::RequiresRegister());
8190 break;
8191 default:
8192 break;
8193 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008194 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008195 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008196 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008197 } else {
8198 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008199 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8200 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8201 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008202 RegisterSet caller_saves = RegisterSet::Empty();
8203 InvokeRuntimeCallingConvention calling_convention;
8204 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8205 locations->SetCustomSlowPathCallerSaves(caller_saves);
8206 } else {
8207 // For non-Baker read barriers we have a temp-clobbering call.
8208 }
8209 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008210 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008211}
8212
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008213// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8214// move.
8215void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008216 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008217 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008218 Location out_loc = locations->Out();
8219 Register out = out_loc.AsRegister<Register>();
8220 Register base_or_current_method_reg;
8221 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008222 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008223 switch (load_kind) {
8224 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008225 case HLoadString::LoadKind::kBootImageAddress:
8226 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008227 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008228 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008229 base_or_current_method_reg =
8230 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008231 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008232 default:
8233 base_or_current_method_reg = ZERO;
8234 break;
8235 }
8236
8237 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008238 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008239 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008240 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008241 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008242 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008243 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008244 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8245 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008246 base_or_current_method_reg);
8247 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008248 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008249 }
8250 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008251 uint32_t address = dchecked_integral_cast<uint32_t>(
8252 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8253 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008254 if (isR6 || !has_irreducible_loops) {
8255 __ LoadLiteral(out,
8256 base_or_current_method_reg,
8257 codegen_->DeduplicateBootImageAddressLiteral(address));
8258 } else {
8259 __ LoadConst32(out, address);
8260 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008261 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008262 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008263 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008264 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008265 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008266 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008267 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008268 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008269 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8270 out,
8271 base_or_current_method_reg);
8272 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8273 return;
8274 }
8275 case HLoadString::LoadKind::kBssEntry: {
8276 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8277 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8278 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8279 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8280 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008281 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008282 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008283 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008284 GenerateGcRootFieldLoad(load,
8285 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008286 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008287 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008288 kCompilerReadBarrierOption,
8289 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008290 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008291 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008292 codegen_->AddSlowPath(slow_path);
8293 __ Beqz(out, slow_path->GetEntryLabel());
8294 __ Bind(slow_path->GetExitLabel());
8295 return;
8296 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008297 case HLoadString::LoadKind::kJitTableAddress: {
8298 CodeGeneratorMIPS::JitPatchInfo* info =
8299 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8300 load->GetStringIndex(),
8301 load->GetString());
8302 bool reordering = __ SetReorder(false);
8303 __ Bind(&info->high_label);
8304 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008305 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008306 GenerateGcRootFieldLoad(load,
8307 out_loc,
8308 out,
8309 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008310 kCompilerReadBarrierOption,
8311 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008312 return;
8313 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008314 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008315 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008316 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008317
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008318 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008319 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008320 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008321 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008322 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008323 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8324 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008325}
8326
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008327void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008328 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008329 locations->SetOut(Location::ConstantLocation(constant));
8330}
8331
8332void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8333 // Will be generated at use site.
8334}
8335
8336void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008337 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8338 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008339 InvokeRuntimeCallingConvention calling_convention;
8340 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8341}
8342
8343void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8344 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008345 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008346 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8347 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008348 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008349 }
8350 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8351}
8352
8353void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8354 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008355 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008356 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008357 case DataType::Type::kInt32:
8358 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008359 locations->SetInAt(0, Location::RequiresRegister());
8360 locations->SetInAt(1, Location::RequiresRegister());
8361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8362 break;
8363
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008364 case DataType::Type::kFloat32:
8365 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008366 locations->SetInAt(0, Location::RequiresFpuRegister());
8367 locations->SetInAt(1, Location::RequiresFpuRegister());
8368 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8369 break;
8370
8371 default:
8372 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8373 }
8374}
8375
8376void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008377 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008378 LocationSummary* locations = instruction->GetLocations();
8379 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8380
8381 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008382 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008383 Register dst = locations->Out().AsRegister<Register>();
8384 Register lhs = locations->InAt(0).AsRegister<Register>();
8385 Register rhs = locations->InAt(1).AsRegister<Register>();
8386
8387 if (isR6) {
8388 __ MulR6(dst, lhs, rhs);
8389 } else {
8390 __ MulR2(dst, lhs, rhs);
8391 }
8392 break;
8393 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008394 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008395 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8396 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8397 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8398 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8399 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8400 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8401
8402 // Extra checks to protect caused by the existance of A1_A2.
8403 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8404 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8405 DCHECK_NE(dst_high, lhs_low);
8406 DCHECK_NE(dst_high, rhs_low);
8407
8408 // A_B * C_D
8409 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8410 // dst_lo: [ low(B*D) ]
8411 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8412
8413 if (isR6) {
8414 __ MulR6(TMP, lhs_high, rhs_low);
8415 __ MulR6(dst_high, lhs_low, rhs_high);
8416 __ Addu(dst_high, dst_high, TMP);
8417 __ MuhuR6(TMP, lhs_low, rhs_low);
8418 __ Addu(dst_high, dst_high, TMP);
8419 __ MulR6(dst_low, lhs_low, rhs_low);
8420 } else {
8421 __ MulR2(TMP, lhs_high, rhs_low);
8422 __ MulR2(dst_high, lhs_low, rhs_high);
8423 __ Addu(dst_high, dst_high, TMP);
8424 __ MultuR2(lhs_low, rhs_low);
8425 __ Mfhi(TMP);
8426 __ Addu(dst_high, dst_high, TMP);
8427 __ Mflo(dst_low);
8428 }
8429 break;
8430 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008431 case DataType::Type::kFloat32:
8432 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008433 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8434 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8435 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008436 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008437 __ MulS(dst, lhs, rhs);
8438 } else {
8439 __ MulD(dst, lhs, rhs);
8440 }
8441 break;
8442 }
8443 default:
8444 LOG(FATAL) << "Unexpected mul type " << type;
8445 }
8446}
8447
8448void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8449 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008450 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008451 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008452 case DataType::Type::kInt32:
8453 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008454 locations->SetInAt(0, Location::RequiresRegister());
8455 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8456 break;
8457
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008458 case DataType::Type::kFloat32:
8459 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008460 locations->SetInAt(0, Location::RequiresFpuRegister());
8461 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8462 break;
8463
8464 default:
8465 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8466 }
8467}
8468
8469void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008470 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008471 LocationSummary* locations = instruction->GetLocations();
8472
8473 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008474 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008475 Register dst = locations->Out().AsRegister<Register>();
8476 Register src = locations->InAt(0).AsRegister<Register>();
8477 __ Subu(dst, ZERO, src);
8478 break;
8479 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008480 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008481 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8482 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8483 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8484 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8485 __ Subu(dst_low, ZERO, src_low);
8486 __ Sltu(TMP, ZERO, dst_low);
8487 __ Subu(dst_high, ZERO, src_high);
8488 __ Subu(dst_high, dst_high, TMP);
8489 break;
8490 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008491 case DataType::Type::kFloat32:
8492 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008493 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8494 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008495 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008496 __ NegS(dst, src);
8497 } else {
8498 __ NegD(dst, src);
8499 }
8500 break;
8501 }
8502 default:
8503 LOG(FATAL) << "Unexpected neg type " << type;
8504 }
8505}
8506
8507void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008508 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8509 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008510 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008511 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008512 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8513 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008514}
8515
8516void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008517 // Note: if heap poisoning is enabled, the entry point takes care
8518 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008519 QuickEntrypointEnum entrypoint =
8520 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8521 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008522 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008523 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008524}
8525
8526void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008527 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8528 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008529 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008530 if (instruction->IsStringAlloc()) {
8531 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8532 } else {
8533 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008534 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008535 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008536}
8537
8538void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008539 // Note: if heap poisoning is enabled, the entry point takes care
8540 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008541 if (instruction->IsStringAlloc()) {
8542 // String is allocated through StringFactory. Call NewEmptyString entry point.
8543 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008544 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008545 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8546 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8547 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008548 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008549 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8550 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008551 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008552 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008553 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008554}
8555
8556void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008557 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008558 locations->SetInAt(0, Location::RequiresRegister());
8559 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8560}
8561
8562void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008563 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008564 LocationSummary* locations = instruction->GetLocations();
8565
8566 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008567 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008568 Register dst = locations->Out().AsRegister<Register>();
8569 Register src = locations->InAt(0).AsRegister<Register>();
8570 __ Nor(dst, src, ZERO);
8571 break;
8572 }
8573
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008574 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008575 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8576 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8577 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8578 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8579 __ Nor(dst_high, src_high, ZERO);
8580 __ Nor(dst_low, src_low, ZERO);
8581 break;
8582 }
8583
8584 default:
8585 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8586 }
8587}
8588
8589void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008590 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008591 locations->SetInAt(0, Location::RequiresRegister());
8592 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8593}
8594
8595void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8596 LocationSummary* locations = instruction->GetLocations();
8597 __ Xori(locations->Out().AsRegister<Register>(),
8598 locations->InAt(0).AsRegister<Register>(),
8599 1);
8600}
8601
8602void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008603 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8604 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008605}
8606
Calin Juravle2ae48182016-03-16 14:05:09 +00008607void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8608 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008609 return;
8610 }
8611 Location obj = instruction->GetLocations()->InAt(0);
8612
8613 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008614 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008615}
8616
Calin Juravle2ae48182016-03-16 14:05:09 +00008617void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008618 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008619 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008620
8621 Location obj = instruction->GetLocations()->InAt(0);
8622
8623 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8624}
8625
8626void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008627 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008628}
8629
8630void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8631 HandleBinaryOp(instruction);
8632}
8633
8634void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8635 HandleBinaryOp(instruction);
8636}
8637
8638void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8639 LOG(FATAL) << "Unreachable";
8640}
8641
8642void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008643 if (instruction->GetNext()->IsSuspendCheck() &&
8644 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8645 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8646 // The back edge will generate the suspend check.
8647 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8648 }
8649
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008650 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8651}
8652
8653void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008654 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008655 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8656 if (location.IsStackSlot()) {
8657 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8658 } else if (location.IsDoubleStackSlot()) {
8659 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8660 }
8661 locations->SetOut(location);
8662}
8663
8664void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8665 ATTRIBUTE_UNUSED) {
8666 // Nothing to do, the parameter is already at its location.
8667}
8668
8669void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8670 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008671 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008672 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8673}
8674
8675void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8676 ATTRIBUTE_UNUSED) {
8677 // Nothing to do, the method is already at its location.
8678}
8679
8680void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008681 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008682 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008683 locations->SetInAt(i, Location::Any());
8684 }
8685 locations->SetOut(Location::Any());
8686}
8687
8688void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8689 LOG(FATAL) << "Unreachable";
8690}
8691
8692void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008693 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008694 bool call_rem;
8695 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8696 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8697 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8698 } else {
8699 call_rem = (type != DataType::Type::kInt32);
8700 }
8701 LocationSummary::CallKind call_kind = call_rem
8702 ? LocationSummary::kCallOnMainOnly
8703 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008704 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008705
8706 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008707 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008708 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008709 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008710 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8711 break;
8712
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008713 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008714 if (call_rem) {
8715 InvokeRuntimeCallingConvention calling_convention;
8716 locations->SetInAt(0, Location::RegisterPairLocation(
8717 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8718 locations->SetInAt(1, Location::RegisterPairLocation(
8719 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8720 locations->SetOut(calling_convention.GetReturnLocation(type));
8721 } else {
8722 locations->SetInAt(0, Location::RequiresRegister());
8723 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8724 locations->SetOut(Location::RequiresRegister());
8725 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008726 break;
8727 }
8728
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008729 case DataType::Type::kFloat32:
8730 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008731 InvokeRuntimeCallingConvention calling_convention;
8732 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8733 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8734 locations->SetOut(calling_convention.GetReturnLocation(type));
8735 break;
8736 }
8737
8738 default:
8739 LOG(FATAL) << "Unexpected rem type " << type;
8740 }
8741}
8742
8743void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008744 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008745 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008746
8747 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008748 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008749 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008750 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008751 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008752 if (locations->InAt(1).IsConstant()) {
8753 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8754 if (imm == 0) {
8755 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8756 } else if (imm == 1 || imm == -1) {
8757 DivRemOneOrMinusOne(instruction);
8758 } else {
8759 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8760 DivRemByPowerOfTwo(instruction);
8761 }
8762 } else {
8763 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8764 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8765 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008766 break;
8767 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008768 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008769 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008770 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008771 break;
8772 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008773 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008774 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008775 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008776 break;
8777 }
8778 default:
8779 LOG(FATAL) << "Unexpected rem type " << type;
8780 }
8781}
8782
Aart Bik3dad3412018-02-28 12:01:46 -08008783void LocationsBuilderMIPS::VisitAbs(HAbs* abs) {
8784 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
8785 switch (abs->GetResultType()) {
8786 case DataType::Type::kInt32:
8787 case DataType::Type::kInt64:
8788 locations->SetInAt(0, Location::RequiresRegister());
8789 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8790 break;
8791 case DataType::Type::kFloat32:
8792 case DataType::Type::kFloat64:
8793 locations->SetInAt(0, Location::RequiresFpuRegister());
8794 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8795 break;
8796 default:
8797 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
8798 }
8799}
8800
8801void InstructionCodeGeneratorMIPS::GenerateAbsFP(LocationSummary* locations,
8802 DataType::Type type,
8803 bool isR2OrNewer,
8804 bool isR6) {
8805 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
8806 FRegister out = locations->Out().AsFpuRegister<FRegister>();
8807
8808 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
8809 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
8810 // (signaling NaN may become quiet though).
8811 //
8812 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
8813 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
8814 // affected by this instruction.
8815 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
8816 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
8817 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
8818 if (isR6) {
8819 if (type == DataType::Type::kFloat64) {
8820 __ AbsD(out, in);
8821 } else {
8822 DCHECK_EQ(type, DataType::Type::kFloat32);
8823 __ AbsS(out, in);
8824 }
8825 } else {
8826 if (type == DataType::Type::kFloat64) {
8827 if (in != out) {
8828 __ MovD(out, in);
8829 }
8830 __ MoveFromFpuHigh(TMP, in);
8831 // ins instruction is not available for R1.
8832 if (isR2OrNewer) {
8833 __ Ins(TMP, ZERO, 31, 1);
8834 } else {
8835 __ Sll(TMP, TMP, 1);
8836 __ Srl(TMP, TMP, 1);
8837 }
8838 __ MoveToFpuHigh(TMP, out);
8839 } else {
8840 DCHECK_EQ(type, DataType::Type::kFloat32);
8841 __ Mfc1(TMP, in);
8842 // ins instruction is not available for R1.
8843 if (isR2OrNewer) {
8844 __ Ins(TMP, ZERO, 31, 1);
8845 } else {
8846 __ Sll(TMP, TMP, 1);
8847 __ Srl(TMP, TMP, 1);
8848 }
8849 __ Mtc1(TMP, out);
8850 }
8851 }
8852}
8853
8854void InstructionCodeGeneratorMIPS::VisitAbs(HAbs* abs) {
8855 LocationSummary* locations = abs->GetLocations();
8856 bool isR2OrNewer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
8857 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8858 switch (abs->GetResultType()) {
8859 case DataType::Type::kInt32: {
8860 Register in = locations->InAt(0).AsRegister<Register>();
8861 Register out = locations->Out().AsRegister<Register>();
8862 __ Sra(AT, in, 31);
8863 __ Xor(out, in, AT);
8864 __ Subu(out, out, AT);
8865 break;
8866 }
8867 case DataType::Type::kInt64: {
8868 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8869 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8870 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8871 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8872 // The comments in this section show the analogous operations which would
8873 // be performed if we had 64-bit registers "in", and "out".
8874 // __ Dsra32(AT, in, 31);
8875 __ Sra(AT, in_hi, 31);
8876 // __ Xor(out, in, AT);
8877 __ Xor(TMP, in_lo, AT);
8878 __ Xor(out_hi, in_hi, AT);
8879 // __ Dsubu(out, out, AT);
8880 __ Subu(out_lo, TMP, AT);
8881 __ Sltu(TMP, out_lo, TMP);
8882 __ Addu(out_hi, out_hi, TMP);
8883 break;
8884 }
8885 case DataType::Type::kFloat32:
8886 case DataType::Type::kFloat64:
8887 GenerateAbsFP(locations, abs->GetResultType(), isR2OrNewer, isR6);
8888 break;
8889 default:
8890 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
8891 }
8892}
8893
Igor Murashkind01745e2017-04-05 16:40:31 -07008894void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8895 constructor_fence->SetLocations(nullptr);
8896}
8897
8898void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8899 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8900 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8901}
8902
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008903void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8904 memory_barrier->SetLocations(nullptr);
8905}
8906
8907void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8908 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8909}
8910
8911void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008912 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008913 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008914 locations->SetInAt(0, MipsReturnLocation(return_type));
8915}
8916
8917void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8918 codegen_->GenerateFrameExit();
8919}
8920
8921void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8922 ret->SetLocations(nullptr);
8923}
8924
8925void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8926 codegen_->GenerateFrameExit();
8927}
8928
Alexey Frunze92d90602015-12-18 18:16:36 -08008929void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8930 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008931}
8932
Alexey Frunze92d90602015-12-18 18:16:36 -08008933void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8934 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008935}
8936
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008937void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8938 HandleShift(shl);
8939}
8940
8941void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8942 HandleShift(shl);
8943}
8944
8945void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8946 HandleShift(shr);
8947}
8948
8949void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8950 HandleShift(shr);
8951}
8952
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008953void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8954 HandleBinaryOp(instruction);
8955}
8956
8957void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8958 HandleBinaryOp(instruction);
8959}
8960
8961void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8962 HandleFieldGet(instruction, instruction->GetFieldInfo());
8963}
8964
8965void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8966 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8967}
8968
8969void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8970 HandleFieldSet(instruction, instruction->GetFieldInfo());
8971}
8972
8973void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008974 HandleFieldSet(instruction,
8975 instruction->GetFieldInfo(),
8976 instruction->GetDexPc(),
8977 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008978}
8979
8980void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8981 HUnresolvedInstanceFieldGet* instruction) {
8982 FieldAccessCallingConventionMIPS calling_convention;
8983 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8984 instruction->GetFieldType(),
8985 calling_convention);
8986}
8987
8988void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8989 HUnresolvedInstanceFieldGet* instruction) {
8990 FieldAccessCallingConventionMIPS calling_convention;
8991 codegen_->GenerateUnresolvedFieldAccess(instruction,
8992 instruction->GetFieldType(),
8993 instruction->GetFieldIndex(),
8994 instruction->GetDexPc(),
8995 calling_convention);
8996}
8997
8998void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8999 HUnresolvedInstanceFieldSet* instruction) {
9000 FieldAccessCallingConventionMIPS calling_convention;
9001 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9002 instruction->GetFieldType(),
9003 calling_convention);
9004}
9005
9006void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
9007 HUnresolvedInstanceFieldSet* instruction) {
9008 FieldAccessCallingConventionMIPS calling_convention;
9009 codegen_->GenerateUnresolvedFieldAccess(instruction,
9010 instruction->GetFieldType(),
9011 instruction->GetFieldIndex(),
9012 instruction->GetDexPc(),
9013 calling_convention);
9014}
9015
9016void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
9017 HUnresolvedStaticFieldGet* instruction) {
9018 FieldAccessCallingConventionMIPS calling_convention;
9019 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9020 instruction->GetFieldType(),
9021 calling_convention);
9022}
9023
9024void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
9025 HUnresolvedStaticFieldGet* instruction) {
9026 FieldAccessCallingConventionMIPS calling_convention;
9027 codegen_->GenerateUnresolvedFieldAccess(instruction,
9028 instruction->GetFieldType(),
9029 instruction->GetFieldIndex(),
9030 instruction->GetDexPc(),
9031 calling_convention);
9032}
9033
9034void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
9035 HUnresolvedStaticFieldSet* instruction) {
9036 FieldAccessCallingConventionMIPS calling_convention;
9037 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9038 instruction->GetFieldType(),
9039 calling_convention);
9040}
9041
9042void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9043 HUnresolvedStaticFieldSet* instruction) {
9044 FieldAccessCallingConventionMIPS calling_convention;
9045 codegen_->GenerateUnresolvedFieldAccess(instruction,
9046 instruction->GetFieldType(),
9047 instruction->GetFieldIndex(),
9048 instruction->GetDexPc(),
9049 calling_convention);
9050}
9051
9052void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009053 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9054 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009055 // In suspend check slow path, usually there are no caller-save registers at all.
9056 // If SIMD instructions are present, however, we force spilling all live SIMD
9057 // registers in full width (since the runtime only saves/restores lower part).
9058 locations->SetCustomSlowPathCallerSaves(
9059 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009060}
9061
9062void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9063 HBasicBlock* block = instruction->GetBlock();
9064 if (block->GetLoopInformation() != nullptr) {
9065 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9066 // The back edge will generate the suspend check.
9067 return;
9068 }
9069 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9070 // The goto will generate the suspend check.
9071 return;
9072 }
9073 GenerateSuspendCheck(instruction, nullptr);
9074}
9075
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009076void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009077 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9078 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009079 InvokeRuntimeCallingConvention calling_convention;
9080 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9081}
9082
9083void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009084 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009085 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9086}
9087
9088void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009089 DataType::Type input_type = conversion->GetInputType();
9090 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009091 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9092 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009093 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009094
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009095 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9096 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009097 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9098 }
9099
9100 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009101 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009102 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9103 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009104 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009105 }
9106
Vladimir Markoca6fff82017-10-03 14:49:14 +01009107 LocationSummary* locations =
9108 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009109
9110 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009111 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009112 locations->SetInAt(0, Location::RequiresFpuRegister());
9113 } else {
9114 locations->SetInAt(0, Location::RequiresRegister());
9115 }
9116
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009117 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009118 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9119 } else {
9120 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9121 }
9122 } else {
9123 InvokeRuntimeCallingConvention calling_convention;
9124
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009125 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009126 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9127 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009128 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009129 locations->SetInAt(0, Location::RegisterPairLocation(
9130 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9131 }
9132
9133 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9134 }
9135}
9136
9137void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9138 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009139 DataType::Type result_type = conversion->GetResultType();
9140 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009141 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009142 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009143
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009144 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9145 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009146
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009147 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009148 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9149 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9150 Register src = locations->InAt(0).AsRegister<Register>();
9151
Alexey Frunzea871ef12016-06-27 15:20:11 -07009152 if (dst_low != src) {
9153 __ Move(dst_low, src);
9154 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009155 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009156 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009157 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009158 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009159 ? locations->InAt(0).AsRegisterPairLow<Register>()
9160 : locations->InAt(0).AsRegister<Register>();
9161
9162 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009163 case DataType::Type::kUint8:
9164 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009165 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009166 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009167 if (has_sign_extension) {
9168 __ Seb(dst, src);
9169 } else {
9170 __ Sll(dst, src, 24);
9171 __ Sra(dst, dst, 24);
9172 }
9173 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009174 case DataType::Type::kUint16:
9175 __ Andi(dst, src, 0xFFFF);
9176 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009177 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009178 if (has_sign_extension) {
9179 __ Seh(dst, src);
9180 } else {
9181 __ Sll(dst, src, 16);
9182 __ Sra(dst, dst, 16);
9183 }
9184 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009185 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009186 if (dst != src) {
9187 __ Move(dst, src);
9188 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009189 break;
9190
9191 default:
9192 LOG(FATAL) << "Unexpected type conversion from " << input_type
9193 << " to " << result_type;
9194 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009195 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9196 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009197 if (isR6) {
9198 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9199 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9200 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9201 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9202 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9203 __ Mtc1(src_low, FTMP);
9204 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009205 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009206 __ Cvtsl(dst, FTMP);
9207 } else {
9208 __ Cvtdl(dst, FTMP);
9209 }
9210 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009211 QuickEntrypointEnum entrypoint =
9212 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009213 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009214 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009215 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9216 } else {
9217 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9218 }
9219 }
9220 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009221 Register src = locations->InAt(0).AsRegister<Register>();
9222 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9223 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009224 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009225 __ Cvtsw(dst, FTMP);
9226 } else {
9227 __ Cvtdw(dst, FTMP);
9228 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009229 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009230 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9231 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009232
9233 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9234 // value of the output type if the input is outside of the range after the truncation or
9235 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9236 // results. This matches the desired float/double-to-int/long conversion exactly.
9237 //
9238 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9239 // value when the input is either a NaN or is outside of the range of the output type
9240 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9241 // the same result.
9242 //
9243 // The code takes care of the different behaviors by first comparing the input to the
9244 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9245 // If the input is greater than or equal to the minimum, it procedes to the truncate
9246 // instruction, which will handle such an input the same way irrespective of NAN2008.
9247 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9248 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009249 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009250 if (isR6) {
9251 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9252 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9253 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9254 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9255 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009256
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009257 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009258 __ TruncLS(FTMP, src);
9259 } else {
9260 __ TruncLD(FTMP, src);
9261 }
9262 __ Mfc1(dst_low, FTMP);
9263 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009264 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009265 QuickEntrypointEnum entrypoint =
9266 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009267 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009268 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009269 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9270 } else {
9271 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9272 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009273 }
9274 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009275 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9276 Register dst = locations->Out().AsRegister<Register>();
9277 MipsLabel truncate;
9278 MipsLabel done;
9279
Lena Djokicf4e23a82017-05-09 15:43:45 +02009280 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009281 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009282 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9283 __ LoadConst32(TMP, min_val);
9284 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009285 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009286 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9287 __ LoadConst32(TMP, High32Bits(min_val));
9288 __ Mtc1(ZERO, FTMP);
9289 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009290 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009291
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009292 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009293 __ ColeS(0, FTMP, src);
9294 } else {
9295 __ ColeD(0, FTMP, src);
9296 }
9297 __ Bc1t(0, &truncate);
9298
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009299 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009300 __ CeqS(0, src, src);
9301 } else {
9302 __ CeqD(0, src, src);
9303 }
9304 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9305 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009306
9307 __ B(&done);
9308
9309 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009310 }
9311
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009312 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009313 __ TruncWS(FTMP, src);
9314 } else {
9315 __ TruncWD(FTMP, src);
9316 }
9317 __ Mfc1(dst, FTMP);
9318
Lena Djokicf4e23a82017-05-09 15:43:45 +02009319 if (!isR6) {
9320 __ Bind(&done);
9321 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009322 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009323 } else if (DataType::IsFloatingPointType(result_type) &&
9324 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009325 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9326 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009327 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009328 __ Cvtsd(dst, src);
9329 } else {
9330 __ Cvtds(dst, src);
9331 }
9332 } else {
9333 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9334 << " to " << result_type;
9335 }
9336}
9337
9338void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9339 HandleShift(ushr);
9340}
9341
9342void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9343 HandleShift(ushr);
9344}
9345
9346void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9347 HandleBinaryOp(instruction);
9348}
9349
9350void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9351 HandleBinaryOp(instruction);
9352}
9353
9354void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9355 // Nothing to do, this should be removed during prepare for register allocator.
9356 LOG(FATAL) << "Unreachable";
9357}
9358
9359void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9360 // Nothing to do, this should be removed during prepare for register allocator.
9361 LOG(FATAL) << "Unreachable";
9362}
9363
9364void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009365 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009366}
9367
9368void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009369 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009370}
9371
9372void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009373 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009374}
9375
9376void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009377 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009378}
9379
9380void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009381 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009382}
9383
9384void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009385 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009386}
9387
9388void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009389 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009390}
9391
9392void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009393 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009394}
9395
9396void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009397 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009398}
9399
9400void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009401 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009402}
9403
9404void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009405 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009406}
9407
9408void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009409 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009410}
9411
9412void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009413 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009414}
9415
9416void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009417 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009418}
9419
9420void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009421 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009422}
9423
9424void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009425 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009426}
9427
9428void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009429 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009430}
9431
9432void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009433 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009434}
9435
9436void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009437 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009438}
9439
9440void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009441 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009442}
9443
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009444void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9445 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009446 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009447 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009448 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9449 uint32_t num_entries = switch_instr->GetNumEntries();
9450 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9451 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9452 // instruction to simulate PC-relative addressing when accessing the jump table.
9453 // NAL clobbers RA. Make sure RA is preserved.
9454 codegen_->ClobberRA();
9455 }
9456 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009457}
9458
Alexey Frunze96b66822016-09-10 02:32:44 -07009459void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9460 int32_t lower_bound,
9461 uint32_t num_entries,
9462 HBasicBlock* switch_block,
9463 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009464 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009465 Register temp_reg = TMP;
9466 __ Addiu32(temp_reg, value_reg, -lower_bound);
9467 // Jump to default if index is negative
9468 // Note: We don't check the case that index is positive while value < lower_bound, because in
9469 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9470 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9471
Alexey Frunze96b66822016-09-10 02:32:44 -07009472 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009473 // Jump to successors[0] if value == lower_bound.
9474 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9475 int32_t last_index = 0;
9476 for (; num_entries - last_index > 2; last_index += 2) {
9477 __ Addiu(temp_reg, temp_reg, -2);
9478 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9479 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9480 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9481 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9482 }
9483 if (num_entries - last_index == 2) {
9484 // The last missing case_value.
9485 __ Addiu(temp_reg, temp_reg, -1);
9486 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009487 }
9488
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009489 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009490 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009491 __ B(codegen_->GetLabelOf(default_block));
9492 }
9493}
9494
Alexey Frunze96b66822016-09-10 02:32:44 -07009495void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9496 Register constant_area,
9497 int32_t lower_bound,
9498 uint32_t num_entries,
9499 HBasicBlock* switch_block,
9500 HBasicBlock* default_block) {
9501 // Create a jump table.
9502 std::vector<MipsLabel*> labels(num_entries);
9503 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9504 for (uint32_t i = 0; i < num_entries; i++) {
9505 labels[i] = codegen_->GetLabelOf(successors[i]);
9506 }
9507 JumpTable* table = __ CreateJumpTable(std::move(labels));
9508
9509 // Is the value in range?
9510 __ Addiu32(TMP, value_reg, -lower_bound);
9511 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9512 __ Sltiu(AT, TMP, num_entries);
9513 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9514 } else {
9515 __ LoadConst32(AT, num_entries);
9516 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9517 }
9518
9519 // We are in the range of the table.
9520 // Load the target address from the jump table, indexing by the value.
9521 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009522 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009523 __ Lw(TMP, TMP, 0);
9524 // Compute the absolute target address by adding the table start address
9525 // (the table contains offsets to targets relative to its start).
9526 __ Addu(TMP, TMP, AT);
9527 // And jump.
9528 __ Jr(TMP);
9529 __ NopIfNoReordering();
9530}
9531
9532void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9533 int32_t lower_bound = switch_instr->GetStartValue();
9534 uint32_t num_entries = switch_instr->GetNumEntries();
9535 LocationSummary* locations = switch_instr->GetLocations();
9536 Register value_reg = locations->InAt(0).AsRegister<Register>();
9537 HBasicBlock* switch_block = switch_instr->GetBlock();
9538 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9539
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009540 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009541 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009542 //
9543 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9544 // to access the jump table and it is implemented by changing HPackedSwitch to
9545 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9546 // VisitMipsPackedSwitch()).
9547 //
9548 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9549 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9550 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009551 GenTableBasedPackedSwitch(value_reg,
9552 ZERO,
9553 lower_bound,
9554 num_entries,
9555 switch_block,
9556 default_block);
9557 } else {
9558 GenPackedSwitchWithCompares(value_reg,
9559 lower_bound,
9560 num_entries,
9561 switch_block,
9562 default_block);
9563 }
9564}
9565
9566void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9567 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009568 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009569 locations->SetInAt(0, Location::RequiresRegister());
9570 // Constant area pointer (HMipsComputeBaseMethodAddress).
9571 locations->SetInAt(1, Location::RequiresRegister());
9572}
9573
9574void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9575 int32_t lower_bound = switch_instr->GetStartValue();
9576 uint32_t num_entries = switch_instr->GetNumEntries();
9577 LocationSummary* locations = switch_instr->GetLocations();
9578 Register value_reg = locations->InAt(0).AsRegister<Register>();
9579 Register constant_area = locations->InAt(1).AsRegister<Register>();
9580 HBasicBlock* switch_block = switch_instr->GetBlock();
9581 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9582
9583 // This is an R2-only path. HPackedSwitch has been changed to
9584 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9585 // required to address the jump table relative to PC.
9586 GenTableBasedPackedSwitch(value_reg,
9587 constant_area,
9588 lower_bound,
9589 num_entries,
9590 switch_block,
9591 default_block);
9592}
9593
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009594void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9595 HMipsComputeBaseMethodAddress* insn) {
9596 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009597 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009598 locations->SetOut(Location::RequiresRegister());
9599}
9600
9601void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9602 HMipsComputeBaseMethodAddress* insn) {
9603 LocationSummary* locations = insn->GetLocations();
9604 Register reg = locations->Out().AsRegister<Register>();
9605
9606 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9607
9608 // Generate a dummy PC-relative call to obtain PC.
9609 __ Nal();
9610 // Grab the return address off RA.
9611 __ Move(reg, RA);
9612
9613 // Remember this offset (the obtained PC value) for later use with constant area.
9614 __ BindPcRelBaseLabel();
9615}
9616
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009617void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9618 // The trampoline uses the same calling convention as dex calling conventions,
9619 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9620 // the method_idx.
9621 HandleInvoke(invoke);
9622}
9623
9624void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9625 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9626}
9627
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009628void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9629 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009630 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009631 locations->SetInAt(0, Location::RequiresRegister());
9632 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009633}
9634
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009635void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9636 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009637 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009638 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009639 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009640 __ LoadFromOffset(kLoadWord,
9641 locations->Out().AsRegister<Register>(),
9642 locations->InAt(0).AsRegister<Register>(),
9643 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009644 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009645 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009646 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009647 __ LoadFromOffset(kLoadWord,
9648 locations->Out().AsRegister<Register>(),
9649 locations->InAt(0).AsRegister<Register>(),
9650 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009651 __ LoadFromOffset(kLoadWord,
9652 locations->Out().AsRegister<Register>(),
9653 locations->Out().AsRegister<Register>(),
9654 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009655 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009656}
9657
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009658void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9659 ATTRIBUTE_UNUSED) {
9660 LOG(FATAL) << "Unreachable";
9661}
9662
9663void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9664 ATTRIBUTE_UNUSED) {
9665 LOG(FATAL) << "Unreachable";
9666}
9667
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009668#undef __
9669#undef QUICK_ENTRY_POINT
9670
9671} // namespace mips
9672} // namespace art