blob: 931c9b033e4cda12f5d33f256fcf8c9c07f03f38 [file] [log] [blame]
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include "code_generator_mips.h"
18
Alexey Frunze4147fcc2017-06-17 19:57:27 -070019#include "arch/mips/asm_support_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020020#include "arch/mips/entrypoints_direct_mips.h"
21#include "arch/mips/instruction_set_features_mips.h"
22#include "art_method.h"
Vladimir Marko94ec2db2017-09-06 17:21:03 +010023#include "class_table.h"
Chris Larsen701566a2015-10-27 15:29:13 -070024#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010025#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020026#include "entrypoints/quick/quick_entrypoints.h"
27#include "entrypoints/quick/quick_entrypoints_enum.h"
28#include "gc/accounting/card_table.h"
Andreas Gampe09659c22017-09-18 18:23:32 -070029#include "heap_poisoning.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020030#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070031#include "intrinsics_mips.h"
Vladimir Markod8dbc8d2017-09-20 13:37:47 +010032#include "linker/linker_patch.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020033#include "mirror/array-inl.h"
34#include "mirror/class-inl.h"
35#include "offsets.h"
Vladimir Marko174b2e22017-10-12 13:34:49 +010036#include "stack_map_stream.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020037#include "thread.h"
38#include "utils/assembler.h"
39#include "utils/mips/assembler_mips.h"
40#include "utils/stack_checks.h"
41
42namespace art {
43namespace mips {
44
45static constexpr int kCurrentMethodStackOffset = 0;
46static constexpr Register kMethodRegisterArgument = A0;
47
Alexey Frunze4147fcc2017-06-17 19:57:27 -070048// Flags controlling the use of thunks for Baker read barriers.
49constexpr bool kBakerReadBarrierThunksEnableForFields = true;
50constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
51constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
52
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010053Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020054 switch (return_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010055 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010056 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010057 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010058 case DataType::Type::kInt8:
59 case DataType::Type::kUint16:
60 case DataType::Type::kInt16:
Aart Bik66c158e2018-01-31 12:55:04 -080061 case DataType::Type::kUint32:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010062 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020063 return Location::RegisterLocation(V0);
64
Aart Bik66c158e2018-01-31 12:55:04 -080065 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010066 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020067 return Location::RegisterPairLocation(V0, V1);
68
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010069 case DataType::Type::kFloat32:
70 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020071 return Location::FpuRegisterLocation(F0);
72
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010073 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020074 return Location();
75 }
76 UNREACHABLE();
77}
78
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010079Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020080 return MipsReturnLocation(type);
81}
82
83Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
84 return Location::RegisterLocation(kMethodRegisterArgument);
85}
86
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020088 Location next_location;
89
90 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010091 case DataType::Type::kReference:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010092 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010093 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010094 case DataType::Type::kInt8:
95 case DataType::Type::kUint16:
96 case DataType::Type::kInt16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +010097 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020098 uint32_t gp_index = gp_index_++;
99 if (gp_index < calling_convention.GetNumberOfRegisters()) {
100 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
101 } else {
102 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
103 next_location = Location::StackSlot(stack_offset);
104 }
105 break;
106 }
107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100108 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200109 uint32_t gp_index = gp_index_;
110 gp_index_ += 2;
111 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800112 Register reg = calling_convention.GetRegisterAt(gp_index);
113 if (reg == A1 || reg == A3) {
114 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200115 gp_index++;
116 }
117 Register low_even = calling_convention.GetRegisterAt(gp_index);
118 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
119 DCHECK_EQ(low_even + 1, high_odd);
120 next_location = Location::RegisterPairLocation(low_even, high_odd);
121 } else {
122 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
123 next_location = Location::DoubleStackSlot(stack_offset);
124 }
125 break;
126 }
127
128 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
129 // will take up the even/odd pair, while floats are stored in even regs only.
130 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100131 case DataType::Type::kFloat32:
132 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200133 uint32_t float_index = float_index_++;
134 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
135 next_location = Location::FpuRegisterLocation(
136 calling_convention.GetFpuRegisterAt(float_index));
137 } else {
138 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100139 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
140 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200141 }
142 break;
143 }
144
Aart Bik66c158e2018-01-31 12:55:04 -0800145 case DataType::Type::kUint32:
146 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100147 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200148 LOG(FATAL) << "Unexpected parameter type " << type;
149 break;
150 }
151
152 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100153 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200154
155 return next_location;
156}
157
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100158Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200159 return MipsReturnLocation(type);
160}
161
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100162// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
163#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700164#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200165
166class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
167 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000168 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200169
170 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
171 LocationSummary* locations = instruction_->GetLocations();
172 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
173 __ Bind(GetEntryLabel());
174 if (instruction_->CanThrowIntoCatchBlock()) {
175 // Live registers will be restored in the catch block if caught.
176 SaveLiveRegisters(codegen, instruction_->GetLocations());
177 }
178 // We're moving two locations to locations that could overlap, so we need a parallel
179 // move resolver.
180 InvokeRuntimeCallingConvention calling_convention;
181 codegen->EmitParallelMoves(locations->InAt(0),
182 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100183 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200184 locations->InAt(1),
185 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100186 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100187 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
188 ? kQuickThrowStringBounds
189 : kQuickThrowArrayBounds;
190 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100191 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200192 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
193 }
194
195 bool IsFatal() const OVERRIDE { return true; }
196
197 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
198
199 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200200 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
201};
202
203class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
204 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000205 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200206
207 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
208 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
209 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100210 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200211 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
212 }
213
214 bool IsFatal() const OVERRIDE { return true; }
215
216 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
217
218 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200219 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
220};
221
222class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
223 public:
224 LoadClassSlowPathMIPS(HLoadClass* cls,
225 HInstruction* at,
226 uint32_t dex_pc,
Vladimir Markof3c52b42017-11-17 17:32:12 +0000227 bool do_clinit)
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700228 : SlowPathCodeMIPS(at),
229 cls_(cls),
230 dex_pc_(dex_pc),
Vladimir Markof3c52b42017-11-17 17:32:12 +0000231 do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
233 }
234
235 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000236 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700237 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200238 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700239 InvokeRuntimeCallingConvention calling_convention;
240 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200241 __ Bind(GetEntryLabel());
242 SaveLiveRegisters(codegen, locations);
243
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000244 dex::TypeIndex type_index = cls_->GetTypeIndex();
245 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100246 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
247 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000248 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200249 if (do_clinit_) {
250 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
251 } else {
252 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
253 }
254
255 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200256 if (out.IsValid()) {
257 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100258 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700259 mips_codegen->MoveLocation(out,
260 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
261 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200262 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200263 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700264
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200265 __ B(GetExitLabel());
266 }
267
268 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
269
270 private:
271 // The class this slow path will load.
272 HLoadClass* const cls_;
273
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200274 // The dex PC of `at_`.
275 const uint32_t dex_pc_;
276
277 // Whether to initialize the class.
278 const bool do_clinit_;
279
280 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
281};
282
283class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
284 public:
Vladimir Markof3c52b42017-11-17 17:32:12 +0000285 explicit LoadStringSlowPathMIPS(HLoadString* instruction)
286 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287
288 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700289 DCHECK(instruction_->IsLoadString());
290 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200291 LocationSummary* locations = instruction_->GetLocations();
292 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Vladimir Markof3c52b42017-11-17 17:32:12 +0000293 const dex::StringIndex string_index = instruction_->AsLoadString()->GetStringIndex();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200294 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700295 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200296 __ Bind(GetEntryLabel());
297 SaveLiveRegisters(codegen, locations);
298
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000299 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100300 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200301 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700302
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100303 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200304 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700305 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200306 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000308
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200309 __ B(GetExitLabel());
310 }
311
312 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
313
314 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200315 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
316};
317
318class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
319 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000320 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200321
322 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
323 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
324 __ Bind(GetEntryLabel());
325 if (instruction_->CanThrowIntoCatchBlock()) {
326 // Live registers will be restored in the catch block if caught.
327 SaveLiveRegisters(codegen, instruction_->GetLocations());
328 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100329 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330 instruction_,
331 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100332 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200333 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
334 }
335
336 bool IsFatal() const OVERRIDE { return true; }
337
338 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
339
340 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
342};
343
344class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
345 public:
346 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000347 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200348
349 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200350 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200351 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
352 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200353 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100354 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200355 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200356 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200357 if (successor_ == nullptr) {
358 __ B(GetReturnLabel());
359 } else {
360 __ B(mips_codegen->GetLabelOf(successor_));
361 }
362 }
363
364 MipsLabel* GetReturnLabel() {
365 DCHECK(successor_ == nullptr);
366 return &return_label_;
367 }
368
369 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
370
Chris Larsena2045912017-11-02 12:39:54 -0700371 HBasicBlock* GetSuccessor() const {
372 return successor_;
373 }
374
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200375 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200376 // If not null, the block to branch to after the suspend check.
377 HBasicBlock* const successor_;
378
379 // If `successor_` is null, the label to branch to after the suspend check.
380 MipsLabel return_label_;
381
382 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
383};
384
385class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
386 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800387 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
388 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200389
390 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
391 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200392 uint32_t dex_pc = instruction_->GetDexPc();
393 DCHECK(instruction_->IsCheckCast()
394 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
395 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
396
397 __ Bind(GetEntryLabel());
Alexey Frunzedfc30af2018-01-24 16:25:10 -0800398 if (!is_fatal_ || instruction_->CanThrowIntoCatchBlock()) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800399 SaveLiveRegisters(codegen, locations);
400 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200401
402 // We're moving two locations to locations that could overlap, so we need a parallel
403 // move resolver.
404 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800405 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200406 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100407 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800408 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200409 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100410 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200411 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100412 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800413 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100414 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200415 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
416 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200417 } else {
418 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800419 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
420 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 }
422
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800423 if (!is_fatal_) {
424 RestoreLiveRegisters(codegen, locations);
425 __ B(GetExitLabel());
426 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200427 }
428
429 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
430
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800431 bool IsFatal() const OVERRIDE { return is_fatal_; }
432
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800434 const bool is_fatal_;
435
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200436 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
437};
438
439class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
440 public:
Aart Bik42249c32016-01-07 15:33:50 -0800441 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000442 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200443
444 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800445 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100447 LocationSummary* locations = instruction_->GetLocations();
448 SaveLiveRegisters(codegen, locations);
449 InvokeRuntimeCallingConvention calling_convention;
450 __ LoadConst32(calling_convention.GetRegisterAt(0),
451 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100452 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100453 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 }
455
456 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
457
458 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200459 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
460};
461
Alexey Frunze15958152017-02-09 19:08:30 -0800462class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
463 public:
464 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
465
466 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
467 LocationSummary* locations = instruction_->GetLocations();
468 __ Bind(GetEntryLabel());
469 SaveLiveRegisters(codegen, locations);
470
471 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100472 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800473 parallel_move.AddMove(
474 locations->InAt(0),
475 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100476 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800477 nullptr);
478 parallel_move.AddMove(
479 locations->InAt(1),
480 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100481 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800482 nullptr);
483 parallel_move.AddMove(
484 locations->InAt(2),
485 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100486 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800487 nullptr);
488 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
489
490 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
491 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
492 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
493 RestoreLiveRegisters(codegen, locations);
494 __ B(GetExitLabel());
495 }
496
497 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
498
499 private:
500 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
501};
502
503// Slow path marking an object reference `ref` during a read
504// barrier. The field `obj.field` in the object `obj` holding this
505// reference does not get updated by this slow path after marking (see
506// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
507//
508// This means that after the execution of this slow path, `ref` will
509// always be up-to-date, but `obj.field` may not; i.e., after the
510// flip, `ref` will be a to-space reference, but `obj.field` will
511// probably still be a from-space reference (unless it gets updated by
512// another thread, or if another thread installed another object
513// reference (different from `ref`) in `obj.field`).
514//
515// If `entrypoint` is a valid location it is assumed to already be
516// holding the entrypoint. The case where the entrypoint is passed in
517// is for the GcRoot read barrier.
518class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
519 public:
520 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
521 Location ref,
522 Location entrypoint = Location::NoLocation())
523 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
524 DCHECK(kEmitCompilerReadBarrier);
525 }
526
527 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
528
529 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
530 LocationSummary* locations = instruction_->GetLocations();
531 Register ref_reg = ref_.AsRegister<Register>();
532 DCHECK(locations->CanCall());
533 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
534 DCHECK(instruction_->IsInstanceFieldGet() ||
535 instruction_->IsStaticFieldGet() ||
536 instruction_->IsArrayGet() ||
537 instruction_->IsArraySet() ||
538 instruction_->IsLoadClass() ||
539 instruction_->IsLoadString() ||
540 instruction_->IsInstanceOf() ||
541 instruction_->IsCheckCast() ||
542 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
543 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
544 << "Unexpected instruction in read barrier marking slow path: "
545 << instruction_->DebugName();
546
547 __ Bind(GetEntryLabel());
548 // No need to save live registers; it's taken care of by the
549 // entrypoint. Also, there is no need to update the stack mask,
550 // as this runtime call will not trigger a garbage collection.
551 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
552 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
553 (S2 <= ref_reg && ref_reg <= S7) ||
554 (ref_reg == FP)) << ref_reg;
555 // "Compact" slow path, saving two moves.
556 //
557 // Instead of using the standard runtime calling convention (input
558 // and output in A0 and V0 respectively):
559 //
560 // A0 <- ref
561 // V0 <- ReadBarrierMark(A0)
562 // ref <- V0
563 //
564 // we just use rX (the register containing `ref`) as input and output
565 // of a dedicated entrypoint:
566 //
567 // rX <- ReadBarrierMarkRegX(rX)
568 //
569 if (entrypoint_.IsValid()) {
570 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
571 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
572 __ Jalr(entrypoint_.AsRegister<Register>());
573 __ NopIfNoReordering();
574 } else {
575 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100576 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800577 // This runtime call does not require a stack map.
578 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
579 instruction_,
580 this,
581 /* direct */ false);
582 }
583 __ B(GetExitLabel());
584 }
585
586 private:
587 // The location (register) of the marked object reference.
588 const Location ref_;
589
590 // The location of the entrypoint if already loaded.
591 const Location entrypoint_;
592
593 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
594};
595
596// Slow path marking an object reference `ref` during a read barrier,
597// and if needed, atomically updating the field `obj.field` in the
598// object `obj` holding this reference after marking (contrary to
599// ReadBarrierMarkSlowPathMIPS above, which never tries to update
600// `obj.field`).
601//
602// This means that after the execution of this slow path, both `ref`
603// and `obj.field` will be up-to-date; i.e., after the flip, both will
604// hold the same to-space reference (unless another thread installed
605// another object reference (different from `ref`) in `obj.field`).
606class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
607 public:
608 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
609 Location ref,
610 Register obj,
611 Location field_offset,
612 Register temp1)
613 : SlowPathCodeMIPS(instruction),
614 ref_(ref),
615 obj_(obj),
616 field_offset_(field_offset),
617 temp1_(temp1) {
618 DCHECK(kEmitCompilerReadBarrier);
619 }
620
621 const char* GetDescription() const OVERRIDE {
622 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
623 }
624
625 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
626 LocationSummary* locations = instruction_->GetLocations();
627 Register ref_reg = ref_.AsRegister<Register>();
628 DCHECK(locations->CanCall());
629 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
630 // This slow path is only used by the UnsafeCASObject intrinsic.
631 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
632 << "Unexpected instruction in read barrier marking and field updating slow path: "
633 << instruction_->DebugName();
634 DCHECK(instruction_->GetLocations()->Intrinsified());
635 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
636 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
637
638 __ Bind(GetEntryLabel());
639
640 // Save the old reference.
641 // Note that we cannot use AT or TMP to save the old reference, as those
642 // are used by the code that follows, but we need the old reference after
643 // the call to the ReadBarrierMarkRegX entry point.
644 DCHECK_NE(temp1_, AT);
645 DCHECK_NE(temp1_, TMP);
646 __ Move(temp1_, ref_reg);
647
648 // No need to save live registers; it's taken care of by the
649 // entrypoint. Also, there is no need to update the stack mask,
650 // as this runtime call will not trigger a garbage collection.
651 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
652 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
653 (S2 <= ref_reg && ref_reg <= S7) ||
654 (ref_reg == FP)) << ref_reg;
655 // "Compact" slow path, saving two moves.
656 //
657 // Instead of using the standard runtime calling convention (input
658 // and output in A0 and V0 respectively):
659 //
660 // A0 <- ref
661 // V0 <- ReadBarrierMark(A0)
662 // ref <- V0
663 //
664 // we just use rX (the register containing `ref`) as input and output
665 // of a dedicated entrypoint:
666 //
667 // rX <- ReadBarrierMarkRegX(rX)
668 //
669 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100670 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800671 // This runtime call does not require a stack map.
672 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
673 instruction_,
674 this,
675 /* direct */ false);
676
677 // If the new reference is different from the old reference,
678 // update the field in the holder (`*(obj_ + field_offset_)`).
679 //
680 // Note that this field could also hold a different object, if
681 // another thread had concurrently changed it. In that case, the
682 // the compare-and-set (CAS) loop below would abort, leaving the
683 // field as-is.
684 MipsLabel done;
685 __ Beq(temp1_, ref_reg, &done);
686
687 // Update the the holder's field atomically. This may fail if
688 // mutator updates before us, but it's OK. This is achieved
689 // using a strong compare-and-set (CAS) operation with relaxed
690 // memory synchronization ordering, where the expected value is
691 // the old reference and the desired value is the new reference.
692
693 // Convenience aliases.
694 Register base = obj_;
695 // The UnsafeCASObject intrinsic uses a register pair as field
696 // offset ("long offset"), of which only the low part contains
697 // data.
698 Register offset = field_offset_.AsRegisterPairLow<Register>();
699 Register expected = temp1_;
700 Register value = ref_reg;
701 Register tmp_ptr = TMP; // Pointer to actual memory.
702 Register tmp = AT; // Value in memory.
703
704 __ Addu(tmp_ptr, base, offset);
705
706 if (kPoisonHeapReferences) {
707 __ PoisonHeapReference(expected);
708 // Do not poison `value` if it is the same register as
709 // `expected`, which has just been poisoned.
710 if (value != expected) {
711 __ PoisonHeapReference(value);
712 }
713 }
714
715 // do {
716 // tmp = [r_ptr] - expected;
717 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
718
719 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
720 MipsLabel loop_head, exit_loop;
721 __ Bind(&loop_head);
722 if (is_r6) {
723 __ LlR6(tmp, tmp_ptr);
724 } else {
725 __ LlR2(tmp, tmp_ptr);
726 }
727 __ Bne(tmp, expected, &exit_loop);
728 __ Move(tmp, value);
729 if (is_r6) {
730 __ ScR6(tmp, tmp_ptr);
731 } else {
732 __ ScR2(tmp, tmp_ptr);
733 }
734 __ Beqz(tmp, &loop_head);
735 __ Bind(&exit_loop);
736
737 if (kPoisonHeapReferences) {
738 __ UnpoisonHeapReference(expected);
739 // Do not unpoison `value` if it is the same register as
740 // `expected`, which has just been unpoisoned.
741 if (value != expected) {
742 __ UnpoisonHeapReference(value);
743 }
744 }
745
746 __ Bind(&done);
747 __ B(GetExitLabel());
748 }
749
750 private:
751 // The location (register) of the marked object reference.
752 const Location ref_;
753 // The register containing the object holding the marked object reference field.
754 const Register obj_;
755 // The location of the offset of the marked reference field within `obj_`.
756 Location field_offset_;
757
758 const Register temp1_;
759
760 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
761};
762
763// Slow path generating a read barrier for a heap reference.
764class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
765 public:
766 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
767 Location out,
768 Location ref,
769 Location obj,
770 uint32_t offset,
771 Location index)
772 : SlowPathCodeMIPS(instruction),
773 out_(out),
774 ref_(ref),
775 obj_(obj),
776 offset_(offset),
777 index_(index) {
778 DCHECK(kEmitCompilerReadBarrier);
779 // If `obj` is equal to `out` or `ref`, it means the initial object
780 // has been overwritten by (or after) the heap object reference load
781 // to be instrumented, e.g.:
782 //
783 // __ LoadFromOffset(kLoadWord, out, out, offset);
784 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
785 //
786 // In that case, we have lost the information about the original
787 // object, and the emitted read barrier cannot work properly.
788 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
789 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
790 }
791
792 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
793 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
794 LocationSummary* locations = instruction_->GetLocations();
795 Register reg_out = out_.AsRegister<Register>();
796 DCHECK(locations->CanCall());
797 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
798 DCHECK(instruction_->IsInstanceFieldGet() ||
799 instruction_->IsStaticFieldGet() ||
800 instruction_->IsArrayGet() ||
801 instruction_->IsInstanceOf() ||
802 instruction_->IsCheckCast() ||
803 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
804 << "Unexpected instruction in read barrier for heap reference slow path: "
805 << instruction_->DebugName();
806
807 __ Bind(GetEntryLabel());
808 SaveLiveRegisters(codegen, locations);
809
810 // We may have to change the index's value, but as `index_` is a
811 // constant member (like other "inputs" of this slow path),
812 // introduce a copy of it, `index`.
813 Location index = index_;
814 if (index_.IsValid()) {
815 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
816 if (instruction_->IsArrayGet()) {
817 // Compute the actual memory offset and store it in `index`.
818 Register index_reg = index_.AsRegister<Register>();
819 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
820 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
821 // We are about to change the value of `index_reg` (see the
822 // calls to art::mips::MipsAssembler::Sll and
823 // art::mips::MipsAssembler::Addiu32 below), but it has
824 // not been saved by the previous call to
825 // art::SlowPathCode::SaveLiveRegisters, as it is a
826 // callee-save register --
827 // art::SlowPathCode::SaveLiveRegisters does not consider
828 // callee-save registers, as it has been designed with the
829 // assumption that callee-save registers are supposed to be
830 // handled by the called function. So, as a callee-save
831 // register, `index_reg` _would_ eventually be saved onto
832 // the stack, but it would be too late: we would have
833 // changed its value earlier. Therefore, we manually save
834 // it here into another freely available register,
835 // `free_reg`, chosen of course among the caller-save
836 // registers (as a callee-save `free_reg` register would
837 // exhibit the same problem).
838 //
839 // Note we could have requested a temporary register from
840 // the register allocator instead; but we prefer not to, as
841 // this is a slow path, and we know we can find a
842 // caller-save register that is available.
843 Register free_reg = FindAvailableCallerSaveRegister(codegen);
844 __ Move(free_reg, index_reg);
845 index_reg = free_reg;
846 index = Location::RegisterLocation(index_reg);
847 } else {
848 // The initial register stored in `index_` has already been
849 // saved in the call to art::SlowPathCode::SaveLiveRegisters
850 // (as it is not a callee-save register), so we can freely
851 // use it.
852 }
853 // Shifting the index value contained in `index_reg` by the scale
854 // factor (2) cannot overflow in practice, as the runtime is
855 // unable to allocate object arrays with a size larger than
856 // 2^26 - 1 (that is, 2^28 - 4 bytes).
857 __ Sll(index_reg, index_reg, TIMES_4);
858 static_assert(
859 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
860 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
861 __ Addiu32(index_reg, index_reg, offset_);
862 } else {
863 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
864 // intrinsics, `index_` is not shifted by a scale factor of 2
865 // (as in the case of ArrayGet), as it is actually an offset
866 // to an object field within an object.
867 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
868 DCHECK(instruction_->GetLocations()->Intrinsified());
869 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
870 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
871 << instruction_->AsInvoke()->GetIntrinsic();
872 DCHECK_EQ(offset_, 0U);
873 DCHECK(index_.IsRegisterPair());
874 // UnsafeGet's offset location is a register pair, the low
875 // part contains the correct offset.
876 index = index_.ToLow();
877 }
878 }
879
880 // We're moving two or three locations to locations that could
881 // overlap, so we need a parallel move resolver.
882 InvokeRuntimeCallingConvention calling_convention;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100883 HParallelMove parallel_move(codegen->GetGraph()->GetAllocator());
Alexey Frunze15958152017-02-09 19:08:30 -0800884 parallel_move.AddMove(ref_,
885 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100886 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800887 nullptr);
888 parallel_move.AddMove(obj_,
889 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100890 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800891 nullptr);
892 if (index.IsValid()) {
893 parallel_move.AddMove(index,
894 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100895 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800896 nullptr);
897 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
898 } else {
899 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
900 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
901 }
902 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
903 instruction_,
904 instruction_->GetDexPc(),
905 this);
906 CheckEntrypointTypes<
907 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200908 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100909 calling_convention.GetReturnLocation(DataType::Type::kReference),
910 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800911
912 RestoreLiveRegisters(codegen, locations);
913 __ B(GetExitLabel());
914 }
915
916 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
917
918 private:
919 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
920 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
921 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
922 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
923 if (i != ref &&
924 i != obj &&
925 !codegen->IsCoreCalleeSaveRegister(i) &&
926 !codegen->IsBlockedCoreRegister(i)) {
927 return static_cast<Register>(i);
928 }
929 }
930 // We shall never fail to find a free caller-save register, as
931 // there are more than two core caller-save registers on MIPS
932 // (meaning it is possible to find one which is different from
933 // `ref` and `obj`).
934 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
935 LOG(FATAL) << "Could not find a free caller-save register";
936 UNREACHABLE();
937 }
938
939 const Location out_;
940 const Location ref_;
941 const Location obj_;
942 const uint32_t offset_;
943 // An additional location containing an index to an array.
944 // Only used for HArrayGet and the UnsafeGetObject &
945 // UnsafeGetObjectVolatile intrinsics.
946 const Location index_;
947
948 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
949};
950
951// Slow path generating a read barrier for a GC root.
952class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
953 public:
954 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
955 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
956 DCHECK(kEmitCompilerReadBarrier);
957 }
958
959 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
960 LocationSummary* locations = instruction_->GetLocations();
961 Register reg_out = out_.AsRegister<Register>();
962 DCHECK(locations->CanCall());
963 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
964 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
965 << "Unexpected instruction in read barrier for GC root slow path: "
966 << instruction_->DebugName();
967
968 __ Bind(GetEntryLabel());
969 SaveLiveRegisters(codegen, locations);
970
971 InvokeRuntimeCallingConvention calling_convention;
972 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +0200973 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
974 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100975 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800976 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
977 instruction_,
978 instruction_->GetDexPc(),
979 this);
980 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +0200981 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100982 calling_convention.GetReturnLocation(DataType::Type::kReference),
983 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800984
985 RestoreLiveRegisters(codegen, locations);
986 __ B(GetExitLabel());
987 }
988
989 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
990
991 private:
992 const Location out_;
993 const Location root_;
994
995 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
996};
997
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200998CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
999 const MipsInstructionSetFeatures& isa_features,
1000 const CompilerOptions& compiler_options,
1001 OptimizingCompilerStats* stats)
1002 : CodeGenerator(graph,
1003 kNumberOfCoreRegisters,
1004 kNumberOfFRegisters,
1005 kNumberOfRegisterPairs,
1006 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1007 arraysize(kCoreCalleeSaves)),
1008 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1009 arraysize(kFpuCalleeSaves)),
1010 compiler_options,
1011 stats),
1012 block_labels_(nullptr),
1013 location_builder_(graph, this),
1014 instruction_visitor_(graph, this),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001015 move_resolver_(graph->GetAllocator(), this),
1016 assembler_(graph->GetAllocator(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001017 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001018 uint32_literals_(std::less<uint32_t>(),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001019 graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001020 boot_image_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001021 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001022 boot_image_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001023 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001024 boot_image_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Markoca6fff82017-10-03 14:49:14 +01001025 string_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1026 jit_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1027 jit_class_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001028 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001029 // Save RA (containing the return address) to mimic Quick.
1030 AddAllocatedRegister(Location::RegisterLocation(RA));
1031}
1032
1033#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001034// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1035#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001036#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001037
1038void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1039 // Ensure that we fix up branches.
1040 __ FinalizeCode();
1041
1042 // Adjust native pc offsets in stack maps.
Vladimir Marko174b2e22017-10-12 13:34:49 +01001043 StackMapStream* stack_map_stream = GetStackMapStream();
1044 for (size_t i = 0, num = stack_map_stream->GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001045 uint32_t old_position =
Vladimir Marko33bff252017-11-01 14:35:42 +00001046 stack_map_stream->GetStackMap(i).native_pc_code_offset.Uint32Value(InstructionSet::kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001047 uint32_t new_position = __ GetAdjustedPosition(old_position);
1048 DCHECK_GE(new_position, old_position);
Vladimir Marko174b2e22017-10-12 13:34:49 +01001049 stack_map_stream->SetStackMapNativePcOffset(i, new_position);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001050 }
1051
1052 // Adjust pc offsets for the disassembly information.
1053 if (disasm_info_ != nullptr) {
1054 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1055 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1056 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1057 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1058 it.second.start = __ GetAdjustedPosition(it.second.start);
1059 it.second.end = __ GetAdjustedPosition(it.second.end);
1060 }
1061 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1062 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1063 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1064 }
1065 }
1066
1067 CodeGenerator::Finalize(allocator);
1068}
1069
1070MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1071 return codegen_->GetAssembler();
1072}
1073
1074void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1075 DCHECK_LT(index, moves_.size());
1076 MoveOperands* move = moves_[index];
1077 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1078}
1079
1080void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1081 DCHECK_LT(index, moves_.size());
1082 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001083 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001084 Location loc1 = move->GetDestination();
1085 Location loc2 = move->GetSource();
1086
1087 DCHECK(!loc1.IsConstant());
1088 DCHECK(!loc2.IsConstant());
1089
1090 if (loc1.Equals(loc2)) {
1091 return;
1092 }
1093
1094 if (loc1.IsRegister() && loc2.IsRegister()) {
1095 // Swap 2 GPRs.
1096 Register r1 = loc1.AsRegister<Register>();
1097 Register r2 = loc2.AsRegister<Register>();
1098 __ Move(TMP, r2);
1099 __ Move(r2, r1);
1100 __ Move(r1, TMP);
1101 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001102 if (codegen_->GetGraph()->HasSIMD()) {
1103 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(loc1));
1104 __ MoveV(VectorRegisterFrom(loc1), VectorRegisterFrom(loc2));
1105 __ MoveV(VectorRegisterFrom(loc2), static_cast<VectorRegister>(FTMP));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001106 } else {
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001107 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1108 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1109 if (type == DataType::Type::kFloat32) {
1110 __ MovS(FTMP, f2);
1111 __ MovS(f2, f1);
1112 __ MovS(f1, FTMP);
1113 } else {
1114 DCHECK_EQ(type, DataType::Type::kFloat64);
1115 __ MovD(FTMP, f2);
1116 __ MovD(f2, f1);
1117 __ MovD(f1, FTMP);
1118 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001119 }
1120 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1121 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1122 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001123 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001124 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1125 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001126 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001127 __ Move(TMP, r2);
1128 __ Mfc1(r2, f1);
1129 __ Mtc1(TMP, f1);
1130 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1131 // Swap 2 GPR register pairs.
1132 Register r1 = loc1.AsRegisterPairLow<Register>();
1133 Register r2 = loc2.AsRegisterPairLow<Register>();
1134 __ Move(TMP, r2);
1135 __ Move(r2, r1);
1136 __ Move(r1, TMP);
1137 r1 = loc1.AsRegisterPairHigh<Register>();
1138 r2 = loc2.AsRegisterPairHigh<Register>();
1139 __ Move(TMP, r2);
1140 __ Move(r2, r1);
1141 __ Move(r1, TMP);
1142 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1143 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1144 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001145 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001146 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1147 : loc2.AsFpuRegister<FRegister>();
1148 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1149 : loc2.AsRegisterPairLow<Register>();
1150 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1151 : loc2.AsRegisterPairHigh<Register>();
1152 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1153 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1154 // unpredictable and the following mfch1 will fail.
1155 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001156 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001157 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001158 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001159 __ Move(r2_l, TMP);
1160 __ Move(r2_h, AT);
1161 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1162 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1163 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1164 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001165 } else if (loc1.IsSIMDStackSlot() && loc2.IsSIMDStackSlot()) {
1166 ExchangeQuadSlots(loc1.GetStackIndex(), loc2.GetStackIndex());
David Brazdilcc0f3112016-01-28 17:14:52 +00001167 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1168 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001169 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1170 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001171 __ Move(TMP, reg);
1172 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1173 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1174 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1175 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1176 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1177 : loc2.AsRegisterPairLow<Register>();
1178 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1179 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001180 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001181 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1182 : loc2.GetHighStackIndex(kMipsWordSize);
1183 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001184 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001185 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001186 __ Move(TMP, reg_h);
1187 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1188 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001189 } else if ((loc1.IsFpuRegister() && loc2.IsSIMDStackSlot()) ||
1190 (loc1.IsSIMDStackSlot() && loc2.IsFpuRegister())) {
1191 Location fp_loc = loc1.IsFpuRegister() ? loc1 : loc2;
1192 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1193 __ MoveV(static_cast<VectorRegister>(FTMP), VectorRegisterFrom(fp_loc));
1194 __ LoadQFromOffset(fp_loc.AsFpuRegister<FRegister>(), SP, offset);
1195 __ StoreQToOffset(FTMP, SP, offset);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001196 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1197 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1198 : loc2.AsFpuRegister<FRegister>();
1199 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001200 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001201 __ MovS(FTMP, reg);
1202 __ LoadSFromOffset(reg, SP, offset);
1203 __ StoreSToOffset(FTMP, SP, offset);
1204 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001205 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001206 __ MovD(FTMP, reg);
1207 __ LoadDFromOffset(reg, SP, offset);
1208 __ StoreDToOffset(FTMP, SP, offset);
1209 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001210 } else {
1211 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1212 }
1213}
1214
1215void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1216 __ Pop(static_cast<Register>(reg));
1217}
1218
1219void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1220 __ Push(static_cast<Register>(reg));
1221}
1222
1223void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1224 // Allocate a scratch register other than TMP, if available.
1225 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1226 // automatically unspilled when the scratch scope object is destroyed).
1227 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1228 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
Chris Larsen715f43e2017-10-23 11:00:32 -07001229 int stack_offset = ensure_scratch.IsSpilled() ? kStackAlignment : 0;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001230 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1231 __ LoadFromOffset(kLoadWord,
1232 Register(ensure_scratch.GetRegister()),
1233 SP,
1234 index1 + stack_offset);
1235 __ LoadFromOffset(kLoadWord,
1236 TMP,
1237 SP,
1238 index2 + stack_offset);
1239 __ StoreToOffset(kStoreWord,
1240 Register(ensure_scratch.GetRegister()),
1241 SP,
1242 index2 + stack_offset);
1243 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1244 }
1245}
1246
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001247void ParallelMoveResolverMIPS::ExchangeQuadSlots(int index1, int index2) {
1248 __ LoadQFromOffset(FTMP, SP, index1);
1249 __ LoadQFromOffset(FTMP2, SP, index2);
1250 __ StoreQToOffset(FTMP, SP, index2);
1251 __ StoreQToOffset(FTMP2, SP, index1);
1252}
1253
Alexey Frunze73296a72016-06-03 22:51:46 -07001254void CodeGeneratorMIPS::ComputeSpillMask() {
1255 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1256 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1257 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1258 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1259 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1260 // within the stack frame.
1261 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1262 core_spill_mask_ |= (1 << ZERO);
1263 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001264}
1265
1266bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001267 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001268 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1269 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1270 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001271 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001272}
1273
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001274static dwarf::Reg DWARFReg(Register reg) {
1275 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1276}
1277
1278// TODO: mapping of floating-point registers to DWARF.
1279
1280void CodeGeneratorMIPS::GenerateFrameEntry() {
1281 __ Bind(&frame_entry_label_);
1282
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001283 if (GetCompilerOptions().CountHotnessInCompiledCode()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01001284 __ Lhu(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
1285 __ Addiu(TMP, TMP, 1);
1286 __ Sh(TMP, kMethodRegisterArgument, ArtMethod::HotnessCountOffset().Int32Value());
Nicolas Geoffray8d728322018-01-18 22:44:32 +00001287 }
1288
Vladimir Marko33bff252017-11-01 14:35:42 +00001289 bool do_overflow_check =
1290 FrameNeedsStackCheck(GetFrameSize(), InstructionSet::kMips) || !IsLeafMethod();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001291
1292 if (do_overflow_check) {
1293 __ LoadFromOffset(kLoadWord,
1294 ZERO,
1295 SP,
Vladimir Marko33bff252017-11-01 14:35:42 +00001296 -static_cast<int32_t>(GetStackOverflowReservedBytes(InstructionSet::kMips)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001297 RecordPcInfo(nullptr, 0);
1298 }
1299
1300 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001301 CHECK_EQ(fpu_spill_mask_, 0u);
1302 CHECK_EQ(core_spill_mask_, 1u << RA);
1303 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001304 return;
1305 }
1306
1307 // Make sure the frame size isn't unreasonably large.
Vladimir Marko33bff252017-11-01 14:35:42 +00001308 if (GetFrameSize() > GetStackOverflowReservedBytes(InstructionSet::kMips)) {
1309 LOG(FATAL) << "Stack frame larger than "
1310 << GetStackOverflowReservedBytes(InstructionSet::kMips) << " bytes";
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001311 }
1312
1313 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001314
Alexey Frunze73296a72016-06-03 22:51:46 -07001315 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001316 __ IncreaseFrameSize(ofs);
1317
Alexey Frunze73296a72016-06-03 22:51:46 -07001318 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1319 Register reg = static_cast<Register>(MostSignificantBit(mask));
1320 mask ^= 1u << reg;
1321 ofs -= kMipsWordSize;
1322 // The ZERO register is only included for alignment.
1323 if (reg != ZERO) {
1324 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001325 __ cfi().RelOffset(DWARFReg(reg), ofs);
1326 }
1327 }
1328
Alexey Frunze73296a72016-06-03 22:51:46 -07001329 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1330 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1331 mask ^= 1u << reg;
1332 ofs -= kMipsDoublewordSize;
1333 __ StoreDToOffset(reg, SP, ofs);
1334 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001335 }
1336
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001337 // Save the current method if we need it. Note that we do not
1338 // do this in HCurrentMethod, as the instruction might have been removed
1339 // in the SSA graph.
1340 if (RequiresCurrentMethod()) {
1341 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1342 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001343
1344 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1345 // Initialize should deoptimize flag to 0.
1346 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1347 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001348}
1349
1350void CodeGeneratorMIPS::GenerateFrameExit() {
1351 __ cfi().RememberState();
1352
1353 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001354 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355
Alexey Frunze73296a72016-06-03 22:51:46 -07001356 // For better instruction scheduling restore RA before other registers.
1357 uint32_t ofs = GetFrameSize();
1358 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1359 Register reg = static_cast<Register>(MostSignificantBit(mask));
1360 mask ^= 1u << reg;
1361 ofs -= kMipsWordSize;
1362 // The ZERO register is only included for alignment.
1363 if (reg != ZERO) {
1364 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001365 __ cfi().Restore(DWARFReg(reg));
1366 }
1367 }
1368
Alexey Frunze73296a72016-06-03 22:51:46 -07001369 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1370 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1371 mask ^= 1u << reg;
1372 ofs -= kMipsDoublewordSize;
1373 __ LoadDFromOffset(reg, SP, ofs);
1374 // TODO: __ cfi().Restore(DWARFReg(reg));
1375 }
1376
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001377 size_t frame_size = GetFrameSize();
1378 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1379 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1380 bool reordering = __ SetReorder(false);
1381 if (exchange) {
1382 __ Jr(RA);
1383 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1384 } else {
1385 __ DecreaseFrameSize(frame_size);
1386 __ Jr(RA);
1387 __ Nop(); // In delay slot.
1388 }
1389 __ SetReorder(reordering);
1390 } else {
1391 __ Jr(RA);
1392 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001393 }
1394
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001395 __ cfi().RestoreState();
1396 __ cfi().DefCFAOffset(GetFrameSize());
1397}
1398
1399void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1400 __ Bind(GetLabelOf(block));
1401}
1402
Lena Djokicca8c2952017-05-29 11:31:46 +02001403VectorRegister VectorRegisterFrom(Location location) {
1404 DCHECK(location.IsFpuRegister());
1405 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1406}
1407
Lena Djokic8098da92017-06-28 12:07:50 +02001408void CodeGeneratorMIPS::MoveLocation(Location destination,
1409 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001410 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001411 if (source.Equals(destination)) {
1412 return;
1413 }
1414
Lena Djokic8098da92017-06-28 12:07:50 +02001415 if (source.IsConstant()) {
1416 MoveConstant(destination, source.GetConstant());
1417 } else {
1418 if (destination.IsRegister()) {
1419 if (source.IsRegister()) {
1420 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1421 } else if (source.IsFpuRegister()) {
1422 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1423 } else {
1424 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001425 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001426 }
1427 } else if (destination.IsRegisterPair()) {
1428 if (source.IsRegisterPair()) {
1429 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1430 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1431 } else if (source.IsFpuRegister()) {
1432 Register dst_high = destination.AsRegisterPairHigh<Register>();
1433 Register dst_low = destination.AsRegisterPairLow<Register>();
1434 FRegister src = source.AsFpuRegister<FRegister>();
1435 __ Mfc1(dst_low, src);
1436 __ MoveFromFpuHigh(dst_high, src);
1437 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001438 DCHECK(source.IsDoubleStackSlot())
1439 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001440 int32_t off = source.GetStackIndex();
1441 Register r = destination.AsRegisterPairLow<Register>();
1442 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1443 }
1444 } else if (destination.IsFpuRegister()) {
1445 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001446 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001447 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1448 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001449 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001450 FRegister dst = destination.AsFpuRegister<FRegister>();
1451 Register src_high = source.AsRegisterPairHigh<Register>();
1452 Register src_low = source.AsRegisterPairLow<Register>();
1453 __ Mtc1(src_low, dst);
1454 __ MoveToFpuHigh(src_high, dst);
1455 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001456 if (GetGraph()->HasSIMD()) {
1457 __ MoveV(VectorRegisterFrom(destination),
1458 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001459 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001460 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001461 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1462 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001463 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001464 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1465 }
Lena Djokic8098da92017-06-28 12:07:50 +02001466 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001467 } else if (source.IsSIMDStackSlot()) {
1468 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001469 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001470 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001471 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1472 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001473 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001474 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1475 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1476 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001477 } else if (destination.IsSIMDStackSlot()) {
1478 if (source.IsFpuRegister()) {
1479 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1480 } else {
1481 DCHECK(source.IsSIMDStackSlot());
1482 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1483 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1484 }
Lena Djokic8098da92017-06-28 12:07:50 +02001485 } else if (destination.IsDoubleStackSlot()) {
1486 int32_t dst_offset = destination.GetStackIndex();
1487 if (source.IsRegisterPair()) {
1488 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1489 } else if (source.IsFpuRegister()) {
1490 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1491 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001492 DCHECK(source.IsDoubleStackSlot())
1493 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001494 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1495 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1496 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1497 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1498 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001499 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001500 DCHECK(destination.IsStackSlot()) << destination;
1501 int32_t dst_offset = destination.GetStackIndex();
1502 if (source.IsRegister()) {
1503 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1504 } else if (source.IsFpuRegister()) {
1505 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1506 } else {
1507 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1508 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1509 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1510 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001511 }
1512 }
1513}
1514
1515void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1516 if (c->IsIntConstant() || c->IsNullConstant()) {
1517 // Move 32 bit constant.
1518 int32_t value = GetInt32ValueOf(c);
1519 if (destination.IsRegister()) {
1520 Register dst = destination.AsRegister<Register>();
1521 __ LoadConst32(dst, value);
1522 } else {
1523 DCHECK(destination.IsStackSlot())
1524 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001525 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001526 }
1527 } else if (c->IsLongConstant()) {
1528 // Move 64 bit constant.
1529 int64_t value = GetInt64ValueOf(c);
1530 if (destination.IsRegisterPair()) {
1531 Register r_h = destination.AsRegisterPairHigh<Register>();
1532 Register r_l = destination.AsRegisterPairLow<Register>();
1533 __ LoadConst64(r_h, r_l, value);
1534 } else {
1535 DCHECK(destination.IsDoubleStackSlot())
1536 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001537 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001538 }
1539 } else if (c->IsFloatConstant()) {
1540 // Move 32 bit float constant.
1541 int32_t value = GetInt32ValueOf(c);
1542 if (destination.IsFpuRegister()) {
1543 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1544 } else {
1545 DCHECK(destination.IsStackSlot())
1546 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001547 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001548 }
1549 } else {
1550 // Move 64 bit double constant.
1551 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1552 int64_t value = GetInt64ValueOf(c);
1553 if (destination.IsFpuRegister()) {
1554 FRegister fd = destination.AsFpuRegister<FRegister>();
1555 __ LoadDConst64(fd, value, TMP);
1556 } else {
1557 DCHECK(destination.IsDoubleStackSlot())
1558 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001559 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001560 }
1561 }
1562}
1563
1564void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1565 DCHECK(destination.IsRegister());
1566 Register dst = destination.AsRegister<Register>();
1567 __ LoadConst32(dst, value);
1568}
1569
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001570void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1571 if (location.IsRegister()) {
1572 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001573 } else if (location.IsRegisterPair()) {
1574 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1575 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576 } else {
1577 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1578 }
1579}
1580
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001581template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001582inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1583 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001584 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001585 for (const PcRelativePatchInfo& info : infos) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001586 const DexFile* dex_file = info.target_dex_file;
Vladimir Markoaad75c62016-10-03 08:46:48 +00001587 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001588 DCHECK(info.label.IsBound());
1589 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001590 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1591 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001592 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1593 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1594 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001595 : __ GetPcRelBaseLabelLocation();
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001596 linker_patches->push_back(Factory(literal_offset, dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001597 }
1598}
1599
Vladimir Markob066d432018-01-03 13:14:37 +00001600linker::LinkerPatch DataBimgRelRoPatchAdapter(size_t literal_offset,
1601 const DexFile* target_dex_file,
1602 uint32_t pc_insn_offset,
1603 uint32_t boot_image_offset) {
1604 DCHECK(target_dex_file == nullptr); // Unused for DataBimgRelRoPatch(), should be null.
1605 return linker::LinkerPatch::DataBimgRelRoPatch(literal_offset, pc_insn_offset, boot_image_offset);
1606}
1607
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001608void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001609 DCHECK(linker_patches->empty());
1610 size_t size =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001611 boot_image_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001612 method_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001613 boot_image_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001614 type_bss_entry_patches_.size() +
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001615 boot_image_string_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001616 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001617 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001618 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001619 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001620 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001621 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001622 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001623 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001624 boot_image_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001625 } else {
Vladimir Markob066d432018-01-03 13:14:37 +00001626 EmitPcRelativeLinkerPatches<DataBimgRelRoPatchAdapter>(
1627 boot_image_method_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001628 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001629 boot_image_type_patches_, linker_patches);
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001630 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001631 boot_image_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001632 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001633 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1634 method_bss_entry_patches_, linker_patches);
1635 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1636 type_bss_entry_patches_, linker_patches);
1637 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1638 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001639 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001640}
1641
Vladimir Markob066d432018-01-03 13:14:37 +00001642CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageRelRoPatch(
1643 uint32_t boot_image_offset,
1644 const PcRelativePatchInfo* info_high) {
1645 return NewPcRelativePatch(
1646 /* dex_file */ nullptr, boot_image_offset, info_high, &boot_image_method_patches_);
1647}
1648
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001649CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001650 MethodReference target_method,
1651 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001652 return NewPcRelativePatch(
1653 target_method.dex_file, target_method.index, info_high, &boot_image_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001654}
1655
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001656CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001657 MethodReference target_method,
1658 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001659 return NewPcRelativePatch(
1660 target_method.dex_file, target_method.index, info_high, &method_bss_entry_patches_);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001661}
1662
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001663CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001664 const DexFile& dex_file,
1665 dex::TypeIndex type_index,
1666 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001667 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &boot_image_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001668}
1669
Vladimir Marko1998cd02017-01-13 13:02:58 +00001670CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001671 const DexFile& dex_file,
1672 dex::TypeIndex type_index,
1673 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001674 return NewPcRelativePatch(&dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001675}
1676
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001677CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewBootImageStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001678 const DexFile& dex_file,
1679 dex::StringIndex string_index,
1680 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001681 return NewPcRelativePatch(
1682 &dex_file, string_index.index_, info_high, &boot_image_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001683}
1684
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001685CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1686 const DexFile& dex_file,
1687 dex::StringIndex string_index,
1688 const PcRelativePatchInfo* info_high) {
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001689 return NewPcRelativePatch(&dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001690}
1691
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001692CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Vladimir Marko59eb30f2018-02-20 11:52:34 +00001693 const DexFile* dex_file,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001694 uint32_t offset_or_index,
1695 const PcRelativePatchInfo* info_high,
1696 ArenaDeque<PcRelativePatchInfo>* patches) {
1697 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001698 return &patches->back();
1699}
1700
Alexey Frunze06a46c42016-07-19 15:00:40 -07001701Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1702 return map->GetOrCreate(
1703 value,
1704 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1705}
1706
Alexey Frunze06a46c42016-07-19 15:00:40 -07001707Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001708 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001709}
1710
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001711void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001712 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001713 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001714 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001715 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001716 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001717 if (GetInstructionSetFeatures().IsR6()) {
1718 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001719 __ Bind(&info_high->label);
1720 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001721 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001722 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001723 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001724 } else {
1725 // If base is ZERO, emit NAL to obtain the actual base.
1726 if (base == ZERO) {
1727 // Generate a dummy PC-relative call to obtain PC.
1728 __ Nal();
1729 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001730 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001731 __ Lui(out, /* placeholder */ 0x1234);
1732 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1733 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1734 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001735 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001736 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001737 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001738 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001739 __ Addu(out, out, (base == ZERO) ? RA : base);
1740 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001741 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001742 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001743}
1744
Alexey Frunze627c1a02017-01-30 19:28:14 -08001745CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1746 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001747 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001748 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001749 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1750 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001751 return &jit_string_patches_.back();
1752}
1753
1754CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1755 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001756 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001757 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001758 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1759 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001760 return &jit_class_patches_.back();
1761}
1762
1763void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1764 const uint8_t* roots_data,
1765 const CodeGeneratorMIPS::JitPatchInfo& info,
1766 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001767 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1768 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001769 uintptr_t address =
1770 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1771 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1772 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001773 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1774 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1775 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1776 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001777 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001778 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1779 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001780 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001781 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001782 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1783 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001784 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001785 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1786 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001787}
1788
1789void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1790 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001791 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1792 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001793 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001794 }
1795 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001796 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1797 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001798 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001799 }
1800}
1801
Goran Jakovljevice114da22016-12-26 14:21:43 +01001802void CodeGeneratorMIPS::MarkGCCard(Register object,
1803 Register value,
1804 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001805 MipsLabel done;
1806 Register card = AT;
1807 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001808 if (value_can_be_null) {
1809 __ Beqz(value, &done);
1810 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001811 __ LoadFromOffset(kLoadWord,
1812 card,
1813 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001814 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001815 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1816 __ Addu(temp, card, temp);
1817 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001818 if (value_can_be_null) {
1819 __ Bind(&done);
1820 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001821}
1822
David Brazdil58282f42016-01-14 12:45:10 +00001823void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001824 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1825 blocked_core_registers_[ZERO] = true;
1826 blocked_core_registers_[K0] = true;
1827 blocked_core_registers_[K1] = true;
1828 blocked_core_registers_[GP] = true;
1829 blocked_core_registers_[SP] = true;
1830 blocked_core_registers_[RA] = true;
1831
1832 // AT and TMP(T8) are used as temporary/scratch registers
1833 // (similar to how AT is used by MIPS assemblers).
1834 blocked_core_registers_[AT] = true;
1835 blocked_core_registers_[TMP] = true;
1836 blocked_fpu_registers_[FTMP] = true;
1837
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001838 if (GetInstructionSetFeatures().HasMsa()) {
1839 // To be used just for MSA instructions.
1840 blocked_fpu_registers_[FTMP2] = true;
1841 }
1842
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001843 // Reserve suspend and thread registers.
1844 blocked_core_registers_[S0] = true;
1845 blocked_core_registers_[TR] = true;
1846
1847 // Reserve T9 for function calls
1848 blocked_core_registers_[T9] = true;
1849
1850 // Reserve odd-numbered FPU registers.
1851 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1852 blocked_fpu_registers_[i] = true;
1853 }
1854
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001855 if (GetGraph()->IsDebuggable()) {
1856 // Stubs do not save callee-save floating point registers. If the graph
1857 // is debuggable, we need to deal with these registers differently. For
1858 // now, just block them.
1859 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1860 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1861 }
1862 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863}
1864
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001865size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1866 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1867 return kMipsWordSize;
1868}
1869
1870size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1871 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1872 return kMipsWordSize;
1873}
1874
1875size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001876 if (GetGraph()->HasSIMD()) {
1877 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1878 } else {
1879 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1880 }
1881 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001882}
1883
1884size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001885 if (GetGraph()->HasSIMD()) {
1886 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1887 } else {
1888 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1889 }
1890 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001891}
1892
1893void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001894 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001895}
1896
1897void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001898 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001899}
1900
Serban Constantinescufca16662016-07-14 09:21:59 +01001901constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1902
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001903void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1904 HInstruction* instruction,
1905 uint32_t dex_pc,
1906 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001907 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001908 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1909 IsDirectEntrypoint(entrypoint));
1910 if (EntrypointRequiresStackMap(entrypoint)) {
1911 RecordPcInfo(instruction, dex_pc, slow_path);
1912 }
1913}
1914
1915void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1916 HInstruction* instruction,
1917 SlowPathCode* slow_path,
1918 bool direct) {
1919 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1920 GenerateInvokeRuntime(entry_point_offset, direct);
1921}
1922
1923void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001924 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001925 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001926 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001927 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001928 // Reserve argument space on stack (for $a0-$a3) for
1929 // entrypoints that directly reference native implementations.
1930 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001931 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001932 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001933 } else {
1934 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001935 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001936 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937}
1938
1939void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1940 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001941 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1942 const size_t status_byte_offset =
1943 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1944 constexpr uint32_t shifted_initialized_value =
1945 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1946
1947 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
Lena Djokic3177e102018-02-28 11:32:40 +01001948 __ Sltiu(TMP, TMP, shifted_initialized_value);
1949 __ Bnez(TMP, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001950 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1951 __ Sync(0);
1952 __ Bind(slow_path->GetExitLabel());
1953}
1954
1955void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1956 __ Sync(0); // Only stype 0 is supported.
1957}
1958
1959void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1960 HBasicBlock* successor) {
1961 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001962 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1963
1964 if (slow_path == nullptr) {
1965 slow_path =
1966 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1967 instruction->SetSlowPath(slow_path);
1968 codegen_->AddSlowPath(slow_path);
1969 if (successor != nullptr) {
1970 DCHECK(successor->IsLoopHeader());
1971 }
1972 } else {
1973 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1974 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001975
1976 __ LoadFromOffset(kLoadUnsignedHalfword,
1977 TMP,
1978 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001979 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001980 if (successor == nullptr) {
1981 __ Bnez(TMP, slow_path->GetEntryLabel());
1982 __ Bind(slow_path->GetReturnLabel());
1983 } else {
1984 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1985 __ B(slow_path->GetEntryLabel());
1986 // slow_path will return to GetLabelOf(successor).
1987 }
1988}
1989
1990InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1991 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001992 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001993 assembler_(codegen->GetAssembler()),
1994 codegen_(codegen) {}
1995
1996void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1997 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001998 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001999 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01002000 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002001 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002002 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002003 locations->SetInAt(0, Location::RequiresRegister());
2004 HInstruction* right = instruction->InputAt(1);
2005 bool can_use_imm = false;
2006 if (right->IsConstant()) {
2007 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2008 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2009 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002010 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002011 DCHECK(instruction->IsSub() || instruction->IsAdd());
2012 if (instruction->IsSub()) {
2013 imm = -imm;
2014 }
2015 if (isR6) {
2016 bool single_use = right->GetUses().HasExactlyOneElement();
2017 int16_t imm_high = High16Bits(imm);
2018 int16_t imm_low = Low16Bits(imm);
2019 if (imm_low < 0) {
2020 imm_high += 1;
2021 }
2022 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2023 } else {
2024 can_use_imm = IsInt<16>(imm);
2025 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 }
2027 }
2028 if (can_use_imm)
2029 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2030 else
2031 locations->SetInAt(1, Location::RequiresRegister());
2032 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2033 break;
2034 }
2035
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002036 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002037 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002038 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2039 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002040 break;
2041 }
2042
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002043 case DataType::Type::kFloat32:
2044 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002045 DCHECK(instruction->IsAdd() || instruction->IsSub());
2046 locations->SetInAt(0, Location::RequiresFpuRegister());
2047 locations->SetInAt(1, Location::RequiresFpuRegister());
2048 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2049 break;
2050
2051 default:
2052 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2053 }
2054}
2055
2056void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002057 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002058 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002059 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002060
2061 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002062 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002063 Register dst = locations->Out().AsRegister<Register>();
2064 Register lhs = locations->InAt(0).AsRegister<Register>();
2065 Location rhs_location = locations->InAt(1);
2066
2067 Register rhs_reg = ZERO;
2068 int32_t rhs_imm = 0;
2069 bool use_imm = rhs_location.IsConstant();
2070 if (use_imm) {
2071 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2072 } else {
2073 rhs_reg = rhs_location.AsRegister<Register>();
2074 }
2075
2076 if (instruction->IsAnd()) {
2077 if (use_imm)
2078 __ Andi(dst, lhs, rhs_imm);
2079 else
2080 __ And(dst, lhs, rhs_reg);
2081 } else if (instruction->IsOr()) {
2082 if (use_imm)
2083 __ Ori(dst, lhs, rhs_imm);
2084 else
2085 __ Or(dst, lhs, rhs_reg);
2086 } else if (instruction->IsXor()) {
2087 if (use_imm)
2088 __ Xori(dst, lhs, rhs_imm);
2089 else
2090 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002091 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002092 DCHECK(instruction->IsAdd() || instruction->IsSub());
2093 if (use_imm) {
2094 if (instruction->IsSub()) {
2095 rhs_imm = -rhs_imm;
2096 }
2097 if (IsInt<16>(rhs_imm)) {
2098 __ Addiu(dst, lhs, rhs_imm);
2099 } else {
2100 DCHECK(isR6);
2101 int16_t rhs_imm_high = High16Bits(rhs_imm);
2102 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2103 if (rhs_imm_low < 0) {
2104 rhs_imm_high += 1;
2105 }
2106 __ Aui(dst, lhs, rhs_imm_high);
2107 if (rhs_imm_low != 0) {
2108 __ Addiu(dst, dst, rhs_imm_low);
2109 }
2110 }
2111 } else if (instruction->IsAdd()) {
2112 __ Addu(dst, lhs, rhs_reg);
2113 } else {
2114 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002115 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002116 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002117 }
2118 break;
2119 }
2120
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002121 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002122 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2123 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2124 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2125 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002126 Location rhs_location = locations->InAt(1);
2127 bool use_imm = rhs_location.IsConstant();
2128 if (!use_imm) {
2129 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2130 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2131 if (instruction->IsAnd()) {
2132 __ And(dst_low, lhs_low, rhs_low);
2133 __ And(dst_high, lhs_high, rhs_high);
2134 } else if (instruction->IsOr()) {
2135 __ Or(dst_low, lhs_low, rhs_low);
2136 __ Or(dst_high, lhs_high, rhs_high);
2137 } else if (instruction->IsXor()) {
2138 __ Xor(dst_low, lhs_low, rhs_low);
2139 __ Xor(dst_high, lhs_high, rhs_high);
2140 } else if (instruction->IsAdd()) {
2141 if (lhs_low == rhs_low) {
2142 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2143 __ Slt(TMP, lhs_low, ZERO);
2144 __ Addu(dst_low, lhs_low, rhs_low);
2145 } else {
2146 __ Addu(dst_low, lhs_low, rhs_low);
2147 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2148 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2149 }
2150 __ Addu(dst_high, lhs_high, rhs_high);
2151 __ Addu(dst_high, dst_high, TMP);
2152 } else {
2153 DCHECK(instruction->IsSub());
2154 __ Sltu(TMP, lhs_low, rhs_low);
2155 __ Subu(dst_low, lhs_low, rhs_low);
2156 __ Subu(dst_high, lhs_high, rhs_high);
2157 __ Subu(dst_high, dst_high, TMP);
2158 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002159 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002160 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2161 if (instruction->IsOr()) {
2162 uint32_t low = Low32Bits(value);
2163 uint32_t high = High32Bits(value);
2164 if (IsUint<16>(low)) {
2165 if (dst_low != lhs_low || low != 0) {
2166 __ Ori(dst_low, lhs_low, low);
2167 }
2168 } else {
2169 __ LoadConst32(TMP, low);
2170 __ Or(dst_low, lhs_low, TMP);
2171 }
2172 if (IsUint<16>(high)) {
2173 if (dst_high != lhs_high || high != 0) {
2174 __ Ori(dst_high, lhs_high, high);
2175 }
2176 } else {
2177 if (high != low) {
2178 __ LoadConst32(TMP, high);
2179 }
2180 __ Or(dst_high, lhs_high, TMP);
2181 }
2182 } else if (instruction->IsXor()) {
2183 uint32_t low = Low32Bits(value);
2184 uint32_t high = High32Bits(value);
2185 if (IsUint<16>(low)) {
2186 if (dst_low != lhs_low || low != 0) {
2187 __ Xori(dst_low, lhs_low, low);
2188 }
2189 } else {
2190 __ LoadConst32(TMP, low);
2191 __ Xor(dst_low, lhs_low, TMP);
2192 }
2193 if (IsUint<16>(high)) {
2194 if (dst_high != lhs_high || high != 0) {
2195 __ Xori(dst_high, lhs_high, high);
2196 }
2197 } else {
2198 if (high != low) {
2199 __ LoadConst32(TMP, high);
2200 }
2201 __ Xor(dst_high, lhs_high, TMP);
2202 }
2203 } else if (instruction->IsAnd()) {
2204 uint32_t low = Low32Bits(value);
2205 uint32_t high = High32Bits(value);
2206 if (IsUint<16>(low)) {
2207 __ Andi(dst_low, lhs_low, low);
2208 } else if (low != 0xFFFFFFFF) {
2209 __ LoadConst32(TMP, low);
2210 __ And(dst_low, lhs_low, TMP);
2211 } else if (dst_low != lhs_low) {
2212 __ Move(dst_low, lhs_low);
2213 }
2214 if (IsUint<16>(high)) {
2215 __ Andi(dst_high, lhs_high, high);
2216 } else if (high != 0xFFFFFFFF) {
2217 if (high != low) {
2218 __ LoadConst32(TMP, high);
2219 }
2220 __ And(dst_high, lhs_high, TMP);
2221 } else if (dst_high != lhs_high) {
2222 __ Move(dst_high, lhs_high);
2223 }
2224 } else {
2225 if (instruction->IsSub()) {
2226 value = -value;
2227 } else {
2228 DCHECK(instruction->IsAdd());
2229 }
2230 int32_t low = Low32Bits(value);
2231 int32_t high = High32Bits(value);
2232 if (IsInt<16>(low)) {
2233 if (dst_low != lhs_low || low != 0) {
2234 __ Addiu(dst_low, lhs_low, low);
2235 }
2236 if (low != 0) {
2237 __ Sltiu(AT, dst_low, low);
2238 }
2239 } else {
2240 __ LoadConst32(TMP, low);
2241 __ Addu(dst_low, lhs_low, TMP);
2242 __ Sltu(AT, dst_low, TMP);
2243 }
2244 if (IsInt<16>(high)) {
2245 if (dst_high != lhs_high || high != 0) {
2246 __ Addiu(dst_high, lhs_high, high);
2247 }
2248 } else {
2249 if (high != low) {
2250 __ LoadConst32(TMP, high);
2251 }
2252 __ Addu(dst_high, lhs_high, TMP);
2253 }
2254 if (low != 0) {
2255 __ Addu(dst_high, dst_high, AT);
2256 }
2257 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002258 }
2259 break;
2260 }
2261
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 case DataType::Type::kFloat32:
2263 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002264 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2265 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2266 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2267 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002268 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002269 __ AddS(dst, lhs, rhs);
2270 } else {
2271 __ AddD(dst, lhs, rhs);
2272 }
2273 } else {
2274 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002275 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002276 __ SubS(dst, lhs, rhs);
2277 } else {
2278 __ SubD(dst, lhs, rhs);
2279 }
2280 }
2281 break;
2282 }
2283
2284 default:
2285 LOG(FATAL) << "Unexpected binary operation type " << type;
2286 }
2287}
2288
2289void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002290 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002291
Vladimir Markoca6fff82017-10-03 14:49:14 +01002292 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002294 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002295 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002296 locations->SetInAt(0, Location::RequiresRegister());
2297 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2298 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2299 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002300 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 locations->SetInAt(0, Location::RequiresRegister());
2302 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2303 locations->SetOut(Location::RequiresRegister());
2304 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002305 default:
2306 LOG(FATAL) << "Unexpected shift type " << type;
2307 }
2308}
2309
2310static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2311
2312void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002313 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002314 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002316
2317 Location rhs_location = locations->InAt(1);
2318 bool use_imm = rhs_location.IsConstant();
2319 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2320 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002321 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002322 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002323 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002324 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2325 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002326
2327 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002328 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002329 Register dst = locations->Out().AsRegister<Register>();
2330 Register lhs = locations->InAt(0).AsRegister<Register>();
2331 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002332 if (shift_value == 0) {
2333 if (dst != lhs) {
2334 __ Move(dst, lhs);
2335 }
2336 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337 __ Sll(dst, lhs, shift_value);
2338 } else if (instr->IsShr()) {
2339 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002340 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002341 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002342 } else {
2343 if (has_ins_rotr) {
2344 __ Rotr(dst, lhs, shift_value);
2345 } else {
2346 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2347 __ Srl(dst, lhs, shift_value);
2348 __ Or(dst, dst, TMP);
2349 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002350 }
2351 } else {
2352 if (instr->IsShl()) {
2353 __ Sllv(dst, lhs, rhs_reg);
2354 } else if (instr->IsShr()) {
2355 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002356 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002357 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002358 } else {
2359 if (has_ins_rotr) {
2360 __ Rotrv(dst, lhs, rhs_reg);
2361 } else {
2362 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002363 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2364 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2365 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2366 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2367 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002368 __ Sllv(TMP, lhs, TMP);
2369 __ Srlv(dst, lhs, rhs_reg);
2370 __ Or(dst, dst, TMP);
2371 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002372 }
2373 }
2374 break;
2375 }
2376
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002377 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2379 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2380 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2381 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2382 if (use_imm) {
2383 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002384 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002385 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002386 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002387 if (instr->IsShl()) {
2388 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2389 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2390 __ Sll(dst_low, lhs_low, shift_value);
2391 } else if (instr->IsShr()) {
2392 __ Srl(dst_low, lhs_low, shift_value);
2393 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2394 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002395 } else if (instr->IsUShr()) {
2396 __ Srl(dst_low, lhs_low, shift_value);
2397 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2398 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002399 } else {
2400 __ Srl(dst_low, lhs_low, shift_value);
2401 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2402 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002403 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002404 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002405 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002406 if (instr->IsShl()) {
2407 __ Sll(dst_low, lhs_low, shift_value);
2408 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2409 __ Sll(dst_high, lhs_high, shift_value);
2410 __ Or(dst_high, dst_high, TMP);
2411 } else if (instr->IsShr()) {
2412 __ Sra(dst_high, lhs_high, shift_value);
2413 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2414 __ Srl(dst_low, lhs_low, shift_value);
2415 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002416 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002417 __ Srl(dst_high, lhs_high, shift_value);
2418 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2419 __ Srl(dst_low, lhs_low, shift_value);
2420 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002421 } else {
2422 __ Srl(TMP, lhs_low, shift_value);
2423 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2424 __ Or(dst_low, dst_low, TMP);
2425 __ Srl(TMP, lhs_high, shift_value);
2426 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2427 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002428 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429 }
2430 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002431 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002433 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434 __ Move(dst_low, ZERO);
2435 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002436 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002437 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002438 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002439 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002440 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002441 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002442 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002443 // 64-bit rotation by 32 is just a swap.
2444 __ Move(dst_low, lhs_high);
2445 __ Move(dst_high, lhs_low);
2446 } else {
2447 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002448 __ Srl(dst_low, lhs_high, shift_value_high);
2449 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2450 __ Srl(dst_high, lhs_low, shift_value_high);
2451 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002452 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002453 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2454 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002455 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002456 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2457 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002458 __ Or(dst_high, dst_high, TMP);
2459 }
2460 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002461 }
2462 }
2463 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002464 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002465 MipsLabel done;
2466 if (instr->IsShl()) {
2467 __ Sllv(dst_low, lhs_low, rhs_reg);
2468 __ Nor(AT, ZERO, rhs_reg);
2469 __ Srl(TMP, lhs_low, 1);
2470 __ Srlv(TMP, TMP, AT);
2471 __ Sllv(dst_high, lhs_high, rhs_reg);
2472 __ Or(dst_high, dst_high, 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_high, dst_low);
2477 __ Move(dst_low, ZERO);
2478 } else {
2479 __ Movn(dst_high, dst_low, TMP);
2480 __ Movn(dst_low, ZERO, TMP);
2481 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002482 } else if (instr->IsShr()) {
2483 __ Srav(dst_high, lhs_high, rhs_reg);
2484 __ Nor(AT, ZERO, rhs_reg);
2485 __ Sll(TMP, lhs_high, 1);
2486 __ Sllv(TMP, TMP, AT);
2487 __ Srlv(dst_low, lhs_low, rhs_reg);
2488 __ Or(dst_low, dst_low, TMP);
2489 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002490 if (isR6) {
2491 __ Beqzc(TMP, &done, /* is_bare */ true);
2492 __ Move(dst_low, dst_high);
2493 __ Sra(dst_high, dst_high, 31);
2494 } else {
2495 __ Sra(AT, dst_high, 31);
2496 __ Movn(dst_low, dst_high, TMP);
2497 __ Movn(dst_high, AT, TMP);
2498 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002499 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002500 __ Srlv(dst_high, lhs_high, rhs_reg);
2501 __ Nor(AT, ZERO, rhs_reg);
2502 __ Sll(TMP, lhs_high, 1);
2503 __ Sllv(TMP, TMP, AT);
2504 __ Srlv(dst_low, lhs_low, rhs_reg);
2505 __ Or(dst_low, dst_low, TMP);
2506 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002507 if (isR6) {
2508 __ Beqzc(TMP, &done, /* is_bare */ true);
2509 __ Move(dst_low, dst_high);
2510 __ Move(dst_high, ZERO);
2511 } else {
2512 __ Movn(dst_low, dst_high, TMP);
2513 __ Movn(dst_high, ZERO, TMP);
2514 }
2515 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002516 __ Nor(AT, ZERO, rhs_reg);
2517 __ Srlv(TMP, lhs_low, rhs_reg);
2518 __ Sll(dst_low, lhs_high, 1);
2519 __ Sllv(dst_low, dst_low, AT);
2520 __ Or(dst_low, dst_low, TMP);
2521 __ Srlv(TMP, lhs_high, rhs_reg);
2522 __ Sll(dst_high, lhs_low, 1);
2523 __ Sllv(dst_high, dst_high, AT);
2524 __ Or(dst_high, dst_high, TMP);
2525 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002526 if (isR6) {
2527 __ Beqzc(TMP, &done, /* is_bare */ true);
2528 __ Move(TMP, dst_high);
2529 __ Move(dst_high, dst_low);
2530 __ Move(dst_low, TMP);
2531 } else {
2532 __ Movn(AT, dst_high, TMP);
2533 __ Movn(dst_high, dst_low, TMP);
2534 __ Movn(dst_low, AT, TMP);
2535 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002536 }
2537 __ Bind(&done);
2538 }
2539 break;
2540 }
2541
2542 default:
2543 LOG(FATAL) << "Unexpected shift operation type " << type;
2544 }
2545}
2546
2547void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2548 HandleBinaryOp(instruction);
2549}
2550
2551void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2552 HandleBinaryOp(instruction);
2553}
2554
2555void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2556 HandleBinaryOp(instruction);
2557}
2558
2559void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2560 HandleBinaryOp(instruction);
2561}
2562
2563void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002565 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002566 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002567 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002568 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2569 object_array_get_with_read_barrier
2570 ? LocationSummary::kCallOnSlowPath
2571 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002572 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2573 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2574 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002575 locations->SetInAt(0, Location::RequiresRegister());
2576 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002577 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002578 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2579 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002580 // The output overlaps in the case of an object array get with
2581 // read barriers enabled: we do not want the move to overwrite the
2582 // array's location, as we need it to emit the read barrier.
2583 locations->SetOut(Location::RequiresRegister(),
2584 object_array_get_with_read_barrier
2585 ? Location::kOutputOverlap
2586 : Location::kNoOutputOverlap);
2587 }
2588 // We need a temporary register for the read barrier marking slow
2589 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2590 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002591 bool temp_needed = instruction->GetIndex()->IsConstant()
2592 ? !kBakerReadBarrierThunksEnableForFields
2593 : !kBakerReadBarrierThunksEnableForArrays;
2594 if (temp_needed) {
2595 locations->AddTemp(Location::RequiresRegister());
2596 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002597 }
2598}
2599
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002600static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2601 auto null_checker = [codegen, instruction]() {
2602 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002603 };
2604 return null_checker;
2605}
2606
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002607void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2608 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002609 Location obj_loc = locations->InAt(0);
2610 Register obj = obj_loc.AsRegister<Register>();
2611 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002612 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002613 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002614 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002615
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002616 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002617 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2618 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002619 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002620 case DataType::Type::kBool:
2621 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002622 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002623 if (index.IsConstant()) {
2624 size_t offset =
2625 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002626 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002627 } else {
2628 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002629 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002630 }
2631 break;
2632 }
2633
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002634 case DataType::Type::kInt8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002635 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002636 if (index.IsConstant()) {
2637 size_t offset =
2638 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002639 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002640 } else {
2641 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002642 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002643 }
2644 break;
2645 }
2646
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002647 case DataType::Type::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002648 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002649 if (maybe_compressed_char_at) {
2650 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2651 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2652 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2653 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2654 "Expecting 0=compressed, 1=uncompressed");
2655 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002656 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002657 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2658 if (maybe_compressed_char_at) {
2659 MipsLabel uncompressed_load, done;
2660 __ Bnez(TMP, &uncompressed_load);
2661 __ LoadFromOffset(kLoadUnsignedByte,
2662 out,
2663 obj,
2664 data_offset + (const_index << TIMES_1));
2665 __ B(&done);
2666 __ Bind(&uncompressed_load);
2667 __ LoadFromOffset(kLoadUnsignedHalfword,
2668 out,
2669 obj,
2670 data_offset + (const_index << TIMES_2));
2671 __ Bind(&done);
2672 } else {
2673 __ LoadFromOffset(kLoadUnsignedHalfword,
2674 out,
2675 obj,
2676 data_offset + (const_index << TIMES_2),
2677 null_checker);
2678 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002679 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002680 Register index_reg = index.AsRegister<Register>();
2681 if (maybe_compressed_char_at) {
2682 MipsLabel uncompressed_load, done;
2683 __ Bnez(TMP, &uncompressed_load);
2684 __ Addu(TMP, obj, index_reg);
2685 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2686 __ B(&done);
2687 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002688 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002689 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2690 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002691 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2692 __ Addu(TMP, index_reg, obj);
2693 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002694 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002695 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002696 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2697 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002698 }
2699 break;
2700 }
2701
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002702 case DataType::Type::kInt16: {
2703 Register out = out_loc.AsRegister<Register>();
2704 if (index.IsConstant()) {
2705 size_t offset =
2706 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2707 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002708 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2709 __ Addu(TMP, index.AsRegister<Register>(), obj);
2710 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002711 } else {
2712 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2713 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2714 }
2715 break;
2716 }
2717
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002718 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002719 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002720 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002721 if (index.IsConstant()) {
2722 size_t offset =
2723 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002724 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002725 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2726 __ Addu(TMP, index.AsRegister<Register>(), obj);
2727 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002728 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002729 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002730 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002731 }
2732 break;
2733 }
2734
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002735 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002736 static_assert(
2737 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2738 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2739 // /* HeapReference<Object> */ out =
2740 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2741 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002742 bool temp_needed = index.IsConstant()
2743 ? !kBakerReadBarrierThunksEnableForFields
2744 : !kBakerReadBarrierThunksEnableForArrays;
2745 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002746 // Note that a potential implicit null check is handled in this
2747 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002748 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2749 if (index.IsConstant()) {
2750 // Array load with a constant index can be treated as a field load.
2751 size_t offset =
2752 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2753 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2754 out_loc,
2755 obj,
2756 offset,
2757 temp,
2758 /* needs_null_check */ false);
2759 } else {
2760 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2761 out_loc,
2762 obj,
2763 data_offset,
2764 index,
2765 temp,
2766 /* needs_null_check */ false);
2767 }
Alexey Frunze15958152017-02-09 19:08:30 -08002768 } else {
2769 Register out = out_loc.AsRegister<Register>();
2770 if (index.IsConstant()) {
2771 size_t offset =
2772 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2773 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2774 // If read barriers are enabled, emit read barriers other than
2775 // Baker's using a slow path (and also unpoison the loaded
2776 // reference, if heap poisoning is enabled).
2777 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2778 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002779 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002780 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2781 // If read barriers are enabled, emit read barriers other than
2782 // Baker's using a slow path (and also unpoison the loaded
2783 // reference, if heap poisoning is enabled).
2784 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2785 out_loc,
2786 out_loc,
2787 obj_loc,
2788 data_offset,
2789 index);
2790 }
2791 }
2792 break;
2793 }
2794
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002795 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002796 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002797 if (index.IsConstant()) {
2798 size_t offset =
2799 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002800 __ LoadFromOffset(kLoadDoubleword, 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 __ LoadFromOffset(kLoadDoubleword, 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_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002806 __ LoadFromOffset(kLoadDoubleword, 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::kFloat32: {
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_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002816 __ LoadSFromOffset(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 __ LoadSFromOffset(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_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002822 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002823 }
2824 break;
2825 }
2826
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002827 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002828 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002829 if (index.IsConstant()) {
2830 size_t offset =
2831 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002832 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002833 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2834 __ Addu(TMP, index.AsRegister<Register>(), obj);
2835 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002836 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002837 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002838 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002839 }
2840 break;
2841 }
2842
Aart Bik66c158e2018-01-31 12:55:04 -08002843 case DataType::Type::kUint32:
2844 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002845 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002846 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2847 UNREACHABLE();
2848 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002849}
2850
2851void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002852 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002853 locations->SetInAt(0, Location::RequiresRegister());
2854 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2855}
2856
2857void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2858 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002859 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002860 Register obj = locations->InAt(0).AsRegister<Register>();
2861 Register out = locations->Out().AsRegister<Register>();
2862 __ LoadFromOffset(kLoadWord, out, obj, offset);
2863 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002864 // Mask out compression flag from String's array length.
2865 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2866 __ Srl(out, out, 1u);
2867 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002868}
2869
Alexey Frunzef58b2482016-09-02 22:14:06 -07002870Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2871 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2872 ? Location::ConstantLocation(instruction->AsConstant())
2873 : Location::RequiresRegister();
2874}
2875
2876Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2877 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2878 // We can store a non-zero float or double constant without first loading it into the FPU,
2879 // but we should only prefer this if the constant has a single use.
2880 if (instruction->IsConstant() &&
2881 (instruction->AsConstant()->IsZeroBitPattern() ||
2882 instruction->GetUses().HasExactlyOneElement())) {
2883 return Location::ConstantLocation(instruction->AsConstant());
2884 // Otherwise fall through and require an FPU register for the constant.
2885 }
2886 return Location::RequiresFpuRegister();
2887}
2888
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002889void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002890 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002891
2892 bool needs_write_barrier =
2893 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2894 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2895
Vladimir Markoca6fff82017-10-03 14:49:14 +01002896 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002897 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002898 may_need_runtime_call_for_type_check ?
2899 LocationSummary::kCallOnSlowPath :
2900 LocationSummary::kNoCall);
2901
2902 locations->SetInAt(0, Location::RequiresRegister());
2903 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002904 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002905 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002906 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002907 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2908 }
2909 if (needs_write_barrier) {
2910 // Temporary register for the write barrier.
2911 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002912 }
2913}
2914
2915void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2916 LocationSummary* locations = instruction->GetLocations();
2917 Register obj = locations->InAt(0).AsRegister<Register>();
2918 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002919 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002920 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002921 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002922 bool needs_write_barrier =
2923 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002924 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002925 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002926
2927 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002928 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002929 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002930 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002931 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002932 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002933 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002934 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002935 __ Addu(base_reg, obj, index.AsRegister<Register>());
2936 }
2937 if (value_location.IsConstant()) {
2938 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2939 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2940 } else {
2941 Register value = value_location.AsRegister<Register>();
2942 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002943 }
2944 break;
2945 }
2946
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002947 case DataType::Type::kUint16:
2948 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002949 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002950 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002951 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002952 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2953 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002954 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002955 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002956 }
2957 if (value_location.IsConstant()) {
2958 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2959 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2960 } else {
2961 Register value = value_location.AsRegister<Register>();
2962 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002963 }
2964 break;
2965 }
2966
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002967 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002968 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2969 if (index.IsConstant()) {
2970 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002971 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2972 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002973 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002974 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002975 }
2976 if (value_location.IsConstant()) {
2977 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2978 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2979 } else {
2980 Register value = value_location.AsRegister<Register>();
2981 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2982 }
2983 break;
2984 }
2985
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002986 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002987 if (value_location.IsConstant()) {
2988 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002989 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002990 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002991 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002992 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002993 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002994 }
Alexey Frunze15958152017-02-09 19:08:30 -08002995 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2996 DCHECK_EQ(value, 0);
2997 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2998 DCHECK(!needs_write_barrier);
2999 DCHECK(!may_need_runtime_call_for_type_check);
3000 break;
3001 }
3002
3003 DCHECK(needs_write_barrier);
3004 Register value = value_location.AsRegister<Register>();
3005 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
3006 Register temp2 = TMP; // Doesn't need to survive slow path.
3007 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3008 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3009 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3010 MipsLabel done;
3011 SlowPathCodeMIPS* slow_path = nullptr;
3012
3013 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003014 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003015 codegen_->AddSlowPath(slow_path);
3016 if (instruction->GetValueCanBeNull()) {
3017 MipsLabel non_zero;
3018 __ Bnez(value, &non_zero);
3019 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3020 if (index.IsConstant()) {
3021 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003022 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3023 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003024 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003025 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003026 }
Alexey Frunze15958152017-02-09 19:08:30 -08003027 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3028 __ B(&done);
3029 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003030 }
Alexey Frunze15958152017-02-09 19:08:30 -08003031
3032 // Note that when read barriers are enabled, the type checks
3033 // are performed without read barriers. This is fine, even in
3034 // the case where a class object is in the from-space after
3035 // the flip, as a comparison involving such a type would not
3036 // produce a false positive; it may of course produce a false
3037 // negative, in which case we would take the ArraySet slow
3038 // path.
3039
3040 // /* HeapReference<Class> */ temp1 = obj->klass_
3041 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3042 __ MaybeUnpoisonHeapReference(temp1);
3043
3044 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3045 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3046 // /* HeapReference<Class> */ temp2 = value->klass_
3047 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3048 // If heap poisoning is enabled, no need to unpoison `temp1`
3049 // nor `temp2`, as we are comparing two poisoned references.
3050
3051 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3052 MipsLabel do_put;
3053 __ Beq(temp1, temp2, &do_put);
3054 // If heap poisoning is enabled, the `temp1` reference has
3055 // not been unpoisoned yet; unpoison it now.
3056 __ MaybeUnpoisonHeapReference(temp1);
3057
3058 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3059 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3060 // If heap poisoning is enabled, no need to unpoison
3061 // `temp1`, as we are comparing against null below.
3062 __ Bnez(temp1, slow_path->GetEntryLabel());
3063 __ Bind(&do_put);
3064 } else {
3065 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3066 }
3067 }
3068
3069 Register source = value;
3070 if (kPoisonHeapReferences) {
3071 // Note that in the case where `value` is a null reference,
3072 // we do not enter this block, as a null reference does not
3073 // need poisoning.
3074 __ Move(temp1, value);
3075 __ PoisonHeapReference(temp1);
3076 source = temp1;
3077 }
3078
3079 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3080 if (index.IsConstant()) {
3081 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003082 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003083 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003084 }
3085 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3086
3087 if (!may_need_runtime_call_for_type_check) {
3088 codegen_->MaybeRecordImplicitNullCheck(instruction);
3089 }
3090
3091 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3092
3093 if (done.IsLinked()) {
3094 __ Bind(&done);
3095 }
3096
3097 if (slow_path != nullptr) {
3098 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003099 }
3100 break;
3101 }
3102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003103 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003104 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003105 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003106 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003107 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3108 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003109 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003110 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003111 }
3112 if (value_location.IsConstant()) {
3113 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3114 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3115 } else {
3116 Register value = value_location.AsRegisterPairLow<Register>();
3117 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003118 }
3119 break;
3120 }
3121
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003122 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003123 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003124 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003125 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003126 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3127 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003128 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003129 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003130 }
3131 if (value_location.IsConstant()) {
3132 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3133 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3134 } else {
3135 FRegister value = value_location.AsFpuRegister<FRegister>();
3136 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003137 }
3138 break;
3139 }
3140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003141 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003142 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003143 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003144 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003145 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3146 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003147 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003148 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003149 }
3150 if (value_location.IsConstant()) {
3151 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3152 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3153 } else {
3154 FRegister value = value_location.AsFpuRegister<FRegister>();
3155 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003156 }
3157 break;
3158 }
3159
Aart Bik66c158e2018-01-31 12:55:04 -08003160 case DataType::Type::kUint32:
3161 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003162 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003163 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3164 UNREACHABLE();
3165 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003166}
3167
Lena Djokica2901602017-09-21 13:50:52 +02003168void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3169 HIntermediateArrayAddressIndex* instruction) {
3170 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003171 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003172
3173 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3174
3175 locations->SetInAt(0, Location::RequiresRegister());
3176 locations->SetInAt(1, Location::ConstantLocation(shift));
3177 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3178}
3179
3180void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3181 HIntermediateArrayAddressIndex* instruction) {
3182 LocationSummary* locations = instruction->GetLocations();
3183 Register index_reg = locations->InAt(0).AsRegister<Register>();
3184 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3185 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3186}
3187
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003188void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003189 RegisterSet caller_saves = RegisterSet::Empty();
3190 InvokeRuntimeCallingConvention calling_convention;
3191 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3192 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3193 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003194
3195 HInstruction* index = instruction->InputAt(0);
3196 HInstruction* length = instruction->InputAt(1);
3197
3198 bool const_index = false;
3199 bool const_length = false;
3200
3201 if (index->IsConstant()) {
3202 if (length->IsConstant()) {
3203 const_index = true;
3204 const_length = true;
3205 } else {
3206 int32_t index_value = index->AsIntConstant()->GetValue();
3207 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3208 const_index = true;
3209 }
3210 }
3211 } else if (length->IsConstant()) {
3212 int32_t length_value = length->AsIntConstant()->GetValue();
3213 if (IsUint<15>(length_value)) {
3214 const_length = true;
3215 }
3216 }
3217
3218 locations->SetInAt(0, const_index
3219 ? Location::ConstantLocation(index->AsConstant())
3220 : Location::RequiresRegister());
3221 locations->SetInAt(1, const_length
3222 ? Location::ConstantLocation(length->AsConstant())
3223 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003224}
3225
3226void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3227 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003228 Location index_loc = locations->InAt(0);
3229 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003230
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003231 if (length_loc.IsConstant()) {
3232 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3233 if (index_loc.IsConstant()) {
3234 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3235 if (index < 0 || index >= length) {
3236 BoundsCheckSlowPathMIPS* slow_path =
3237 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3238 codegen_->AddSlowPath(slow_path);
3239 __ B(slow_path->GetEntryLabel());
3240 } else {
3241 // Nothing to be done.
3242 }
3243 return;
3244 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003245
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003246 BoundsCheckSlowPathMIPS* slow_path =
3247 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3248 codegen_->AddSlowPath(slow_path);
3249 Register index = index_loc.AsRegister<Register>();
3250 if (length == 0) {
3251 __ B(slow_path->GetEntryLabel());
3252 } else if (length == 1) {
3253 __ Bnez(index, slow_path->GetEntryLabel());
3254 } else {
3255 DCHECK(IsUint<15>(length)) << length;
3256 __ Sltiu(TMP, index, length);
3257 __ Beqz(TMP, slow_path->GetEntryLabel());
3258 }
3259 } else {
3260 Register length = length_loc.AsRegister<Register>();
3261 BoundsCheckSlowPathMIPS* slow_path =
3262 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3263 codegen_->AddSlowPath(slow_path);
3264 if (index_loc.IsConstant()) {
3265 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3266 if (index < 0) {
3267 __ B(slow_path->GetEntryLabel());
3268 } else if (index == 0) {
3269 __ Blez(length, slow_path->GetEntryLabel());
3270 } else {
3271 DCHECK(IsInt<16>(index + 1)) << index;
3272 __ Sltiu(TMP, length, index + 1);
3273 __ Bnez(TMP, slow_path->GetEntryLabel());
3274 }
3275 } else {
3276 Register index = index_loc.AsRegister<Register>();
3277 __ Bgeu(index, length, slow_path->GetEntryLabel());
3278 }
3279 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003280}
3281
Alexey Frunze15958152017-02-09 19:08:30 -08003282// Temp is used for read barrier.
3283static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3284 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003285 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003286 (kUseBakerReadBarrier ||
3287 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3288 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3289 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3290 return 1;
3291 }
3292 return 0;
3293}
3294
3295// Extra temp is used for read barrier.
3296static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3297 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3298}
3299
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003300void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003301 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003302 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003303 LocationSummary* locations =
3304 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003305 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003306 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003307 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003308}
3309
3310void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003311 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003312 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003313 Location obj_loc = locations->InAt(0);
3314 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003315 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003316 Location temp_loc = locations->GetTemp(0);
3317 Register temp = temp_loc.AsRegister<Register>();
3318 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3319 DCHECK_LE(num_temps, 2u);
3320 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003321 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3322 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3323 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3324 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3325 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3326 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3327 const uint32_t object_array_data_offset =
3328 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3329 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003330
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003331 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003332 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003333 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3334 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003335 codegen_->AddSlowPath(slow_path);
3336
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003337 // Avoid this check if we know `obj` is not null.
3338 if (instruction->MustDoNullCheck()) {
3339 __ Beqz(obj, &done);
3340 }
3341
3342 switch (type_check_kind) {
3343 case TypeCheckKind::kExactCheck:
3344 case TypeCheckKind::kArrayCheck: {
3345 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003346 GenerateReferenceLoadTwoRegisters(instruction,
3347 temp_loc,
3348 obj_loc,
3349 class_offset,
3350 maybe_temp2_loc,
3351 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003352 // Jump to slow path for throwing the exception or doing a
3353 // more involved array check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003354 __ Bne(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003355 break;
3356 }
3357
3358 case TypeCheckKind::kAbstractClassCheck: {
3359 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003360 GenerateReferenceLoadTwoRegisters(instruction,
3361 temp_loc,
3362 obj_loc,
3363 class_offset,
3364 maybe_temp2_loc,
3365 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003366 // If the class is abstract, we eagerly fetch the super class of the
3367 // object to avoid doing a comparison we know will fail.
3368 MipsLabel loop;
3369 __ Bind(&loop);
3370 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003371 GenerateReferenceLoadOneRegister(instruction,
3372 temp_loc,
3373 super_offset,
3374 maybe_temp2_loc,
3375 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003376 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3377 // exception.
3378 __ Beqz(temp, slow_path->GetEntryLabel());
3379 // Otherwise, compare the classes.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003380 __ Bne(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003381 break;
3382 }
3383
3384 case TypeCheckKind::kClassHierarchyCheck: {
3385 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003386 GenerateReferenceLoadTwoRegisters(instruction,
3387 temp_loc,
3388 obj_loc,
3389 class_offset,
3390 maybe_temp2_loc,
3391 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003392 // Walk over the class hierarchy to find a match.
3393 MipsLabel loop;
3394 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003395 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003396 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003397 GenerateReferenceLoadOneRegister(instruction,
3398 temp_loc,
3399 super_offset,
3400 maybe_temp2_loc,
3401 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003402 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3403 // exception. Otherwise, jump to the beginning of the loop.
3404 __ Bnez(temp, &loop);
3405 __ B(slow_path->GetEntryLabel());
3406 break;
3407 }
3408
3409 case TypeCheckKind::kArrayObjectCheck: {
3410 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003411 GenerateReferenceLoadTwoRegisters(instruction,
3412 temp_loc,
3413 obj_loc,
3414 class_offset,
3415 maybe_temp2_loc,
3416 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003417 // Do an exact check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003418 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003419 // Otherwise, we need to check that the object's class is a non-primitive array.
3420 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003421 GenerateReferenceLoadOneRegister(instruction,
3422 temp_loc,
3423 component_offset,
3424 maybe_temp2_loc,
3425 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003426 // If the component type is null, jump to the slow path to throw the exception.
3427 __ Beqz(temp, slow_path->GetEntryLabel());
3428 // Otherwise, the object is indeed an array, further check that this component
3429 // type is not a primitive type.
3430 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3431 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3432 __ Bnez(temp, slow_path->GetEntryLabel());
3433 break;
3434 }
3435
3436 case TypeCheckKind::kUnresolvedCheck:
3437 // We always go into the type check slow path for the unresolved check case.
3438 // We cannot directly call the CheckCast runtime entry point
3439 // without resorting to a type checking slow path here (i.e. by
3440 // calling InvokeRuntime directly), as it would require to
3441 // assign fixed registers for the inputs of this HInstanceOf
3442 // instruction (following the runtime calling convention), which
3443 // might be cluttered by the potential first read barrier
3444 // emission at the beginning of this method.
3445 __ B(slow_path->GetEntryLabel());
3446 break;
3447
3448 case TypeCheckKind::kInterfaceCheck: {
3449 // Avoid read barriers to improve performance of the fast path. We can not get false
3450 // positives by doing this.
3451 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003452 GenerateReferenceLoadTwoRegisters(instruction,
3453 temp_loc,
3454 obj_loc,
3455 class_offset,
3456 maybe_temp2_loc,
3457 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003458 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003459 GenerateReferenceLoadTwoRegisters(instruction,
3460 temp_loc,
3461 temp_loc,
3462 iftable_offset,
3463 maybe_temp2_loc,
3464 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003465 // Iftable is never null.
3466 __ Lw(TMP, temp, array_length_offset);
3467 // Loop through the iftable and check if any class matches.
3468 MipsLabel loop;
3469 __ Bind(&loop);
3470 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3471 __ Beqz(TMP, slow_path->GetEntryLabel());
3472 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3473 __ MaybeUnpoisonHeapReference(AT);
3474 // Go to next interface.
3475 __ Addiu(TMP, TMP, -2);
3476 // Compare the classes and continue the loop if they do not match.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003477 __ Bne(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003478 break;
3479 }
3480 }
3481
3482 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003483 __ Bind(slow_path->GetExitLabel());
3484}
3485
3486void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3487 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003488 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003489 locations->SetInAt(0, Location::RequiresRegister());
3490 if (check->HasUses()) {
3491 locations->SetOut(Location::SameAsFirstInput());
3492 }
3493}
3494
3495void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3496 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003497 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003498 check->GetLoadClass(),
3499 check,
3500 check->GetDexPc(),
3501 true);
3502 codegen_->AddSlowPath(slow_path);
3503 GenerateClassInitializationCheck(slow_path,
3504 check->GetLocations()->InAt(0).AsRegister<Register>());
3505}
3506
3507void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003508 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003509
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003510 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003511 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003512
3513 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003514 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003515 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003516 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003517 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003518 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003519 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003520 locations->SetInAt(0, Location::RequiresRegister());
3521 locations->SetInAt(1, Location::RequiresRegister());
3522 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3523 break;
3524
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003525 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003526 locations->SetInAt(0, Location::RequiresRegister());
3527 locations->SetInAt(1, Location::RequiresRegister());
3528 // Output overlaps because it is written before doing the low comparison.
3529 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3530 break;
3531
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003532 case DataType::Type::kFloat32:
3533 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003534 locations->SetInAt(0, Location::RequiresFpuRegister());
3535 locations->SetInAt(1, Location::RequiresFpuRegister());
3536 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003537 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003538
3539 default:
3540 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3541 }
3542}
3543
3544void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3545 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003546 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003547 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003548 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003549
3550 // 0 if: left == right
3551 // 1 if: left > right
3552 // -1 if: left < right
3553 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003554 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003555 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003556 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003557 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003558 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003559 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003560 Register lhs = locations->InAt(0).AsRegister<Register>();
3561 Register rhs = locations->InAt(1).AsRegister<Register>();
3562 __ Slt(TMP, lhs, rhs);
3563 __ Slt(res, rhs, lhs);
3564 __ Subu(res, res, TMP);
3565 break;
3566 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003567 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003568 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003569 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3570 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3571 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3572 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3573 // TODO: more efficient (direct) comparison with a constant.
3574 __ Slt(TMP, lhs_high, rhs_high);
3575 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3576 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3577 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3578 __ Sltu(TMP, lhs_low, rhs_low);
3579 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3580 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3581 __ Bind(&done);
3582 break;
3583 }
3584
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003585 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003586 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003587 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3588 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3589 MipsLabel done;
3590 if (isR6) {
3591 __ CmpEqS(FTMP, lhs, rhs);
3592 __ LoadConst32(res, 0);
3593 __ Bc1nez(FTMP, &done);
3594 if (gt_bias) {
3595 __ CmpLtS(FTMP, lhs, rhs);
3596 __ LoadConst32(res, -1);
3597 __ Bc1nez(FTMP, &done);
3598 __ LoadConst32(res, 1);
3599 } else {
3600 __ CmpLtS(FTMP, rhs, lhs);
3601 __ LoadConst32(res, 1);
3602 __ Bc1nez(FTMP, &done);
3603 __ LoadConst32(res, -1);
3604 }
3605 } else {
3606 if (gt_bias) {
3607 __ ColtS(0, lhs, rhs);
3608 __ LoadConst32(res, -1);
3609 __ Bc1t(0, &done);
3610 __ CeqS(0, lhs, rhs);
3611 __ LoadConst32(res, 1);
3612 __ Movt(res, ZERO, 0);
3613 } else {
3614 __ ColtS(0, rhs, lhs);
3615 __ LoadConst32(res, 1);
3616 __ Bc1t(0, &done);
3617 __ CeqS(0, lhs, rhs);
3618 __ LoadConst32(res, -1);
3619 __ Movt(res, ZERO, 0);
3620 }
3621 }
3622 __ Bind(&done);
3623 break;
3624 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003625 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003626 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003627 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3628 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3629 MipsLabel done;
3630 if (isR6) {
3631 __ CmpEqD(FTMP, lhs, rhs);
3632 __ LoadConst32(res, 0);
3633 __ Bc1nez(FTMP, &done);
3634 if (gt_bias) {
3635 __ CmpLtD(FTMP, lhs, rhs);
3636 __ LoadConst32(res, -1);
3637 __ Bc1nez(FTMP, &done);
3638 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003639 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003640 __ CmpLtD(FTMP, rhs, lhs);
3641 __ LoadConst32(res, 1);
3642 __ Bc1nez(FTMP, &done);
3643 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003644 }
3645 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003646 if (gt_bias) {
3647 __ ColtD(0, lhs, rhs);
3648 __ LoadConst32(res, -1);
3649 __ Bc1t(0, &done);
3650 __ CeqD(0, lhs, rhs);
3651 __ LoadConst32(res, 1);
3652 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003653 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003654 __ ColtD(0, rhs, lhs);
3655 __ LoadConst32(res, 1);
3656 __ Bc1t(0, &done);
3657 __ CeqD(0, lhs, rhs);
3658 __ LoadConst32(res, -1);
3659 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003660 }
3661 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003662 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003663 break;
3664 }
3665
3666 default:
3667 LOG(FATAL) << "Unimplemented compare type " << in_type;
3668 }
3669}
3670
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003671void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003672 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003673 switch (instruction->InputAt(0)->GetType()) {
3674 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003675 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003676 locations->SetInAt(0, Location::RequiresRegister());
3677 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3678 break;
3679
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003680 case DataType::Type::kFloat32:
3681 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003682 locations->SetInAt(0, Location::RequiresFpuRegister());
3683 locations->SetInAt(1, Location::RequiresFpuRegister());
3684 break;
3685 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003686 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003687 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3688 }
3689}
3690
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003691void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003692 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003693 return;
3694 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003697 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003698
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003699 switch (type) {
3700 default:
3701 // Integer case.
3702 GenerateIntCompare(instruction->GetCondition(), locations);
3703 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003705 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003706 GenerateLongCompare(instruction->GetCondition(), locations);
3707 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003708
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003709 case DataType::Type::kFloat32:
3710 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003711 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3712 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003713 }
3714}
3715
Alexey Frunze7e99e052015-11-24 19:28:01 -08003716void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3717 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003718
3719 LocationSummary* locations = instruction->GetLocations();
3720 Location second = locations->InAt(1);
3721 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003722 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003723 DCHECK(imm == 1 || imm == -1);
3724
Lena Djokic4b8025c2017-12-21 16:15:50 +01003725 if (instruction->GetResultType() == DataType::Type::kInt32) {
3726 Register out = locations->Out().AsRegister<Register>();
3727 Register dividend = locations->InAt(0).AsRegister<Register>();
3728
3729 if (instruction->IsRem()) {
3730 __ Move(out, ZERO);
3731 } else {
3732 if (imm == -1) {
3733 __ Subu(out, ZERO, dividend);
3734 } else if (out != dividend) {
3735 __ Move(out, dividend);
3736 }
3737 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003738 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003739 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3740 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3741 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3742 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3743 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3744
3745 if (instruction->IsRem()) {
3746 __ Move(out_high, ZERO);
3747 __ Move(out_low, ZERO);
3748 } else {
3749 if (imm == -1) {
3750 __ Subu(out_low, ZERO, in_low);
3751 __ Sltu(AT, ZERO, out_low);
3752 __ Subu(out_high, ZERO, in_high);
3753 __ Subu(out_high, out_high, AT);
3754 } else {
3755 __ Move(out_low, in_low);
3756 __ Move(out_high, in_high);
3757 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003758 }
3759 }
3760}
3761
3762void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3763 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003764
3765 LocationSummary* locations = instruction->GetLocations();
3766 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003767 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3768 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003769 DCHECK(second.IsConstant());
3770
Lena Djokic4b8025c2017-12-21 16:15:50 +01003771 if (instruction->GetResultType() == DataType::Type::kInt32) {
3772 Register out = locations->Out().AsRegister<Register>();
3773 Register dividend = locations->InAt(0).AsRegister<Register>();
3774 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3775 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3776 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003777
Lena Djokic4b8025c2017-12-21 16:15:50 +01003778 if (instruction->IsDiv()) {
3779 if (ctz_imm == 1) {
3780 // Fast path for division by +/-2, which is very common.
3781 __ Srl(TMP, dividend, 31);
3782 } else {
3783 __ Sra(TMP, dividend, 31);
3784 __ Srl(TMP, TMP, 32 - ctz_imm);
3785 }
3786 __ Addu(out, dividend, TMP);
3787 __ Sra(out, out, ctz_imm);
3788 if (imm < 0) {
3789 __ Subu(out, ZERO, out);
3790 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003791 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003792 if (ctz_imm == 1) {
3793 // Fast path for modulo +/-2, which is very common.
3794 __ Sra(TMP, dividend, 31);
3795 __ Subu(out, dividend, TMP);
3796 __ Andi(out, out, 1);
3797 __ Addu(out, out, TMP);
3798 } else {
3799 __ Sra(TMP, dividend, 31);
3800 __ Srl(TMP, TMP, 32 - ctz_imm);
3801 __ Addu(out, dividend, TMP);
3802 if (IsUint<16>(abs_imm - 1)) {
3803 __ Andi(out, out, abs_imm - 1);
3804 } else {
3805 if (is_r2_or_newer) {
3806 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3807 } else {
3808 __ Sll(out, out, 32 - ctz_imm);
3809 __ Srl(out, out, 32 - ctz_imm);
3810 }
3811 }
3812 __ Subu(out, out, TMP);
3813 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003814 }
3815 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003816 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3817 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3818 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3819 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3820 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3821 int64_t imm = Int64FromConstant(second.GetConstant());
3822 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3823 int ctz_imm = CTZ(abs_imm);
3824
3825 if (instruction->IsDiv()) {
3826 if (ctz_imm < 32) {
3827 if (ctz_imm == 1) {
3828 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003829 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003830 __ Sra(AT, in_high, 31);
3831 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003832 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003833 __ Addu(AT, AT, in_low);
3834 __ Sltu(TMP, AT, in_low);
3835 __ Addu(out_high, in_high, TMP);
3836 __ Srl(out_low, AT, ctz_imm);
3837 if (is_r2_or_newer) {
3838 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3839 __ Sra(out_high, out_high, ctz_imm);
3840 } else {
3841 __ Sll(AT, out_high, 32 - ctz_imm);
3842 __ Sra(out_high, out_high, ctz_imm);
3843 __ Or(out_low, out_low, AT);
3844 }
3845 if (imm < 0) {
3846 __ Subu(out_low, ZERO, out_low);
3847 __ Sltu(AT, ZERO, out_low);
3848 __ Subu(out_high, ZERO, out_high);
3849 __ Subu(out_high, out_high, AT);
3850 }
3851 } else if (ctz_imm == 32) {
3852 __ Sra(AT, in_high, 31);
3853 __ Addu(AT, AT, in_low);
3854 __ Sltu(AT, AT, in_low);
3855 __ Addu(out_low, in_high, AT);
3856 if (imm < 0) {
3857 __ Srl(TMP, out_low, 31);
3858 __ Subu(out_low, ZERO, out_low);
3859 __ Sltu(AT, ZERO, out_low);
3860 __ Subu(out_high, TMP, AT);
3861 } else {
3862 __ Sra(out_high, out_low, 31);
3863 }
3864 } else if (ctz_imm < 63) {
3865 __ Sra(AT, in_high, 31);
3866 __ Srl(TMP, AT, 64 - ctz_imm);
3867 __ Addu(AT, AT, in_low);
3868 __ Sltu(AT, AT, in_low);
3869 __ Addu(out_low, in_high, AT);
3870 __ Addu(out_low, out_low, TMP);
3871 __ Sra(out_low, out_low, ctz_imm - 32);
3872 if (imm < 0) {
3873 __ Subu(out_low, ZERO, out_low);
3874 }
3875 __ Sra(out_high, out_low, 31);
3876 } else {
3877 DCHECK_LT(imm, 0);
3878 if (is_r6) {
3879 __ Aui(AT, in_high, 0x8000);
3880 } else {
3881 __ Lui(AT, 0x8000);
3882 __ Xor(AT, AT, in_high);
3883 }
3884 __ Or(AT, AT, in_low);
3885 __ Sltiu(out_low, AT, 1);
3886 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003887 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003888 } else {
3889 if ((ctz_imm == 1) && !is_r6) {
3890 __ Andi(AT, in_low, 1);
3891 __ Sll(TMP, in_low, 31);
3892 __ And(TMP, in_high, TMP);
3893 __ Sra(out_high, TMP, 31);
3894 __ Or(out_low, out_high, AT);
3895 } else if (ctz_imm < 32) {
3896 __ Sra(AT, in_high, 31);
3897 if (ctz_imm <= 16) {
3898 __ Andi(out_low, in_low, abs_imm - 1);
3899 } else if (is_r2_or_newer) {
3900 __ Ext(out_low, in_low, 0, ctz_imm);
3901 } else {
3902 __ Sll(out_low, in_low, 32 - ctz_imm);
3903 __ Srl(out_low, out_low, 32 - ctz_imm);
3904 }
3905 if (is_r6) {
3906 __ Selnez(out_high, AT, out_low);
3907 } else {
3908 __ Movz(AT, ZERO, out_low);
3909 __ Move(out_high, AT);
3910 }
3911 if (is_r2_or_newer) {
3912 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
3913 } else {
3914 __ Sll(AT, out_high, ctz_imm);
3915 __ Or(out_low, out_low, AT);
3916 }
3917 } else if (ctz_imm == 32) {
3918 __ Sra(AT, in_high, 31);
3919 __ Move(out_low, in_low);
3920 if (is_r6) {
3921 __ Selnez(out_high, AT, out_low);
3922 } else {
3923 __ Movz(AT, ZERO, out_low);
3924 __ Move(out_high, AT);
3925 }
3926 } else if (ctz_imm < 63) {
3927 __ Sra(AT, in_high, 31);
3928 __ Move(TMP, in_low);
3929 if (ctz_imm - 32 <= 16) {
3930 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
3931 } else if (is_r2_or_newer) {
3932 __ Ext(out_high, in_high, 0, ctz_imm - 32);
3933 } else {
3934 __ Sll(out_high, in_high, 64 - ctz_imm);
3935 __ Srl(out_high, out_high, 64 - ctz_imm);
3936 }
3937 __ Move(out_low, TMP);
3938 __ Or(TMP, TMP, out_high);
3939 if (is_r6) {
3940 __ Selnez(AT, AT, TMP);
3941 } else {
3942 __ Movz(AT, ZERO, TMP);
3943 }
3944 if (is_r2_or_newer) {
3945 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
3946 } else {
3947 __ Sll(AT, AT, ctz_imm - 32);
3948 __ Or(out_high, out_high, AT);
3949 }
3950 } else {
3951 if (is_r6) {
3952 __ Aui(AT, in_high, 0x8000);
3953 } else {
3954 __ Lui(AT, 0x8000);
3955 __ Xor(AT, AT, in_high);
3956 }
3957 __ Or(AT, AT, in_low);
3958 __ Sltiu(AT, AT, 1);
3959 __ Sll(AT, AT, 31);
3960 __ Move(out_low, in_low);
3961 __ Xor(out_high, in_high, AT);
3962 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003963 }
3964 }
3965}
3966
3967void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3968 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003969 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003970
3971 LocationSummary* locations = instruction->GetLocations();
3972 Location second = locations->InAt(1);
3973 DCHECK(second.IsConstant());
3974
3975 Register out = locations->Out().AsRegister<Register>();
3976 Register dividend = locations->InAt(0).AsRegister<Register>();
3977 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3978
3979 int64_t magic;
3980 int shift;
3981 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3982
3983 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3984
3985 __ LoadConst32(TMP, magic);
3986 if (isR6) {
3987 __ MuhR6(TMP, dividend, TMP);
3988 } else {
3989 __ MultR2(dividend, TMP);
3990 __ Mfhi(TMP);
3991 }
3992 if (imm > 0 && magic < 0) {
3993 __ Addu(TMP, TMP, dividend);
3994 } else if (imm < 0 && magic > 0) {
3995 __ Subu(TMP, TMP, dividend);
3996 }
3997
3998 if (shift != 0) {
3999 __ Sra(TMP, TMP, shift);
4000 }
4001
4002 if (instruction->IsDiv()) {
4003 __ Sra(out, TMP, 31);
4004 __ Subu(out, TMP, out);
4005 } else {
4006 __ Sra(AT, TMP, 31);
4007 __ Subu(AT, TMP, AT);
4008 __ LoadConst32(TMP, imm);
4009 if (isR6) {
4010 __ MulR6(TMP, AT, TMP);
4011 } else {
4012 __ MulR2(TMP, AT, TMP);
4013 }
4014 __ Subu(out, dividend, TMP);
4015 }
4016}
4017
4018void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4019 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004020 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004021
4022 LocationSummary* locations = instruction->GetLocations();
4023 Register out = locations->Out().AsRegister<Register>();
4024 Location second = locations->InAt(1);
4025
4026 if (second.IsConstant()) {
4027 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4028 if (imm == 0) {
4029 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4030 } else if (imm == 1 || imm == -1) {
4031 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004032 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004033 DivRemByPowerOfTwo(instruction);
4034 } else {
4035 DCHECK(imm <= -2 || imm >= 2);
4036 GenerateDivRemWithAnyConstant(instruction);
4037 }
4038 } else {
4039 Register dividend = locations->InAt(0).AsRegister<Register>();
4040 Register divisor = second.AsRegister<Register>();
4041 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4042 if (instruction->IsDiv()) {
4043 if (isR6) {
4044 __ DivR6(out, dividend, divisor);
4045 } else {
4046 __ DivR2(out, dividend, divisor);
4047 }
4048 } else {
4049 if (isR6) {
4050 __ ModR6(out, dividend, divisor);
4051 } else {
4052 __ ModR2(out, dividend, divisor);
4053 }
4054 }
4055 }
4056}
4057
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004058void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004059 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004060 bool call_long_div = false;
4061 if (type == DataType::Type::kInt64) {
4062 if (div->InputAt(1)->IsConstant()) {
4063 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4064 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4065 } else {
4066 call_long_div = true;
4067 }
4068 }
4069 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004070 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004071 : LocationSummary::kNoCall;
4072
Vladimir Markoca6fff82017-10-03 14:49:14 +01004073 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004074
4075 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004076 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004077 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004078 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004079 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4080 break;
4081
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004082 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004083 if (call_long_div) {
4084 InvokeRuntimeCallingConvention calling_convention;
4085 locations->SetInAt(0, Location::RegisterPairLocation(
4086 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4087 locations->SetInAt(1, Location::RegisterPairLocation(
4088 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4089 locations->SetOut(calling_convention.GetReturnLocation(type));
4090 } else {
4091 locations->SetInAt(0, Location::RequiresRegister());
4092 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4093 locations->SetOut(Location::RequiresRegister());
4094 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004095 break;
4096 }
4097
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004098 case DataType::Type::kFloat32:
4099 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004100 locations->SetInAt(0, Location::RequiresFpuRegister());
4101 locations->SetInAt(1, Location::RequiresFpuRegister());
4102 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4103 break;
4104
4105 default:
4106 LOG(FATAL) << "Unexpected div type " << type;
4107 }
4108}
4109
4110void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004111 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004112 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004113
4114 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004115 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004116 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004117 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004118 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004119 if (locations->InAt(1).IsConstant()) {
4120 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4121 if (imm == 0) {
4122 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4123 } else if (imm == 1 || imm == -1) {
4124 DivRemOneOrMinusOne(instruction);
4125 } else {
4126 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4127 DivRemByPowerOfTwo(instruction);
4128 }
4129 } else {
4130 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4131 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4132 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004133 break;
4134 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004135 case DataType::Type::kFloat32:
4136 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004137 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4138 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4139 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004140 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004141 __ DivS(dst, lhs, rhs);
4142 } else {
4143 __ DivD(dst, lhs, rhs);
4144 }
4145 break;
4146 }
4147 default:
4148 LOG(FATAL) << "Unexpected div type " << type;
4149 }
4150}
4151
4152void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004153 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004154 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004155}
4156
4157void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004158 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004159 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004160 codegen_->AddSlowPath(slow_path);
4161 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004162 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004163
4164 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004165 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004166 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004167 case DataType::Type::kInt8:
4168 case DataType::Type::kUint16:
4169 case DataType::Type::kInt16:
4170 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004171 if (value.IsConstant()) {
4172 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4173 __ B(slow_path->GetEntryLabel());
4174 } else {
4175 // A division by a non-null constant is valid. We don't need to perform
4176 // any check, so simply fall through.
4177 }
4178 } else {
4179 DCHECK(value.IsRegister()) << value;
4180 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4181 }
4182 break;
4183 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004184 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004185 if (value.IsConstant()) {
4186 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4187 __ B(slow_path->GetEntryLabel());
4188 } else {
4189 // A division by a non-null constant is valid. We don't need to perform
4190 // any check, so simply fall through.
4191 }
4192 } else {
4193 DCHECK(value.IsRegisterPair()) << value;
4194 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4195 __ Beqz(TMP, slow_path->GetEntryLabel());
4196 }
4197 break;
4198 }
4199 default:
4200 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4201 }
4202}
4203
4204void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4205 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004206 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004207 locations->SetOut(Location::ConstantLocation(constant));
4208}
4209
4210void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4211 // Will be generated at use site.
4212}
4213
4214void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4215 exit->SetLocations(nullptr);
4216}
4217
4218void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4219}
4220
4221void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4222 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004223 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004224 locations->SetOut(Location::ConstantLocation(constant));
4225}
4226
4227void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4228 // Will be generated at use site.
4229}
4230
4231void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4232 got->SetLocations(nullptr);
4233}
4234
4235void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004236 if (successor->IsExitBlock()) {
4237 DCHECK(got->GetPrevious()->AlwaysThrows());
4238 return; // no code needed
4239 }
4240
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004241 HBasicBlock* block = got->GetBlock();
4242 HInstruction* previous = got->GetPrevious();
4243 HLoopInformation* info = block->GetLoopInformation();
4244
4245 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004246 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4247 __ Lw(AT, SP, kCurrentMethodStackOffset);
4248 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4249 __ Addiu(TMP, TMP, 1);
4250 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4251 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004252 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4253 return;
4254 }
4255 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4256 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4257 }
4258 if (!codegen_->GoesToNextBlock(block, successor)) {
4259 __ B(codegen_->GetLabelOf(successor));
4260 }
4261}
4262
4263void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4264 HandleGoto(got, got->GetSuccessor());
4265}
4266
4267void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4268 try_boundary->SetLocations(nullptr);
4269}
4270
4271void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4272 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4273 if (!successor->IsExitBlock()) {
4274 HandleGoto(try_boundary, successor);
4275 }
4276}
4277
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004278void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4279 LocationSummary* locations) {
4280 Register dst = locations->Out().AsRegister<Register>();
4281 Register lhs = locations->InAt(0).AsRegister<Register>();
4282 Location rhs_location = locations->InAt(1);
4283 Register rhs_reg = ZERO;
4284 int64_t rhs_imm = 0;
4285 bool use_imm = rhs_location.IsConstant();
4286 if (use_imm) {
4287 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4288 } else {
4289 rhs_reg = rhs_location.AsRegister<Register>();
4290 }
4291
4292 switch (cond) {
4293 case kCondEQ:
4294 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004295 if (use_imm && IsInt<16>(-rhs_imm)) {
4296 if (rhs_imm == 0) {
4297 if (cond == kCondEQ) {
4298 __ Sltiu(dst, lhs, 1);
4299 } else {
4300 __ Sltu(dst, ZERO, lhs);
4301 }
4302 } else {
4303 __ Addiu(dst, lhs, -rhs_imm);
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 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004310 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004311 if (use_imm && IsUint<16>(rhs_imm)) {
4312 __ Xori(dst, lhs, rhs_imm);
4313 } else {
4314 if (use_imm) {
4315 rhs_reg = TMP;
4316 __ LoadConst32(rhs_reg, rhs_imm);
4317 }
4318 __ Xor(dst, lhs, rhs_reg);
4319 }
4320 if (cond == kCondEQ) {
4321 __ Sltiu(dst, dst, 1);
4322 } else {
4323 __ Sltu(dst, ZERO, dst);
4324 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004325 }
4326 break;
4327
4328 case kCondLT:
4329 case kCondGE:
4330 if (use_imm && IsInt<16>(rhs_imm)) {
4331 __ Slti(dst, lhs, rhs_imm);
4332 } else {
4333 if (use_imm) {
4334 rhs_reg = TMP;
4335 __ LoadConst32(rhs_reg, rhs_imm);
4336 }
4337 __ Slt(dst, lhs, rhs_reg);
4338 }
4339 if (cond == kCondGE) {
4340 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4341 // only the slt instruction but no sge.
4342 __ Xori(dst, dst, 1);
4343 }
4344 break;
4345
4346 case kCondLE:
4347 case kCondGT:
4348 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4349 // Simulate lhs <= rhs via lhs < rhs + 1.
4350 __ Slti(dst, lhs, rhs_imm + 1);
4351 if (cond == kCondGT) {
4352 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4353 // only the slti instruction but no sgti.
4354 __ Xori(dst, dst, 1);
4355 }
4356 } else {
4357 if (use_imm) {
4358 rhs_reg = TMP;
4359 __ LoadConst32(rhs_reg, rhs_imm);
4360 }
4361 __ Slt(dst, rhs_reg, lhs);
4362 if (cond == kCondLE) {
4363 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4364 // only the slt instruction but no sle.
4365 __ Xori(dst, dst, 1);
4366 }
4367 }
4368 break;
4369
4370 case kCondB:
4371 case kCondAE:
4372 if (use_imm && IsInt<16>(rhs_imm)) {
4373 // Sltiu sign-extends its 16-bit immediate operand before
4374 // the comparison and thus lets us compare directly with
4375 // unsigned values in the ranges [0, 0x7fff] and
4376 // [0xffff8000, 0xffffffff].
4377 __ Sltiu(dst, lhs, rhs_imm);
4378 } else {
4379 if (use_imm) {
4380 rhs_reg = TMP;
4381 __ LoadConst32(rhs_reg, rhs_imm);
4382 }
4383 __ Sltu(dst, lhs, rhs_reg);
4384 }
4385 if (cond == kCondAE) {
4386 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4387 // only the sltu instruction but no sgeu.
4388 __ Xori(dst, dst, 1);
4389 }
4390 break;
4391
4392 case kCondBE:
4393 case kCondA:
4394 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4395 // Simulate lhs <= rhs via lhs < rhs + 1.
4396 // Note that this only works if rhs + 1 does not overflow
4397 // to 0, hence the check above.
4398 // Sltiu sign-extends its 16-bit immediate operand before
4399 // the comparison and thus lets us compare directly with
4400 // unsigned values in the ranges [0, 0x7fff] and
4401 // [0xffff8000, 0xffffffff].
4402 __ Sltiu(dst, lhs, rhs_imm + 1);
4403 if (cond == kCondA) {
4404 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4405 // only the sltiu instruction but no sgtiu.
4406 __ Xori(dst, dst, 1);
4407 }
4408 } else {
4409 if (use_imm) {
4410 rhs_reg = TMP;
4411 __ LoadConst32(rhs_reg, rhs_imm);
4412 }
4413 __ Sltu(dst, rhs_reg, lhs);
4414 if (cond == kCondBE) {
4415 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4416 // only the sltu instruction but no sleu.
4417 __ Xori(dst, dst, 1);
4418 }
4419 }
4420 break;
4421 }
4422}
4423
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004424bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4425 LocationSummary* input_locations,
4426 Register dst) {
4427 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4428 Location rhs_location = input_locations->InAt(1);
4429 Register rhs_reg = ZERO;
4430 int64_t rhs_imm = 0;
4431 bool use_imm = rhs_location.IsConstant();
4432 if (use_imm) {
4433 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4434 } else {
4435 rhs_reg = rhs_location.AsRegister<Register>();
4436 }
4437
4438 switch (cond) {
4439 case kCondEQ:
4440 case kCondNE:
4441 if (use_imm && IsInt<16>(-rhs_imm)) {
4442 __ Addiu(dst, lhs, -rhs_imm);
4443 } else if (use_imm && IsUint<16>(rhs_imm)) {
4444 __ Xori(dst, lhs, rhs_imm);
4445 } else {
4446 if (use_imm) {
4447 rhs_reg = TMP;
4448 __ LoadConst32(rhs_reg, rhs_imm);
4449 }
4450 __ Xor(dst, lhs, rhs_reg);
4451 }
4452 return (cond == kCondEQ);
4453
4454 case kCondLT:
4455 case kCondGE:
4456 if (use_imm && IsInt<16>(rhs_imm)) {
4457 __ Slti(dst, lhs, rhs_imm);
4458 } else {
4459 if (use_imm) {
4460 rhs_reg = TMP;
4461 __ LoadConst32(rhs_reg, rhs_imm);
4462 }
4463 __ Slt(dst, lhs, rhs_reg);
4464 }
4465 return (cond == kCondGE);
4466
4467 case kCondLE:
4468 case kCondGT:
4469 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4470 // Simulate lhs <= rhs via lhs < rhs + 1.
4471 __ Slti(dst, lhs, rhs_imm + 1);
4472 return (cond == kCondGT);
4473 } else {
4474 if (use_imm) {
4475 rhs_reg = TMP;
4476 __ LoadConst32(rhs_reg, rhs_imm);
4477 }
4478 __ Slt(dst, rhs_reg, lhs);
4479 return (cond == kCondLE);
4480 }
4481
4482 case kCondB:
4483 case kCondAE:
4484 if (use_imm && IsInt<16>(rhs_imm)) {
4485 // Sltiu sign-extends its 16-bit immediate operand before
4486 // the comparison and thus lets us compare directly with
4487 // unsigned values in the ranges [0, 0x7fff] and
4488 // [0xffff8000, 0xffffffff].
4489 __ Sltiu(dst, lhs, rhs_imm);
4490 } else {
4491 if (use_imm) {
4492 rhs_reg = TMP;
4493 __ LoadConst32(rhs_reg, rhs_imm);
4494 }
4495 __ Sltu(dst, lhs, rhs_reg);
4496 }
4497 return (cond == kCondAE);
4498
4499 case kCondBE:
4500 case kCondA:
4501 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4502 // Simulate lhs <= rhs via lhs < rhs + 1.
4503 // Note that this only works if rhs + 1 does not overflow
4504 // to 0, hence the check above.
4505 // Sltiu sign-extends its 16-bit immediate operand before
4506 // the comparison and thus lets us compare directly with
4507 // unsigned values in the ranges [0, 0x7fff] and
4508 // [0xffff8000, 0xffffffff].
4509 __ Sltiu(dst, lhs, rhs_imm + 1);
4510 return (cond == kCondA);
4511 } else {
4512 if (use_imm) {
4513 rhs_reg = TMP;
4514 __ LoadConst32(rhs_reg, rhs_imm);
4515 }
4516 __ Sltu(dst, rhs_reg, lhs);
4517 return (cond == kCondBE);
4518 }
4519 }
4520}
4521
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004522void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4523 LocationSummary* locations,
4524 MipsLabel* label) {
4525 Register lhs = locations->InAt(0).AsRegister<Register>();
4526 Location rhs_location = locations->InAt(1);
4527 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004528 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004529 bool use_imm = rhs_location.IsConstant();
4530 if (use_imm) {
4531 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4532 } else {
4533 rhs_reg = rhs_location.AsRegister<Register>();
4534 }
4535
4536 if (use_imm && rhs_imm == 0) {
4537 switch (cond) {
4538 case kCondEQ:
4539 case kCondBE: // <= 0 if zero
4540 __ Beqz(lhs, label);
4541 break;
4542 case kCondNE:
4543 case kCondA: // > 0 if non-zero
4544 __ Bnez(lhs, label);
4545 break;
4546 case kCondLT:
4547 __ Bltz(lhs, label);
4548 break;
4549 case kCondGE:
4550 __ Bgez(lhs, label);
4551 break;
4552 case kCondLE:
4553 __ Blez(lhs, label);
4554 break;
4555 case kCondGT:
4556 __ Bgtz(lhs, label);
4557 break;
4558 case kCondB: // always false
4559 break;
4560 case kCondAE: // always true
4561 __ B(label);
4562 break;
4563 }
4564 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004565 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4566 if (isR6 || !use_imm) {
4567 if (use_imm) {
4568 rhs_reg = TMP;
4569 __ LoadConst32(rhs_reg, rhs_imm);
4570 }
4571 switch (cond) {
4572 case kCondEQ:
4573 __ Beq(lhs, rhs_reg, label);
4574 break;
4575 case kCondNE:
4576 __ Bne(lhs, rhs_reg, label);
4577 break;
4578 case kCondLT:
4579 __ Blt(lhs, rhs_reg, label);
4580 break;
4581 case kCondGE:
4582 __ Bge(lhs, rhs_reg, label);
4583 break;
4584 case kCondLE:
4585 __ Bge(rhs_reg, lhs, label);
4586 break;
4587 case kCondGT:
4588 __ Blt(rhs_reg, lhs, label);
4589 break;
4590 case kCondB:
4591 __ Bltu(lhs, rhs_reg, label);
4592 break;
4593 case kCondAE:
4594 __ Bgeu(lhs, rhs_reg, label);
4595 break;
4596 case kCondBE:
4597 __ Bgeu(rhs_reg, lhs, label);
4598 break;
4599 case kCondA:
4600 __ Bltu(rhs_reg, lhs, label);
4601 break;
4602 }
4603 } else {
4604 // Special cases for more efficient comparison with constants on R2.
4605 switch (cond) {
4606 case kCondEQ:
4607 __ LoadConst32(TMP, rhs_imm);
4608 __ Beq(lhs, TMP, label);
4609 break;
4610 case kCondNE:
4611 __ LoadConst32(TMP, rhs_imm);
4612 __ Bne(lhs, TMP, label);
4613 break;
4614 case kCondLT:
4615 if (IsInt<16>(rhs_imm)) {
4616 __ Slti(TMP, lhs, rhs_imm);
4617 __ Bnez(TMP, label);
4618 } else {
4619 __ LoadConst32(TMP, rhs_imm);
4620 __ Blt(lhs, TMP, label);
4621 }
4622 break;
4623 case kCondGE:
4624 if (IsInt<16>(rhs_imm)) {
4625 __ Slti(TMP, lhs, rhs_imm);
4626 __ Beqz(TMP, label);
4627 } else {
4628 __ LoadConst32(TMP, rhs_imm);
4629 __ Bge(lhs, TMP, label);
4630 }
4631 break;
4632 case kCondLE:
4633 if (IsInt<16>(rhs_imm + 1)) {
4634 // Simulate lhs <= rhs via lhs < rhs + 1.
4635 __ Slti(TMP, lhs, rhs_imm + 1);
4636 __ Bnez(TMP, label);
4637 } else {
4638 __ LoadConst32(TMP, rhs_imm);
4639 __ Bge(TMP, lhs, label);
4640 }
4641 break;
4642 case kCondGT:
4643 if (IsInt<16>(rhs_imm + 1)) {
4644 // Simulate lhs > rhs via !(lhs < rhs + 1).
4645 __ Slti(TMP, lhs, rhs_imm + 1);
4646 __ Beqz(TMP, label);
4647 } else {
4648 __ LoadConst32(TMP, rhs_imm);
4649 __ Blt(TMP, lhs, label);
4650 }
4651 break;
4652 case kCondB:
4653 if (IsInt<16>(rhs_imm)) {
4654 __ Sltiu(TMP, lhs, rhs_imm);
4655 __ Bnez(TMP, label);
4656 } else {
4657 __ LoadConst32(TMP, rhs_imm);
4658 __ Bltu(lhs, TMP, label);
4659 }
4660 break;
4661 case kCondAE:
4662 if (IsInt<16>(rhs_imm)) {
4663 __ Sltiu(TMP, lhs, rhs_imm);
4664 __ Beqz(TMP, label);
4665 } else {
4666 __ LoadConst32(TMP, rhs_imm);
4667 __ Bgeu(lhs, TMP, label);
4668 }
4669 break;
4670 case kCondBE:
4671 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4672 // Simulate lhs <= rhs via lhs < rhs + 1.
4673 // Note that this only works if rhs + 1 does not overflow
4674 // to 0, hence the check above.
4675 __ Sltiu(TMP, lhs, rhs_imm + 1);
4676 __ Bnez(TMP, label);
4677 } else {
4678 __ LoadConst32(TMP, rhs_imm);
4679 __ Bgeu(TMP, lhs, label);
4680 }
4681 break;
4682 case kCondA:
4683 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4684 // Simulate lhs > rhs via !(lhs < rhs + 1).
4685 // Note that this only works if rhs + 1 does not overflow
4686 // to 0, hence the check above.
4687 __ Sltiu(TMP, lhs, rhs_imm + 1);
4688 __ Beqz(TMP, label);
4689 } else {
4690 __ LoadConst32(TMP, rhs_imm);
4691 __ Bltu(TMP, lhs, label);
4692 }
4693 break;
4694 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004695 }
4696 }
4697}
4698
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004699void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4700 LocationSummary* locations) {
4701 Register dst = locations->Out().AsRegister<Register>();
4702 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4703 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4704 Location rhs_location = locations->InAt(1);
4705 Register rhs_high = ZERO;
4706 Register rhs_low = ZERO;
4707 int64_t imm = 0;
4708 uint32_t imm_high = 0;
4709 uint32_t imm_low = 0;
4710 bool use_imm = rhs_location.IsConstant();
4711 if (use_imm) {
4712 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4713 imm_high = High32Bits(imm);
4714 imm_low = Low32Bits(imm);
4715 } else {
4716 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4717 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4718 }
4719 if (use_imm && imm == 0) {
4720 switch (cond) {
4721 case kCondEQ:
4722 case kCondBE: // <= 0 if zero
4723 __ Or(dst, lhs_high, lhs_low);
4724 __ Sltiu(dst, dst, 1);
4725 break;
4726 case kCondNE:
4727 case kCondA: // > 0 if non-zero
4728 __ Or(dst, lhs_high, lhs_low);
4729 __ Sltu(dst, ZERO, dst);
4730 break;
4731 case kCondLT:
4732 __ Slt(dst, lhs_high, ZERO);
4733 break;
4734 case kCondGE:
4735 __ Slt(dst, lhs_high, ZERO);
4736 __ Xori(dst, dst, 1);
4737 break;
4738 case kCondLE:
4739 __ Or(TMP, lhs_high, lhs_low);
4740 __ Sra(AT, lhs_high, 31);
4741 __ Sltu(dst, AT, TMP);
4742 __ Xori(dst, dst, 1);
4743 break;
4744 case kCondGT:
4745 __ Or(TMP, lhs_high, lhs_low);
4746 __ Sra(AT, lhs_high, 31);
4747 __ Sltu(dst, AT, TMP);
4748 break;
4749 case kCondB: // always false
4750 __ Andi(dst, dst, 0);
4751 break;
4752 case kCondAE: // always true
4753 __ Ori(dst, ZERO, 1);
4754 break;
4755 }
4756 } else if (use_imm) {
4757 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4758 switch (cond) {
4759 case kCondEQ:
4760 __ LoadConst32(TMP, imm_high);
4761 __ Xor(TMP, TMP, lhs_high);
4762 __ LoadConst32(AT, imm_low);
4763 __ Xor(AT, AT, lhs_low);
4764 __ Or(dst, TMP, AT);
4765 __ Sltiu(dst, dst, 1);
4766 break;
4767 case kCondNE:
4768 __ LoadConst32(TMP, imm_high);
4769 __ Xor(TMP, TMP, lhs_high);
4770 __ LoadConst32(AT, imm_low);
4771 __ Xor(AT, AT, lhs_low);
4772 __ Or(dst, TMP, AT);
4773 __ Sltu(dst, ZERO, dst);
4774 break;
4775 case kCondLT:
4776 case kCondGE:
4777 if (dst == lhs_low) {
4778 __ LoadConst32(TMP, imm_low);
4779 __ Sltu(dst, lhs_low, TMP);
4780 }
4781 __ LoadConst32(TMP, imm_high);
4782 __ Slt(AT, lhs_high, TMP);
4783 __ Slt(TMP, TMP, lhs_high);
4784 if (dst != lhs_low) {
4785 __ LoadConst32(dst, imm_low);
4786 __ Sltu(dst, lhs_low, dst);
4787 }
4788 __ Slt(dst, TMP, dst);
4789 __ Or(dst, dst, AT);
4790 if (cond == kCondGE) {
4791 __ Xori(dst, dst, 1);
4792 }
4793 break;
4794 case kCondGT:
4795 case kCondLE:
4796 if (dst == lhs_low) {
4797 __ LoadConst32(TMP, imm_low);
4798 __ Sltu(dst, TMP, lhs_low);
4799 }
4800 __ LoadConst32(TMP, imm_high);
4801 __ Slt(AT, TMP, lhs_high);
4802 __ Slt(TMP, lhs_high, TMP);
4803 if (dst != lhs_low) {
4804 __ LoadConst32(dst, imm_low);
4805 __ Sltu(dst, dst, lhs_low);
4806 }
4807 __ Slt(dst, TMP, dst);
4808 __ Or(dst, dst, AT);
4809 if (cond == kCondLE) {
4810 __ Xori(dst, dst, 1);
4811 }
4812 break;
4813 case kCondB:
4814 case kCondAE:
4815 if (dst == lhs_low) {
4816 __ LoadConst32(TMP, imm_low);
4817 __ Sltu(dst, lhs_low, TMP);
4818 }
4819 __ LoadConst32(TMP, imm_high);
4820 __ Sltu(AT, lhs_high, TMP);
4821 __ Sltu(TMP, TMP, lhs_high);
4822 if (dst != lhs_low) {
4823 __ LoadConst32(dst, imm_low);
4824 __ Sltu(dst, lhs_low, dst);
4825 }
4826 __ Slt(dst, TMP, dst);
4827 __ Or(dst, dst, AT);
4828 if (cond == kCondAE) {
4829 __ Xori(dst, dst, 1);
4830 }
4831 break;
4832 case kCondA:
4833 case kCondBE:
4834 if (dst == lhs_low) {
4835 __ LoadConst32(TMP, imm_low);
4836 __ Sltu(dst, TMP, lhs_low);
4837 }
4838 __ LoadConst32(TMP, imm_high);
4839 __ Sltu(AT, TMP, lhs_high);
4840 __ Sltu(TMP, lhs_high, TMP);
4841 if (dst != lhs_low) {
4842 __ LoadConst32(dst, imm_low);
4843 __ Sltu(dst, dst, lhs_low);
4844 }
4845 __ Slt(dst, TMP, dst);
4846 __ Or(dst, dst, AT);
4847 if (cond == kCondBE) {
4848 __ Xori(dst, dst, 1);
4849 }
4850 break;
4851 }
4852 } else {
4853 switch (cond) {
4854 case kCondEQ:
4855 __ Xor(TMP, lhs_high, rhs_high);
4856 __ Xor(AT, lhs_low, rhs_low);
4857 __ Or(dst, TMP, AT);
4858 __ Sltiu(dst, dst, 1);
4859 break;
4860 case kCondNE:
4861 __ Xor(TMP, lhs_high, rhs_high);
4862 __ Xor(AT, lhs_low, rhs_low);
4863 __ Or(dst, TMP, AT);
4864 __ Sltu(dst, ZERO, dst);
4865 break;
4866 case kCondLT:
4867 case kCondGE:
4868 __ Slt(TMP, rhs_high, lhs_high);
4869 __ Sltu(AT, lhs_low, rhs_low);
4870 __ Slt(TMP, TMP, AT);
4871 __ Slt(AT, lhs_high, rhs_high);
4872 __ Or(dst, AT, TMP);
4873 if (cond == kCondGE) {
4874 __ Xori(dst, dst, 1);
4875 }
4876 break;
4877 case kCondGT:
4878 case kCondLE:
4879 __ Slt(TMP, lhs_high, rhs_high);
4880 __ Sltu(AT, rhs_low, lhs_low);
4881 __ Slt(TMP, TMP, AT);
4882 __ Slt(AT, rhs_high, lhs_high);
4883 __ Or(dst, AT, TMP);
4884 if (cond == kCondLE) {
4885 __ Xori(dst, dst, 1);
4886 }
4887 break;
4888 case kCondB:
4889 case kCondAE:
4890 __ Sltu(TMP, rhs_high, lhs_high);
4891 __ Sltu(AT, lhs_low, rhs_low);
4892 __ Slt(TMP, TMP, AT);
4893 __ Sltu(AT, lhs_high, rhs_high);
4894 __ Or(dst, AT, TMP);
4895 if (cond == kCondAE) {
4896 __ Xori(dst, dst, 1);
4897 }
4898 break;
4899 case kCondA:
4900 case kCondBE:
4901 __ Sltu(TMP, lhs_high, rhs_high);
4902 __ Sltu(AT, rhs_low, lhs_low);
4903 __ Slt(TMP, TMP, AT);
4904 __ Sltu(AT, rhs_high, lhs_high);
4905 __ Or(dst, AT, TMP);
4906 if (cond == kCondBE) {
4907 __ Xori(dst, dst, 1);
4908 }
4909 break;
4910 }
4911 }
4912}
4913
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004914void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4915 LocationSummary* locations,
4916 MipsLabel* label) {
4917 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4918 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4919 Location rhs_location = locations->InAt(1);
4920 Register rhs_high = ZERO;
4921 Register rhs_low = ZERO;
4922 int64_t imm = 0;
4923 uint32_t imm_high = 0;
4924 uint32_t imm_low = 0;
4925 bool use_imm = rhs_location.IsConstant();
4926 if (use_imm) {
4927 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4928 imm_high = High32Bits(imm);
4929 imm_low = Low32Bits(imm);
4930 } else {
4931 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4932 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4933 }
4934
4935 if (use_imm && imm == 0) {
4936 switch (cond) {
4937 case kCondEQ:
4938 case kCondBE: // <= 0 if zero
4939 __ Or(TMP, lhs_high, lhs_low);
4940 __ Beqz(TMP, label);
4941 break;
4942 case kCondNE:
4943 case kCondA: // > 0 if non-zero
4944 __ Or(TMP, lhs_high, lhs_low);
4945 __ Bnez(TMP, label);
4946 break;
4947 case kCondLT:
4948 __ Bltz(lhs_high, label);
4949 break;
4950 case kCondGE:
4951 __ Bgez(lhs_high, label);
4952 break;
4953 case kCondLE:
4954 __ Or(TMP, lhs_high, lhs_low);
4955 __ Sra(AT, lhs_high, 31);
4956 __ Bgeu(AT, TMP, label);
4957 break;
4958 case kCondGT:
4959 __ Or(TMP, lhs_high, lhs_low);
4960 __ Sra(AT, lhs_high, 31);
4961 __ Bltu(AT, TMP, label);
4962 break;
4963 case kCondB: // always false
4964 break;
4965 case kCondAE: // always true
4966 __ B(label);
4967 break;
4968 }
4969 } else if (use_imm) {
4970 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4971 switch (cond) {
4972 case kCondEQ:
4973 __ LoadConst32(TMP, imm_high);
4974 __ Xor(TMP, TMP, lhs_high);
4975 __ LoadConst32(AT, imm_low);
4976 __ Xor(AT, AT, lhs_low);
4977 __ Or(TMP, TMP, AT);
4978 __ Beqz(TMP, label);
4979 break;
4980 case kCondNE:
4981 __ LoadConst32(TMP, imm_high);
4982 __ Xor(TMP, TMP, lhs_high);
4983 __ LoadConst32(AT, imm_low);
4984 __ Xor(AT, AT, lhs_low);
4985 __ Or(TMP, TMP, AT);
4986 __ Bnez(TMP, label);
4987 break;
4988 case kCondLT:
4989 __ LoadConst32(TMP, imm_high);
4990 __ Blt(lhs_high, TMP, label);
4991 __ Slt(TMP, TMP, lhs_high);
4992 __ LoadConst32(AT, imm_low);
4993 __ Sltu(AT, lhs_low, AT);
4994 __ Blt(TMP, AT, label);
4995 break;
4996 case kCondGE:
4997 __ LoadConst32(TMP, imm_high);
4998 __ Blt(TMP, lhs_high, label);
4999 __ Slt(TMP, lhs_high, TMP);
5000 __ LoadConst32(AT, imm_low);
5001 __ Sltu(AT, lhs_low, AT);
5002 __ Or(TMP, TMP, AT);
5003 __ Beqz(TMP, label);
5004 break;
5005 case kCondLE:
5006 __ LoadConst32(TMP, imm_high);
5007 __ Blt(lhs_high, TMP, label);
5008 __ Slt(TMP, TMP, lhs_high);
5009 __ LoadConst32(AT, imm_low);
5010 __ Sltu(AT, AT, lhs_low);
5011 __ Or(TMP, TMP, AT);
5012 __ Beqz(TMP, label);
5013 break;
5014 case kCondGT:
5015 __ LoadConst32(TMP, imm_high);
5016 __ Blt(TMP, lhs_high, label);
5017 __ Slt(TMP, lhs_high, TMP);
5018 __ LoadConst32(AT, imm_low);
5019 __ Sltu(AT, AT, lhs_low);
5020 __ Blt(TMP, AT, label);
5021 break;
5022 case kCondB:
5023 __ LoadConst32(TMP, imm_high);
5024 __ Bltu(lhs_high, TMP, label);
5025 __ Sltu(TMP, TMP, lhs_high);
5026 __ LoadConst32(AT, imm_low);
5027 __ Sltu(AT, lhs_low, AT);
5028 __ Blt(TMP, AT, label);
5029 break;
5030 case kCondAE:
5031 __ LoadConst32(TMP, imm_high);
5032 __ Bltu(TMP, lhs_high, label);
5033 __ Sltu(TMP, lhs_high, TMP);
5034 __ LoadConst32(AT, imm_low);
5035 __ Sltu(AT, lhs_low, AT);
5036 __ Or(TMP, TMP, AT);
5037 __ Beqz(TMP, label);
5038 break;
5039 case kCondBE:
5040 __ LoadConst32(TMP, imm_high);
5041 __ Bltu(lhs_high, TMP, label);
5042 __ Sltu(TMP, TMP, lhs_high);
5043 __ LoadConst32(AT, imm_low);
5044 __ Sltu(AT, AT, lhs_low);
5045 __ Or(TMP, TMP, AT);
5046 __ Beqz(TMP, label);
5047 break;
5048 case kCondA:
5049 __ LoadConst32(TMP, imm_high);
5050 __ Bltu(TMP, lhs_high, label);
5051 __ Sltu(TMP, lhs_high, TMP);
5052 __ LoadConst32(AT, imm_low);
5053 __ Sltu(AT, AT, lhs_low);
5054 __ Blt(TMP, AT, label);
5055 break;
5056 }
5057 } else {
5058 switch (cond) {
5059 case kCondEQ:
5060 __ Xor(TMP, lhs_high, rhs_high);
5061 __ Xor(AT, lhs_low, rhs_low);
5062 __ Or(TMP, TMP, AT);
5063 __ Beqz(TMP, label);
5064 break;
5065 case kCondNE:
5066 __ Xor(TMP, lhs_high, rhs_high);
5067 __ Xor(AT, lhs_low, rhs_low);
5068 __ Or(TMP, TMP, AT);
5069 __ Bnez(TMP, label);
5070 break;
5071 case kCondLT:
5072 __ Blt(lhs_high, rhs_high, label);
5073 __ Slt(TMP, rhs_high, lhs_high);
5074 __ Sltu(AT, lhs_low, rhs_low);
5075 __ Blt(TMP, AT, label);
5076 break;
5077 case kCondGE:
5078 __ Blt(rhs_high, lhs_high, label);
5079 __ Slt(TMP, lhs_high, rhs_high);
5080 __ Sltu(AT, lhs_low, rhs_low);
5081 __ Or(TMP, TMP, AT);
5082 __ Beqz(TMP, label);
5083 break;
5084 case kCondLE:
5085 __ Blt(lhs_high, rhs_high, label);
5086 __ Slt(TMP, rhs_high, lhs_high);
5087 __ Sltu(AT, rhs_low, lhs_low);
5088 __ Or(TMP, TMP, AT);
5089 __ Beqz(TMP, label);
5090 break;
5091 case kCondGT:
5092 __ Blt(rhs_high, lhs_high, label);
5093 __ Slt(TMP, lhs_high, rhs_high);
5094 __ Sltu(AT, rhs_low, lhs_low);
5095 __ Blt(TMP, AT, label);
5096 break;
5097 case kCondB:
5098 __ Bltu(lhs_high, rhs_high, label);
5099 __ Sltu(TMP, rhs_high, lhs_high);
5100 __ Sltu(AT, lhs_low, rhs_low);
5101 __ Blt(TMP, AT, label);
5102 break;
5103 case kCondAE:
5104 __ Bltu(rhs_high, lhs_high, label);
5105 __ Sltu(TMP, lhs_high, rhs_high);
5106 __ Sltu(AT, lhs_low, rhs_low);
5107 __ Or(TMP, TMP, AT);
5108 __ Beqz(TMP, label);
5109 break;
5110 case kCondBE:
5111 __ Bltu(lhs_high, rhs_high, label);
5112 __ Sltu(TMP, rhs_high, lhs_high);
5113 __ Sltu(AT, rhs_low, lhs_low);
5114 __ Or(TMP, TMP, AT);
5115 __ Beqz(TMP, label);
5116 break;
5117 case kCondA:
5118 __ Bltu(rhs_high, lhs_high, label);
5119 __ Sltu(TMP, lhs_high, rhs_high);
5120 __ Sltu(AT, rhs_low, lhs_low);
5121 __ Blt(TMP, AT, label);
5122 break;
5123 }
5124 }
5125}
5126
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005127void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5128 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005129 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005130 LocationSummary* locations) {
5131 Register dst = locations->Out().AsRegister<Register>();
5132 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5133 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5134 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005135 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005136 if (isR6) {
5137 switch (cond) {
5138 case kCondEQ:
5139 __ CmpEqS(FTMP, lhs, rhs);
5140 __ Mfc1(dst, FTMP);
5141 __ Andi(dst, dst, 1);
5142 break;
5143 case kCondNE:
5144 __ CmpEqS(FTMP, lhs, rhs);
5145 __ Mfc1(dst, FTMP);
5146 __ Addiu(dst, dst, 1);
5147 break;
5148 case kCondLT:
5149 if (gt_bias) {
5150 __ CmpLtS(FTMP, lhs, rhs);
5151 } else {
5152 __ CmpUltS(FTMP, lhs, rhs);
5153 }
5154 __ Mfc1(dst, FTMP);
5155 __ Andi(dst, dst, 1);
5156 break;
5157 case kCondLE:
5158 if (gt_bias) {
5159 __ CmpLeS(FTMP, lhs, rhs);
5160 } else {
5161 __ CmpUleS(FTMP, lhs, rhs);
5162 }
5163 __ Mfc1(dst, FTMP);
5164 __ Andi(dst, dst, 1);
5165 break;
5166 case kCondGT:
5167 if (gt_bias) {
5168 __ CmpUltS(FTMP, rhs, lhs);
5169 } else {
5170 __ CmpLtS(FTMP, rhs, lhs);
5171 }
5172 __ Mfc1(dst, FTMP);
5173 __ Andi(dst, dst, 1);
5174 break;
5175 case kCondGE:
5176 if (gt_bias) {
5177 __ CmpUleS(FTMP, rhs, lhs);
5178 } else {
5179 __ CmpLeS(FTMP, rhs, lhs);
5180 }
5181 __ Mfc1(dst, FTMP);
5182 __ Andi(dst, dst, 1);
5183 break;
5184 default:
5185 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5186 UNREACHABLE();
5187 }
5188 } else {
5189 switch (cond) {
5190 case kCondEQ:
5191 __ CeqS(0, lhs, rhs);
5192 __ LoadConst32(dst, 1);
5193 __ Movf(dst, ZERO, 0);
5194 break;
5195 case kCondNE:
5196 __ CeqS(0, lhs, rhs);
5197 __ LoadConst32(dst, 1);
5198 __ Movt(dst, ZERO, 0);
5199 break;
5200 case kCondLT:
5201 if (gt_bias) {
5202 __ ColtS(0, lhs, rhs);
5203 } else {
5204 __ CultS(0, lhs, rhs);
5205 }
5206 __ LoadConst32(dst, 1);
5207 __ Movf(dst, ZERO, 0);
5208 break;
5209 case kCondLE:
5210 if (gt_bias) {
5211 __ ColeS(0, lhs, rhs);
5212 } else {
5213 __ CuleS(0, lhs, rhs);
5214 }
5215 __ LoadConst32(dst, 1);
5216 __ Movf(dst, ZERO, 0);
5217 break;
5218 case kCondGT:
5219 if (gt_bias) {
5220 __ CultS(0, rhs, lhs);
5221 } else {
5222 __ ColtS(0, rhs, lhs);
5223 }
5224 __ LoadConst32(dst, 1);
5225 __ Movf(dst, ZERO, 0);
5226 break;
5227 case kCondGE:
5228 if (gt_bias) {
5229 __ CuleS(0, rhs, lhs);
5230 } else {
5231 __ ColeS(0, rhs, lhs);
5232 }
5233 __ LoadConst32(dst, 1);
5234 __ Movf(dst, ZERO, 0);
5235 break;
5236 default:
5237 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5238 UNREACHABLE();
5239 }
5240 }
5241 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005242 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005243 if (isR6) {
5244 switch (cond) {
5245 case kCondEQ:
5246 __ CmpEqD(FTMP, lhs, rhs);
5247 __ Mfc1(dst, FTMP);
5248 __ Andi(dst, dst, 1);
5249 break;
5250 case kCondNE:
5251 __ CmpEqD(FTMP, lhs, rhs);
5252 __ Mfc1(dst, FTMP);
5253 __ Addiu(dst, dst, 1);
5254 break;
5255 case kCondLT:
5256 if (gt_bias) {
5257 __ CmpLtD(FTMP, lhs, rhs);
5258 } else {
5259 __ CmpUltD(FTMP, lhs, rhs);
5260 }
5261 __ Mfc1(dst, FTMP);
5262 __ Andi(dst, dst, 1);
5263 break;
5264 case kCondLE:
5265 if (gt_bias) {
5266 __ CmpLeD(FTMP, lhs, rhs);
5267 } else {
5268 __ CmpUleD(FTMP, lhs, rhs);
5269 }
5270 __ Mfc1(dst, FTMP);
5271 __ Andi(dst, dst, 1);
5272 break;
5273 case kCondGT:
5274 if (gt_bias) {
5275 __ CmpUltD(FTMP, rhs, lhs);
5276 } else {
5277 __ CmpLtD(FTMP, rhs, lhs);
5278 }
5279 __ Mfc1(dst, FTMP);
5280 __ Andi(dst, dst, 1);
5281 break;
5282 case kCondGE:
5283 if (gt_bias) {
5284 __ CmpUleD(FTMP, rhs, lhs);
5285 } else {
5286 __ CmpLeD(FTMP, rhs, lhs);
5287 }
5288 __ Mfc1(dst, FTMP);
5289 __ Andi(dst, dst, 1);
5290 break;
5291 default:
5292 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5293 UNREACHABLE();
5294 }
5295 } else {
5296 switch (cond) {
5297 case kCondEQ:
5298 __ CeqD(0, lhs, rhs);
5299 __ LoadConst32(dst, 1);
5300 __ Movf(dst, ZERO, 0);
5301 break;
5302 case kCondNE:
5303 __ CeqD(0, lhs, rhs);
5304 __ LoadConst32(dst, 1);
5305 __ Movt(dst, ZERO, 0);
5306 break;
5307 case kCondLT:
5308 if (gt_bias) {
5309 __ ColtD(0, lhs, rhs);
5310 } else {
5311 __ CultD(0, lhs, rhs);
5312 }
5313 __ LoadConst32(dst, 1);
5314 __ Movf(dst, ZERO, 0);
5315 break;
5316 case kCondLE:
5317 if (gt_bias) {
5318 __ ColeD(0, lhs, rhs);
5319 } else {
5320 __ CuleD(0, lhs, rhs);
5321 }
5322 __ LoadConst32(dst, 1);
5323 __ Movf(dst, ZERO, 0);
5324 break;
5325 case kCondGT:
5326 if (gt_bias) {
5327 __ CultD(0, rhs, lhs);
5328 } else {
5329 __ ColtD(0, rhs, lhs);
5330 }
5331 __ LoadConst32(dst, 1);
5332 __ Movf(dst, ZERO, 0);
5333 break;
5334 case kCondGE:
5335 if (gt_bias) {
5336 __ CuleD(0, rhs, lhs);
5337 } else {
5338 __ ColeD(0, rhs, lhs);
5339 }
5340 __ LoadConst32(dst, 1);
5341 __ Movf(dst, ZERO, 0);
5342 break;
5343 default:
5344 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5345 UNREACHABLE();
5346 }
5347 }
5348 }
5349}
5350
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005351bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5352 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005353 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005354 LocationSummary* input_locations,
5355 int cc) {
5356 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5357 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5358 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005359 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005360 switch (cond) {
5361 case kCondEQ:
5362 __ CeqS(cc, lhs, rhs);
5363 return false;
5364 case kCondNE:
5365 __ CeqS(cc, lhs, rhs);
5366 return true;
5367 case kCondLT:
5368 if (gt_bias) {
5369 __ ColtS(cc, lhs, rhs);
5370 } else {
5371 __ CultS(cc, lhs, rhs);
5372 }
5373 return false;
5374 case kCondLE:
5375 if (gt_bias) {
5376 __ ColeS(cc, lhs, rhs);
5377 } else {
5378 __ CuleS(cc, lhs, rhs);
5379 }
5380 return false;
5381 case kCondGT:
5382 if (gt_bias) {
5383 __ CultS(cc, rhs, lhs);
5384 } else {
5385 __ ColtS(cc, rhs, lhs);
5386 }
5387 return false;
5388 case kCondGE:
5389 if (gt_bias) {
5390 __ CuleS(cc, rhs, lhs);
5391 } else {
5392 __ ColeS(cc, rhs, lhs);
5393 }
5394 return false;
5395 default:
5396 LOG(FATAL) << "Unexpected non-floating-point condition";
5397 UNREACHABLE();
5398 }
5399 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005400 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005401 switch (cond) {
5402 case kCondEQ:
5403 __ CeqD(cc, lhs, rhs);
5404 return false;
5405 case kCondNE:
5406 __ CeqD(cc, lhs, rhs);
5407 return true;
5408 case kCondLT:
5409 if (gt_bias) {
5410 __ ColtD(cc, lhs, rhs);
5411 } else {
5412 __ CultD(cc, lhs, rhs);
5413 }
5414 return false;
5415 case kCondLE:
5416 if (gt_bias) {
5417 __ ColeD(cc, lhs, rhs);
5418 } else {
5419 __ CuleD(cc, lhs, rhs);
5420 }
5421 return false;
5422 case kCondGT:
5423 if (gt_bias) {
5424 __ CultD(cc, rhs, lhs);
5425 } else {
5426 __ ColtD(cc, rhs, lhs);
5427 }
5428 return false;
5429 case kCondGE:
5430 if (gt_bias) {
5431 __ CuleD(cc, rhs, lhs);
5432 } else {
5433 __ ColeD(cc, rhs, lhs);
5434 }
5435 return false;
5436 default:
5437 LOG(FATAL) << "Unexpected non-floating-point condition";
5438 UNREACHABLE();
5439 }
5440 }
5441}
5442
5443bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5444 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005445 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005446 LocationSummary* input_locations,
5447 FRegister dst) {
5448 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5449 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5450 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005451 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005452 switch (cond) {
5453 case kCondEQ:
5454 __ CmpEqS(dst, lhs, rhs);
5455 return false;
5456 case kCondNE:
5457 __ CmpEqS(dst, lhs, rhs);
5458 return true;
5459 case kCondLT:
5460 if (gt_bias) {
5461 __ CmpLtS(dst, lhs, rhs);
5462 } else {
5463 __ CmpUltS(dst, lhs, rhs);
5464 }
5465 return false;
5466 case kCondLE:
5467 if (gt_bias) {
5468 __ CmpLeS(dst, lhs, rhs);
5469 } else {
5470 __ CmpUleS(dst, lhs, rhs);
5471 }
5472 return false;
5473 case kCondGT:
5474 if (gt_bias) {
5475 __ CmpUltS(dst, rhs, lhs);
5476 } else {
5477 __ CmpLtS(dst, rhs, lhs);
5478 }
5479 return false;
5480 case kCondGE:
5481 if (gt_bias) {
5482 __ CmpUleS(dst, rhs, lhs);
5483 } else {
5484 __ CmpLeS(dst, rhs, lhs);
5485 }
5486 return false;
5487 default:
5488 LOG(FATAL) << "Unexpected non-floating-point condition";
5489 UNREACHABLE();
5490 }
5491 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005492 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005493 switch (cond) {
5494 case kCondEQ:
5495 __ CmpEqD(dst, lhs, rhs);
5496 return false;
5497 case kCondNE:
5498 __ CmpEqD(dst, lhs, rhs);
5499 return true;
5500 case kCondLT:
5501 if (gt_bias) {
5502 __ CmpLtD(dst, lhs, rhs);
5503 } else {
5504 __ CmpUltD(dst, lhs, rhs);
5505 }
5506 return false;
5507 case kCondLE:
5508 if (gt_bias) {
5509 __ CmpLeD(dst, lhs, rhs);
5510 } else {
5511 __ CmpUleD(dst, lhs, rhs);
5512 }
5513 return false;
5514 case kCondGT:
5515 if (gt_bias) {
5516 __ CmpUltD(dst, rhs, lhs);
5517 } else {
5518 __ CmpLtD(dst, rhs, lhs);
5519 }
5520 return false;
5521 case kCondGE:
5522 if (gt_bias) {
5523 __ CmpUleD(dst, rhs, lhs);
5524 } else {
5525 __ CmpLeD(dst, rhs, lhs);
5526 }
5527 return false;
5528 default:
5529 LOG(FATAL) << "Unexpected non-floating-point condition";
5530 UNREACHABLE();
5531 }
5532 }
5533}
5534
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005535void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5536 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005537 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005538 LocationSummary* locations,
5539 MipsLabel* label) {
5540 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5541 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5542 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005543 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005544 if (isR6) {
5545 switch (cond) {
5546 case kCondEQ:
5547 __ CmpEqS(FTMP, lhs, rhs);
5548 __ Bc1nez(FTMP, label);
5549 break;
5550 case kCondNE:
5551 __ CmpEqS(FTMP, lhs, rhs);
5552 __ Bc1eqz(FTMP, label);
5553 break;
5554 case kCondLT:
5555 if (gt_bias) {
5556 __ CmpLtS(FTMP, lhs, rhs);
5557 } else {
5558 __ CmpUltS(FTMP, lhs, rhs);
5559 }
5560 __ Bc1nez(FTMP, label);
5561 break;
5562 case kCondLE:
5563 if (gt_bias) {
5564 __ CmpLeS(FTMP, lhs, rhs);
5565 } else {
5566 __ CmpUleS(FTMP, lhs, rhs);
5567 }
5568 __ Bc1nez(FTMP, label);
5569 break;
5570 case kCondGT:
5571 if (gt_bias) {
5572 __ CmpUltS(FTMP, rhs, lhs);
5573 } else {
5574 __ CmpLtS(FTMP, rhs, lhs);
5575 }
5576 __ Bc1nez(FTMP, label);
5577 break;
5578 case kCondGE:
5579 if (gt_bias) {
5580 __ CmpUleS(FTMP, rhs, lhs);
5581 } else {
5582 __ CmpLeS(FTMP, rhs, lhs);
5583 }
5584 __ Bc1nez(FTMP, label);
5585 break;
5586 default:
5587 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005588 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005589 }
5590 } else {
5591 switch (cond) {
5592 case kCondEQ:
5593 __ CeqS(0, lhs, rhs);
5594 __ Bc1t(0, label);
5595 break;
5596 case kCondNE:
5597 __ CeqS(0, lhs, rhs);
5598 __ Bc1f(0, label);
5599 break;
5600 case kCondLT:
5601 if (gt_bias) {
5602 __ ColtS(0, lhs, rhs);
5603 } else {
5604 __ CultS(0, lhs, rhs);
5605 }
5606 __ Bc1t(0, label);
5607 break;
5608 case kCondLE:
5609 if (gt_bias) {
5610 __ ColeS(0, lhs, rhs);
5611 } else {
5612 __ CuleS(0, lhs, rhs);
5613 }
5614 __ Bc1t(0, label);
5615 break;
5616 case kCondGT:
5617 if (gt_bias) {
5618 __ CultS(0, rhs, lhs);
5619 } else {
5620 __ ColtS(0, rhs, lhs);
5621 }
5622 __ Bc1t(0, label);
5623 break;
5624 case kCondGE:
5625 if (gt_bias) {
5626 __ CuleS(0, rhs, lhs);
5627 } else {
5628 __ ColeS(0, rhs, lhs);
5629 }
5630 __ Bc1t(0, label);
5631 break;
5632 default:
5633 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005634 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005635 }
5636 }
5637 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005638 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005639 if (isR6) {
5640 switch (cond) {
5641 case kCondEQ:
5642 __ CmpEqD(FTMP, lhs, rhs);
5643 __ Bc1nez(FTMP, label);
5644 break;
5645 case kCondNE:
5646 __ CmpEqD(FTMP, lhs, rhs);
5647 __ Bc1eqz(FTMP, label);
5648 break;
5649 case kCondLT:
5650 if (gt_bias) {
5651 __ CmpLtD(FTMP, lhs, rhs);
5652 } else {
5653 __ CmpUltD(FTMP, lhs, rhs);
5654 }
5655 __ Bc1nez(FTMP, label);
5656 break;
5657 case kCondLE:
5658 if (gt_bias) {
5659 __ CmpLeD(FTMP, lhs, rhs);
5660 } else {
5661 __ CmpUleD(FTMP, lhs, rhs);
5662 }
5663 __ Bc1nez(FTMP, label);
5664 break;
5665 case kCondGT:
5666 if (gt_bias) {
5667 __ CmpUltD(FTMP, rhs, lhs);
5668 } else {
5669 __ CmpLtD(FTMP, rhs, lhs);
5670 }
5671 __ Bc1nez(FTMP, label);
5672 break;
5673 case kCondGE:
5674 if (gt_bias) {
5675 __ CmpUleD(FTMP, rhs, lhs);
5676 } else {
5677 __ CmpLeD(FTMP, rhs, lhs);
5678 }
5679 __ Bc1nez(FTMP, label);
5680 break;
5681 default:
5682 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005683 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005684 }
5685 } else {
5686 switch (cond) {
5687 case kCondEQ:
5688 __ CeqD(0, lhs, rhs);
5689 __ Bc1t(0, label);
5690 break;
5691 case kCondNE:
5692 __ CeqD(0, lhs, rhs);
5693 __ Bc1f(0, label);
5694 break;
5695 case kCondLT:
5696 if (gt_bias) {
5697 __ ColtD(0, lhs, rhs);
5698 } else {
5699 __ CultD(0, lhs, rhs);
5700 }
5701 __ Bc1t(0, label);
5702 break;
5703 case kCondLE:
5704 if (gt_bias) {
5705 __ ColeD(0, lhs, rhs);
5706 } else {
5707 __ CuleD(0, lhs, rhs);
5708 }
5709 __ Bc1t(0, label);
5710 break;
5711 case kCondGT:
5712 if (gt_bias) {
5713 __ CultD(0, rhs, lhs);
5714 } else {
5715 __ ColtD(0, rhs, lhs);
5716 }
5717 __ Bc1t(0, label);
5718 break;
5719 case kCondGE:
5720 if (gt_bias) {
5721 __ CuleD(0, rhs, lhs);
5722 } else {
5723 __ ColeD(0, rhs, lhs);
5724 }
5725 __ Bc1t(0, label);
5726 break;
5727 default:
5728 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005729 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005730 }
5731 }
5732 }
5733}
5734
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005735void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005736 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005737 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005738 MipsLabel* false_target) {
5739 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005740
David Brazdil0debae72015-11-12 18:37:00 +00005741 if (true_target == nullptr && false_target == nullptr) {
5742 // Nothing to do. The code always falls through.
5743 return;
5744 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005745 // Constant condition, statically compared against "true" (integer value 1).
5746 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005747 if (true_target != nullptr) {
5748 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005749 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005750 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005751 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005752 if (false_target != nullptr) {
5753 __ B(false_target);
5754 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005755 }
David Brazdil0debae72015-11-12 18:37:00 +00005756 return;
5757 }
5758
5759 // The following code generates these patterns:
5760 // (1) true_target == nullptr && false_target != nullptr
5761 // - opposite condition true => branch to false_target
5762 // (2) true_target != nullptr && false_target == nullptr
5763 // - condition true => branch to true_target
5764 // (3) true_target != nullptr && false_target != nullptr
5765 // - condition true => branch to true_target
5766 // - branch to false_target
5767 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005768 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005769 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005770 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005771 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005772 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5773 } else {
5774 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5775 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005776 } else {
5777 // The condition instruction has not been materialized, use its inputs as
5778 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005779 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005780 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005781 LocationSummary* locations = cond->GetLocations();
5782 IfCondition if_cond = condition->GetCondition();
5783 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005784
David Brazdil0debae72015-11-12 18:37:00 +00005785 if (true_target == nullptr) {
5786 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005787 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005788 }
5789
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005790 switch (type) {
5791 default:
5792 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5793 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005794 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005795 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5796 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005797 case DataType::Type::kFloat32:
5798 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005799 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5800 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005801 }
5802 }
David Brazdil0debae72015-11-12 18:37:00 +00005803
5804 // If neither branch falls through (case 3), the conditional branch to `true_target`
5805 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5806 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005807 __ B(false_target);
5808 }
5809}
5810
5811void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005812 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005813 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005814 locations->SetInAt(0, Location::RequiresRegister());
5815 }
5816}
5817
5818void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005819 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5820 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5821 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5822 nullptr : codegen_->GetLabelOf(true_successor);
5823 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5824 nullptr : codegen_->GetLabelOf(false_successor);
5825 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005826}
5827
5828void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005829 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005830 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005831 InvokeRuntimeCallingConvention calling_convention;
5832 RegisterSet caller_saves = RegisterSet::Empty();
5833 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5834 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005835 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005836 locations->SetInAt(0, Location::RequiresRegister());
5837 }
5838}
5839
5840void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005841 SlowPathCodeMIPS* slow_path =
5842 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005843 GenerateTestAndBranch(deoptimize,
5844 /* condition_input_index */ 0,
5845 slow_path->GetEntryLabel(),
5846 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005847}
5848
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005849// This function returns true if a conditional move can be generated for HSelect.
5850// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5851// branches and regular moves.
5852//
5853// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5854//
5855// While determining feasibility of a conditional move and setting inputs/outputs
5856// are two distinct tasks, this function does both because they share quite a bit
5857// of common logic.
5858static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5859 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5860 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5861 HCondition* condition = cond->AsCondition();
5862
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005863 DataType::Type cond_type =
5864 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5865 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005866
5867 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5868 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5869 bool is_true_value_zero_constant =
5870 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5871 bool is_false_value_zero_constant =
5872 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5873
5874 bool can_move_conditionally = false;
5875 bool use_const_for_false_in = false;
5876 bool use_const_for_true_in = false;
5877
5878 if (!cond->IsConstant()) {
5879 switch (cond_type) {
5880 default:
5881 switch (dst_type) {
5882 default:
5883 // Moving int on int condition.
5884 if (is_r6) {
5885 if (is_true_value_zero_constant) {
5886 // seleqz out_reg, false_reg, cond_reg
5887 can_move_conditionally = true;
5888 use_const_for_true_in = true;
5889 } else if (is_false_value_zero_constant) {
5890 // selnez out_reg, true_reg, cond_reg
5891 can_move_conditionally = true;
5892 use_const_for_false_in = true;
5893 } else if (materialized) {
5894 // Not materializing unmaterialized int conditions
5895 // to keep the instruction count low.
5896 // selnez AT, true_reg, cond_reg
5897 // seleqz TMP, false_reg, cond_reg
5898 // or out_reg, AT, TMP
5899 can_move_conditionally = true;
5900 }
5901 } else {
5902 // movn out_reg, true_reg/ZERO, cond_reg
5903 can_move_conditionally = true;
5904 use_const_for_true_in = is_true_value_zero_constant;
5905 }
5906 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005907 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005908 // Moving long on int condition.
5909 if (is_r6) {
5910 if (is_true_value_zero_constant) {
5911 // seleqz out_reg_lo, false_reg_lo, cond_reg
5912 // seleqz out_reg_hi, false_reg_hi, cond_reg
5913 can_move_conditionally = true;
5914 use_const_for_true_in = true;
5915 } else if (is_false_value_zero_constant) {
5916 // selnez out_reg_lo, true_reg_lo, cond_reg
5917 // selnez out_reg_hi, true_reg_hi, cond_reg
5918 can_move_conditionally = true;
5919 use_const_for_false_in = true;
5920 }
5921 // Other long conditional moves would generate 6+ instructions,
5922 // which is too many.
5923 } else {
5924 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5925 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5926 can_move_conditionally = true;
5927 use_const_for_true_in = is_true_value_zero_constant;
5928 }
5929 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005930 case DataType::Type::kFloat32:
5931 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005932 // Moving float/double on int condition.
5933 if (is_r6) {
5934 if (materialized) {
5935 // Not materializing unmaterialized int conditions
5936 // to keep the instruction count low.
5937 can_move_conditionally = true;
5938 if (is_true_value_zero_constant) {
5939 // sltu TMP, ZERO, cond_reg
5940 // mtc1 TMP, temp_cond_reg
5941 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5942 use_const_for_true_in = true;
5943 } else if (is_false_value_zero_constant) {
5944 // sltu TMP, ZERO, cond_reg
5945 // mtc1 TMP, temp_cond_reg
5946 // selnez.fmt out_reg, true_reg, temp_cond_reg
5947 use_const_for_false_in = true;
5948 } else {
5949 // sltu TMP, ZERO, cond_reg
5950 // mtc1 TMP, temp_cond_reg
5951 // sel.fmt temp_cond_reg, false_reg, true_reg
5952 // mov.fmt out_reg, temp_cond_reg
5953 }
5954 }
5955 } else {
5956 // movn.fmt out_reg, true_reg, cond_reg
5957 can_move_conditionally = true;
5958 }
5959 break;
5960 }
5961 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005962 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005963 // We don't materialize long comparison now
5964 // and use conditional branches instead.
5965 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005966 case DataType::Type::kFloat32:
5967 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005968 switch (dst_type) {
5969 default:
5970 // Moving int on float/double condition.
5971 if (is_r6) {
5972 if (is_true_value_zero_constant) {
5973 // mfc1 TMP, temp_cond_reg
5974 // seleqz out_reg, false_reg, TMP
5975 can_move_conditionally = true;
5976 use_const_for_true_in = true;
5977 } else if (is_false_value_zero_constant) {
5978 // mfc1 TMP, temp_cond_reg
5979 // selnez out_reg, true_reg, TMP
5980 can_move_conditionally = true;
5981 use_const_for_false_in = true;
5982 } else {
5983 // mfc1 TMP, temp_cond_reg
5984 // selnez AT, true_reg, TMP
5985 // seleqz TMP, false_reg, TMP
5986 // or out_reg, AT, TMP
5987 can_move_conditionally = true;
5988 }
5989 } else {
5990 // movt out_reg, true_reg/ZERO, cc
5991 can_move_conditionally = true;
5992 use_const_for_true_in = is_true_value_zero_constant;
5993 }
5994 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005995 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005996 // Moving long on float/double condition.
5997 if (is_r6) {
5998 if (is_true_value_zero_constant) {
5999 // mfc1 TMP, temp_cond_reg
6000 // seleqz out_reg_lo, false_reg_lo, TMP
6001 // seleqz out_reg_hi, false_reg_hi, TMP
6002 can_move_conditionally = true;
6003 use_const_for_true_in = true;
6004 } else if (is_false_value_zero_constant) {
6005 // mfc1 TMP, temp_cond_reg
6006 // selnez out_reg_lo, true_reg_lo, TMP
6007 // selnez out_reg_hi, true_reg_hi, TMP
6008 can_move_conditionally = true;
6009 use_const_for_false_in = true;
6010 }
6011 // Other long conditional moves would generate 6+ instructions,
6012 // which is too many.
6013 } else {
6014 // movt out_reg_lo, true_reg_lo/ZERO, cc
6015 // movt out_reg_hi, true_reg_hi/ZERO, cc
6016 can_move_conditionally = true;
6017 use_const_for_true_in = is_true_value_zero_constant;
6018 }
6019 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006020 case DataType::Type::kFloat32:
6021 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006022 // Moving float/double on float/double condition.
6023 if (is_r6) {
6024 can_move_conditionally = true;
6025 if (is_true_value_zero_constant) {
6026 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6027 use_const_for_true_in = true;
6028 } else if (is_false_value_zero_constant) {
6029 // selnez.fmt out_reg, true_reg, temp_cond_reg
6030 use_const_for_false_in = true;
6031 } else {
6032 // sel.fmt temp_cond_reg, false_reg, true_reg
6033 // mov.fmt out_reg, temp_cond_reg
6034 }
6035 } else {
6036 // movt.fmt out_reg, true_reg, cc
6037 can_move_conditionally = true;
6038 }
6039 break;
6040 }
6041 break;
6042 }
6043 }
6044
6045 if (can_move_conditionally) {
6046 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6047 } else {
6048 DCHECK(!use_const_for_false_in);
6049 DCHECK(!use_const_for_true_in);
6050 }
6051
6052 if (locations_to_set != nullptr) {
6053 if (use_const_for_false_in) {
6054 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6055 } else {
6056 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006057 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006058 ? Location::RequiresFpuRegister()
6059 : Location::RequiresRegister());
6060 }
6061 if (use_const_for_true_in) {
6062 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6063 } else {
6064 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006065 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006066 ? Location::RequiresFpuRegister()
6067 : Location::RequiresRegister());
6068 }
6069 if (materialized) {
6070 locations_to_set->SetInAt(2, Location::RequiresRegister());
6071 }
6072 // On R6 we don't require the output to be the same as the
6073 // first input for conditional moves unlike on R2.
6074 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6075 if (is_out_same_as_first_in) {
6076 locations_to_set->SetOut(Location::SameAsFirstInput());
6077 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006078 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006079 ? Location::RequiresFpuRegister()
6080 : Location::RequiresRegister());
6081 }
6082 }
6083
6084 return can_move_conditionally;
6085}
6086
6087void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6088 LocationSummary* locations = select->GetLocations();
6089 Location dst = locations->Out();
6090 Location src = locations->InAt(1);
6091 Register src_reg = ZERO;
6092 Register src_reg_high = ZERO;
6093 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6094 Register cond_reg = TMP;
6095 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006096 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006097 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006098 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006099
6100 if (IsBooleanValueOrMaterializedCondition(cond)) {
6101 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6102 } else {
6103 HCondition* condition = cond->AsCondition();
6104 LocationSummary* cond_locations = cond->GetLocations();
6105 IfCondition if_cond = condition->GetCondition();
6106 cond_type = condition->InputAt(0)->GetType();
6107 switch (cond_type) {
6108 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006109 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006110 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6111 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006112 case DataType::Type::kFloat32:
6113 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006114 cond_inverted = MaterializeFpCompareR2(if_cond,
6115 condition->IsGtBias(),
6116 cond_type,
6117 cond_locations,
6118 cond_cc);
6119 break;
6120 }
6121 }
6122
6123 DCHECK(dst.Equals(locations->InAt(0)));
6124 if (src.IsRegister()) {
6125 src_reg = src.AsRegister<Register>();
6126 } else if (src.IsRegisterPair()) {
6127 src_reg = src.AsRegisterPairLow<Register>();
6128 src_reg_high = src.AsRegisterPairHigh<Register>();
6129 } else if (src.IsConstant()) {
6130 DCHECK(src.GetConstant()->IsZeroBitPattern());
6131 }
6132
6133 switch (cond_type) {
6134 default:
6135 switch (dst_type) {
6136 default:
6137 if (cond_inverted) {
6138 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6139 } else {
6140 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6141 }
6142 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006143 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006144 if (cond_inverted) {
6145 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6146 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6147 } else {
6148 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6149 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6150 }
6151 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006152 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006153 if (cond_inverted) {
6154 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6155 } else {
6156 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6157 }
6158 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006159 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006160 if (cond_inverted) {
6161 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6162 } else {
6163 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6164 }
6165 break;
6166 }
6167 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006168 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006169 LOG(FATAL) << "Unreachable";
6170 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006171 case DataType::Type::kFloat32:
6172 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006173 switch (dst_type) {
6174 default:
6175 if (cond_inverted) {
6176 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6177 } else {
6178 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6179 }
6180 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006181 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006182 if (cond_inverted) {
6183 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6184 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6185 } else {
6186 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6187 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6188 }
6189 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006190 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006191 if (cond_inverted) {
6192 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6193 } else {
6194 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6195 }
6196 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006197 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006198 if (cond_inverted) {
6199 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6200 } else {
6201 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6202 }
6203 break;
6204 }
6205 break;
6206 }
6207}
6208
6209void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6210 LocationSummary* locations = select->GetLocations();
6211 Location dst = locations->Out();
6212 Location false_src = locations->InAt(0);
6213 Location true_src = locations->InAt(1);
6214 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6215 Register cond_reg = TMP;
6216 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006217 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006218 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006219 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006220
6221 if (IsBooleanValueOrMaterializedCondition(cond)) {
6222 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6223 } else {
6224 HCondition* condition = cond->AsCondition();
6225 LocationSummary* cond_locations = cond->GetLocations();
6226 IfCondition if_cond = condition->GetCondition();
6227 cond_type = condition->InputAt(0)->GetType();
6228 switch (cond_type) {
6229 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006230 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006231 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6232 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006233 case DataType::Type::kFloat32:
6234 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006235 cond_inverted = MaterializeFpCompareR6(if_cond,
6236 condition->IsGtBias(),
6237 cond_type,
6238 cond_locations,
6239 fcond_reg);
6240 break;
6241 }
6242 }
6243
6244 if (true_src.IsConstant()) {
6245 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6246 }
6247 if (false_src.IsConstant()) {
6248 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6249 }
6250
6251 switch (dst_type) {
6252 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006253 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006254 __ Mfc1(cond_reg, fcond_reg);
6255 }
6256 if (true_src.IsConstant()) {
6257 if (cond_inverted) {
6258 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6259 } else {
6260 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6261 }
6262 } else if (false_src.IsConstant()) {
6263 if (cond_inverted) {
6264 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6265 } else {
6266 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6267 }
6268 } else {
6269 DCHECK_NE(cond_reg, AT);
6270 if (cond_inverted) {
6271 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6272 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6273 } else {
6274 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6275 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6276 }
6277 __ Or(dst.AsRegister<Register>(), AT, TMP);
6278 }
6279 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006280 case DataType::Type::kInt64: {
6281 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006282 __ Mfc1(cond_reg, fcond_reg);
6283 }
6284 Register dst_lo = dst.AsRegisterPairLow<Register>();
6285 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6286 if (true_src.IsConstant()) {
6287 Register src_lo = false_src.AsRegisterPairLow<Register>();
6288 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6289 if (cond_inverted) {
6290 __ Selnez(dst_lo, src_lo, cond_reg);
6291 __ Selnez(dst_hi, src_hi, cond_reg);
6292 } else {
6293 __ Seleqz(dst_lo, src_lo, cond_reg);
6294 __ Seleqz(dst_hi, src_hi, cond_reg);
6295 }
6296 } else {
6297 DCHECK(false_src.IsConstant());
6298 Register src_lo = true_src.AsRegisterPairLow<Register>();
6299 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6300 if (cond_inverted) {
6301 __ Seleqz(dst_lo, src_lo, cond_reg);
6302 __ Seleqz(dst_hi, src_hi, cond_reg);
6303 } else {
6304 __ Selnez(dst_lo, src_lo, cond_reg);
6305 __ Selnez(dst_hi, src_hi, cond_reg);
6306 }
6307 }
6308 break;
6309 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006310 case DataType::Type::kFloat32: {
6311 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006312 // sel*.fmt tests bit 0 of the condition register, account for that.
6313 __ Sltu(TMP, ZERO, cond_reg);
6314 __ Mtc1(TMP, fcond_reg);
6315 }
6316 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6317 if (true_src.IsConstant()) {
6318 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6319 if (cond_inverted) {
6320 __ SelnezS(dst_reg, src_reg, fcond_reg);
6321 } else {
6322 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6323 }
6324 } else if (false_src.IsConstant()) {
6325 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6326 if (cond_inverted) {
6327 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6328 } else {
6329 __ SelnezS(dst_reg, src_reg, fcond_reg);
6330 }
6331 } else {
6332 if (cond_inverted) {
6333 __ SelS(fcond_reg,
6334 true_src.AsFpuRegister<FRegister>(),
6335 false_src.AsFpuRegister<FRegister>());
6336 } else {
6337 __ SelS(fcond_reg,
6338 false_src.AsFpuRegister<FRegister>(),
6339 true_src.AsFpuRegister<FRegister>());
6340 }
6341 __ MovS(dst_reg, fcond_reg);
6342 }
6343 break;
6344 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006345 case DataType::Type::kFloat64: {
6346 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006347 // sel*.fmt tests bit 0 of the condition register, account for that.
6348 __ Sltu(TMP, ZERO, cond_reg);
6349 __ Mtc1(TMP, fcond_reg);
6350 }
6351 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6352 if (true_src.IsConstant()) {
6353 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6354 if (cond_inverted) {
6355 __ SelnezD(dst_reg, src_reg, fcond_reg);
6356 } else {
6357 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6358 }
6359 } else if (false_src.IsConstant()) {
6360 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6361 if (cond_inverted) {
6362 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6363 } else {
6364 __ SelnezD(dst_reg, src_reg, fcond_reg);
6365 }
6366 } else {
6367 if (cond_inverted) {
6368 __ SelD(fcond_reg,
6369 true_src.AsFpuRegister<FRegister>(),
6370 false_src.AsFpuRegister<FRegister>());
6371 } else {
6372 __ SelD(fcond_reg,
6373 false_src.AsFpuRegister<FRegister>(),
6374 true_src.AsFpuRegister<FRegister>());
6375 }
6376 __ MovD(dst_reg, fcond_reg);
6377 }
6378 break;
6379 }
6380 }
6381}
6382
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006383void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006384 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006385 LocationSummary(flag, LocationSummary::kNoCall);
6386 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006387}
6388
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006389void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6390 __ LoadFromOffset(kLoadWord,
6391 flag->GetLocations()->Out().AsRegister<Register>(),
6392 SP,
6393 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006394}
6395
David Brazdil74eb1b22015-12-14 11:44:01 +00006396void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006397 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006398 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006399}
6400
6401void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006402 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6403 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6404 if (is_r6) {
6405 GenConditionalMoveR6(select);
6406 } else {
6407 GenConditionalMoveR2(select);
6408 }
6409 } else {
6410 LocationSummary* locations = select->GetLocations();
6411 MipsLabel false_target;
6412 GenerateTestAndBranch(select,
6413 /* condition_input_index */ 2,
6414 /* true_target */ nullptr,
6415 &false_target);
6416 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6417 __ Bind(&false_target);
6418 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006419}
6420
David Srbecky0cf44932015-12-09 14:09:59 +00006421void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006422 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006423}
6424
David Srbeckyd28f4a02016-03-14 17:14:24 +00006425void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6426 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006427}
6428
6429void CodeGeneratorMIPS::GenerateNop() {
6430 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006431}
6432
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006433void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006434 DataType::Type field_type = field_info.GetFieldType();
6435 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006436 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006437 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006438 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006439 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006440 instruction,
6441 generate_volatile
6442 ? LocationSummary::kCallOnMainOnly
6443 : (object_field_get_with_read_barrier
6444 ? LocationSummary::kCallOnSlowPath
6445 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006446
Alexey Frunzec61c0762017-04-10 13:54:23 -07006447 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6448 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6449 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006450 locations->SetInAt(0, Location::RequiresRegister());
6451 if (generate_volatile) {
6452 InvokeRuntimeCallingConvention calling_convention;
6453 // need A0 to hold base + offset
6454 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006455 if (field_type == DataType::Type::kInt64) {
6456 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006457 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006458 // Use Location::Any() to prevent situations when running out of available fp registers.
6459 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006460 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006461 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006462 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6463 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6464 }
6465 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006466 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006467 locations->SetOut(Location::RequiresFpuRegister());
6468 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006469 // The output overlaps in the case of an object field get with
6470 // read barriers enabled: we do not want the move to overwrite the
6471 // object's location, as we need it to emit the read barrier.
6472 locations->SetOut(Location::RequiresRegister(),
6473 object_field_get_with_read_barrier
6474 ? Location::kOutputOverlap
6475 : Location::kNoOutputOverlap);
6476 }
6477 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6478 // We need a temporary register for the read barrier marking slow
6479 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006480 if (!kBakerReadBarrierThunksEnableForFields) {
6481 locations->AddTemp(Location::RequiresRegister());
6482 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006483 }
6484 }
6485}
6486
6487void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6488 const FieldInfo& field_info,
6489 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006490 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6491 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006492 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006493 Location obj_loc = locations->InAt(0);
6494 Register obj = obj_loc.AsRegister<Register>();
6495 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006496 LoadOperandType load_type = kLoadUnsignedByte;
6497 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006498 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006499 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006500
6501 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006502 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006503 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006504 load_type = kLoadUnsignedByte;
6505 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006506 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006507 load_type = kLoadSignedByte;
6508 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006509 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006510 load_type = kLoadUnsignedHalfword;
6511 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006512 case DataType::Type::kInt16:
6513 load_type = kLoadSignedHalfword;
6514 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006515 case DataType::Type::kInt32:
6516 case DataType::Type::kFloat32:
6517 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006518 load_type = kLoadWord;
6519 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006520 case DataType::Type::kInt64:
6521 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006522 load_type = kLoadDoubleword;
6523 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006524 case DataType::Type::kUint32:
6525 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006526 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006527 LOG(FATAL) << "Unreachable type " << type;
6528 UNREACHABLE();
6529 }
6530
6531 if (is_volatile && load_type == kLoadDoubleword) {
6532 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006533 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006534 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006535 __ LoadFromOffset(kLoadWord,
6536 ZERO,
6537 locations->GetTemp(0).AsRegister<Register>(),
6538 0,
6539 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006540 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006541 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006542 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006543 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006544 if (dst_loc.IsFpuRegister()) {
6545 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006546 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006547 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006548 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006549 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006550 __ StoreToOffset(kStoreWord,
6551 locations->GetTemp(1).AsRegister<Register>(),
6552 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006553 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006554 __ StoreToOffset(kStoreWord,
6555 locations->GetTemp(2).AsRegister<Register>(),
6556 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006557 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006558 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006559 }
6560 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006561 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006562 // /* HeapReference<Object> */ dst = *(obj + offset)
6563 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006564 Location temp_loc =
6565 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006566 // Note that a potential implicit null check is handled in this
6567 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6568 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6569 dst_loc,
6570 obj,
6571 offset,
6572 temp_loc,
6573 /* needs_null_check */ true);
6574 if (is_volatile) {
6575 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6576 }
6577 } else {
6578 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6579 if (is_volatile) {
6580 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6581 }
6582 // If read barriers are enabled, emit read barriers other than
6583 // Baker's using a slow path (and also unpoison the loaded
6584 // reference, if heap poisoning is enabled).
6585 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6586 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006587 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006588 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006589 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006590 DCHECK(dst_loc.IsRegisterPair());
6591 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006592 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006593 DCHECK(dst_loc.IsRegister());
6594 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006595 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006596 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006597 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006598 DCHECK(dst_loc.IsFpuRegister());
6599 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006600 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006601 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006602 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006603 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006604 }
6605 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006606 }
6607
Alexey Frunze15958152017-02-09 19:08:30 -08006608 // Memory barriers, in the case of references, are handled in the
6609 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006610 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006611 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6612 }
6613}
6614
6615void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006616 DataType::Type field_type = field_info.GetFieldType();
6617 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006618 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006619 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006620 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006621
6622 locations->SetInAt(0, Location::RequiresRegister());
6623 if (generate_volatile) {
6624 InvokeRuntimeCallingConvention calling_convention;
6625 // need A0 to hold base + offset
6626 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006627 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006628 locations->SetInAt(1, Location::RegisterPairLocation(
6629 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6630 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006631 // Use Location::Any() to prevent situations when running out of available fp registers.
6632 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006633 // Pass FP parameters in core registers.
6634 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6635 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6636 }
6637 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006638 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006639 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006640 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006641 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006642 }
6643 }
6644}
6645
6646void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6647 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006648 uint32_t dex_pc,
6649 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006650 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006651 LocationSummary* locations = instruction->GetLocations();
6652 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006653 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006654 StoreOperandType store_type = kStoreByte;
6655 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006656 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006657 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006658 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006659
6660 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006661 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006662 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006663 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006664 store_type = kStoreByte;
6665 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006666 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006667 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006668 store_type = kStoreHalfword;
6669 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006670 case DataType::Type::kInt32:
6671 case DataType::Type::kFloat32:
6672 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006673 store_type = kStoreWord;
6674 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006675 case DataType::Type::kInt64:
6676 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006677 store_type = kStoreDoubleword;
6678 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006679 case DataType::Type::kUint32:
6680 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006681 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006682 LOG(FATAL) << "Unreachable type " << type;
6683 UNREACHABLE();
6684 }
6685
6686 if (is_volatile) {
6687 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6688 }
6689
6690 if (is_volatile && store_type == kStoreDoubleword) {
6691 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006692 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006693 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006694 __ LoadFromOffset(kLoadWord,
6695 ZERO,
6696 locations->GetTemp(0).AsRegister<Register>(),
6697 0,
6698 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006699 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006700 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006701 if (value_location.IsFpuRegister()) {
6702 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6703 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006704 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006705 value_location.AsFpuRegister<FRegister>());
6706 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006707 __ LoadFromOffset(kLoadWord,
6708 locations->GetTemp(1).AsRegister<Register>(),
6709 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006710 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006711 __ LoadFromOffset(kLoadWord,
6712 locations->GetTemp(2).AsRegister<Register>(),
6713 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006714 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006715 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006716 DCHECK(value_location.IsConstant());
6717 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6718 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006719 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6720 locations->GetTemp(1).AsRegister<Register>(),
6721 value);
6722 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006723 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006724 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006725 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6726 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006727 if (value_location.IsConstant()) {
6728 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6729 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006730 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006731 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006732 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006733 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006734 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006735 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006736 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006737 if (kPoisonHeapReferences && needs_write_barrier) {
6738 // Note that in the case where `value` is a null reference,
6739 // we do not enter this block, as a null reference does not
6740 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006741 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006742 __ PoisonHeapReference(TMP, src);
6743 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6744 } else {
6745 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6746 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006747 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006748 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006749 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006750 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006751 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006752 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006753 }
6754 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006755 }
6756
Alexey Frunzec061de12017-02-14 13:27:23 -08006757 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006758 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006759 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006760 }
6761
6762 if (is_volatile) {
6763 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6764 }
6765}
6766
6767void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6768 HandleFieldGet(instruction, instruction->GetFieldInfo());
6769}
6770
6771void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6772 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6773}
6774
6775void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6776 HandleFieldSet(instruction, instruction->GetFieldInfo());
6777}
6778
6779void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006780 HandleFieldSet(instruction,
6781 instruction->GetFieldInfo(),
6782 instruction->GetDexPc(),
6783 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006784}
6785
Alexey Frunze15958152017-02-09 19:08:30 -08006786void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6787 HInstruction* instruction,
6788 Location out,
6789 uint32_t offset,
6790 Location maybe_temp,
6791 ReadBarrierOption read_barrier_option) {
6792 Register out_reg = out.AsRegister<Register>();
6793 if (read_barrier_option == kWithReadBarrier) {
6794 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006795 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6796 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6797 }
Alexey Frunze15958152017-02-09 19:08:30 -08006798 if (kUseBakerReadBarrier) {
6799 // Load with fast path based Baker's read barrier.
6800 // /* HeapReference<Object> */ out = *(out + offset)
6801 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6802 out,
6803 out_reg,
6804 offset,
6805 maybe_temp,
6806 /* needs_null_check */ false);
6807 } else {
6808 // Load with slow path based read barrier.
6809 // Save the value of `out` into `maybe_temp` before overwriting it
6810 // in the following move operation, as we will need it for the
6811 // read barrier below.
6812 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6813 // /* HeapReference<Object> */ out = *(out + offset)
6814 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6815 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6816 }
6817 } else {
6818 // Plain load with no read barrier.
6819 // /* HeapReference<Object> */ out = *(out + offset)
6820 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6821 __ MaybeUnpoisonHeapReference(out_reg);
6822 }
6823}
6824
6825void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6826 HInstruction* instruction,
6827 Location out,
6828 Location obj,
6829 uint32_t offset,
6830 Location maybe_temp,
6831 ReadBarrierOption read_barrier_option) {
6832 Register out_reg = out.AsRegister<Register>();
6833 Register obj_reg = obj.AsRegister<Register>();
6834 if (read_barrier_option == kWithReadBarrier) {
6835 CHECK(kEmitCompilerReadBarrier);
6836 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006837 if (!kBakerReadBarrierThunksEnableForFields) {
6838 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6839 }
Alexey Frunze15958152017-02-09 19:08:30 -08006840 // Load with fast path based Baker's read barrier.
6841 // /* HeapReference<Object> */ out = *(obj + offset)
6842 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6843 out,
6844 obj_reg,
6845 offset,
6846 maybe_temp,
6847 /* needs_null_check */ false);
6848 } else {
6849 // Load with slow path based read barrier.
6850 // /* HeapReference<Object> */ out = *(obj + offset)
6851 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6852 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6853 }
6854 } else {
6855 // Plain load with no read barrier.
6856 // /* HeapReference<Object> */ out = *(obj + offset)
6857 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6858 __ MaybeUnpoisonHeapReference(out_reg);
6859 }
6860}
6861
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006862static inline int GetBakerMarkThunkNumber(Register reg) {
6863 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6864 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6865 return reg - V0;
6866 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6867 return 14 + (reg - S2);
6868 } else if (reg == FP) { // One more.
6869 return 20;
6870 }
6871 LOG(FATAL) << "Unexpected register " << reg;
6872 UNREACHABLE();
6873}
6874
6875static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6876 int num = GetBakerMarkThunkNumber(reg) +
6877 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6878 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6879}
6880
6881static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6882 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6883 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6884}
6885
Alexey Frunze15958152017-02-09 19:08:30 -08006886void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6887 Location root,
6888 Register obj,
6889 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006890 ReadBarrierOption read_barrier_option,
6891 MipsLabel* label_low) {
6892 bool reordering;
6893 if (label_low != nullptr) {
6894 DCHECK_EQ(offset, 0x5678u);
6895 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006896 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006897 if (read_barrier_option == kWithReadBarrier) {
6898 DCHECK(kEmitCompilerReadBarrier);
6899 if (kUseBakerReadBarrier) {
6900 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6901 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006902 if (kBakerReadBarrierThunksEnableForGcRoots) {
6903 // Note that we do not actually check the value of `GetIsGcMarking()`
6904 // to decide whether to mark the loaded GC root or not. Instead, we
6905 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6906 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6907 // vice versa.
6908 //
6909 // We use thunks for the slow path. That thunk checks the reference
6910 // and jumps to the entrypoint if needed.
6911 //
6912 // temp = Thread::Current()->pReadBarrierMarkReg00
6913 // // AKA &art_quick_read_barrier_mark_introspection.
6914 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6915 // if (temp != nullptr) {
6916 // temp = &gc_root_thunk<root_reg>
6917 // root = temp(root)
6918 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006919
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006920 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6921 const int32_t entry_point_offset =
6922 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6923 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6924 int16_t offset_low = Low16Bits(offset);
6925 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6926 // extension in lw.
6927 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6928 Register base = short_offset ? obj : TMP;
6929 // Loading the entrypoint does not require a load acquire since it is only changed when
6930 // threads are suspended or running a checkpoint.
6931 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6932 reordering = __ SetReorder(false);
6933 if (!short_offset) {
6934 DCHECK(!label_low);
6935 __ AddUpper(base, obj, offset_high);
6936 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006937 MipsLabel skip_call;
6938 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006939 if (label_low != nullptr) {
6940 DCHECK(short_offset);
6941 __ Bind(label_low);
6942 }
6943 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6944 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6945 // in delay slot.
6946 if (isR6) {
6947 __ Jialc(T9, thunk_disp);
6948 } else {
6949 __ Addiu(T9, T9, thunk_disp);
6950 __ Jalr(T9);
6951 __ Nop();
6952 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006953 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006954 __ SetReorder(reordering);
6955 } else {
6956 // Note that we do not actually check the value of `GetIsGcMarking()`
6957 // to decide whether to mark the loaded GC root or not. Instead, we
6958 // load into `temp` (T9) the read barrier mark entry point corresponding
6959 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6960 // is false, and vice versa.
6961 //
6962 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6963 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6964 // if (temp != null) {
6965 // root = temp(root)
6966 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006967
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006968 if (label_low != nullptr) {
6969 reordering = __ SetReorder(false);
6970 __ Bind(label_low);
6971 }
6972 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6973 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6974 if (label_low != nullptr) {
6975 __ SetReorder(reordering);
6976 }
6977 static_assert(
6978 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6979 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6980 "have different sizes.");
6981 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6982 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6983 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006984
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006985 // Slow path marking the GC root `root`.
6986 Location temp = Location::RegisterLocation(T9);
6987 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006988 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006989 instruction,
6990 root,
6991 /*entrypoint*/ temp);
6992 codegen_->AddSlowPath(slow_path);
6993
6994 const int32_t entry_point_offset =
6995 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6996 // Loading the entrypoint does not require a load acquire since it is only changed when
6997 // threads are suspended or running a checkpoint.
6998 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6999 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
7000 __ Bind(slow_path->GetExitLabel());
7001 }
Alexey Frunze15958152017-02-09 19:08:30 -08007002 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007003 if (label_low != nullptr) {
7004 reordering = __ SetReorder(false);
7005 __ Bind(label_low);
7006 }
Alexey Frunze15958152017-02-09 19:08:30 -08007007 // GC root loaded through a slow path for read barriers other
7008 // than Baker's.
7009 // /* GcRoot<mirror::Object>* */ root = obj + offset
7010 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007011 if (label_low != nullptr) {
7012 __ SetReorder(reordering);
7013 }
Alexey Frunze15958152017-02-09 19:08:30 -08007014 // /* mirror::Object* */ root = root->Read()
7015 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7016 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007017 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007018 if (label_low != nullptr) {
7019 reordering = __ SetReorder(false);
7020 __ Bind(label_low);
7021 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007022 // Plain GC root load with no read barrier.
7023 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7024 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7025 // Note that GC roots are not affected by heap poisoning, thus we
7026 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007027 if (label_low != nullptr) {
7028 __ SetReorder(reordering);
7029 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007030 }
7031}
7032
Alexey Frunze15958152017-02-09 19:08:30 -08007033void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7034 Location ref,
7035 Register obj,
7036 uint32_t offset,
7037 Location temp,
7038 bool needs_null_check) {
7039 DCHECK(kEmitCompilerReadBarrier);
7040 DCHECK(kUseBakerReadBarrier);
7041
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007042 if (kBakerReadBarrierThunksEnableForFields) {
7043 // Note that we do not actually check the value of `GetIsGcMarking()`
7044 // to decide whether to mark the loaded reference or not. Instead, we
7045 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7046 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7047 // vice versa.
7048 //
7049 // We use thunks for the slow path. That thunk checks the reference
7050 // and jumps to the entrypoint if needed. If the holder is not gray,
7051 // it issues a load-load memory barrier and returns to the original
7052 // reference load.
7053 //
7054 // temp = Thread::Current()->pReadBarrierMarkReg00
7055 // // AKA &art_quick_read_barrier_mark_introspection.
7056 // if (temp != nullptr) {
7057 // temp = &field_array_thunk<holder_reg>
7058 // temp()
7059 // }
7060 // not_gray_return_address:
7061 // // If the offset is too large to fit into the lw instruction, we
7062 // // use an adjusted base register (TMP) here. This register
7063 // // receives bits 16 ... 31 of the offset before the thunk invocation
7064 // // and the thunk benefits from it.
7065 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7066 // gray_return_address:
7067
7068 DCHECK(temp.IsInvalid());
7069 bool isR6 = GetInstructionSetFeatures().IsR6();
7070 int16_t offset_low = Low16Bits(offset);
7071 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7072 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7073 bool reordering = __ SetReorder(false);
7074 const int32_t entry_point_offset =
7075 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7076 // There may have or may have not been a null check if the field offset is smaller than
7077 // the page size.
7078 // There must've been a null check in case it's actually a load from an array.
7079 // We will, however, perform an explicit null check in the thunk as it's easier to
7080 // do it than not.
7081 if (instruction->IsArrayGet()) {
7082 DCHECK(!needs_null_check);
7083 }
7084 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7085 // Loading the entrypoint does not require a load acquire since it is only changed when
7086 // threads are suspended or running a checkpoint.
7087 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7088 Register ref_reg = ref.AsRegister<Register>();
7089 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007090 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007091 if (short_offset) {
7092 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007093 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007094 __ Nop(); // In forbidden slot.
7095 __ Jialc(T9, thunk_disp);
7096 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007097 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007098 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7099 __ Jalr(T9);
7100 __ Nop(); // In delay slot.
7101 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007102 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007103 } else {
7104 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007105 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007106 __ Aui(base, obj, offset_high); // In delay slot.
7107 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007108 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007109 } else {
7110 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007111 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007112 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7113 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007114 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007115 __ Addu(base, base, obj); // In delay slot.
7116 }
7117 }
7118 // /* HeapReference<Object> */ ref = *(obj + offset)
7119 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7120 if (needs_null_check) {
7121 MaybeRecordImplicitNullCheck(instruction);
7122 }
7123 __ MaybeUnpoisonHeapReference(ref_reg);
7124 __ SetReorder(reordering);
7125 return;
7126 }
7127
Alexey Frunze15958152017-02-09 19:08:30 -08007128 // /* HeapReference<Object> */ ref = *(obj + offset)
7129 Location no_index = Location::NoLocation();
7130 ScaleFactor no_scale_factor = TIMES_1;
7131 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7132 ref,
7133 obj,
7134 offset,
7135 no_index,
7136 no_scale_factor,
7137 temp,
7138 needs_null_check);
7139}
7140
7141void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7142 Location ref,
7143 Register obj,
7144 uint32_t data_offset,
7145 Location index,
7146 Location temp,
7147 bool needs_null_check) {
7148 DCHECK(kEmitCompilerReadBarrier);
7149 DCHECK(kUseBakerReadBarrier);
7150
7151 static_assert(
7152 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7153 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007154 ScaleFactor scale_factor = TIMES_4;
7155
7156 if (kBakerReadBarrierThunksEnableForArrays) {
7157 // Note that we do not actually check the value of `GetIsGcMarking()`
7158 // to decide whether to mark the loaded reference or not. Instead, we
7159 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7160 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7161 // vice versa.
7162 //
7163 // We use thunks for the slow path. That thunk checks the reference
7164 // and jumps to the entrypoint if needed. If the holder is not gray,
7165 // it issues a load-load memory barrier and returns to the original
7166 // reference load.
7167 //
7168 // temp = Thread::Current()->pReadBarrierMarkReg00
7169 // // AKA &art_quick_read_barrier_mark_introspection.
7170 // if (temp != nullptr) {
7171 // temp = &field_array_thunk<holder_reg>
7172 // temp()
7173 // }
7174 // not_gray_return_address:
7175 // // The element address is pre-calculated in the TMP register before the
7176 // // thunk invocation and the thunk benefits from it.
7177 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7178 // gray_return_address:
7179
7180 DCHECK(temp.IsInvalid());
7181 DCHECK(index.IsValid());
7182 bool reordering = __ SetReorder(false);
7183 const int32_t entry_point_offset =
7184 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7185 // We will not do the explicit null check in the thunk as some form of a null check
7186 // must've been done earlier.
7187 DCHECK(!needs_null_check);
7188 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7189 // Loading the entrypoint does not require a load acquire since it is only changed when
7190 // threads are suspended or running a checkpoint.
7191 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7192 Register ref_reg = ref.AsRegister<Register>();
7193 Register index_reg = index.IsRegisterPair()
7194 ? index.AsRegisterPairLow<Register>()
7195 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007196 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007197 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007198 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007199 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7200 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007201 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007202 } else {
7203 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007204 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007205 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7206 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007207 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007208 __ Addu(TMP, TMP, obj); // In delay slot.
7209 }
7210 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7211 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7212 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7213 __ MaybeUnpoisonHeapReference(ref_reg);
7214 __ SetReorder(reordering);
7215 return;
7216 }
7217
Alexey Frunze15958152017-02-09 19:08:30 -08007218 // /* HeapReference<Object> */ ref =
7219 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007220 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7221 ref,
7222 obj,
7223 data_offset,
7224 index,
7225 scale_factor,
7226 temp,
7227 needs_null_check);
7228}
7229
7230void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7231 Location ref,
7232 Register obj,
7233 uint32_t offset,
7234 Location index,
7235 ScaleFactor scale_factor,
7236 Location temp,
7237 bool needs_null_check,
7238 bool always_update_field) {
7239 DCHECK(kEmitCompilerReadBarrier);
7240 DCHECK(kUseBakerReadBarrier);
7241
7242 // In slow path based read barriers, the read barrier call is
7243 // inserted after the original load. However, in fast path based
7244 // Baker's read barriers, we need to perform the load of
7245 // mirror::Object::monitor_ *before* the original reference load.
7246 // This load-load ordering is required by the read barrier.
7247 // The fast path/slow path (for Baker's algorithm) should look like:
7248 //
7249 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7250 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7251 // HeapReference<Object> ref = *src; // Original reference load.
7252 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7253 // if (is_gray) {
7254 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7255 // }
7256 //
7257 // Note: the original implementation in ReadBarrier::Barrier is
7258 // slightly more complex as it performs additional checks that we do
7259 // not do here for performance reasons.
7260
7261 Register ref_reg = ref.AsRegister<Register>();
7262 Register temp_reg = temp.AsRegister<Register>();
7263 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7264
7265 // /* int32_t */ monitor = obj->monitor_
7266 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7267 if (needs_null_check) {
7268 MaybeRecordImplicitNullCheck(instruction);
7269 }
7270 // /* LockWord */ lock_word = LockWord(monitor)
7271 static_assert(sizeof(LockWord) == sizeof(int32_t),
7272 "art::LockWord and int32_t have different sizes.");
7273
7274 __ Sync(0); // Barrier to prevent load-load reordering.
7275
7276 // The actual reference load.
7277 if (index.IsValid()) {
7278 // Load types involving an "index": ArrayGet,
7279 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7280 // intrinsics.
7281 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7282 if (index.IsConstant()) {
7283 size_t computed_offset =
7284 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7285 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7286 } else {
7287 // Handle the special case of the
7288 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7289 // intrinsics, which use a register pair as index ("long
7290 // offset"), of which only the low part contains data.
7291 Register index_reg = index.IsRegisterPair()
7292 ? index.AsRegisterPairLow<Register>()
7293 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007294 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007295 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7296 }
7297 } else {
7298 // /* HeapReference<Object> */ ref = *(obj + offset)
7299 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7300 }
7301
7302 // Object* ref = ref_addr->AsMirrorPtr()
7303 __ MaybeUnpoisonHeapReference(ref_reg);
7304
7305 // Slow path marking the object `ref` when it is gray.
7306 SlowPathCodeMIPS* slow_path;
7307 if (always_update_field) {
7308 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7309 // of the form `obj + field_offset`, where `obj` is a register and
7310 // `field_offset` is a register pair (of which only the lower half
7311 // is used). Thus `offset` and `scale_factor` above are expected
7312 // to be null in this code path.
7313 DCHECK_EQ(offset, 0u);
7314 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007315 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007316 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7317 ref,
7318 obj,
7319 /* field_offset */ index,
7320 temp_reg);
7321 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007322 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007323 }
7324 AddSlowPath(slow_path);
7325
7326 // if (rb_state == ReadBarrier::GrayState())
7327 // ref = ReadBarrier::Mark(ref);
7328 // Given the numeric representation, it's enough to check the low bit of the
7329 // rb_state. We do that by shifting the bit into the sign bit (31) and
7330 // performing a branch on less than zero.
7331 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7332 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7333 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7334 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7335 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7336 __ Bind(slow_path->GetExitLabel());
7337}
7338
7339void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7340 Location out,
7341 Location ref,
7342 Location obj,
7343 uint32_t offset,
7344 Location index) {
7345 DCHECK(kEmitCompilerReadBarrier);
7346
7347 // Insert a slow path based read barrier *after* the reference load.
7348 //
7349 // If heap poisoning is enabled, the unpoisoning of the loaded
7350 // reference will be carried out by the runtime within the slow
7351 // path.
7352 //
7353 // Note that `ref` currently does not get unpoisoned (when heap
7354 // poisoning is enabled), which is alright as the `ref` argument is
7355 // not used by the artReadBarrierSlow entry point.
7356 //
7357 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007358 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007359 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7360 AddSlowPath(slow_path);
7361
7362 __ B(slow_path->GetEntryLabel());
7363 __ Bind(slow_path->GetExitLabel());
7364}
7365
7366void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7367 Location out,
7368 Location ref,
7369 Location obj,
7370 uint32_t offset,
7371 Location index) {
7372 if (kEmitCompilerReadBarrier) {
7373 // Baker's read barriers shall be handled by the fast path
7374 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7375 DCHECK(!kUseBakerReadBarrier);
7376 // If heap poisoning is enabled, unpoisoning will be taken care of
7377 // by the runtime within the slow path.
7378 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7379 } else if (kPoisonHeapReferences) {
7380 __ UnpoisonHeapReference(out.AsRegister<Register>());
7381 }
7382}
7383
7384void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7385 Location out,
7386 Location root) {
7387 DCHECK(kEmitCompilerReadBarrier);
7388
7389 // Insert a slow path based read barrier *after* the GC root load.
7390 //
7391 // Note that GC roots are not affected by heap poisoning, so we do
7392 // not need to do anything special for this here.
7393 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007394 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007395 AddSlowPath(slow_path);
7396
7397 __ B(slow_path->GetEntryLabel());
7398 __ Bind(slow_path->GetExitLabel());
7399}
7400
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007401void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007402 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7403 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007404 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007405 switch (type_check_kind) {
7406 case TypeCheckKind::kExactCheck:
7407 case TypeCheckKind::kAbstractClassCheck:
7408 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007409 case TypeCheckKind::kArrayObjectCheck: {
7410 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7411 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7412 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007413 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007414 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007415 case TypeCheckKind::kArrayCheck:
7416 case TypeCheckKind::kUnresolvedCheck:
7417 case TypeCheckKind::kInterfaceCheck:
7418 call_kind = LocationSummary::kCallOnSlowPath;
7419 break;
7420 }
7421
Vladimir Markoca6fff82017-10-03 14:49:14 +01007422 LocationSummary* locations =
7423 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007424 if (baker_read_barrier_slow_path) {
7425 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7426 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007427 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007428 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007429 // The output does overlap inputs.
7430 // Note that TypeCheckSlowPathMIPS uses this register too.
7431 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007432 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007433}
7434
7435void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007436 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007437 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007438 Location obj_loc = locations->InAt(0);
7439 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007440 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007441 Location out_loc = locations->Out();
7442 Register out = out_loc.AsRegister<Register>();
7443 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7444 DCHECK_LE(num_temps, 1u);
7445 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007446 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7447 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7448 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7449 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007450 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007451 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007452
7453 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007454 // Avoid this check if we know `obj` is not null.
7455 if (instruction->MustDoNullCheck()) {
7456 __ Move(out, ZERO);
7457 __ Beqz(obj, &done);
7458 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007459
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007460 switch (type_check_kind) {
7461 case TypeCheckKind::kExactCheck: {
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 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007472 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007473 __ Sltiu(out, out, 1);
7474 break;
7475 }
7476
7477 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007478 ReadBarrierOption read_barrier_option =
7479 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007480 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007481 GenerateReferenceLoadTwoRegisters(instruction,
7482 out_loc,
7483 obj_loc,
7484 class_offset,
7485 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007486 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007487 // If the class is abstract, we eagerly fetch the super class of the
7488 // object to avoid doing a comparison we know will fail.
7489 MipsLabel loop;
7490 __ Bind(&loop);
7491 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007492 GenerateReferenceLoadOneRegister(instruction,
7493 out_loc,
7494 super_offset,
7495 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007496 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007497 // If `out` is null, we use it for the result, and jump to `done`.
7498 __ Beqz(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007499 __ Bne(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007500 __ LoadConst32(out, 1);
7501 break;
7502 }
7503
7504 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007505 ReadBarrierOption read_barrier_option =
7506 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007507 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007508 GenerateReferenceLoadTwoRegisters(instruction,
7509 out_loc,
7510 obj_loc,
7511 class_offset,
7512 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007513 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007514 // Walk over the class hierarchy to find a match.
7515 MipsLabel loop, success;
7516 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007517 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007518 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007519 GenerateReferenceLoadOneRegister(instruction,
7520 out_loc,
7521 super_offset,
7522 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007523 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007524 __ Bnez(out, &loop);
7525 // If `out` is null, we use it for the result, and jump to `done`.
7526 __ B(&done);
7527 __ Bind(&success);
7528 __ LoadConst32(out, 1);
7529 break;
7530 }
7531
7532 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007533 ReadBarrierOption read_barrier_option =
7534 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007535 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007536 GenerateReferenceLoadTwoRegisters(instruction,
7537 out_loc,
7538 obj_loc,
7539 class_offset,
7540 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007541 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007542 // Do an exact check.
7543 MipsLabel success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007544 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007545 // Otherwise, we need to check that the object's class is a non-primitive array.
7546 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007547 GenerateReferenceLoadOneRegister(instruction,
7548 out_loc,
7549 component_offset,
7550 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007551 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007552 // If `out` is null, we use it for the result, and jump to `done`.
7553 __ Beqz(out, &done);
7554 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7555 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7556 __ Sltiu(out, out, 1);
7557 __ B(&done);
7558 __ Bind(&success);
7559 __ LoadConst32(out, 1);
7560 break;
7561 }
7562
7563 case TypeCheckKind::kArrayCheck: {
7564 // No read barrier since the slow path will retry upon failure.
7565 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007566 GenerateReferenceLoadTwoRegisters(instruction,
7567 out_loc,
7568 obj_loc,
7569 class_offset,
7570 maybe_temp_loc,
7571 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007572 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007573 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7574 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007575 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007576 __ Bne(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007577 __ LoadConst32(out, 1);
7578 break;
7579 }
7580
7581 case TypeCheckKind::kUnresolvedCheck:
7582 case TypeCheckKind::kInterfaceCheck: {
7583 // Note that we indeed only call on slow path, but we always go
7584 // into the slow path for the unresolved and interface check
7585 // cases.
7586 //
7587 // We cannot directly call the InstanceofNonTrivial runtime
7588 // entry point without resorting to a type checking slow path
7589 // here (i.e. by calling InvokeRuntime directly), as it would
7590 // require to assign fixed registers for the inputs of this
7591 // HInstanceOf instruction (following the runtime calling
7592 // convention), which might be cluttered by the potential first
7593 // read barrier emission at the beginning of this method.
7594 //
7595 // TODO: Introduce a new runtime entry point taking the object
7596 // to test (instead of its class) as argument, and let it deal
7597 // with the read barrier issues. This will let us refactor this
7598 // case of the `switch` code as it was previously (with a direct
7599 // call to the runtime not using a type checking slow path).
7600 // This should also be beneficial for the other cases above.
7601 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007602 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7603 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007604 codegen_->AddSlowPath(slow_path);
7605 __ B(slow_path->GetEntryLabel());
7606 break;
7607 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007608 }
7609
7610 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007611
7612 if (slow_path != nullptr) {
7613 __ Bind(slow_path->GetExitLabel());
7614 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007615}
7616
7617void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007618 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007619 locations->SetOut(Location::ConstantLocation(constant));
7620}
7621
7622void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7623 // Will be generated at use site.
7624}
7625
7626void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007627 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007628 locations->SetOut(Location::ConstantLocation(constant));
7629}
7630
7631void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7632 // Will be generated at use site.
7633}
7634
7635void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7636 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7637 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7638}
7639
7640void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7641 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007642 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007643 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007644 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007645}
7646
7647void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7648 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7649 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007650 Location receiver = invoke->GetLocations()->InAt(0);
7651 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007652 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007653
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007654 // temp = object->GetClass();
7655 if (receiver.IsStackSlot()) {
7656 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7657 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7658 } else {
7659 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7660 }
7661 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007662 // Instead of simply (possibly) unpoisoning `temp` here, we should
7663 // emit a read barrier for the previous class reference load.
7664 // However this is not required in practice, as this is an
7665 // intermediate/temporary reference and because the current
7666 // concurrent copying collector keeps the from-space memory
7667 // intact/accessible until the end of the marking phase (the
7668 // concurrent copying collector may not in the future).
7669 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007670 __ LoadFromOffset(kLoadWord, temp, temp,
7671 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7672 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007673 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007674 // temp = temp->GetImtEntryAt(method_offset);
7675 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7676 // T9 = temp->GetEntryPoint();
7677 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
Lena Djokic3177e102018-02-28 11:32:40 +01007678 // Set the hidden argument.
7679 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7680 invoke->GetDexMethodIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007681 // T9();
7682 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007683 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007684 DCHECK(!codegen_->IsLeafMethod());
7685 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7686}
7687
7688void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007689 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7690 if (intrinsic.TryDispatch(invoke)) {
7691 return;
7692 }
7693
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007694 HandleInvoke(invoke);
7695}
7696
7697void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007698 // Explicit clinit checks triggered by static invokes must have been pruned by
7699 // art::PrepareForRegisterAllocation.
7700 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007701
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007702 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007703 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7704 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007705
Chris Larsen701566a2015-10-27 15:29:13 -07007706 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7707 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007708 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7709 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7710 }
Chris Larsen701566a2015-10-27 15:29:13 -07007711 return;
7712 }
7713
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007714 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007715
7716 // Add the extra input register if either the dex cache array base register
7717 // or the PC-relative base register for accessing literals is needed.
7718 if (has_extra_input) {
7719 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7720 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007721}
7722
Orion Hodsonac141392017-01-13 11:53:47 +00007723void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7724 HandleInvoke(invoke);
7725}
7726
7727void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7728 codegen_->GenerateInvokePolymorphicCall(invoke);
7729}
7730
Chris Larsen701566a2015-10-27 15:29:13 -07007731static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007732 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007733 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7734 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007735 return true;
7736 }
7737 return false;
7738}
7739
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007740HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007741 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007742 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007743 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007744 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007745 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007746 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007747 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007748 case HLoadString::LoadKind::kJitTableAddress:
7749 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007750 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007751 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007752 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007753 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007754 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007755 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007756}
7757
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007758HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7759 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007760 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007761 case HLoadClass::LoadKind::kInvalid:
7762 LOG(FATAL) << "UNREACHABLE";
7763 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007764 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007765 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007766 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007767 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007768 case HLoadClass::LoadKind::kBssEntry:
7769 DCHECK(!Runtime::Current()->UseJitCompilation());
7770 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007771 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007772 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007773 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007774 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007775 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007776 break;
7777 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007778 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007779}
7780
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007781Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7782 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007783 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007784 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007785 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7786 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7787 if (!invoke->GetLocations()->Intrinsified()) {
7788 return location.AsRegister<Register>();
7789 }
7790 // For intrinsics we allow any location, so it may be on the stack.
7791 if (!location.IsRegister()) {
7792 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7793 return temp;
7794 }
7795 // For register locations, check if the register was saved. If so, get it from the stack.
7796 // Note: There is a chance that the register was saved but not overwritten, so we could
7797 // save one load. However, since this is just an intrinsic slow path we prefer this
7798 // simple and more robust approach rather that trying to determine if that's the case.
7799 SlowPathCode* slow_path = GetCurrentSlowPath();
7800 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7801 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7802 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7803 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7804 return temp;
7805 }
7806 return location.AsRegister<Register>();
7807}
7808
Vladimir Markodc151b22015-10-15 18:02:30 +01007809HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7810 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007811 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007812 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007813}
7814
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007815void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7816 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007817 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007818 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007819 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7820 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007821 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007822 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7823 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007824 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7825 : ZERO;
7826
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007827 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007828 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007829 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007830 uint32_t offset =
7831 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007832 __ LoadFromOffset(kLoadWord,
7833 temp.AsRegister<Register>(),
7834 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007835 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007836 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007837 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007838 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007839 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007840 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007841 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7842 DCHECK(GetCompilerOptions().IsBootImage());
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007843 PcRelativePatchInfo* info_high = NewBootImageMethodPatch(invoke->GetTargetMethod());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007844 PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00007845 NewBootImageMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007846 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007847 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7848 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007849 break;
7850 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007851 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7852 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7853 break;
Vladimir Markob066d432018-01-03 13:14:37 +00007854 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageRelRo: {
7855 uint32_t boot_image_offset = invoke->GetDispatchInfo().method_load_data;
7856 PcRelativePatchInfo* info_high = NewBootImageRelRoPatch(boot_image_offset);
7857 PcRelativePatchInfo* info_low = NewBootImageRelRoPatch(boot_image_offset, info_high);
7858 Register temp_reg = temp.AsRegister<Register>();
7859 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7860 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
7861 break;
7862 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007863 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007864 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007865 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007866 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7867 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007868 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007869 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7870 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007871 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007872 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007873 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7874 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7875 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007876 }
7877 }
7878
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007879 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007880 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007881 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007882 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007883 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7884 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007885 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007886 T9,
7887 callee_method.AsRegister<Register>(),
7888 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007889 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007890 // T9()
7891 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007892 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007893 break;
7894 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007895 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7896
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007897 DCHECK(!IsLeafMethod());
7898}
7899
7900void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007901 // Explicit clinit checks triggered by static invokes must have been pruned by
7902 // art::PrepareForRegisterAllocation.
7903 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007904
7905 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7906 return;
7907 }
7908
7909 LocationSummary* locations = invoke->GetLocations();
7910 codegen_->GenerateStaticOrDirectCall(invoke,
7911 locations->HasTemps()
7912 ? locations->GetTemp(0)
7913 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007914}
7915
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007916void CodeGeneratorMIPS::GenerateVirtualCall(
7917 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007918 // Use the calling convention instead of the location of the receiver, as
7919 // intrinsics may have put the receiver in a different register. In the intrinsics
7920 // slow path, the arguments have been moved to the right place, so here we are
7921 // guaranteed that the receiver is the first register of the calling convention.
7922 InvokeDexCallingConvention calling_convention;
7923 Register receiver = calling_convention.GetRegisterAt(0);
7924
Chris Larsen3acee732015-11-18 13:31:08 -08007925 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007926 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7927 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7928 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007929 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007930
7931 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007932 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007933 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007934 // Instead of simply (possibly) unpoisoning `temp` here, we should
7935 // emit a read barrier for the previous class reference load.
7936 // However this is not required in practice, as this is an
7937 // intermediate/temporary reference and because the current
7938 // concurrent copying collector keeps the from-space memory
7939 // intact/accessible until the end of the marking phase (the
7940 // concurrent copying collector may not in the future).
7941 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007942 // temp = temp->GetMethodAt(method_offset);
7943 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7944 // T9 = temp->GetEntryPoint();
7945 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7946 // T9();
7947 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007948 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007949 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007950}
7951
7952void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7953 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7954 return;
7955 }
7956
7957 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007958 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007959}
7960
7961void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007962 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007963 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007964 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007965 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7966 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007967 return;
7968 }
Vladimir Marko41559982017-01-06 14:04:23 +00007969 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007970 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007971 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007972 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7973 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007974 ? LocationSummary::kCallOnSlowPath
7975 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007976 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007977 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7978 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7979 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007980 switch (load_kind) {
7981 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007982 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007983 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007984 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007985 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007986 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007987 break;
7988 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007989 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007990 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7991 codegen_->ClobberRA();
7992 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007993 break;
7994 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007995 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007996 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007997 locations->SetInAt(0, Location::RequiresRegister());
7998 break;
7999 default:
8000 break;
8001 }
8002 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008003 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
8004 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8005 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008006 RegisterSet caller_saves = RegisterSet::Empty();
8007 InvokeRuntimeCallingConvention calling_convention;
8008 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8009 locations->SetCustomSlowPathCallerSaves(caller_saves);
8010 } else {
8011 // For non-Baker read barriers we have a temp-clobbering call.
8012 }
8013 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008014}
8015
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008016// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8017// move.
8018void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00008019 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008020 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00008021 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01008022 return;
8023 }
Vladimir Marko41559982017-01-06 14:04:23 +00008024 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008025
Vladimir Marko41559982017-01-06 14:04:23 +00008026 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008027 Location out_loc = locations->Out();
8028 Register out = out_loc.AsRegister<Register>();
8029 Register base_or_current_method_reg;
8030 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008031 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008032 switch (load_kind) {
8033 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008034 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008035 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008036 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008037 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008038 base_or_current_method_reg =
8039 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008040 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008041 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008042 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008043 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8044 break;
8045 default:
8046 base_or_current_method_reg = ZERO;
8047 break;
8048 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008049
Alexey Frunze15958152017-02-09 19:08:30 -08008050 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8051 ? kWithoutReadBarrier
8052 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008053 bool generate_null_check = false;
8054 switch (load_kind) {
8055 case HLoadClass::LoadKind::kReferrersClass: {
8056 DCHECK(!cls->CanCallRuntime());
8057 DCHECK(!cls->MustGenerateClinitCheck());
8058 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8059 GenerateGcRootFieldLoad(cls,
8060 out_loc,
8061 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008062 ArtMethod::DeclaringClassOffset().Int32Value(),
8063 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008064 break;
8065 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008066 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008067 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008068 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008069 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008070 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008071 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008072 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008073 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8074 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008075 base_or_current_method_reg);
8076 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008077 break;
8078 }
8079 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08008080 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008081 uint32_t address = dchecked_integral_cast<uint32_t>(
8082 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
8083 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008084 if (isR6 || !has_irreducible_loops) {
8085 __ LoadLiteral(out,
8086 base_or_current_method_reg,
8087 codegen_->DeduplicateBootImageAddressLiteral(address));
8088 } else {
8089 __ LoadConst32(out, address);
8090 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008091 break;
8092 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008093 case HLoadClass::LoadKind::kBootImageClassTable: {
8094 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8095 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008096 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008097 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008098 codegen_->NewBootImageTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008099 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8100 out,
8101 base_or_current_method_reg);
8102 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8103 // Extract the reference from the slot data, i.e. clear the hash bits.
8104 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
8105 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
8106 if (masked_hash != 0) {
8107 __ Addiu(out, out, -masked_hash);
8108 }
8109 break;
8110 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008111 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008112 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8113 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008114 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8115 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008116 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008117 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008118 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008119 GenerateGcRootFieldLoad(cls,
8120 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008121 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008122 /* placeholder */ 0x5678,
8123 read_barrier_option,
8124 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008125 generate_null_check = true;
8126 break;
8127 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008128 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008129 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8130 cls->GetTypeIndex(),
8131 cls->GetClass());
8132 bool reordering = __ SetReorder(false);
8133 __ Bind(&info->high_label);
8134 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008135 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008136 GenerateGcRootFieldLoad(cls,
8137 out_loc,
8138 out,
8139 /* placeholder */ 0x5678,
8140 read_barrier_option,
8141 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008142 break;
8143 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008144 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008145 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008146 LOG(FATAL) << "UNREACHABLE";
8147 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008148 }
8149
8150 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8151 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008152 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00008153 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07008154 codegen_->AddSlowPath(slow_path);
8155 if (generate_null_check) {
8156 __ Beqz(out, slow_path->GetEntryLabel());
8157 }
8158 if (cls->MustGenerateClinitCheck()) {
8159 GenerateClassInitializationCheck(slow_path, out);
8160 } else {
8161 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008162 }
8163 }
8164}
8165
8166static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008167 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008168}
8169
8170void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8171 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008172 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008173 locations->SetOut(Location::RequiresRegister());
8174}
8175
8176void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8177 Register out = load->GetLocations()->Out().AsRegister<Register>();
8178 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8179}
8180
8181void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008182 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008183}
8184
8185void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8186 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8187}
8188
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008189void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008190 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008191 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008192 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008193 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008194 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008195 switch (load_kind) {
8196 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008197 case HLoadString::LoadKind::kBootImageAddress:
8198 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008199 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008200 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008201 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008202 break;
8203 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008204 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008205 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
8206 codegen_->ClobberRA();
8207 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008208 break;
8209 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008210 FALLTHROUGH_INTENDED;
8211 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008212 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008213 locations->SetInAt(0, Location::RequiresRegister());
8214 break;
8215 default:
8216 break;
8217 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008218 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008219 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008220 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008221 } else {
8222 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008223 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8224 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8225 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008226 RegisterSet caller_saves = RegisterSet::Empty();
8227 InvokeRuntimeCallingConvention calling_convention;
8228 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8229 locations->SetCustomSlowPathCallerSaves(caller_saves);
8230 } else {
8231 // For non-Baker read barriers we have a temp-clobbering call.
8232 }
8233 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008234 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008235}
8236
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008237// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8238// move.
8239void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008240 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008241 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008242 Location out_loc = locations->Out();
8243 Register out = out_loc.AsRegister<Register>();
8244 Register base_or_current_method_reg;
8245 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008246 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008247 switch (load_kind) {
8248 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008249 case HLoadString::LoadKind::kBootImageAddress:
8250 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008251 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008252 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008253 base_or_current_method_reg =
8254 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008255 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008256 default:
8257 base_or_current_method_reg = ZERO;
8258 break;
8259 }
8260
8261 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008262 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008263 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008264 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008265 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008266 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008267 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008268 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8269 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008270 base_or_current_method_reg);
8271 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008272 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008273 }
8274 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008275 uint32_t address = dchecked_integral_cast<uint32_t>(
8276 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8277 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008278 if (isR6 || !has_irreducible_loops) {
8279 __ LoadLiteral(out,
8280 base_or_current_method_reg,
8281 codegen_->DeduplicateBootImageAddressLiteral(address));
8282 } else {
8283 __ LoadConst32(out, address);
8284 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008285 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008286 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008287 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008288 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008289 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008290 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008291 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko59eb30f2018-02-20 11:52:34 +00008292 codegen_->NewBootImageStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008293 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8294 out,
8295 base_or_current_method_reg);
8296 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8297 return;
8298 }
8299 case HLoadString::LoadKind::kBssEntry: {
8300 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8301 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8302 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8303 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8304 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008305 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008306 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008307 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008308 GenerateGcRootFieldLoad(load,
8309 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008310 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008311 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008312 kCompilerReadBarrierOption,
8313 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008314 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008315 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008316 codegen_->AddSlowPath(slow_path);
8317 __ Beqz(out, slow_path->GetEntryLabel());
8318 __ Bind(slow_path->GetExitLabel());
8319 return;
8320 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008321 case HLoadString::LoadKind::kJitTableAddress: {
8322 CodeGeneratorMIPS::JitPatchInfo* info =
8323 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8324 load->GetStringIndex(),
8325 load->GetString());
8326 bool reordering = __ SetReorder(false);
8327 __ Bind(&info->high_label);
8328 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008329 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008330 GenerateGcRootFieldLoad(load,
8331 out_loc,
8332 out,
8333 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008334 kCompilerReadBarrierOption,
8335 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008336 return;
8337 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008338 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008339 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008340 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008341
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008342 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008343 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008344 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008345 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008346 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008347 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8348 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008349}
8350
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008351void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008352 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008353 locations->SetOut(Location::ConstantLocation(constant));
8354}
8355
8356void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8357 // Will be generated at use site.
8358}
8359
8360void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008361 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8362 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008363 InvokeRuntimeCallingConvention calling_convention;
8364 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8365}
8366
8367void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8368 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008369 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008370 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8371 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008372 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008373 }
8374 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8375}
8376
8377void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8378 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008379 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008380 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008381 case DataType::Type::kInt32:
8382 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008383 locations->SetInAt(0, Location::RequiresRegister());
8384 locations->SetInAt(1, Location::RequiresRegister());
8385 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8386 break;
8387
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008388 case DataType::Type::kFloat32:
8389 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008390 locations->SetInAt(0, Location::RequiresFpuRegister());
8391 locations->SetInAt(1, Location::RequiresFpuRegister());
8392 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8393 break;
8394
8395 default:
8396 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8397 }
8398}
8399
8400void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008401 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008402 LocationSummary* locations = instruction->GetLocations();
8403 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8404
8405 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008406 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008407 Register dst = locations->Out().AsRegister<Register>();
8408 Register lhs = locations->InAt(0).AsRegister<Register>();
8409 Register rhs = locations->InAt(1).AsRegister<Register>();
8410
8411 if (isR6) {
8412 __ MulR6(dst, lhs, rhs);
8413 } else {
8414 __ MulR2(dst, lhs, rhs);
8415 }
8416 break;
8417 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008418 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008419 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8420 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8421 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8422 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8423 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8424 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8425
8426 // Extra checks to protect caused by the existance of A1_A2.
8427 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8428 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8429 DCHECK_NE(dst_high, lhs_low);
8430 DCHECK_NE(dst_high, rhs_low);
8431
8432 // A_B * C_D
8433 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8434 // dst_lo: [ low(B*D) ]
8435 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8436
8437 if (isR6) {
8438 __ MulR6(TMP, lhs_high, rhs_low);
8439 __ MulR6(dst_high, lhs_low, rhs_high);
8440 __ Addu(dst_high, dst_high, TMP);
8441 __ MuhuR6(TMP, lhs_low, rhs_low);
8442 __ Addu(dst_high, dst_high, TMP);
8443 __ MulR6(dst_low, lhs_low, rhs_low);
8444 } else {
8445 __ MulR2(TMP, lhs_high, rhs_low);
8446 __ MulR2(dst_high, lhs_low, rhs_high);
8447 __ Addu(dst_high, dst_high, TMP);
8448 __ MultuR2(lhs_low, rhs_low);
8449 __ Mfhi(TMP);
8450 __ Addu(dst_high, dst_high, TMP);
8451 __ Mflo(dst_low);
8452 }
8453 break;
8454 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008455 case DataType::Type::kFloat32:
8456 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008457 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8458 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8459 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008460 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008461 __ MulS(dst, lhs, rhs);
8462 } else {
8463 __ MulD(dst, lhs, rhs);
8464 }
8465 break;
8466 }
8467 default:
8468 LOG(FATAL) << "Unexpected mul type " << type;
8469 }
8470}
8471
8472void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8473 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008474 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008475 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008476 case DataType::Type::kInt32:
8477 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008478 locations->SetInAt(0, Location::RequiresRegister());
8479 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8480 break;
8481
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008482 case DataType::Type::kFloat32:
8483 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008484 locations->SetInAt(0, Location::RequiresFpuRegister());
8485 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8486 break;
8487
8488 default:
8489 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8490 }
8491}
8492
8493void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008494 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008495 LocationSummary* locations = instruction->GetLocations();
8496
8497 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008498 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008499 Register dst = locations->Out().AsRegister<Register>();
8500 Register src = locations->InAt(0).AsRegister<Register>();
8501 __ Subu(dst, ZERO, src);
8502 break;
8503 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008504 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008505 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8506 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8507 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8508 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8509 __ Subu(dst_low, ZERO, src_low);
8510 __ Sltu(TMP, ZERO, dst_low);
8511 __ Subu(dst_high, ZERO, src_high);
8512 __ Subu(dst_high, dst_high, TMP);
8513 break;
8514 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008515 case DataType::Type::kFloat32:
8516 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008517 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8518 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008519 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008520 __ NegS(dst, src);
8521 } else {
8522 __ NegD(dst, src);
8523 }
8524 break;
8525 }
8526 default:
8527 LOG(FATAL) << "Unexpected neg type " << type;
8528 }
8529}
8530
8531void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008532 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8533 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008534 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008535 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008536 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8537 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008538}
8539
8540void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008541 // Note: if heap poisoning is enabled, the entry point takes care
8542 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008543 QuickEntrypointEnum entrypoint =
8544 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8545 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008546 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008547 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008548}
8549
8550void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008551 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8552 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008553 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008554 if (instruction->IsStringAlloc()) {
8555 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8556 } else {
8557 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008558 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008559 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008560}
8561
8562void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008563 // Note: if heap poisoning is enabled, the entry point takes care
8564 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008565 if (instruction->IsStringAlloc()) {
8566 // String is allocated through StringFactory. Call NewEmptyString entry point.
8567 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008568 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008569 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8570 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8571 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008572 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008573 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8574 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008575 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008576 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008577 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008578}
8579
8580void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008581 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008582 locations->SetInAt(0, Location::RequiresRegister());
8583 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8584}
8585
8586void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008587 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008588 LocationSummary* locations = instruction->GetLocations();
8589
8590 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008591 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008592 Register dst = locations->Out().AsRegister<Register>();
8593 Register src = locations->InAt(0).AsRegister<Register>();
8594 __ Nor(dst, src, ZERO);
8595 break;
8596 }
8597
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008598 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008599 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8600 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8601 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8602 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8603 __ Nor(dst_high, src_high, ZERO);
8604 __ Nor(dst_low, src_low, ZERO);
8605 break;
8606 }
8607
8608 default:
8609 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8610 }
8611}
8612
8613void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008614 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008615 locations->SetInAt(0, Location::RequiresRegister());
8616 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8617}
8618
8619void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8620 LocationSummary* locations = instruction->GetLocations();
8621 __ Xori(locations->Out().AsRegister<Register>(),
8622 locations->InAt(0).AsRegister<Register>(),
8623 1);
8624}
8625
8626void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008627 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8628 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008629}
8630
Calin Juravle2ae48182016-03-16 14:05:09 +00008631void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8632 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008633 return;
8634 }
8635 Location obj = instruction->GetLocations()->InAt(0);
8636
8637 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008638 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008639}
8640
Calin Juravle2ae48182016-03-16 14:05:09 +00008641void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008642 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008643 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008644
8645 Location obj = instruction->GetLocations()->InAt(0);
8646
8647 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8648}
8649
8650void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008651 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008652}
8653
8654void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8655 HandleBinaryOp(instruction);
8656}
8657
8658void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8659 HandleBinaryOp(instruction);
8660}
8661
8662void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8663 LOG(FATAL) << "Unreachable";
8664}
8665
8666void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008667 if (instruction->GetNext()->IsSuspendCheck() &&
8668 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8669 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8670 // The back edge will generate the suspend check.
8671 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8672 }
8673
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008674 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8675}
8676
8677void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008678 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008679 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8680 if (location.IsStackSlot()) {
8681 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8682 } else if (location.IsDoubleStackSlot()) {
8683 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8684 }
8685 locations->SetOut(location);
8686}
8687
8688void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8689 ATTRIBUTE_UNUSED) {
8690 // Nothing to do, the parameter is already at its location.
8691}
8692
8693void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8694 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008695 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008696 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8697}
8698
8699void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8700 ATTRIBUTE_UNUSED) {
8701 // Nothing to do, the method is already at its location.
8702}
8703
8704void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008705 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008706 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008707 locations->SetInAt(i, Location::Any());
8708 }
8709 locations->SetOut(Location::Any());
8710}
8711
8712void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8713 LOG(FATAL) << "Unreachable";
8714}
8715
8716void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008717 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008718 bool call_rem;
8719 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8720 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8721 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8722 } else {
8723 call_rem = (type != DataType::Type::kInt32);
8724 }
8725 LocationSummary::CallKind call_kind = call_rem
8726 ? LocationSummary::kCallOnMainOnly
8727 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008728 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008729
8730 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008731 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008732 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008733 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008734 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8735 break;
8736
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008737 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008738 if (call_rem) {
8739 InvokeRuntimeCallingConvention calling_convention;
8740 locations->SetInAt(0, Location::RegisterPairLocation(
8741 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8742 locations->SetInAt(1, Location::RegisterPairLocation(
8743 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8744 locations->SetOut(calling_convention.GetReturnLocation(type));
8745 } else {
8746 locations->SetInAt(0, Location::RequiresRegister());
8747 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8748 locations->SetOut(Location::RequiresRegister());
8749 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008750 break;
8751 }
8752
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008753 case DataType::Type::kFloat32:
8754 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008755 InvokeRuntimeCallingConvention calling_convention;
8756 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8757 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8758 locations->SetOut(calling_convention.GetReturnLocation(type));
8759 break;
8760 }
8761
8762 default:
8763 LOG(FATAL) << "Unexpected rem type " << type;
8764 }
8765}
8766
8767void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008768 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008769 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008770
8771 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008772 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008773 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008774 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008775 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008776 if (locations->InAt(1).IsConstant()) {
8777 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8778 if (imm == 0) {
8779 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8780 } else if (imm == 1 || imm == -1) {
8781 DivRemOneOrMinusOne(instruction);
8782 } else {
8783 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8784 DivRemByPowerOfTwo(instruction);
8785 }
8786 } else {
8787 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8788 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8789 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008790 break;
8791 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008792 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008793 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008794 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008795 break;
8796 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008797 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008798 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008799 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008800 break;
8801 }
8802 default:
8803 LOG(FATAL) << "Unexpected rem type " << type;
8804 }
8805}
8806
Aart Bik3dad3412018-02-28 12:01:46 -08008807void LocationsBuilderMIPS::VisitAbs(HAbs* abs) {
8808 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(abs);
8809 switch (abs->GetResultType()) {
8810 case DataType::Type::kInt32:
8811 case DataType::Type::kInt64:
8812 locations->SetInAt(0, Location::RequiresRegister());
8813 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8814 break;
8815 case DataType::Type::kFloat32:
8816 case DataType::Type::kFloat64:
8817 locations->SetInAt(0, Location::RequiresFpuRegister());
8818 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8819 break;
8820 default:
8821 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
8822 }
8823}
8824
8825void InstructionCodeGeneratorMIPS::GenerateAbsFP(LocationSummary* locations,
8826 DataType::Type type,
8827 bool isR2OrNewer,
8828 bool isR6) {
8829 FRegister in = locations->InAt(0).AsFpuRegister<FRegister>();
8830 FRegister out = locations->Out().AsFpuRegister<FRegister>();
8831
8832 // Note, as a "quality of implementation", rather than pure "spec compliance", we require that
8833 // Math.abs() clears the sign bit (but changes nothing else) for all numbers, including NaN
8834 // (signaling NaN may become quiet though).
8835 //
8836 // The ABS.fmt instructions (abs.s and abs.d) do exactly that when NAN2008=1 (R6). For this case,
8837 // both regular floating point numbers and NAN values are treated alike, only the sign bit is
8838 // affected by this instruction.
8839 // But when NAN2008=0 (R2 and before), the ABS.fmt instructions can't be used. For this case, any
8840 // NaN operand signals invalid operation. This means that other bits (not just sign bit) might be
8841 // changed when doing abs(NaN). Because of that, we clear sign bit in a different way.
8842 if (isR6) {
8843 if (type == DataType::Type::kFloat64) {
8844 __ AbsD(out, in);
8845 } else {
8846 DCHECK_EQ(type, DataType::Type::kFloat32);
8847 __ AbsS(out, in);
8848 }
8849 } else {
8850 if (type == DataType::Type::kFloat64) {
8851 if (in != out) {
8852 __ MovD(out, in);
8853 }
8854 __ MoveFromFpuHigh(TMP, in);
8855 // ins instruction is not available for R1.
8856 if (isR2OrNewer) {
8857 __ Ins(TMP, ZERO, 31, 1);
8858 } else {
8859 __ Sll(TMP, TMP, 1);
8860 __ Srl(TMP, TMP, 1);
8861 }
8862 __ MoveToFpuHigh(TMP, out);
8863 } else {
8864 DCHECK_EQ(type, DataType::Type::kFloat32);
8865 __ Mfc1(TMP, in);
8866 // ins instruction is not available for R1.
8867 if (isR2OrNewer) {
8868 __ Ins(TMP, ZERO, 31, 1);
8869 } else {
8870 __ Sll(TMP, TMP, 1);
8871 __ Srl(TMP, TMP, 1);
8872 }
8873 __ Mtc1(TMP, out);
8874 }
8875 }
8876}
8877
8878void InstructionCodeGeneratorMIPS::VisitAbs(HAbs* abs) {
8879 LocationSummary* locations = abs->GetLocations();
8880 bool isR2OrNewer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
8881 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8882 switch (abs->GetResultType()) {
8883 case DataType::Type::kInt32: {
8884 Register in = locations->InAt(0).AsRegister<Register>();
8885 Register out = locations->Out().AsRegister<Register>();
8886 __ Sra(AT, in, 31);
8887 __ Xor(out, in, AT);
8888 __ Subu(out, out, AT);
8889 break;
8890 }
8891 case DataType::Type::kInt64: {
8892 Register in_lo = locations->InAt(0).AsRegisterPairLow<Register>();
8893 Register in_hi = locations->InAt(0).AsRegisterPairHigh<Register>();
8894 Register out_lo = locations->Out().AsRegisterPairLow<Register>();
8895 Register out_hi = locations->Out().AsRegisterPairHigh<Register>();
8896 // The comments in this section show the analogous operations which would
8897 // be performed if we had 64-bit registers "in", and "out".
8898 // __ Dsra32(AT, in, 31);
8899 __ Sra(AT, in_hi, 31);
8900 // __ Xor(out, in, AT);
8901 __ Xor(TMP, in_lo, AT);
8902 __ Xor(out_hi, in_hi, AT);
8903 // __ Dsubu(out, out, AT);
8904 __ Subu(out_lo, TMP, AT);
8905 __ Sltu(TMP, out_lo, TMP);
8906 __ Addu(out_hi, out_hi, TMP);
8907 break;
8908 }
8909 case DataType::Type::kFloat32:
8910 case DataType::Type::kFloat64:
8911 GenerateAbsFP(locations, abs->GetResultType(), isR2OrNewer, isR6);
8912 break;
8913 default:
8914 LOG(FATAL) << "Unexpected abs type " << abs->GetResultType();
8915 }
8916}
8917
Igor Murashkind01745e2017-04-05 16:40:31 -07008918void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8919 constructor_fence->SetLocations(nullptr);
8920}
8921
8922void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8923 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8924 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8925}
8926
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008927void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8928 memory_barrier->SetLocations(nullptr);
8929}
8930
8931void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8932 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8933}
8934
8935void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008936 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008937 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008938 locations->SetInAt(0, MipsReturnLocation(return_type));
8939}
8940
8941void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8942 codegen_->GenerateFrameExit();
8943}
8944
8945void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8946 ret->SetLocations(nullptr);
8947}
8948
8949void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8950 codegen_->GenerateFrameExit();
8951}
8952
Alexey Frunze92d90602015-12-18 18:16:36 -08008953void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8954 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008955}
8956
Alexey Frunze92d90602015-12-18 18:16:36 -08008957void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8958 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008959}
8960
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008961void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8962 HandleShift(shl);
8963}
8964
8965void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8966 HandleShift(shl);
8967}
8968
8969void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8970 HandleShift(shr);
8971}
8972
8973void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8974 HandleShift(shr);
8975}
8976
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008977void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8978 HandleBinaryOp(instruction);
8979}
8980
8981void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8982 HandleBinaryOp(instruction);
8983}
8984
8985void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8986 HandleFieldGet(instruction, instruction->GetFieldInfo());
8987}
8988
8989void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8990 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8991}
8992
8993void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8994 HandleFieldSet(instruction, instruction->GetFieldInfo());
8995}
8996
8997void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008998 HandleFieldSet(instruction,
8999 instruction->GetFieldInfo(),
9000 instruction->GetDexPc(),
9001 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009002}
9003
9004void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
9005 HUnresolvedInstanceFieldGet* instruction) {
9006 FieldAccessCallingConventionMIPS calling_convention;
9007 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9008 instruction->GetFieldType(),
9009 calling_convention);
9010}
9011
9012void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
9013 HUnresolvedInstanceFieldGet* instruction) {
9014 FieldAccessCallingConventionMIPS calling_convention;
9015 codegen_->GenerateUnresolvedFieldAccess(instruction,
9016 instruction->GetFieldType(),
9017 instruction->GetFieldIndex(),
9018 instruction->GetDexPc(),
9019 calling_convention);
9020}
9021
9022void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
9023 HUnresolvedInstanceFieldSet* instruction) {
9024 FieldAccessCallingConventionMIPS calling_convention;
9025 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9026 instruction->GetFieldType(),
9027 calling_convention);
9028}
9029
9030void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
9031 HUnresolvedInstanceFieldSet* instruction) {
9032 FieldAccessCallingConventionMIPS calling_convention;
9033 codegen_->GenerateUnresolvedFieldAccess(instruction,
9034 instruction->GetFieldType(),
9035 instruction->GetFieldIndex(),
9036 instruction->GetDexPc(),
9037 calling_convention);
9038}
9039
9040void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
9041 HUnresolvedStaticFieldGet* instruction) {
9042 FieldAccessCallingConventionMIPS calling_convention;
9043 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9044 instruction->GetFieldType(),
9045 calling_convention);
9046}
9047
9048void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
9049 HUnresolvedStaticFieldGet* instruction) {
9050 FieldAccessCallingConventionMIPS calling_convention;
9051 codegen_->GenerateUnresolvedFieldAccess(instruction,
9052 instruction->GetFieldType(),
9053 instruction->GetFieldIndex(),
9054 instruction->GetDexPc(),
9055 calling_convention);
9056}
9057
9058void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
9059 HUnresolvedStaticFieldSet* instruction) {
9060 FieldAccessCallingConventionMIPS calling_convention;
9061 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
9062 instruction->GetFieldType(),
9063 calling_convention);
9064}
9065
9066void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
9067 HUnresolvedStaticFieldSet* instruction) {
9068 FieldAccessCallingConventionMIPS calling_convention;
9069 codegen_->GenerateUnresolvedFieldAccess(instruction,
9070 instruction->GetFieldType(),
9071 instruction->GetFieldIndex(),
9072 instruction->GetDexPc(),
9073 calling_convention);
9074}
9075
9076void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009077 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9078 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02009079 // In suspend check slow path, usually there are no caller-save registers at all.
9080 // If SIMD instructions are present, however, we force spilling all live SIMD
9081 // registers in full width (since the runtime only saves/restores lower part).
9082 locations->SetCustomSlowPathCallerSaves(
9083 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009084}
9085
9086void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
9087 HBasicBlock* block = instruction->GetBlock();
9088 if (block->GetLoopInformation() != nullptr) {
9089 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
9090 // The back edge will generate the suspend check.
9091 return;
9092 }
9093 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
9094 // The goto will generate the suspend check.
9095 return;
9096 }
9097 GenerateSuspendCheck(instruction, nullptr);
9098}
9099
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009100void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01009101 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
9102 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009103 InvokeRuntimeCallingConvention calling_convention;
9104 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
9105}
9106
9107void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01009108 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009109 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
9110}
9111
9112void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009113 DataType::Type input_type = conversion->GetInputType();
9114 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009115 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9116 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009117 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009118
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009119 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
9120 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009121 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
9122 }
9123
9124 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009125 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009126 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
9127 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01009128 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009129 }
9130
Vladimir Markoca6fff82017-10-03 14:49:14 +01009131 LocationSummary* locations =
9132 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009133
9134 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009135 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009136 locations->SetInAt(0, Location::RequiresFpuRegister());
9137 } else {
9138 locations->SetInAt(0, Location::RequiresRegister());
9139 }
9140
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009141 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009142 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9143 } else {
9144 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9145 }
9146 } else {
9147 InvokeRuntimeCallingConvention calling_convention;
9148
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009149 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009150 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9151 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009152 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009153 locations->SetInAt(0, Location::RegisterPairLocation(
9154 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9155 }
9156
9157 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9158 }
9159}
9160
9161void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9162 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009163 DataType::Type result_type = conversion->GetResultType();
9164 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009165 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009166 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009167
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009168 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9169 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009170
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009171 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009172 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9173 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9174 Register src = locations->InAt(0).AsRegister<Register>();
9175
Alexey Frunzea871ef12016-06-27 15:20:11 -07009176 if (dst_low != src) {
9177 __ Move(dst_low, src);
9178 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009179 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009180 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009181 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009182 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009183 ? locations->InAt(0).AsRegisterPairLow<Register>()
9184 : locations->InAt(0).AsRegister<Register>();
9185
9186 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009187 case DataType::Type::kUint8:
9188 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009189 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009190 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009191 if (has_sign_extension) {
9192 __ Seb(dst, src);
9193 } else {
9194 __ Sll(dst, src, 24);
9195 __ Sra(dst, dst, 24);
9196 }
9197 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009198 case DataType::Type::kUint16:
9199 __ Andi(dst, src, 0xFFFF);
9200 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009201 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009202 if (has_sign_extension) {
9203 __ Seh(dst, src);
9204 } else {
9205 __ Sll(dst, src, 16);
9206 __ Sra(dst, dst, 16);
9207 }
9208 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009209 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009210 if (dst != src) {
9211 __ Move(dst, src);
9212 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009213 break;
9214
9215 default:
9216 LOG(FATAL) << "Unexpected type conversion from " << input_type
9217 << " to " << result_type;
9218 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009219 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9220 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009221 if (isR6) {
9222 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9223 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9224 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9225 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9226 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9227 __ Mtc1(src_low, FTMP);
9228 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009229 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009230 __ Cvtsl(dst, FTMP);
9231 } else {
9232 __ Cvtdl(dst, FTMP);
9233 }
9234 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009235 QuickEntrypointEnum entrypoint =
9236 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009237 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009238 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009239 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9240 } else {
9241 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9242 }
9243 }
9244 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009245 Register src = locations->InAt(0).AsRegister<Register>();
9246 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9247 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009248 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009249 __ Cvtsw(dst, FTMP);
9250 } else {
9251 __ Cvtdw(dst, FTMP);
9252 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009253 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009254 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9255 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009256
9257 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9258 // value of the output type if the input is outside of the range after the truncation or
9259 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9260 // results. This matches the desired float/double-to-int/long conversion exactly.
9261 //
9262 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9263 // value when the input is either a NaN or is outside of the range of the output type
9264 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9265 // the same result.
9266 //
9267 // The code takes care of the different behaviors by first comparing the input to the
9268 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9269 // If the input is greater than or equal to the minimum, it procedes to the truncate
9270 // instruction, which will handle such an input the same way irrespective of NAN2008.
9271 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9272 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009273 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009274 if (isR6) {
9275 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9276 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9277 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9278 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9279 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009280
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009281 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009282 __ TruncLS(FTMP, src);
9283 } else {
9284 __ TruncLD(FTMP, src);
9285 }
9286 __ Mfc1(dst_low, FTMP);
9287 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009288 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009289 QuickEntrypointEnum entrypoint =
9290 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009291 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009292 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009293 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9294 } else {
9295 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9296 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009297 }
9298 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009299 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9300 Register dst = locations->Out().AsRegister<Register>();
9301 MipsLabel truncate;
9302 MipsLabel done;
9303
Lena Djokicf4e23a82017-05-09 15:43:45 +02009304 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009305 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009306 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9307 __ LoadConst32(TMP, min_val);
9308 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009309 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009310 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9311 __ LoadConst32(TMP, High32Bits(min_val));
9312 __ Mtc1(ZERO, FTMP);
9313 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009314 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009315
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009316 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009317 __ ColeS(0, FTMP, src);
9318 } else {
9319 __ ColeD(0, FTMP, src);
9320 }
9321 __ Bc1t(0, &truncate);
9322
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009323 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009324 __ CeqS(0, src, src);
9325 } else {
9326 __ CeqD(0, src, src);
9327 }
9328 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9329 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009330
9331 __ B(&done);
9332
9333 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009334 }
9335
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009336 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009337 __ TruncWS(FTMP, src);
9338 } else {
9339 __ TruncWD(FTMP, src);
9340 }
9341 __ Mfc1(dst, FTMP);
9342
Lena Djokicf4e23a82017-05-09 15:43:45 +02009343 if (!isR6) {
9344 __ Bind(&done);
9345 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009346 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009347 } else if (DataType::IsFloatingPointType(result_type) &&
9348 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009349 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9350 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009351 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009352 __ Cvtsd(dst, src);
9353 } else {
9354 __ Cvtds(dst, src);
9355 }
9356 } else {
9357 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9358 << " to " << result_type;
9359 }
9360}
9361
9362void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9363 HandleShift(ushr);
9364}
9365
9366void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9367 HandleShift(ushr);
9368}
9369
9370void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9371 HandleBinaryOp(instruction);
9372}
9373
9374void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9375 HandleBinaryOp(instruction);
9376}
9377
9378void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9379 // Nothing to do, this should be removed during prepare for register allocator.
9380 LOG(FATAL) << "Unreachable";
9381}
9382
9383void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9384 // Nothing to do, this should be removed during prepare for register allocator.
9385 LOG(FATAL) << "Unreachable";
9386}
9387
9388void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009389 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009390}
9391
9392void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009393 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009394}
9395
9396void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009397 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009398}
9399
9400void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009401 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009402}
9403
9404void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009405 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009406}
9407
9408void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009409 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009410}
9411
9412void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009413 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009414}
9415
9416void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009417 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009418}
9419
9420void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009421 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009422}
9423
9424void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009425 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009426}
9427
9428void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009429 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009430}
9431
9432void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009433 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009434}
9435
9436void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009437 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009438}
9439
9440void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009441 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009442}
9443
9444void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009445 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009446}
9447
9448void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009449 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009450}
9451
9452void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009453 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009454}
9455
9456void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009457 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009458}
9459
9460void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009461 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009462}
9463
9464void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009465 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009466}
9467
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009468void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9469 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009470 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009471 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009472 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9473 uint32_t num_entries = switch_instr->GetNumEntries();
9474 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9475 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9476 // instruction to simulate PC-relative addressing when accessing the jump table.
9477 // NAL clobbers RA. Make sure RA is preserved.
9478 codegen_->ClobberRA();
9479 }
9480 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009481}
9482
Alexey Frunze96b66822016-09-10 02:32:44 -07009483void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9484 int32_t lower_bound,
9485 uint32_t num_entries,
9486 HBasicBlock* switch_block,
9487 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009488 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009489 Register temp_reg = TMP;
9490 __ Addiu32(temp_reg, value_reg, -lower_bound);
9491 // Jump to default if index is negative
9492 // Note: We don't check the case that index is positive while value < lower_bound, because in
9493 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9494 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9495
Alexey Frunze96b66822016-09-10 02:32:44 -07009496 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009497 // Jump to successors[0] if value == lower_bound.
9498 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9499 int32_t last_index = 0;
9500 for (; num_entries - last_index > 2; last_index += 2) {
9501 __ Addiu(temp_reg, temp_reg, -2);
9502 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9503 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9504 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9505 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9506 }
9507 if (num_entries - last_index == 2) {
9508 // The last missing case_value.
9509 __ Addiu(temp_reg, temp_reg, -1);
9510 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009511 }
9512
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009513 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009514 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009515 __ B(codegen_->GetLabelOf(default_block));
9516 }
9517}
9518
Alexey Frunze96b66822016-09-10 02:32:44 -07009519void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9520 Register constant_area,
9521 int32_t lower_bound,
9522 uint32_t num_entries,
9523 HBasicBlock* switch_block,
9524 HBasicBlock* default_block) {
9525 // Create a jump table.
9526 std::vector<MipsLabel*> labels(num_entries);
9527 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9528 for (uint32_t i = 0; i < num_entries; i++) {
9529 labels[i] = codegen_->GetLabelOf(successors[i]);
9530 }
9531 JumpTable* table = __ CreateJumpTable(std::move(labels));
9532
9533 // Is the value in range?
9534 __ Addiu32(TMP, value_reg, -lower_bound);
9535 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9536 __ Sltiu(AT, TMP, num_entries);
9537 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9538 } else {
9539 __ LoadConst32(AT, num_entries);
9540 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9541 }
9542
9543 // We are in the range of the table.
9544 // Load the target address from the jump table, indexing by the value.
9545 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009546 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009547 __ Lw(TMP, TMP, 0);
9548 // Compute the absolute target address by adding the table start address
9549 // (the table contains offsets to targets relative to its start).
9550 __ Addu(TMP, TMP, AT);
9551 // And jump.
9552 __ Jr(TMP);
9553 __ NopIfNoReordering();
9554}
9555
9556void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9557 int32_t lower_bound = switch_instr->GetStartValue();
9558 uint32_t num_entries = switch_instr->GetNumEntries();
9559 LocationSummary* locations = switch_instr->GetLocations();
9560 Register value_reg = locations->InAt(0).AsRegister<Register>();
9561 HBasicBlock* switch_block = switch_instr->GetBlock();
9562 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9563
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009564 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009565 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009566 //
9567 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9568 // to access the jump table and it is implemented by changing HPackedSwitch to
9569 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9570 // VisitMipsPackedSwitch()).
9571 //
9572 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9573 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9574 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009575 GenTableBasedPackedSwitch(value_reg,
9576 ZERO,
9577 lower_bound,
9578 num_entries,
9579 switch_block,
9580 default_block);
9581 } else {
9582 GenPackedSwitchWithCompares(value_reg,
9583 lower_bound,
9584 num_entries,
9585 switch_block,
9586 default_block);
9587 }
9588}
9589
9590void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9591 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009592 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009593 locations->SetInAt(0, Location::RequiresRegister());
9594 // Constant area pointer (HMipsComputeBaseMethodAddress).
9595 locations->SetInAt(1, Location::RequiresRegister());
9596}
9597
9598void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9599 int32_t lower_bound = switch_instr->GetStartValue();
9600 uint32_t num_entries = switch_instr->GetNumEntries();
9601 LocationSummary* locations = switch_instr->GetLocations();
9602 Register value_reg = locations->InAt(0).AsRegister<Register>();
9603 Register constant_area = locations->InAt(1).AsRegister<Register>();
9604 HBasicBlock* switch_block = switch_instr->GetBlock();
9605 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9606
9607 // This is an R2-only path. HPackedSwitch has been changed to
9608 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9609 // required to address the jump table relative to PC.
9610 GenTableBasedPackedSwitch(value_reg,
9611 constant_area,
9612 lower_bound,
9613 num_entries,
9614 switch_block,
9615 default_block);
9616}
9617
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009618void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9619 HMipsComputeBaseMethodAddress* insn) {
9620 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009621 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009622 locations->SetOut(Location::RequiresRegister());
9623}
9624
9625void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9626 HMipsComputeBaseMethodAddress* insn) {
9627 LocationSummary* locations = insn->GetLocations();
9628 Register reg = locations->Out().AsRegister<Register>();
9629
9630 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9631
9632 // Generate a dummy PC-relative call to obtain PC.
9633 __ Nal();
9634 // Grab the return address off RA.
9635 __ Move(reg, RA);
9636
9637 // Remember this offset (the obtained PC value) for later use with constant area.
9638 __ BindPcRelBaseLabel();
9639}
9640
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009641void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9642 // The trampoline uses the same calling convention as dex calling conventions,
9643 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9644 // the method_idx.
9645 HandleInvoke(invoke);
9646}
9647
9648void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9649 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9650}
9651
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009652void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9653 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009654 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009655 locations->SetInAt(0, Location::RequiresRegister());
9656 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009657}
9658
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009659void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9660 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009661 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009662 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009663 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009664 __ LoadFromOffset(kLoadWord,
9665 locations->Out().AsRegister<Register>(),
9666 locations->InAt(0).AsRegister<Register>(),
9667 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009668 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009669 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009670 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009671 __ LoadFromOffset(kLoadWord,
9672 locations->Out().AsRegister<Register>(),
9673 locations->InAt(0).AsRegister<Register>(),
9674 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009675 __ LoadFromOffset(kLoadWord,
9676 locations->Out().AsRegister<Register>(),
9677 locations->Out().AsRegister<Register>(),
9678 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009679 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009680}
9681
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009682void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9683 ATTRIBUTE_UNUSED) {
9684 LOG(FATAL) << "Unreachable";
9685}
9686
9687void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9688 ATTRIBUTE_UNUSED) {
9689 LOG(FATAL) << "Unreachable";
9690}
9691
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009692#undef __
9693#undef QUICK_ENTRY_POINT
9694
9695} // namespace mips
9696} // namespace art