blob: 97604b38a1f868bffcb5f501799325bb65df8e14 [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)),
1020 pc_relative_method_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1021 method_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1022 pc_relative_type_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1023 type_bss_entry_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1024 pc_relative_string_patches_(graph->GetAllocator()->Adapter(kArenaAllocCodeGenerator)),
1025 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) {
1586 const DexFile& dex_file = info.target_dex_file;
1587 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();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001596 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001597 }
1598}
1599
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001600void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001601 DCHECK(linker_patches->empty());
1602 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001603 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001604 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001605 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001606 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001607 pc_relative_string_patches_.size() +
1608 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001609 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001610 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001611 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1612 pc_relative_method_patches_, linker_patches);
1613 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1614 pc_relative_type_patches_, linker_patches);
1615 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1616 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001617 } else {
1618 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001619 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1620 pc_relative_type_patches_, linker_patches);
1621 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1622 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001623 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001624 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1625 method_bss_entry_patches_, linker_patches);
1626 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1627 type_bss_entry_patches_, linker_patches);
1628 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1629 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001630 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001631}
1632
Vladimir Marko65979462017-05-19 17:25:12 +01001633CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001634 MethodReference target_method,
1635 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001636 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001637 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001638 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001639 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001640}
1641
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001642CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001643 MethodReference target_method,
1644 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001645 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001646 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001647 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001648 &method_bss_entry_patches_);
1649}
1650
Alexey Frunze06a46c42016-07-19 15:00:40 -07001651CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001652 const DexFile& dex_file,
1653 dex::TypeIndex type_index,
1654 const PcRelativePatchInfo* info_high) {
1655 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001656}
1657
Vladimir Marko1998cd02017-01-13 13:02:58 +00001658CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001659 const DexFile& dex_file,
1660 dex::TypeIndex type_index,
1661 const PcRelativePatchInfo* info_high) {
1662 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001663}
1664
Vladimir Marko65979462017-05-19 17:25:12 +01001665CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001666 const DexFile& dex_file,
1667 dex::StringIndex string_index,
1668 const PcRelativePatchInfo* info_high) {
1669 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001670}
1671
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001672CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1673 const DexFile& dex_file,
1674 dex::StringIndex string_index,
1675 const PcRelativePatchInfo* info_high) {
1676 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1677}
1678
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001679CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001680 const DexFile& dex_file,
1681 uint32_t offset_or_index,
1682 const PcRelativePatchInfo* info_high,
1683 ArenaDeque<PcRelativePatchInfo>* patches) {
1684 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001685 return &patches->back();
1686}
1687
Alexey Frunze06a46c42016-07-19 15:00:40 -07001688Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1689 return map->GetOrCreate(
1690 value,
1691 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1692}
1693
Alexey Frunze06a46c42016-07-19 15:00:40 -07001694Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001695 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001696}
1697
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001698void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001699 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001700 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001701 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001702 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001703 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001704 if (GetInstructionSetFeatures().IsR6()) {
1705 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001706 __ Bind(&info_high->label);
1707 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001708 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001709 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001710 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001711 } else {
1712 // If base is ZERO, emit NAL to obtain the actual base.
1713 if (base == ZERO) {
1714 // Generate a dummy PC-relative call to obtain PC.
1715 __ Nal();
1716 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001717 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001718 __ Lui(out, /* placeholder */ 0x1234);
1719 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1720 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1721 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001722 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001723 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001724 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001725 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001726 __ Addu(out, out, (base == ZERO) ? RA : base);
1727 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001728 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001729 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001730}
1731
Alexey Frunze627c1a02017-01-30 19:28:14 -08001732CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1733 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001734 dex::StringIndex string_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001735 Handle<mirror::String> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001736 ReserveJitStringRoot(StringReference(&dex_file, string_index), handle);
1737 jit_string_patches_.emplace_back(dex_file, string_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001738 return &jit_string_patches_.back();
1739}
1740
1741CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1742 const DexFile& dex_file,
Vladimir Marko174b2e22017-10-12 13:34:49 +01001743 dex::TypeIndex type_index,
Alexey Frunze627c1a02017-01-30 19:28:14 -08001744 Handle<mirror::Class> handle) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001745 ReserveJitClassRoot(TypeReference(&dex_file, type_index), handle);
1746 jit_class_patches_.emplace_back(dex_file, type_index.index_);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001747 return &jit_class_patches_.back();
1748}
1749
1750void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1751 const uint8_t* roots_data,
1752 const CodeGeneratorMIPS::JitPatchInfo& info,
1753 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001754 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1755 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001756 uintptr_t address =
1757 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1758 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1759 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001760 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1761 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1762 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1763 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001764 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001765 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1766 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001767 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001768 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001769 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1770 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001771 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001772 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1773 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001774}
1775
1776void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1777 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001778 StringReference string_reference(&info.target_dex_file, dex::StringIndex(info.index));
1779 uint64_t index_in_table = GetJitStringRootIndex(string_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001780 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001781 }
1782 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01001783 TypeReference type_reference(&info.target_dex_file, dex::TypeIndex(info.index));
1784 uint64_t index_in_table = GetJitClassRootIndex(type_reference);
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001785 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001786 }
1787}
1788
Goran Jakovljevice114da22016-12-26 14:21:43 +01001789void CodeGeneratorMIPS::MarkGCCard(Register object,
1790 Register value,
1791 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001792 MipsLabel done;
1793 Register card = AT;
1794 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001795 if (value_can_be_null) {
1796 __ Beqz(value, &done);
1797 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001798 __ LoadFromOffset(kLoadWord,
1799 card,
1800 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001801 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001802 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1803 __ Addu(temp, card, temp);
1804 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001805 if (value_can_be_null) {
1806 __ Bind(&done);
1807 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001808}
1809
David Brazdil58282f42016-01-14 12:45:10 +00001810void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001811 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1812 blocked_core_registers_[ZERO] = true;
1813 blocked_core_registers_[K0] = true;
1814 blocked_core_registers_[K1] = true;
1815 blocked_core_registers_[GP] = true;
1816 blocked_core_registers_[SP] = true;
1817 blocked_core_registers_[RA] = true;
1818
1819 // AT and TMP(T8) are used as temporary/scratch registers
1820 // (similar to how AT is used by MIPS assemblers).
1821 blocked_core_registers_[AT] = true;
1822 blocked_core_registers_[TMP] = true;
1823 blocked_fpu_registers_[FTMP] = true;
1824
Goran Jakovljevice7de5ec2017-12-14 10:25:20 +01001825 if (GetInstructionSetFeatures().HasMsa()) {
1826 // To be used just for MSA instructions.
1827 blocked_fpu_registers_[FTMP2] = true;
1828 }
1829
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001830 // Reserve suspend and thread registers.
1831 blocked_core_registers_[S0] = true;
1832 blocked_core_registers_[TR] = true;
1833
1834 // Reserve T9 for function calls
1835 blocked_core_registers_[T9] = true;
1836
1837 // Reserve odd-numbered FPU registers.
1838 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1839 blocked_fpu_registers_[i] = true;
1840 }
1841
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001842 if (GetGraph()->IsDebuggable()) {
1843 // Stubs do not save callee-save floating point registers. If the graph
1844 // is debuggable, we need to deal with these registers differently. For
1845 // now, just block them.
1846 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1847 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1848 }
1849 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001850}
1851
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001852size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1853 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1854 return kMipsWordSize;
1855}
1856
1857size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1858 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1859 return kMipsWordSize;
1860}
1861
1862size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001863 if (GetGraph()->HasSIMD()) {
1864 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1865 } else {
1866 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1867 }
1868 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001869}
1870
1871size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001872 if (GetGraph()->HasSIMD()) {
1873 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1874 } else {
1875 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1876 }
1877 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001878}
1879
1880void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001881 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001882}
1883
1884void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001885 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001886}
1887
Serban Constantinescufca16662016-07-14 09:21:59 +01001888constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1889
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001890void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1891 HInstruction* instruction,
1892 uint32_t dex_pc,
1893 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001894 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001895 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1896 IsDirectEntrypoint(entrypoint));
1897 if (EntrypointRequiresStackMap(entrypoint)) {
1898 RecordPcInfo(instruction, dex_pc, slow_path);
1899 }
1900}
1901
1902void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1903 HInstruction* instruction,
1904 SlowPathCode* slow_path,
1905 bool direct) {
1906 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1907 GenerateInvokeRuntime(entry_point_offset, direct);
1908}
1909
1910void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001911 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001912 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001913 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001914 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001915 // Reserve argument space on stack (for $a0-$a3) for
1916 // entrypoints that directly reference native implementations.
1917 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001918 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001919 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001920 } else {
1921 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001922 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001923 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001924}
1925
1926void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1927 Register class_reg) {
Vladimir Markodc682aa2018-01-04 18:42:57 +00001928 constexpr size_t status_lsb_position = SubtypeCheckBits::BitStructSizeOf();
1929 const size_t status_byte_offset =
1930 mirror::Class::StatusOffset().SizeValue() + (status_lsb_position / kBitsPerByte);
1931 constexpr uint32_t shifted_initialized_value =
1932 enum_cast<uint32_t>(ClassStatus::kInitialized) << (status_lsb_position % kBitsPerByte);
1933
1934 __ LoadFromOffset(kLoadUnsignedByte, TMP, class_reg, status_byte_offset);
1935 __ LoadConst32(AT, shifted_initialized_value);
Vladimir Marko2c64a832018-01-04 11:31:56 +00001936 __ Bltu(TMP, AT, slow_path->GetEntryLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1938 __ Sync(0);
1939 __ Bind(slow_path->GetExitLabel());
1940}
1941
1942void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1943 __ Sync(0); // Only stype 0 is supported.
1944}
1945
1946void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1947 HBasicBlock* successor) {
1948 SuspendCheckSlowPathMIPS* slow_path =
Chris Larsena2045912017-11-02 12:39:54 -07001949 down_cast<SuspendCheckSlowPathMIPS*>(instruction->GetSlowPath());
1950
1951 if (slow_path == nullptr) {
1952 slow_path =
1953 new (codegen_->GetScopedAllocator()) SuspendCheckSlowPathMIPS(instruction, successor);
1954 instruction->SetSlowPath(slow_path);
1955 codegen_->AddSlowPath(slow_path);
1956 if (successor != nullptr) {
1957 DCHECK(successor->IsLoopHeader());
1958 }
1959 } else {
1960 DCHECK_EQ(slow_path->GetSuccessor(), successor);
1961 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001962
1963 __ LoadFromOffset(kLoadUnsignedHalfword,
1964 TMP,
1965 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001966 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001967 if (successor == nullptr) {
1968 __ Bnez(TMP, slow_path->GetEntryLabel());
1969 __ Bind(slow_path->GetReturnLabel());
1970 } else {
1971 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1972 __ B(slow_path->GetEntryLabel());
1973 // slow_path will return to GetLabelOf(successor).
1974 }
1975}
1976
1977InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1978 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001979 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001980 assembler_(codegen->GetAssembler()),
1981 codegen_(codegen) {}
1982
1983void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1984 DCHECK_EQ(instruction->InputCount(), 2U);
Vladimir Markoca6fff82017-10-03 14:49:14 +01001985 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001986 DataType::Type type = instruction->GetResultType();
Lena Djokic38530172017-11-16 11:11:50 +01001987 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001988 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001989 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001990 locations->SetInAt(0, Location::RequiresRegister());
1991 HInstruction* right = instruction->InputAt(1);
1992 bool can_use_imm = false;
1993 if (right->IsConstant()) {
1994 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1995 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1996 can_use_imm = IsUint<16>(imm);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001997 } else {
Lena Djokic38530172017-11-16 11:11:50 +01001998 DCHECK(instruction->IsSub() || instruction->IsAdd());
1999 if (instruction->IsSub()) {
2000 imm = -imm;
2001 }
2002 if (isR6) {
2003 bool single_use = right->GetUses().HasExactlyOneElement();
2004 int16_t imm_high = High16Bits(imm);
2005 int16_t imm_low = Low16Bits(imm);
2006 if (imm_low < 0) {
2007 imm_high += 1;
2008 }
2009 can_use_imm = !((imm_high != 0) && (imm_low != 0)) || single_use;
2010 } else {
2011 can_use_imm = IsInt<16>(imm);
2012 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002013 }
2014 }
2015 if (can_use_imm)
2016 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2017 else
2018 locations->SetInAt(1, Location::RequiresRegister());
2019 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2020 break;
2021 }
2022
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002023 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002024 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002025 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2026 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002027 break;
2028 }
2029
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002030 case DataType::Type::kFloat32:
2031 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002032 DCHECK(instruction->IsAdd() || instruction->IsSub());
2033 locations->SetInAt(0, Location::RequiresFpuRegister());
2034 locations->SetInAt(1, Location::RequiresFpuRegister());
2035 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2036 break;
2037
2038 default:
2039 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2040 }
2041}
2042
2043void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002044 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002045 LocationSummary* locations = instruction->GetLocations();
Lena Djokic38530172017-11-16 11:11:50 +01002046 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002047
2048 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002049 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002050 Register dst = locations->Out().AsRegister<Register>();
2051 Register lhs = locations->InAt(0).AsRegister<Register>();
2052 Location rhs_location = locations->InAt(1);
2053
2054 Register rhs_reg = ZERO;
2055 int32_t rhs_imm = 0;
2056 bool use_imm = rhs_location.IsConstant();
2057 if (use_imm) {
2058 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2059 } else {
2060 rhs_reg = rhs_location.AsRegister<Register>();
2061 }
2062
2063 if (instruction->IsAnd()) {
2064 if (use_imm)
2065 __ Andi(dst, lhs, rhs_imm);
2066 else
2067 __ And(dst, lhs, rhs_reg);
2068 } else if (instruction->IsOr()) {
2069 if (use_imm)
2070 __ Ori(dst, lhs, rhs_imm);
2071 else
2072 __ Or(dst, lhs, rhs_reg);
2073 } else if (instruction->IsXor()) {
2074 if (use_imm)
2075 __ Xori(dst, lhs, rhs_imm);
2076 else
2077 __ Xor(dst, lhs, rhs_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002078 } else {
Lena Djokic38530172017-11-16 11:11:50 +01002079 DCHECK(instruction->IsAdd() || instruction->IsSub());
2080 if (use_imm) {
2081 if (instruction->IsSub()) {
2082 rhs_imm = -rhs_imm;
2083 }
2084 if (IsInt<16>(rhs_imm)) {
2085 __ Addiu(dst, lhs, rhs_imm);
2086 } else {
2087 DCHECK(isR6);
2088 int16_t rhs_imm_high = High16Bits(rhs_imm);
2089 int16_t rhs_imm_low = Low16Bits(rhs_imm);
2090 if (rhs_imm_low < 0) {
2091 rhs_imm_high += 1;
2092 }
2093 __ Aui(dst, lhs, rhs_imm_high);
2094 if (rhs_imm_low != 0) {
2095 __ Addiu(dst, dst, rhs_imm_low);
2096 }
2097 }
2098 } else if (instruction->IsAdd()) {
2099 __ Addu(dst, lhs, rhs_reg);
2100 } else {
2101 DCHECK(instruction->IsSub());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002102 __ Subu(dst, lhs, rhs_reg);
Lena Djokic38530172017-11-16 11:11:50 +01002103 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002104 }
2105 break;
2106 }
2107
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002108 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002109 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2110 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2111 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2112 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002113 Location rhs_location = locations->InAt(1);
2114 bool use_imm = rhs_location.IsConstant();
2115 if (!use_imm) {
2116 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2117 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2118 if (instruction->IsAnd()) {
2119 __ And(dst_low, lhs_low, rhs_low);
2120 __ And(dst_high, lhs_high, rhs_high);
2121 } else if (instruction->IsOr()) {
2122 __ Or(dst_low, lhs_low, rhs_low);
2123 __ Or(dst_high, lhs_high, rhs_high);
2124 } else if (instruction->IsXor()) {
2125 __ Xor(dst_low, lhs_low, rhs_low);
2126 __ Xor(dst_high, lhs_high, rhs_high);
2127 } else if (instruction->IsAdd()) {
2128 if (lhs_low == rhs_low) {
2129 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2130 __ Slt(TMP, lhs_low, ZERO);
2131 __ Addu(dst_low, lhs_low, rhs_low);
2132 } else {
2133 __ Addu(dst_low, lhs_low, rhs_low);
2134 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2135 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2136 }
2137 __ Addu(dst_high, lhs_high, rhs_high);
2138 __ Addu(dst_high, dst_high, TMP);
2139 } else {
2140 DCHECK(instruction->IsSub());
2141 __ Sltu(TMP, lhs_low, rhs_low);
2142 __ Subu(dst_low, lhs_low, rhs_low);
2143 __ Subu(dst_high, lhs_high, rhs_high);
2144 __ Subu(dst_high, dst_high, TMP);
2145 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002146 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002147 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2148 if (instruction->IsOr()) {
2149 uint32_t low = Low32Bits(value);
2150 uint32_t high = High32Bits(value);
2151 if (IsUint<16>(low)) {
2152 if (dst_low != lhs_low || low != 0) {
2153 __ Ori(dst_low, lhs_low, low);
2154 }
2155 } else {
2156 __ LoadConst32(TMP, low);
2157 __ Or(dst_low, lhs_low, TMP);
2158 }
2159 if (IsUint<16>(high)) {
2160 if (dst_high != lhs_high || high != 0) {
2161 __ Ori(dst_high, lhs_high, high);
2162 }
2163 } else {
2164 if (high != low) {
2165 __ LoadConst32(TMP, high);
2166 }
2167 __ Or(dst_high, lhs_high, TMP);
2168 }
2169 } else if (instruction->IsXor()) {
2170 uint32_t low = Low32Bits(value);
2171 uint32_t high = High32Bits(value);
2172 if (IsUint<16>(low)) {
2173 if (dst_low != lhs_low || low != 0) {
2174 __ Xori(dst_low, lhs_low, low);
2175 }
2176 } else {
2177 __ LoadConst32(TMP, low);
2178 __ Xor(dst_low, lhs_low, TMP);
2179 }
2180 if (IsUint<16>(high)) {
2181 if (dst_high != lhs_high || high != 0) {
2182 __ Xori(dst_high, lhs_high, high);
2183 }
2184 } else {
2185 if (high != low) {
2186 __ LoadConst32(TMP, high);
2187 }
2188 __ Xor(dst_high, lhs_high, TMP);
2189 }
2190 } else if (instruction->IsAnd()) {
2191 uint32_t low = Low32Bits(value);
2192 uint32_t high = High32Bits(value);
2193 if (IsUint<16>(low)) {
2194 __ Andi(dst_low, lhs_low, low);
2195 } else if (low != 0xFFFFFFFF) {
2196 __ LoadConst32(TMP, low);
2197 __ And(dst_low, lhs_low, TMP);
2198 } else if (dst_low != lhs_low) {
2199 __ Move(dst_low, lhs_low);
2200 }
2201 if (IsUint<16>(high)) {
2202 __ Andi(dst_high, lhs_high, high);
2203 } else if (high != 0xFFFFFFFF) {
2204 if (high != low) {
2205 __ LoadConst32(TMP, high);
2206 }
2207 __ And(dst_high, lhs_high, TMP);
2208 } else if (dst_high != lhs_high) {
2209 __ Move(dst_high, lhs_high);
2210 }
2211 } else {
2212 if (instruction->IsSub()) {
2213 value = -value;
2214 } else {
2215 DCHECK(instruction->IsAdd());
2216 }
2217 int32_t low = Low32Bits(value);
2218 int32_t high = High32Bits(value);
2219 if (IsInt<16>(low)) {
2220 if (dst_low != lhs_low || low != 0) {
2221 __ Addiu(dst_low, lhs_low, low);
2222 }
2223 if (low != 0) {
2224 __ Sltiu(AT, dst_low, low);
2225 }
2226 } else {
2227 __ LoadConst32(TMP, low);
2228 __ Addu(dst_low, lhs_low, TMP);
2229 __ Sltu(AT, dst_low, TMP);
2230 }
2231 if (IsInt<16>(high)) {
2232 if (dst_high != lhs_high || high != 0) {
2233 __ Addiu(dst_high, lhs_high, high);
2234 }
2235 } else {
2236 if (high != low) {
2237 __ LoadConst32(TMP, high);
2238 }
2239 __ Addu(dst_high, lhs_high, TMP);
2240 }
2241 if (low != 0) {
2242 __ Addu(dst_high, dst_high, AT);
2243 }
2244 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002245 }
2246 break;
2247 }
2248
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002249 case DataType::Type::kFloat32:
2250 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002251 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2252 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2253 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2254 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002255 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002256 __ AddS(dst, lhs, rhs);
2257 } else {
2258 __ AddD(dst, lhs, rhs);
2259 }
2260 } else {
2261 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002262 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002263 __ SubS(dst, lhs, rhs);
2264 } else {
2265 __ SubD(dst, lhs, rhs);
2266 }
2267 }
2268 break;
2269 }
2270
2271 default:
2272 LOG(FATAL) << "Unexpected binary operation type " << type;
2273 }
2274}
2275
2276void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002277 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002278
Vladimir Markoca6fff82017-10-03 14:49:14 +01002279 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002280 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002281 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002282 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002283 locations->SetInAt(0, Location::RequiresRegister());
2284 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2285 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2286 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002287 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002288 locations->SetInAt(0, Location::RequiresRegister());
2289 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2290 locations->SetOut(Location::RequiresRegister());
2291 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002292 default:
2293 LOG(FATAL) << "Unexpected shift type " << type;
2294 }
2295}
2296
2297static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2298
2299void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002300 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002301 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002302 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303
2304 Location rhs_location = locations->InAt(1);
2305 bool use_imm = rhs_location.IsConstant();
2306 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2307 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002308 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002309 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002310 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002311 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2312 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002313
2314 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002315 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002316 Register dst = locations->Out().AsRegister<Register>();
2317 Register lhs = locations->InAt(0).AsRegister<Register>();
2318 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002319 if (shift_value == 0) {
2320 if (dst != lhs) {
2321 __ Move(dst, lhs);
2322 }
2323 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002324 __ Sll(dst, lhs, shift_value);
2325 } else if (instr->IsShr()) {
2326 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002327 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002328 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002329 } else {
2330 if (has_ins_rotr) {
2331 __ Rotr(dst, lhs, shift_value);
2332 } else {
2333 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2334 __ Srl(dst, lhs, shift_value);
2335 __ Or(dst, dst, TMP);
2336 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002337 }
2338 } else {
2339 if (instr->IsShl()) {
2340 __ Sllv(dst, lhs, rhs_reg);
2341 } else if (instr->IsShr()) {
2342 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002343 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002344 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002345 } else {
2346 if (has_ins_rotr) {
2347 __ Rotrv(dst, lhs, rhs_reg);
2348 } else {
2349 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002350 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2351 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2352 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2353 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2354 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002355 __ Sllv(TMP, lhs, TMP);
2356 __ Srlv(dst, lhs, rhs_reg);
2357 __ Or(dst, dst, TMP);
2358 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002359 }
2360 }
2361 break;
2362 }
2363
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002364 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2366 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2367 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2368 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2369 if (use_imm) {
2370 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002371 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002372 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002373 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002374 if (instr->IsShl()) {
2375 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2376 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2377 __ Sll(dst_low, lhs_low, shift_value);
2378 } else if (instr->IsShr()) {
2379 __ Srl(dst_low, lhs_low, shift_value);
2380 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2381 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002382 } else if (instr->IsUShr()) {
2383 __ Srl(dst_low, lhs_low, shift_value);
2384 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2385 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002386 } else {
2387 __ Srl(dst_low, lhs_low, shift_value);
2388 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2389 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002390 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002391 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002392 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002393 if (instr->IsShl()) {
2394 __ Sll(dst_low, lhs_low, shift_value);
2395 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2396 __ Sll(dst_high, lhs_high, shift_value);
2397 __ Or(dst_high, dst_high, TMP);
2398 } else if (instr->IsShr()) {
2399 __ Sra(dst_high, lhs_high, shift_value);
2400 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2401 __ Srl(dst_low, lhs_low, shift_value);
2402 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002403 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002404 __ Srl(dst_high, lhs_high, shift_value);
2405 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2406 __ Srl(dst_low, lhs_low, shift_value);
2407 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002408 } else {
2409 __ Srl(TMP, lhs_low, shift_value);
2410 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2411 __ Or(dst_low, dst_low, TMP);
2412 __ Srl(TMP, lhs_high, shift_value);
2413 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2414 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002415 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002416 }
2417 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002418 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002419 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002420 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002421 __ Move(dst_low, ZERO);
2422 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002423 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002424 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002425 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002426 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002427 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002428 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002429 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002430 // 64-bit rotation by 32 is just a swap.
2431 __ Move(dst_low, lhs_high);
2432 __ Move(dst_high, lhs_low);
2433 } else {
2434 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002435 __ Srl(dst_low, lhs_high, shift_value_high);
2436 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2437 __ Srl(dst_high, lhs_low, shift_value_high);
2438 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002439 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002440 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2441 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002442 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002443 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2444 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002445 __ Or(dst_high, dst_high, TMP);
2446 }
2447 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002448 }
2449 }
2450 } else {
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002451 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002452 MipsLabel done;
2453 if (instr->IsShl()) {
2454 __ Sllv(dst_low, lhs_low, rhs_reg);
2455 __ Nor(AT, ZERO, rhs_reg);
2456 __ Srl(TMP, lhs_low, 1);
2457 __ Srlv(TMP, TMP, AT);
2458 __ Sllv(dst_high, lhs_high, rhs_reg);
2459 __ Or(dst_high, dst_high, TMP);
2460 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002461 if (isR6) {
2462 __ Beqzc(TMP, &done, /* is_bare */ true);
2463 __ Move(dst_high, dst_low);
2464 __ Move(dst_low, ZERO);
2465 } else {
2466 __ Movn(dst_high, dst_low, TMP);
2467 __ Movn(dst_low, ZERO, TMP);
2468 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002469 } else if (instr->IsShr()) {
2470 __ Srav(dst_high, lhs_high, rhs_reg);
2471 __ Nor(AT, ZERO, rhs_reg);
2472 __ Sll(TMP, lhs_high, 1);
2473 __ Sllv(TMP, TMP, AT);
2474 __ Srlv(dst_low, lhs_low, rhs_reg);
2475 __ Or(dst_low, dst_low, TMP);
2476 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002477 if (isR6) {
2478 __ Beqzc(TMP, &done, /* is_bare */ true);
2479 __ Move(dst_low, dst_high);
2480 __ Sra(dst_high, dst_high, 31);
2481 } else {
2482 __ Sra(AT, dst_high, 31);
2483 __ Movn(dst_low, dst_high, TMP);
2484 __ Movn(dst_high, AT, TMP);
2485 }
Alexey Frunze92d90602015-12-18 18:16:36 -08002486 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002487 __ Srlv(dst_high, lhs_high, rhs_reg);
2488 __ Nor(AT, ZERO, rhs_reg);
2489 __ Sll(TMP, lhs_high, 1);
2490 __ Sllv(TMP, TMP, AT);
2491 __ Srlv(dst_low, lhs_low, rhs_reg);
2492 __ Or(dst_low, dst_low, TMP);
2493 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002494 if (isR6) {
2495 __ Beqzc(TMP, &done, /* is_bare */ true);
2496 __ Move(dst_low, dst_high);
2497 __ Move(dst_high, ZERO);
2498 } else {
2499 __ Movn(dst_low, dst_high, TMP);
2500 __ Movn(dst_high, ZERO, TMP);
2501 }
2502 } else { // Rotate.
Alexey Frunze92d90602015-12-18 18:16:36 -08002503 __ Nor(AT, ZERO, rhs_reg);
2504 __ Srlv(TMP, lhs_low, rhs_reg);
2505 __ Sll(dst_low, lhs_high, 1);
2506 __ Sllv(dst_low, dst_low, AT);
2507 __ Or(dst_low, dst_low, TMP);
2508 __ Srlv(TMP, lhs_high, rhs_reg);
2509 __ Sll(dst_high, lhs_low, 1);
2510 __ Sllv(dst_high, dst_high, AT);
2511 __ Or(dst_high, dst_high, TMP);
2512 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
Chris Larsen3e5fecd2017-11-09 14:21:28 -08002513 if (isR6) {
2514 __ Beqzc(TMP, &done, /* is_bare */ true);
2515 __ Move(TMP, dst_high);
2516 __ Move(dst_high, dst_low);
2517 __ Move(dst_low, TMP);
2518 } else {
2519 __ Movn(AT, dst_high, TMP);
2520 __ Movn(dst_high, dst_low, TMP);
2521 __ Movn(dst_low, AT, TMP);
2522 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002523 }
2524 __ Bind(&done);
2525 }
2526 break;
2527 }
2528
2529 default:
2530 LOG(FATAL) << "Unexpected shift operation type " << type;
2531 }
2532}
2533
2534void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2535 HandleBinaryOp(instruction);
2536}
2537
2538void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2539 HandleBinaryOp(instruction);
2540}
2541
2542void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2543 HandleBinaryOp(instruction);
2544}
2545
2546void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2547 HandleBinaryOp(instruction);
2548}
2549
2550void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002551 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002552 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002553 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002554 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01002555 new (GetGraph()->GetAllocator()) LocationSummary(instruction,
2556 object_array_get_with_read_barrier
2557 ? LocationSummary::kCallOnSlowPath
2558 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002559 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2560 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2561 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002562 locations->SetInAt(0, Location::RequiresRegister());
2563 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002564 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002565 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2566 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002567 // The output overlaps in the case of an object array get with
2568 // read barriers enabled: we do not want the move to overwrite the
2569 // array's location, as we need it to emit the read barrier.
2570 locations->SetOut(Location::RequiresRegister(),
2571 object_array_get_with_read_barrier
2572 ? Location::kOutputOverlap
2573 : Location::kNoOutputOverlap);
2574 }
2575 // We need a temporary register for the read barrier marking slow
2576 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2577 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002578 bool temp_needed = instruction->GetIndex()->IsConstant()
2579 ? !kBakerReadBarrierThunksEnableForFields
2580 : !kBakerReadBarrierThunksEnableForArrays;
2581 if (temp_needed) {
2582 locations->AddTemp(Location::RequiresRegister());
2583 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002584 }
2585}
2586
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002587static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2588 auto null_checker = [codegen, instruction]() {
2589 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002590 };
2591 return null_checker;
2592}
2593
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002594void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2595 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002596 Location obj_loc = locations->InAt(0);
2597 Register obj = obj_loc.AsRegister<Register>();
2598 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002599 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002600 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002601 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002602
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002603 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002604 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2605 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002606 switch (type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002607 case DataType::Type::kBool:
2608 case DataType::Type::kUint8: {
Alexey Frunze15958152017-02-09 19:08:30 -08002609 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002610 if (index.IsConstant()) {
2611 size_t offset =
2612 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002613 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002614 } else {
2615 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002616 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002617 }
2618 break;
2619 }
2620
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002621 case DataType::Type::kInt8: {
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(kLoadSignedByte, 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(kLoadSignedByte, 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::kUint16: {
Alexey Frunze15958152017-02-09 19:08:30 -08002635 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002636 if (maybe_compressed_char_at) {
2637 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2638 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2639 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2640 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2641 "Expecting 0=compressed, 1=uncompressed");
2642 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002643 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002644 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2645 if (maybe_compressed_char_at) {
2646 MipsLabel uncompressed_load, done;
2647 __ Bnez(TMP, &uncompressed_load);
2648 __ LoadFromOffset(kLoadUnsignedByte,
2649 out,
2650 obj,
2651 data_offset + (const_index << TIMES_1));
2652 __ B(&done);
2653 __ Bind(&uncompressed_load);
2654 __ LoadFromOffset(kLoadUnsignedHalfword,
2655 out,
2656 obj,
2657 data_offset + (const_index << TIMES_2));
2658 __ Bind(&done);
2659 } else {
2660 __ LoadFromOffset(kLoadUnsignedHalfword,
2661 out,
2662 obj,
2663 data_offset + (const_index << TIMES_2),
2664 null_checker);
2665 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002666 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002667 Register index_reg = index.AsRegister<Register>();
2668 if (maybe_compressed_char_at) {
2669 MipsLabel uncompressed_load, done;
2670 __ Bnez(TMP, &uncompressed_load);
2671 __ Addu(TMP, obj, index_reg);
2672 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2673 __ B(&done);
2674 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002675 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002676 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2677 __ Bind(&done);
Lena Djokica2901602017-09-21 13:50:52 +02002678 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2679 __ Addu(TMP, index_reg, obj);
2680 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002681 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002682 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002683 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2684 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002685 }
2686 break;
2687 }
2688
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002689 case DataType::Type::kInt16: {
2690 Register out = out_loc.AsRegister<Register>();
2691 if (index.IsConstant()) {
2692 size_t offset =
2693 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
2694 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002695 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2696 __ Addu(TMP, index.AsRegister<Register>(), obj);
2697 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002698 } else {
2699 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
2700 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
2701 }
2702 break;
2703 }
2704
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002705 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002706 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002707 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002708 if (index.IsConstant()) {
2709 size_t offset =
2710 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002711 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002712 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2713 __ Addu(TMP, index.AsRegister<Register>(), obj);
2714 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002715 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002716 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002717 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002718 }
2719 break;
2720 }
2721
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002722 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002723 static_assert(
2724 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2725 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2726 // /* HeapReference<Object> */ out =
2727 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2728 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002729 bool temp_needed = index.IsConstant()
2730 ? !kBakerReadBarrierThunksEnableForFields
2731 : !kBakerReadBarrierThunksEnableForArrays;
2732 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002733 // Note that a potential implicit null check is handled in this
2734 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002735 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2736 if (index.IsConstant()) {
2737 // Array load with a constant index can be treated as a field load.
2738 size_t offset =
2739 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2740 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2741 out_loc,
2742 obj,
2743 offset,
2744 temp,
2745 /* needs_null_check */ false);
2746 } else {
2747 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2748 out_loc,
2749 obj,
2750 data_offset,
2751 index,
2752 temp,
2753 /* needs_null_check */ false);
2754 }
Alexey Frunze15958152017-02-09 19:08:30 -08002755 } else {
2756 Register out = out_loc.AsRegister<Register>();
2757 if (index.IsConstant()) {
2758 size_t offset =
2759 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2760 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2761 // If read barriers are enabled, emit read barriers other than
2762 // Baker's using a slow path (and also unpoison the loaded
2763 // reference, if heap poisoning is enabled).
2764 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2765 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002766 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002767 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2768 // If read barriers are enabled, emit read barriers other than
2769 // Baker's using a slow path (and also unpoison the loaded
2770 // reference, if heap poisoning is enabled).
2771 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2772 out_loc,
2773 out_loc,
2774 obj_loc,
2775 data_offset,
2776 index);
2777 }
2778 }
2779 break;
2780 }
2781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002782 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002783 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002784 if (index.IsConstant()) {
2785 size_t offset =
2786 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002787 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002788 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2789 __ Addu(TMP, index.AsRegister<Register>(), obj);
2790 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002792 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002793 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002794 }
2795 break;
2796 }
2797
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002798 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002799 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002800 if (index.IsConstant()) {
2801 size_t offset =
2802 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002803 __ LoadSFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002804 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2805 __ Addu(TMP, index.AsRegister<Register>(), obj);
2806 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002807 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002808 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002809 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002810 }
2811 break;
2812 }
2813
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002814 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002815 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002816 if (index.IsConstant()) {
2817 size_t offset =
2818 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002819 __ LoadDFromOffset(out, obj, offset, null_checker);
Lena Djokica2901602017-09-21 13:50:52 +02002820 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2821 __ Addu(TMP, index.AsRegister<Register>(), obj);
2822 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002823 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002824 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002825 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002826 }
2827 break;
2828 }
2829
Aart Bik66c158e2018-01-31 12:55:04 -08002830 case DataType::Type::kUint32:
2831 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002832 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002833 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2834 UNREACHABLE();
2835 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002836}
2837
2838void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01002839 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002840 locations->SetInAt(0, Location::RequiresRegister());
2841 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2842}
2843
2844void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2845 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002846 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002847 Register obj = locations->InAt(0).AsRegister<Register>();
2848 Register out = locations->Out().AsRegister<Register>();
2849 __ LoadFromOffset(kLoadWord, out, obj, offset);
2850 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002851 // Mask out compression flag from String's array length.
2852 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2853 __ Srl(out, out, 1u);
2854 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002855}
2856
Alexey Frunzef58b2482016-09-02 22:14:06 -07002857Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2858 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2859 ? Location::ConstantLocation(instruction->AsConstant())
2860 : Location::RequiresRegister();
2861}
2862
2863Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2864 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2865 // We can store a non-zero float or double constant without first loading it into the FPU,
2866 // but we should only prefer this if the constant has a single use.
2867 if (instruction->IsConstant() &&
2868 (instruction->AsConstant()->IsZeroBitPattern() ||
2869 instruction->GetUses().HasExactlyOneElement())) {
2870 return Location::ConstantLocation(instruction->AsConstant());
2871 // Otherwise fall through and require an FPU register for the constant.
2872 }
2873 return Location::RequiresFpuRegister();
2874}
2875
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002876void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002877 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002878
2879 bool needs_write_barrier =
2880 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2881 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2882
Vladimir Markoca6fff82017-10-03 14:49:14 +01002883 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002884 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002885 may_need_runtime_call_for_type_check ?
2886 LocationSummary::kCallOnSlowPath :
2887 LocationSummary::kNoCall);
2888
2889 locations->SetInAt(0, Location::RequiresRegister());
2890 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002891 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002892 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002893 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002894 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2895 }
2896 if (needs_write_barrier) {
2897 // Temporary register for the write barrier.
2898 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002899 }
2900}
2901
2902void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2903 LocationSummary* locations = instruction->GetLocations();
2904 Register obj = locations->InAt(0).AsRegister<Register>();
2905 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002906 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002907 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002908 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002909 bool needs_write_barrier =
2910 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002911 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002912 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002913
2914 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002915 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002916 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002917 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002918 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002919 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002920 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002921 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002922 __ Addu(base_reg, obj, index.AsRegister<Register>());
2923 }
2924 if (value_location.IsConstant()) {
2925 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2926 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2927 } else {
2928 Register value = value_location.AsRegister<Register>();
2929 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002930 }
2931 break;
2932 }
2933
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01002934 case DataType::Type::kUint16:
2935 case DataType::Type::kInt16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002936 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002937 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002938 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Lena Djokica2901602017-09-21 13:50:52 +02002939 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2940 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002941 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002942 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002943 }
2944 if (value_location.IsConstant()) {
2945 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2946 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2947 } else {
2948 Register value = value_location.AsRegister<Register>();
2949 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002950 }
2951 break;
2952 }
2953
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002954 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002955 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2956 if (index.IsConstant()) {
2957 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02002958 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
2959 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunze15958152017-02-09 19:08:30 -08002960 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002961 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002962 }
2963 if (value_location.IsConstant()) {
2964 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2965 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2966 } else {
2967 Register value = value_location.AsRegister<Register>();
2968 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2969 }
2970 break;
2971 }
2972
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002973 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002974 if (value_location.IsConstant()) {
2975 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002976 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002977 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002978 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002979 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002980 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002981 }
Alexey Frunze15958152017-02-09 19:08:30 -08002982 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2983 DCHECK_EQ(value, 0);
2984 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2985 DCHECK(!needs_write_barrier);
2986 DCHECK(!may_need_runtime_call_for_type_check);
2987 break;
2988 }
2989
2990 DCHECK(needs_write_barrier);
2991 Register value = value_location.AsRegister<Register>();
2992 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2993 Register temp2 = TMP; // Doesn't need to survive slow path.
2994 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2995 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2996 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2997 MipsLabel done;
2998 SlowPathCodeMIPS* slow_path = nullptr;
2999
3000 if (may_need_runtime_call_for_type_check) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01003001 slow_path = new (codegen_->GetScopedAllocator()) ArraySetSlowPathMIPS(instruction);
Alexey Frunze15958152017-02-09 19:08:30 -08003002 codegen_->AddSlowPath(slow_path);
3003 if (instruction->GetValueCanBeNull()) {
3004 MipsLabel non_zero;
3005 __ Bnez(value, &non_zero);
3006 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3007 if (index.IsConstant()) {
3008 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003009 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3010 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Alexey Frunzec061de12017-02-14 13:27:23 -08003011 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003012 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08003013 }
Alexey Frunze15958152017-02-09 19:08:30 -08003014 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
3015 __ B(&done);
3016 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003017 }
Alexey Frunze15958152017-02-09 19:08:30 -08003018
3019 // Note that when read barriers are enabled, the type checks
3020 // are performed without read barriers. This is fine, even in
3021 // the case where a class object is in the from-space after
3022 // the flip, as a comparison involving such a type would not
3023 // produce a false positive; it may of course produce a false
3024 // negative, in which case we would take the ArraySet slow
3025 // path.
3026
3027 // /* HeapReference<Class> */ temp1 = obj->klass_
3028 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
3029 __ MaybeUnpoisonHeapReference(temp1);
3030
3031 // /* HeapReference<Class> */ temp1 = temp1->component_type_
3032 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
3033 // /* HeapReference<Class> */ temp2 = value->klass_
3034 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
3035 // If heap poisoning is enabled, no need to unpoison `temp1`
3036 // nor `temp2`, as we are comparing two poisoned references.
3037
3038 if (instruction->StaticTypeOfArrayIsObjectArray()) {
3039 MipsLabel do_put;
3040 __ Beq(temp1, temp2, &do_put);
3041 // If heap poisoning is enabled, the `temp1` reference has
3042 // not been unpoisoned yet; unpoison it now.
3043 __ MaybeUnpoisonHeapReference(temp1);
3044
3045 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3046 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3047 // If heap poisoning is enabled, no need to unpoison
3048 // `temp1`, as we are comparing against null below.
3049 __ Bnez(temp1, slow_path->GetEntryLabel());
3050 __ Bind(&do_put);
3051 } else {
3052 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3053 }
3054 }
3055
3056 Register source = value;
3057 if (kPoisonHeapReferences) {
3058 // Note that in the case where `value` is a null reference,
3059 // we do not enter this block, as a null reference does not
3060 // need poisoning.
3061 __ Move(temp1, value);
3062 __ PoisonHeapReference(temp1);
3063 source = temp1;
3064 }
3065
3066 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3067 if (index.IsConstant()) {
3068 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003069 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003070 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003071 }
3072 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3073
3074 if (!may_need_runtime_call_for_type_check) {
3075 codegen_->MaybeRecordImplicitNullCheck(instruction);
3076 }
3077
3078 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3079
3080 if (done.IsLinked()) {
3081 __ Bind(&done);
3082 }
3083
3084 if (slow_path != nullptr) {
3085 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003086 }
3087 break;
3088 }
3089
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003090 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003091 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003092 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003093 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003094 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3095 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003096 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003097 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003098 }
3099 if (value_location.IsConstant()) {
3100 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3101 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3102 } else {
3103 Register value = value_location.AsRegisterPairLow<Register>();
3104 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003105 }
3106 break;
3107 }
3108
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003109 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003110 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003111 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003112 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Lena Djokica2901602017-09-21 13:50:52 +02003113 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3114 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003115 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003116 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003117 }
3118 if (value_location.IsConstant()) {
3119 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3120 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3121 } else {
3122 FRegister value = value_location.AsFpuRegister<FRegister>();
3123 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003124 }
3125 break;
3126 }
3127
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003128 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003129 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003130 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003131 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Lena Djokica2901602017-09-21 13:50:52 +02003132 } else if (instruction->InputAt(1)->IsIntermediateArrayAddressIndex()) {
3133 __ Addu(base_reg, index.AsRegister<Register>(), obj);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003134 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003135 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003136 }
3137 if (value_location.IsConstant()) {
3138 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3139 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3140 } else {
3141 FRegister value = value_location.AsFpuRegister<FRegister>();
3142 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003143 }
3144 break;
3145 }
3146
Aart Bik66c158e2018-01-31 12:55:04 -08003147 case DataType::Type::kUint32:
3148 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003149 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003150 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3151 UNREACHABLE();
3152 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003153}
3154
Lena Djokica2901602017-09-21 13:50:52 +02003155void LocationsBuilderMIPS::VisitIntermediateArrayAddressIndex(
3156 HIntermediateArrayAddressIndex* instruction) {
3157 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003158 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Lena Djokica2901602017-09-21 13:50:52 +02003159
3160 HIntConstant* shift = instruction->GetShift()->AsIntConstant();
3161
3162 locations->SetInAt(0, Location::RequiresRegister());
3163 locations->SetInAt(1, Location::ConstantLocation(shift));
3164 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3165}
3166
3167void InstructionCodeGeneratorMIPS::VisitIntermediateArrayAddressIndex(
3168 HIntermediateArrayAddressIndex* instruction) {
3169 LocationSummary* locations = instruction->GetLocations();
3170 Register index_reg = locations->InAt(0).AsRegister<Register>();
3171 uint32_t shift = instruction->GetShift()->AsIntConstant()->GetValue();
3172 __ Sll(locations->Out().AsRegister<Register>(), index_reg, shift);
3173}
3174
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003175void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003176 RegisterSet caller_saves = RegisterSet::Empty();
3177 InvokeRuntimeCallingConvention calling_convention;
3178 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3179 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3180 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003181
3182 HInstruction* index = instruction->InputAt(0);
3183 HInstruction* length = instruction->InputAt(1);
3184
3185 bool const_index = false;
3186 bool const_length = false;
3187
3188 if (index->IsConstant()) {
3189 if (length->IsConstant()) {
3190 const_index = true;
3191 const_length = true;
3192 } else {
3193 int32_t index_value = index->AsIntConstant()->GetValue();
3194 if (index_value < 0 || IsInt<16>(index_value + 1)) {
3195 const_index = true;
3196 }
3197 }
3198 } else if (length->IsConstant()) {
3199 int32_t length_value = length->AsIntConstant()->GetValue();
3200 if (IsUint<15>(length_value)) {
3201 const_length = true;
3202 }
3203 }
3204
3205 locations->SetInAt(0, const_index
3206 ? Location::ConstantLocation(index->AsConstant())
3207 : Location::RequiresRegister());
3208 locations->SetInAt(1, const_length
3209 ? Location::ConstantLocation(length->AsConstant())
3210 : Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003211}
3212
3213void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3214 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003215 Location index_loc = locations->InAt(0);
3216 Location length_loc = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003217
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003218 if (length_loc.IsConstant()) {
3219 int32_t length = length_loc.GetConstant()->AsIntConstant()->GetValue();
3220 if (index_loc.IsConstant()) {
3221 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3222 if (index < 0 || index >= length) {
3223 BoundsCheckSlowPathMIPS* slow_path =
3224 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3225 codegen_->AddSlowPath(slow_path);
3226 __ B(slow_path->GetEntryLabel());
3227 } else {
3228 // Nothing to be done.
3229 }
3230 return;
3231 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003232
Goran Jakovljevicdbd43032017-11-15 16:31:56 +01003233 BoundsCheckSlowPathMIPS* slow_path =
3234 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3235 codegen_->AddSlowPath(slow_path);
3236 Register index = index_loc.AsRegister<Register>();
3237 if (length == 0) {
3238 __ B(slow_path->GetEntryLabel());
3239 } else if (length == 1) {
3240 __ Bnez(index, slow_path->GetEntryLabel());
3241 } else {
3242 DCHECK(IsUint<15>(length)) << length;
3243 __ Sltiu(TMP, index, length);
3244 __ Beqz(TMP, slow_path->GetEntryLabel());
3245 }
3246 } else {
3247 Register length = length_loc.AsRegister<Register>();
3248 BoundsCheckSlowPathMIPS* slow_path =
3249 new (codegen_->GetScopedAllocator()) BoundsCheckSlowPathMIPS(instruction);
3250 codegen_->AddSlowPath(slow_path);
3251 if (index_loc.IsConstant()) {
3252 int32_t index = index_loc.GetConstant()->AsIntConstant()->GetValue();
3253 if (index < 0) {
3254 __ B(slow_path->GetEntryLabel());
3255 } else if (index == 0) {
3256 __ Blez(length, slow_path->GetEntryLabel());
3257 } else {
3258 DCHECK(IsInt<16>(index + 1)) << index;
3259 __ Sltiu(TMP, length, index + 1);
3260 __ Bnez(TMP, slow_path->GetEntryLabel());
3261 }
3262 } else {
3263 Register index = index_loc.AsRegister<Register>();
3264 __ Bgeu(index, length, slow_path->GetEntryLabel());
3265 }
3266 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003267}
3268
Alexey Frunze15958152017-02-09 19:08:30 -08003269// Temp is used for read barrier.
3270static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3271 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003272 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003273 (kUseBakerReadBarrier ||
3274 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3275 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3276 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3277 return 1;
3278 }
3279 return 0;
3280}
3281
3282// Extra temp is used for read barrier.
3283static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3284 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3285}
3286
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003287void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003288 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003289 LocationSummary::CallKind call_kind = CodeGenerator::GetCheckCastCallKind(instruction);
Vladimir Markoca6fff82017-10-03 14:49:14 +01003290 LocationSummary* locations =
3291 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003292 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003293 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003294 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003295}
3296
3297void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003298 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003299 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003300 Location obj_loc = locations->InAt(0);
3301 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003302 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003303 Location temp_loc = locations->GetTemp(0);
3304 Register temp = temp_loc.AsRegister<Register>();
3305 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3306 DCHECK_LE(num_temps, 2u);
3307 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003308 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3309 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3310 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3311 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3312 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3313 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3314 const uint32_t object_array_data_offset =
3315 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3316 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003317
Alexey Frunzedfc30af2018-01-24 16:25:10 -08003318 bool is_type_check_slow_path_fatal = CodeGenerator::IsTypeCheckSlowPathFatal(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003319 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003320 new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
3321 instruction, is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003322 codegen_->AddSlowPath(slow_path);
3323
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003324 // Avoid this check if we know `obj` is not null.
3325 if (instruction->MustDoNullCheck()) {
3326 __ Beqz(obj, &done);
3327 }
3328
3329 switch (type_check_kind) {
3330 case TypeCheckKind::kExactCheck:
3331 case TypeCheckKind::kArrayCheck: {
3332 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003333 GenerateReferenceLoadTwoRegisters(instruction,
3334 temp_loc,
3335 obj_loc,
3336 class_offset,
3337 maybe_temp2_loc,
3338 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003339 // Jump to slow path for throwing the exception or doing a
3340 // more involved array check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003341 __ Bne(temp, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003342 break;
3343 }
3344
3345 case TypeCheckKind::kAbstractClassCheck: {
3346 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003347 GenerateReferenceLoadTwoRegisters(instruction,
3348 temp_loc,
3349 obj_loc,
3350 class_offset,
3351 maybe_temp2_loc,
3352 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003353 // If the class is abstract, we eagerly fetch the super class of the
3354 // object to avoid doing a comparison we know will fail.
3355 MipsLabel loop;
3356 __ Bind(&loop);
3357 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003358 GenerateReferenceLoadOneRegister(instruction,
3359 temp_loc,
3360 super_offset,
3361 maybe_temp2_loc,
3362 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003363 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3364 // exception.
3365 __ Beqz(temp, slow_path->GetEntryLabel());
3366 // Otherwise, compare the classes.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003367 __ Bne(temp, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003368 break;
3369 }
3370
3371 case TypeCheckKind::kClassHierarchyCheck: {
3372 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003373 GenerateReferenceLoadTwoRegisters(instruction,
3374 temp_loc,
3375 obj_loc,
3376 class_offset,
3377 maybe_temp2_loc,
3378 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003379 // Walk over the class hierarchy to find a match.
3380 MipsLabel loop;
3381 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003382 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003383 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003384 GenerateReferenceLoadOneRegister(instruction,
3385 temp_loc,
3386 super_offset,
3387 maybe_temp2_loc,
3388 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003389 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3390 // exception. Otherwise, jump to the beginning of the loop.
3391 __ Bnez(temp, &loop);
3392 __ B(slow_path->GetEntryLabel());
3393 break;
3394 }
3395
3396 case TypeCheckKind::kArrayObjectCheck: {
3397 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003398 GenerateReferenceLoadTwoRegisters(instruction,
3399 temp_loc,
3400 obj_loc,
3401 class_offset,
3402 maybe_temp2_loc,
3403 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003404 // Do an exact check.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003405 __ Beq(temp, cls, &done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003406 // Otherwise, we need to check that the object's class is a non-primitive array.
3407 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003408 GenerateReferenceLoadOneRegister(instruction,
3409 temp_loc,
3410 component_offset,
3411 maybe_temp2_loc,
3412 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003413 // If the component type is null, jump to the slow path to throw the exception.
3414 __ Beqz(temp, slow_path->GetEntryLabel());
3415 // Otherwise, the object is indeed an array, further check that this component
3416 // type is not a primitive type.
3417 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3418 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3419 __ Bnez(temp, slow_path->GetEntryLabel());
3420 break;
3421 }
3422
3423 case TypeCheckKind::kUnresolvedCheck:
3424 // We always go into the type check slow path for the unresolved check case.
3425 // We cannot directly call the CheckCast runtime entry point
3426 // without resorting to a type checking slow path here (i.e. by
3427 // calling InvokeRuntime directly), as it would require to
3428 // assign fixed registers for the inputs of this HInstanceOf
3429 // instruction (following the runtime calling convention), which
3430 // might be cluttered by the potential first read barrier
3431 // emission at the beginning of this method.
3432 __ B(slow_path->GetEntryLabel());
3433 break;
3434
3435 case TypeCheckKind::kInterfaceCheck: {
3436 // Avoid read barriers to improve performance of the fast path. We can not get false
3437 // positives by doing this.
3438 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003439 GenerateReferenceLoadTwoRegisters(instruction,
3440 temp_loc,
3441 obj_loc,
3442 class_offset,
3443 maybe_temp2_loc,
3444 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003445 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003446 GenerateReferenceLoadTwoRegisters(instruction,
3447 temp_loc,
3448 temp_loc,
3449 iftable_offset,
3450 maybe_temp2_loc,
3451 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003452 // Iftable is never null.
3453 __ Lw(TMP, temp, array_length_offset);
3454 // Loop through the iftable and check if any class matches.
3455 MipsLabel loop;
3456 __ Bind(&loop);
3457 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3458 __ Beqz(TMP, slow_path->GetEntryLabel());
3459 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3460 __ MaybeUnpoisonHeapReference(AT);
3461 // Go to next interface.
3462 __ Addiu(TMP, TMP, -2);
3463 // Compare the classes and continue the loop if they do not match.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00003464 __ Bne(AT, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003465 break;
3466 }
3467 }
3468
3469 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003470 __ Bind(slow_path->GetExitLabel());
3471}
3472
3473void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3474 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003475 new (GetGraph()->GetAllocator()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003476 locations->SetInAt(0, Location::RequiresRegister());
3477 if (check->HasUses()) {
3478 locations->SetOut(Location::SameAsFirstInput());
3479 }
3480}
3481
3482void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3483 // We assume the class is not null.
Vladimir Marko174b2e22017-10-12 13:34:49 +01003484 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003485 check->GetLoadClass(),
3486 check,
3487 check->GetDexPc(),
3488 true);
3489 codegen_->AddSlowPath(slow_path);
3490 GenerateClassInitializationCheck(slow_path,
3491 check->GetLocations()->InAt(0).AsRegister<Register>());
3492}
3493
3494void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003495 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003496
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003497 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003498 new (GetGraph()->GetAllocator()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003499
3500 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003501 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003502 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003503 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003504 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003505 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003506 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003507 locations->SetInAt(0, Location::RequiresRegister());
3508 locations->SetInAt(1, Location::RequiresRegister());
3509 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3510 break;
3511
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003512 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003513 locations->SetInAt(0, Location::RequiresRegister());
3514 locations->SetInAt(1, Location::RequiresRegister());
3515 // Output overlaps because it is written before doing the low comparison.
3516 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3517 break;
3518
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003519 case DataType::Type::kFloat32:
3520 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003521 locations->SetInAt(0, Location::RequiresFpuRegister());
3522 locations->SetInAt(1, Location::RequiresFpuRegister());
3523 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003524 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003525
3526 default:
3527 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3528 }
3529}
3530
3531void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3532 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003533 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003534 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003535 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003536
3537 // 0 if: left == right
3538 // 1 if: left > right
3539 // -1 if: left < right
3540 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003541 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003542 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003543 case DataType::Type::kInt8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003544 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003545 case DataType::Type::kInt16:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003546 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003547 Register lhs = locations->InAt(0).AsRegister<Register>();
3548 Register rhs = locations->InAt(1).AsRegister<Register>();
3549 __ Slt(TMP, lhs, rhs);
3550 __ Slt(res, rhs, lhs);
3551 __ Subu(res, res, TMP);
3552 break;
3553 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003554 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003555 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003556 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3557 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3558 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3559 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3560 // TODO: more efficient (direct) comparison with a constant.
3561 __ Slt(TMP, lhs_high, rhs_high);
3562 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3563 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3564 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3565 __ Sltu(TMP, lhs_low, rhs_low);
3566 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3567 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3568 __ Bind(&done);
3569 break;
3570 }
3571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003572 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003573 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003574 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3575 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3576 MipsLabel done;
3577 if (isR6) {
3578 __ CmpEqS(FTMP, lhs, rhs);
3579 __ LoadConst32(res, 0);
3580 __ Bc1nez(FTMP, &done);
3581 if (gt_bias) {
3582 __ CmpLtS(FTMP, lhs, rhs);
3583 __ LoadConst32(res, -1);
3584 __ Bc1nez(FTMP, &done);
3585 __ LoadConst32(res, 1);
3586 } else {
3587 __ CmpLtS(FTMP, rhs, lhs);
3588 __ LoadConst32(res, 1);
3589 __ Bc1nez(FTMP, &done);
3590 __ LoadConst32(res, -1);
3591 }
3592 } else {
3593 if (gt_bias) {
3594 __ ColtS(0, lhs, rhs);
3595 __ LoadConst32(res, -1);
3596 __ Bc1t(0, &done);
3597 __ CeqS(0, lhs, rhs);
3598 __ LoadConst32(res, 1);
3599 __ Movt(res, ZERO, 0);
3600 } else {
3601 __ ColtS(0, rhs, lhs);
3602 __ LoadConst32(res, 1);
3603 __ Bc1t(0, &done);
3604 __ CeqS(0, lhs, rhs);
3605 __ LoadConst32(res, -1);
3606 __ Movt(res, ZERO, 0);
3607 }
3608 }
3609 __ Bind(&done);
3610 break;
3611 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003612 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003613 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003614 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3615 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3616 MipsLabel done;
3617 if (isR6) {
3618 __ CmpEqD(FTMP, lhs, rhs);
3619 __ LoadConst32(res, 0);
3620 __ Bc1nez(FTMP, &done);
3621 if (gt_bias) {
3622 __ CmpLtD(FTMP, lhs, rhs);
3623 __ LoadConst32(res, -1);
3624 __ Bc1nez(FTMP, &done);
3625 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003626 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003627 __ CmpLtD(FTMP, rhs, lhs);
3628 __ LoadConst32(res, 1);
3629 __ Bc1nez(FTMP, &done);
3630 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003631 }
3632 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003633 if (gt_bias) {
3634 __ ColtD(0, lhs, rhs);
3635 __ LoadConst32(res, -1);
3636 __ Bc1t(0, &done);
3637 __ CeqD(0, lhs, rhs);
3638 __ LoadConst32(res, 1);
3639 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003640 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003641 __ ColtD(0, rhs, lhs);
3642 __ LoadConst32(res, 1);
3643 __ Bc1t(0, &done);
3644 __ CeqD(0, lhs, rhs);
3645 __ LoadConst32(res, -1);
3646 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003647 }
3648 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003649 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003650 break;
3651 }
3652
3653 default:
3654 LOG(FATAL) << "Unimplemented compare type " << in_type;
3655 }
3656}
3657
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003658void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003659 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003660 switch (instruction->InputAt(0)->GetType()) {
3661 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003662 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003663 locations->SetInAt(0, Location::RequiresRegister());
3664 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3665 break;
3666
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003667 case DataType::Type::kFloat32:
3668 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003669 locations->SetInAt(0, Location::RequiresFpuRegister());
3670 locations->SetInAt(1, Location::RequiresFpuRegister());
3671 break;
3672 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003673 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003674 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3675 }
3676}
3677
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003678void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003679 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003680 return;
3681 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003682
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003683 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003684 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003685
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003686 switch (type) {
3687 default:
3688 // Integer case.
3689 GenerateIntCompare(instruction->GetCondition(), locations);
3690 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003691
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003692 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003693 GenerateLongCompare(instruction->GetCondition(), locations);
3694 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003695
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003696 case DataType::Type::kFloat32:
3697 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003698 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3699 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003700 }
3701}
3702
Alexey Frunze7e99e052015-11-24 19:28:01 -08003703void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3704 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003705
3706 LocationSummary* locations = instruction->GetLocations();
3707 Location second = locations->InAt(1);
3708 DCHECK(second.IsConstant());
Lena Djokic4b8025c2017-12-21 16:15:50 +01003709 int64_t imm = Int64FromConstant(second.GetConstant());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003710 DCHECK(imm == 1 || imm == -1);
3711
Lena Djokic4b8025c2017-12-21 16:15:50 +01003712 if (instruction->GetResultType() == DataType::Type::kInt32) {
3713 Register out = locations->Out().AsRegister<Register>();
3714 Register dividend = locations->InAt(0).AsRegister<Register>();
3715
3716 if (instruction->IsRem()) {
3717 __ Move(out, ZERO);
3718 } else {
3719 if (imm == -1) {
3720 __ Subu(out, ZERO, dividend);
3721 } else if (out != dividend) {
3722 __ Move(out, dividend);
3723 }
3724 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003725 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003726 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3727 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3728 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3729 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3730 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3731
3732 if (instruction->IsRem()) {
3733 __ Move(out_high, ZERO);
3734 __ Move(out_low, ZERO);
3735 } else {
3736 if (imm == -1) {
3737 __ Subu(out_low, ZERO, in_low);
3738 __ Sltu(AT, ZERO, out_low);
3739 __ Subu(out_high, ZERO, in_high);
3740 __ Subu(out_high, out_high, AT);
3741 } else {
3742 __ Move(out_low, in_low);
3743 __ Move(out_high, in_high);
3744 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003745 }
3746 }
3747}
3748
3749void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3750 DCHECK(instruction->IsDiv() || instruction->IsRem());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003751
3752 LocationSummary* locations = instruction->GetLocations();
3753 Location second = locations->InAt(1);
Lena Djokic4b8025c2017-12-21 16:15:50 +01003754 const bool is_r2_or_newer = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
3755 const bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze7e99e052015-11-24 19:28:01 -08003756 DCHECK(second.IsConstant());
3757
Lena Djokic4b8025c2017-12-21 16:15:50 +01003758 if (instruction->GetResultType() == DataType::Type::kInt32) {
3759 Register out = locations->Out().AsRegister<Register>();
3760 Register dividend = locations->InAt(0).AsRegister<Register>();
3761 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3762 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
3763 int ctz_imm = CTZ(abs_imm);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003764
Lena Djokic4b8025c2017-12-21 16:15:50 +01003765 if (instruction->IsDiv()) {
3766 if (ctz_imm == 1) {
3767 // Fast path for division by +/-2, which is very common.
3768 __ Srl(TMP, dividend, 31);
3769 } else {
3770 __ Sra(TMP, dividend, 31);
3771 __ Srl(TMP, TMP, 32 - ctz_imm);
3772 }
3773 __ Addu(out, dividend, TMP);
3774 __ Sra(out, out, ctz_imm);
3775 if (imm < 0) {
3776 __ Subu(out, ZERO, out);
3777 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003778 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003779 if (ctz_imm == 1) {
3780 // Fast path for modulo +/-2, which is very common.
3781 __ Sra(TMP, dividend, 31);
3782 __ Subu(out, dividend, TMP);
3783 __ Andi(out, out, 1);
3784 __ Addu(out, out, TMP);
3785 } else {
3786 __ Sra(TMP, dividend, 31);
3787 __ Srl(TMP, TMP, 32 - ctz_imm);
3788 __ Addu(out, dividend, TMP);
3789 if (IsUint<16>(abs_imm - 1)) {
3790 __ Andi(out, out, abs_imm - 1);
3791 } else {
3792 if (is_r2_or_newer) {
3793 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3794 } else {
3795 __ Sll(out, out, 32 - ctz_imm);
3796 __ Srl(out, out, 32 - ctz_imm);
3797 }
3798 }
3799 __ Subu(out, out, TMP);
3800 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003801 }
3802 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003803 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt64);
3804 Register out_high = locations->Out().AsRegisterPairHigh<Register>();
3805 Register out_low = locations->Out().AsRegisterPairLow<Register>();
3806 Register in_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3807 Register in_low = locations->InAt(0).AsRegisterPairLow<Register>();
3808 int64_t imm = Int64FromConstant(second.GetConstant());
3809 uint64_t abs_imm = static_cast<uint64_t>(AbsOrMin(imm));
3810 int ctz_imm = CTZ(abs_imm);
3811
3812 if (instruction->IsDiv()) {
3813 if (ctz_imm < 32) {
3814 if (ctz_imm == 1) {
3815 __ Srl(AT, in_high, 31);
Lena Djokica556e6b2017-12-13 12:09:42 +01003816 } else {
Lena Djokic4b8025c2017-12-21 16:15:50 +01003817 __ Sra(AT, in_high, 31);
3818 __ Srl(AT, AT, 32 - ctz_imm);
Lena Djokica556e6b2017-12-13 12:09:42 +01003819 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003820 __ Addu(AT, AT, in_low);
3821 __ Sltu(TMP, AT, in_low);
3822 __ Addu(out_high, in_high, TMP);
3823 __ Srl(out_low, AT, ctz_imm);
3824 if (is_r2_or_newer) {
3825 __ Ins(out_low, out_high, 32 - ctz_imm, ctz_imm);
3826 __ Sra(out_high, out_high, ctz_imm);
3827 } else {
3828 __ Sll(AT, out_high, 32 - ctz_imm);
3829 __ Sra(out_high, out_high, ctz_imm);
3830 __ Or(out_low, out_low, AT);
3831 }
3832 if (imm < 0) {
3833 __ Subu(out_low, ZERO, out_low);
3834 __ Sltu(AT, ZERO, out_low);
3835 __ Subu(out_high, ZERO, out_high);
3836 __ Subu(out_high, out_high, AT);
3837 }
3838 } else if (ctz_imm == 32) {
3839 __ Sra(AT, in_high, 31);
3840 __ Addu(AT, AT, in_low);
3841 __ Sltu(AT, AT, in_low);
3842 __ Addu(out_low, in_high, AT);
3843 if (imm < 0) {
3844 __ Srl(TMP, out_low, 31);
3845 __ Subu(out_low, ZERO, out_low);
3846 __ Sltu(AT, ZERO, out_low);
3847 __ Subu(out_high, TMP, AT);
3848 } else {
3849 __ Sra(out_high, out_low, 31);
3850 }
3851 } else if (ctz_imm < 63) {
3852 __ Sra(AT, in_high, 31);
3853 __ Srl(TMP, AT, 64 - ctz_imm);
3854 __ Addu(AT, AT, in_low);
3855 __ Sltu(AT, AT, in_low);
3856 __ Addu(out_low, in_high, AT);
3857 __ Addu(out_low, out_low, TMP);
3858 __ Sra(out_low, out_low, ctz_imm - 32);
3859 if (imm < 0) {
3860 __ Subu(out_low, ZERO, out_low);
3861 }
3862 __ Sra(out_high, out_low, 31);
3863 } else {
3864 DCHECK_LT(imm, 0);
3865 if (is_r6) {
3866 __ Aui(AT, in_high, 0x8000);
3867 } else {
3868 __ Lui(AT, 0x8000);
3869 __ Xor(AT, AT, in_high);
3870 }
3871 __ Or(AT, AT, in_low);
3872 __ Sltiu(out_low, AT, 1);
3873 __ Move(out_high, ZERO);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003874 }
Lena Djokic4b8025c2017-12-21 16:15:50 +01003875 } else {
3876 if ((ctz_imm == 1) && !is_r6) {
3877 __ Andi(AT, in_low, 1);
3878 __ Sll(TMP, in_low, 31);
3879 __ And(TMP, in_high, TMP);
3880 __ Sra(out_high, TMP, 31);
3881 __ Or(out_low, out_high, AT);
3882 } else if (ctz_imm < 32) {
3883 __ Sra(AT, in_high, 31);
3884 if (ctz_imm <= 16) {
3885 __ Andi(out_low, in_low, abs_imm - 1);
3886 } else if (is_r2_or_newer) {
3887 __ Ext(out_low, in_low, 0, ctz_imm);
3888 } else {
3889 __ Sll(out_low, in_low, 32 - ctz_imm);
3890 __ Srl(out_low, out_low, 32 - ctz_imm);
3891 }
3892 if (is_r6) {
3893 __ Selnez(out_high, AT, out_low);
3894 } else {
3895 __ Movz(AT, ZERO, out_low);
3896 __ Move(out_high, AT);
3897 }
3898 if (is_r2_or_newer) {
3899 __ Ins(out_low, out_high, ctz_imm, 32 - ctz_imm);
3900 } else {
3901 __ Sll(AT, out_high, ctz_imm);
3902 __ Or(out_low, out_low, AT);
3903 }
3904 } else if (ctz_imm == 32) {
3905 __ Sra(AT, in_high, 31);
3906 __ Move(out_low, in_low);
3907 if (is_r6) {
3908 __ Selnez(out_high, AT, out_low);
3909 } else {
3910 __ Movz(AT, ZERO, out_low);
3911 __ Move(out_high, AT);
3912 }
3913 } else if (ctz_imm < 63) {
3914 __ Sra(AT, in_high, 31);
3915 __ Move(TMP, in_low);
3916 if (ctz_imm - 32 <= 16) {
3917 __ Andi(out_high, in_high, (1 << (ctz_imm - 32)) - 1);
3918 } else if (is_r2_or_newer) {
3919 __ Ext(out_high, in_high, 0, ctz_imm - 32);
3920 } else {
3921 __ Sll(out_high, in_high, 64 - ctz_imm);
3922 __ Srl(out_high, out_high, 64 - ctz_imm);
3923 }
3924 __ Move(out_low, TMP);
3925 __ Or(TMP, TMP, out_high);
3926 if (is_r6) {
3927 __ Selnez(AT, AT, TMP);
3928 } else {
3929 __ Movz(AT, ZERO, TMP);
3930 }
3931 if (is_r2_or_newer) {
3932 __ Ins(out_high, AT, ctz_imm - 32, 64 - ctz_imm);
3933 } else {
3934 __ Sll(AT, AT, ctz_imm - 32);
3935 __ Or(out_high, out_high, AT);
3936 }
3937 } else {
3938 if (is_r6) {
3939 __ Aui(AT, in_high, 0x8000);
3940 } else {
3941 __ Lui(AT, 0x8000);
3942 __ Xor(AT, AT, in_high);
3943 }
3944 __ Or(AT, AT, in_low);
3945 __ Sltiu(AT, AT, 1);
3946 __ Sll(AT, AT, 31);
3947 __ Move(out_low, in_low);
3948 __ Xor(out_high, in_high, AT);
3949 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003950 }
3951 }
3952}
3953
3954void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3955 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003956 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003957
3958 LocationSummary* locations = instruction->GetLocations();
3959 Location second = locations->InAt(1);
3960 DCHECK(second.IsConstant());
3961
3962 Register out = locations->Out().AsRegister<Register>();
3963 Register dividend = locations->InAt(0).AsRegister<Register>();
3964 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3965
3966 int64_t magic;
3967 int shift;
3968 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3969
3970 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3971
3972 __ LoadConst32(TMP, magic);
3973 if (isR6) {
3974 __ MuhR6(TMP, dividend, TMP);
3975 } else {
3976 __ MultR2(dividend, TMP);
3977 __ Mfhi(TMP);
3978 }
3979 if (imm > 0 && magic < 0) {
3980 __ Addu(TMP, TMP, dividend);
3981 } else if (imm < 0 && magic > 0) {
3982 __ Subu(TMP, TMP, dividend);
3983 }
3984
3985 if (shift != 0) {
3986 __ Sra(TMP, TMP, shift);
3987 }
3988
3989 if (instruction->IsDiv()) {
3990 __ Sra(out, TMP, 31);
3991 __ Subu(out, TMP, out);
3992 } else {
3993 __ Sra(AT, TMP, 31);
3994 __ Subu(AT, TMP, AT);
3995 __ LoadConst32(TMP, imm);
3996 if (isR6) {
3997 __ MulR6(TMP, AT, TMP);
3998 } else {
3999 __ MulR2(TMP, AT, TMP);
4000 }
4001 __ Subu(out, dividend, TMP);
4002 }
4003}
4004
4005void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
4006 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004007 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08004008
4009 LocationSummary* locations = instruction->GetLocations();
4010 Register out = locations->Out().AsRegister<Register>();
4011 Location second = locations->InAt(1);
4012
4013 if (second.IsConstant()) {
4014 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
4015 if (imm == 0) {
4016 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4017 } else if (imm == 1 || imm == -1) {
4018 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00004019 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08004020 DivRemByPowerOfTwo(instruction);
4021 } else {
4022 DCHECK(imm <= -2 || imm >= 2);
4023 GenerateDivRemWithAnyConstant(instruction);
4024 }
4025 } else {
4026 Register dividend = locations->InAt(0).AsRegister<Register>();
4027 Register divisor = second.AsRegister<Register>();
4028 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4029 if (instruction->IsDiv()) {
4030 if (isR6) {
4031 __ DivR6(out, dividend, divisor);
4032 } else {
4033 __ DivR2(out, dividend, divisor);
4034 }
4035 } else {
4036 if (isR6) {
4037 __ ModR6(out, dividend, divisor);
4038 } else {
4039 __ ModR2(out, dividend, divisor);
4040 }
4041 }
4042 }
4043}
4044
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004045void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004046 DataType::Type type = div->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01004047 bool call_long_div = false;
4048 if (type == DataType::Type::kInt64) {
4049 if (div->InputAt(1)->IsConstant()) {
4050 int64_t imm = CodeGenerator::GetInt64ValueOf(div->InputAt(1)->AsConstant());
4051 call_long_div = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
4052 } else {
4053 call_long_div = true;
4054 }
4055 }
4056 LocationSummary::CallKind call_kind = call_long_div
Serban Constantinescu54ff4822016-07-07 18:03:19 +01004057 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004058 : LocationSummary::kNoCall;
4059
Vladimir Markoca6fff82017-10-03 14:49:14 +01004060 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004061
4062 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004063 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004064 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08004065 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004066 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
4067 break;
4068
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004069 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004070 if (call_long_div) {
4071 InvokeRuntimeCallingConvention calling_convention;
4072 locations->SetInAt(0, Location::RegisterPairLocation(
4073 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
4074 locations->SetInAt(1, Location::RegisterPairLocation(
4075 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
4076 locations->SetOut(calling_convention.GetReturnLocation(type));
4077 } else {
4078 locations->SetInAt(0, Location::RequiresRegister());
4079 locations->SetInAt(1, Location::ConstantLocation(div->InputAt(1)->AsConstant()));
4080 locations->SetOut(Location::RequiresRegister());
4081 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004082 break;
4083 }
4084
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004085 case DataType::Type::kFloat32:
4086 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004087 locations->SetInAt(0, Location::RequiresFpuRegister());
4088 locations->SetInAt(1, Location::RequiresFpuRegister());
4089 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
4090 break;
4091
4092 default:
4093 LOG(FATAL) << "Unexpected div type " << type;
4094 }
4095}
4096
4097void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004098 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004099 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004100
4101 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004102 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08004103 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004104 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004105 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01004106 if (locations->InAt(1).IsConstant()) {
4107 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
4108 if (imm == 0) {
4109 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
4110 } else if (imm == 1 || imm == -1) {
4111 DivRemOneOrMinusOne(instruction);
4112 } else {
4113 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
4114 DivRemByPowerOfTwo(instruction);
4115 }
4116 } else {
4117 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
4118 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
4119 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004120 break;
4121 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004122 case DataType::Type::kFloat32:
4123 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004124 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
4125 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4126 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004127 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004128 __ DivS(dst, lhs, rhs);
4129 } else {
4130 __ DivD(dst, lhs, rhs);
4131 }
4132 break;
4133 }
4134 default:
4135 LOG(FATAL) << "Unexpected div type " << type;
4136 }
4137}
4138
4139void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01004140 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004141 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004142}
4143
4144void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01004145 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01004146 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004147 codegen_->AddSlowPath(slow_path);
4148 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004149 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004150
4151 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004152 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01004153 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004154 case DataType::Type::kInt8:
4155 case DataType::Type::kUint16:
4156 case DataType::Type::kInt16:
4157 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004158 if (value.IsConstant()) {
4159 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
4160 __ B(slow_path->GetEntryLabel());
4161 } else {
4162 // A division by a non-null constant is valid. We don't need to perform
4163 // any check, so simply fall through.
4164 }
4165 } else {
4166 DCHECK(value.IsRegister()) << value;
4167 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
4168 }
4169 break;
4170 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004171 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004172 if (value.IsConstant()) {
4173 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
4174 __ B(slow_path->GetEntryLabel());
4175 } else {
4176 // A division by a non-null constant is valid. We don't need to perform
4177 // any check, so simply fall through.
4178 }
4179 } else {
4180 DCHECK(value.IsRegisterPair()) << value;
4181 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
4182 __ Beqz(TMP, slow_path->GetEntryLabel());
4183 }
4184 break;
4185 }
4186 default:
4187 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
4188 }
4189}
4190
4191void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
4192 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004193 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004194 locations->SetOut(Location::ConstantLocation(constant));
4195}
4196
4197void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
4198 // Will be generated at use site.
4199}
4200
4201void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4202 exit->SetLocations(nullptr);
4203}
4204
4205void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4206}
4207
4208void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4209 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004210 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004211 locations->SetOut(Location::ConstantLocation(constant));
4212}
4213
4214void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4215 // Will be generated at use site.
4216}
4217
4218void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4219 got->SetLocations(nullptr);
4220}
4221
4222void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004223 if (successor->IsExitBlock()) {
4224 DCHECK(got->GetPrevious()->AlwaysThrows());
4225 return; // no code needed
4226 }
4227
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004228 HBasicBlock* block = got->GetBlock();
4229 HInstruction* previous = got->GetPrevious();
4230 HLoopInformation* info = block->GetLoopInformation();
4231
4232 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004233 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4234 __ Lw(AT, SP, kCurrentMethodStackOffset);
4235 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4236 __ Addiu(TMP, TMP, 1);
4237 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004239 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4240 return;
4241 }
4242 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4243 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4244 }
4245 if (!codegen_->GoesToNextBlock(block, successor)) {
4246 __ B(codegen_->GetLabelOf(successor));
4247 }
4248}
4249
4250void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4251 HandleGoto(got, got->GetSuccessor());
4252}
4253
4254void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4255 try_boundary->SetLocations(nullptr);
4256}
4257
4258void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4259 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4260 if (!successor->IsExitBlock()) {
4261 HandleGoto(try_boundary, successor);
4262 }
4263}
4264
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004265void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4266 LocationSummary* locations) {
4267 Register dst = locations->Out().AsRegister<Register>();
4268 Register lhs = locations->InAt(0).AsRegister<Register>();
4269 Location rhs_location = locations->InAt(1);
4270 Register rhs_reg = ZERO;
4271 int64_t rhs_imm = 0;
4272 bool use_imm = rhs_location.IsConstant();
4273 if (use_imm) {
4274 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4275 } else {
4276 rhs_reg = rhs_location.AsRegister<Register>();
4277 }
4278
4279 switch (cond) {
4280 case kCondEQ:
4281 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004282 if (use_imm && IsInt<16>(-rhs_imm)) {
4283 if (rhs_imm == 0) {
4284 if (cond == kCondEQ) {
4285 __ Sltiu(dst, lhs, 1);
4286 } else {
4287 __ Sltu(dst, ZERO, lhs);
4288 }
4289 } else {
4290 __ Addiu(dst, lhs, -rhs_imm);
4291 if (cond == kCondEQ) {
4292 __ Sltiu(dst, dst, 1);
4293 } else {
4294 __ Sltu(dst, ZERO, dst);
4295 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004296 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004297 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004298 if (use_imm && IsUint<16>(rhs_imm)) {
4299 __ Xori(dst, lhs, rhs_imm);
4300 } else {
4301 if (use_imm) {
4302 rhs_reg = TMP;
4303 __ LoadConst32(rhs_reg, rhs_imm);
4304 }
4305 __ Xor(dst, lhs, rhs_reg);
4306 }
4307 if (cond == kCondEQ) {
4308 __ Sltiu(dst, dst, 1);
4309 } else {
4310 __ Sltu(dst, ZERO, dst);
4311 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004312 }
4313 break;
4314
4315 case kCondLT:
4316 case kCondGE:
4317 if (use_imm && IsInt<16>(rhs_imm)) {
4318 __ Slti(dst, lhs, rhs_imm);
4319 } else {
4320 if (use_imm) {
4321 rhs_reg = TMP;
4322 __ LoadConst32(rhs_reg, rhs_imm);
4323 }
4324 __ Slt(dst, lhs, rhs_reg);
4325 }
4326 if (cond == kCondGE) {
4327 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4328 // only the slt instruction but no sge.
4329 __ Xori(dst, dst, 1);
4330 }
4331 break;
4332
4333 case kCondLE:
4334 case kCondGT:
4335 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4336 // Simulate lhs <= rhs via lhs < rhs + 1.
4337 __ Slti(dst, lhs, rhs_imm + 1);
4338 if (cond == kCondGT) {
4339 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4340 // only the slti instruction but no sgti.
4341 __ Xori(dst, dst, 1);
4342 }
4343 } else {
4344 if (use_imm) {
4345 rhs_reg = TMP;
4346 __ LoadConst32(rhs_reg, rhs_imm);
4347 }
4348 __ Slt(dst, rhs_reg, lhs);
4349 if (cond == kCondLE) {
4350 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4351 // only the slt instruction but no sle.
4352 __ Xori(dst, dst, 1);
4353 }
4354 }
4355 break;
4356
4357 case kCondB:
4358 case kCondAE:
4359 if (use_imm && IsInt<16>(rhs_imm)) {
4360 // Sltiu sign-extends its 16-bit immediate operand before
4361 // the comparison and thus lets us compare directly with
4362 // unsigned values in the ranges [0, 0x7fff] and
4363 // [0xffff8000, 0xffffffff].
4364 __ Sltiu(dst, lhs, rhs_imm);
4365 } else {
4366 if (use_imm) {
4367 rhs_reg = TMP;
4368 __ LoadConst32(rhs_reg, rhs_imm);
4369 }
4370 __ Sltu(dst, lhs, rhs_reg);
4371 }
4372 if (cond == kCondAE) {
4373 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4374 // only the sltu instruction but no sgeu.
4375 __ Xori(dst, dst, 1);
4376 }
4377 break;
4378
4379 case kCondBE:
4380 case kCondA:
4381 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4382 // Simulate lhs <= rhs via lhs < rhs + 1.
4383 // Note that this only works if rhs + 1 does not overflow
4384 // to 0, hence the check above.
4385 // Sltiu sign-extends its 16-bit immediate operand before
4386 // the comparison and thus lets us compare directly with
4387 // unsigned values in the ranges [0, 0x7fff] and
4388 // [0xffff8000, 0xffffffff].
4389 __ Sltiu(dst, lhs, rhs_imm + 1);
4390 if (cond == kCondA) {
4391 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4392 // only the sltiu instruction but no sgtiu.
4393 __ Xori(dst, dst, 1);
4394 }
4395 } else {
4396 if (use_imm) {
4397 rhs_reg = TMP;
4398 __ LoadConst32(rhs_reg, rhs_imm);
4399 }
4400 __ Sltu(dst, rhs_reg, lhs);
4401 if (cond == kCondBE) {
4402 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4403 // only the sltu instruction but no sleu.
4404 __ Xori(dst, dst, 1);
4405 }
4406 }
4407 break;
4408 }
4409}
4410
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004411bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4412 LocationSummary* input_locations,
4413 Register dst) {
4414 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4415 Location rhs_location = input_locations->InAt(1);
4416 Register rhs_reg = ZERO;
4417 int64_t rhs_imm = 0;
4418 bool use_imm = rhs_location.IsConstant();
4419 if (use_imm) {
4420 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4421 } else {
4422 rhs_reg = rhs_location.AsRegister<Register>();
4423 }
4424
4425 switch (cond) {
4426 case kCondEQ:
4427 case kCondNE:
4428 if (use_imm && IsInt<16>(-rhs_imm)) {
4429 __ Addiu(dst, lhs, -rhs_imm);
4430 } else if (use_imm && IsUint<16>(rhs_imm)) {
4431 __ Xori(dst, lhs, rhs_imm);
4432 } else {
4433 if (use_imm) {
4434 rhs_reg = TMP;
4435 __ LoadConst32(rhs_reg, rhs_imm);
4436 }
4437 __ Xor(dst, lhs, rhs_reg);
4438 }
4439 return (cond == kCondEQ);
4440
4441 case kCondLT:
4442 case kCondGE:
4443 if (use_imm && IsInt<16>(rhs_imm)) {
4444 __ Slti(dst, lhs, rhs_imm);
4445 } else {
4446 if (use_imm) {
4447 rhs_reg = TMP;
4448 __ LoadConst32(rhs_reg, rhs_imm);
4449 }
4450 __ Slt(dst, lhs, rhs_reg);
4451 }
4452 return (cond == kCondGE);
4453
4454 case kCondLE:
4455 case kCondGT:
4456 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4457 // Simulate lhs <= rhs via lhs < rhs + 1.
4458 __ Slti(dst, lhs, rhs_imm + 1);
4459 return (cond == kCondGT);
4460 } else {
4461 if (use_imm) {
4462 rhs_reg = TMP;
4463 __ LoadConst32(rhs_reg, rhs_imm);
4464 }
4465 __ Slt(dst, rhs_reg, lhs);
4466 return (cond == kCondLE);
4467 }
4468
4469 case kCondB:
4470 case kCondAE:
4471 if (use_imm && IsInt<16>(rhs_imm)) {
4472 // Sltiu sign-extends its 16-bit immediate operand before
4473 // the comparison and thus lets us compare directly with
4474 // unsigned values in the ranges [0, 0x7fff] and
4475 // [0xffff8000, 0xffffffff].
4476 __ Sltiu(dst, lhs, rhs_imm);
4477 } else {
4478 if (use_imm) {
4479 rhs_reg = TMP;
4480 __ LoadConst32(rhs_reg, rhs_imm);
4481 }
4482 __ Sltu(dst, lhs, rhs_reg);
4483 }
4484 return (cond == kCondAE);
4485
4486 case kCondBE:
4487 case kCondA:
4488 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4489 // Simulate lhs <= rhs via lhs < rhs + 1.
4490 // Note that this only works if rhs + 1 does not overflow
4491 // to 0, hence the check above.
4492 // Sltiu sign-extends its 16-bit immediate operand before
4493 // the comparison and thus lets us compare directly with
4494 // unsigned values in the ranges [0, 0x7fff] and
4495 // [0xffff8000, 0xffffffff].
4496 __ Sltiu(dst, lhs, rhs_imm + 1);
4497 return (cond == kCondA);
4498 } else {
4499 if (use_imm) {
4500 rhs_reg = TMP;
4501 __ LoadConst32(rhs_reg, rhs_imm);
4502 }
4503 __ Sltu(dst, rhs_reg, lhs);
4504 return (cond == kCondBE);
4505 }
4506 }
4507}
4508
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004509void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4510 LocationSummary* locations,
4511 MipsLabel* label) {
4512 Register lhs = locations->InAt(0).AsRegister<Register>();
4513 Location rhs_location = locations->InAt(1);
4514 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004515 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004516 bool use_imm = rhs_location.IsConstant();
4517 if (use_imm) {
4518 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4519 } else {
4520 rhs_reg = rhs_location.AsRegister<Register>();
4521 }
4522
4523 if (use_imm && rhs_imm == 0) {
4524 switch (cond) {
4525 case kCondEQ:
4526 case kCondBE: // <= 0 if zero
4527 __ Beqz(lhs, label);
4528 break;
4529 case kCondNE:
4530 case kCondA: // > 0 if non-zero
4531 __ Bnez(lhs, label);
4532 break;
4533 case kCondLT:
4534 __ Bltz(lhs, label);
4535 break;
4536 case kCondGE:
4537 __ Bgez(lhs, label);
4538 break;
4539 case kCondLE:
4540 __ Blez(lhs, label);
4541 break;
4542 case kCondGT:
4543 __ Bgtz(lhs, label);
4544 break;
4545 case kCondB: // always false
4546 break;
4547 case kCondAE: // always true
4548 __ B(label);
4549 break;
4550 }
4551 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004552 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4553 if (isR6 || !use_imm) {
4554 if (use_imm) {
4555 rhs_reg = TMP;
4556 __ LoadConst32(rhs_reg, rhs_imm);
4557 }
4558 switch (cond) {
4559 case kCondEQ:
4560 __ Beq(lhs, rhs_reg, label);
4561 break;
4562 case kCondNE:
4563 __ Bne(lhs, rhs_reg, label);
4564 break;
4565 case kCondLT:
4566 __ Blt(lhs, rhs_reg, label);
4567 break;
4568 case kCondGE:
4569 __ Bge(lhs, rhs_reg, label);
4570 break;
4571 case kCondLE:
4572 __ Bge(rhs_reg, lhs, label);
4573 break;
4574 case kCondGT:
4575 __ Blt(rhs_reg, lhs, label);
4576 break;
4577 case kCondB:
4578 __ Bltu(lhs, rhs_reg, label);
4579 break;
4580 case kCondAE:
4581 __ Bgeu(lhs, rhs_reg, label);
4582 break;
4583 case kCondBE:
4584 __ Bgeu(rhs_reg, lhs, label);
4585 break;
4586 case kCondA:
4587 __ Bltu(rhs_reg, lhs, label);
4588 break;
4589 }
4590 } else {
4591 // Special cases for more efficient comparison with constants on R2.
4592 switch (cond) {
4593 case kCondEQ:
4594 __ LoadConst32(TMP, rhs_imm);
4595 __ Beq(lhs, TMP, label);
4596 break;
4597 case kCondNE:
4598 __ LoadConst32(TMP, rhs_imm);
4599 __ Bne(lhs, TMP, label);
4600 break;
4601 case kCondLT:
4602 if (IsInt<16>(rhs_imm)) {
4603 __ Slti(TMP, lhs, rhs_imm);
4604 __ Bnez(TMP, label);
4605 } else {
4606 __ LoadConst32(TMP, rhs_imm);
4607 __ Blt(lhs, TMP, label);
4608 }
4609 break;
4610 case kCondGE:
4611 if (IsInt<16>(rhs_imm)) {
4612 __ Slti(TMP, lhs, rhs_imm);
4613 __ Beqz(TMP, label);
4614 } else {
4615 __ LoadConst32(TMP, rhs_imm);
4616 __ Bge(lhs, TMP, label);
4617 }
4618 break;
4619 case kCondLE:
4620 if (IsInt<16>(rhs_imm + 1)) {
4621 // Simulate lhs <= rhs via lhs < rhs + 1.
4622 __ Slti(TMP, lhs, rhs_imm + 1);
4623 __ Bnez(TMP, label);
4624 } else {
4625 __ LoadConst32(TMP, rhs_imm);
4626 __ Bge(TMP, lhs, label);
4627 }
4628 break;
4629 case kCondGT:
4630 if (IsInt<16>(rhs_imm + 1)) {
4631 // Simulate lhs > rhs via !(lhs < rhs + 1).
4632 __ Slti(TMP, lhs, rhs_imm + 1);
4633 __ Beqz(TMP, label);
4634 } else {
4635 __ LoadConst32(TMP, rhs_imm);
4636 __ Blt(TMP, lhs, label);
4637 }
4638 break;
4639 case kCondB:
4640 if (IsInt<16>(rhs_imm)) {
4641 __ Sltiu(TMP, lhs, rhs_imm);
4642 __ Bnez(TMP, label);
4643 } else {
4644 __ LoadConst32(TMP, rhs_imm);
4645 __ Bltu(lhs, TMP, label);
4646 }
4647 break;
4648 case kCondAE:
4649 if (IsInt<16>(rhs_imm)) {
4650 __ Sltiu(TMP, lhs, rhs_imm);
4651 __ Beqz(TMP, label);
4652 } else {
4653 __ LoadConst32(TMP, rhs_imm);
4654 __ Bgeu(lhs, TMP, label);
4655 }
4656 break;
4657 case kCondBE:
4658 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4659 // Simulate lhs <= rhs via lhs < rhs + 1.
4660 // Note that this only works if rhs + 1 does not overflow
4661 // to 0, hence the check above.
4662 __ Sltiu(TMP, lhs, rhs_imm + 1);
4663 __ Bnez(TMP, label);
4664 } else {
4665 __ LoadConst32(TMP, rhs_imm);
4666 __ Bgeu(TMP, lhs, label);
4667 }
4668 break;
4669 case kCondA:
4670 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4671 // Simulate lhs > rhs via !(lhs < rhs + 1).
4672 // Note that this only works if rhs + 1 does not overflow
4673 // to 0, hence the check above.
4674 __ Sltiu(TMP, lhs, rhs_imm + 1);
4675 __ Beqz(TMP, label);
4676 } else {
4677 __ LoadConst32(TMP, rhs_imm);
4678 __ Bltu(TMP, lhs, label);
4679 }
4680 break;
4681 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004682 }
4683 }
4684}
4685
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004686void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4687 LocationSummary* locations) {
4688 Register dst = locations->Out().AsRegister<Register>();
4689 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4690 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4691 Location rhs_location = locations->InAt(1);
4692 Register rhs_high = ZERO;
4693 Register rhs_low = ZERO;
4694 int64_t imm = 0;
4695 uint32_t imm_high = 0;
4696 uint32_t imm_low = 0;
4697 bool use_imm = rhs_location.IsConstant();
4698 if (use_imm) {
4699 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4700 imm_high = High32Bits(imm);
4701 imm_low = Low32Bits(imm);
4702 } else {
4703 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4704 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4705 }
4706 if (use_imm && imm == 0) {
4707 switch (cond) {
4708 case kCondEQ:
4709 case kCondBE: // <= 0 if zero
4710 __ Or(dst, lhs_high, lhs_low);
4711 __ Sltiu(dst, dst, 1);
4712 break;
4713 case kCondNE:
4714 case kCondA: // > 0 if non-zero
4715 __ Or(dst, lhs_high, lhs_low);
4716 __ Sltu(dst, ZERO, dst);
4717 break;
4718 case kCondLT:
4719 __ Slt(dst, lhs_high, ZERO);
4720 break;
4721 case kCondGE:
4722 __ Slt(dst, lhs_high, ZERO);
4723 __ Xori(dst, dst, 1);
4724 break;
4725 case kCondLE:
4726 __ Or(TMP, lhs_high, lhs_low);
4727 __ Sra(AT, lhs_high, 31);
4728 __ Sltu(dst, AT, TMP);
4729 __ Xori(dst, dst, 1);
4730 break;
4731 case kCondGT:
4732 __ Or(TMP, lhs_high, lhs_low);
4733 __ Sra(AT, lhs_high, 31);
4734 __ Sltu(dst, AT, TMP);
4735 break;
4736 case kCondB: // always false
4737 __ Andi(dst, dst, 0);
4738 break;
4739 case kCondAE: // always true
4740 __ Ori(dst, ZERO, 1);
4741 break;
4742 }
4743 } else if (use_imm) {
4744 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4745 switch (cond) {
4746 case kCondEQ:
4747 __ LoadConst32(TMP, imm_high);
4748 __ Xor(TMP, TMP, lhs_high);
4749 __ LoadConst32(AT, imm_low);
4750 __ Xor(AT, AT, lhs_low);
4751 __ Or(dst, TMP, AT);
4752 __ Sltiu(dst, dst, 1);
4753 break;
4754 case kCondNE:
4755 __ LoadConst32(TMP, imm_high);
4756 __ Xor(TMP, TMP, lhs_high);
4757 __ LoadConst32(AT, imm_low);
4758 __ Xor(AT, AT, lhs_low);
4759 __ Or(dst, TMP, AT);
4760 __ Sltu(dst, ZERO, dst);
4761 break;
4762 case kCondLT:
4763 case kCondGE:
4764 if (dst == lhs_low) {
4765 __ LoadConst32(TMP, imm_low);
4766 __ Sltu(dst, lhs_low, TMP);
4767 }
4768 __ LoadConst32(TMP, imm_high);
4769 __ Slt(AT, lhs_high, TMP);
4770 __ Slt(TMP, TMP, lhs_high);
4771 if (dst != lhs_low) {
4772 __ LoadConst32(dst, imm_low);
4773 __ Sltu(dst, lhs_low, dst);
4774 }
4775 __ Slt(dst, TMP, dst);
4776 __ Or(dst, dst, AT);
4777 if (cond == kCondGE) {
4778 __ Xori(dst, dst, 1);
4779 }
4780 break;
4781 case kCondGT:
4782 case kCondLE:
4783 if (dst == lhs_low) {
4784 __ LoadConst32(TMP, imm_low);
4785 __ Sltu(dst, TMP, lhs_low);
4786 }
4787 __ LoadConst32(TMP, imm_high);
4788 __ Slt(AT, TMP, lhs_high);
4789 __ Slt(TMP, lhs_high, TMP);
4790 if (dst != lhs_low) {
4791 __ LoadConst32(dst, imm_low);
4792 __ Sltu(dst, dst, lhs_low);
4793 }
4794 __ Slt(dst, TMP, dst);
4795 __ Or(dst, dst, AT);
4796 if (cond == kCondLE) {
4797 __ Xori(dst, dst, 1);
4798 }
4799 break;
4800 case kCondB:
4801 case kCondAE:
4802 if (dst == lhs_low) {
4803 __ LoadConst32(TMP, imm_low);
4804 __ Sltu(dst, lhs_low, TMP);
4805 }
4806 __ LoadConst32(TMP, imm_high);
4807 __ Sltu(AT, lhs_high, TMP);
4808 __ Sltu(TMP, TMP, lhs_high);
4809 if (dst != lhs_low) {
4810 __ LoadConst32(dst, imm_low);
4811 __ Sltu(dst, lhs_low, dst);
4812 }
4813 __ Slt(dst, TMP, dst);
4814 __ Or(dst, dst, AT);
4815 if (cond == kCondAE) {
4816 __ Xori(dst, dst, 1);
4817 }
4818 break;
4819 case kCondA:
4820 case kCondBE:
4821 if (dst == lhs_low) {
4822 __ LoadConst32(TMP, imm_low);
4823 __ Sltu(dst, TMP, lhs_low);
4824 }
4825 __ LoadConst32(TMP, imm_high);
4826 __ Sltu(AT, TMP, lhs_high);
4827 __ Sltu(TMP, lhs_high, TMP);
4828 if (dst != lhs_low) {
4829 __ LoadConst32(dst, imm_low);
4830 __ Sltu(dst, dst, lhs_low);
4831 }
4832 __ Slt(dst, TMP, dst);
4833 __ Or(dst, dst, AT);
4834 if (cond == kCondBE) {
4835 __ Xori(dst, dst, 1);
4836 }
4837 break;
4838 }
4839 } else {
4840 switch (cond) {
4841 case kCondEQ:
4842 __ Xor(TMP, lhs_high, rhs_high);
4843 __ Xor(AT, lhs_low, rhs_low);
4844 __ Or(dst, TMP, AT);
4845 __ Sltiu(dst, dst, 1);
4846 break;
4847 case kCondNE:
4848 __ Xor(TMP, lhs_high, rhs_high);
4849 __ Xor(AT, lhs_low, rhs_low);
4850 __ Or(dst, TMP, AT);
4851 __ Sltu(dst, ZERO, dst);
4852 break;
4853 case kCondLT:
4854 case kCondGE:
4855 __ Slt(TMP, rhs_high, lhs_high);
4856 __ Sltu(AT, lhs_low, rhs_low);
4857 __ Slt(TMP, TMP, AT);
4858 __ Slt(AT, lhs_high, rhs_high);
4859 __ Or(dst, AT, TMP);
4860 if (cond == kCondGE) {
4861 __ Xori(dst, dst, 1);
4862 }
4863 break;
4864 case kCondGT:
4865 case kCondLE:
4866 __ Slt(TMP, lhs_high, rhs_high);
4867 __ Sltu(AT, rhs_low, lhs_low);
4868 __ Slt(TMP, TMP, AT);
4869 __ Slt(AT, rhs_high, lhs_high);
4870 __ Or(dst, AT, TMP);
4871 if (cond == kCondLE) {
4872 __ Xori(dst, dst, 1);
4873 }
4874 break;
4875 case kCondB:
4876 case kCondAE:
4877 __ Sltu(TMP, rhs_high, lhs_high);
4878 __ Sltu(AT, lhs_low, rhs_low);
4879 __ Slt(TMP, TMP, AT);
4880 __ Sltu(AT, lhs_high, rhs_high);
4881 __ Or(dst, AT, TMP);
4882 if (cond == kCondAE) {
4883 __ Xori(dst, dst, 1);
4884 }
4885 break;
4886 case kCondA:
4887 case kCondBE:
4888 __ Sltu(TMP, lhs_high, rhs_high);
4889 __ Sltu(AT, rhs_low, lhs_low);
4890 __ Slt(TMP, TMP, AT);
4891 __ Sltu(AT, rhs_high, lhs_high);
4892 __ Or(dst, AT, TMP);
4893 if (cond == kCondBE) {
4894 __ Xori(dst, dst, 1);
4895 }
4896 break;
4897 }
4898 }
4899}
4900
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004901void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4902 LocationSummary* locations,
4903 MipsLabel* label) {
4904 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4905 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4906 Location rhs_location = locations->InAt(1);
4907 Register rhs_high = ZERO;
4908 Register rhs_low = ZERO;
4909 int64_t imm = 0;
4910 uint32_t imm_high = 0;
4911 uint32_t imm_low = 0;
4912 bool use_imm = rhs_location.IsConstant();
4913 if (use_imm) {
4914 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4915 imm_high = High32Bits(imm);
4916 imm_low = Low32Bits(imm);
4917 } else {
4918 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4919 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4920 }
4921
4922 if (use_imm && imm == 0) {
4923 switch (cond) {
4924 case kCondEQ:
4925 case kCondBE: // <= 0 if zero
4926 __ Or(TMP, lhs_high, lhs_low);
4927 __ Beqz(TMP, label);
4928 break;
4929 case kCondNE:
4930 case kCondA: // > 0 if non-zero
4931 __ Or(TMP, lhs_high, lhs_low);
4932 __ Bnez(TMP, label);
4933 break;
4934 case kCondLT:
4935 __ Bltz(lhs_high, label);
4936 break;
4937 case kCondGE:
4938 __ Bgez(lhs_high, label);
4939 break;
4940 case kCondLE:
4941 __ Or(TMP, lhs_high, lhs_low);
4942 __ Sra(AT, lhs_high, 31);
4943 __ Bgeu(AT, TMP, label);
4944 break;
4945 case kCondGT:
4946 __ Or(TMP, lhs_high, lhs_low);
4947 __ Sra(AT, lhs_high, 31);
4948 __ Bltu(AT, TMP, label);
4949 break;
4950 case kCondB: // always false
4951 break;
4952 case kCondAE: // always true
4953 __ B(label);
4954 break;
4955 }
4956 } else if (use_imm) {
4957 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4958 switch (cond) {
4959 case kCondEQ:
4960 __ LoadConst32(TMP, imm_high);
4961 __ Xor(TMP, TMP, lhs_high);
4962 __ LoadConst32(AT, imm_low);
4963 __ Xor(AT, AT, lhs_low);
4964 __ Or(TMP, TMP, AT);
4965 __ Beqz(TMP, label);
4966 break;
4967 case kCondNE:
4968 __ LoadConst32(TMP, imm_high);
4969 __ Xor(TMP, TMP, lhs_high);
4970 __ LoadConst32(AT, imm_low);
4971 __ Xor(AT, AT, lhs_low);
4972 __ Or(TMP, TMP, AT);
4973 __ Bnez(TMP, label);
4974 break;
4975 case kCondLT:
4976 __ LoadConst32(TMP, imm_high);
4977 __ Blt(lhs_high, TMP, label);
4978 __ Slt(TMP, TMP, lhs_high);
4979 __ LoadConst32(AT, imm_low);
4980 __ Sltu(AT, lhs_low, AT);
4981 __ Blt(TMP, AT, label);
4982 break;
4983 case kCondGE:
4984 __ LoadConst32(TMP, imm_high);
4985 __ Blt(TMP, lhs_high, label);
4986 __ Slt(TMP, lhs_high, TMP);
4987 __ LoadConst32(AT, imm_low);
4988 __ Sltu(AT, lhs_low, AT);
4989 __ Or(TMP, TMP, AT);
4990 __ Beqz(TMP, label);
4991 break;
4992 case kCondLE:
4993 __ LoadConst32(TMP, imm_high);
4994 __ Blt(lhs_high, TMP, label);
4995 __ Slt(TMP, TMP, lhs_high);
4996 __ LoadConst32(AT, imm_low);
4997 __ Sltu(AT, AT, lhs_low);
4998 __ Or(TMP, TMP, AT);
4999 __ Beqz(TMP, label);
5000 break;
5001 case kCondGT:
5002 __ LoadConst32(TMP, imm_high);
5003 __ Blt(TMP, lhs_high, label);
5004 __ Slt(TMP, lhs_high, TMP);
5005 __ LoadConst32(AT, imm_low);
5006 __ Sltu(AT, AT, lhs_low);
5007 __ Blt(TMP, AT, label);
5008 break;
5009 case kCondB:
5010 __ LoadConst32(TMP, imm_high);
5011 __ Bltu(lhs_high, TMP, label);
5012 __ Sltu(TMP, TMP, lhs_high);
5013 __ LoadConst32(AT, imm_low);
5014 __ Sltu(AT, lhs_low, AT);
5015 __ Blt(TMP, AT, label);
5016 break;
5017 case kCondAE:
5018 __ LoadConst32(TMP, imm_high);
5019 __ Bltu(TMP, lhs_high, label);
5020 __ Sltu(TMP, lhs_high, TMP);
5021 __ LoadConst32(AT, imm_low);
5022 __ Sltu(AT, lhs_low, AT);
5023 __ Or(TMP, TMP, AT);
5024 __ Beqz(TMP, label);
5025 break;
5026 case kCondBE:
5027 __ LoadConst32(TMP, imm_high);
5028 __ Bltu(lhs_high, TMP, label);
5029 __ Sltu(TMP, TMP, lhs_high);
5030 __ LoadConst32(AT, imm_low);
5031 __ Sltu(AT, AT, lhs_low);
5032 __ Or(TMP, TMP, AT);
5033 __ Beqz(TMP, label);
5034 break;
5035 case kCondA:
5036 __ LoadConst32(TMP, imm_high);
5037 __ Bltu(TMP, lhs_high, label);
5038 __ Sltu(TMP, lhs_high, TMP);
5039 __ LoadConst32(AT, imm_low);
5040 __ Sltu(AT, AT, lhs_low);
5041 __ Blt(TMP, AT, label);
5042 break;
5043 }
5044 } else {
5045 switch (cond) {
5046 case kCondEQ:
5047 __ Xor(TMP, lhs_high, rhs_high);
5048 __ Xor(AT, lhs_low, rhs_low);
5049 __ Or(TMP, TMP, AT);
5050 __ Beqz(TMP, label);
5051 break;
5052 case kCondNE:
5053 __ Xor(TMP, lhs_high, rhs_high);
5054 __ Xor(AT, lhs_low, rhs_low);
5055 __ Or(TMP, TMP, AT);
5056 __ Bnez(TMP, label);
5057 break;
5058 case kCondLT:
5059 __ Blt(lhs_high, rhs_high, label);
5060 __ Slt(TMP, rhs_high, lhs_high);
5061 __ Sltu(AT, lhs_low, rhs_low);
5062 __ Blt(TMP, AT, label);
5063 break;
5064 case kCondGE:
5065 __ Blt(rhs_high, lhs_high, label);
5066 __ Slt(TMP, lhs_high, rhs_high);
5067 __ Sltu(AT, lhs_low, rhs_low);
5068 __ Or(TMP, TMP, AT);
5069 __ Beqz(TMP, label);
5070 break;
5071 case kCondLE:
5072 __ Blt(lhs_high, rhs_high, label);
5073 __ Slt(TMP, rhs_high, lhs_high);
5074 __ Sltu(AT, rhs_low, lhs_low);
5075 __ Or(TMP, TMP, AT);
5076 __ Beqz(TMP, label);
5077 break;
5078 case kCondGT:
5079 __ Blt(rhs_high, lhs_high, label);
5080 __ Slt(TMP, lhs_high, rhs_high);
5081 __ Sltu(AT, rhs_low, lhs_low);
5082 __ Blt(TMP, AT, label);
5083 break;
5084 case kCondB:
5085 __ Bltu(lhs_high, rhs_high, label);
5086 __ Sltu(TMP, rhs_high, lhs_high);
5087 __ Sltu(AT, lhs_low, rhs_low);
5088 __ Blt(TMP, AT, label);
5089 break;
5090 case kCondAE:
5091 __ Bltu(rhs_high, lhs_high, label);
5092 __ Sltu(TMP, lhs_high, rhs_high);
5093 __ Sltu(AT, lhs_low, rhs_low);
5094 __ Or(TMP, TMP, AT);
5095 __ Beqz(TMP, label);
5096 break;
5097 case kCondBE:
5098 __ Bltu(lhs_high, rhs_high, label);
5099 __ Sltu(TMP, rhs_high, lhs_high);
5100 __ Sltu(AT, rhs_low, lhs_low);
5101 __ Or(TMP, TMP, AT);
5102 __ Beqz(TMP, label);
5103 break;
5104 case kCondA:
5105 __ Bltu(rhs_high, lhs_high, label);
5106 __ Sltu(TMP, lhs_high, rhs_high);
5107 __ Sltu(AT, rhs_low, lhs_low);
5108 __ Blt(TMP, AT, label);
5109 break;
5110 }
5111 }
5112}
5113
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005114void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
5115 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005116 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005117 LocationSummary* locations) {
5118 Register dst = locations->Out().AsRegister<Register>();
5119 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5120 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5121 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005122 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005123 if (isR6) {
5124 switch (cond) {
5125 case kCondEQ:
5126 __ CmpEqS(FTMP, lhs, rhs);
5127 __ Mfc1(dst, FTMP);
5128 __ Andi(dst, dst, 1);
5129 break;
5130 case kCondNE:
5131 __ CmpEqS(FTMP, lhs, rhs);
5132 __ Mfc1(dst, FTMP);
5133 __ Addiu(dst, dst, 1);
5134 break;
5135 case kCondLT:
5136 if (gt_bias) {
5137 __ CmpLtS(FTMP, lhs, rhs);
5138 } else {
5139 __ CmpUltS(FTMP, lhs, rhs);
5140 }
5141 __ Mfc1(dst, FTMP);
5142 __ Andi(dst, dst, 1);
5143 break;
5144 case kCondLE:
5145 if (gt_bias) {
5146 __ CmpLeS(FTMP, lhs, rhs);
5147 } else {
5148 __ CmpUleS(FTMP, lhs, rhs);
5149 }
5150 __ Mfc1(dst, FTMP);
5151 __ Andi(dst, dst, 1);
5152 break;
5153 case kCondGT:
5154 if (gt_bias) {
5155 __ CmpUltS(FTMP, rhs, lhs);
5156 } else {
5157 __ CmpLtS(FTMP, rhs, lhs);
5158 }
5159 __ Mfc1(dst, FTMP);
5160 __ Andi(dst, dst, 1);
5161 break;
5162 case kCondGE:
5163 if (gt_bias) {
5164 __ CmpUleS(FTMP, rhs, lhs);
5165 } else {
5166 __ CmpLeS(FTMP, rhs, lhs);
5167 }
5168 __ Mfc1(dst, FTMP);
5169 __ Andi(dst, dst, 1);
5170 break;
5171 default:
5172 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5173 UNREACHABLE();
5174 }
5175 } else {
5176 switch (cond) {
5177 case kCondEQ:
5178 __ CeqS(0, lhs, rhs);
5179 __ LoadConst32(dst, 1);
5180 __ Movf(dst, ZERO, 0);
5181 break;
5182 case kCondNE:
5183 __ CeqS(0, lhs, rhs);
5184 __ LoadConst32(dst, 1);
5185 __ Movt(dst, ZERO, 0);
5186 break;
5187 case kCondLT:
5188 if (gt_bias) {
5189 __ ColtS(0, lhs, rhs);
5190 } else {
5191 __ CultS(0, lhs, rhs);
5192 }
5193 __ LoadConst32(dst, 1);
5194 __ Movf(dst, ZERO, 0);
5195 break;
5196 case kCondLE:
5197 if (gt_bias) {
5198 __ ColeS(0, lhs, rhs);
5199 } else {
5200 __ CuleS(0, lhs, rhs);
5201 }
5202 __ LoadConst32(dst, 1);
5203 __ Movf(dst, ZERO, 0);
5204 break;
5205 case kCondGT:
5206 if (gt_bias) {
5207 __ CultS(0, rhs, lhs);
5208 } else {
5209 __ ColtS(0, rhs, lhs);
5210 }
5211 __ LoadConst32(dst, 1);
5212 __ Movf(dst, ZERO, 0);
5213 break;
5214 case kCondGE:
5215 if (gt_bias) {
5216 __ CuleS(0, rhs, lhs);
5217 } else {
5218 __ ColeS(0, rhs, lhs);
5219 }
5220 __ LoadConst32(dst, 1);
5221 __ Movf(dst, ZERO, 0);
5222 break;
5223 default:
5224 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5225 UNREACHABLE();
5226 }
5227 }
5228 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005229 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005230 if (isR6) {
5231 switch (cond) {
5232 case kCondEQ:
5233 __ CmpEqD(FTMP, lhs, rhs);
5234 __ Mfc1(dst, FTMP);
5235 __ Andi(dst, dst, 1);
5236 break;
5237 case kCondNE:
5238 __ CmpEqD(FTMP, lhs, rhs);
5239 __ Mfc1(dst, FTMP);
5240 __ Addiu(dst, dst, 1);
5241 break;
5242 case kCondLT:
5243 if (gt_bias) {
5244 __ CmpLtD(FTMP, lhs, rhs);
5245 } else {
5246 __ CmpUltD(FTMP, lhs, rhs);
5247 }
5248 __ Mfc1(dst, FTMP);
5249 __ Andi(dst, dst, 1);
5250 break;
5251 case kCondLE:
5252 if (gt_bias) {
5253 __ CmpLeD(FTMP, lhs, rhs);
5254 } else {
5255 __ CmpUleD(FTMP, lhs, rhs);
5256 }
5257 __ Mfc1(dst, FTMP);
5258 __ Andi(dst, dst, 1);
5259 break;
5260 case kCondGT:
5261 if (gt_bias) {
5262 __ CmpUltD(FTMP, rhs, lhs);
5263 } else {
5264 __ CmpLtD(FTMP, rhs, lhs);
5265 }
5266 __ Mfc1(dst, FTMP);
5267 __ Andi(dst, dst, 1);
5268 break;
5269 case kCondGE:
5270 if (gt_bias) {
5271 __ CmpUleD(FTMP, rhs, lhs);
5272 } else {
5273 __ CmpLeD(FTMP, rhs, lhs);
5274 }
5275 __ Mfc1(dst, FTMP);
5276 __ Andi(dst, dst, 1);
5277 break;
5278 default:
5279 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5280 UNREACHABLE();
5281 }
5282 } else {
5283 switch (cond) {
5284 case kCondEQ:
5285 __ CeqD(0, lhs, rhs);
5286 __ LoadConst32(dst, 1);
5287 __ Movf(dst, ZERO, 0);
5288 break;
5289 case kCondNE:
5290 __ CeqD(0, lhs, rhs);
5291 __ LoadConst32(dst, 1);
5292 __ Movt(dst, ZERO, 0);
5293 break;
5294 case kCondLT:
5295 if (gt_bias) {
5296 __ ColtD(0, lhs, rhs);
5297 } else {
5298 __ CultD(0, lhs, rhs);
5299 }
5300 __ LoadConst32(dst, 1);
5301 __ Movf(dst, ZERO, 0);
5302 break;
5303 case kCondLE:
5304 if (gt_bias) {
5305 __ ColeD(0, lhs, rhs);
5306 } else {
5307 __ CuleD(0, lhs, rhs);
5308 }
5309 __ LoadConst32(dst, 1);
5310 __ Movf(dst, ZERO, 0);
5311 break;
5312 case kCondGT:
5313 if (gt_bias) {
5314 __ CultD(0, rhs, lhs);
5315 } else {
5316 __ ColtD(0, rhs, lhs);
5317 }
5318 __ LoadConst32(dst, 1);
5319 __ Movf(dst, ZERO, 0);
5320 break;
5321 case kCondGE:
5322 if (gt_bias) {
5323 __ CuleD(0, rhs, lhs);
5324 } else {
5325 __ ColeD(0, rhs, lhs);
5326 }
5327 __ LoadConst32(dst, 1);
5328 __ Movf(dst, ZERO, 0);
5329 break;
5330 default:
5331 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5332 UNREACHABLE();
5333 }
5334 }
5335 }
5336}
5337
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005338bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5339 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005340 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005341 LocationSummary* input_locations,
5342 int cc) {
5343 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5344 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5345 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005346 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005347 switch (cond) {
5348 case kCondEQ:
5349 __ CeqS(cc, lhs, rhs);
5350 return false;
5351 case kCondNE:
5352 __ CeqS(cc, lhs, rhs);
5353 return true;
5354 case kCondLT:
5355 if (gt_bias) {
5356 __ ColtS(cc, lhs, rhs);
5357 } else {
5358 __ CultS(cc, lhs, rhs);
5359 }
5360 return false;
5361 case kCondLE:
5362 if (gt_bias) {
5363 __ ColeS(cc, lhs, rhs);
5364 } else {
5365 __ CuleS(cc, lhs, rhs);
5366 }
5367 return false;
5368 case kCondGT:
5369 if (gt_bias) {
5370 __ CultS(cc, rhs, lhs);
5371 } else {
5372 __ ColtS(cc, rhs, lhs);
5373 }
5374 return false;
5375 case kCondGE:
5376 if (gt_bias) {
5377 __ CuleS(cc, rhs, lhs);
5378 } else {
5379 __ ColeS(cc, rhs, lhs);
5380 }
5381 return false;
5382 default:
5383 LOG(FATAL) << "Unexpected non-floating-point condition";
5384 UNREACHABLE();
5385 }
5386 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005387 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005388 switch (cond) {
5389 case kCondEQ:
5390 __ CeqD(cc, lhs, rhs);
5391 return false;
5392 case kCondNE:
5393 __ CeqD(cc, lhs, rhs);
5394 return true;
5395 case kCondLT:
5396 if (gt_bias) {
5397 __ ColtD(cc, lhs, rhs);
5398 } else {
5399 __ CultD(cc, lhs, rhs);
5400 }
5401 return false;
5402 case kCondLE:
5403 if (gt_bias) {
5404 __ ColeD(cc, lhs, rhs);
5405 } else {
5406 __ CuleD(cc, lhs, rhs);
5407 }
5408 return false;
5409 case kCondGT:
5410 if (gt_bias) {
5411 __ CultD(cc, rhs, lhs);
5412 } else {
5413 __ ColtD(cc, rhs, lhs);
5414 }
5415 return false;
5416 case kCondGE:
5417 if (gt_bias) {
5418 __ CuleD(cc, rhs, lhs);
5419 } else {
5420 __ ColeD(cc, rhs, lhs);
5421 }
5422 return false;
5423 default:
5424 LOG(FATAL) << "Unexpected non-floating-point condition";
5425 UNREACHABLE();
5426 }
5427 }
5428}
5429
5430bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5431 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005432 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005433 LocationSummary* input_locations,
5434 FRegister dst) {
5435 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5436 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5437 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005438 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005439 switch (cond) {
5440 case kCondEQ:
5441 __ CmpEqS(dst, lhs, rhs);
5442 return false;
5443 case kCondNE:
5444 __ CmpEqS(dst, lhs, rhs);
5445 return true;
5446 case kCondLT:
5447 if (gt_bias) {
5448 __ CmpLtS(dst, lhs, rhs);
5449 } else {
5450 __ CmpUltS(dst, lhs, rhs);
5451 }
5452 return false;
5453 case kCondLE:
5454 if (gt_bias) {
5455 __ CmpLeS(dst, lhs, rhs);
5456 } else {
5457 __ CmpUleS(dst, lhs, rhs);
5458 }
5459 return false;
5460 case kCondGT:
5461 if (gt_bias) {
5462 __ CmpUltS(dst, rhs, lhs);
5463 } else {
5464 __ CmpLtS(dst, rhs, lhs);
5465 }
5466 return false;
5467 case kCondGE:
5468 if (gt_bias) {
5469 __ CmpUleS(dst, rhs, lhs);
5470 } else {
5471 __ CmpLeS(dst, rhs, lhs);
5472 }
5473 return false;
5474 default:
5475 LOG(FATAL) << "Unexpected non-floating-point condition";
5476 UNREACHABLE();
5477 }
5478 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005479 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005480 switch (cond) {
5481 case kCondEQ:
5482 __ CmpEqD(dst, lhs, rhs);
5483 return false;
5484 case kCondNE:
5485 __ CmpEqD(dst, lhs, rhs);
5486 return true;
5487 case kCondLT:
5488 if (gt_bias) {
5489 __ CmpLtD(dst, lhs, rhs);
5490 } else {
5491 __ CmpUltD(dst, lhs, rhs);
5492 }
5493 return false;
5494 case kCondLE:
5495 if (gt_bias) {
5496 __ CmpLeD(dst, lhs, rhs);
5497 } else {
5498 __ CmpUleD(dst, lhs, rhs);
5499 }
5500 return false;
5501 case kCondGT:
5502 if (gt_bias) {
5503 __ CmpUltD(dst, rhs, lhs);
5504 } else {
5505 __ CmpLtD(dst, rhs, lhs);
5506 }
5507 return false;
5508 case kCondGE:
5509 if (gt_bias) {
5510 __ CmpUleD(dst, rhs, lhs);
5511 } else {
5512 __ CmpLeD(dst, rhs, lhs);
5513 }
5514 return false;
5515 default:
5516 LOG(FATAL) << "Unexpected non-floating-point condition";
5517 UNREACHABLE();
5518 }
5519 }
5520}
5521
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005522void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5523 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005524 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005525 LocationSummary* locations,
5526 MipsLabel* label) {
5527 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5528 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5529 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005530 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005531 if (isR6) {
5532 switch (cond) {
5533 case kCondEQ:
5534 __ CmpEqS(FTMP, lhs, rhs);
5535 __ Bc1nez(FTMP, label);
5536 break;
5537 case kCondNE:
5538 __ CmpEqS(FTMP, lhs, rhs);
5539 __ Bc1eqz(FTMP, label);
5540 break;
5541 case kCondLT:
5542 if (gt_bias) {
5543 __ CmpLtS(FTMP, lhs, rhs);
5544 } else {
5545 __ CmpUltS(FTMP, lhs, rhs);
5546 }
5547 __ Bc1nez(FTMP, label);
5548 break;
5549 case kCondLE:
5550 if (gt_bias) {
5551 __ CmpLeS(FTMP, lhs, rhs);
5552 } else {
5553 __ CmpUleS(FTMP, lhs, rhs);
5554 }
5555 __ Bc1nez(FTMP, label);
5556 break;
5557 case kCondGT:
5558 if (gt_bias) {
5559 __ CmpUltS(FTMP, rhs, lhs);
5560 } else {
5561 __ CmpLtS(FTMP, rhs, lhs);
5562 }
5563 __ Bc1nez(FTMP, label);
5564 break;
5565 case kCondGE:
5566 if (gt_bias) {
5567 __ CmpUleS(FTMP, rhs, lhs);
5568 } else {
5569 __ CmpLeS(FTMP, rhs, lhs);
5570 }
5571 __ Bc1nez(FTMP, label);
5572 break;
5573 default:
5574 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005575 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005576 }
5577 } else {
5578 switch (cond) {
5579 case kCondEQ:
5580 __ CeqS(0, lhs, rhs);
5581 __ Bc1t(0, label);
5582 break;
5583 case kCondNE:
5584 __ CeqS(0, lhs, rhs);
5585 __ Bc1f(0, label);
5586 break;
5587 case kCondLT:
5588 if (gt_bias) {
5589 __ ColtS(0, lhs, rhs);
5590 } else {
5591 __ CultS(0, lhs, rhs);
5592 }
5593 __ Bc1t(0, label);
5594 break;
5595 case kCondLE:
5596 if (gt_bias) {
5597 __ ColeS(0, lhs, rhs);
5598 } else {
5599 __ CuleS(0, lhs, rhs);
5600 }
5601 __ Bc1t(0, label);
5602 break;
5603 case kCondGT:
5604 if (gt_bias) {
5605 __ CultS(0, rhs, lhs);
5606 } else {
5607 __ ColtS(0, rhs, lhs);
5608 }
5609 __ Bc1t(0, label);
5610 break;
5611 case kCondGE:
5612 if (gt_bias) {
5613 __ CuleS(0, rhs, lhs);
5614 } else {
5615 __ ColeS(0, rhs, lhs);
5616 }
5617 __ Bc1t(0, label);
5618 break;
5619 default:
5620 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005621 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005622 }
5623 }
5624 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005625 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005626 if (isR6) {
5627 switch (cond) {
5628 case kCondEQ:
5629 __ CmpEqD(FTMP, lhs, rhs);
5630 __ Bc1nez(FTMP, label);
5631 break;
5632 case kCondNE:
5633 __ CmpEqD(FTMP, lhs, rhs);
5634 __ Bc1eqz(FTMP, label);
5635 break;
5636 case kCondLT:
5637 if (gt_bias) {
5638 __ CmpLtD(FTMP, lhs, rhs);
5639 } else {
5640 __ CmpUltD(FTMP, lhs, rhs);
5641 }
5642 __ Bc1nez(FTMP, label);
5643 break;
5644 case kCondLE:
5645 if (gt_bias) {
5646 __ CmpLeD(FTMP, lhs, rhs);
5647 } else {
5648 __ CmpUleD(FTMP, lhs, rhs);
5649 }
5650 __ Bc1nez(FTMP, label);
5651 break;
5652 case kCondGT:
5653 if (gt_bias) {
5654 __ CmpUltD(FTMP, rhs, lhs);
5655 } else {
5656 __ CmpLtD(FTMP, rhs, lhs);
5657 }
5658 __ Bc1nez(FTMP, label);
5659 break;
5660 case kCondGE:
5661 if (gt_bias) {
5662 __ CmpUleD(FTMP, rhs, lhs);
5663 } else {
5664 __ CmpLeD(FTMP, rhs, lhs);
5665 }
5666 __ Bc1nez(FTMP, label);
5667 break;
5668 default:
5669 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005670 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005671 }
5672 } else {
5673 switch (cond) {
5674 case kCondEQ:
5675 __ CeqD(0, lhs, rhs);
5676 __ Bc1t(0, label);
5677 break;
5678 case kCondNE:
5679 __ CeqD(0, lhs, rhs);
5680 __ Bc1f(0, label);
5681 break;
5682 case kCondLT:
5683 if (gt_bias) {
5684 __ ColtD(0, lhs, rhs);
5685 } else {
5686 __ CultD(0, lhs, rhs);
5687 }
5688 __ Bc1t(0, label);
5689 break;
5690 case kCondLE:
5691 if (gt_bias) {
5692 __ ColeD(0, lhs, rhs);
5693 } else {
5694 __ CuleD(0, lhs, rhs);
5695 }
5696 __ Bc1t(0, label);
5697 break;
5698 case kCondGT:
5699 if (gt_bias) {
5700 __ CultD(0, rhs, lhs);
5701 } else {
5702 __ ColtD(0, rhs, lhs);
5703 }
5704 __ Bc1t(0, label);
5705 break;
5706 case kCondGE:
5707 if (gt_bias) {
5708 __ CuleD(0, rhs, lhs);
5709 } else {
5710 __ ColeD(0, rhs, lhs);
5711 }
5712 __ Bc1t(0, label);
5713 break;
5714 default:
5715 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005716 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005717 }
5718 }
5719 }
5720}
5721
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005722void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005723 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005724 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005725 MipsLabel* false_target) {
5726 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005727
David Brazdil0debae72015-11-12 18:37:00 +00005728 if (true_target == nullptr && false_target == nullptr) {
5729 // Nothing to do. The code always falls through.
5730 return;
5731 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005732 // Constant condition, statically compared against "true" (integer value 1).
5733 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005734 if (true_target != nullptr) {
5735 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005736 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005737 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005738 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005739 if (false_target != nullptr) {
5740 __ B(false_target);
5741 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005742 }
David Brazdil0debae72015-11-12 18:37:00 +00005743 return;
5744 }
5745
5746 // The following code generates these patterns:
5747 // (1) true_target == nullptr && false_target != nullptr
5748 // - opposite condition true => branch to false_target
5749 // (2) true_target != nullptr && false_target == nullptr
5750 // - condition true => branch to true_target
5751 // (3) true_target != nullptr && false_target != nullptr
5752 // - condition true => branch to true_target
5753 // - branch to false_target
5754 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005755 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005756 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005757 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005758 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005759 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5760 } else {
5761 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5762 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005763 } else {
5764 // The condition instruction has not been materialized, use its inputs as
5765 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005766 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005767 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005768 LocationSummary* locations = cond->GetLocations();
5769 IfCondition if_cond = condition->GetCondition();
5770 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005771
David Brazdil0debae72015-11-12 18:37:00 +00005772 if (true_target == nullptr) {
5773 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005774 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005775 }
5776
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005777 switch (type) {
5778 default:
5779 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5780 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005781 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005782 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5783 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005784 case DataType::Type::kFloat32:
5785 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005786 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5787 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005788 }
5789 }
David Brazdil0debae72015-11-12 18:37:00 +00005790
5791 // If neither branch falls through (case 3), the conditional branch to `true_target`
5792 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5793 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005794 __ B(false_target);
5795 }
5796}
5797
5798void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005799 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005800 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005801 locations->SetInAt(0, Location::RequiresRegister());
5802 }
5803}
5804
5805void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005806 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5807 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5808 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5809 nullptr : codegen_->GetLabelOf(true_successor);
5810 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5811 nullptr : codegen_->GetLabelOf(false_successor);
5812 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005813}
5814
5815void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005816 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005817 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005818 InvokeRuntimeCallingConvention calling_convention;
5819 RegisterSet caller_saves = RegisterSet::Empty();
5820 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5821 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005822 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005823 locations->SetInAt(0, Location::RequiresRegister());
5824 }
5825}
5826
5827void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005828 SlowPathCodeMIPS* slow_path =
5829 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005830 GenerateTestAndBranch(deoptimize,
5831 /* condition_input_index */ 0,
5832 slow_path->GetEntryLabel(),
5833 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005834}
5835
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005836// This function returns true if a conditional move can be generated for HSelect.
5837// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5838// branches and regular moves.
5839//
5840// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5841//
5842// While determining feasibility of a conditional move and setting inputs/outputs
5843// are two distinct tasks, this function does both because they share quite a bit
5844// of common logic.
5845static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5846 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5847 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5848 HCondition* condition = cond->AsCondition();
5849
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005850 DataType::Type cond_type =
5851 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5852 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005853
5854 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5855 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5856 bool is_true_value_zero_constant =
5857 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5858 bool is_false_value_zero_constant =
5859 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5860
5861 bool can_move_conditionally = false;
5862 bool use_const_for_false_in = false;
5863 bool use_const_for_true_in = false;
5864
5865 if (!cond->IsConstant()) {
5866 switch (cond_type) {
5867 default:
5868 switch (dst_type) {
5869 default:
5870 // Moving int on int condition.
5871 if (is_r6) {
5872 if (is_true_value_zero_constant) {
5873 // seleqz out_reg, false_reg, cond_reg
5874 can_move_conditionally = true;
5875 use_const_for_true_in = true;
5876 } else if (is_false_value_zero_constant) {
5877 // selnez out_reg, true_reg, cond_reg
5878 can_move_conditionally = true;
5879 use_const_for_false_in = true;
5880 } else if (materialized) {
5881 // Not materializing unmaterialized int conditions
5882 // to keep the instruction count low.
5883 // selnez AT, true_reg, cond_reg
5884 // seleqz TMP, false_reg, cond_reg
5885 // or out_reg, AT, TMP
5886 can_move_conditionally = true;
5887 }
5888 } else {
5889 // movn out_reg, true_reg/ZERO, cond_reg
5890 can_move_conditionally = true;
5891 use_const_for_true_in = is_true_value_zero_constant;
5892 }
5893 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005894 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005895 // Moving long on int condition.
5896 if (is_r6) {
5897 if (is_true_value_zero_constant) {
5898 // seleqz out_reg_lo, false_reg_lo, cond_reg
5899 // seleqz out_reg_hi, false_reg_hi, cond_reg
5900 can_move_conditionally = true;
5901 use_const_for_true_in = true;
5902 } else if (is_false_value_zero_constant) {
5903 // selnez out_reg_lo, true_reg_lo, cond_reg
5904 // selnez out_reg_hi, true_reg_hi, cond_reg
5905 can_move_conditionally = true;
5906 use_const_for_false_in = true;
5907 }
5908 // Other long conditional moves would generate 6+ instructions,
5909 // which is too many.
5910 } else {
5911 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5912 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5913 can_move_conditionally = true;
5914 use_const_for_true_in = is_true_value_zero_constant;
5915 }
5916 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005917 case DataType::Type::kFloat32:
5918 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005919 // Moving float/double on int condition.
5920 if (is_r6) {
5921 if (materialized) {
5922 // Not materializing unmaterialized int conditions
5923 // to keep the instruction count low.
5924 can_move_conditionally = true;
5925 if (is_true_value_zero_constant) {
5926 // sltu TMP, ZERO, cond_reg
5927 // mtc1 TMP, temp_cond_reg
5928 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5929 use_const_for_true_in = true;
5930 } else if (is_false_value_zero_constant) {
5931 // sltu TMP, ZERO, cond_reg
5932 // mtc1 TMP, temp_cond_reg
5933 // selnez.fmt out_reg, true_reg, temp_cond_reg
5934 use_const_for_false_in = true;
5935 } else {
5936 // sltu TMP, ZERO, cond_reg
5937 // mtc1 TMP, temp_cond_reg
5938 // sel.fmt temp_cond_reg, false_reg, true_reg
5939 // mov.fmt out_reg, temp_cond_reg
5940 }
5941 }
5942 } else {
5943 // movn.fmt out_reg, true_reg, cond_reg
5944 can_move_conditionally = true;
5945 }
5946 break;
5947 }
5948 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005949 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005950 // We don't materialize long comparison now
5951 // and use conditional branches instead.
5952 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005953 case DataType::Type::kFloat32:
5954 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005955 switch (dst_type) {
5956 default:
5957 // Moving int on float/double condition.
5958 if (is_r6) {
5959 if (is_true_value_zero_constant) {
5960 // mfc1 TMP, temp_cond_reg
5961 // seleqz out_reg, false_reg, TMP
5962 can_move_conditionally = true;
5963 use_const_for_true_in = true;
5964 } else if (is_false_value_zero_constant) {
5965 // mfc1 TMP, temp_cond_reg
5966 // selnez out_reg, true_reg, TMP
5967 can_move_conditionally = true;
5968 use_const_for_false_in = true;
5969 } else {
5970 // mfc1 TMP, temp_cond_reg
5971 // selnez AT, true_reg, TMP
5972 // seleqz TMP, false_reg, TMP
5973 // or out_reg, AT, TMP
5974 can_move_conditionally = true;
5975 }
5976 } else {
5977 // movt out_reg, true_reg/ZERO, cc
5978 can_move_conditionally = true;
5979 use_const_for_true_in = is_true_value_zero_constant;
5980 }
5981 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005982 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005983 // Moving long on float/double condition.
5984 if (is_r6) {
5985 if (is_true_value_zero_constant) {
5986 // mfc1 TMP, temp_cond_reg
5987 // seleqz out_reg_lo, false_reg_lo, TMP
5988 // seleqz out_reg_hi, false_reg_hi, TMP
5989 can_move_conditionally = true;
5990 use_const_for_true_in = true;
5991 } else if (is_false_value_zero_constant) {
5992 // mfc1 TMP, temp_cond_reg
5993 // selnez out_reg_lo, true_reg_lo, TMP
5994 // selnez out_reg_hi, true_reg_hi, TMP
5995 can_move_conditionally = true;
5996 use_const_for_false_in = true;
5997 }
5998 // Other long conditional moves would generate 6+ instructions,
5999 // which is too many.
6000 } else {
6001 // movt out_reg_lo, true_reg_lo/ZERO, cc
6002 // movt out_reg_hi, true_reg_hi/ZERO, cc
6003 can_move_conditionally = true;
6004 use_const_for_true_in = is_true_value_zero_constant;
6005 }
6006 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006007 case DataType::Type::kFloat32:
6008 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006009 // Moving float/double on float/double condition.
6010 if (is_r6) {
6011 can_move_conditionally = true;
6012 if (is_true_value_zero_constant) {
6013 // seleqz.fmt out_reg, false_reg, temp_cond_reg
6014 use_const_for_true_in = true;
6015 } else if (is_false_value_zero_constant) {
6016 // selnez.fmt out_reg, true_reg, temp_cond_reg
6017 use_const_for_false_in = true;
6018 } else {
6019 // sel.fmt temp_cond_reg, false_reg, true_reg
6020 // mov.fmt out_reg, temp_cond_reg
6021 }
6022 } else {
6023 // movt.fmt out_reg, true_reg, cc
6024 can_move_conditionally = true;
6025 }
6026 break;
6027 }
6028 break;
6029 }
6030 }
6031
6032 if (can_move_conditionally) {
6033 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
6034 } else {
6035 DCHECK(!use_const_for_false_in);
6036 DCHECK(!use_const_for_true_in);
6037 }
6038
6039 if (locations_to_set != nullptr) {
6040 if (use_const_for_false_in) {
6041 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
6042 } else {
6043 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006044 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006045 ? Location::RequiresFpuRegister()
6046 : Location::RequiresRegister());
6047 }
6048 if (use_const_for_true_in) {
6049 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
6050 } else {
6051 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006052 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006053 ? Location::RequiresFpuRegister()
6054 : Location::RequiresRegister());
6055 }
6056 if (materialized) {
6057 locations_to_set->SetInAt(2, Location::RequiresRegister());
6058 }
6059 // On R6 we don't require the output to be the same as the
6060 // first input for conditional moves unlike on R2.
6061 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
6062 if (is_out_same_as_first_in) {
6063 locations_to_set->SetOut(Location::SameAsFirstInput());
6064 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006065 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006066 ? Location::RequiresFpuRegister()
6067 : Location::RequiresRegister());
6068 }
6069 }
6070
6071 return can_move_conditionally;
6072}
6073
6074void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
6075 LocationSummary* locations = select->GetLocations();
6076 Location dst = locations->Out();
6077 Location src = locations->InAt(1);
6078 Register src_reg = ZERO;
6079 Register src_reg_high = ZERO;
6080 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6081 Register cond_reg = TMP;
6082 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006083 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006084 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006085 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006086
6087 if (IsBooleanValueOrMaterializedCondition(cond)) {
6088 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6089 } else {
6090 HCondition* condition = cond->AsCondition();
6091 LocationSummary* cond_locations = cond->GetLocations();
6092 IfCondition if_cond = condition->GetCondition();
6093 cond_type = condition->InputAt(0)->GetType();
6094 switch (cond_type) {
6095 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006096 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006097 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6098 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006099 case DataType::Type::kFloat32:
6100 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006101 cond_inverted = MaterializeFpCompareR2(if_cond,
6102 condition->IsGtBias(),
6103 cond_type,
6104 cond_locations,
6105 cond_cc);
6106 break;
6107 }
6108 }
6109
6110 DCHECK(dst.Equals(locations->InAt(0)));
6111 if (src.IsRegister()) {
6112 src_reg = src.AsRegister<Register>();
6113 } else if (src.IsRegisterPair()) {
6114 src_reg = src.AsRegisterPairLow<Register>();
6115 src_reg_high = src.AsRegisterPairHigh<Register>();
6116 } else if (src.IsConstant()) {
6117 DCHECK(src.GetConstant()->IsZeroBitPattern());
6118 }
6119
6120 switch (cond_type) {
6121 default:
6122 switch (dst_type) {
6123 default:
6124 if (cond_inverted) {
6125 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
6126 } else {
6127 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
6128 }
6129 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006130 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006131 if (cond_inverted) {
6132 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6133 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6134 } else {
6135 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
6136 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
6137 }
6138 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006139 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006140 if (cond_inverted) {
6141 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6142 } else {
6143 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6144 }
6145 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006146 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006147 if (cond_inverted) {
6148 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6149 } else {
6150 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
6151 }
6152 break;
6153 }
6154 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006155 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006156 LOG(FATAL) << "Unreachable";
6157 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006158 case DataType::Type::kFloat32:
6159 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006160 switch (dst_type) {
6161 default:
6162 if (cond_inverted) {
6163 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
6164 } else {
6165 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
6166 }
6167 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006168 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006169 if (cond_inverted) {
6170 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6171 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6172 } else {
6173 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
6174 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
6175 }
6176 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006177 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006178 if (cond_inverted) {
6179 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6180 } else {
6181 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6182 }
6183 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006184 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006185 if (cond_inverted) {
6186 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6187 } else {
6188 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
6189 }
6190 break;
6191 }
6192 break;
6193 }
6194}
6195
6196void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
6197 LocationSummary* locations = select->GetLocations();
6198 Location dst = locations->Out();
6199 Location false_src = locations->InAt(0);
6200 Location true_src = locations->InAt(1);
6201 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6202 Register cond_reg = TMP;
6203 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006204 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006205 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006206 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006207
6208 if (IsBooleanValueOrMaterializedCondition(cond)) {
6209 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6210 } else {
6211 HCondition* condition = cond->AsCondition();
6212 LocationSummary* cond_locations = cond->GetLocations();
6213 IfCondition if_cond = condition->GetCondition();
6214 cond_type = condition->InputAt(0)->GetType();
6215 switch (cond_type) {
6216 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006217 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006218 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6219 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 case DataType::Type::kFloat32:
6221 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006222 cond_inverted = MaterializeFpCompareR6(if_cond,
6223 condition->IsGtBias(),
6224 cond_type,
6225 cond_locations,
6226 fcond_reg);
6227 break;
6228 }
6229 }
6230
6231 if (true_src.IsConstant()) {
6232 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6233 }
6234 if (false_src.IsConstant()) {
6235 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6236 }
6237
6238 switch (dst_type) {
6239 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006240 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006241 __ Mfc1(cond_reg, fcond_reg);
6242 }
6243 if (true_src.IsConstant()) {
6244 if (cond_inverted) {
6245 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6246 } else {
6247 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6248 }
6249 } else if (false_src.IsConstant()) {
6250 if (cond_inverted) {
6251 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6252 } else {
6253 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6254 }
6255 } else {
6256 DCHECK_NE(cond_reg, AT);
6257 if (cond_inverted) {
6258 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6259 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6260 } else {
6261 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6262 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6263 }
6264 __ Or(dst.AsRegister<Register>(), AT, TMP);
6265 }
6266 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006267 case DataType::Type::kInt64: {
6268 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006269 __ Mfc1(cond_reg, fcond_reg);
6270 }
6271 Register dst_lo = dst.AsRegisterPairLow<Register>();
6272 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6273 if (true_src.IsConstant()) {
6274 Register src_lo = false_src.AsRegisterPairLow<Register>();
6275 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6276 if (cond_inverted) {
6277 __ Selnez(dst_lo, src_lo, cond_reg);
6278 __ Selnez(dst_hi, src_hi, cond_reg);
6279 } else {
6280 __ Seleqz(dst_lo, src_lo, cond_reg);
6281 __ Seleqz(dst_hi, src_hi, cond_reg);
6282 }
6283 } else {
6284 DCHECK(false_src.IsConstant());
6285 Register src_lo = true_src.AsRegisterPairLow<Register>();
6286 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6287 if (cond_inverted) {
6288 __ Seleqz(dst_lo, src_lo, cond_reg);
6289 __ Seleqz(dst_hi, src_hi, cond_reg);
6290 } else {
6291 __ Selnez(dst_lo, src_lo, cond_reg);
6292 __ Selnez(dst_hi, src_hi, cond_reg);
6293 }
6294 }
6295 break;
6296 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006297 case DataType::Type::kFloat32: {
6298 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006299 // sel*.fmt tests bit 0 of the condition register, account for that.
6300 __ Sltu(TMP, ZERO, cond_reg);
6301 __ Mtc1(TMP, fcond_reg);
6302 }
6303 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6304 if (true_src.IsConstant()) {
6305 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6306 if (cond_inverted) {
6307 __ SelnezS(dst_reg, src_reg, fcond_reg);
6308 } else {
6309 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6310 }
6311 } else if (false_src.IsConstant()) {
6312 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6313 if (cond_inverted) {
6314 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6315 } else {
6316 __ SelnezS(dst_reg, src_reg, fcond_reg);
6317 }
6318 } else {
6319 if (cond_inverted) {
6320 __ SelS(fcond_reg,
6321 true_src.AsFpuRegister<FRegister>(),
6322 false_src.AsFpuRegister<FRegister>());
6323 } else {
6324 __ SelS(fcond_reg,
6325 false_src.AsFpuRegister<FRegister>(),
6326 true_src.AsFpuRegister<FRegister>());
6327 }
6328 __ MovS(dst_reg, fcond_reg);
6329 }
6330 break;
6331 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006332 case DataType::Type::kFloat64: {
6333 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006334 // sel*.fmt tests bit 0 of the condition register, account for that.
6335 __ Sltu(TMP, ZERO, cond_reg);
6336 __ Mtc1(TMP, fcond_reg);
6337 }
6338 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6339 if (true_src.IsConstant()) {
6340 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6341 if (cond_inverted) {
6342 __ SelnezD(dst_reg, src_reg, fcond_reg);
6343 } else {
6344 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6345 }
6346 } else if (false_src.IsConstant()) {
6347 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6348 if (cond_inverted) {
6349 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6350 } else {
6351 __ SelnezD(dst_reg, src_reg, fcond_reg);
6352 }
6353 } else {
6354 if (cond_inverted) {
6355 __ SelD(fcond_reg,
6356 true_src.AsFpuRegister<FRegister>(),
6357 false_src.AsFpuRegister<FRegister>());
6358 } else {
6359 __ SelD(fcond_reg,
6360 false_src.AsFpuRegister<FRegister>(),
6361 true_src.AsFpuRegister<FRegister>());
6362 }
6363 __ MovD(dst_reg, fcond_reg);
6364 }
6365 break;
6366 }
6367 }
6368}
6369
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006370void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006371 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006372 LocationSummary(flag, LocationSummary::kNoCall);
6373 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006374}
6375
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006376void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6377 __ LoadFromOffset(kLoadWord,
6378 flag->GetLocations()->Out().AsRegister<Register>(),
6379 SP,
6380 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006381}
6382
David Brazdil74eb1b22015-12-14 11:44:01 +00006383void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006384 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006385 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006386}
6387
6388void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006389 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6390 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6391 if (is_r6) {
6392 GenConditionalMoveR6(select);
6393 } else {
6394 GenConditionalMoveR2(select);
6395 }
6396 } else {
6397 LocationSummary* locations = select->GetLocations();
6398 MipsLabel false_target;
6399 GenerateTestAndBranch(select,
6400 /* condition_input_index */ 2,
6401 /* true_target */ nullptr,
6402 &false_target);
6403 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6404 __ Bind(&false_target);
6405 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006406}
6407
David Srbecky0cf44932015-12-09 14:09:59 +00006408void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006409 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006410}
6411
David Srbeckyd28f4a02016-03-14 17:14:24 +00006412void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6413 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006414}
6415
6416void CodeGeneratorMIPS::GenerateNop() {
6417 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006418}
6419
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006420void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006421 DataType::Type field_type = field_info.GetFieldType();
6422 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006423 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006424 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006425 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006426 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006427 instruction,
6428 generate_volatile
6429 ? LocationSummary::kCallOnMainOnly
6430 : (object_field_get_with_read_barrier
6431 ? LocationSummary::kCallOnSlowPath
6432 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006433
Alexey Frunzec61c0762017-04-10 13:54:23 -07006434 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6435 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6436 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006437 locations->SetInAt(0, Location::RequiresRegister());
6438 if (generate_volatile) {
6439 InvokeRuntimeCallingConvention calling_convention;
6440 // need A0 to hold base + offset
6441 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006442 if (field_type == DataType::Type::kInt64) {
6443 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006444 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006445 // Use Location::Any() to prevent situations when running out of available fp registers.
6446 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006447 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006448 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006449 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6450 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6451 }
6452 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006453 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006454 locations->SetOut(Location::RequiresFpuRegister());
6455 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006456 // The output overlaps in the case of an object field get with
6457 // read barriers enabled: we do not want the move to overwrite the
6458 // object's location, as we need it to emit the read barrier.
6459 locations->SetOut(Location::RequiresRegister(),
6460 object_field_get_with_read_barrier
6461 ? Location::kOutputOverlap
6462 : Location::kNoOutputOverlap);
6463 }
6464 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6465 // We need a temporary register for the read barrier marking slow
6466 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006467 if (!kBakerReadBarrierThunksEnableForFields) {
6468 locations->AddTemp(Location::RequiresRegister());
6469 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006470 }
6471 }
6472}
6473
6474void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6475 const FieldInfo& field_info,
6476 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006477 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6478 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006479 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006480 Location obj_loc = locations->InAt(0);
6481 Register obj = obj_loc.AsRegister<Register>();
6482 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006483 LoadOperandType load_type = kLoadUnsignedByte;
6484 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006485 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006486 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006487
6488 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006489 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006490 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006491 load_type = kLoadUnsignedByte;
6492 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006493 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006494 load_type = kLoadSignedByte;
6495 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006496 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006497 load_type = kLoadUnsignedHalfword;
6498 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006499 case DataType::Type::kInt16:
6500 load_type = kLoadSignedHalfword;
6501 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006502 case DataType::Type::kInt32:
6503 case DataType::Type::kFloat32:
6504 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006505 load_type = kLoadWord;
6506 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006507 case DataType::Type::kInt64:
6508 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006509 load_type = kLoadDoubleword;
6510 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006511 case DataType::Type::kUint32:
6512 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006513 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006514 LOG(FATAL) << "Unreachable type " << type;
6515 UNREACHABLE();
6516 }
6517
6518 if (is_volatile && load_type == kLoadDoubleword) {
6519 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006520 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006521 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006522 __ LoadFromOffset(kLoadWord,
6523 ZERO,
6524 locations->GetTemp(0).AsRegister<Register>(),
6525 0,
6526 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006527 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006528 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006529 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006530 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006531 if (dst_loc.IsFpuRegister()) {
6532 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006533 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006534 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006535 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006536 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006537 __ StoreToOffset(kStoreWord,
6538 locations->GetTemp(1).AsRegister<Register>(),
6539 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006540 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006541 __ StoreToOffset(kStoreWord,
6542 locations->GetTemp(2).AsRegister<Register>(),
6543 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006544 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006545 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006546 }
6547 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006548 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006549 // /* HeapReference<Object> */ dst = *(obj + offset)
6550 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006551 Location temp_loc =
6552 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006553 // Note that a potential implicit null check is handled in this
6554 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6555 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6556 dst_loc,
6557 obj,
6558 offset,
6559 temp_loc,
6560 /* needs_null_check */ true);
6561 if (is_volatile) {
6562 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6563 }
6564 } else {
6565 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6566 if (is_volatile) {
6567 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6568 }
6569 // If read barriers are enabled, emit read barriers other than
6570 // Baker's using a slow path (and also unpoison the loaded
6571 // reference, if heap poisoning is enabled).
6572 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6573 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006574 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006575 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006576 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006577 DCHECK(dst_loc.IsRegisterPair());
6578 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006579 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006580 DCHECK(dst_loc.IsRegister());
6581 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006582 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006583 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006584 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006585 DCHECK(dst_loc.IsFpuRegister());
6586 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006587 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006588 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006589 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006590 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006591 }
6592 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006593 }
6594
Alexey Frunze15958152017-02-09 19:08:30 -08006595 // Memory barriers, in the case of references, are handled in the
6596 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006597 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006598 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6599 }
6600}
6601
6602void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006603 DataType::Type field_type = field_info.GetFieldType();
6604 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006605 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006606 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006607 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006608
6609 locations->SetInAt(0, Location::RequiresRegister());
6610 if (generate_volatile) {
6611 InvokeRuntimeCallingConvention calling_convention;
6612 // need A0 to hold base + offset
6613 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006614 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006615 locations->SetInAt(1, Location::RegisterPairLocation(
6616 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6617 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006618 // Use Location::Any() to prevent situations when running out of available fp registers.
6619 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006620 // Pass FP parameters in core registers.
6621 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6622 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6623 }
6624 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006625 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006626 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006627 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006628 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006629 }
6630 }
6631}
6632
6633void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6634 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006635 uint32_t dex_pc,
6636 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006637 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006638 LocationSummary* locations = instruction->GetLocations();
6639 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006640 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006641 StoreOperandType store_type = kStoreByte;
6642 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006643 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006644 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006645 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006646
6647 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006648 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006649 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006650 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006651 store_type = kStoreByte;
6652 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006653 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006654 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006655 store_type = kStoreHalfword;
6656 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006657 case DataType::Type::kInt32:
6658 case DataType::Type::kFloat32:
6659 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006660 store_type = kStoreWord;
6661 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006662 case DataType::Type::kInt64:
6663 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006664 store_type = kStoreDoubleword;
6665 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006666 case DataType::Type::kUint32:
6667 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006668 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006669 LOG(FATAL) << "Unreachable type " << type;
6670 UNREACHABLE();
6671 }
6672
6673 if (is_volatile) {
6674 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6675 }
6676
6677 if (is_volatile && store_type == kStoreDoubleword) {
6678 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006679 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006680 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006681 __ LoadFromOffset(kLoadWord,
6682 ZERO,
6683 locations->GetTemp(0).AsRegister<Register>(),
6684 0,
6685 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006686 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006687 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006688 if (value_location.IsFpuRegister()) {
6689 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6690 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006691 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006692 value_location.AsFpuRegister<FRegister>());
6693 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006694 __ LoadFromOffset(kLoadWord,
6695 locations->GetTemp(1).AsRegister<Register>(),
6696 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006697 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006698 __ LoadFromOffset(kLoadWord,
6699 locations->GetTemp(2).AsRegister<Register>(),
6700 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006701 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006702 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006703 DCHECK(value_location.IsConstant());
6704 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6705 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006706 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6707 locations->GetTemp(1).AsRegister<Register>(),
6708 value);
6709 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006710 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006711 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006712 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6713 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006714 if (value_location.IsConstant()) {
6715 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6716 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006717 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006718 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006719 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006720 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006721 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006722 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006723 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006724 if (kPoisonHeapReferences && needs_write_barrier) {
6725 // Note that in the case where `value` is a null reference,
6726 // we do not enter this block, as a null reference does not
6727 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006728 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006729 __ PoisonHeapReference(TMP, src);
6730 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6731 } else {
6732 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6733 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006734 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006735 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006736 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006737 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006738 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006739 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006740 }
6741 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006742 }
6743
Alexey Frunzec061de12017-02-14 13:27:23 -08006744 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006745 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006746 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006747 }
6748
6749 if (is_volatile) {
6750 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6751 }
6752}
6753
6754void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6755 HandleFieldGet(instruction, instruction->GetFieldInfo());
6756}
6757
6758void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6759 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6760}
6761
6762void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6763 HandleFieldSet(instruction, instruction->GetFieldInfo());
6764}
6765
6766void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006767 HandleFieldSet(instruction,
6768 instruction->GetFieldInfo(),
6769 instruction->GetDexPc(),
6770 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006771}
6772
Alexey Frunze15958152017-02-09 19:08:30 -08006773void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6774 HInstruction* instruction,
6775 Location out,
6776 uint32_t offset,
6777 Location maybe_temp,
6778 ReadBarrierOption read_barrier_option) {
6779 Register out_reg = out.AsRegister<Register>();
6780 if (read_barrier_option == kWithReadBarrier) {
6781 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006782 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6783 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6784 }
Alexey Frunze15958152017-02-09 19:08:30 -08006785 if (kUseBakerReadBarrier) {
6786 // Load with fast path based Baker's read barrier.
6787 // /* HeapReference<Object> */ out = *(out + offset)
6788 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6789 out,
6790 out_reg,
6791 offset,
6792 maybe_temp,
6793 /* needs_null_check */ false);
6794 } else {
6795 // Load with slow path based read barrier.
6796 // Save the value of `out` into `maybe_temp` before overwriting it
6797 // in the following move operation, as we will need it for the
6798 // read barrier below.
6799 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6800 // /* HeapReference<Object> */ out = *(out + offset)
6801 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6802 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6803 }
6804 } else {
6805 // Plain load with no read barrier.
6806 // /* HeapReference<Object> */ out = *(out + offset)
6807 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6808 __ MaybeUnpoisonHeapReference(out_reg);
6809 }
6810}
6811
6812void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6813 HInstruction* instruction,
6814 Location out,
6815 Location obj,
6816 uint32_t offset,
6817 Location maybe_temp,
6818 ReadBarrierOption read_barrier_option) {
6819 Register out_reg = out.AsRegister<Register>();
6820 Register obj_reg = obj.AsRegister<Register>();
6821 if (read_barrier_option == kWithReadBarrier) {
6822 CHECK(kEmitCompilerReadBarrier);
6823 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006824 if (!kBakerReadBarrierThunksEnableForFields) {
6825 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6826 }
Alexey Frunze15958152017-02-09 19:08:30 -08006827 // Load with fast path based Baker's read barrier.
6828 // /* HeapReference<Object> */ out = *(obj + offset)
6829 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6830 out,
6831 obj_reg,
6832 offset,
6833 maybe_temp,
6834 /* needs_null_check */ false);
6835 } else {
6836 // Load with slow path based read barrier.
6837 // /* HeapReference<Object> */ out = *(obj + offset)
6838 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6839 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6840 }
6841 } else {
6842 // Plain load with no read barrier.
6843 // /* HeapReference<Object> */ out = *(obj + offset)
6844 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6845 __ MaybeUnpoisonHeapReference(out_reg);
6846 }
6847}
6848
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006849static inline int GetBakerMarkThunkNumber(Register reg) {
6850 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6851 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6852 return reg - V0;
6853 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6854 return 14 + (reg - S2);
6855 } else if (reg == FP) { // One more.
6856 return 20;
6857 }
6858 LOG(FATAL) << "Unexpected register " << reg;
6859 UNREACHABLE();
6860}
6861
6862static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6863 int num = GetBakerMarkThunkNumber(reg) +
6864 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6865 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6866}
6867
6868static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6869 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6870 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6871}
6872
Alexey Frunze15958152017-02-09 19:08:30 -08006873void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6874 Location root,
6875 Register obj,
6876 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006877 ReadBarrierOption read_barrier_option,
6878 MipsLabel* label_low) {
6879 bool reordering;
6880 if (label_low != nullptr) {
6881 DCHECK_EQ(offset, 0x5678u);
6882 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006883 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006884 if (read_barrier_option == kWithReadBarrier) {
6885 DCHECK(kEmitCompilerReadBarrier);
6886 if (kUseBakerReadBarrier) {
6887 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6888 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006889 if (kBakerReadBarrierThunksEnableForGcRoots) {
6890 // Note that we do not actually check the value of `GetIsGcMarking()`
6891 // to decide whether to mark the loaded GC root or not. Instead, we
6892 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6893 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6894 // vice versa.
6895 //
6896 // We use thunks for the slow path. That thunk checks the reference
6897 // and jumps to the entrypoint if needed.
6898 //
6899 // temp = Thread::Current()->pReadBarrierMarkReg00
6900 // // AKA &art_quick_read_barrier_mark_introspection.
6901 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6902 // if (temp != nullptr) {
6903 // temp = &gc_root_thunk<root_reg>
6904 // root = temp(root)
6905 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006906
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006907 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6908 const int32_t entry_point_offset =
6909 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6910 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6911 int16_t offset_low = Low16Bits(offset);
6912 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6913 // extension in lw.
6914 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6915 Register base = short_offset ? obj : TMP;
6916 // Loading the entrypoint does not require a load acquire since it is only changed when
6917 // threads are suspended or running a checkpoint.
6918 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6919 reordering = __ SetReorder(false);
6920 if (!short_offset) {
6921 DCHECK(!label_low);
6922 __ AddUpper(base, obj, offset_high);
6923 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006924 MipsLabel skip_call;
6925 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006926 if (label_low != nullptr) {
6927 DCHECK(short_offset);
6928 __ Bind(label_low);
6929 }
6930 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6931 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6932 // in delay slot.
6933 if (isR6) {
6934 __ Jialc(T9, thunk_disp);
6935 } else {
6936 __ Addiu(T9, T9, thunk_disp);
6937 __ Jalr(T9);
6938 __ Nop();
6939 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006940 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006941 __ SetReorder(reordering);
6942 } else {
6943 // Note that we do not actually check the value of `GetIsGcMarking()`
6944 // to decide whether to mark the loaded GC root or not. Instead, we
6945 // load into `temp` (T9) the read barrier mark entry point corresponding
6946 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6947 // is false, and vice versa.
6948 //
6949 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6950 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6951 // if (temp != null) {
6952 // root = temp(root)
6953 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006954
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006955 if (label_low != nullptr) {
6956 reordering = __ SetReorder(false);
6957 __ Bind(label_low);
6958 }
6959 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6960 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6961 if (label_low != nullptr) {
6962 __ SetReorder(reordering);
6963 }
6964 static_assert(
6965 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6966 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6967 "have different sizes.");
6968 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6969 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6970 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006971
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006972 // Slow path marking the GC root `root`.
6973 Location temp = Location::RegisterLocation(T9);
6974 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006975 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006976 instruction,
6977 root,
6978 /*entrypoint*/ temp);
6979 codegen_->AddSlowPath(slow_path);
6980
6981 const int32_t entry_point_offset =
6982 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6983 // Loading the entrypoint does not require a load acquire since it is only changed when
6984 // threads are suspended or running a checkpoint.
6985 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6986 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6987 __ Bind(slow_path->GetExitLabel());
6988 }
Alexey Frunze15958152017-02-09 19:08:30 -08006989 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006990 if (label_low != nullptr) {
6991 reordering = __ SetReorder(false);
6992 __ Bind(label_low);
6993 }
Alexey Frunze15958152017-02-09 19:08:30 -08006994 // GC root loaded through a slow path for read barriers other
6995 // than Baker's.
6996 // /* GcRoot<mirror::Object>* */ root = obj + offset
6997 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006998 if (label_low != nullptr) {
6999 __ SetReorder(reordering);
7000 }
Alexey Frunze15958152017-02-09 19:08:30 -08007001 // /* mirror::Object* */ root = root->Read()
7002 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
7003 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007004 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007005 if (label_low != nullptr) {
7006 reordering = __ SetReorder(false);
7007 __ Bind(label_low);
7008 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007009 // Plain GC root load with no read barrier.
7010 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
7011 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
7012 // Note that GC roots are not affected by heap poisoning, thus we
7013 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007014 if (label_low != nullptr) {
7015 __ SetReorder(reordering);
7016 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007017 }
7018}
7019
Alexey Frunze15958152017-02-09 19:08:30 -08007020void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
7021 Location ref,
7022 Register obj,
7023 uint32_t offset,
7024 Location temp,
7025 bool needs_null_check) {
7026 DCHECK(kEmitCompilerReadBarrier);
7027 DCHECK(kUseBakerReadBarrier);
7028
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007029 if (kBakerReadBarrierThunksEnableForFields) {
7030 // Note that we do not actually check the value of `GetIsGcMarking()`
7031 // to decide whether to mark the loaded reference or not. Instead, we
7032 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7033 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7034 // vice versa.
7035 //
7036 // We use thunks for the slow path. That thunk checks the reference
7037 // and jumps to the entrypoint if needed. If the holder is not gray,
7038 // it issues a load-load memory barrier and returns to the original
7039 // reference load.
7040 //
7041 // temp = Thread::Current()->pReadBarrierMarkReg00
7042 // // AKA &art_quick_read_barrier_mark_introspection.
7043 // if (temp != nullptr) {
7044 // temp = &field_array_thunk<holder_reg>
7045 // temp()
7046 // }
7047 // not_gray_return_address:
7048 // // If the offset is too large to fit into the lw instruction, we
7049 // // use an adjusted base register (TMP) here. This register
7050 // // receives bits 16 ... 31 of the offset before the thunk invocation
7051 // // and the thunk benefits from it.
7052 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
7053 // gray_return_address:
7054
7055 DCHECK(temp.IsInvalid());
7056 bool isR6 = GetInstructionSetFeatures().IsR6();
7057 int16_t offset_low = Low16Bits(offset);
7058 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
7059 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
7060 bool reordering = __ SetReorder(false);
7061 const int32_t entry_point_offset =
7062 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7063 // There may have or may have not been a null check if the field offset is smaller than
7064 // the page size.
7065 // There must've been a null check in case it's actually a load from an array.
7066 // We will, however, perform an explicit null check in the thunk as it's easier to
7067 // do it than not.
7068 if (instruction->IsArrayGet()) {
7069 DCHECK(!needs_null_check);
7070 }
7071 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
7072 // Loading the entrypoint does not require a load acquire since it is only changed when
7073 // threads are suspended or running a checkpoint.
7074 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7075 Register ref_reg = ref.AsRegister<Register>();
7076 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07007077 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007078 if (short_offset) {
7079 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007080 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007081 __ Nop(); // In forbidden slot.
7082 __ Jialc(T9, thunk_disp);
7083 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007084 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007085 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7086 __ Jalr(T9);
7087 __ Nop(); // In delay slot.
7088 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07007089 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007090 } else {
7091 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007092 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007093 __ Aui(base, obj, offset_high); // In delay slot.
7094 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007095 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007096 } else {
7097 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007098 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007099 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7100 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007101 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007102 __ Addu(base, base, obj); // In delay slot.
7103 }
7104 }
7105 // /* HeapReference<Object> */ ref = *(obj + offset)
7106 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
7107 if (needs_null_check) {
7108 MaybeRecordImplicitNullCheck(instruction);
7109 }
7110 __ MaybeUnpoisonHeapReference(ref_reg);
7111 __ SetReorder(reordering);
7112 return;
7113 }
7114
Alexey Frunze15958152017-02-09 19:08:30 -08007115 // /* HeapReference<Object> */ ref = *(obj + offset)
7116 Location no_index = Location::NoLocation();
7117 ScaleFactor no_scale_factor = TIMES_1;
7118 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7119 ref,
7120 obj,
7121 offset,
7122 no_index,
7123 no_scale_factor,
7124 temp,
7125 needs_null_check);
7126}
7127
7128void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
7129 Location ref,
7130 Register obj,
7131 uint32_t data_offset,
7132 Location index,
7133 Location temp,
7134 bool needs_null_check) {
7135 DCHECK(kEmitCompilerReadBarrier);
7136 DCHECK(kUseBakerReadBarrier);
7137
7138 static_assert(
7139 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
7140 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007141 ScaleFactor scale_factor = TIMES_4;
7142
7143 if (kBakerReadBarrierThunksEnableForArrays) {
7144 // Note that we do not actually check the value of `GetIsGcMarking()`
7145 // to decide whether to mark the loaded reference or not. Instead, we
7146 // load into `temp` (T9) the read barrier mark introspection entrypoint.
7147 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
7148 // vice versa.
7149 //
7150 // We use thunks for the slow path. That thunk checks the reference
7151 // and jumps to the entrypoint if needed. If the holder is not gray,
7152 // it issues a load-load memory barrier and returns to the original
7153 // reference load.
7154 //
7155 // temp = Thread::Current()->pReadBarrierMarkReg00
7156 // // AKA &art_quick_read_barrier_mark_introspection.
7157 // if (temp != nullptr) {
7158 // temp = &field_array_thunk<holder_reg>
7159 // temp()
7160 // }
7161 // not_gray_return_address:
7162 // // The element address is pre-calculated in the TMP register before the
7163 // // thunk invocation and the thunk benefits from it.
7164 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
7165 // gray_return_address:
7166
7167 DCHECK(temp.IsInvalid());
7168 DCHECK(index.IsValid());
7169 bool reordering = __ SetReorder(false);
7170 const int32_t entry_point_offset =
7171 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
7172 // We will not do the explicit null check in the thunk as some form of a null check
7173 // must've been done earlier.
7174 DCHECK(!needs_null_check);
7175 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
7176 // Loading the entrypoint does not require a load acquire since it is only changed when
7177 // threads are suspended or running a checkpoint.
7178 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
7179 Register ref_reg = ref.AsRegister<Register>();
7180 Register index_reg = index.IsRegisterPair()
7181 ? index.AsRegisterPairLow<Register>()
7182 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07007183 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007184 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07007185 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007186 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
7187 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007188 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007189 } else {
7190 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007191 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007192 __ Addiu(T9, T9, thunk_disp); // In delay slot.
7193 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07007194 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007195 __ Addu(TMP, TMP, obj); // In delay slot.
7196 }
7197 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
7198 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
7199 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
7200 __ MaybeUnpoisonHeapReference(ref_reg);
7201 __ SetReorder(reordering);
7202 return;
7203 }
7204
Alexey Frunze15958152017-02-09 19:08:30 -08007205 // /* HeapReference<Object> */ ref =
7206 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007207 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7208 ref,
7209 obj,
7210 data_offset,
7211 index,
7212 scale_factor,
7213 temp,
7214 needs_null_check);
7215}
7216
7217void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7218 Location ref,
7219 Register obj,
7220 uint32_t offset,
7221 Location index,
7222 ScaleFactor scale_factor,
7223 Location temp,
7224 bool needs_null_check,
7225 bool always_update_field) {
7226 DCHECK(kEmitCompilerReadBarrier);
7227 DCHECK(kUseBakerReadBarrier);
7228
7229 // In slow path based read barriers, the read barrier call is
7230 // inserted after the original load. However, in fast path based
7231 // Baker's read barriers, we need to perform the load of
7232 // mirror::Object::monitor_ *before* the original reference load.
7233 // This load-load ordering is required by the read barrier.
7234 // The fast path/slow path (for Baker's algorithm) should look like:
7235 //
7236 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7237 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7238 // HeapReference<Object> ref = *src; // Original reference load.
7239 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7240 // if (is_gray) {
7241 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7242 // }
7243 //
7244 // Note: the original implementation in ReadBarrier::Barrier is
7245 // slightly more complex as it performs additional checks that we do
7246 // not do here for performance reasons.
7247
7248 Register ref_reg = ref.AsRegister<Register>();
7249 Register temp_reg = temp.AsRegister<Register>();
7250 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7251
7252 // /* int32_t */ monitor = obj->monitor_
7253 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7254 if (needs_null_check) {
7255 MaybeRecordImplicitNullCheck(instruction);
7256 }
7257 // /* LockWord */ lock_word = LockWord(monitor)
7258 static_assert(sizeof(LockWord) == sizeof(int32_t),
7259 "art::LockWord and int32_t have different sizes.");
7260
7261 __ Sync(0); // Barrier to prevent load-load reordering.
7262
7263 // The actual reference load.
7264 if (index.IsValid()) {
7265 // Load types involving an "index": ArrayGet,
7266 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7267 // intrinsics.
7268 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7269 if (index.IsConstant()) {
7270 size_t computed_offset =
7271 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7272 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7273 } else {
7274 // Handle the special case of the
7275 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7276 // intrinsics, which use a register pair as index ("long
7277 // offset"), of which only the low part contains data.
7278 Register index_reg = index.IsRegisterPair()
7279 ? index.AsRegisterPairLow<Register>()
7280 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007281 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007282 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7283 }
7284 } else {
7285 // /* HeapReference<Object> */ ref = *(obj + offset)
7286 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7287 }
7288
7289 // Object* ref = ref_addr->AsMirrorPtr()
7290 __ MaybeUnpoisonHeapReference(ref_reg);
7291
7292 // Slow path marking the object `ref` when it is gray.
7293 SlowPathCodeMIPS* slow_path;
7294 if (always_update_field) {
7295 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7296 // of the form `obj + field_offset`, where `obj` is a register and
7297 // `field_offset` is a register pair (of which only the lower half
7298 // is used). Thus `offset` and `scale_factor` above are expected
7299 // to be null in this code path.
7300 DCHECK_EQ(offset, 0u);
7301 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007302 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007303 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7304 ref,
7305 obj,
7306 /* field_offset */ index,
7307 temp_reg);
7308 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007309 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007310 }
7311 AddSlowPath(slow_path);
7312
7313 // if (rb_state == ReadBarrier::GrayState())
7314 // ref = ReadBarrier::Mark(ref);
7315 // Given the numeric representation, it's enough to check the low bit of the
7316 // rb_state. We do that by shifting the bit into the sign bit (31) and
7317 // performing a branch on less than zero.
7318 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7319 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7320 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7321 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7322 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7323 __ Bind(slow_path->GetExitLabel());
7324}
7325
7326void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7327 Location out,
7328 Location ref,
7329 Location obj,
7330 uint32_t offset,
7331 Location index) {
7332 DCHECK(kEmitCompilerReadBarrier);
7333
7334 // Insert a slow path based read barrier *after* the reference load.
7335 //
7336 // If heap poisoning is enabled, the unpoisoning of the loaded
7337 // reference will be carried out by the runtime within the slow
7338 // path.
7339 //
7340 // Note that `ref` currently does not get unpoisoned (when heap
7341 // poisoning is enabled), which is alright as the `ref` argument is
7342 // not used by the artReadBarrierSlow entry point.
7343 //
7344 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007345 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007346 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7347 AddSlowPath(slow_path);
7348
7349 __ B(slow_path->GetEntryLabel());
7350 __ Bind(slow_path->GetExitLabel());
7351}
7352
7353void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7354 Location out,
7355 Location ref,
7356 Location obj,
7357 uint32_t offset,
7358 Location index) {
7359 if (kEmitCompilerReadBarrier) {
7360 // Baker's read barriers shall be handled by the fast path
7361 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7362 DCHECK(!kUseBakerReadBarrier);
7363 // If heap poisoning is enabled, unpoisoning will be taken care of
7364 // by the runtime within the slow path.
7365 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7366 } else if (kPoisonHeapReferences) {
7367 __ UnpoisonHeapReference(out.AsRegister<Register>());
7368 }
7369}
7370
7371void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7372 Location out,
7373 Location root) {
7374 DCHECK(kEmitCompilerReadBarrier);
7375
7376 // Insert a slow path based read barrier *after* the GC root load.
7377 //
7378 // Note that GC roots are not affected by heap poisoning, so we do
7379 // not need to do anything special for this here.
7380 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007381 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007382 AddSlowPath(slow_path);
7383
7384 __ B(slow_path->GetEntryLabel());
7385 __ Bind(slow_path->GetExitLabel());
7386}
7387
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007388void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007389 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7390 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007391 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007392 switch (type_check_kind) {
7393 case TypeCheckKind::kExactCheck:
7394 case TypeCheckKind::kAbstractClassCheck:
7395 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007396 case TypeCheckKind::kArrayObjectCheck: {
7397 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7398 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7399 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007400 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007401 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007402 case TypeCheckKind::kArrayCheck:
7403 case TypeCheckKind::kUnresolvedCheck:
7404 case TypeCheckKind::kInterfaceCheck:
7405 call_kind = LocationSummary::kCallOnSlowPath;
7406 break;
7407 }
7408
Vladimir Markoca6fff82017-10-03 14:49:14 +01007409 LocationSummary* locations =
7410 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007411 if (baker_read_barrier_slow_path) {
7412 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7413 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007414 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007415 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007416 // The output does overlap inputs.
7417 // Note that TypeCheckSlowPathMIPS uses this register too.
7418 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007419 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007420}
7421
7422void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007423 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007424 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007425 Location obj_loc = locations->InAt(0);
7426 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007427 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007428 Location out_loc = locations->Out();
7429 Register out = out_loc.AsRegister<Register>();
7430 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7431 DCHECK_LE(num_temps, 1u);
7432 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007433 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7434 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7435 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7436 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007437 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007438 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007439
7440 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007441 // Avoid this check if we know `obj` is not null.
7442 if (instruction->MustDoNullCheck()) {
7443 __ Move(out, ZERO);
7444 __ Beqz(obj, &done);
7445 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007446
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007447 switch (type_check_kind) {
7448 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007449 ReadBarrierOption read_barrier_option =
7450 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007451 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007452 GenerateReferenceLoadTwoRegisters(instruction,
7453 out_loc,
7454 obj_loc,
7455 class_offset,
7456 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007457 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007458 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007459 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007460 __ Sltiu(out, out, 1);
7461 break;
7462 }
7463
7464 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007465 ReadBarrierOption read_barrier_option =
7466 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007467 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007468 GenerateReferenceLoadTwoRegisters(instruction,
7469 out_loc,
7470 obj_loc,
7471 class_offset,
7472 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007473 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007474 // If the class is abstract, we eagerly fetch the super class of the
7475 // object to avoid doing a comparison we know will fail.
7476 MipsLabel loop;
7477 __ Bind(&loop);
7478 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007479 GenerateReferenceLoadOneRegister(instruction,
7480 out_loc,
7481 super_offset,
7482 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007483 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007484 // If `out` is null, we use it for the result, and jump to `done`.
7485 __ Beqz(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007486 __ Bne(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007487 __ LoadConst32(out, 1);
7488 break;
7489 }
7490
7491 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007492 ReadBarrierOption read_barrier_option =
7493 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007494 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007495 GenerateReferenceLoadTwoRegisters(instruction,
7496 out_loc,
7497 obj_loc,
7498 class_offset,
7499 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007500 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007501 // Walk over the class hierarchy to find a match.
7502 MipsLabel loop, success;
7503 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007504 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007505 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007506 GenerateReferenceLoadOneRegister(instruction,
7507 out_loc,
7508 super_offset,
7509 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007510 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007511 __ Bnez(out, &loop);
7512 // If `out` is null, we use it for the result, and jump to `done`.
7513 __ B(&done);
7514 __ Bind(&success);
7515 __ LoadConst32(out, 1);
7516 break;
7517 }
7518
7519 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007520 ReadBarrierOption read_barrier_option =
7521 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007522 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007523 GenerateReferenceLoadTwoRegisters(instruction,
7524 out_loc,
7525 obj_loc,
7526 class_offset,
7527 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007528 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007529 // Do an exact check.
7530 MipsLabel success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007531 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007532 // Otherwise, we need to check that the object's class is a non-primitive array.
7533 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007534 GenerateReferenceLoadOneRegister(instruction,
7535 out_loc,
7536 component_offset,
7537 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007538 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007539 // If `out` is null, we use it for the result, and jump to `done`.
7540 __ Beqz(out, &done);
7541 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7542 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7543 __ Sltiu(out, out, 1);
7544 __ B(&done);
7545 __ Bind(&success);
7546 __ LoadConst32(out, 1);
7547 break;
7548 }
7549
7550 case TypeCheckKind::kArrayCheck: {
7551 // No read barrier since the slow path will retry upon failure.
7552 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007553 GenerateReferenceLoadTwoRegisters(instruction,
7554 out_loc,
7555 obj_loc,
7556 class_offset,
7557 maybe_temp_loc,
7558 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007559 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007560 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7561 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007562 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007563 __ Bne(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007564 __ LoadConst32(out, 1);
7565 break;
7566 }
7567
7568 case TypeCheckKind::kUnresolvedCheck:
7569 case TypeCheckKind::kInterfaceCheck: {
7570 // Note that we indeed only call on slow path, but we always go
7571 // into the slow path for the unresolved and interface check
7572 // cases.
7573 //
7574 // We cannot directly call the InstanceofNonTrivial runtime
7575 // entry point without resorting to a type checking slow path
7576 // here (i.e. by calling InvokeRuntime directly), as it would
7577 // require to assign fixed registers for the inputs of this
7578 // HInstanceOf instruction (following the runtime calling
7579 // convention), which might be cluttered by the potential first
7580 // read barrier emission at the beginning of this method.
7581 //
7582 // TODO: Introduce a new runtime entry point taking the object
7583 // to test (instead of its class) as argument, and let it deal
7584 // with the read barrier issues. This will let us refactor this
7585 // case of the `switch` code as it was previously (with a direct
7586 // call to the runtime not using a type checking slow path).
7587 // This should also be beneficial for the other cases above.
7588 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007589 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7590 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007591 codegen_->AddSlowPath(slow_path);
7592 __ B(slow_path->GetEntryLabel());
7593 break;
7594 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007595 }
7596
7597 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007598
7599 if (slow_path != nullptr) {
7600 __ Bind(slow_path->GetExitLabel());
7601 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007602}
7603
7604void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007605 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007606 locations->SetOut(Location::ConstantLocation(constant));
7607}
7608
7609void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7610 // Will be generated at use site.
7611}
7612
7613void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007614 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007615 locations->SetOut(Location::ConstantLocation(constant));
7616}
7617
7618void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7619 // Will be generated at use site.
7620}
7621
7622void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7623 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7624 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7625}
7626
7627void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7628 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007629 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007630 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007631 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007632}
7633
7634void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7635 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7636 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007637 Location receiver = invoke->GetLocations()->InAt(0);
7638 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007639 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007640
7641 // Set the hidden argument.
7642 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7643 invoke->GetDexMethodIndex());
7644
7645 // temp = object->GetClass();
7646 if (receiver.IsStackSlot()) {
7647 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7648 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7649 } else {
7650 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7651 }
7652 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007653 // Instead of simply (possibly) unpoisoning `temp` here, we should
7654 // emit a read barrier for the previous class reference load.
7655 // However this is not required in practice, as this is an
7656 // intermediate/temporary reference and because the current
7657 // concurrent copying collector keeps the from-space memory
7658 // intact/accessible until the end of the marking phase (the
7659 // concurrent copying collector may not in the future).
7660 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007661 __ LoadFromOffset(kLoadWord, temp, temp,
7662 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7663 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007664 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007665 // temp = temp->GetImtEntryAt(method_offset);
7666 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7667 // T9 = temp->GetEntryPoint();
7668 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7669 // T9();
7670 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007671 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007672 DCHECK(!codegen_->IsLeafMethod());
7673 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7674}
7675
7676void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007677 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7678 if (intrinsic.TryDispatch(invoke)) {
7679 return;
7680 }
7681
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007682 HandleInvoke(invoke);
7683}
7684
7685void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007686 // Explicit clinit checks triggered by static invokes must have been pruned by
7687 // art::PrepareForRegisterAllocation.
7688 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007689
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007690 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007691 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7692 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007693
Chris Larsen701566a2015-10-27 15:29:13 -07007694 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7695 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007696 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7697 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7698 }
Chris Larsen701566a2015-10-27 15:29:13 -07007699 return;
7700 }
7701
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007702 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007703
7704 // Add the extra input register if either the dex cache array base register
7705 // or the PC-relative base register for accessing literals is needed.
7706 if (has_extra_input) {
7707 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7708 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007709}
7710
Orion Hodsonac141392017-01-13 11:53:47 +00007711void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7712 HandleInvoke(invoke);
7713}
7714
7715void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7716 codegen_->GenerateInvokePolymorphicCall(invoke);
7717}
7718
Chris Larsen701566a2015-10-27 15:29:13 -07007719static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007720 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007721 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7722 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007723 return true;
7724 }
7725 return false;
7726}
7727
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007728HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007729 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007730 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007731 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007732 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007733 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007734 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007735 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007736 case HLoadString::LoadKind::kJitTableAddress:
7737 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007738 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007739 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007740 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007741 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007742 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007743 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007744}
7745
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007746HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7747 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007748 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007749 case HLoadClass::LoadKind::kInvalid:
7750 LOG(FATAL) << "UNREACHABLE";
7751 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007752 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007753 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007754 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007755 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007756 case HLoadClass::LoadKind::kBssEntry:
7757 DCHECK(!Runtime::Current()->UseJitCompilation());
7758 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007759 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007760 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007761 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007762 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007763 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007764 break;
7765 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007766 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007767}
7768
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007769Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7770 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007771 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007772 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007773 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7774 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7775 if (!invoke->GetLocations()->Intrinsified()) {
7776 return location.AsRegister<Register>();
7777 }
7778 // For intrinsics we allow any location, so it may be on the stack.
7779 if (!location.IsRegister()) {
7780 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7781 return temp;
7782 }
7783 // For register locations, check if the register was saved. If so, get it from the stack.
7784 // Note: There is a chance that the register was saved but not overwritten, so we could
7785 // save one load. However, since this is just an intrinsic slow path we prefer this
7786 // simple and more robust approach rather that trying to determine if that's the case.
7787 SlowPathCode* slow_path = GetCurrentSlowPath();
7788 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7789 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7790 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7791 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7792 return temp;
7793 }
7794 return location.AsRegister<Register>();
7795}
7796
Vladimir Markodc151b22015-10-15 18:02:30 +01007797HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7798 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007799 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007800 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007801}
7802
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007803void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7804 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007805 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007806 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007807 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7808 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007809 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007810 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7811 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007812 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7813 : ZERO;
7814
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007815 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007816 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007817 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007818 uint32_t offset =
7819 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007820 __ LoadFromOffset(kLoadWord,
7821 temp.AsRegister<Register>(),
7822 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007823 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007824 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007825 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007826 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007827 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007828 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007829 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7830 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007831 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7832 PcRelativePatchInfo* info_low =
7833 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007834 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007835 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7836 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007837 break;
7838 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007839 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7840 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7841 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007842 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007843 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007844 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007845 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7846 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007847 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007848 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7849 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007850 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007851 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007852 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7853 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7854 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007855 }
7856 }
7857
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007858 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007859 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007860 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007861 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007862 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7863 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007864 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007865 T9,
7866 callee_method.AsRegister<Register>(),
7867 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007868 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007869 // T9()
7870 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007871 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007872 break;
7873 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007874 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7875
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007876 DCHECK(!IsLeafMethod());
7877}
7878
7879void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007880 // Explicit clinit checks triggered by static invokes must have been pruned by
7881 // art::PrepareForRegisterAllocation.
7882 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007883
7884 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7885 return;
7886 }
7887
7888 LocationSummary* locations = invoke->GetLocations();
7889 codegen_->GenerateStaticOrDirectCall(invoke,
7890 locations->HasTemps()
7891 ? locations->GetTemp(0)
7892 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007893}
7894
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007895void CodeGeneratorMIPS::GenerateVirtualCall(
7896 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007897 // Use the calling convention instead of the location of the receiver, as
7898 // intrinsics may have put the receiver in a different register. In the intrinsics
7899 // slow path, the arguments have been moved to the right place, so here we are
7900 // guaranteed that the receiver is the first register of the calling convention.
7901 InvokeDexCallingConvention calling_convention;
7902 Register receiver = calling_convention.GetRegisterAt(0);
7903
Chris Larsen3acee732015-11-18 13:31:08 -08007904 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007905 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7906 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7907 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007908 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007909
7910 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007911 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007912 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007913 // Instead of simply (possibly) unpoisoning `temp` here, we should
7914 // emit a read barrier for the previous class reference load.
7915 // However this is not required in practice, as this is an
7916 // intermediate/temporary reference and because the current
7917 // concurrent copying collector keeps the from-space memory
7918 // intact/accessible until the end of the marking phase (the
7919 // concurrent copying collector may not in the future).
7920 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007921 // temp = temp->GetMethodAt(method_offset);
7922 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7923 // T9 = temp->GetEntryPoint();
7924 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7925 // T9();
7926 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007927 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007928 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007929}
7930
7931void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7932 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7933 return;
7934 }
7935
7936 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007937 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007938}
7939
7940void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007941 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007942 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007943 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007944 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7945 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007946 return;
7947 }
Vladimir Marko41559982017-01-06 14:04:23 +00007948 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007949 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007950 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007951 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7952 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007953 ? LocationSummary::kCallOnSlowPath
7954 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007955 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007956 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7957 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7958 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007959 switch (load_kind) {
7960 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007961 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007962 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007963 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007964 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007965 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007966 break;
7967 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007968 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007969 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7970 codegen_->ClobberRA();
7971 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007972 break;
7973 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007974 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007975 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007976 locations->SetInAt(0, Location::RequiresRegister());
7977 break;
7978 default:
7979 break;
7980 }
7981 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007982 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7983 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7984 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007985 RegisterSet caller_saves = RegisterSet::Empty();
7986 InvokeRuntimeCallingConvention calling_convention;
7987 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7988 locations->SetCustomSlowPathCallerSaves(caller_saves);
7989 } else {
7990 // For non-Baker read barriers we have a temp-clobbering call.
7991 }
7992 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007993}
7994
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007995// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7996// move.
7997void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007998 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007999 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00008000 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01008001 return;
8002 }
Vladimir Marko41559982017-01-06 14:04:23 +00008003 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01008004
Vladimir Marko41559982017-01-06 14:04:23 +00008005 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008006 Location out_loc = locations->Out();
8007 Register out = out_loc.AsRegister<Register>();
8008 Register base_or_current_method_reg;
8009 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008010 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008011 switch (load_kind) {
8012 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008013 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008014 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008015 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008016 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008017 base_or_current_method_reg =
8018 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008019 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008020 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008021 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008022 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
8023 break;
8024 default:
8025 base_or_current_method_reg = ZERO;
8026 break;
8027 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00008028
Alexey Frunze15958152017-02-09 19:08:30 -08008029 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
8030 ? kWithoutReadBarrier
8031 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008032 bool generate_null_check = false;
8033 switch (load_kind) {
8034 case HLoadClass::LoadKind::kReferrersClass: {
8035 DCHECK(!cls->CanCallRuntime());
8036 DCHECK(!cls->MustGenerateClinitCheck());
8037 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
8038 GenerateGcRootFieldLoad(cls,
8039 out_loc,
8040 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08008041 ArtMethod::DeclaringClassOffset().Int32Value(),
8042 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008043 break;
8044 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008045 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008046 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08008047 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008048 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07008049 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008050 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8051 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008052 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8053 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008054 base_or_current_method_reg);
8055 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008056 break;
8057 }
8058 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08008059 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00008060 uint32_t address = dchecked_integral_cast<uint32_t>(
8061 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
8062 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008063 if (isR6 || !has_irreducible_loops) {
8064 __ LoadLiteral(out,
8065 base_or_current_method_reg,
8066 codegen_->DeduplicateBootImageAddressLiteral(address));
8067 } else {
8068 __ LoadConst32(out, address);
8069 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008070 break;
8071 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01008072 case HLoadClass::LoadKind::kBootImageClassTable: {
8073 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8074 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8075 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
8076 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8077 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
8078 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8079 out,
8080 base_or_current_method_reg);
8081 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8082 // Extract the reference from the slot data, i.e. clear the hash bits.
8083 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
8084 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
8085 if (masked_hash != 0) {
8086 __ Addiu(out, out, -masked_hash);
8087 }
8088 break;
8089 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008090 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00008091 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
8092 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008093 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8094 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008095 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008096 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008097 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008098 GenerateGcRootFieldLoad(cls,
8099 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008100 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008101 /* placeholder */ 0x5678,
8102 read_barrier_option,
8103 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008104 generate_null_check = true;
8105 break;
8106 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00008107 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08008108 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
8109 cls->GetTypeIndex(),
8110 cls->GetClass());
8111 bool reordering = __ SetReorder(false);
8112 __ Bind(&info->high_label);
8113 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008114 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008115 GenerateGcRootFieldLoad(cls,
8116 out_loc,
8117 out,
8118 /* placeholder */ 0x5678,
8119 read_barrier_option,
8120 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008121 break;
8122 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008123 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00008124 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00008125 LOG(FATAL) << "UNREACHABLE";
8126 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008127 }
8128
8129 if (generate_null_check || cls->MustGenerateClinitCheck()) {
8130 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01008131 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00008132 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07008133 codegen_->AddSlowPath(slow_path);
8134 if (generate_null_check) {
8135 __ Beqz(out, slow_path->GetEntryLabel());
8136 }
8137 if (cls->MustGenerateClinitCheck()) {
8138 GenerateClassInitializationCheck(slow_path, out);
8139 } else {
8140 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008141 }
8142 }
8143}
8144
8145static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07008146 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008147}
8148
8149void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
8150 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008151 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008152 locations->SetOut(Location::RequiresRegister());
8153}
8154
8155void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
8156 Register out = load->GetLocations()->Out().AsRegister<Register>();
8157 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
8158}
8159
8160void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008161 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008162}
8163
8164void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
8165 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
8166}
8167
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008168void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08008169 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01008170 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07008171 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07008172 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008173 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008174 switch (load_kind) {
8175 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008176 case HLoadString::LoadKind::kBootImageAddress:
8177 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008178 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008179 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07008180 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008181 break;
8182 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008183 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07008184 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
8185 codegen_->ClobberRA();
8186 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008187 break;
8188 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008189 FALLTHROUGH_INTENDED;
8190 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008191 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07008192 locations->SetInAt(0, Location::RequiresRegister());
8193 break;
8194 default:
8195 break;
8196 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008197 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07008198 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008199 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07008200 } else {
8201 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008202 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8203 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8204 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008205 RegisterSet caller_saves = RegisterSet::Empty();
8206 InvokeRuntimeCallingConvention calling_convention;
8207 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8208 locations->SetCustomSlowPathCallerSaves(caller_saves);
8209 } else {
8210 // For non-Baker read barriers we have a temp-clobbering call.
8211 }
8212 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008213 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008214}
8215
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008216// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8217// move.
8218void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008219 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008220 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008221 Location out_loc = locations->Out();
8222 Register out = out_loc.AsRegister<Register>();
8223 Register base_or_current_method_reg;
8224 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008225 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008226 switch (load_kind) {
8227 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008228 case HLoadString::LoadKind::kBootImageAddress:
8229 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008230 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008231 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008232 base_or_current_method_reg =
8233 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008234 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008235 default:
8236 base_or_current_method_reg = ZERO;
8237 break;
8238 }
8239
8240 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008241 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008242 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008243 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008244 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008245 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8246 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008247 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8248 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008249 base_or_current_method_reg);
8250 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008251 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008252 }
8253 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008254 uint32_t address = dchecked_integral_cast<uint32_t>(
8255 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8256 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008257 if (isR6 || !has_irreducible_loops) {
8258 __ LoadLiteral(out,
8259 base_or_current_method_reg,
8260 codegen_->DeduplicateBootImageAddressLiteral(address));
8261 } else {
8262 __ LoadConst32(out, address);
8263 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008264 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008265 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008266 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008267 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008268 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008269 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008270 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8271 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008272 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8273 out,
8274 base_or_current_method_reg);
8275 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8276 return;
8277 }
8278 case HLoadString::LoadKind::kBssEntry: {
8279 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8280 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8281 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8282 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8283 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008284 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008285 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008286 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008287 GenerateGcRootFieldLoad(load,
8288 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008289 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008290 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008291 kCompilerReadBarrierOption,
8292 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008293 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008294 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008295 codegen_->AddSlowPath(slow_path);
8296 __ Beqz(out, slow_path->GetEntryLabel());
8297 __ Bind(slow_path->GetExitLabel());
8298 return;
8299 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008300 case HLoadString::LoadKind::kJitTableAddress: {
8301 CodeGeneratorMIPS::JitPatchInfo* info =
8302 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8303 load->GetStringIndex(),
8304 load->GetString());
8305 bool reordering = __ SetReorder(false);
8306 __ Bind(&info->high_label);
8307 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008308 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008309 GenerateGcRootFieldLoad(load,
8310 out_loc,
8311 out,
8312 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008313 kCompilerReadBarrierOption,
8314 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008315 return;
8316 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008317 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008318 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008319 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008320
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008321 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008322 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008323 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008324 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008325 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008326 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8327 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008328}
8329
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008330void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008331 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008332 locations->SetOut(Location::ConstantLocation(constant));
8333}
8334
8335void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8336 // Will be generated at use site.
8337}
8338
8339void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008340 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8341 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008342 InvokeRuntimeCallingConvention calling_convention;
8343 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8344}
8345
8346void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8347 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008348 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008349 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8350 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008351 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008352 }
8353 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8354}
8355
8356void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8357 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008358 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008359 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008360 case DataType::Type::kInt32:
8361 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008362 locations->SetInAt(0, Location::RequiresRegister());
8363 locations->SetInAt(1, Location::RequiresRegister());
8364 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8365 break;
8366
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008367 case DataType::Type::kFloat32:
8368 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008369 locations->SetInAt(0, Location::RequiresFpuRegister());
8370 locations->SetInAt(1, Location::RequiresFpuRegister());
8371 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8372 break;
8373
8374 default:
8375 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8376 }
8377}
8378
8379void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008380 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008381 LocationSummary* locations = instruction->GetLocations();
8382 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8383
8384 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008385 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008386 Register dst = locations->Out().AsRegister<Register>();
8387 Register lhs = locations->InAt(0).AsRegister<Register>();
8388 Register rhs = locations->InAt(1).AsRegister<Register>();
8389
8390 if (isR6) {
8391 __ MulR6(dst, lhs, rhs);
8392 } else {
8393 __ MulR2(dst, lhs, rhs);
8394 }
8395 break;
8396 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008397 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008398 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8399 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8400 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8401 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8402 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8403 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8404
8405 // Extra checks to protect caused by the existance of A1_A2.
8406 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8407 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8408 DCHECK_NE(dst_high, lhs_low);
8409 DCHECK_NE(dst_high, rhs_low);
8410
8411 // A_B * C_D
8412 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8413 // dst_lo: [ low(B*D) ]
8414 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8415
8416 if (isR6) {
8417 __ MulR6(TMP, lhs_high, rhs_low);
8418 __ MulR6(dst_high, lhs_low, rhs_high);
8419 __ Addu(dst_high, dst_high, TMP);
8420 __ MuhuR6(TMP, lhs_low, rhs_low);
8421 __ Addu(dst_high, dst_high, TMP);
8422 __ MulR6(dst_low, lhs_low, rhs_low);
8423 } else {
8424 __ MulR2(TMP, lhs_high, rhs_low);
8425 __ MulR2(dst_high, lhs_low, rhs_high);
8426 __ Addu(dst_high, dst_high, TMP);
8427 __ MultuR2(lhs_low, rhs_low);
8428 __ Mfhi(TMP);
8429 __ Addu(dst_high, dst_high, TMP);
8430 __ Mflo(dst_low);
8431 }
8432 break;
8433 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008434 case DataType::Type::kFloat32:
8435 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008436 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8437 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8438 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008439 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008440 __ MulS(dst, lhs, rhs);
8441 } else {
8442 __ MulD(dst, lhs, rhs);
8443 }
8444 break;
8445 }
8446 default:
8447 LOG(FATAL) << "Unexpected mul type " << type;
8448 }
8449}
8450
8451void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8452 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008453 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008454 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008455 case DataType::Type::kInt32:
8456 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008457 locations->SetInAt(0, Location::RequiresRegister());
8458 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8459 break;
8460
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008461 case DataType::Type::kFloat32:
8462 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008463 locations->SetInAt(0, Location::RequiresFpuRegister());
8464 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8465 break;
8466
8467 default:
8468 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8469 }
8470}
8471
8472void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008473 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008474 LocationSummary* locations = instruction->GetLocations();
8475
8476 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008477 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008478 Register dst = locations->Out().AsRegister<Register>();
8479 Register src = locations->InAt(0).AsRegister<Register>();
8480 __ Subu(dst, ZERO, src);
8481 break;
8482 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008483 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008484 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8485 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8486 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8487 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8488 __ Subu(dst_low, ZERO, src_low);
8489 __ Sltu(TMP, ZERO, dst_low);
8490 __ Subu(dst_high, ZERO, src_high);
8491 __ Subu(dst_high, dst_high, TMP);
8492 break;
8493 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008494 case DataType::Type::kFloat32:
8495 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008496 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8497 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008498 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008499 __ NegS(dst, src);
8500 } else {
8501 __ NegD(dst, src);
8502 }
8503 break;
8504 }
8505 default:
8506 LOG(FATAL) << "Unexpected neg type " << type;
8507 }
8508}
8509
8510void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008511 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8512 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008513 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008514 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008515 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8516 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008517}
8518
8519void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008520 // Note: if heap poisoning is enabled, the entry point takes care
8521 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008522 QuickEntrypointEnum entrypoint =
8523 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8524 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008525 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008526 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008527}
8528
8529void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008530 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8531 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008532 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008533 if (instruction->IsStringAlloc()) {
8534 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8535 } else {
8536 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008537 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008538 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008539}
8540
8541void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008542 // Note: if heap poisoning is enabled, the entry point takes care
8543 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008544 if (instruction->IsStringAlloc()) {
8545 // String is allocated through StringFactory. Call NewEmptyString entry point.
8546 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008547 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008548 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8549 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8550 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008551 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008552 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8553 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008554 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008555 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008556 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008557}
8558
8559void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008560 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008561 locations->SetInAt(0, Location::RequiresRegister());
8562 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8563}
8564
8565void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008566 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008567 LocationSummary* locations = instruction->GetLocations();
8568
8569 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008570 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008571 Register dst = locations->Out().AsRegister<Register>();
8572 Register src = locations->InAt(0).AsRegister<Register>();
8573 __ Nor(dst, src, ZERO);
8574 break;
8575 }
8576
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008577 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008578 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8579 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8580 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8581 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8582 __ Nor(dst_high, src_high, ZERO);
8583 __ Nor(dst_low, src_low, ZERO);
8584 break;
8585 }
8586
8587 default:
8588 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8589 }
8590}
8591
8592void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008593 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008594 locations->SetInAt(0, Location::RequiresRegister());
8595 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8596}
8597
8598void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8599 LocationSummary* locations = instruction->GetLocations();
8600 __ Xori(locations->Out().AsRegister<Register>(),
8601 locations->InAt(0).AsRegister<Register>(),
8602 1);
8603}
8604
8605void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008606 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8607 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008608}
8609
Calin Juravle2ae48182016-03-16 14:05:09 +00008610void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8611 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008612 return;
8613 }
8614 Location obj = instruction->GetLocations()->InAt(0);
8615
8616 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008617 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008618}
8619
Calin Juravle2ae48182016-03-16 14:05:09 +00008620void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008621 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008622 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008623
8624 Location obj = instruction->GetLocations()->InAt(0);
8625
8626 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8627}
8628
8629void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008630 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008631}
8632
8633void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8634 HandleBinaryOp(instruction);
8635}
8636
8637void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8638 HandleBinaryOp(instruction);
8639}
8640
8641void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8642 LOG(FATAL) << "Unreachable";
8643}
8644
8645void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008646 if (instruction->GetNext()->IsSuspendCheck() &&
8647 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8648 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8649 // The back edge will generate the suspend check.
8650 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8651 }
8652
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008653 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8654}
8655
8656void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008657 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008658 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8659 if (location.IsStackSlot()) {
8660 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8661 } else if (location.IsDoubleStackSlot()) {
8662 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8663 }
8664 locations->SetOut(location);
8665}
8666
8667void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8668 ATTRIBUTE_UNUSED) {
8669 // Nothing to do, the parameter is already at its location.
8670}
8671
8672void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8673 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008674 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008675 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8676}
8677
8678void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8679 ATTRIBUTE_UNUSED) {
8680 // Nothing to do, the method is already at its location.
8681}
8682
8683void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008684 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008685 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008686 locations->SetInAt(i, Location::Any());
8687 }
8688 locations->SetOut(Location::Any());
8689}
8690
8691void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8692 LOG(FATAL) << "Unreachable";
8693}
8694
8695void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008696 DataType::Type type = rem->GetResultType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008697 bool call_rem;
8698 if ((type == DataType::Type::kInt64) && rem->InputAt(1)->IsConstant()) {
8699 int64_t imm = CodeGenerator::GetInt64ValueOf(rem->InputAt(1)->AsConstant());
8700 call_rem = (imm != 0) && !IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm)));
8701 } else {
8702 call_rem = (type != DataType::Type::kInt32);
8703 }
8704 LocationSummary::CallKind call_kind = call_rem
8705 ? LocationSummary::kCallOnMainOnly
8706 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008707 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008708
8709 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008710 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008711 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008712 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008713 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8714 break;
8715
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008716 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008717 if (call_rem) {
8718 InvokeRuntimeCallingConvention calling_convention;
8719 locations->SetInAt(0, Location::RegisterPairLocation(
8720 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8721 locations->SetInAt(1, Location::RegisterPairLocation(
8722 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8723 locations->SetOut(calling_convention.GetReturnLocation(type));
8724 } else {
8725 locations->SetInAt(0, Location::RequiresRegister());
8726 locations->SetInAt(1, Location::ConstantLocation(rem->InputAt(1)->AsConstant()));
8727 locations->SetOut(Location::RequiresRegister());
8728 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008729 break;
8730 }
8731
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008732 case DataType::Type::kFloat32:
8733 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008734 InvokeRuntimeCallingConvention calling_convention;
8735 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8736 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8737 locations->SetOut(calling_convention.GetReturnLocation(type));
8738 break;
8739 }
8740
8741 default:
8742 LOG(FATAL) << "Unexpected rem type " << type;
8743 }
8744}
8745
8746void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008747 DataType::Type type = instruction->GetType();
Lena Djokic4b8025c2017-12-21 16:15:50 +01008748 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008749
8750 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008751 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008752 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008753 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008754 case DataType::Type::kInt64: {
Lena Djokic4b8025c2017-12-21 16:15:50 +01008755 if (locations->InAt(1).IsConstant()) {
8756 int64_t imm = locations->InAt(1).GetConstant()->AsLongConstant()->GetValue();
8757 if (imm == 0) {
8758 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
8759 } else if (imm == 1 || imm == -1) {
8760 DivRemOneOrMinusOne(instruction);
8761 } else {
8762 DCHECK(IsPowerOfTwo(static_cast<uint64_t>(AbsOrMin(imm))));
8763 DivRemByPowerOfTwo(instruction);
8764 }
8765 } else {
8766 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
8767 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8768 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008769 break;
8770 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008771 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008772 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008773 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008774 break;
8775 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008776 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008777 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008778 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008779 break;
8780 }
8781 default:
8782 LOG(FATAL) << "Unexpected rem type " << type;
8783 }
8784}
8785
Igor Murashkind01745e2017-04-05 16:40:31 -07008786void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8787 constructor_fence->SetLocations(nullptr);
8788}
8789
8790void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8791 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8792 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8793}
8794
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008795void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8796 memory_barrier->SetLocations(nullptr);
8797}
8798
8799void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8800 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8801}
8802
8803void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008804 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008805 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008806 locations->SetInAt(0, MipsReturnLocation(return_type));
8807}
8808
8809void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8810 codegen_->GenerateFrameExit();
8811}
8812
8813void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8814 ret->SetLocations(nullptr);
8815}
8816
8817void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8818 codegen_->GenerateFrameExit();
8819}
8820
Alexey Frunze92d90602015-12-18 18:16:36 -08008821void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8822 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008823}
8824
Alexey Frunze92d90602015-12-18 18:16:36 -08008825void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8826 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008827}
8828
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008829void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8830 HandleShift(shl);
8831}
8832
8833void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8834 HandleShift(shl);
8835}
8836
8837void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8838 HandleShift(shr);
8839}
8840
8841void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8842 HandleShift(shr);
8843}
8844
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008845void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8846 HandleBinaryOp(instruction);
8847}
8848
8849void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8850 HandleBinaryOp(instruction);
8851}
8852
8853void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8854 HandleFieldGet(instruction, instruction->GetFieldInfo());
8855}
8856
8857void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8858 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8859}
8860
8861void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8862 HandleFieldSet(instruction, instruction->GetFieldInfo());
8863}
8864
8865void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008866 HandleFieldSet(instruction,
8867 instruction->GetFieldInfo(),
8868 instruction->GetDexPc(),
8869 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008870}
8871
8872void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8873 HUnresolvedInstanceFieldGet* instruction) {
8874 FieldAccessCallingConventionMIPS calling_convention;
8875 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8876 instruction->GetFieldType(),
8877 calling_convention);
8878}
8879
8880void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8881 HUnresolvedInstanceFieldGet* instruction) {
8882 FieldAccessCallingConventionMIPS calling_convention;
8883 codegen_->GenerateUnresolvedFieldAccess(instruction,
8884 instruction->GetFieldType(),
8885 instruction->GetFieldIndex(),
8886 instruction->GetDexPc(),
8887 calling_convention);
8888}
8889
8890void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8891 HUnresolvedInstanceFieldSet* instruction) {
8892 FieldAccessCallingConventionMIPS calling_convention;
8893 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8894 instruction->GetFieldType(),
8895 calling_convention);
8896}
8897
8898void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8899 HUnresolvedInstanceFieldSet* instruction) {
8900 FieldAccessCallingConventionMIPS calling_convention;
8901 codegen_->GenerateUnresolvedFieldAccess(instruction,
8902 instruction->GetFieldType(),
8903 instruction->GetFieldIndex(),
8904 instruction->GetDexPc(),
8905 calling_convention);
8906}
8907
8908void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8909 HUnresolvedStaticFieldGet* instruction) {
8910 FieldAccessCallingConventionMIPS calling_convention;
8911 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8912 instruction->GetFieldType(),
8913 calling_convention);
8914}
8915
8916void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8917 HUnresolvedStaticFieldGet* instruction) {
8918 FieldAccessCallingConventionMIPS calling_convention;
8919 codegen_->GenerateUnresolvedFieldAccess(instruction,
8920 instruction->GetFieldType(),
8921 instruction->GetFieldIndex(),
8922 instruction->GetDexPc(),
8923 calling_convention);
8924}
8925
8926void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8927 HUnresolvedStaticFieldSet* instruction) {
8928 FieldAccessCallingConventionMIPS calling_convention;
8929 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8930 instruction->GetFieldType(),
8931 calling_convention);
8932}
8933
8934void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8935 HUnresolvedStaticFieldSet* instruction) {
8936 FieldAccessCallingConventionMIPS calling_convention;
8937 codegen_->GenerateUnresolvedFieldAccess(instruction,
8938 instruction->GetFieldType(),
8939 instruction->GetFieldIndex(),
8940 instruction->GetDexPc(),
8941 calling_convention);
8942}
8943
8944void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008945 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8946 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008947 // In suspend check slow path, usually there are no caller-save registers at all.
8948 // If SIMD instructions are present, however, we force spilling all live SIMD
8949 // registers in full width (since the runtime only saves/restores lower part).
8950 locations->SetCustomSlowPathCallerSaves(
8951 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008952}
8953
8954void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8955 HBasicBlock* block = instruction->GetBlock();
8956 if (block->GetLoopInformation() != nullptr) {
8957 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8958 // The back edge will generate the suspend check.
8959 return;
8960 }
8961 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8962 // The goto will generate the suspend check.
8963 return;
8964 }
8965 GenerateSuspendCheck(instruction, nullptr);
8966}
8967
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008968void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008969 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8970 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008971 InvokeRuntimeCallingConvention calling_convention;
8972 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8973}
8974
8975void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008976 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008977 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8978}
8979
8980void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008981 DataType::Type input_type = conversion->GetInputType();
8982 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008983 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8984 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008985 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008986
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008987 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8988 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008989 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8990 }
8991
8992 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008993 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008994 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8995 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008996 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008997 }
8998
Vladimir Markoca6fff82017-10-03 14:49:14 +01008999 LocationSummary* locations =
9000 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009001
9002 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009003 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009004 locations->SetInAt(0, Location::RequiresFpuRegister());
9005 } else {
9006 locations->SetInAt(0, Location::RequiresRegister());
9007 }
9008
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009009 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009010 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
9011 } else {
9012 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
9013 }
9014 } else {
9015 InvokeRuntimeCallingConvention calling_convention;
9016
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009017 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009018 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
9019 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009020 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009021 locations->SetInAt(0, Location::RegisterPairLocation(
9022 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
9023 }
9024
9025 locations->SetOut(calling_convention.GetReturnLocation(result_type));
9026 }
9027}
9028
9029void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
9030 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009031 DataType::Type result_type = conversion->GetResultType();
9032 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009033 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009034 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009035
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009036 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
9037 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009038
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009039 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009040 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9041 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
9042 Register src = locations->InAt(0).AsRegister<Register>();
9043
Alexey Frunzea871ef12016-06-27 15:20:11 -07009044 if (dst_low != src) {
9045 __ Move(dst_low, src);
9046 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009047 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009048 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009049 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009050 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009051 ? locations->InAt(0).AsRegisterPairLow<Register>()
9052 : locations->InAt(0).AsRegister<Register>();
9053
9054 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009055 case DataType::Type::kUint8:
9056 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009057 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009058 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009059 if (has_sign_extension) {
9060 __ Seb(dst, src);
9061 } else {
9062 __ Sll(dst, src, 24);
9063 __ Sra(dst, dst, 24);
9064 }
9065 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01009066 case DataType::Type::kUint16:
9067 __ Andi(dst, src, 0xFFFF);
9068 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009069 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009070 if (has_sign_extension) {
9071 __ Seh(dst, src);
9072 } else {
9073 __ Sll(dst, src, 16);
9074 __ Sra(dst, dst, 16);
9075 }
9076 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009077 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07009078 if (dst != src) {
9079 __ Move(dst, src);
9080 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009081 break;
9082
9083 default:
9084 LOG(FATAL) << "Unexpected type conversion from " << input_type
9085 << " to " << result_type;
9086 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009087 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
9088 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009089 if (isR6) {
9090 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9091 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9092 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
9093 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
9094 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9095 __ Mtc1(src_low, FTMP);
9096 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009097 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009098 __ Cvtsl(dst, FTMP);
9099 } else {
9100 __ Cvtdl(dst, FTMP);
9101 }
9102 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009103 QuickEntrypointEnum entrypoint =
9104 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01009105 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009106 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009107 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
9108 } else {
9109 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
9110 }
9111 }
9112 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009113 Register src = locations->InAt(0).AsRegister<Register>();
9114 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9115 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009116 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009117 __ Cvtsw(dst, FTMP);
9118 } else {
9119 __ Cvtdw(dst, FTMP);
9120 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009121 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009122 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
9123 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009124
9125 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
9126 // value of the output type if the input is outside of the range after the truncation or
9127 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
9128 // results. This matches the desired float/double-to-int/long conversion exactly.
9129 //
9130 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
9131 // value when the input is either a NaN or is outside of the range of the output type
9132 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
9133 // the same result.
9134 //
9135 // The code takes care of the different behaviors by first comparing the input to the
9136 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
9137 // If the input is greater than or equal to the minimum, it procedes to the truncate
9138 // instruction, which will handle such an input the same way irrespective of NAN2008.
9139 // Otherwise the input is compared to itself to determine whether it is a NaN or not
9140 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009141 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009142 if (isR6) {
9143 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
9144 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
9145 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9146 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
9147 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009148
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009149 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009150 __ TruncLS(FTMP, src);
9151 } else {
9152 __ TruncLD(FTMP, src);
9153 }
9154 __ Mfc1(dst_low, FTMP);
9155 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009156 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009157 QuickEntrypointEnum entrypoint =
9158 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01009159 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009160 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009161 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
9162 } else {
9163 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
9164 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009165 }
9166 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009167 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
9168 Register dst = locations->Out().AsRegister<Register>();
9169 MipsLabel truncate;
9170 MipsLabel done;
9171
Lena Djokicf4e23a82017-05-09 15:43:45 +02009172 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009173 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009174 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
9175 __ LoadConst32(TMP, min_val);
9176 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009177 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02009178 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
9179 __ LoadConst32(TMP, High32Bits(min_val));
9180 __ Mtc1(ZERO, FTMP);
9181 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009182 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009183
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009184 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009185 __ ColeS(0, FTMP, src);
9186 } else {
9187 __ ColeD(0, FTMP, src);
9188 }
9189 __ Bc1t(0, &truncate);
9190
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009191 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009192 __ CeqS(0, src, src);
9193 } else {
9194 __ CeqD(0, src, src);
9195 }
9196 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
9197 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02009198
9199 __ B(&done);
9200
9201 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009202 }
9203
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009204 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08009205 __ TruncWS(FTMP, src);
9206 } else {
9207 __ TruncWD(FTMP, src);
9208 }
9209 __ Mfc1(dst, FTMP);
9210
Lena Djokicf4e23a82017-05-09 15:43:45 +02009211 if (!isR6) {
9212 __ Bind(&done);
9213 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009214 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009215 } else if (DataType::IsFloatingPointType(result_type) &&
9216 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009217 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
9218 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01009219 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009220 __ Cvtsd(dst, src);
9221 } else {
9222 __ Cvtds(dst, src);
9223 }
9224 } else {
9225 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
9226 << " to " << result_type;
9227 }
9228}
9229
9230void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9231 HandleShift(ushr);
9232}
9233
9234void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9235 HandleShift(ushr);
9236}
9237
9238void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9239 HandleBinaryOp(instruction);
9240}
9241
9242void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9243 HandleBinaryOp(instruction);
9244}
9245
9246void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9247 // Nothing to do, this should be removed during prepare for register allocator.
9248 LOG(FATAL) << "Unreachable";
9249}
9250
9251void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9252 // Nothing to do, this should be removed during prepare for register allocator.
9253 LOG(FATAL) << "Unreachable";
9254}
9255
9256void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009257 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009258}
9259
9260void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009261 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009262}
9263
9264void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009265 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009266}
9267
9268void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009269 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009270}
9271
9272void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009273 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009274}
9275
9276void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009277 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009278}
9279
9280void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009281 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009282}
9283
9284void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009285 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009286}
9287
9288void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009289 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009290}
9291
9292void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009293 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009294}
9295
9296void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009297 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009298}
9299
9300void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009301 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009302}
9303
9304void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009305 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009306}
9307
9308void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009309 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009310}
9311
9312void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009313 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009314}
9315
9316void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009317 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009318}
9319
9320void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009321 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009322}
9323
9324void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009325 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009326}
9327
9328void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009329 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009330}
9331
9332void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009333 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009334}
9335
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009336void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9337 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009338 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009339 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009340 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9341 uint32_t num_entries = switch_instr->GetNumEntries();
9342 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9343 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9344 // instruction to simulate PC-relative addressing when accessing the jump table.
9345 // NAL clobbers RA. Make sure RA is preserved.
9346 codegen_->ClobberRA();
9347 }
9348 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009349}
9350
Alexey Frunze96b66822016-09-10 02:32:44 -07009351void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9352 int32_t lower_bound,
9353 uint32_t num_entries,
9354 HBasicBlock* switch_block,
9355 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009356 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009357 Register temp_reg = TMP;
9358 __ Addiu32(temp_reg, value_reg, -lower_bound);
9359 // Jump to default if index is negative
9360 // Note: We don't check the case that index is positive while value < lower_bound, because in
9361 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9362 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9363
Alexey Frunze96b66822016-09-10 02:32:44 -07009364 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009365 // Jump to successors[0] if value == lower_bound.
9366 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9367 int32_t last_index = 0;
9368 for (; num_entries - last_index > 2; last_index += 2) {
9369 __ Addiu(temp_reg, temp_reg, -2);
9370 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9371 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9372 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9373 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9374 }
9375 if (num_entries - last_index == 2) {
9376 // The last missing case_value.
9377 __ Addiu(temp_reg, temp_reg, -1);
9378 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009379 }
9380
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009381 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009382 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009383 __ B(codegen_->GetLabelOf(default_block));
9384 }
9385}
9386
Alexey Frunze96b66822016-09-10 02:32:44 -07009387void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9388 Register constant_area,
9389 int32_t lower_bound,
9390 uint32_t num_entries,
9391 HBasicBlock* switch_block,
9392 HBasicBlock* default_block) {
9393 // Create a jump table.
9394 std::vector<MipsLabel*> labels(num_entries);
9395 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9396 for (uint32_t i = 0; i < num_entries; i++) {
9397 labels[i] = codegen_->GetLabelOf(successors[i]);
9398 }
9399 JumpTable* table = __ CreateJumpTable(std::move(labels));
9400
9401 // Is the value in range?
9402 __ Addiu32(TMP, value_reg, -lower_bound);
9403 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9404 __ Sltiu(AT, TMP, num_entries);
9405 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9406 } else {
9407 __ LoadConst32(AT, num_entries);
9408 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9409 }
9410
9411 // We are in the range of the table.
9412 // Load the target address from the jump table, indexing by the value.
9413 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009414 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009415 __ Lw(TMP, TMP, 0);
9416 // Compute the absolute target address by adding the table start address
9417 // (the table contains offsets to targets relative to its start).
9418 __ Addu(TMP, TMP, AT);
9419 // And jump.
9420 __ Jr(TMP);
9421 __ NopIfNoReordering();
9422}
9423
9424void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9425 int32_t lower_bound = switch_instr->GetStartValue();
9426 uint32_t num_entries = switch_instr->GetNumEntries();
9427 LocationSummary* locations = switch_instr->GetLocations();
9428 Register value_reg = locations->InAt(0).AsRegister<Register>();
9429 HBasicBlock* switch_block = switch_instr->GetBlock();
9430 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9431
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009432 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009433 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009434 //
9435 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9436 // to access the jump table and it is implemented by changing HPackedSwitch to
9437 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9438 // VisitMipsPackedSwitch()).
9439 //
9440 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9441 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9442 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009443 GenTableBasedPackedSwitch(value_reg,
9444 ZERO,
9445 lower_bound,
9446 num_entries,
9447 switch_block,
9448 default_block);
9449 } else {
9450 GenPackedSwitchWithCompares(value_reg,
9451 lower_bound,
9452 num_entries,
9453 switch_block,
9454 default_block);
9455 }
9456}
9457
9458void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9459 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009460 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009461 locations->SetInAt(0, Location::RequiresRegister());
9462 // Constant area pointer (HMipsComputeBaseMethodAddress).
9463 locations->SetInAt(1, Location::RequiresRegister());
9464}
9465
9466void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9467 int32_t lower_bound = switch_instr->GetStartValue();
9468 uint32_t num_entries = switch_instr->GetNumEntries();
9469 LocationSummary* locations = switch_instr->GetLocations();
9470 Register value_reg = locations->InAt(0).AsRegister<Register>();
9471 Register constant_area = locations->InAt(1).AsRegister<Register>();
9472 HBasicBlock* switch_block = switch_instr->GetBlock();
9473 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9474
9475 // This is an R2-only path. HPackedSwitch has been changed to
9476 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9477 // required to address the jump table relative to PC.
9478 GenTableBasedPackedSwitch(value_reg,
9479 constant_area,
9480 lower_bound,
9481 num_entries,
9482 switch_block,
9483 default_block);
9484}
9485
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009486void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9487 HMipsComputeBaseMethodAddress* insn) {
9488 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009489 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009490 locations->SetOut(Location::RequiresRegister());
9491}
9492
9493void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9494 HMipsComputeBaseMethodAddress* insn) {
9495 LocationSummary* locations = insn->GetLocations();
9496 Register reg = locations->Out().AsRegister<Register>();
9497
9498 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9499
9500 // Generate a dummy PC-relative call to obtain PC.
9501 __ Nal();
9502 // Grab the return address off RA.
9503 __ Move(reg, RA);
9504
9505 // Remember this offset (the obtained PC value) for later use with constant area.
9506 __ BindPcRelBaseLabel();
9507}
9508
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009509void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9510 // The trampoline uses the same calling convention as dex calling conventions,
9511 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9512 // the method_idx.
9513 HandleInvoke(invoke);
9514}
9515
9516void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9517 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9518}
9519
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009520void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9521 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009522 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009523 locations->SetInAt(0, Location::RequiresRegister());
9524 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009525}
9526
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009527void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9528 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009529 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009530 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009531 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009532 __ LoadFromOffset(kLoadWord,
9533 locations->Out().AsRegister<Register>(),
9534 locations->InAt(0).AsRegister<Register>(),
9535 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009536 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009537 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009538 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009539 __ LoadFromOffset(kLoadWord,
9540 locations->Out().AsRegister<Register>(),
9541 locations->InAt(0).AsRegister<Register>(),
9542 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009543 __ LoadFromOffset(kLoadWord,
9544 locations->Out().AsRegister<Register>(),
9545 locations->Out().AsRegister<Register>(),
9546 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009547 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009548}
9549
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009550void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9551 ATTRIBUTE_UNUSED) {
9552 LOG(FATAL) << "Unreachable";
9553}
9554
9555void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9556 ATTRIBUTE_UNUSED) {
9557 LOG(FATAL) << "Unreachable";
9558}
9559
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009560#undef __
9561#undef QUICK_ENTRY_POINT
9562
9563} // namespace mips
9564} // namespace art