blob: eb122b778187a36cbf5ed304af6b740519974d37 [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());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003705 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003706
3707 LocationSummary* locations = instruction->GetLocations();
3708 Location second = locations->InAt(1);
3709 DCHECK(second.IsConstant());
3710
3711 Register out = locations->Out().AsRegister<Register>();
3712 Register dividend = locations->InAt(0).AsRegister<Register>();
3713 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3714 DCHECK(imm == 1 || imm == -1);
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 }
3725}
3726
3727void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3728 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003729 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003730
3731 LocationSummary* locations = instruction->GetLocations();
3732 Location second = locations->InAt(1);
3733 DCHECK(second.IsConstant());
3734
3735 Register out = locations->Out().AsRegister<Register>();
3736 Register dividend = locations->InAt(0).AsRegister<Register>();
3737 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003738 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003739 int ctz_imm = CTZ(abs_imm);
3740
3741 if (instruction->IsDiv()) {
3742 if (ctz_imm == 1) {
3743 // Fast path for division by +/-2, which is very common.
3744 __ Srl(TMP, dividend, 31);
3745 } else {
3746 __ Sra(TMP, dividend, 31);
3747 __ Srl(TMP, TMP, 32 - ctz_imm);
3748 }
3749 __ Addu(out, dividend, TMP);
3750 __ Sra(out, out, ctz_imm);
3751 if (imm < 0) {
3752 __ Subu(out, ZERO, out);
3753 }
3754 } else {
3755 if (ctz_imm == 1) {
3756 // Fast path for modulo +/-2, which is very common.
3757 __ Sra(TMP, dividend, 31);
3758 __ Subu(out, dividend, TMP);
3759 __ Andi(out, out, 1);
3760 __ Addu(out, out, TMP);
3761 } else {
3762 __ Sra(TMP, dividend, 31);
3763 __ Srl(TMP, TMP, 32 - ctz_imm);
3764 __ Addu(out, dividend, TMP);
3765 if (IsUint<16>(abs_imm - 1)) {
3766 __ Andi(out, out, abs_imm - 1);
3767 } else {
Lena Djokica556e6b2017-12-13 12:09:42 +01003768 if (codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2()) {
3769 __ Ins(out, ZERO, ctz_imm, 32 - ctz_imm);
3770 } else {
3771 __ Sll(out, out, 32 - ctz_imm);
3772 __ Srl(out, out, 32 - ctz_imm);
3773 }
Alexey Frunze7e99e052015-11-24 19:28:01 -08003774 }
3775 __ Subu(out, out, TMP);
3776 }
3777 }
3778}
3779
3780void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3781 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003782 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003783
3784 LocationSummary* locations = instruction->GetLocations();
3785 Location second = locations->InAt(1);
3786 DCHECK(second.IsConstant());
3787
3788 Register out = locations->Out().AsRegister<Register>();
3789 Register dividend = locations->InAt(0).AsRegister<Register>();
3790 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3791
3792 int64_t magic;
3793 int shift;
3794 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3795
3796 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3797
3798 __ LoadConst32(TMP, magic);
3799 if (isR6) {
3800 __ MuhR6(TMP, dividend, TMP);
3801 } else {
3802 __ MultR2(dividend, TMP);
3803 __ Mfhi(TMP);
3804 }
3805 if (imm > 0 && magic < 0) {
3806 __ Addu(TMP, TMP, dividend);
3807 } else if (imm < 0 && magic > 0) {
3808 __ Subu(TMP, TMP, dividend);
3809 }
3810
3811 if (shift != 0) {
3812 __ Sra(TMP, TMP, shift);
3813 }
3814
3815 if (instruction->IsDiv()) {
3816 __ Sra(out, TMP, 31);
3817 __ Subu(out, TMP, out);
3818 } else {
3819 __ Sra(AT, TMP, 31);
3820 __ Subu(AT, TMP, AT);
3821 __ LoadConst32(TMP, imm);
3822 if (isR6) {
3823 __ MulR6(TMP, AT, TMP);
3824 } else {
3825 __ MulR2(TMP, AT, TMP);
3826 }
3827 __ Subu(out, dividend, TMP);
3828 }
3829}
3830
3831void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3832 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003833 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003834
3835 LocationSummary* locations = instruction->GetLocations();
3836 Register out = locations->Out().AsRegister<Register>();
3837 Location second = locations->InAt(1);
3838
3839 if (second.IsConstant()) {
3840 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3841 if (imm == 0) {
3842 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3843 } else if (imm == 1 || imm == -1) {
3844 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003845 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003846 DivRemByPowerOfTwo(instruction);
3847 } else {
3848 DCHECK(imm <= -2 || imm >= 2);
3849 GenerateDivRemWithAnyConstant(instruction);
3850 }
3851 } else {
3852 Register dividend = locations->InAt(0).AsRegister<Register>();
3853 Register divisor = second.AsRegister<Register>();
3854 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3855 if (instruction->IsDiv()) {
3856 if (isR6) {
3857 __ DivR6(out, dividend, divisor);
3858 } else {
3859 __ DivR2(out, dividend, divisor);
3860 }
3861 } else {
3862 if (isR6) {
3863 __ ModR6(out, dividend, divisor);
3864 } else {
3865 __ ModR2(out, dividend, divisor);
3866 }
3867 }
3868 }
3869}
3870
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003871void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003872 DataType::Type type = div->GetResultType();
3873 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003874 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003875 : LocationSummary::kNoCall;
3876
Vladimir Markoca6fff82017-10-03 14:49:14 +01003877 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(div, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003878
3879 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003880 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003881 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003882 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003883 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3884 break;
3885
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003886 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003887 InvokeRuntimeCallingConvention calling_convention;
3888 locations->SetInAt(0, Location::RegisterPairLocation(
3889 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3890 locations->SetInAt(1, Location::RegisterPairLocation(
3891 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3892 locations->SetOut(calling_convention.GetReturnLocation(type));
3893 break;
3894 }
3895
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003896 case DataType::Type::kFloat32:
3897 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003898 locations->SetInAt(0, Location::RequiresFpuRegister());
3899 locations->SetInAt(1, Location::RequiresFpuRegister());
3900 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3901 break;
3902
3903 default:
3904 LOG(FATAL) << "Unexpected div type " << type;
3905 }
3906}
3907
3908void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003909 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003910 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003911
3912 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003913 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003914 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003915 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003916 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003917 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003918 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3919 break;
3920 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003921 case DataType::Type::kFloat32:
3922 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003923 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3924 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3925 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003926 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003927 __ DivS(dst, lhs, rhs);
3928 } else {
3929 __ DivD(dst, lhs, rhs);
3930 }
3931 break;
3932 }
3933 default:
3934 LOG(FATAL) << "Unexpected div type " << type;
3935 }
3936}
3937
3938void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003939 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003940 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003941}
3942
3943void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01003944 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01003945 new (codegen_->GetScopedAllocator()) DivZeroCheckSlowPathMIPS(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003946 codegen_->AddSlowPath(slow_path);
3947 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003948 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003949
3950 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003951 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01003952 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003953 case DataType::Type::kInt8:
3954 case DataType::Type::kUint16:
3955 case DataType::Type::kInt16:
3956 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003957 if (value.IsConstant()) {
3958 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3959 __ B(slow_path->GetEntryLabel());
3960 } else {
3961 // A division by a non-null constant is valid. We don't need to perform
3962 // any check, so simply fall through.
3963 }
3964 } else {
3965 DCHECK(value.IsRegister()) << value;
3966 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3967 }
3968 break;
3969 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003970 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003971 if (value.IsConstant()) {
3972 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3973 __ B(slow_path->GetEntryLabel());
3974 } else {
3975 // A division by a non-null constant is valid. We don't need to perform
3976 // any check, so simply fall through.
3977 }
3978 } else {
3979 DCHECK(value.IsRegisterPair()) << value;
3980 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3981 __ Beqz(TMP, slow_path->GetEntryLabel());
3982 }
3983 break;
3984 }
3985 default:
3986 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3987 }
3988}
3989
3990void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3991 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01003992 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003993 locations->SetOut(Location::ConstantLocation(constant));
3994}
3995
3996void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3997 // Will be generated at use site.
3998}
3999
4000void LocationsBuilderMIPS::VisitExit(HExit* exit) {
4001 exit->SetLocations(nullptr);
4002}
4003
4004void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
4005}
4006
4007void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
4008 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01004009 new (GetGraph()->GetAllocator()) LocationSummary(constant, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004010 locations->SetOut(Location::ConstantLocation(constant));
4011}
4012
4013void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
4014 // Will be generated at use site.
4015}
4016
4017void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
4018 got->SetLocations(nullptr);
4019}
4020
4021void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
Aart Bika8b8e9b2018-01-09 11:01:02 -08004022 if (successor->IsExitBlock()) {
4023 DCHECK(got->GetPrevious()->AlwaysThrows());
4024 return; // no code needed
4025 }
4026
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004027 HBasicBlock* block = got->GetBlock();
4028 HInstruction* previous = got->GetPrevious();
4029 HLoopInformation* info = block->GetLoopInformation();
4030
4031 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
Goran Jakovljevicfeec1672018-02-08 10:20:14 +01004032 if (codegen_->GetCompilerOptions().CountHotnessInCompiledCode()) {
4033 __ Lw(AT, SP, kCurrentMethodStackOffset);
4034 __ Lhu(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4035 __ Addiu(TMP, TMP, 1);
4036 __ Sh(TMP, AT, ArtMethod::HotnessCountOffset().Int32Value());
4037 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02004038 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
4039 return;
4040 }
4041 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
4042 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
4043 }
4044 if (!codegen_->GoesToNextBlock(block, successor)) {
4045 __ B(codegen_->GetLabelOf(successor));
4046 }
4047}
4048
4049void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
4050 HandleGoto(got, got->GetSuccessor());
4051}
4052
4053void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4054 try_boundary->SetLocations(nullptr);
4055}
4056
4057void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
4058 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
4059 if (!successor->IsExitBlock()) {
4060 HandleGoto(try_boundary, successor);
4061 }
4062}
4063
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004064void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
4065 LocationSummary* locations) {
4066 Register dst = locations->Out().AsRegister<Register>();
4067 Register lhs = locations->InAt(0).AsRegister<Register>();
4068 Location rhs_location = locations->InAt(1);
4069 Register rhs_reg = ZERO;
4070 int64_t rhs_imm = 0;
4071 bool use_imm = rhs_location.IsConstant();
4072 if (use_imm) {
4073 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4074 } else {
4075 rhs_reg = rhs_location.AsRegister<Register>();
4076 }
4077
4078 switch (cond) {
4079 case kCondEQ:
4080 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07004081 if (use_imm && IsInt<16>(-rhs_imm)) {
4082 if (rhs_imm == 0) {
4083 if (cond == kCondEQ) {
4084 __ Sltiu(dst, lhs, 1);
4085 } else {
4086 __ Sltu(dst, ZERO, lhs);
4087 }
4088 } else {
4089 __ Addiu(dst, lhs, -rhs_imm);
4090 if (cond == kCondEQ) {
4091 __ Sltiu(dst, dst, 1);
4092 } else {
4093 __ Sltu(dst, ZERO, dst);
4094 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004095 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004096 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004097 if (use_imm && IsUint<16>(rhs_imm)) {
4098 __ Xori(dst, lhs, rhs_imm);
4099 } else {
4100 if (use_imm) {
4101 rhs_reg = TMP;
4102 __ LoadConst32(rhs_reg, rhs_imm);
4103 }
4104 __ Xor(dst, lhs, rhs_reg);
4105 }
4106 if (cond == kCondEQ) {
4107 __ Sltiu(dst, dst, 1);
4108 } else {
4109 __ Sltu(dst, ZERO, dst);
4110 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004111 }
4112 break;
4113
4114 case kCondLT:
4115 case kCondGE:
4116 if (use_imm && IsInt<16>(rhs_imm)) {
4117 __ Slti(dst, lhs, rhs_imm);
4118 } else {
4119 if (use_imm) {
4120 rhs_reg = TMP;
4121 __ LoadConst32(rhs_reg, rhs_imm);
4122 }
4123 __ Slt(dst, lhs, rhs_reg);
4124 }
4125 if (cond == kCondGE) {
4126 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4127 // only the slt instruction but no sge.
4128 __ Xori(dst, dst, 1);
4129 }
4130 break;
4131
4132 case kCondLE:
4133 case kCondGT:
4134 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4135 // Simulate lhs <= rhs via lhs < rhs + 1.
4136 __ Slti(dst, lhs, rhs_imm + 1);
4137 if (cond == kCondGT) {
4138 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4139 // only the slti instruction but no sgti.
4140 __ Xori(dst, dst, 1);
4141 }
4142 } else {
4143 if (use_imm) {
4144 rhs_reg = TMP;
4145 __ LoadConst32(rhs_reg, rhs_imm);
4146 }
4147 __ Slt(dst, rhs_reg, lhs);
4148 if (cond == kCondLE) {
4149 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4150 // only the slt instruction but no sle.
4151 __ Xori(dst, dst, 1);
4152 }
4153 }
4154 break;
4155
4156 case kCondB:
4157 case kCondAE:
4158 if (use_imm && IsInt<16>(rhs_imm)) {
4159 // Sltiu sign-extends its 16-bit immediate operand before
4160 // the comparison and thus lets us compare directly with
4161 // unsigned values in the ranges [0, 0x7fff] and
4162 // [0xffff8000, 0xffffffff].
4163 __ Sltiu(dst, lhs, rhs_imm);
4164 } else {
4165 if (use_imm) {
4166 rhs_reg = TMP;
4167 __ LoadConst32(rhs_reg, rhs_imm);
4168 }
4169 __ Sltu(dst, lhs, rhs_reg);
4170 }
4171 if (cond == kCondAE) {
4172 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4173 // only the sltu instruction but no sgeu.
4174 __ Xori(dst, dst, 1);
4175 }
4176 break;
4177
4178 case kCondBE:
4179 case kCondA:
4180 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4181 // Simulate lhs <= rhs via lhs < rhs + 1.
4182 // Note that this only works if rhs + 1 does not overflow
4183 // to 0, hence the check above.
4184 // Sltiu sign-extends its 16-bit immediate operand before
4185 // the comparison and thus lets us compare directly with
4186 // unsigned values in the ranges [0, 0x7fff] and
4187 // [0xffff8000, 0xffffffff].
4188 __ Sltiu(dst, lhs, rhs_imm + 1);
4189 if (cond == kCondA) {
4190 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4191 // only the sltiu instruction but no sgtiu.
4192 __ Xori(dst, dst, 1);
4193 }
4194 } else {
4195 if (use_imm) {
4196 rhs_reg = TMP;
4197 __ LoadConst32(rhs_reg, rhs_imm);
4198 }
4199 __ Sltu(dst, rhs_reg, lhs);
4200 if (cond == kCondBE) {
4201 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4202 // only the sltu instruction but no sleu.
4203 __ Xori(dst, dst, 1);
4204 }
4205 }
4206 break;
4207 }
4208}
4209
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004210bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4211 LocationSummary* input_locations,
4212 Register dst) {
4213 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4214 Location rhs_location = input_locations->InAt(1);
4215 Register rhs_reg = ZERO;
4216 int64_t rhs_imm = 0;
4217 bool use_imm = rhs_location.IsConstant();
4218 if (use_imm) {
4219 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4220 } else {
4221 rhs_reg = rhs_location.AsRegister<Register>();
4222 }
4223
4224 switch (cond) {
4225 case kCondEQ:
4226 case kCondNE:
4227 if (use_imm && IsInt<16>(-rhs_imm)) {
4228 __ Addiu(dst, lhs, -rhs_imm);
4229 } else if (use_imm && IsUint<16>(rhs_imm)) {
4230 __ Xori(dst, lhs, rhs_imm);
4231 } else {
4232 if (use_imm) {
4233 rhs_reg = TMP;
4234 __ LoadConst32(rhs_reg, rhs_imm);
4235 }
4236 __ Xor(dst, lhs, rhs_reg);
4237 }
4238 return (cond == kCondEQ);
4239
4240 case kCondLT:
4241 case kCondGE:
4242 if (use_imm && IsInt<16>(rhs_imm)) {
4243 __ Slti(dst, lhs, rhs_imm);
4244 } else {
4245 if (use_imm) {
4246 rhs_reg = TMP;
4247 __ LoadConst32(rhs_reg, rhs_imm);
4248 }
4249 __ Slt(dst, lhs, rhs_reg);
4250 }
4251 return (cond == kCondGE);
4252
4253 case kCondLE:
4254 case kCondGT:
4255 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4256 // Simulate lhs <= rhs via lhs < rhs + 1.
4257 __ Slti(dst, lhs, rhs_imm + 1);
4258 return (cond == kCondGT);
4259 } else {
4260 if (use_imm) {
4261 rhs_reg = TMP;
4262 __ LoadConst32(rhs_reg, rhs_imm);
4263 }
4264 __ Slt(dst, rhs_reg, lhs);
4265 return (cond == kCondLE);
4266 }
4267
4268 case kCondB:
4269 case kCondAE:
4270 if (use_imm && IsInt<16>(rhs_imm)) {
4271 // Sltiu sign-extends its 16-bit immediate operand before
4272 // the comparison and thus lets us compare directly with
4273 // unsigned values in the ranges [0, 0x7fff] and
4274 // [0xffff8000, 0xffffffff].
4275 __ Sltiu(dst, lhs, rhs_imm);
4276 } else {
4277 if (use_imm) {
4278 rhs_reg = TMP;
4279 __ LoadConst32(rhs_reg, rhs_imm);
4280 }
4281 __ Sltu(dst, lhs, rhs_reg);
4282 }
4283 return (cond == kCondAE);
4284
4285 case kCondBE:
4286 case kCondA:
4287 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4288 // Simulate lhs <= rhs via lhs < rhs + 1.
4289 // Note that this only works if rhs + 1 does not overflow
4290 // to 0, hence the check above.
4291 // Sltiu sign-extends its 16-bit immediate operand before
4292 // the comparison and thus lets us compare directly with
4293 // unsigned values in the ranges [0, 0x7fff] and
4294 // [0xffff8000, 0xffffffff].
4295 __ Sltiu(dst, lhs, rhs_imm + 1);
4296 return (cond == kCondA);
4297 } else {
4298 if (use_imm) {
4299 rhs_reg = TMP;
4300 __ LoadConst32(rhs_reg, rhs_imm);
4301 }
4302 __ Sltu(dst, rhs_reg, lhs);
4303 return (cond == kCondBE);
4304 }
4305 }
4306}
4307
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004308void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4309 LocationSummary* locations,
4310 MipsLabel* label) {
4311 Register lhs = locations->InAt(0).AsRegister<Register>();
4312 Location rhs_location = locations->InAt(1);
4313 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004314 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004315 bool use_imm = rhs_location.IsConstant();
4316 if (use_imm) {
4317 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4318 } else {
4319 rhs_reg = rhs_location.AsRegister<Register>();
4320 }
4321
4322 if (use_imm && rhs_imm == 0) {
4323 switch (cond) {
4324 case kCondEQ:
4325 case kCondBE: // <= 0 if zero
4326 __ Beqz(lhs, label);
4327 break;
4328 case kCondNE:
4329 case kCondA: // > 0 if non-zero
4330 __ Bnez(lhs, label);
4331 break;
4332 case kCondLT:
4333 __ Bltz(lhs, label);
4334 break;
4335 case kCondGE:
4336 __ Bgez(lhs, label);
4337 break;
4338 case kCondLE:
4339 __ Blez(lhs, label);
4340 break;
4341 case kCondGT:
4342 __ Bgtz(lhs, label);
4343 break;
4344 case kCondB: // always false
4345 break;
4346 case kCondAE: // always true
4347 __ B(label);
4348 break;
4349 }
4350 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004351 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4352 if (isR6 || !use_imm) {
4353 if (use_imm) {
4354 rhs_reg = TMP;
4355 __ LoadConst32(rhs_reg, rhs_imm);
4356 }
4357 switch (cond) {
4358 case kCondEQ:
4359 __ Beq(lhs, rhs_reg, label);
4360 break;
4361 case kCondNE:
4362 __ Bne(lhs, rhs_reg, label);
4363 break;
4364 case kCondLT:
4365 __ Blt(lhs, rhs_reg, label);
4366 break;
4367 case kCondGE:
4368 __ Bge(lhs, rhs_reg, label);
4369 break;
4370 case kCondLE:
4371 __ Bge(rhs_reg, lhs, label);
4372 break;
4373 case kCondGT:
4374 __ Blt(rhs_reg, lhs, label);
4375 break;
4376 case kCondB:
4377 __ Bltu(lhs, rhs_reg, label);
4378 break;
4379 case kCondAE:
4380 __ Bgeu(lhs, rhs_reg, label);
4381 break;
4382 case kCondBE:
4383 __ Bgeu(rhs_reg, lhs, label);
4384 break;
4385 case kCondA:
4386 __ Bltu(rhs_reg, lhs, label);
4387 break;
4388 }
4389 } else {
4390 // Special cases for more efficient comparison with constants on R2.
4391 switch (cond) {
4392 case kCondEQ:
4393 __ LoadConst32(TMP, rhs_imm);
4394 __ Beq(lhs, TMP, label);
4395 break;
4396 case kCondNE:
4397 __ LoadConst32(TMP, rhs_imm);
4398 __ Bne(lhs, TMP, label);
4399 break;
4400 case kCondLT:
4401 if (IsInt<16>(rhs_imm)) {
4402 __ Slti(TMP, lhs, rhs_imm);
4403 __ Bnez(TMP, label);
4404 } else {
4405 __ LoadConst32(TMP, rhs_imm);
4406 __ Blt(lhs, TMP, label);
4407 }
4408 break;
4409 case kCondGE:
4410 if (IsInt<16>(rhs_imm)) {
4411 __ Slti(TMP, lhs, rhs_imm);
4412 __ Beqz(TMP, label);
4413 } else {
4414 __ LoadConst32(TMP, rhs_imm);
4415 __ Bge(lhs, TMP, label);
4416 }
4417 break;
4418 case kCondLE:
4419 if (IsInt<16>(rhs_imm + 1)) {
4420 // Simulate lhs <= rhs via lhs < rhs + 1.
4421 __ Slti(TMP, lhs, rhs_imm + 1);
4422 __ Bnez(TMP, label);
4423 } else {
4424 __ LoadConst32(TMP, rhs_imm);
4425 __ Bge(TMP, lhs, label);
4426 }
4427 break;
4428 case kCondGT:
4429 if (IsInt<16>(rhs_imm + 1)) {
4430 // Simulate lhs > rhs via !(lhs < rhs + 1).
4431 __ Slti(TMP, lhs, rhs_imm + 1);
4432 __ Beqz(TMP, label);
4433 } else {
4434 __ LoadConst32(TMP, rhs_imm);
4435 __ Blt(TMP, lhs, label);
4436 }
4437 break;
4438 case kCondB:
4439 if (IsInt<16>(rhs_imm)) {
4440 __ Sltiu(TMP, lhs, rhs_imm);
4441 __ Bnez(TMP, label);
4442 } else {
4443 __ LoadConst32(TMP, rhs_imm);
4444 __ Bltu(lhs, TMP, label);
4445 }
4446 break;
4447 case kCondAE:
4448 if (IsInt<16>(rhs_imm)) {
4449 __ Sltiu(TMP, lhs, rhs_imm);
4450 __ Beqz(TMP, label);
4451 } else {
4452 __ LoadConst32(TMP, rhs_imm);
4453 __ Bgeu(lhs, TMP, label);
4454 }
4455 break;
4456 case kCondBE:
4457 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4458 // Simulate lhs <= rhs via lhs < rhs + 1.
4459 // Note that this only works if rhs + 1 does not overflow
4460 // to 0, hence the check above.
4461 __ Sltiu(TMP, lhs, rhs_imm + 1);
4462 __ Bnez(TMP, label);
4463 } else {
4464 __ LoadConst32(TMP, rhs_imm);
4465 __ Bgeu(TMP, lhs, label);
4466 }
4467 break;
4468 case kCondA:
4469 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4470 // Simulate lhs > rhs via !(lhs < rhs + 1).
4471 // Note that this only works if rhs + 1 does not overflow
4472 // to 0, hence the check above.
4473 __ Sltiu(TMP, lhs, rhs_imm + 1);
4474 __ Beqz(TMP, label);
4475 } else {
4476 __ LoadConst32(TMP, rhs_imm);
4477 __ Bltu(TMP, lhs, label);
4478 }
4479 break;
4480 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004481 }
4482 }
4483}
4484
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004485void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4486 LocationSummary* locations) {
4487 Register dst = locations->Out().AsRegister<Register>();
4488 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4489 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4490 Location rhs_location = locations->InAt(1);
4491 Register rhs_high = ZERO;
4492 Register rhs_low = ZERO;
4493 int64_t imm = 0;
4494 uint32_t imm_high = 0;
4495 uint32_t imm_low = 0;
4496 bool use_imm = rhs_location.IsConstant();
4497 if (use_imm) {
4498 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4499 imm_high = High32Bits(imm);
4500 imm_low = Low32Bits(imm);
4501 } else {
4502 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4503 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4504 }
4505 if (use_imm && imm == 0) {
4506 switch (cond) {
4507 case kCondEQ:
4508 case kCondBE: // <= 0 if zero
4509 __ Or(dst, lhs_high, lhs_low);
4510 __ Sltiu(dst, dst, 1);
4511 break;
4512 case kCondNE:
4513 case kCondA: // > 0 if non-zero
4514 __ Or(dst, lhs_high, lhs_low);
4515 __ Sltu(dst, ZERO, dst);
4516 break;
4517 case kCondLT:
4518 __ Slt(dst, lhs_high, ZERO);
4519 break;
4520 case kCondGE:
4521 __ Slt(dst, lhs_high, ZERO);
4522 __ Xori(dst, dst, 1);
4523 break;
4524 case kCondLE:
4525 __ Or(TMP, lhs_high, lhs_low);
4526 __ Sra(AT, lhs_high, 31);
4527 __ Sltu(dst, AT, TMP);
4528 __ Xori(dst, dst, 1);
4529 break;
4530 case kCondGT:
4531 __ Or(TMP, lhs_high, lhs_low);
4532 __ Sra(AT, lhs_high, 31);
4533 __ Sltu(dst, AT, TMP);
4534 break;
4535 case kCondB: // always false
4536 __ Andi(dst, dst, 0);
4537 break;
4538 case kCondAE: // always true
4539 __ Ori(dst, ZERO, 1);
4540 break;
4541 }
4542 } else if (use_imm) {
4543 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4544 switch (cond) {
4545 case kCondEQ:
4546 __ LoadConst32(TMP, imm_high);
4547 __ Xor(TMP, TMP, lhs_high);
4548 __ LoadConst32(AT, imm_low);
4549 __ Xor(AT, AT, lhs_low);
4550 __ Or(dst, TMP, AT);
4551 __ Sltiu(dst, dst, 1);
4552 break;
4553 case kCondNE:
4554 __ LoadConst32(TMP, imm_high);
4555 __ Xor(TMP, TMP, lhs_high);
4556 __ LoadConst32(AT, imm_low);
4557 __ Xor(AT, AT, lhs_low);
4558 __ Or(dst, TMP, AT);
4559 __ Sltu(dst, ZERO, dst);
4560 break;
4561 case kCondLT:
4562 case kCondGE:
4563 if (dst == lhs_low) {
4564 __ LoadConst32(TMP, imm_low);
4565 __ Sltu(dst, lhs_low, TMP);
4566 }
4567 __ LoadConst32(TMP, imm_high);
4568 __ Slt(AT, lhs_high, TMP);
4569 __ Slt(TMP, TMP, lhs_high);
4570 if (dst != lhs_low) {
4571 __ LoadConst32(dst, imm_low);
4572 __ Sltu(dst, lhs_low, dst);
4573 }
4574 __ Slt(dst, TMP, dst);
4575 __ Or(dst, dst, AT);
4576 if (cond == kCondGE) {
4577 __ Xori(dst, dst, 1);
4578 }
4579 break;
4580 case kCondGT:
4581 case kCondLE:
4582 if (dst == lhs_low) {
4583 __ LoadConst32(TMP, imm_low);
4584 __ Sltu(dst, TMP, lhs_low);
4585 }
4586 __ LoadConst32(TMP, imm_high);
4587 __ Slt(AT, TMP, lhs_high);
4588 __ Slt(TMP, lhs_high, TMP);
4589 if (dst != lhs_low) {
4590 __ LoadConst32(dst, imm_low);
4591 __ Sltu(dst, dst, lhs_low);
4592 }
4593 __ Slt(dst, TMP, dst);
4594 __ Or(dst, dst, AT);
4595 if (cond == kCondLE) {
4596 __ Xori(dst, dst, 1);
4597 }
4598 break;
4599 case kCondB:
4600 case kCondAE:
4601 if (dst == lhs_low) {
4602 __ LoadConst32(TMP, imm_low);
4603 __ Sltu(dst, lhs_low, TMP);
4604 }
4605 __ LoadConst32(TMP, imm_high);
4606 __ Sltu(AT, lhs_high, TMP);
4607 __ Sltu(TMP, TMP, lhs_high);
4608 if (dst != lhs_low) {
4609 __ LoadConst32(dst, imm_low);
4610 __ Sltu(dst, lhs_low, dst);
4611 }
4612 __ Slt(dst, TMP, dst);
4613 __ Or(dst, dst, AT);
4614 if (cond == kCondAE) {
4615 __ Xori(dst, dst, 1);
4616 }
4617 break;
4618 case kCondA:
4619 case kCondBE:
4620 if (dst == lhs_low) {
4621 __ LoadConst32(TMP, imm_low);
4622 __ Sltu(dst, TMP, lhs_low);
4623 }
4624 __ LoadConst32(TMP, imm_high);
4625 __ Sltu(AT, TMP, lhs_high);
4626 __ Sltu(TMP, lhs_high, TMP);
4627 if (dst != lhs_low) {
4628 __ LoadConst32(dst, imm_low);
4629 __ Sltu(dst, dst, lhs_low);
4630 }
4631 __ Slt(dst, TMP, dst);
4632 __ Or(dst, dst, AT);
4633 if (cond == kCondBE) {
4634 __ Xori(dst, dst, 1);
4635 }
4636 break;
4637 }
4638 } else {
4639 switch (cond) {
4640 case kCondEQ:
4641 __ Xor(TMP, lhs_high, rhs_high);
4642 __ Xor(AT, lhs_low, rhs_low);
4643 __ Or(dst, TMP, AT);
4644 __ Sltiu(dst, dst, 1);
4645 break;
4646 case kCondNE:
4647 __ Xor(TMP, lhs_high, rhs_high);
4648 __ Xor(AT, lhs_low, rhs_low);
4649 __ Or(dst, TMP, AT);
4650 __ Sltu(dst, ZERO, dst);
4651 break;
4652 case kCondLT:
4653 case kCondGE:
4654 __ Slt(TMP, rhs_high, lhs_high);
4655 __ Sltu(AT, lhs_low, rhs_low);
4656 __ Slt(TMP, TMP, AT);
4657 __ Slt(AT, lhs_high, rhs_high);
4658 __ Or(dst, AT, TMP);
4659 if (cond == kCondGE) {
4660 __ Xori(dst, dst, 1);
4661 }
4662 break;
4663 case kCondGT:
4664 case kCondLE:
4665 __ Slt(TMP, lhs_high, rhs_high);
4666 __ Sltu(AT, rhs_low, lhs_low);
4667 __ Slt(TMP, TMP, AT);
4668 __ Slt(AT, rhs_high, lhs_high);
4669 __ Or(dst, AT, TMP);
4670 if (cond == kCondLE) {
4671 __ Xori(dst, dst, 1);
4672 }
4673 break;
4674 case kCondB:
4675 case kCondAE:
4676 __ Sltu(TMP, rhs_high, lhs_high);
4677 __ Sltu(AT, lhs_low, rhs_low);
4678 __ Slt(TMP, TMP, AT);
4679 __ Sltu(AT, lhs_high, rhs_high);
4680 __ Or(dst, AT, TMP);
4681 if (cond == kCondAE) {
4682 __ Xori(dst, dst, 1);
4683 }
4684 break;
4685 case kCondA:
4686 case kCondBE:
4687 __ Sltu(TMP, lhs_high, rhs_high);
4688 __ Sltu(AT, rhs_low, lhs_low);
4689 __ Slt(TMP, TMP, AT);
4690 __ Sltu(AT, rhs_high, lhs_high);
4691 __ Or(dst, AT, TMP);
4692 if (cond == kCondBE) {
4693 __ Xori(dst, dst, 1);
4694 }
4695 break;
4696 }
4697 }
4698}
4699
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004700void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4701 LocationSummary* locations,
4702 MipsLabel* label) {
4703 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4704 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4705 Location rhs_location = locations->InAt(1);
4706 Register rhs_high = ZERO;
4707 Register rhs_low = ZERO;
4708 int64_t imm = 0;
4709 uint32_t imm_high = 0;
4710 uint32_t imm_low = 0;
4711 bool use_imm = rhs_location.IsConstant();
4712 if (use_imm) {
4713 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4714 imm_high = High32Bits(imm);
4715 imm_low = Low32Bits(imm);
4716 } else {
4717 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4718 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4719 }
4720
4721 if (use_imm && imm == 0) {
4722 switch (cond) {
4723 case kCondEQ:
4724 case kCondBE: // <= 0 if zero
4725 __ Or(TMP, lhs_high, lhs_low);
4726 __ Beqz(TMP, label);
4727 break;
4728 case kCondNE:
4729 case kCondA: // > 0 if non-zero
4730 __ Or(TMP, lhs_high, lhs_low);
4731 __ Bnez(TMP, label);
4732 break;
4733 case kCondLT:
4734 __ Bltz(lhs_high, label);
4735 break;
4736 case kCondGE:
4737 __ Bgez(lhs_high, label);
4738 break;
4739 case kCondLE:
4740 __ Or(TMP, lhs_high, lhs_low);
4741 __ Sra(AT, lhs_high, 31);
4742 __ Bgeu(AT, TMP, label);
4743 break;
4744 case kCondGT:
4745 __ Or(TMP, lhs_high, lhs_low);
4746 __ Sra(AT, lhs_high, 31);
4747 __ Bltu(AT, TMP, label);
4748 break;
4749 case kCondB: // always false
4750 break;
4751 case kCondAE: // always true
4752 __ B(label);
4753 break;
4754 }
4755 } else if (use_imm) {
4756 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4757 switch (cond) {
4758 case kCondEQ:
4759 __ LoadConst32(TMP, imm_high);
4760 __ Xor(TMP, TMP, lhs_high);
4761 __ LoadConst32(AT, imm_low);
4762 __ Xor(AT, AT, lhs_low);
4763 __ Or(TMP, TMP, AT);
4764 __ Beqz(TMP, label);
4765 break;
4766 case kCondNE:
4767 __ LoadConst32(TMP, imm_high);
4768 __ Xor(TMP, TMP, lhs_high);
4769 __ LoadConst32(AT, imm_low);
4770 __ Xor(AT, AT, lhs_low);
4771 __ Or(TMP, TMP, AT);
4772 __ Bnez(TMP, label);
4773 break;
4774 case kCondLT:
4775 __ LoadConst32(TMP, imm_high);
4776 __ Blt(lhs_high, TMP, label);
4777 __ Slt(TMP, TMP, lhs_high);
4778 __ LoadConst32(AT, imm_low);
4779 __ Sltu(AT, lhs_low, AT);
4780 __ Blt(TMP, AT, label);
4781 break;
4782 case kCondGE:
4783 __ LoadConst32(TMP, imm_high);
4784 __ Blt(TMP, lhs_high, label);
4785 __ Slt(TMP, lhs_high, TMP);
4786 __ LoadConst32(AT, imm_low);
4787 __ Sltu(AT, lhs_low, AT);
4788 __ Or(TMP, TMP, AT);
4789 __ Beqz(TMP, label);
4790 break;
4791 case kCondLE:
4792 __ LoadConst32(TMP, imm_high);
4793 __ Blt(lhs_high, TMP, label);
4794 __ Slt(TMP, TMP, lhs_high);
4795 __ LoadConst32(AT, imm_low);
4796 __ Sltu(AT, AT, lhs_low);
4797 __ Or(TMP, TMP, AT);
4798 __ Beqz(TMP, label);
4799 break;
4800 case kCondGT:
4801 __ LoadConst32(TMP, imm_high);
4802 __ Blt(TMP, lhs_high, label);
4803 __ Slt(TMP, lhs_high, TMP);
4804 __ LoadConst32(AT, imm_low);
4805 __ Sltu(AT, AT, lhs_low);
4806 __ Blt(TMP, AT, label);
4807 break;
4808 case kCondB:
4809 __ LoadConst32(TMP, imm_high);
4810 __ Bltu(lhs_high, TMP, label);
4811 __ Sltu(TMP, TMP, lhs_high);
4812 __ LoadConst32(AT, imm_low);
4813 __ Sltu(AT, lhs_low, AT);
4814 __ Blt(TMP, AT, label);
4815 break;
4816 case kCondAE:
4817 __ LoadConst32(TMP, imm_high);
4818 __ Bltu(TMP, lhs_high, label);
4819 __ Sltu(TMP, lhs_high, TMP);
4820 __ LoadConst32(AT, imm_low);
4821 __ Sltu(AT, lhs_low, AT);
4822 __ Or(TMP, TMP, AT);
4823 __ Beqz(TMP, label);
4824 break;
4825 case kCondBE:
4826 __ LoadConst32(TMP, imm_high);
4827 __ Bltu(lhs_high, TMP, label);
4828 __ Sltu(TMP, TMP, lhs_high);
4829 __ LoadConst32(AT, imm_low);
4830 __ Sltu(AT, AT, lhs_low);
4831 __ Or(TMP, TMP, AT);
4832 __ Beqz(TMP, label);
4833 break;
4834 case kCondA:
4835 __ LoadConst32(TMP, imm_high);
4836 __ Bltu(TMP, lhs_high, label);
4837 __ Sltu(TMP, lhs_high, TMP);
4838 __ LoadConst32(AT, imm_low);
4839 __ Sltu(AT, AT, lhs_low);
4840 __ Blt(TMP, AT, label);
4841 break;
4842 }
4843 } else {
4844 switch (cond) {
4845 case kCondEQ:
4846 __ Xor(TMP, lhs_high, rhs_high);
4847 __ Xor(AT, lhs_low, rhs_low);
4848 __ Or(TMP, TMP, AT);
4849 __ Beqz(TMP, label);
4850 break;
4851 case kCondNE:
4852 __ Xor(TMP, lhs_high, rhs_high);
4853 __ Xor(AT, lhs_low, rhs_low);
4854 __ Or(TMP, TMP, AT);
4855 __ Bnez(TMP, label);
4856 break;
4857 case kCondLT:
4858 __ Blt(lhs_high, rhs_high, label);
4859 __ Slt(TMP, rhs_high, lhs_high);
4860 __ Sltu(AT, lhs_low, rhs_low);
4861 __ Blt(TMP, AT, label);
4862 break;
4863 case kCondGE:
4864 __ Blt(rhs_high, lhs_high, label);
4865 __ Slt(TMP, lhs_high, rhs_high);
4866 __ Sltu(AT, lhs_low, rhs_low);
4867 __ Or(TMP, TMP, AT);
4868 __ Beqz(TMP, label);
4869 break;
4870 case kCondLE:
4871 __ Blt(lhs_high, rhs_high, label);
4872 __ Slt(TMP, rhs_high, lhs_high);
4873 __ Sltu(AT, rhs_low, lhs_low);
4874 __ Or(TMP, TMP, AT);
4875 __ Beqz(TMP, label);
4876 break;
4877 case kCondGT:
4878 __ Blt(rhs_high, lhs_high, label);
4879 __ Slt(TMP, lhs_high, rhs_high);
4880 __ Sltu(AT, rhs_low, lhs_low);
4881 __ Blt(TMP, AT, label);
4882 break;
4883 case kCondB:
4884 __ Bltu(lhs_high, rhs_high, label);
4885 __ Sltu(TMP, rhs_high, lhs_high);
4886 __ Sltu(AT, lhs_low, rhs_low);
4887 __ Blt(TMP, AT, label);
4888 break;
4889 case kCondAE:
4890 __ Bltu(rhs_high, lhs_high, label);
4891 __ Sltu(TMP, lhs_high, rhs_high);
4892 __ Sltu(AT, lhs_low, rhs_low);
4893 __ Or(TMP, TMP, AT);
4894 __ Beqz(TMP, label);
4895 break;
4896 case kCondBE:
4897 __ Bltu(lhs_high, rhs_high, label);
4898 __ Sltu(TMP, rhs_high, lhs_high);
4899 __ Sltu(AT, rhs_low, lhs_low);
4900 __ Or(TMP, TMP, AT);
4901 __ Beqz(TMP, label);
4902 break;
4903 case kCondA:
4904 __ Bltu(rhs_high, lhs_high, label);
4905 __ Sltu(TMP, lhs_high, rhs_high);
4906 __ Sltu(AT, rhs_low, lhs_low);
4907 __ Blt(TMP, AT, label);
4908 break;
4909 }
4910 }
4911}
4912
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004913void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4914 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004915 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004916 LocationSummary* locations) {
4917 Register dst = locations->Out().AsRegister<Register>();
4918 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4919 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4920 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004921 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004922 if (isR6) {
4923 switch (cond) {
4924 case kCondEQ:
4925 __ CmpEqS(FTMP, lhs, rhs);
4926 __ Mfc1(dst, FTMP);
4927 __ Andi(dst, dst, 1);
4928 break;
4929 case kCondNE:
4930 __ CmpEqS(FTMP, lhs, rhs);
4931 __ Mfc1(dst, FTMP);
4932 __ Addiu(dst, dst, 1);
4933 break;
4934 case kCondLT:
4935 if (gt_bias) {
4936 __ CmpLtS(FTMP, lhs, rhs);
4937 } else {
4938 __ CmpUltS(FTMP, lhs, rhs);
4939 }
4940 __ Mfc1(dst, FTMP);
4941 __ Andi(dst, dst, 1);
4942 break;
4943 case kCondLE:
4944 if (gt_bias) {
4945 __ CmpLeS(FTMP, lhs, rhs);
4946 } else {
4947 __ CmpUleS(FTMP, lhs, rhs);
4948 }
4949 __ Mfc1(dst, FTMP);
4950 __ Andi(dst, dst, 1);
4951 break;
4952 case kCondGT:
4953 if (gt_bias) {
4954 __ CmpUltS(FTMP, rhs, lhs);
4955 } else {
4956 __ CmpLtS(FTMP, rhs, lhs);
4957 }
4958 __ Mfc1(dst, FTMP);
4959 __ Andi(dst, dst, 1);
4960 break;
4961 case kCondGE:
4962 if (gt_bias) {
4963 __ CmpUleS(FTMP, rhs, lhs);
4964 } else {
4965 __ CmpLeS(FTMP, rhs, lhs);
4966 }
4967 __ Mfc1(dst, FTMP);
4968 __ Andi(dst, dst, 1);
4969 break;
4970 default:
4971 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4972 UNREACHABLE();
4973 }
4974 } else {
4975 switch (cond) {
4976 case kCondEQ:
4977 __ CeqS(0, lhs, rhs);
4978 __ LoadConst32(dst, 1);
4979 __ Movf(dst, ZERO, 0);
4980 break;
4981 case kCondNE:
4982 __ CeqS(0, lhs, rhs);
4983 __ LoadConst32(dst, 1);
4984 __ Movt(dst, ZERO, 0);
4985 break;
4986 case kCondLT:
4987 if (gt_bias) {
4988 __ ColtS(0, lhs, rhs);
4989 } else {
4990 __ CultS(0, lhs, rhs);
4991 }
4992 __ LoadConst32(dst, 1);
4993 __ Movf(dst, ZERO, 0);
4994 break;
4995 case kCondLE:
4996 if (gt_bias) {
4997 __ ColeS(0, lhs, rhs);
4998 } else {
4999 __ CuleS(0, lhs, rhs);
5000 }
5001 __ LoadConst32(dst, 1);
5002 __ Movf(dst, ZERO, 0);
5003 break;
5004 case kCondGT:
5005 if (gt_bias) {
5006 __ CultS(0, rhs, lhs);
5007 } else {
5008 __ ColtS(0, rhs, lhs);
5009 }
5010 __ LoadConst32(dst, 1);
5011 __ Movf(dst, ZERO, 0);
5012 break;
5013 case kCondGE:
5014 if (gt_bias) {
5015 __ CuleS(0, rhs, lhs);
5016 } else {
5017 __ ColeS(0, rhs, lhs);
5018 }
5019 __ LoadConst32(dst, 1);
5020 __ Movf(dst, ZERO, 0);
5021 break;
5022 default:
5023 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5024 UNREACHABLE();
5025 }
5026 }
5027 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005028 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07005029 if (isR6) {
5030 switch (cond) {
5031 case kCondEQ:
5032 __ CmpEqD(FTMP, lhs, rhs);
5033 __ Mfc1(dst, FTMP);
5034 __ Andi(dst, dst, 1);
5035 break;
5036 case kCondNE:
5037 __ CmpEqD(FTMP, lhs, rhs);
5038 __ Mfc1(dst, FTMP);
5039 __ Addiu(dst, dst, 1);
5040 break;
5041 case kCondLT:
5042 if (gt_bias) {
5043 __ CmpLtD(FTMP, lhs, rhs);
5044 } else {
5045 __ CmpUltD(FTMP, lhs, rhs);
5046 }
5047 __ Mfc1(dst, FTMP);
5048 __ Andi(dst, dst, 1);
5049 break;
5050 case kCondLE:
5051 if (gt_bias) {
5052 __ CmpLeD(FTMP, lhs, rhs);
5053 } else {
5054 __ CmpUleD(FTMP, lhs, rhs);
5055 }
5056 __ Mfc1(dst, FTMP);
5057 __ Andi(dst, dst, 1);
5058 break;
5059 case kCondGT:
5060 if (gt_bias) {
5061 __ CmpUltD(FTMP, rhs, lhs);
5062 } else {
5063 __ CmpLtD(FTMP, rhs, lhs);
5064 }
5065 __ Mfc1(dst, FTMP);
5066 __ Andi(dst, dst, 1);
5067 break;
5068 case kCondGE:
5069 if (gt_bias) {
5070 __ CmpUleD(FTMP, rhs, lhs);
5071 } else {
5072 __ CmpLeD(FTMP, rhs, lhs);
5073 }
5074 __ Mfc1(dst, FTMP);
5075 __ Andi(dst, dst, 1);
5076 break;
5077 default:
5078 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5079 UNREACHABLE();
5080 }
5081 } else {
5082 switch (cond) {
5083 case kCondEQ:
5084 __ CeqD(0, lhs, rhs);
5085 __ LoadConst32(dst, 1);
5086 __ Movf(dst, ZERO, 0);
5087 break;
5088 case kCondNE:
5089 __ CeqD(0, lhs, rhs);
5090 __ LoadConst32(dst, 1);
5091 __ Movt(dst, ZERO, 0);
5092 break;
5093 case kCondLT:
5094 if (gt_bias) {
5095 __ ColtD(0, lhs, rhs);
5096 } else {
5097 __ CultD(0, lhs, rhs);
5098 }
5099 __ LoadConst32(dst, 1);
5100 __ Movf(dst, ZERO, 0);
5101 break;
5102 case kCondLE:
5103 if (gt_bias) {
5104 __ ColeD(0, lhs, rhs);
5105 } else {
5106 __ CuleD(0, lhs, rhs);
5107 }
5108 __ LoadConst32(dst, 1);
5109 __ Movf(dst, ZERO, 0);
5110 break;
5111 case kCondGT:
5112 if (gt_bias) {
5113 __ CultD(0, rhs, lhs);
5114 } else {
5115 __ ColtD(0, rhs, lhs);
5116 }
5117 __ LoadConst32(dst, 1);
5118 __ Movf(dst, ZERO, 0);
5119 break;
5120 case kCondGE:
5121 if (gt_bias) {
5122 __ CuleD(0, rhs, lhs);
5123 } else {
5124 __ ColeD(0, rhs, lhs);
5125 }
5126 __ LoadConst32(dst, 1);
5127 __ Movf(dst, ZERO, 0);
5128 break;
5129 default:
5130 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5131 UNREACHABLE();
5132 }
5133 }
5134 }
5135}
5136
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005137bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5138 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005139 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005140 LocationSummary* input_locations,
5141 int cc) {
5142 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5143 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5144 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005145 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005146 switch (cond) {
5147 case kCondEQ:
5148 __ CeqS(cc, lhs, rhs);
5149 return false;
5150 case kCondNE:
5151 __ CeqS(cc, lhs, rhs);
5152 return true;
5153 case kCondLT:
5154 if (gt_bias) {
5155 __ ColtS(cc, lhs, rhs);
5156 } else {
5157 __ CultS(cc, lhs, rhs);
5158 }
5159 return false;
5160 case kCondLE:
5161 if (gt_bias) {
5162 __ ColeS(cc, lhs, rhs);
5163 } else {
5164 __ CuleS(cc, lhs, rhs);
5165 }
5166 return false;
5167 case kCondGT:
5168 if (gt_bias) {
5169 __ CultS(cc, rhs, lhs);
5170 } else {
5171 __ ColtS(cc, rhs, lhs);
5172 }
5173 return false;
5174 case kCondGE:
5175 if (gt_bias) {
5176 __ CuleS(cc, rhs, lhs);
5177 } else {
5178 __ ColeS(cc, rhs, lhs);
5179 }
5180 return false;
5181 default:
5182 LOG(FATAL) << "Unexpected non-floating-point condition";
5183 UNREACHABLE();
5184 }
5185 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005186 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005187 switch (cond) {
5188 case kCondEQ:
5189 __ CeqD(cc, lhs, rhs);
5190 return false;
5191 case kCondNE:
5192 __ CeqD(cc, lhs, rhs);
5193 return true;
5194 case kCondLT:
5195 if (gt_bias) {
5196 __ ColtD(cc, lhs, rhs);
5197 } else {
5198 __ CultD(cc, lhs, rhs);
5199 }
5200 return false;
5201 case kCondLE:
5202 if (gt_bias) {
5203 __ ColeD(cc, lhs, rhs);
5204 } else {
5205 __ CuleD(cc, lhs, rhs);
5206 }
5207 return false;
5208 case kCondGT:
5209 if (gt_bias) {
5210 __ CultD(cc, rhs, lhs);
5211 } else {
5212 __ ColtD(cc, rhs, lhs);
5213 }
5214 return false;
5215 case kCondGE:
5216 if (gt_bias) {
5217 __ CuleD(cc, rhs, lhs);
5218 } else {
5219 __ ColeD(cc, rhs, lhs);
5220 }
5221 return false;
5222 default:
5223 LOG(FATAL) << "Unexpected non-floating-point condition";
5224 UNREACHABLE();
5225 }
5226 }
5227}
5228
5229bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5230 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005231 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005232 LocationSummary* input_locations,
5233 FRegister dst) {
5234 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5235 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5236 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005237 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005238 switch (cond) {
5239 case kCondEQ:
5240 __ CmpEqS(dst, lhs, rhs);
5241 return false;
5242 case kCondNE:
5243 __ CmpEqS(dst, lhs, rhs);
5244 return true;
5245 case kCondLT:
5246 if (gt_bias) {
5247 __ CmpLtS(dst, lhs, rhs);
5248 } else {
5249 __ CmpUltS(dst, lhs, rhs);
5250 }
5251 return false;
5252 case kCondLE:
5253 if (gt_bias) {
5254 __ CmpLeS(dst, lhs, rhs);
5255 } else {
5256 __ CmpUleS(dst, lhs, rhs);
5257 }
5258 return false;
5259 case kCondGT:
5260 if (gt_bias) {
5261 __ CmpUltS(dst, rhs, lhs);
5262 } else {
5263 __ CmpLtS(dst, rhs, lhs);
5264 }
5265 return false;
5266 case kCondGE:
5267 if (gt_bias) {
5268 __ CmpUleS(dst, rhs, lhs);
5269 } else {
5270 __ CmpLeS(dst, rhs, lhs);
5271 }
5272 return false;
5273 default:
5274 LOG(FATAL) << "Unexpected non-floating-point condition";
5275 UNREACHABLE();
5276 }
5277 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005278 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005279 switch (cond) {
5280 case kCondEQ:
5281 __ CmpEqD(dst, lhs, rhs);
5282 return false;
5283 case kCondNE:
5284 __ CmpEqD(dst, lhs, rhs);
5285 return true;
5286 case kCondLT:
5287 if (gt_bias) {
5288 __ CmpLtD(dst, lhs, rhs);
5289 } else {
5290 __ CmpUltD(dst, lhs, rhs);
5291 }
5292 return false;
5293 case kCondLE:
5294 if (gt_bias) {
5295 __ CmpLeD(dst, lhs, rhs);
5296 } else {
5297 __ CmpUleD(dst, lhs, rhs);
5298 }
5299 return false;
5300 case kCondGT:
5301 if (gt_bias) {
5302 __ CmpUltD(dst, rhs, lhs);
5303 } else {
5304 __ CmpLtD(dst, rhs, lhs);
5305 }
5306 return false;
5307 case kCondGE:
5308 if (gt_bias) {
5309 __ CmpUleD(dst, rhs, lhs);
5310 } else {
5311 __ CmpLeD(dst, rhs, lhs);
5312 }
5313 return false;
5314 default:
5315 LOG(FATAL) << "Unexpected non-floating-point condition";
5316 UNREACHABLE();
5317 }
5318 }
5319}
5320
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005321void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5322 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005323 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005324 LocationSummary* locations,
5325 MipsLabel* label) {
5326 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5327 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5328 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005329 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005330 if (isR6) {
5331 switch (cond) {
5332 case kCondEQ:
5333 __ CmpEqS(FTMP, lhs, rhs);
5334 __ Bc1nez(FTMP, label);
5335 break;
5336 case kCondNE:
5337 __ CmpEqS(FTMP, lhs, rhs);
5338 __ Bc1eqz(FTMP, label);
5339 break;
5340 case kCondLT:
5341 if (gt_bias) {
5342 __ CmpLtS(FTMP, lhs, rhs);
5343 } else {
5344 __ CmpUltS(FTMP, lhs, rhs);
5345 }
5346 __ Bc1nez(FTMP, label);
5347 break;
5348 case kCondLE:
5349 if (gt_bias) {
5350 __ CmpLeS(FTMP, lhs, rhs);
5351 } else {
5352 __ CmpUleS(FTMP, lhs, rhs);
5353 }
5354 __ Bc1nez(FTMP, label);
5355 break;
5356 case kCondGT:
5357 if (gt_bias) {
5358 __ CmpUltS(FTMP, rhs, lhs);
5359 } else {
5360 __ CmpLtS(FTMP, rhs, lhs);
5361 }
5362 __ Bc1nez(FTMP, label);
5363 break;
5364 case kCondGE:
5365 if (gt_bias) {
5366 __ CmpUleS(FTMP, rhs, lhs);
5367 } else {
5368 __ CmpLeS(FTMP, rhs, lhs);
5369 }
5370 __ Bc1nez(FTMP, label);
5371 break;
5372 default:
5373 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005374 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005375 }
5376 } else {
5377 switch (cond) {
5378 case kCondEQ:
5379 __ CeqS(0, lhs, rhs);
5380 __ Bc1t(0, label);
5381 break;
5382 case kCondNE:
5383 __ CeqS(0, lhs, rhs);
5384 __ Bc1f(0, label);
5385 break;
5386 case kCondLT:
5387 if (gt_bias) {
5388 __ ColtS(0, lhs, rhs);
5389 } else {
5390 __ CultS(0, lhs, rhs);
5391 }
5392 __ Bc1t(0, label);
5393 break;
5394 case kCondLE:
5395 if (gt_bias) {
5396 __ ColeS(0, lhs, rhs);
5397 } else {
5398 __ CuleS(0, lhs, rhs);
5399 }
5400 __ Bc1t(0, label);
5401 break;
5402 case kCondGT:
5403 if (gt_bias) {
5404 __ CultS(0, rhs, lhs);
5405 } else {
5406 __ ColtS(0, rhs, lhs);
5407 }
5408 __ Bc1t(0, label);
5409 break;
5410 case kCondGE:
5411 if (gt_bias) {
5412 __ CuleS(0, rhs, lhs);
5413 } else {
5414 __ ColeS(0, rhs, lhs);
5415 }
5416 __ Bc1t(0, label);
5417 break;
5418 default:
5419 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005420 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005421 }
5422 }
5423 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005424 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005425 if (isR6) {
5426 switch (cond) {
5427 case kCondEQ:
5428 __ CmpEqD(FTMP, lhs, rhs);
5429 __ Bc1nez(FTMP, label);
5430 break;
5431 case kCondNE:
5432 __ CmpEqD(FTMP, lhs, rhs);
5433 __ Bc1eqz(FTMP, label);
5434 break;
5435 case kCondLT:
5436 if (gt_bias) {
5437 __ CmpLtD(FTMP, lhs, rhs);
5438 } else {
5439 __ CmpUltD(FTMP, lhs, rhs);
5440 }
5441 __ Bc1nez(FTMP, label);
5442 break;
5443 case kCondLE:
5444 if (gt_bias) {
5445 __ CmpLeD(FTMP, lhs, rhs);
5446 } else {
5447 __ CmpUleD(FTMP, lhs, rhs);
5448 }
5449 __ Bc1nez(FTMP, label);
5450 break;
5451 case kCondGT:
5452 if (gt_bias) {
5453 __ CmpUltD(FTMP, rhs, lhs);
5454 } else {
5455 __ CmpLtD(FTMP, rhs, lhs);
5456 }
5457 __ Bc1nez(FTMP, label);
5458 break;
5459 case kCondGE:
5460 if (gt_bias) {
5461 __ CmpUleD(FTMP, rhs, lhs);
5462 } else {
5463 __ CmpLeD(FTMP, rhs, lhs);
5464 }
5465 __ Bc1nez(FTMP, label);
5466 break;
5467 default:
5468 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005469 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005470 }
5471 } else {
5472 switch (cond) {
5473 case kCondEQ:
5474 __ CeqD(0, lhs, rhs);
5475 __ Bc1t(0, label);
5476 break;
5477 case kCondNE:
5478 __ CeqD(0, lhs, rhs);
5479 __ Bc1f(0, label);
5480 break;
5481 case kCondLT:
5482 if (gt_bias) {
5483 __ ColtD(0, lhs, rhs);
5484 } else {
5485 __ CultD(0, lhs, rhs);
5486 }
5487 __ Bc1t(0, label);
5488 break;
5489 case kCondLE:
5490 if (gt_bias) {
5491 __ ColeD(0, lhs, rhs);
5492 } else {
5493 __ CuleD(0, lhs, rhs);
5494 }
5495 __ Bc1t(0, label);
5496 break;
5497 case kCondGT:
5498 if (gt_bias) {
5499 __ CultD(0, rhs, lhs);
5500 } else {
5501 __ ColtD(0, rhs, lhs);
5502 }
5503 __ Bc1t(0, label);
5504 break;
5505 case kCondGE:
5506 if (gt_bias) {
5507 __ CuleD(0, rhs, lhs);
5508 } else {
5509 __ ColeD(0, rhs, lhs);
5510 }
5511 __ Bc1t(0, label);
5512 break;
5513 default:
5514 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005515 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005516 }
5517 }
5518 }
5519}
5520
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005521void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005522 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005523 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005524 MipsLabel* false_target) {
5525 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005526
David Brazdil0debae72015-11-12 18:37:00 +00005527 if (true_target == nullptr && false_target == nullptr) {
5528 // Nothing to do. The code always falls through.
5529 return;
5530 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005531 // Constant condition, statically compared against "true" (integer value 1).
5532 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005533 if (true_target != nullptr) {
5534 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005535 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005536 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005537 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005538 if (false_target != nullptr) {
5539 __ B(false_target);
5540 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005541 }
David Brazdil0debae72015-11-12 18:37:00 +00005542 return;
5543 }
5544
5545 // The following code generates these patterns:
5546 // (1) true_target == nullptr && false_target != nullptr
5547 // - opposite condition true => branch to false_target
5548 // (2) true_target != nullptr && false_target == nullptr
5549 // - condition true => branch to true_target
5550 // (3) true_target != nullptr && false_target != nullptr
5551 // - condition true => branch to true_target
5552 // - branch to false_target
5553 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005554 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005555 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005556 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005557 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005558 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5559 } else {
5560 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5561 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005562 } else {
5563 // The condition instruction has not been materialized, use its inputs as
5564 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005565 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005566 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005567 LocationSummary* locations = cond->GetLocations();
5568 IfCondition if_cond = condition->GetCondition();
5569 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005570
David Brazdil0debae72015-11-12 18:37:00 +00005571 if (true_target == nullptr) {
5572 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005573 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005574 }
5575
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005576 switch (type) {
5577 default:
5578 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5579 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005580 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005581 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5582 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005583 case DataType::Type::kFloat32:
5584 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005585 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5586 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005587 }
5588 }
David Brazdil0debae72015-11-12 18:37:00 +00005589
5590 // If neither branch falls through (case 3), the conditional branch to `true_target`
5591 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5592 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005593 __ B(false_target);
5594 }
5595}
5596
5597void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005598 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005599 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005600 locations->SetInAt(0, Location::RequiresRegister());
5601 }
5602}
5603
5604void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005605 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5606 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5607 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5608 nullptr : codegen_->GetLabelOf(true_successor);
5609 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5610 nullptr : codegen_->GetLabelOf(false_successor);
5611 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005612}
5613
5614void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01005615 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005616 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005617 InvokeRuntimeCallingConvention calling_convention;
5618 RegisterSet caller_saves = RegisterSet::Empty();
5619 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5620 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005621 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005622 locations->SetInAt(0, Location::RequiresRegister());
5623 }
5624}
5625
5626void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005627 SlowPathCodeMIPS* slow_path =
5628 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005629 GenerateTestAndBranch(deoptimize,
5630 /* condition_input_index */ 0,
5631 slow_path->GetEntryLabel(),
5632 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005633}
5634
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005635// This function returns true if a conditional move can be generated for HSelect.
5636// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5637// branches and regular moves.
5638//
5639// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5640//
5641// While determining feasibility of a conditional move and setting inputs/outputs
5642// are two distinct tasks, this function does both because they share quite a bit
5643// of common logic.
5644static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5645 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5646 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5647 HCondition* condition = cond->AsCondition();
5648
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005649 DataType::Type cond_type =
5650 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5651 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005652
5653 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5654 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5655 bool is_true_value_zero_constant =
5656 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5657 bool is_false_value_zero_constant =
5658 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5659
5660 bool can_move_conditionally = false;
5661 bool use_const_for_false_in = false;
5662 bool use_const_for_true_in = false;
5663
5664 if (!cond->IsConstant()) {
5665 switch (cond_type) {
5666 default:
5667 switch (dst_type) {
5668 default:
5669 // Moving int on int condition.
5670 if (is_r6) {
5671 if (is_true_value_zero_constant) {
5672 // seleqz out_reg, false_reg, cond_reg
5673 can_move_conditionally = true;
5674 use_const_for_true_in = true;
5675 } else if (is_false_value_zero_constant) {
5676 // selnez out_reg, true_reg, cond_reg
5677 can_move_conditionally = true;
5678 use_const_for_false_in = true;
5679 } else if (materialized) {
5680 // Not materializing unmaterialized int conditions
5681 // to keep the instruction count low.
5682 // selnez AT, true_reg, cond_reg
5683 // seleqz TMP, false_reg, cond_reg
5684 // or out_reg, AT, TMP
5685 can_move_conditionally = true;
5686 }
5687 } else {
5688 // movn out_reg, true_reg/ZERO, cond_reg
5689 can_move_conditionally = true;
5690 use_const_for_true_in = is_true_value_zero_constant;
5691 }
5692 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005693 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005694 // Moving long on int condition.
5695 if (is_r6) {
5696 if (is_true_value_zero_constant) {
5697 // seleqz out_reg_lo, false_reg_lo, cond_reg
5698 // seleqz out_reg_hi, false_reg_hi, cond_reg
5699 can_move_conditionally = true;
5700 use_const_for_true_in = true;
5701 } else if (is_false_value_zero_constant) {
5702 // selnez out_reg_lo, true_reg_lo, cond_reg
5703 // selnez out_reg_hi, true_reg_hi, cond_reg
5704 can_move_conditionally = true;
5705 use_const_for_false_in = true;
5706 }
5707 // Other long conditional moves would generate 6+ instructions,
5708 // which is too many.
5709 } else {
5710 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5711 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5712 can_move_conditionally = true;
5713 use_const_for_true_in = is_true_value_zero_constant;
5714 }
5715 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005716 case DataType::Type::kFloat32:
5717 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005718 // Moving float/double on int condition.
5719 if (is_r6) {
5720 if (materialized) {
5721 // Not materializing unmaterialized int conditions
5722 // to keep the instruction count low.
5723 can_move_conditionally = true;
5724 if (is_true_value_zero_constant) {
5725 // sltu TMP, ZERO, cond_reg
5726 // mtc1 TMP, temp_cond_reg
5727 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5728 use_const_for_true_in = true;
5729 } else if (is_false_value_zero_constant) {
5730 // sltu TMP, ZERO, cond_reg
5731 // mtc1 TMP, temp_cond_reg
5732 // selnez.fmt out_reg, true_reg, temp_cond_reg
5733 use_const_for_false_in = true;
5734 } else {
5735 // sltu TMP, ZERO, cond_reg
5736 // mtc1 TMP, temp_cond_reg
5737 // sel.fmt temp_cond_reg, false_reg, true_reg
5738 // mov.fmt out_reg, temp_cond_reg
5739 }
5740 }
5741 } else {
5742 // movn.fmt out_reg, true_reg, cond_reg
5743 can_move_conditionally = true;
5744 }
5745 break;
5746 }
5747 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005748 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005749 // We don't materialize long comparison now
5750 // and use conditional branches instead.
5751 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005752 case DataType::Type::kFloat32:
5753 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005754 switch (dst_type) {
5755 default:
5756 // Moving int on float/double condition.
5757 if (is_r6) {
5758 if (is_true_value_zero_constant) {
5759 // mfc1 TMP, temp_cond_reg
5760 // seleqz out_reg, false_reg, TMP
5761 can_move_conditionally = true;
5762 use_const_for_true_in = true;
5763 } else if (is_false_value_zero_constant) {
5764 // mfc1 TMP, temp_cond_reg
5765 // selnez out_reg, true_reg, TMP
5766 can_move_conditionally = true;
5767 use_const_for_false_in = true;
5768 } else {
5769 // mfc1 TMP, temp_cond_reg
5770 // selnez AT, true_reg, TMP
5771 // seleqz TMP, false_reg, TMP
5772 // or out_reg, AT, TMP
5773 can_move_conditionally = true;
5774 }
5775 } else {
5776 // movt out_reg, true_reg/ZERO, cc
5777 can_move_conditionally = true;
5778 use_const_for_true_in = is_true_value_zero_constant;
5779 }
5780 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005781 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005782 // Moving long on float/double condition.
5783 if (is_r6) {
5784 if (is_true_value_zero_constant) {
5785 // mfc1 TMP, temp_cond_reg
5786 // seleqz out_reg_lo, false_reg_lo, TMP
5787 // seleqz out_reg_hi, false_reg_hi, TMP
5788 can_move_conditionally = true;
5789 use_const_for_true_in = true;
5790 } else if (is_false_value_zero_constant) {
5791 // mfc1 TMP, temp_cond_reg
5792 // selnez out_reg_lo, true_reg_lo, TMP
5793 // selnez out_reg_hi, true_reg_hi, TMP
5794 can_move_conditionally = true;
5795 use_const_for_false_in = true;
5796 }
5797 // Other long conditional moves would generate 6+ instructions,
5798 // which is too many.
5799 } else {
5800 // movt out_reg_lo, true_reg_lo/ZERO, cc
5801 // movt out_reg_hi, true_reg_hi/ZERO, cc
5802 can_move_conditionally = true;
5803 use_const_for_true_in = is_true_value_zero_constant;
5804 }
5805 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005806 case DataType::Type::kFloat32:
5807 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005808 // Moving float/double on float/double condition.
5809 if (is_r6) {
5810 can_move_conditionally = true;
5811 if (is_true_value_zero_constant) {
5812 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5813 use_const_for_true_in = true;
5814 } else if (is_false_value_zero_constant) {
5815 // selnez.fmt out_reg, true_reg, temp_cond_reg
5816 use_const_for_false_in = true;
5817 } else {
5818 // sel.fmt temp_cond_reg, false_reg, true_reg
5819 // mov.fmt out_reg, temp_cond_reg
5820 }
5821 } else {
5822 // movt.fmt out_reg, true_reg, cc
5823 can_move_conditionally = true;
5824 }
5825 break;
5826 }
5827 break;
5828 }
5829 }
5830
5831 if (can_move_conditionally) {
5832 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5833 } else {
5834 DCHECK(!use_const_for_false_in);
5835 DCHECK(!use_const_for_true_in);
5836 }
5837
5838 if (locations_to_set != nullptr) {
5839 if (use_const_for_false_in) {
5840 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5841 } else {
5842 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005843 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005844 ? Location::RequiresFpuRegister()
5845 : Location::RequiresRegister());
5846 }
5847 if (use_const_for_true_in) {
5848 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5849 } else {
5850 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005851 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005852 ? Location::RequiresFpuRegister()
5853 : Location::RequiresRegister());
5854 }
5855 if (materialized) {
5856 locations_to_set->SetInAt(2, Location::RequiresRegister());
5857 }
5858 // On R6 we don't require the output to be the same as the
5859 // first input for conditional moves unlike on R2.
5860 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5861 if (is_out_same_as_first_in) {
5862 locations_to_set->SetOut(Location::SameAsFirstInput());
5863 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005864 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005865 ? Location::RequiresFpuRegister()
5866 : Location::RequiresRegister());
5867 }
5868 }
5869
5870 return can_move_conditionally;
5871}
5872
5873void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5874 LocationSummary* locations = select->GetLocations();
5875 Location dst = locations->Out();
5876 Location src = locations->InAt(1);
5877 Register src_reg = ZERO;
5878 Register src_reg_high = ZERO;
5879 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5880 Register cond_reg = TMP;
5881 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005882 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005883 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005884 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005885
5886 if (IsBooleanValueOrMaterializedCondition(cond)) {
5887 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5888 } else {
5889 HCondition* condition = cond->AsCondition();
5890 LocationSummary* cond_locations = cond->GetLocations();
5891 IfCondition if_cond = condition->GetCondition();
5892 cond_type = condition->InputAt(0)->GetType();
5893 switch (cond_type) {
5894 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005895 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005896 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5897 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005898 case DataType::Type::kFloat32:
5899 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005900 cond_inverted = MaterializeFpCompareR2(if_cond,
5901 condition->IsGtBias(),
5902 cond_type,
5903 cond_locations,
5904 cond_cc);
5905 break;
5906 }
5907 }
5908
5909 DCHECK(dst.Equals(locations->InAt(0)));
5910 if (src.IsRegister()) {
5911 src_reg = src.AsRegister<Register>();
5912 } else if (src.IsRegisterPair()) {
5913 src_reg = src.AsRegisterPairLow<Register>();
5914 src_reg_high = src.AsRegisterPairHigh<Register>();
5915 } else if (src.IsConstant()) {
5916 DCHECK(src.GetConstant()->IsZeroBitPattern());
5917 }
5918
5919 switch (cond_type) {
5920 default:
5921 switch (dst_type) {
5922 default:
5923 if (cond_inverted) {
5924 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5925 } else {
5926 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5927 }
5928 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005929 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005930 if (cond_inverted) {
5931 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5932 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5933 } else {
5934 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5935 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5936 }
5937 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005938 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005939 if (cond_inverted) {
5940 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5941 } else {
5942 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5943 }
5944 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005945 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005946 if (cond_inverted) {
5947 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5948 } else {
5949 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5950 }
5951 break;
5952 }
5953 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005954 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005955 LOG(FATAL) << "Unreachable";
5956 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005957 case DataType::Type::kFloat32:
5958 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005959 switch (dst_type) {
5960 default:
5961 if (cond_inverted) {
5962 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5963 } else {
5964 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5965 }
5966 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005967 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005968 if (cond_inverted) {
5969 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5970 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5971 } else {
5972 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5973 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5974 }
5975 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005976 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005977 if (cond_inverted) {
5978 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5979 } else {
5980 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5981 }
5982 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005983 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005984 if (cond_inverted) {
5985 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5986 } else {
5987 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5988 }
5989 break;
5990 }
5991 break;
5992 }
5993}
5994
5995void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5996 LocationSummary* locations = select->GetLocations();
5997 Location dst = locations->Out();
5998 Location false_src = locations->InAt(0);
5999 Location true_src = locations->InAt(1);
6000 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
6001 Register cond_reg = TMP;
6002 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006003 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006004 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006005 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006006
6007 if (IsBooleanValueOrMaterializedCondition(cond)) {
6008 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
6009 } else {
6010 HCondition* condition = cond->AsCondition();
6011 LocationSummary* cond_locations = cond->GetLocations();
6012 IfCondition if_cond = condition->GetCondition();
6013 cond_type = condition->InputAt(0)->GetType();
6014 switch (cond_type) {
6015 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006016 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006017 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
6018 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006019 case DataType::Type::kFloat32:
6020 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006021 cond_inverted = MaterializeFpCompareR6(if_cond,
6022 condition->IsGtBias(),
6023 cond_type,
6024 cond_locations,
6025 fcond_reg);
6026 break;
6027 }
6028 }
6029
6030 if (true_src.IsConstant()) {
6031 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
6032 }
6033 if (false_src.IsConstant()) {
6034 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
6035 }
6036
6037 switch (dst_type) {
6038 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006039 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006040 __ Mfc1(cond_reg, fcond_reg);
6041 }
6042 if (true_src.IsConstant()) {
6043 if (cond_inverted) {
6044 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6045 } else {
6046 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
6047 }
6048 } else if (false_src.IsConstant()) {
6049 if (cond_inverted) {
6050 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6051 } else {
6052 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
6053 }
6054 } else {
6055 DCHECK_NE(cond_reg, AT);
6056 if (cond_inverted) {
6057 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
6058 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
6059 } else {
6060 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
6061 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
6062 }
6063 __ Or(dst.AsRegister<Register>(), AT, TMP);
6064 }
6065 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006066 case DataType::Type::kInt64: {
6067 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006068 __ Mfc1(cond_reg, fcond_reg);
6069 }
6070 Register dst_lo = dst.AsRegisterPairLow<Register>();
6071 Register dst_hi = dst.AsRegisterPairHigh<Register>();
6072 if (true_src.IsConstant()) {
6073 Register src_lo = false_src.AsRegisterPairLow<Register>();
6074 Register src_hi = false_src.AsRegisterPairHigh<Register>();
6075 if (cond_inverted) {
6076 __ Selnez(dst_lo, src_lo, cond_reg);
6077 __ Selnez(dst_hi, src_hi, cond_reg);
6078 } else {
6079 __ Seleqz(dst_lo, src_lo, cond_reg);
6080 __ Seleqz(dst_hi, src_hi, cond_reg);
6081 }
6082 } else {
6083 DCHECK(false_src.IsConstant());
6084 Register src_lo = true_src.AsRegisterPairLow<Register>();
6085 Register src_hi = true_src.AsRegisterPairHigh<Register>();
6086 if (cond_inverted) {
6087 __ Seleqz(dst_lo, src_lo, cond_reg);
6088 __ Seleqz(dst_hi, src_hi, cond_reg);
6089 } else {
6090 __ Selnez(dst_lo, src_lo, cond_reg);
6091 __ Selnez(dst_hi, src_hi, cond_reg);
6092 }
6093 }
6094 break;
6095 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006096 case DataType::Type::kFloat32: {
6097 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006098 // sel*.fmt tests bit 0 of the condition register, account for that.
6099 __ Sltu(TMP, ZERO, cond_reg);
6100 __ Mtc1(TMP, fcond_reg);
6101 }
6102 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6103 if (true_src.IsConstant()) {
6104 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6105 if (cond_inverted) {
6106 __ SelnezS(dst_reg, src_reg, fcond_reg);
6107 } else {
6108 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6109 }
6110 } else if (false_src.IsConstant()) {
6111 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6112 if (cond_inverted) {
6113 __ SeleqzS(dst_reg, src_reg, fcond_reg);
6114 } else {
6115 __ SelnezS(dst_reg, src_reg, fcond_reg);
6116 }
6117 } else {
6118 if (cond_inverted) {
6119 __ SelS(fcond_reg,
6120 true_src.AsFpuRegister<FRegister>(),
6121 false_src.AsFpuRegister<FRegister>());
6122 } else {
6123 __ SelS(fcond_reg,
6124 false_src.AsFpuRegister<FRegister>(),
6125 true_src.AsFpuRegister<FRegister>());
6126 }
6127 __ MovS(dst_reg, fcond_reg);
6128 }
6129 break;
6130 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006131 case DataType::Type::kFloat64: {
6132 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006133 // sel*.fmt tests bit 0 of the condition register, account for that.
6134 __ Sltu(TMP, ZERO, cond_reg);
6135 __ Mtc1(TMP, fcond_reg);
6136 }
6137 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6138 if (true_src.IsConstant()) {
6139 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6140 if (cond_inverted) {
6141 __ SelnezD(dst_reg, src_reg, fcond_reg);
6142 } else {
6143 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6144 }
6145 } else if (false_src.IsConstant()) {
6146 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6147 if (cond_inverted) {
6148 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6149 } else {
6150 __ SelnezD(dst_reg, src_reg, fcond_reg);
6151 }
6152 } else {
6153 if (cond_inverted) {
6154 __ SelD(fcond_reg,
6155 true_src.AsFpuRegister<FRegister>(),
6156 false_src.AsFpuRegister<FRegister>());
6157 } else {
6158 __ SelD(fcond_reg,
6159 false_src.AsFpuRegister<FRegister>(),
6160 true_src.AsFpuRegister<FRegister>());
6161 }
6162 __ MovD(dst_reg, fcond_reg);
6163 }
6164 break;
6165 }
6166 }
6167}
6168
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006169void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006170 LocationSummary* locations = new (GetGraph()->GetAllocator())
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006171 LocationSummary(flag, LocationSummary::kNoCall);
6172 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006173}
6174
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006175void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6176 __ LoadFromOffset(kLoadWord,
6177 flag->GetLocations()->Out().AsRegister<Register>(),
6178 SP,
6179 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006180}
6181
David Brazdil74eb1b22015-12-14 11:44:01 +00006182void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006183 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006184 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006185}
6186
6187void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006188 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6189 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6190 if (is_r6) {
6191 GenConditionalMoveR6(select);
6192 } else {
6193 GenConditionalMoveR2(select);
6194 }
6195 } else {
6196 LocationSummary* locations = select->GetLocations();
6197 MipsLabel false_target;
6198 GenerateTestAndBranch(select,
6199 /* condition_input_index */ 2,
6200 /* true_target */ nullptr,
6201 &false_target);
6202 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6203 __ Bind(&false_target);
6204 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006205}
6206
David Srbecky0cf44932015-12-09 14:09:59 +00006207void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01006208 new (GetGraph()->GetAllocator()) LocationSummary(info);
David Srbecky0cf44932015-12-09 14:09:59 +00006209}
6210
David Srbeckyd28f4a02016-03-14 17:14:24 +00006211void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6212 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006213}
6214
6215void CodeGeneratorMIPS::GenerateNop() {
6216 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006217}
6218
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006219void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006220 DataType::Type field_type = field_info.GetFieldType();
6221 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006222 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006223 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006224 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Vladimir Markoca6fff82017-10-03 14:49:14 +01006225 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006226 instruction,
6227 generate_volatile
6228 ? LocationSummary::kCallOnMainOnly
6229 : (object_field_get_with_read_barrier
6230 ? LocationSummary::kCallOnSlowPath
6231 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006232
Alexey Frunzec61c0762017-04-10 13:54:23 -07006233 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6234 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6235 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006236 locations->SetInAt(0, Location::RequiresRegister());
6237 if (generate_volatile) {
6238 InvokeRuntimeCallingConvention calling_convention;
6239 // need A0 to hold base + offset
6240 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006241 if (field_type == DataType::Type::kInt64) {
6242 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006243 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006244 // Use Location::Any() to prevent situations when running out of available fp registers.
6245 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006246 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006247 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006248 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6249 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6250 }
6251 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006252 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006253 locations->SetOut(Location::RequiresFpuRegister());
6254 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006255 // The output overlaps in the case of an object field get with
6256 // read barriers enabled: we do not want the move to overwrite the
6257 // object's location, as we need it to emit the read barrier.
6258 locations->SetOut(Location::RequiresRegister(),
6259 object_field_get_with_read_barrier
6260 ? Location::kOutputOverlap
6261 : Location::kNoOutputOverlap);
6262 }
6263 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6264 // We need a temporary register for the read barrier marking slow
6265 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006266 if (!kBakerReadBarrierThunksEnableForFields) {
6267 locations->AddTemp(Location::RequiresRegister());
6268 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006269 }
6270 }
6271}
6272
6273void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6274 const FieldInfo& field_info,
6275 uint32_t dex_pc) {
Vladimir Marko61b92282017-10-11 13:23:17 +01006276 DCHECK_EQ(DataType::Size(field_info.GetFieldType()), DataType::Size(instruction->GetType()));
6277 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006278 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006279 Location obj_loc = locations->InAt(0);
6280 Register obj = obj_loc.AsRegister<Register>();
6281 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006282 LoadOperandType load_type = kLoadUnsignedByte;
6283 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006284 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006285 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006286
6287 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006288 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006289 case DataType::Type::kUint8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006290 load_type = kLoadUnsignedByte;
6291 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006292 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006293 load_type = kLoadSignedByte;
6294 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006295 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006296 load_type = kLoadUnsignedHalfword;
6297 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006298 case DataType::Type::kInt16:
6299 load_type = kLoadSignedHalfword;
6300 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006301 case DataType::Type::kInt32:
6302 case DataType::Type::kFloat32:
6303 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006304 load_type = kLoadWord;
6305 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006306 case DataType::Type::kInt64:
6307 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006308 load_type = kLoadDoubleword;
6309 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006310 case DataType::Type::kUint32:
6311 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006312 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006313 LOG(FATAL) << "Unreachable type " << type;
6314 UNREACHABLE();
6315 }
6316
6317 if (is_volatile && load_type == kLoadDoubleword) {
6318 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006319 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006320 // Do implicit Null check
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006321 __ LoadFromOffset(kLoadWord,
6322 ZERO,
6323 locations->GetTemp(0).AsRegister<Register>(),
6324 0,
6325 null_checker);
Serban Constantinescufca16662016-07-14 09:21:59 +01006326 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006327 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006328 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006329 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006330 if (dst_loc.IsFpuRegister()) {
6331 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006332 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006333 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006334 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006335 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006336 __ StoreToOffset(kStoreWord,
6337 locations->GetTemp(1).AsRegister<Register>(),
6338 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006339 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006340 __ StoreToOffset(kStoreWord,
6341 locations->GetTemp(2).AsRegister<Register>(),
6342 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006343 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006344 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006345 }
6346 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006347 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006348 // /* HeapReference<Object> */ dst = *(obj + offset)
6349 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006350 Location temp_loc =
6351 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006352 // Note that a potential implicit null check is handled in this
6353 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6354 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6355 dst_loc,
6356 obj,
6357 offset,
6358 temp_loc,
6359 /* needs_null_check */ true);
6360 if (is_volatile) {
6361 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6362 }
6363 } else {
6364 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6365 if (is_volatile) {
6366 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6367 }
6368 // If read barriers are enabled, emit read barriers other than
6369 // Baker's using a slow path (and also unpoison the loaded
6370 // reference, if heap poisoning is enabled).
6371 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6372 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006373 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006374 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006375 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006376 DCHECK(dst_loc.IsRegisterPair());
6377 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006378 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006379 DCHECK(dst_loc.IsRegister());
6380 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006381 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006382 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006383 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006384 DCHECK(dst_loc.IsFpuRegister());
6385 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006386 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006387 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006388 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006389 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006390 }
6391 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 }
6393
Alexey Frunze15958152017-02-09 19:08:30 -08006394 // Memory barriers, in the case of references, are handled in the
6395 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006396 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006397 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6398 }
6399}
6400
6401void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006402 DataType::Type field_type = field_info.GetFieldType();
6403 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006404 bool generate_volatile = field_info.IsVolatile() && is_wide;
Vladimir Markoca6fff82017-10-03 14:49:14 +01006405 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006406 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006407
6408 locations->SetInAt(0, Location::RequiresRegister());
6409 if (generate_volatile) {
6410 InvokeRuntimeCallingConvention calling_convention;
6411 // need A0 to hold base + offset
6412 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006413 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006414 locations->SetInAt(1, Location::RegisterPairLocation(
6415 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6416 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006417 // Use Location::Any() to prevent situations when running out of available fp registers.
6418 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006419 // Pass FP parameters in core registers.
6420 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6421 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6422 }
6423 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006424 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006425 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006426 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006427 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006428 }
6429 }
6430}
6431
6432void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6433 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006434 uint32_t dex_pc,
6435 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006436 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006437 LocationSummary* locations = instruction->GetLocations();
6438 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006439 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006440 StoreOperandType store_type = kStoreByte;
6441 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006442 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006443 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006444 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006445
6446 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006447 case DataType::Type::kBool:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006448 case DataType::Type::kUint8:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006449 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006450 store_type = kStoreByte;
6451 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006452 case DataType::Type::kUint16:
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01006453 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006454 store_type = kStoreHalfword;
6455 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006456 case DataType::Type::kInt32:
6457 case DataType::Type::kFloat32:
6458 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006459 store_type = kStoreWord;
6460 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006461 case DataType::Type::kInt64:
6462 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006463 store_type = kStoreDoubleword;
6464 break;
Aart Bik66c158e2018-01-31 12:55:04 -08006465 case DataType::Type::kUint32:
6466 case DataType::Type::kUint64:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006467 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006468 LOG(FATAL) << "Unreachable type " << type;
6469 UNREACHABLE();
6470 }
6471
6472 if (is_volatile) {
6473 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6474 }
6475
6476 if (is_volatile && store_type == kStoreDoubleword) {
6477 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006478 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006479 // Do implicit Null check.
Goran Jakovljevic2e61a572017-10-23 08:58:15 +02006480 __ LoadFromOffset(kLoadWord,
6481 ZERO,
6482 locations->GetTemp(0).AsRegister<Register>(),
6483 0,
6484 null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006485 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006486 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006487 if (value_location.IsFpuRegister()) {
6488 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6489 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006490 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006491 value_location.AsFpuRegister<FRegister>());
6492 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006493 __ LoadFromOffset(kLoadWord,
6494 locations->GetTemp(1).AsRegister<Register>(),
6495 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006496 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006497 __ LoadFromOffset(kLoadWord,
6498 locations->GetTemp(2).AsRegister<Register>(),
6499 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006500 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006501 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006502 DCHECK(value_location.IsConstant());
6503 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6504 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006505 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6506 locations->GetTemp(1).AsRegister<Register>(),
6507 value);
6508 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006509 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006510 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006511 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6512 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006513 if (value_location.IsConstant()) {
6514 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6515 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006516 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006517 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006518 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006519 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006520 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006521 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006522 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006523 if (kPoisonHeapReferences && needs_write_barrier) {
6524 // Note that in the case where `value` is a null reference,
6525 // we do not enter this block, as a null reference does not
6526 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006527 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006528 __ PoisonHeapReference(TMP, src);
6529 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6530 } else {
6531 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6532 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006533 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006534 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006535 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006536 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006537 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006538 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006539 }
6540 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006541 }
6542
Alexey Frunzec061de12017-02-14 13:27:23 -08006543 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006544 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006545 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006546 }
6547
6548 if (is_volatile) {
6549 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6550 }
6551}
6552
6553void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6554 HandleFieldGet(instruction, instruction->GetFieldInfo());
6555}
6556
6557void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6558 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6559}
6560
6561void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6562 HandleFieldSet(instruction, instruction->GetFieldInfo());
6563}
6564
6565void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006566 HandleFieldSet(instruction,
6567 instruction->GetFieldInfo(),
6568 instruction->GetDexPc(),
6569 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006570}
6571
Alexey Frunze15958152017-02-09 19:08:30 -08006572void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6573 HInstruction* instruction,
6574 Location out,
6575 uint32_t offset,
6576 Location maybe_temp,
6577 ReadBarrierOption read_barrier_option) {
6578 Register out_reg = out.AsRegister<Register>();
6579 if (read_barrier_option == kWithReadBarrier) {
6580 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006581 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6582 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6583 }
Alexey Frunze15958152017-02-09 19:08:30 -08006584 if (kUseBakerReadBarrier) {
6585 // Load with fast path based Baker's read barrier.
6586 // /* HeapReference<Object> */ out = *(out + offset)
6587 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6588 out,
6589 out_reg,
6590 offset,
6591 maybe_temp,
6592 /* needs_null_check */ false);
6593 } else {
6594 // Load with slow path based read barrier.
6595 // Save the value of `out` into `maybe_temp` before overwriting it
6596 // in the following move operation, as we will need it for the
6597 // read barrier below.
6598 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6599 // /* HeapReference<Object> */ out = *(out + offset)
6600 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6601 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6602 }
6603 } else {
6604 // Plain load with no read barrier.
6605 // /* HeapReference<Object> */ out = *(out + offset)
6606 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6607 __ MaybeUnpoisonHeapReference(out_reg);
6608 }
6609}
6610
6611void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6612 HInstruction* instruction,
6613 Location out,
6614 Location obj,
6615 uint32_t offset,
6616 Location maybe_temp,
6617 ReadBarrierOption read_barrier_option) {
6618 Register out_reg = out.AsRegister<Register>();
6619 Register obj_reg = obj.AsRegister<Register>();
6620 if (read_barrier_option == kWithReadBarrier) {
6621 CHECK(kEmitCompilerReadBarrier);
6622 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006623 if (!kBakerReadBarrierThunksEnableForFields) {
6624 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6625 }
Alexey Frunze15958152017-02-09 19:08:30 -08006626 // Load with fast path based Baker's read barrier.
6627 // /* HeapReference<Object> */ out = *(obj + offset)
6628 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6629 out,
6630 obj_reg,
6631 offset,
6632 maybe_temp,
6633 /* needs_null_check */ false);
6634 } else {
6635 // Load with slow path based read barrier.
6636 // /* HeapReference<Object> */ out = *(obj + offset)
6637 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6638 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6639 }
6640 } else {
6641 // Plain load with no read barrier.
6642 // /* HeapReference<Object> */ out = *(obj + offset)
6643 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6644 __ MaybeUnpoisonHeapReference(out_reg);
6645 }
6646}
6647
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006648static inline int GetBakerMarkThunkNumber(Register reg) {
6649 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6650 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6651 return reg - V0;
6652 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6653 return 14 + (reg - S2);
6654 } else if (reg == FP) { // One more.
6655 return 20;
6656 }
6657 LOG(FATAL) << "Unexpected register " << reg;
6658 UNREACHABLE();
6659}
6660
6661static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6662 int num = GetBakerMarkThunkNumber(reg) +
6663 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6664 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6665}
6666
6667static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6668 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6669 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6670}
6671
Alexey Frunze15958152017-02-09 19:08:30 -08006672void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6673 Location root,
6674 Register obj,
6675 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006676 ReadBarrierOption read_barrier_option,
6677 MipsLabel* label_low) {
6678 bool reordering;
6679 if (label_low != nullptr) {
6680 DCHECK_EQ(offset, 0x5678u);
6681 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006682 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006683 if (read_barrier_option == kWithReadBarrier) {
6684 DCHECK(kEmitCompilerReadBarrier);
6685 if (kUseBakerReadBarrier) {
6686 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6687 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006688 if (kBakerReadBarrierThunksEnableForGcRoots) {
6689 // Note that we do not actually check the value of `GetIsGcMarking()`
6690 // to decide whether to mark the loaded GC root or not. Instead, we
6691 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6692 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6693 // vice versa.
6694 //
6695 // We use thunks for the slow path. That thunk checks the reference
6696 // and jumps to the entrypoint if needed.
6697 //
6698 // temp = Thread::Current()->pReadBarrierMarkReg00
6699 // // AKA &art_quick_read_barrier_mark_introspection.
6700 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6701 // if (temp != nullptr) {
6702 // temp = &gc_root_thunk<root_reg>
6703 // root = temp(root)
6704 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006705
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006706 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6707 const int32_t entry_point_offset =
6708 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6709 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6710 int16_t offset_low = Low16Bits(offset);
6711 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6712 // extension in lw.
6713 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6714 Register base = short_offset ? obj : TMP;
6715 // Loading the entrypoint does not require a load acquire since it is only changed when
6716 // threads are suspended or running a checkpoint.
6717 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6718 reordering = __ SetReorder(false);
6719 if (!short_offset) {
6720 DCHECK(!label_low);
6721 __ AddUpper(base, obj, offset_high);
6722 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006723 MipsLabel skip_call;
6724 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006725 if (label_low != nullptr) {
6726 DCHECK(short_offset);
6727 __ Bind(label_low);
6728 }
6729 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6730 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6731 // in delay slot.
6732 if (isR6) {
6733 __ Jialc(T9, thunk_disp);
6734 } else {
6735 __ Addiu(T9, T9, thunk_disp);
6736 __ Jalr(T9);
6737 __ Nop();
6738 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006739 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006740 __ SetReorder(reordering);
6741 } else {
6742 // Note that we do not actually check the value of `GetIsGcMarking()`
6743 // to decide whether to mark the loaded GC root or not. Instead, we
6744 // load into `temp` (T9) the read barrier mark entry point corresponding
6745 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6746 // is false, and vice versa.
6747 //
6748 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6749 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6750 // if (temp != null) {
6751 // root = temp(root)
6752 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006753
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006754 if (label_low != nullptr) {
6755 reordering = __ SetReorder(false);
6756 __ Bind(label_low);
6757 }
6758 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6759 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6760 if (label_low != nullptr) {
6761 __ SetReorder(reordering);
6762 }
6763 static_assert(
6764 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6765 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6766 "have different sizes.");
6767 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6768 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6769 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006770
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006771 // Slow path marking the GC root `root`.
6772 Location temp = Location::RegisterLocation(T9);
6773 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01006774 new (codegen_->GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006775 instruction,
6776 root,
6777 /*entrypoint*/ temp);
6778 codegen_->AddSlowPath(slow_path);
6779
6780 const int32_t entry_point_offset =
6781 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6782 // Loading the entrypoint does not require a load acquire since it is only changed when
6783 // threads are suspended or running a checkpoint.
6784 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6785 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6786 __ Bind(slow_path->GetExitLabel());
6787 }
Alexey Frunze15958152017-02-09 19:08:30 -08006788 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006789 if (label_low != nullptr) {
6790 reordering = __ SetReorder(false);
6791 __ Bind(label_low);
6792 }
Alexey Frunze15958152017-02-09 19:08:30 -08006793 // GC root loaded through a slow path for read barriers other
6794 // than Baker's.
6795 // /* GcRoot<mirror::Object>* */ root = obj + offset
6796 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006797 if (label_low != nullptr) {
6798 __ SetReorder(reordering);
6799 }
Alexey Frunze15958152017-02-09 19:08:30 -08006800 // /* mirror::Object* */ root = root->Read()
6801 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6802 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006803 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006804 if (label_low != nullptr) {
6805 reordering = __ SetReorder(false);
6806 __ Bind(label_low);
6807 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006808 // Plain GC root load with no read barrier.
6809 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6810 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6811 // Note that GC roots are not affected by heap poisoning, thus we
6812 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006813 if (label_low != nullptr) {
6814 __ SetReorder(reordering);
6815 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006816 }
6817}
6818
Alexey Frunze15958152017-02-09 19:08:30 -08006819void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6820 Location ref,
6821 Register obj,
6822 uint32_t offset,
6823 Location temp,
6824 bool needs_null_check) {
6825 DCHECK(kEmitCompilerReadBarrier);
6826 DCHECK(kUseBakerReadBarrier);
6827
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006828 if (kBakerReadBarrierThunksEnableForFields) {
6829 // Note that we do not actually check the value of `GetIsGcMarking()`
6830 // to decide whether to mark the loaded reference or not. Instead, we
6831 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6832 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6833 // vice versa.
6834 //
6835 // We use thunks for the slow path. That thunk checks the reference
6836 // and jumps to the entrypoint if needed. If the holder is not gray,
6837 // it issues a load-load memory barrier and returns to the original
6838 // reference load.
6839 //
6840 // temp = Thread::Current()->pReadBarrierMarkReg00
6841 // // AKA &art_quick_read_barrier_mark_introspection.
6842 // if (temp != nullptr) {
6843 // temp = &field_array_thunk<holder_reg>
6844 // temp()
6845 // }
6846 // not_gray_return_address:
6847 // // If the offset is too large to fit into the lw instruction, we
6848 // // use an adjusted base register (TMP) here. This register
6849 // // receives bits 16 ... 31 of the offset before the thunk invocation
6850 // // and the thunk benefits from it.
6851 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6852 // gray_return_address:
6853
6854 DCHECK(temp.IsInvalid());
6855 bool isR6 = GetInstructionSetFeatures().IsR6();
6856 int16_t offset_low = Low16Bits(offset);
6857 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6858 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6859 bool reordering = __ SetReorder(false);
6860 const int32_t entry_point_offset =
6861 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6862 // There may have or may have not been a null check if the field offset is smaller than
6863 // the page size.
6864 // There must've been a null check in case it's actually a load from an array.
6865 // We will, however, perform an explicit null check in the thunk as it's easier to
6866 // do it than not.
6867 if (instruction->IsArrayGet()) {
6868 DCHECK(!needs_null_check);
6869 }
6870 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6871 // Loading the entrypoint does not require a load acquire since it is only changed when
6872 // threads are suspended or running a checkpoint.
6873 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6874 Register ref_reg = ref.AsRegister<Register>();
6875 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006876 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006877 if (short_offset) {
6878 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006879 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006880 __ Nop(); // In forbidden slot.
6881 __ Jialc(T9, thunk_disp);
6882 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006883 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006884 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6885 __ Jalr(T9);
6886 __ Nop(); // In delay slot.
6887 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006888 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006889 } else {
6890 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006891 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006892 __ Aui(base, obj, offset_high); // In delay slot.
6893 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006894 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006895 } else {
6896 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006897 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006898 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6899 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006900 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006901 __ Addu(base, base, obj); // In delay slot.
6902 }
6903 }
6904 // /* HeapReference<Object> */ ref = *(obj + offset)
6905 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6906 if (needs_null_check) {
6907 MaybeRecordImplicitNullCheck(instruction);
6908 }
6909 __ MaybeUnpoisonHeapReference(ref_reg);
6910 __ SetReorder(reordering);
6911 return;
6912 }
6913
Alexey Frunze15958152017-02-09 19:08:30 -08006914 // /* HeapReference<Object> */ ref = *(obj + offset)
6915 Location no_index = Location::NoLocation();
6916 ScaleFactor no_scale_factor = TIMES_1;
6917 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6918 ref,
6919 obj,
6920 offset,
6921 no_index,
6922 no_scale_factor,
6923 temp,
6924 needs_null_check);
6925}
6926
6927void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6928 Location ref,
6929 Register obj,
6930 uint32_t data_offset,
6931 Location index,
6932 Location temp,
6933 bool needs_null_check) {
6934 DCHECK(kEmitCompilerReadBarrier);
6935 DCHECK(kUseBakerReadBarrier);
6936
6937 static_assert(
6938 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6939 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006940 ScaleFactor scale_factor = TIMES_4;
6941
6942 if (kBakerReadBarrierThunksEnableForArrays) {
6943 // Note that we do not actually check the value of `GetIsGcMarking()`
6944 // to decide whether to mark the loaded reference or not. Instead, we
6945 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6946 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6947 // vice versa.
6948 //
6949 // We use thunks for the slow path. That thunk checks the reference
6950 // and jumps to the entrypoint if needed. If the holder is not gray,
6951 // it issues a load-load memory barrier and returns to the original
6952 // reference load.
6953 //
6954 // temp = Thread::Current()->pReadBarrierMarkReg00
6955 // // AKA &art_quick_read_barrier_mark_introspection.
6956 // if (temp != nullptr) {
6957 // temp = &field_array_thunk<holder_reg>
6958 // temp()
6959 // }
6960 // not_gray_return_address:
6961 // // The element address is pre-calculated in the TMP register before the
6962 // // thunk invocation and the thunk benefits from it.
6963 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6964 // gray_return_address:
6965
6966 DCHECK(temp.IsInvalid());
6967 DCHECK(index.IsValid());
6968 bool reordering = __ SetReorder(false);
6969 const int32_t entry_point_offset =
6970 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6971 // We will not do the explicit null check in the thunk as some form of a null check
6972 // must've been done earlier.
6973 DCHECK(!needs_null_check);
6974 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6975 // Loading the entrypoint does not require a load acquire since it is only changed when
6976 // threads are suspended or running a checkpoint.
6977 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6978 Register ref_reg = ref.AsRegister<Register>();
6979 Register index_reg = index.IsRegisterPair()
6980 ? index.AsRegisterPairLow<Register>()
6981 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006982 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006983 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006984 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006985 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6986 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006987 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006988 } else {
6989 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006990 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006991 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6992 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006993 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006994 __ Addu(TMP, TMP, obj); // In delay slot.
6995 }
6996 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6997 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6998 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6999 __ MaybeUnpoisonHeapReference(ref_reg);
7000 __ SetReorder(reordering);
7001 return;
7002 }
7003
Alexey Frunze15958152017-02-09 19:08:30 -08007004 // /* HeapReference<Object> */ ref =
7005 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08007006 GenerateReferenceLoadWithBakerReadBarrier(instruction,
7007 ref,
7008 obj,
7009 data_offset,
7010 index,
7011 scale_factor,
7012 temp,
7013 needs_null_check);
7014}
7015
7016void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
7017 Location ref,
7018 Register obj,
7019 uint32_t offset,
7020 Location index,
7021 ScaleFactor scale_factor,
7022 Location temp,
7023 bool needs_null_check,
7024 bool always_update_field) {
7025 DCHECK(kEmitCompilerReadBarrier);
7026 DCHECK(kUseBakerReadBarrier);
7027
7028 // In slow path based read barriers, the read barrier call is
7029 // inserted after the original load. However, in fast path based
7030 // Baker's read barriers, we need to perform the load of
7031 // mirror::Object::monitor_ *before* the original reference load.
7032 // This load-load ordering is required by the read barrier.
7033 // The fast path/slow path (for Baker's algorithm) should look like:
7034 //
7035 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
7036 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
7037 // HeapReference<Object> ref = *src; // Original reference load.
7038 // bool is_gray = (rb_state == ReadBarrier::GrayState());
7039 // if (is_gray) {
7040 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
7041 // }
7042 //
7043 // Note: the original implementation in ReadBarrier::Barrier is
7044 // slightly more complex as it performs additional checks that we do
7045 // not do here for performance reasons.
7046
7047 Register ref_reg = ref.AsRegister<Register>();
7048 Register temp_reg = temp.AsRegister<Register>();
7049 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
7050
7051 // /* int32_t */ monitor = obj->monitor_
7052 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
7053 if (needs_null_check) {
7054 MaybeRecordImplicitNullCheck(instruction);
7055 }
7056 // /* LockWord */ lock_word = LockWord(monitor)
7057 static_assert(sizeof(LockWord) == sizeof(int32_t),
7058 "art::LockWord and int32_t have different sizes.");
7059
7060 __ Sync(0); // Barrier to prevent load-load reordering.
7061
7062 // The actual reference load.
7063 if (index.IsValid()) {
7064 // Load types involving an "index": ArrayGet,
7065 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7066 // intrinsics.
7067 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
7068 if (index.IsConstant()) {
7069 size_t computed_offset =
7070 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
7071 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
7072 } else {
7073 // Handle the special case of the
7074 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
7075 // intrinsics, which use a register pair as index ("long
7076 // offset"), of which only the low part contains data.
7077 Register index_reg = index.IsRegisterPair()
7078 ? index.AsRegisterPairLow<Register>()
7079 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07007080 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08007081 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
7082 }
7083 } else {
7084 // /* HeapReference<Object> */ ref = *(obj + offset)
7085 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
7086 }
7087
7088 // Object* ref = ref_addr->AsMirrorPtr()
7089 __ MaybeUnpoisonHeapReference(ref_reg);
7090
7091 // Slow path marking the object `ref` when it is gray.
7092 SlowPathCodeMIPS* slow_path;
7093 if (always_update_field) {
7094 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
7095 // of the form `obj + field_offset`, where `obj` is a register and
7096 // `field_offset` is a register pair (of which only the lower half
7097 // is used). Thus `offset` and `scale_factor` above are expected
7098 // to be null in this code path.
7099 DCHECK_EQ(offset, 0u);
7100 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
Vladimir Marko174b2e22017-10-12 13:34:49 +01007101 slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007102 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
7103 ref,
7104 obj,
7105 /* field_offset */ index,
7106 temp_reg);
7107 } else {
Vladimir Marko174b2e22017-10-12 13:34:49 +01007108 slow_path = new (GetScopedAllocator()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
Alexey Frunze15958152017-02-09 19:08:30 -08007109 }
7110 AddSlowPath(slow_path);
7111
7112 // if (rb_state == ReadBarrier::GrayState())
7113 // ref = ReadBarrier::Mark(ref);
7114 // Given the numeric representation, it's enough to check the low bit of the
7115 // rb_state. We do that by shifting the bit into the sign bit (31) and
7116 // performing a branch on less than zero.
7117 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
7118 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
7119 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
7120 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
7121 __ Bltz(temp_reg, slow_path->GetEntryLabel());
7122 __ Bind(slow_path->GetExitLabel());
7123}
7124
7125void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
7126 Location out,
7127 Location ref,
7128 Location obj,
7129 uint32_t offset,
7130 Location index) {
7131 DCHECK(kEmitCompilerReadBarrier);
7132
7133 // Insert a slow path based read barrier *after* the reference load.
7134 //
7135 // If heap poisoning is enabled, the unpoisoning of the loaded
7136 // reference will be carried out by the runtime within the slow
7137 // path.
7138 //
7139 // Note that `ref` currently does not get unpoisoned (when heap
7140 // poisoning is enabled), which is alright as the `ref` argument is
7141 // not used by the artReadBarrierSlow entry point.
7142 //
7143 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
Vladimir Marko174b2e22017-10-12 13:34:49 +01007144 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator())
Alexey Frunze15958152017-02-09 19:08:30 -08007145 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7146 AddSlowPath(slow_path);
7147
7148 __ B(slow_path->GetEntryLabel());
7149 __ Bind(slow_path->GetExitLabel());
7150}
7151
7152void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7153 Location out,
7154 Location ref,
7155 Location obj,
7156 uint32_t offset,
7157 Location index) {
7158 if (kEmitCompilerReadBarrier) {
7159 // Baker's read barriers shall be handled by the fast path
7160 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7161 DCHECK(!kUseBakerReadBarrier);
7162 // If heap poisoning is enabled, unpoisoning will be taken care of
7163 // by the runtime within the slow path.
7164 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7165 } else if (kPoisonHeapReferences) {
7166 __ UnpoisonHeapReference(out.AsRegister<Register>());
7167 }
7168}
7169
7170void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7171 Location out,
7172 Location root) {
7173 DCHECK(kEmitCompilerReadBarrier);
7174
7175 // Insert a slow path based read barrier *after* the GC root load.
7176 //
7177 // Note that GC roots are not affected by heap poisoning, so we do
7178 // not need to do anything special for this here.
7179 SlowPathCodeMIPS* slow_path =
Vladimir Marko174b2e22017-10-12 13:34:49 +01007180 new (GetScopedAllocator()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
Alexey Frunze15958152017-02-09 19:08:30 -08007181 AddSlowPath(slow_path);
7182
7183 __ B(slow_path->GetEntryLabel());
7184 __ Bind(slow_path->GetExitLabel());
7185}
7186
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007187void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007188 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7189 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007190 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007191 switch (type_check_kind) {
7192 case TypeCheckKind::kExactCheck:
7193 case TypeCheckKind::kAbstractClassCheck:
7194 case TypeCheckKind::kClassHierarchyCheck:
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007195 case TypeCheckKind::kArrayObjectCheck: {
7196 bool needs_read_barrier = CodeGenerator::InstanceOfNeedsReadBarrier(instruction);
7197 call_kind = needs_read_barrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
7198 baker_read_barrier_slow_path = kUseBakerReadBarrier && needs_read_barrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007199 break;
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007200 }
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007201 case TypeCheckKind::kArrayCheck:
7202 case TypeCheckKind::kUnresolvedCheck:
7203 case TypeCheckKind::kInterfaceCheck:
7204 call_kind = LocationSummary::kCallOnSlowPath;
7205 break;
7206 }
7207
Vladimir Markoca6fff82017-10-03 14:49:14 +01007208 LocationSummary* locations =
7209 new (GetGraph()->GetAllocator()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007210 if (baker_read_barrier_slow_path) {
7211 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7212 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007213 locations->SetInAt(0, Location::RequiresRegister());
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007214 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007215 // The output does overlap inputs.
7216 // Note that TypeCheckSlowPathMIPS uses this register too.
7217 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007218 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007219}
7220
7221void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007222 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007223 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007224 Location obj_loc = locations->InAt(0);
7225 Register obj = obj_loc.AsRegister<Register>();
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007226 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007227 Location out_loc = locations->Out();
7228 Register out = out_loc.AsRegister<Register>();
7229 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7230 DCHECK_LE(num_temps, 1u);
7231 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007232 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7233 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7234 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7235 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007236 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007237 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007238
7239 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007240 // Avoid this check if we know `obj` is not null.
7241 if (instruction->MustDoNullCheck()) {
7242 __ Move(out, ZERO);
7243 __ Beqz(obj, &done);
7244 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007245
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007246 switch (type_check_kind) {
7247 case TypeCheckKind::kExactCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007248 ReadBarrierOption read_barrier_option =
7249 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007250 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007251 GenerateReferenceLoadTwoRegisters(instruction,
7252 out_loc,
7253 obj_loc,
7254 class_offset,
7255 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007256 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007257 // Classes must be equal for the instanceof to succeed.
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007258 __ Xor(out, out, cls);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007259 __ Sltiu(out, out, 1);
7260 break;
7261 }
7262
7263 case TypeCheckKind::kAbstractClassCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007264 ReadBarrierOption read_barrier_option =
7265 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007266 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007267 GenerateReferenceLoadTwoRegisters(instruction,
7268 out_loc,
7269 obj_loc,
7270 class_offset,
7271 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007272 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007273 // If the class is abstract, we eagerly fetch the super class of the
7274 // object to avoid doing a comparison we know will fail.
7275 MipsLabel loop;
7276 __ Bind(&loop);
7277 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007278 GenerateReferenceLoadOneRegister(instruction,
7279 out_loc,
7280 super_offset,
7281 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007282 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007283 // If `out` is null, we use it for the result, and jump to `done`.
7284 __ Beqz(out, &done);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007285 __ Bne(out, cls, &loop);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007286 __ LoadConst32(out, 1);
7287 break;
7288 }
7289
7290 case TypeCheckKind::kClassHierarchyCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007291 ReadBarrierOption read_barrier_option =
7292 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007293 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007294 GenerateReferenceLoadTwoRegisters(instruction,
7295 out_loc,
7296 obj_loc,
7297 class_offset,
7298 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007299 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007300 // Walk over the class hierarchy to find a match.
7301 MipsLabel loop, success;
7302 __ Bind(&loop);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007303 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007304 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007305 GenerateReferenceLoadOneRegister(instruction,
7306 out_loc,
7307 super_offset,
7308 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007309 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007310 __ Bnez(out, &loop);
7311 // If `out` is null, we use it for the result, and jump to `done`.
7312 __ B(&done);
7313 __ Bind(&success);
7314 __ LoadConst32(out, 1);
7315 break;
7316 }
7317
7318 case TypeCheckKind::kArrayObjectCheck: {
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007319 ReadBarrierOption read_barrier_option =
7320 CodeGenerator::ReadBarrierOptionForInstanceOf(instruction);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007321 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007322 GenerateReferenceLoadTwoRegisters(instruction,
7323 out_loc,
7324 obj_loc,
7325 class_offset,
7326 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007327 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007328 // Do an exact check.
7329 MipsLabel success;
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007330 __ Beq(out, cls, &success);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007331 // Otherwise, we need to check that the object's class is a non-primitive array.
7332 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007333 GenerateReferenceLoadOneRegister(instruction,
7334 out_loc,
7335 component_offset,
7336 maybe_temp_loc,
Alexey Frunzedfc30af2018-01-24 16:25:10 -08007337 read_barrier_option);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007338 // If `out` is null, we use it for the result, and jump to `done`.
7339 __ Beqz(out, &done);
7340 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7341 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7342 __ Sltiu(out, out, 1);
7343 __ B(&done);
7344 __ Bind(&success);
7345 __ LoadConst32(out, 1);
7346 break;
7347 }
7348
7349 case TypeCheckKind::kArrayCheck: {
7350 // No read barrier since the slow path will retry upon failure.
7351 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007352 GenerateReferenceLoadTwoRegisters(instruction,
7353 out_loc,
7354 obj_loc,
7355 class_offset,
7356 maybe_temp_loc,
7357 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007358 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007359 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7360 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007361 codegen_->AddSlowPath(slow_path);
Nicolas Geoffraybff7a522018-01-25 13:33:07 +00007362 __ Bne(out, cls, slow_path->GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007363 __ LoadConst32(out, 1);
7364 break;
7365 }
7366
7367 case TypeCheckKind::kUnresolvedCheck:
7368 case TypeCheckKind::kInterfaceCheck: {
7369 // Note that we indeed only call on slow path, but we always go
7370 // into the slow path for the unresolved and interface check
7371 // cases.
7372 //
7373 // We cannot directly call the InstanceofNonTrivial runtime
7374 // entry point without resorting to a type checking slow path
7375 // here (i.e. by calling InvokeRuntime directly), as it would
7376 // require to assign fixed registers for the inputs of this
7377 // HInstanceOf instruction (following the runtime calling
7378 // convention), which might be cluttered by the potential first
7379 // read barrier emission at the beginning of this method.
7380 //
7381 // TODO: Introduce a new runtime entry point taking the object
7382 // to test (instead of its class) as argument, and let it deal
7383 // with the read barrier issues. This will let us refactor this
7384 // case of the `switch` code as it was previously (with a direct
7385 // call to the runtime not using a type checking slow path).
7386 // This should also be beneficial for the other cases above.
7387 DCHECK(locations->OnlyCallsOnSlowPath());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007388 slow_path = new (codegen_->GetScopedAllocator()) TypeCheckSlowPathMIPS(
7389 instruction, /* is_fatal */ false);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007390 codegen_->AddSlowPath(slow_path);
7391 __ B(slow_path->GetEntryLabel());
7392 break;
7393 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007394 }
7395
7396 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007397
7398 if (slow_path != nullptr) {
7399 __ Bind(slow_path->GetExitLabel());
7400 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007401}
7402
7403void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007404 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007405 locations->SetOut(Location::ConstantLocation(constant));
7406}
7407
7408void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7409 // Will be generated at use site.
7410}
7411
7412void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007413 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007414 locations->SetOut(Location::ConstantLocation(constant));
7415}
7416
7417void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7418 // Will be generated at use site.
7419}
7420
7421void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7422 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7423 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7424}
7425
7426void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7427 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007428 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007429 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007430 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007431}
7432
7433void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7434 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7435 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007436 Location receiver = invoke->GetLocations()->InAt(0);
7437 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007438 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007439
7440 // Set the hidden argument.
7441 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7442 invoke->GetDexMethodIndex());
7443
7444 // temp = object->GetClass();
7445 if (receiver.IsStackSlot()) {
7446 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7447 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7448 } else {
7449 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7450 }
7451 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007452 // Instead of simply (possibly) unpoisoning `temp` here, we should
7453 // emit a read barrier for the previous class reference load.
7454 // However this is not required in practice, as this is an
7455 // intermediate/temporary reference and because the current
7456 // concurrent copying collector keeps the from-space memory
7457 // intact/accessible until the end of the marking phase (the
7458 // concurrent copying collector may not in the future).
7459 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007460 __ LoadFromOffset(kLoadWord, temp, temp,
7461 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7462 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007463 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007464 // temp = temp->GetImtEntryAt(method_offset);
7465 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7466 // T9 = temp->GetEntryPoint();
7467 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7468 // T9();
7469 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007470 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007471 DCHECK(!codegen_->IsLeafMethod());
7472 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7473}
7474
7475void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007476 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7477 if (intrinsic.TryDispatch(invoke)) {
7478 return;
7479 }
7480
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007481 HandleInvoke(invoke);
7482}
7483
7484void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007485 // Explicit clinit checks triggered by static invokes must have been pruned by
7486 // art::PrepareForRegisterAllocation.
7487 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007488
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007489 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007490 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7491 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007492
Chris Larsen701566a2015-10-27 15:29:13 -07007493 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7494 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007495 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7496 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7497 }
Chris Larsen701566a2015-10-27 15:29:13 -07007498 return;
7499 }
7500
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007501 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007502
7503 // Add the extra input register if either the dex cache array base register
7504 // or the PC-relative base register for accessing literals is needed.
7505 if (has_extra_input) {
7506 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7507 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007508}
7509
Orion Hodsonac141392017-01-13 11:53:47 +00007510void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7511 HandleInvoke(invoke);
7512}
7513
7514void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7515 codegen_->GenerateInvokePolymorphicCall(invoke);
7516}
7517
Chris Larsen701566a2015-10-27 15:29:13 -07007518static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007519 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007520 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7521 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007522 return true;
7523 }
7524 return false;
7525}
7526
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007527HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007528 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007529 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007530 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007531 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007532 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007533 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007534 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007535 case HLoadString::LoadKind::kJitTableAddress:
7536 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007537 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007538 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007539 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007540 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007541 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007542 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007543}
7544
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007545HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7546 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007547 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007548 case HLoadClass::LoadKind::kInvalid:
7549 LOG(FATAL) << "UNREACHABLE";
7550 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007551 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007552 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007553 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007554 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007555 case HLoadClass::LoadKind::kBssEntry:
7556 DCHECK(!Runtime::Current()->UseJitCompilation());
7557 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007558 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007559 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007560 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007561 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007562 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007563 break;
7564 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007565 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007566}
7567
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007568Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7569 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007570 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007571 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007572 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7573 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7574 if (!invoke->GetLocations()->Intrinsified()) {
7575 return location.AsRegister<Register>();
7576 }
7577 // For intrinsics we allow any location, so it may be on the stack.
7578 if (!location.IsRegister()) {
7579 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7580 return temp;
7581 }
7582 // For register locations, check if the register was saved. If so, get it from the stack.
7583 // Note: There is a chance that the register was saved but not overwritten, so we could
7584 // save one load. However, since this is just an intrinsic slow path we prefer this
7585 // simple and more robust approach rather that trying to determine if that's the case.
7586 SlowPathCode* slow_path = GetCurrentSlowPath();
7587 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7588 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7589 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7590 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7591 return temp;
7592 }
7593 return location.AsRegister<Register>();
7594}
7595
Vladimir Markodc151b22015-10-15 18:02:30 +01007596HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7597 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007598 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007599 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007600}
7601
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007602void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7603 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007604 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007605 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007606 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7607 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007608 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007609 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7610 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007611 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7612 : ZERO;
7613
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007614 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007615 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007616 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007617 uint32_t offset =
7618 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007619 __ LoadFromOffset(kLoadWord,
7620 temp.AsRegister<Register>(),
7621 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007622 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007623 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007624 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007625 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007626 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007627 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007628 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7629 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007630 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7631 PcRelativePatchInfo* info_low =
7632 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007633 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007634 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7635 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007636 break;
7637 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007638 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7639 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7640 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007641 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007642 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007643 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007644 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7645 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007646 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007647 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7648 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007649 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007650 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007651 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7652 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7653 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007654 }
7655 }
7656
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007657 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007658 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007659 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007660 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007661 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7662 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007663 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007664 T9,
7665 callee_method.AsRegister<Register>(),
7666 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007667 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007668 // T9()
7669 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007670 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007671 break;
7672 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007673 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7674
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007675 DCHECK(!IsLeafMethod());
7676}
7677
7678void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007679 // Explicit clinit checks triggered by static invokes must have been pruned by
7680 // art::PrepareForRegisterAllocation.
7681 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007682
7683 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7684 return;
7685 }
7686
7687 LocationSummary* locations = invoke->GetLocations();
7688 codegen_->GenerateStaticOrDirectCall(invoke,
7689 locations->HasTemps()
7690 ? locations->GetTemp(0)
7691 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007692}
7693
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007694void CodeGeneratorMIPS::GenerateVirtualCall(
7695 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007696 // Use the calling convention instead of the location of the receiver, as
7697 // intrinsics may have put the receiver in a different register. In the intrinsics
7698 // slow path, the arguments have been moved to the right place, so here we are
7699 // guaranteed that the receiver is the first register of the calling convention.
7700 InvokeDexCallingConvention calling_convention;
7701 Register receiver = calling_convention.GetRegisterAt(0);
7702
Chris Larsen3acee732015-11-18 13:31:08 -08007703 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007704 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7705 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7706 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007707 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007708
7709 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007710 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007711 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007712 // Instead of simply (possibly) unpoisoning `temp` here, we should
7713 // emit a read barrier for the previous class reference load.
7714 // However this is not required in practice, as this is an
7715 // intermediate/temporary reference and because the current
7716 // concurrent copying collector keeps the from-space memory
7717 // intact/accessible until the end of the marking phase (the
7718 // concurrent copying collector may not in the future).
7719 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007720 // temp = temp->GetMethodAt(method_offset);
7721 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7722 // T9 = temp->GetEntryPoint();
7723 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7724 // T9();
7725 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007726 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007727 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007728}
7729
7730void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7731 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7732 return;
7733 }
7734
7735 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007736 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007737}
7738
7739void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007740 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007741 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007742 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007743 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7744 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007745 return;
7746 }
Vladimir Marko41559982017-01-06 14:04:23 +00007747 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007748 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007749 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007750 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7751 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007752 ? LocationSummary::kCallOnSlowPath
7753 : LocationSummary::kNoCall;
Vladimir Markoca6fff82017-10-03 14:49:14 +01007754 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007755 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7756 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7757 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007758 switch (load_kind) {
7759 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007760 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007761 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007762 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007763 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007764 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007765 break;
7766 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007767 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007768 if (load_kind != HLoadClass::LoadKind::kBootImageAddress) {
7769 codegen_->ClobberRA();
7770 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007771 break;
7772 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007773 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007774 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007775 locations->SetInAt(0, Location::RequiresRegister());
7776 break;
7777 default:
7778 break;
7779 }
7780 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007781 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7782 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7783 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07007784 RegisterSet caller_saves = RegisterSet::Empty();
7785 InvokeRuntimeCallingConvention calling_convention;
7786 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7787 locations->SetCustomSlowPathCallerSaves(caller_saves);
7788 } else {
7789 // For non-Baker read barriers we have a temp-clobbering call.
7790 }
7791 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007792}
7793
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007794// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7795// move.
7796void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007797 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007798 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007799 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007800 return;
7801 }
Vladimir Marko41559982017-01-06 14:04:23 +00007802 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007803
Vladimir Marko41559982017-01-06 14:04:23 +00007804 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007805 Location out_loc = locations->Out();
7806 Register out = out_loc.AsRegister<Register>();
7807 Register base_or_current_method_reg;
7808 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007809 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007810 switch (load_kind) {
7811 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007812 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007813 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007814 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007815 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007816 base_or_current_method_reg =
7817 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007818 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007819 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007820 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007821 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7822 break;
7823 default:
7824 base_or_current_method_reg = ZERO;
7825 break;
7826 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007827
Alexey Frunze15958152017-02-09 19:08:30 -08007828 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7829 ? kWithoutReadBarrier
7830 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007831 bool generate_null_check = false;
7832 switch (load_kind) {
7833 case HLoadClass::LoadKind::kReferrersClass: {
7834 DCHECK(!cls->CanCallRuntime());
7835 DCHECK(!cls->MustGenerateClinitCheck());
7836 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7837 GenerateGcRootFieldLoad(cls,
7838 out_loc,
7839 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007840 ArtMethod::DeclaringClassOffset().Int32Value(),
7841 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007842 break;
7843 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007844 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007845 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007846 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007847 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007848 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007849 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7850 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007851 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7852 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007853 base_or_current_method_reg);
7854 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007855 break;
7856 }
7857 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007858 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007859 uint32_t address = dchecked_integral_cast<uint32_t>(
7860 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7861 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007862 if (isR6 || !has_irreducible_loops) {
7863 __ LoadLiteral(out,
7864 base_or_current_method_reg,
7865 codegen_->DeduplicateBootImageAddressLiteral(address));
7866 } else {
7867 __ LoadConst32(out, address);
7868 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007869 break;
7870 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007871 case HLoadClass::LoadKind::kBootImageClassTable: {
7872 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7873 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7874 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7875 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7876 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7877 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7878 out,
7879 base_or_current_method_reg);
7880 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7881 // Extract the reference from the slot data, i.e. clear the hash bits.
7882 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7883 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7884 if (masked_hash != 0) {
7885 __ Addiu(out, out, -masked_hash);
7886 }
7887 break;
7888 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007889 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Markof3c52b42017-11-17 17:32:12 +00007890 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high =
7891 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007892 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7893 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007894 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007895 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007896 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007897 GenerateGcRootFieldLoad(cls,
7898 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00007899 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007900 /* placeholder */ 0x5678,
7901 read_barrier_option,
7902 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007903 generate_null_check = true;
7904 break;
7905 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007906 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007907 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7908 cls->GetTypeIndex(),
7909 cls->GetClass());
7910 bool reordering = __ SetReorder(false);
7911 __ Bind(&info->high_label);
7912 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007913 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007914 GenerateGcRootFieldLoad(cls,
7915 out_loc,
7916 out,
7917 /* placeholder */ 0x5678,
7918 read_barrier_option,
7919 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007920 break;
7921 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007922 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007923 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007924 LOG(FATAL) << "UNREACHABLE";
7925 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007926 }
7927
7928 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7929 DCHECK(cls->CanCallRuntime());
Vladimir Marko174b2e22017-10-12 13:34:49 +01007930 SlowPathCodeMIPS* slow_path = new (codegen_->GetScopedAllocator()) LoadClassSlowPathMIPS(
Vladimir Markof3c52b42017-11-17 17:32:12 +00007931 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007932 codegen_->AddSlowPath(slow_path);
7933 if (generate_null_check) {
7934 __ Beqz(out, slow_path->GetEntryLabel());
7935 }
7936 if (cls->MustGenerateClinitCheck()) {
7937 GenerateClassInitializationCheck(slow_path, out);
7938 } else {
7939 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007940 }
7941 }
7942}
7943
7944static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007945 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007946}
7947
7948void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7949 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01007950 new (GetGraph()->GetAllocator()) LocationSummary(load, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007951 locations->SetOut(Location::RequiresRegister());
7952}
7953
7954void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7955 Register out = load->GetLocations()->Out().AsRegister<Register>();
7956 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7957}
7958
7959void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01007960 new (GetGraph()->GetAllocator()) LocationSummary(clear, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007961}
7962
7963void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7964 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7965}
7966
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007967void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007968 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Vladimir Markoca6fff82017-10-03 14:49:14 +01007969 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007970 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007971 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007972 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007973 switch (load_kind) {
7974 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007975 case HLoadString::LoadKind::kBootImageAddress:
7976 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007977 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007978 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007979 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007980 break;
7981 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007982 if (has_irreducible_loops) {
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07007983 if (load_kind != HLoadString::LoadKind::kBootImageAddress) {
7984 codegen_->ClobberRA();
7985 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007986 break;
7987 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007988 FALLTHROUGH_INTENDED;
7989 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007990 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007991 locations->SetInAt(0, Location::RequiresRegister());
7992 break;
7993 default:
7994 break;
7995 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007996 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007997 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007998 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007999 } else {
8000 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07008001 if (load_kind == HLoadString::LoadKind::kBssEntry) {
8002 if (!kUseReadBarrier || kUseBakerReadBarrier) {
8003 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunzec61c0762017-04-10 13:54:23 -07008004 RegisterSet caller_saves = RegisterSet::Empty();
8005 InvokeRuntimeCallingConvention calling_convention;
8006 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8007 locations->SetCustomSlowPathCallerSaves(caller_saves);
8008 } else {
8009 // For non-Baker read barriers we have a temp-clobbering call.
8010 }
8011 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07008012 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008013}
8014
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008015// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
8016// move.
8017void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008018 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008019 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008020 Location out_loc = locations->Out();
8021 Register out = out_loc.AsRegister<Register>();
8022 Register base_or_current_method_reg;
8023 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008024 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008025 switch (load_kind) {
8026 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07008027 case HLoadString::LoadKind::kBootImageAddress:
8028 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008029 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00008030 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008031 base_or_current_method_reg =
8032 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07008033 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008034 default:
8035 base_or_current_method_reg = ZERO;
8036 break;
8037 }
8038
8039 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07008040 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008041 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008042 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008043 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008044 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8045 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008046 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8047 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07008048 base_or_current_method_reg);
8049 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008050 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008051 }
8052 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00008053 uint32_t address = dchecked_integral_cast<uint32_t>(
8054 reinterpret_cast<uintptr_t>(load->GetString().Get()));
8055 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02008056 if (isR6 || !has_irreducible_loops) {
8057 __ LoadLiteral(out,
8058 base_or_current_method_reg,
8059 codegen_->DeduplicateBootImageAddressLiteral(address));
8060 } else {
8061 __ LoadConst32(out, address);
8062 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008063 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008064 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008065 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00008066 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008067 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00008068 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008069 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8070 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01008071 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
8072 out,
8073 base_or_current_method_reg);
8074 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
8075 return;
8076 }
8077 case HLoadString::LoadKind::kBssEntry: {
8078 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
8079 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
8080 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
8081 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
8082 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008083 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008084 out,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008085 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008086 GenerateGcRootFieldLoad(load,
8087 out_loc,
Vladimir Markof3c52b42017-11-17 17:32:12 +00008088 out,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008089 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008090 kCompilerReadBarrierOption,
8091 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07008092 SlowPathCodeMIPS* slow_path =
Vladimir Markof3c52b42017-11-17 17:32:12 +00008093 new (codegen_->GetScopedAllocator()) LoadStringSlowPathMIPS(load);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008094 codegen_->AddSlowPath(slow_path);
8095 __ Beqz(out, slow_path->GetEntryLabel());
8096 __ Bind(slow_path->GetExitLabel());
8097 return;
8098 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08008099 case HLoadString::LoadKind::kJitTableAddress: {
8100 CodeGeneratorMIPS::JitPatchInfo* info =
8101 codegen_->NewJitRootStringPatch(load->GetDexFile(),
8102 load->GetStringIndex(),
8103 load->GetString());
8104 bool reordering = __ SetReorder(false);
8105 __ Bind(&info->high_label);
8106 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008107 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08008108 GenerateGcRootFieldLoad(load,
8109 out_loc,
8110 out,
8111 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07008112 kCompilerReadBarrierOption,
8113 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08008114 return;
8115 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07008116 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008117 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07008118 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00008119
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07008120 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01008121 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008122 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07008123 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08008124 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00008125 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
8126 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008127}
8128
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008129void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008130 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(constant);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008131 locations->SetOut(Location::ConstantLocation(constant));
8132}
8133
8134void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8135 // Will be generated at use site.
8136}
8137
8138void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008139 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8140 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008141 InvokeRuntimeCallingConvention calling_convention;
8142 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8143}
8144
8145void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8146 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008147 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008148 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8149 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008150 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008151 }
8152 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8153}
8154
8155void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8156 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008157 new (GetGraph()->GetAllocator()) LocationSummary(mul, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008158 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008159 case DataType::Type::kInt32:
8160 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008161 locations->SetInAt(0, Location::RequiresRegister());
8162 locations->SetInAt(1, Location::RequiresRegister());
8163 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8164 break;
8165
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008166 case DataType::Type::kFloat32:
8167 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008168 locations->SetInAt(0, Location::RequiresFpuRegister());
8169 locations->SetInAt(1, Location::RequiresFpuRegister());
8170 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8171 break;
8172
8173 default:
8174 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8175 }
8176}
8177
8178void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008179 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008180 LocationSummary* locations = instruction->GetLocations();
8181 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8182
8183 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008184 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008185 Register dst = locations->Out().AsRegister<Register>();
8186 Register lhs = locations->InAt(0).AsRegister<Register>();
8187 Register rhs = locations->InAt(1).AsRegister<Register>();
8188
8189 if (isR6) {
8190 __ MulR6(dst, lhs, rhs);
8191 } else {
8192 __ MulR2(dst, lhs, rhs);
8193 }
8194 break;
8195 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008196 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008197 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8198 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8199 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8200 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8201 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8202 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8203
8204 // Extra checks to protect caused by the existance of A1_A2.
8205 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8206 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8207 DCHECK_NE(dst_high, lhs_low);
8208 DCHECK_NE(dst_high, rhs_low);
8209
8210 // A_B * C_D
8211 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8212 // dst_lo: [ low(B*D) ]
8213 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8214
8215 if (isR6) {
8216 __ MulR6(TMP, lhs_high, rhs_low);
8217 __ MulR6(dst_high, lhs_low, rhs_high);
8218 __ Addu(dst_high, dst_high, TMP);
8219 __ MuhuR6(TMP, lhs_low, rhs_low);
8220 __ Addu(dst_high, dst_high, TMP);
8221 __ MulR6(dst_low, lhs_low, rhs_low);
8222 } else {
8223 __ MulR2(TMP, lhs_high, rhs_low);
8224 __ MulR2(dst_high, lhs_low, rhs_high);
8225 __ Addu(dst_high, dst_high, TMP);
8226 __ MultuR2(lhs_low, rhs_low);
8227 __ Mfhi(TMP);
8228 __ Addu(dst_high, dst_high, TMP);
8229 __ Mflo(dst_low);
8230 }
8231 break;
8232 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008233 case DataType::Type::kFloat32:
8234 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008235 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8236 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8237 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008238 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008239 __ MulS(dst, lhs, rhs);
8240 } else {
8241 __ MulD(dst, lhs, rhs);
8242 }
8243 break;
8244 }
8245 default:
8246 LOG(FATAL) << "Unexpected mul type " << type;
8247 }
8248}
8249
8250void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8251 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008252 new (GetGraph()->GetAllocator()) LocationSummary(neg, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008253 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008254 case DataType::Type::kInt32:
8255 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008256 locations->SetInAt(0, Location::RequiresRegister());
8257 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8258 break;
8259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008260 case DataType::Type::kFloat32:
8261 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008262 locations->SetInAt(0, Location::RequiresFpuRegister());
8263 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8264 break;
8265
8266 default:
8267 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8268 }
8269}
8270
8271void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008272 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008273 LocationSummary* locations = instruction->GetLocations();
8274
8275 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008276 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008277 Register dst = locations->Out().AsRegister<Register>();
8278 Register src = locations->InAt(0).AsRegister<Register>();
8279 __ Subu(dst, ZERO, src);
8280 break;
8281 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008282 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008283 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8284 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8285 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8286 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8287 __ Subu(dst_low, ZERO, src_low);
8288 __ Sltu(TMP, ZERO, dst_low);
8289 __ Subu(dst_high, ZERO, src_high);
8290 __ Subu(dst_high, dst_high, TMP);
8291 break;
8292 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008293 case DataType::Type::kFloat32:
8294 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008295 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8296 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008297 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008298 __ NegS(dst, src);
8299 } else {
8300 __ NegD(dst, src);
8301 }
8302 break;
8303 }
8304 default:
8305 LOG(FATAL) << "Unexpected neg type " << type;
8306 }
8307}
8308
8309void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008310 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8311 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008312 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008313 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008314 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8315 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008316}
8317
8318void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008319 // Note: if heap poisoning is enabled, the entry point takes care
8320 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008321 QuickEntrypointEnum entrypoint =
8322 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8323 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008324 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008325 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008326}
8327
8328void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008329 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8330 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008331 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008332 if (instruction->IsStringAlloc()) {
8333 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8334 } else {
8335 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008336 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008337 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008338}
8339
8340void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008341 // Note: if heap poisoning is enabled, the entry point takes care
8342 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008343 if (instruction->IsStringAlloc()) {
8344 // String is allocated through StringFactory. Call NewEmptyString entry point.
8345 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008346 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008347 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8348 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8349 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008350 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008351 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8352 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008353 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008354 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008355 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008356}
8357
8358void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008359 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008360 locations->SetInAt(0, Location::RequiresRegister());
8361 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8362}
8363
8364void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008365 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008366 LocationSummary* locations = instruction->GetLocations();
8367
8368 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008369 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008370 Register dst = locations->Out().AsRegister<Register>();
8371 Register src = locations->InAt(0).AsRegister<Register>();
8372 __ Nor(dst, src, ZERO);
8373 break;
8374 }
8375
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008376 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008377 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8378 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8379 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8380 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8381 __ Nor(dst_high, src_high, ZERO);
8382 __ Nor(dst_low, src_low, ZERO);
8383 break;
8384 }
8385
8386 default:
8387 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8388 }
8389}
8390
8391void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008392 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008393 locations->SetInAt(0, Location::RequiresRegister());
8394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8395}
8396
8397void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8398 LocationSummary* locations = instruction->GetLocations();
8399 __ Xori(locations->Out().AsRegister<Register>(),
8400 locations->InAt(0).AsRegister<Register>(),
8401 1);
8402}
8403
8404void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008405 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8406 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008407}
8408
Calin Juravle2ae48182016-03-16 14:05:09 +00008409void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8410 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008411 return;
8412 }
8413 Location obj = instruction->GetLocations()->InAt(0);
8414
8415 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008416 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008417}
8418
Calin Juravle2ae48182016-03-16 14:05:09 +00008419void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Vladimir Marko174b2e22017-10-12 13:34:49 +01008420 SlowPathCodeMIPS* slow_path = new (GetScopedAllocator()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008421 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008422
8423 Location obj = instruction->GetLocations()->InAt(0);
8424
8425 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8426}
8427
8428void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008429 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008430}
8431
8432void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8433 HandleBinaryOp(instruction);
8434}
8435
8436void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8437 HandleBinaryOp(instruction);
8438}
8439
8440void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8441 LOG(FATAL) << "Unreachable";
8442}
8443
8444void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
Vladimir Markobea75ff2017-10-11 20:39:54 +01008445 if (instruction->GetNext()->IsSuspendCheck() &&
8446 instruction->GetBlock()->GetLoopInformation() != nullptr) {
8447 HSuspendCheck* suspend_check = instruction->GetNext()->AsSuspendCheck();
8448 // The back edge will generate the suspend check.
8449 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(suspend_check, instruction);
8450 }
8451
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008452 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8453}
8454
8455void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008456 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008457 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8458 if (location.IsStackSlot()) {
8459 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8460 } else if (location.IsDoubleStackSlot()) {
8461 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8462 }
8463 locations->SetOut(location);
8464}
8465
8466void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8467 ATTRIBUTE_UNUSED) {
8468 // Nothing to do, the parameter is already at its location.
8469}
8470
8471void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8472 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01008473 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008474 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8475}
8476
8477void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8478 ATTRIBUTE_UNUSED) {
8479 // Nothing to do, the method is already at its location.
8480}
8481
8482void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008483 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008484 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008485 locations->SetInAt(i, Location::Any());
8486 }
8487 locations->SetOut(Location::Any());
8488}
8489
8490void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8491 LOG(FATAL) << "Unreachable";
8492}
8493
8494void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008495 DataType::Type type = rem->GetResultType();
8496 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8497 ? LocationSummary::kNoCall
8498 : LocationSummary::kCallOnMainOnly;
Vladimir Markoca6fff82017-10-03 14:49:14 +01008499 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(rem, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008500
8501 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008502 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008503 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008504 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008505 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8506 break;
8507
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008508 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008509 InvokeRuntimeCallingConvention calling_convention;
8510 locations->SetInAt(0, Location::RegisterPairLocation(
8511 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8512 locations->SetInAt(1, Location::RegisterPairLocation(
8513 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8514 locations->SetOut(calling_convention.GetReturnLocation(type));
8515 break;
8516 }
8517
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008518 case DataType::Type::kFloat32:
8519 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008520 InvokeRuntimeCallingConvention calling_convention;
8521 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8522 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8523 locations->SetOut(calling_convention.GetReturnLocation(type));
8524 break;
8525 }
8526
8527 default:
8528 LOG(FATAL) << "Unexpected rem type " << type;
8529 }
8530}
8531
8532void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008533 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008534
8535 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008536 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008537 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008538 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008539 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008540 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008541 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8542 break;
8543 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008544 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008545 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008546 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008547 break;
8548 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008549 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008550 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008551 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008552 break;
8553 }
8554 default:
8555 LOG(FATAL) << "Unexpected rem type " << type;
8556 }
8557}
8558
Igor Murashkind01745e2017-04-05 16:40:31 -07008559void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8560 constructor_fence->SetLocations(nullptr);
8561}
8562
8563void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8564 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8565 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8566}
8567
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008568void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8569 memory_barrier->SetLocations(nullptr);
8570}
8571
8572void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8573 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8574}
8575
8576void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008577 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008578 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008579 locations->SetInAt(0, MipsReturnLocation(return_type));
8580}
8581
8582void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8583 codegen_->GenerateFrameExit();
8584}
8585
8586void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8587 ret->SetLocations(nullptr);
8588}
8589
8590void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8591 codegen_->GenerateFrameExit();
8592}
8593
Alexey Frunze92d90602015-12-18 18:16:36 -08008594void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8595 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008596}
8597
Alexey Frunze92d90602015-12-18 18:16:36 -08008598void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8599 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008600}
8601
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008602void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8603 HandleShift(shl);
8604}
8605
8606void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8607 HandleShift(shl);
8608}
8609
8610void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8611 HandleShift(shr);
8612}
8613
8614void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8615 HandleShift(shr);
8616}
8617
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008618void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8619 HandleBinaryOp(instruction);
8620}
8621
8622void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8623 HandleBinaryOp(instruction);
8624}
8625
8626void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8627 HandleFieldGet(instruction, instruction->GetFieldInfo());
8628}
8629
8630void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8631 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8632}
8633
8634void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8635 HandleFieldSet(instruction, instruction->GetFieldInfo());
8636}
8637
8638void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008639 HandleFieldSet(instruction,
8640 instruction->GetFieldInfo(),
8641 instruction->GetDexPc(),
8642 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008643}
8644
8645void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8646 HUnresolvedInstanceFieldGet* instruction) {
8647 FieldAccessCallingConventionMIPS calling_convention;
8648 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8649 instruction->GetFieldType(),
8650 calling_convention);
8651}
8652
8653void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8654 HUnresolvedInstanceFieldGet* instruction) {
8655 FieldAccessCallingConventionMIPS calling_convention;
8656 codegen_->GenerateUnresolvedFieldAccess(instruction,
8657 instruction->GetFieldType(),
8658 instruction->GetFieldIndex(),
8659 instruction->GetDexPc(),
8660 calling_convention);
8661}
8662
8663void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8664 HUnresolvedInstanceFieldSet* instruction) {
8665 FieldAccessCallingConventionMIPS calling_convention;
8666 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8667 instruction->GetFieldType(),
8668 calling_convention);
8669}
8670
8671void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8672 HUnresolvedInstanceFieldSet* instruction) {
8673 FieldAccessCallingConventionMIPS calling_convention;
8674 codegen_->GenerateUnresolvedFieldAccess(instruction,
8675 instruction->GetFieldType(),
8676 instruction->GetFieldIndex(),
8677 instruction->GetDexPc(),
8678 calling_convention);
8679}
8680
8681void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8682 HUnresolvedStaticFieldGet* instruction) {
8683 FieldAccessCallingConventionMIPS calling_convention;
8684 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8685 instruction->GetFieldType(),
8686 calling_convention);
8687}
8688
8689void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8690 HUnresolvedStaticFieldGet* instruction) {
8691 FieldAccessCallingConventionMIPS calling_convention;
8692 codegen_->GenerateUnresolvedFieldAccess(instruction,
8693 instruction->GetFieldType(),
8694 instruction->GetFieldIndex(),
8695 instruction->GetDexPc(),
8696 calling_convention);
8697}
8698
8699void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8700 HUnresolvedStaticFieldSet* instruction) {
8701 FieldAccessCallingConventionMIPS calling_convention;
8702 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8703 instruction->GetFieldType(),
8704 calling_convention);
8705}
8706
8707void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8708 HUnresolvedStaticFieldSet* instruction) {
8709 FieldAccessCallingConventionMIPS calling_convention;
8710 codegen_->GenerateUnresolvedFieldAccess(instruction,
8711 instruction->GetFieldType(),
8712 instruction->GetFieldIndex(),
8713 instruction->GetDexPc(),
8714 calling_convention);
8715}
8716
8717void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008718 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8719 instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008720 // In suspend check slow path, usually there are no caller-save registers at all.
8721 // If SIMD instructions are present, however, we force spilling all live SIMD
8722 // registers in full width (since the runtime only saves/restores lower part).
8723 locations->SetCustomSlowPathCallerSaves(
8724 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008725}
8726
8727void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8728 HBasicBlock* block = instruction->GetBlock();
8729 if (block->GetLoopInformation() != nullptr) {
8730 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8731 // The back edge will generate the suspend check.
8732 return;
8733 }
8734 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8735 // The goto will generate the suspend check.
8736 return;
8737 }
8738 GenerateSuspendCheck(instruction, nullptr);
8739}
8740
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008741void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
Vladimir Markoca6fff82017-10-03 14:49:14 +01008742 LocationSummary* locations = new (GetGraph()->GetAllocator()) LocationSummary(
8743 instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008744 InvokeRuntimeCallingConvention calling_convention;
8745 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8746}
8747
8748void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008749 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008750 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8751}
8752
8753void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008754 DataType::Type input_type = conversion->GetInputType();
8755 DataType::Type result_type = conversion->GetResultType();
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008756 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8757 << input_type << " -> " << result_type;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008758 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008760 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8761 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008762 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8763 }
8764
8765 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008766 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008767 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8768 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008769 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008770 }
8771
Vladimir Markoca6fff82017-10-03 14:49:14 +01008772 LocationSummary* locations =
8773 new (GetGraph()->GetAllocator()) LocationSummary(conversion, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008774
8775 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008776 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008777 locations->SetInAt(0, Location::RequiresFpuRegister());
8778 } else {
8779 locations->SetInAt(0, Location::RequiresRegister());
8780 }
8781
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008782 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008783 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8784 } else {
8785 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8786 }
8787 } else {
8788 InvokeRuntimeCallingConvention calling_convention;
8789
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008790 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008791 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8792 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008793 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008794 locations->SetInAt(0, Location::RegisterPairLocation(
8795 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8796 }
8797
8798 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8799 }
8800}
8801
8802void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8803 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008804 DataType::Type result_type = conversion->GetResultType();
8805 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008806 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008807 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008808
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008809 DCHECK(!DataType::IsTypeConversionImplicit(input_type, result_type))
8810 << input_type << " -> " << result_type;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008811
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008812 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008813 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8814 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8815 Register src = locations->InAt(0).AsRegister<Register>();
8816
Alexey Frunzea871ef12016-06-27 15:20:11 -07008817 if (dst_low != src) {
8818 __ Move(dst_low, src);
8819 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008820 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008821 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008822 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008823 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008824 ? locations->InAt(0).AsRegisterPairLow<Register>()
8825 : locations->InAt(0).AsRegister<Register>();
8826
8827 switch (result_type) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008828 case DataType::Type::kUint8:
8829 __ Andi(dst, src, 0xFF);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008830 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008831 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008832 if (has_sign_extension) {
8833 __ Seb(dst, src);
8834 } else {
8835 __ Sll(dst, src, 24);
8836 __ Sra(dst, dst, 24);
8837 }
8838 break;
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01008839 case DataType::Type::kUint16:
8840 __ Andi(dst, src, 0xFFFF);
8841 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008842 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008843 if (has_sign_extension) {
8844 __ Seh(dst, src);
8845 } else {
8846 __ Sll(dst, src, 16);
8847 __ Sra(dst, dst, 16);
8848 }
8849 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008850 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008851 if (dst != src) {
8852 __ Move(dst, src);
8853 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008854 break;
8855
8856 default:
8857 LOG(FATAL) << "Unexpected type conversion from " << input_type
8858 << " to " << result_type;
8859 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008860 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8861 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008862 if (isR6) {
8863 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8864 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8865 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8866 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8867 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8868 __ Mtc1(src_low, FTMP);
8869 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008870 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008871 __ Cvtsl(dst, FTMP);
8872 } else {
8873 __ Cvtdl(dst, FTMP);
8874 }
8875 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008876 QuickEntrypointEnum entrypoint =
8877 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008878 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008879 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008880 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8881 } else {
8882 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8883 }
8884 }
8885 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008886 Register src = locations->InAt(0).AsRegister<Register>();
8887 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8888 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008889 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008890 __ Cvtsw(dst, FTMP);
8891 } else {
8892 __ Cvtdw(dst, FTMP);
8893 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008894 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008895 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8896 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008897
8898 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8899 // value of the output type if the input is outside of the range after the truncation or
8900 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8901 // results. This matches the desired float/double-to-int/long conversion exactly.
8902 //
8903 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8904 // value when the input is either a NaN or is outside of the range of the output type
8905 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8906 // the same result.
8907 //
8908 // The code takes care of the different behaviors by first comparing the input to the
8909 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8910 // If the input is greater than or equal to the minimum, it procedes to the truncate
8911 // instruction, which will handle such an input the same way irrespective of NAN2008.
8912 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8913 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008914 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008915 if (isR6) {
8916 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8917 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8918 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8919 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8920 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008921
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008922 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008923 __ TruncLS(FTMP, src);
8924 } else {
8925 __ TruncLD(FTMP, src);
8926 }
8927 __ Mfc1(dst_low, FTMP);
8928 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008929 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008930 QuickEntrypointEnum entrypoint =
8931 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008932 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008933 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008934 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8935 } else {
8936 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8937 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008938 }
8939 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008940 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8941 Register dst = locations->Out().AsRegister<Register>();
8942 MipsLabel truncate;
8943 MipsLabel done;
8944
Lena Djokicf4e23a82017-05-09 15:43:45 +02008945 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008946 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008947 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8948 __ LoadConst32(TMP, min_val);
8949 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008950 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008951 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8952 __ LoadConst32(TMP, High32Bits(min_val));
8953 __ Mtc1(ZERO, FTMP);
8954 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008955 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008956
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008957 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008958 __ ColeS(0, FTMP, src);
8959 } else {
8960 __ ColeD(0, FTMP, src);
8961 }
8962 __ Bc1t(0, &truncate);
8963
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008964 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008965 __ CeqS(0, src, src);
8966 } else {
8967 __ CeqD(0, src, src);
8968 }
8969 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8970 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008971
8972 __ B(&done);
8973
8974 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008975 }
8976
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008977 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008978 __ TruncWS(FTMP, src);
8979 } else {
8980 __ TruncWD(FTMP, src);
8981 }
8982 __ Mfc1(dst, FTMP);
8983
Lena Djokicf4e23a82017-05-09 15:43:45 +02008984 if (!isR6) {
8985 __ Bind(&done);
8986 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008987 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008988 } else if (DataType::IsFloatingPointType(result_type) &&
8989 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008990 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8991 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008992 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008993 __ Cvtsd(dst, src);
8994 } else {
8995 __ Cvtds(dst, src);
8996 }
8997 } else {
8998 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8999 << " to " << result_type;
9000 }
9001}
9002
9003void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
9004 HandleShift(ushr);
9005}
9006
9007void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
9008 HandleShift(ushr);
9009}
9010
9011void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
9012 HandleBinaryOp(instruction);
9013}
9014
9015void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
9016 HandleBinaryOp(instruction);
9017}
9018
9019void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9020 // Nothing to do, this should be removed during prepare for register allocator.
9021 LOG(FATAL) << "Unreachable";
9022}
9023
9024void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
9025 // Nothing to do, this should be removed during prepare for register allocator.
9026 LOG(FATAL) << "Unreachable";
9027}
9028
9029void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009030 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009031}
9032
9033void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009034 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009035}
9036
9037void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009038 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009039}
9040
9041void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009042 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009043}
9044
9045void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009046 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009047}
9048
9049void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009050 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009051}
9052
9053void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009054 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009055}
9056
9057void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009058 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009059}
9060
9061void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009062 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009063}
9064
9065void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009066 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009067}
9068
9069void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009070 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009071}
9072
9073void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009074 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009075}
9076
9077void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009078 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009079}
9080
9081void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009082 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009083}
9084
9085void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009086 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009087}
9088
9089void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009090 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009091}
9092
9093void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009094 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009095}
9096
9097void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009098 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009099}
9100
9101void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009102 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009103}
9104
9105void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00009106 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009107}
9108
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009109void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9110 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009111 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009112 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009113 if (!codegen_->GetInstructionSetFeatures().IsR6()) {
9114 uint32_t num_entries = switch_instr->GetNumEntries();
9115 if (num_entries > InstructionCodeGeneratorMIPS::kPackedSwitchJumpTableThreshold) {
9116 // When there's no HMipsComputeBaseMethodAddress input, R2 uses the NAL
9117 // instruction to simulate PC-relative addressing when accessing the jump table.
9118 // NAL clobbers RA. Make sure RA is preserved.
9119 codegen_->ClobberRA();
9120 }
9121 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009122}
9123
Alexey Frunze96b66822016-09-10 02:32:44 -07009124void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
9125 int32_t lower_bound,
9126 uint32_t num_entries,
9127 HBasicBlock* switch_block,
9128 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009129 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009130 Register temp_reg = TMP;
9131 __ Addiu32(temp_reg, value_reg, -lower_bound);
9132 // Jump to default if index is negative
9133 // Note: We don't check the case that index is positive while value < lower_bound, because in
9134 // this case, index >= num_entries must be true. So that we can save one branch instruction.
9135 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
9136
Alexey Frunze96b66822016-09-10 02:32:44 -07009137 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009138 // Jump to successors[0] if value == lower_bound.
9139 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
9140 int32_t last_index = 0;
9141 for (; num_entries - last_index > 2; last_index += 2) {
9142 __ Addiu(temp_reg, temp_reg, -2);
9143 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
9144 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
9145 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
9146 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
9147 }
9148 if (num_entries - last_index == 2) {
9149 // The last missing case_value.
9150 __ Addiu(temp_reg, temp_reg, -1);
9151 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009152 }
9153
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009154 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009155 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009156 __ B(codegen_->GetLabelOf(default_block));
9157 }
9158}
9159
Alexey Frunze96b66822016-09-10 02:32:44 -07009160void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9161 Register constant_area,
9162 int32_t lower_bound,
9163 uint32_t num_entries,
9164 HBasicBlock* switch_block,
9165 HBasicBlock* default_block) {
9166 // Create a jump table.
9167 std::vector<MipsLabel*> labels(num_entries);
9168 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9169 for (uint32_t i = 0; i < num_entries; i++) {
9170 labels[i] = codegen_->GetLabelOf(successors[i]);
9171 }
9172 JumpTable* table = __ CreateJumpTable(std::move(labels));
9173
9174 // Is the value in range?
9175 __ Addiu32(TMP, value_reg, -lower_bound);
9176 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9177 __ Sltiu(AT, TMP, num_entries);
9178 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9179 } else {
9180 __ LoadConst32(AT, num_entries);
9181 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9182 }
9183
9184 // We are in the range of the table.
9185 // Load the target address from the jump table, indexing by the value.
9186 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009187 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009188 __ Lw(TMP, TMP, 0);
9189 // Compute the absolute target address by adding the table start address
9190 // (the table contains offsets to targets relative to its start).
9191 __ Addu(TMP, TMP, AT);
9192 // And jump.
9193 __ Jr(TMP);
9194 __ NopIfNoReordering();
9195}
9196
9197void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9198 int32_t lower_bound = switch_instr->GetStartValue();
9199 uint32_t num_entries = switch_instr->GetNumEntries();
9200 LocationSummary* locations = switch_instr->GetLocations();
9201 Register value_reg = locations->InAt(0).AsRegister<Register>();
9202 HBasicBlock* switch_block = switch_instr->GetBlock();
9203 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9204
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009205 if (num_entries > kPackedSwitchJumpTableThreshold) {
Alexey Frunze96b66822016-09-10 02:32:44 -07009206 // R6 uses PC-relative addressing to access the jump table.
Alexey Frunze3b8c82f2017-10-10 23:01:34 -07009207 //
9208 // R2, OTOH, uses an HMipsComputeBaseMethodAddress input (when available)
9209 // to access the jump table and it is implemented by changing HPackedSwitch to
9210 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress (see
9211 // VisitMipsPackedSwitch()).
9212 //
9213 // When there's no HMipsComputeBaseMethodAddress input (e.g. in presence of
9214 // irreducible loops), R2 uses the NAL instruction to simulate PC-relative
9215 // addressing.
Alexey Frunze96b66822016-09-10 02:32:44 -07009216 GenTableBasedPackedSwitch(value_reg,
9217 ZERO,
9218 lower_bound,
9219 num_entries,
9220 switch_block,
9221 default_block);
9222 } else {
9223 GenPackedSwitchWithCompares(value_reg,
9224 lower_bound,
9225 num_entries,
9226 switch_block,
9227 default_block);
9228 }
9229}
9230
9231void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9232 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009233 new (GetGraph()->GetAllocator()) LocationSummary(switch_instr, LocationSummary::kNoCall);
Alexey Frunze96b66822016-09-10 02:32:44 -07009234 locations->SetInAt(0, Location::RequiresRegister());
9235 // Constant area pointer (HMipsComputeBaseMethodAddress).
9236 locations->SetInAt(1, Location::RequiresRegister());
9237}
9238
9239void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9240 int32_t lower_bound = switch_instr->GetStartValue();
9241 uint32_t num_entries = switch_instr->GetNumEntries();
9242 LocationSummary* locations = switch_instr->GetLocations();
9243 Register value_reg = locations->InAt(0).AsRegister<Register>();
9244 Register constant_area = locations->InAt(1).AsRegister<Register>();
9245 HBasicBlock* switch_block = switch_instr->GetBlock();
9246 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9247
9248 // This is an R2-only path. HPackedSwitch has been changed to
9249 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9250 // required to address the jump table relative to PC.
9251 GenTableBasedPackedSwitch(value_reg,
9252 constant_area,
9253 lower_bound,
9254 num_entries,
9255 switch_block,
9256 default_block);
9257}
9258
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009259void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9260 HMipsComputeBaseMethodAddress* insn) {
9261 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009262 new (GetGraph()->GetAllocator()) LocationSummary(insn, LocationSummary::kNoCall);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009263 locations->SetOut(Location::RequiresRegister());
9264}
9265
9266void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9267 HMipsComputeBaseMethodAddress* insn) {
9268 LocationSummary* locations = insn->GetLocations();
9269 Register reg = locations->Out().AsRegister<Register>();
9270
9271 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9272
9273 // Generate a dummy PC-relative call to obtain PC.
9274 __ Nal();
9275 // Grab the return address off RA.
9276 __ Move(reg, RA);
9277
9278 // Remember this offset (the obtained PC value) for later use with constant area.
9279 __ BindPcRelBaseLabel();
9280}
9281
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009282void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9283 // The trampoline uses the same calling convention as dex calling conventions,
9284 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9285 // the method_idx.
9286 HandleInvoke(invoke);
9287}
9288
9289void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9290 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9291}
9292
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009293void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9294 LocationSummary* locations =
Vladimir Markoca6fff82017-10-03 14:49:14 +01009295 new (GetGraph()->GetAllocator()) LocationSummary(instruction, LocationSummary::kNoCall);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009296 locations->SetInAt(0, Location::RequiresRegister());
9297 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009298}
9299
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009300void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9301 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009302 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009303 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009304 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009305 __ LoadFromOffset(kLoadWord,
9306 locations->Out().AsRegister<Register>(),
9307 locations->InAt(0).AsRegister<Register>(),
9308 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009309 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009310 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009311 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009312 __ LoadFromOffset(kLoadWord,
9313 locations->Out().AsRegister<Register>(),
9314 locations->InAt(0).AsRegister<Register>(),
9315 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009316 __ LoadFromOffset(kLoadWord,
9317 locations->Out().AsRegister<Register>(),
9318 locations->Out().AsRegister<Register>(),
9319 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009320 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009321}
9322
xueliang.zhonge0eb4832017-10-30 13:43:14 +00009323void LocationsBuilderMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9324 ATTRIBUTE_UNUSED) {
9325 LOG(FATAL) << "Unreachable";
9326}
9327
9328void InstructionCodeGeneratorMIPS::VisitIntermediateAddress(HIntermediateAddress* instruction
9329 ATTRIBUTE_UNUSED) {
9330 LOG(FATAL) << "Unreachable";
9331}
9332
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009333#undef __
9334#undef QUICK_ENTRY_POINT
9335
9336} // namespace mips
9337} // namespace art