blob: a7c85574eeb1c0a088562b1a34639dcbe252e881 [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"
36#include "thread.h"
37#include "utils/assembler.h"
38#include "utils/mips/assembler_mips.h"
39#include "utils/stack_checks.h"
40
41namespace art {
42namespace mips {
43
44static constexpr int kCurrentMethodStackOffset = 0;
45static constexpr Register kMethodRegisterArgument = A0;
46
Alexey Frunze4147fcc2017-06-17 19:57:27 -070047// Flags controlling the use of thunks for Baker read barriers.
48constexpr bool kBakerReadBarrierThunksEnableForFields = true;
49constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
50constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
51
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010052Location MipsReturnLocation(DataType::Type return_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020053 switch (return_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010054 case DataType::Type::kBool:
55 case DataType::Type::kInt8:
56 case DataType::Type::kUint16:
57 case DataType::Type::kInt16:
58 case DataType::Type::kInt32:
59 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020060 return Location::RegisterLocation(V0);
61
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010062 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020063 return Location::RegisterPairLocation(V0, V1);
64
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010065 case DataType::Type::kFloat32:
66 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020067 return Location::FpuRegisterLocation(F0);
68
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010069 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020070 return Location();
71 }
72 UNREACHABLE();
73}
74
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010075Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(DataType::Type type) const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020076 return MipsReturnLocation(type);
77}
78
79Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
80 return Location::RegisterLocation(kMethodRegisterArgument);
81}
82
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010083Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020084 Location next_location;
85
86 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010087 case DataType::Type::kBool:
88 case DataType::Type::kInt8:
89 case DataType::Type::kUint16:
90 case DataType::Type::kInt16:
91 case DataType::Type::kInt32:
92 case DataType::Type::kReference: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020093 uint32_t gp_index = gp_index_++;
94 if (gp_index < calling_convention.GetNumberOfRegisters()) {
95 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
96 } else {
97 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
98 next_location = Location::StackSlot(stack_offset);
99 }
100 break;
101 }
102
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100103 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200104 uint32_t gp_index = gp_index_;
105 gp_index_ += 2;
106 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800107 Register reg = calling_convention.GetRegisterAt(gp_index);
108 if (reg == A1 || reg == A3) {
109 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200110 gp_index++;
111 }
112 Register low_even = calling_convention.GetRegisterAt(gp_index);
113 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
114 DCHECK_EQ(low_even + 1, high_odd);
115 next_location = Location::RegisterPairLocation(low_even, high_odd);
116 } else {
117 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
118 next_location = Location::DoubleStackSlot(stack_offset);
119 }
120 break;
121 }
122
123 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
124 // will take up the even/odd pair, while floats are stored in even regs only.
125 // On 64 bit FPU, both double and float are stored in even registers only.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100126 case DataType::Type::kFloat32:
127 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200128 uint32_t float_index = float_index_++;
129 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
130 next_location = Location::FpuRegisterLocation(
131 calling_convention.GetFpuRegisterAt(float_index));
132 } else {
133 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100134 next_location = DataType::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
135 : Location::StackSlot(stack_offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200136 }
137 break;
138 }
139
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100140 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200141 LOG(FATAL) << "Unexpected parameter type " << type;
142 break;
143 }
144
145 // Space on the stack is reserved for all arguments.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100146 stack_index_ += DataType::Is64BitType(type) ? 2 : 1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200147
148 return next_location;
149}
150
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100151Location InvokeRuntimeCallingConvention::GetReturnLocation(DataType::Type type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200152 return MipsReturnLocation(type);
153}
154
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100155// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
156#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700157#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200158
159class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
160 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000161 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200162
163 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
164 LocationSummary* locations = instruction_->GetLocations();
165 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
166 __ Bind(GetEntryLabel());
167 if (instruction_->CanThrowIntoCatchBlock()) {
168 // Live registers will be restored in the catch block if caught.
169 SaveLiveRegisters(codegen, instruction_->GetLocations());
170 }
171 // We're moving two locations to locations that could overlap, so we need a parallel
172 // move resolver.
173 InvokeRuntimeCallingConvention calling_convention;
174 codegen->EmitParallelMoves(locations->InAt(0),
175 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100176 DataType::Type::kInt32,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200177 locations->InAt(1),
178 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100179 DataType::Type::kInt32);
Serban Constantinescufca16662016-07-14 09:21:59 +0100180 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
181 ? kQuickThrowStringBounds
182 : kQuickThrowArrayBounds;
183 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100184 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200185 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
186 }
187
188 bool IsFatal() const OVERRIDE { return true; }
189
190 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
191
192 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200193 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
194};
195
196class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
197 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000198 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200199
200 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
201 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
202 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100203 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200204 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
205 }
206
207 bool IsFatal() const OVERRIDE { return true; }
208
209 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
210
211 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200212 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
213};
214
215class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
216 public:
217 LoadClassSlowPathMIPS(HLoadClass* cls,
218 HInstruction* at,
219 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700220 bool do_clinit,
221 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr)
222 : SlowPathCodeMIPS(at),
223 cls_(cls),
224 dex_pc_(dex_pc),
225 do_clinit_(do_clinit),
226 bss_info_high_(bss_info_high) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200227 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
228 }
229
230 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000231 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700232 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200233 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700234 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700235 InvokeRuntimeCallingConvention calling_convention;
236 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
237 const bool is_load_class_bss_entry =
238 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200239 __ Bind(GetEntryLabel());
240 SaveLiveRegisters(codegen, locations);
241
Alexey Frunzec61c0762017-04-10 13:54:23 -0700242 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
243 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700244 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700245 Register temp = locations->GetTemp(0).AsRegister<Register>();
246 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
247 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
248 // kSaveEverything call.
249 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
250 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
251 if (temp_is_a0) {
252 __ Move(entry_address, temp);
253 }
254 }
255
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000256 dex::TypeIndex type_index = cls_->GetTypeIndex();
257 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100258 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
259 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000260 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200261 if (do_clinit_) {
262 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
263 } else {
264 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
265 }
266
Alexey Frunzec61c0762017-04-10 13:54:23 -0700267 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700268 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700269 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700270 DCHECK(bss_info_high_);
271 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
272 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700273 __ Sw(calling_convention.GetRegisterAt(0),
274 entry_address,
275 /* placeholder */ 0x5678,
276 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700277 }
278
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200279 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200280 if (out.IsValid()) {
281 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100282 DataType::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700283 mips_codegen->MoveLocation(out,
284 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
285 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200287 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700288
289 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700290 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
291 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700292 // the class entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700293 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200294 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
295 Register base =
296 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700297 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000298 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700299 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
300 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700301 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
302 __ Sw(out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000303 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200304 __ B(GetExitLabel());
305 }
306
307 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
308
309 private:
310 // The class this slow path will load.
311 HLoadClass* const cls_;
312
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200313 // The dex PC of `at_`.
314 const uint32_t dex_pc_;
315
316 // Whether to initialize the class.
317 const bool do_clinit_;
318
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700319 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
320 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
321
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200322 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
323};
324
325class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
326 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700327 explicit LoadStringSlowPathMIPS(HLoadString* instruction,
328 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high)
329 : SlowPathCodeMIPS(instruction), bss_info_high_(bss_info_high) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200330
331 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700332 DCHECK(instruction_->IsLoadString());
333 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200334 LocationSummary* locations = instruction_->GetLocations();
335 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700336 HLoadString* load = instruction_->AsLoadString();
337 const dex::StringIndex string_index = load->GetStringIndex();
338 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200339 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700340 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700341 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200342 __ Bind(GetEntryLabel());
343 SaveLiveRegisters(codegen, locations);
344
Alexey Frunzec61c0762017-04-10 13:54:23 -0700345 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
346 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700347 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700348 Register temp = locations->GetTemp(0).AsRegister<Register>();
349 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
350 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
351 // kSaveEverything call.
352 entry_address = temp_is_a0 ? out : temp;
353 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
354 if (temp_is_a0) {
355 __ Move(entry_address, temp);
356 }
357 }
358
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000359 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100360 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200361 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700362
363 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700364 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700365 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700366 DCHECK(bss_info_high_);
367 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100368 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700369 __ Sw(calling_convention.GetRegisterAt(0),
370 entry_address,
371 /* placeholder */ 0x5678,
372 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700373 }
374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100375 DataType::Type type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200376 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700377 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200378 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200379 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000380
Alexey Frunzec61c0762017-04-10 13:54:23 -0700381 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700382 if (!baker_or_no_read_barriers) {
383 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700384 // the string entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700385 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200386 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
387 Register base =
388 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700389 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100390 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700391 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100392 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700393 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
394 __ Sw(out, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700395 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200396 __ B(GetExitLabel());
397 }
398
399 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
400
401 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700402 // Pointer to the high half PC-relative patch info.
403 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
404
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200405 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
406};
407
408class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
409 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000410 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200411
412 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
413 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
414 __ Bind(GetEntryLabel());
415 if (instruction_->CanThrowIntoCatchBlock()) {
416 // Live registers will be restored in the catch block if caught.
417 SaveLiveRegisters(codegen, instruction_->GetLocations());
418 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100419 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200420 instruction_,
421 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100422 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200423 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
424 }
425
426 bool IsFatal() const OVERRIDE { return true; }
427
428 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
429
430 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200431 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
432};
433
434class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
435 public:
436 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000437 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200438
439 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200440 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200441 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
442 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200443 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100444 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200445 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200446 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200447 if (successor_ == nullptr) {
448 __ B(GetReturnLabel());
449 } else {
450 __ B(mips_codegen->GetLabelOf(successor_));
451 }
452 }
453
454 MipsLabel* GetReturnLabel() {
455 DCHECK(successor_ == nullptr);
456 return &return_label_;
457 }
458
459 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
460
461 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200462 // If not null, the block to branch to after the suspend check.
463 HBasicBlock* const successor_;
464
465 // If `successor_` is null, the label to branch to after the suspend check.
466 MipsLabel return_label_;
467
468 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
469};
470
471class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
472 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800473 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
474 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200475
476 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
477 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200478 uint32_t dex_pc = instruction_->GetDexPc();
479 DCHECK(instruction_->IsCheckCast()
480 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
481 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
482
483 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800484 if (!is_fatal_) {
485 SaveLiveRegisters(codegen, locations);
486 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200487
488 // We're moving two locations to locations that could overlap, so we need a parallel
489 // move resolver.
490 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800491 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200492 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100493 DataType::Type::kReference,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800494 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200495 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100496 DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200497 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100498 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800499 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100500 DataType::Type ret_type = instruction_->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200501 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
502 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200503 } else {
504 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800505 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
506 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200507 }
508
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800509 if (!is_fatal_) {
510 RestoreLiveRegisters(codegen, locations);
511 __ B(GetExitLabel());
512 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200513 }
514
515 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
516
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800517 bool IsFatal() const OVERRIDE { return is_fatal_; }
518
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200519 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800520 const bool is_fatal_;
521
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200522 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
523};
524
525class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
526 public:
Aart Bik42249c32016-01-07 15:33:50 -0800527 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000528 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200529
530 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800531 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200532 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100533 LocationSummary* locations = instruction_->GetLocations();
534 SaveLiveRegisters(codegen, locations);
535 InvokeRuntimeCallingConvention calling_convention;
536 __ LoadConst32(calling_convention.GetRegisterAt(0),
537 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100538 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100539 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200540 }
541
542 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
543
544 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200545 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
546};
547
Alexey Frunze15958152017-02-09 19:08:30 -0800548class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
549 public:
550 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
551
552 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
553 LocationSummary* locations = instruction_->GetLocations();
554 __ Bind(GetEntryLabel());
555 SaveLiveRegisters(codegen, locations);
556
557 InvokeRuntimeCallingConvention calling_convention;
558 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
559 parallel_move.AddMove(
560 locations->InAt(0),
561 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100562 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800563 nullptr);
564 parallel_move.AddMove(
565 locations->InAt(1),
566 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100567 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800568 nullptr);
569 parallel_move.AddMove(
570 locations->InAt(2),
571 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100572 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800573 nullptr);
574 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
575
576 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
577 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
578 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
579 RestoreLiveRegisters(codegen, locations);
580 __ B(GetExitLabel());
581 }
582
583 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
584
585 private:
586 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
587};
588
589// Slow path marking an object reference `ref` during a read
590// barrier. The field `obj.field` in the object `obj` holding this
591// reference does not get updated by this slow path after marking (see
592// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
593//
594// This means that after the execution of this slow path, `ref` will
595// always be up-to-date, but `obj.field` may not; i.e., after the
596// flip, `ref` will be a to-space reference, but `obj.field` will
597// probably still be a from-space reference (unless it gets updated by
598// another thread, or if another thread installed another object
599// reference (different from `ref`) in `obj.field`).
600//
601// If `entrypoint` is a valid location it is assumed to already be
602// holding the entrypoint. The case where the entrypoint is passed in
603// is for the GcRoot read barrier.
604class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
605 public:
606 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
607 Location ref,
608 Location entrypoint = Location::NoLocation())
609 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
610 DCHECK(kEmitCompilerReadBarrier);
611 }
612
613 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
614
615 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
616 LocationSummary* locations = instruction_->GetLocations();
617 Register ref_reg = ref_.AsRegister<Register>();
618 DCHECK(locations->CanCall());
619 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
620 DCHECK(instruction_->IsInstanceFieldGet() ||
621 instruction_->IsStaticFieldGet() ||
622 instruction_->IsArrayGet() ||
623 instruction_->IsArraySet() ||
624 instruction_->IsLoadClass() ||
625 instruction_->IsLoadString() ||
626 instruction_->IsInstanceOf() ||
627 instruction_->IsCheckCast() ||
628 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
629 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
630 << "Unexpected instruction in read barrier marking slow path: "
631 << instruction_->DebugName();
632
633 __ Bind(GetEntryLabel());
634 // No need to save live registers; it's taken care of by the
635 // entrypoint. Also, there is no need to update the stack mask,
636 // as this runtime call will not trigger a garbage collection.
637 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
638 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
639 (S2 <= ref_reg && ref_reg <= S7) ||
640 (ref_reg == FP)) << ref_reg;
641 // "Compact" slow path, saving two moves.
642 //
643 // Instead of using the standard runtime calling convention (input
644 // and output in A0 and V0 respectively):
645 //
646 // A0 <- ref
647 // V0 <- ReadBarrierMark(A0)
648 // ref <- V0
649 //
650 // we just use rX (the register containing `ref`) as input and output
651 // of a dedicated entrypoint:
652 //
653 // rX <- ReadBarrierMarkRegX(rX)
654 //
655 if (entrypoint_.IsValid()) {
656 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
657 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
658 __ Jalr(entrypoint_.AsRegister<Register>());
659 __ NopIfNoReordering();
660 } else {
661 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100662 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800663 // This runtime call does not require a stack map.
664 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
665 instruction_,
666 this,
667 /* direct */ false);
668 }
669 __ B(GetExitLabel());
670 }
671
672 private:
673 // The location (register) of the marked object reference.
674 const Location ref_;
675
676 // The location of the entrypoint if already loaded.
677 const Location entrypoint_;
678
679 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
680};
681
682// Slow path marking an object reference `ref` during a read barrier,
683// and if needed, atomically updating the field `obj.field` in the
684// object `obj` holding this reference after marking (contrary to
685// ReadBarrierMarkSlowPathMIPS above, which never tries to update
686// `obj.field`).
687//
688// This means that after the execution of this slow path, both `ref`
689// and `obj.field` will be up-to-date; i.e., after the flip, both will
690// hold the same to-space reference (unless another thread installed
691// another object reference (different from `ref`) in `obj.field`).
692class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
693 public:
694 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
695 Location ref,
696 Register obj,
697 Location field_offset,
698 Register temp1)
699 : SlowPathCodeMIPS(instruction),
700 ref_(ref),
701 obj_(obj),
702 field_offset_(field_offset),
703 temp1_(temp1) {
704 DCHECK(kEmitCompilerReadBarrier);
705 }
706
707 const char* GetDescription() const OVERRIDE {
708 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
709 }
710
711 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
712 LocationSummary* locations = instruction_->GetLocations();
713 Register ref_reg = ref_.AsRegister<Register>();
714 DCHECK(locations->CanCall());
715 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
716 // This slow path is only used by the UnsafeCASObject intrinsic.
717 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
718 << "Unexpected instruction in read barrier marking and field updating slow path: "
719 << instruction_->DebugName();
720 DCHECK(instruction_->GetLocations()->Intrinsified());
721 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
722 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
723
724 __ Bind(GetEntryLabel());
725
726 // Save the old reference.
727 // Note that we cannot use AT or TMP to save the old reference, as those
728 // are used by the code that follows, but we need the old reference after
729 // the call to the ReadBarrierMarkRegX entry point.
730 DCHECK_NE(temp1_, AT);
731 DCHECK_NE(temp1_, TMP);
732 __ Move(temp1_, ref_reg);
733
734 // No need to save live registers; it's taken care of by the
735 // entrypoint. Also, there is no need to update the stack mask,
736 // as this runtime call will not trigger a garbage collection.
737 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
738 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
739 (S2 <= ref_reg && ref_reg <= S7) ||
740 (ref_reg == FP)) << ref_reg;
741 // "Compact" slow path, saving two moves.
742 //
743 // Instead of using the standard runtime calling convention (input
744 // and output in A0 and V0 respectively):
745 //
746 // A0 <- ref
747 // V0 <- ReadBarrierMark(A0)
748 // ref <- V0
749 //
750 // we just use rX (the register containing `ref`) as input and output
751 // of a dedicated entrypoint:
752 //
753 // rX <- ReadBarrierMarkRegX(rX)
754 //
755 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100756 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800757 // This runtime call does not require a stack map.
758 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
759 instruction_,
760 this,
761 /* direct */ false);
762
763 // If the new reference is different from the old reference,
764 // update the field in the holder (`*(obj_ + field_offset_)`).
765 //
766 // Note that this field could also hold a different object, if
767 // another thread had concurrently changed it. In that case, the
768 // the compare-and-set (CAS) loop below would abort, leaving the
769 // field as-is.
770 MipsLabel done;
771 __ Beq(temp1_, ref_reg, &done);
772
773 // Update the the holder's field atomically. This may fail if
774 // mutator updates before us, but it's OK. This is achieved
775 // using a strong compare-and-set (CAS) operation with relaxed
776 // memory synchronization ordering, where the expected value is
777 // the old reference and the desired value is the new reference.
778
779 // Convenience aliases.
780 Register base = obj_;
781 // The UnsafeCASObject intrinsic uses a register pair as field
782 // offset ("long offset"), of which only the low part contains
783 // data.
784 Register offset = field_offset_.AsRegisterPairLow<Register>();
785 Register expected = temp1_;
786 Register value = ref_reg;
787 Register tmp_ptr = TMP; // Pointer to actual memory.
788 Register tmp = AT; // Value in memory.
789
790 __ Addu(tmp_ptr, base, offset);
791
792 if (kPoisonHeapReferences) {
793 __ PoisonHeapReference(expected);
794 // Do not poison `value` if it is the same register as
795 // `expected`, which has just been poisoned.
796 if (value != expected) {
797 __ PoisonHeapReference(value);
798 }
799 }
800
801 // do {
802 // tmp = [r_ptr] - expected;
803 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
804
805 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
806 MipsLabel loop_head, exit_loop;
807 __ Bind(&loop_head);
808 if (is_r6) {
809 __ LlR6(tmp, tmp_ptr);
810 } else {
811 __ LlR2(tmp, tmp_ptr);
812 }
813 __ Bne(tmp, expected, &exit_loop);
814 __ Move(tmp, value);
815 if (is_r6) {
816 __ ScR6(tmp, tmp_ptr);
817 } else {
818 __ ScR2(tmp, tmp_ptr);
819 }
820 __ Beqz(tmp, &loop_head);
821 __ Bind(&exit_loop);
822
823 if (kPoisonHeapReferences) {
824 __ UnpoisonHeapReference(expected);
825 // Do not unpoison `value` if it is the same register as
826 // `expected`, which has just been unpoisoned.
827 if (value != expected) {
828 __ UnpoisonHeapReference(value);
829 }
830 }
831
832 __ Bind(&done);
833 __ B(GetExitLabel());
834 }
835
836 private:
837 // The location (register) of the marked object reference.
838 const Location ref_;
839 // The register containing the object holding the marked object reference field.
840 const Register obj_;
841 // The location of the offset of the marked reference field within `obj_`.
842 Location field_offset_;
843
844 const Register temp1_;
845
846 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
847};
848
849// Slow path generating a read barrier for a heap reference.
850class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
851 public:
852 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
853 Location out,
854 Location ref,
855 Location obj,
856 uint32_t offset,
857 Location index)
858 : SlowPathCodeMIPS(instruction),
859 out_(out),
860 ref_(ref),
861 obj_(obj),
862 offset_(offset),
863 index_(index) {
864 DCHECK(kEmitCompilerReadBarrier);
865 // If `obj` is equal to `out` or `ref`, it means the initial object
866 // has been overwritten by (or after) the heap object reference load
867 // to be instrumented, e.g.:
868 //
869 // __ LoadFromOffset(kLoadWord, out, out, offset);
870 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
871 //
872 // In that case, we have lost the information about the original
873 // object, and the emitted read barrier cannot work properly.
874 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
875 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
876 }
877
878 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
879 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
880 LocationSummary* locations = instruction_->GetLocations();
881 Register reg_out = out_.AsRegister<Register>();
882 DCHECK(locations->CanCall());
883 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
884 DCHECK(instruction_->IsInstanceFieldGet() ||
885 instruction_->IsStaticFieldGet() ||
886 instruction_->IsArrayGet() ||
887 instruction_->IsInstanceOf() ||
888 instruction_->IsCheckCast() ||
889 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
890 << "Unexpected instruction in read barrier for heap reference slow path: "
891 << instruction_->DebugName();
892
893 __ Bind(GetEntryLabel());
894 SaveLiveRegisters(codegen, locations);
895
896 // We may have to change the index's value, but as `index_` is a
897 // constant member (like other "inputs" of this slow path),
898 // introduce a copy of it, `index`.
899 Location index = index_;
900 if (index_.IsValid()) {
901 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
902 if (instruction_->IsArrayGet()) {
903 // Compute the actual memory offset and store it in `index`.
904 Register index_reg = index_.AsRegister<Register>();
905 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
906 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
907 // We are about to change the value of `index_reg` (see the
908 // calls to art::mips::MipsAssembler::Sll and
909 // art::mips::MipsAssembler::Addiu32 below), but it has
910 // not been saved by the previous call to
911 // art::SlowPathCode::SaveLiveRegisters, as it is a
912 // callee-save register --
913 // art::SlowPathCode::SaveLiveRegisters does not consider
914 // callee-save registers, as it has been designed with the
915 // assumption that callee-save registers are supposed to be
916 // handled by the called function. So, as a callee-save
917 // register, `index_reg` _would_ eventually be saved onto
918 // the stack, but it would be too late: we would have
919 // changed its value earlier. Therefore, we manually save
920 // it here into another freely available register,
921 // `free_reg`, chosen of course among the caller-save
922 // registers (as a callee-save `free_reg` register would
923 // exhibit the same problem).
924 //
925 // Note we could have requested a temporary register from
926 // the register allocator instead; but we prefer not to, as
927 // this is a slow path, and we know we can find a
928 // caller-save register that is available.
929 Register free_reg = FindAvailableCallerSaveRegister(codegen);
930 __ Move(free_reg, index_reg);
931 index_reg = free_reg;
932 index = Location::RegisterLocation(index_reg);
933 } else {
934 // The initial register stored in `index_` has already been
935 // saved in the call to art::SlowPathCode::SaveLiveRegisters
936 // (as it is not a callee-save register), so we can freely
937 // use it.
938 }
939 // Shifting the index value contained in `index_reg` by the scale
940 // factor (2) cannot overflow in practice, as the runtime is
941 // unable to allocate object arrays with a size larger than
942 // 2^26 - 1 (that is, 2^28 - 4 bytes).
943 __ Sll(index_reg, index_reg, TIMES_4);
944 static_assert(
945 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
946 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
947 __ Addiu32(index_reg, index_reg, offset_);
948 } else {
949 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
950 // intrinsics, `index_` is not shifted by a scale factor of 2
951 // (as in the case of ArrayGet), as it is actually an offset
952 // to an object field within an object.
953 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
954 DCHECK(instruction_->GetLocations()->Intrinsified());
955 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
956 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
957 << instruction_->AsInvoke()->GetIntrinsic();
958 DCHECK_EQ(offset_, 0U);
959 DCHECK(index_.IsRegisterPair());
960 // UnsafeGet's offset location is a register pair, the low
961 // part contains the correct offset.
962 index = index_.ToLow();
963 }
964 }
965
966 // We're moving two or three locations to locations that could
967 // overlap, so we need a parallel move resolver.
968 InvokeRuntimeCallingConvention calling_convention;
969 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
970 parallel_move.AddMove(ref_,
971 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100972 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800973 nullptr);
974 parallel_move.AddMove(obj_,
975 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100976 DataType::Type::kReference,
Alexey Frunze15958152017-02-09 19:08:30 -0800977 nullptr);
978 if (index.IsValid()) {
979 parallel_move.AddMove(index,
980 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100981 DataType::Type::kInt32,
Alexey Frunze15958152017-02-09 19:08:30 -0800982 nullptr);
983 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
984 } else {
985 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
986 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
987 }
988 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
989 instruction_,
990 instruction_->GetDexPc(),
991 this);
992 CheckEntrypointTypes<
993 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200994 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100995 calling_convention.GetReturnLocation(DataType::Type::kReference),
996 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -0800997
998 RestoreLiveRegisters(codegen, locations);
999 __ B(GetExitLabel());
1000 }
1001
1002 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
1003
1004 private:
1005 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1006 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1007 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1008 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1009 if (i != ref &&
1010 i != obj &&
1011 !codegen->IsCoreCalleeSaveRegister(i) &&
1012 !codegen->IsBlockedCoreRegister(i)) {
1013 return static_cast<Register>(i);
1014 }
1015 }
1016 // We shall never fail to find a free caller-save register, as
1017 // there are more than two core caller-save registers on MIPS
1018 // (meaning it is possible to find one which is different from
1019 // `ref` and `obj`).
1020 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1021 LOG(FATAL) << "Could not find a free caller-save register";
1022 UNREACHABLE();
1023 }
1024
1025 const Location out_;
1026 const Location ref_;
1027 const Location obj_;
1028 const uint32_t offset_;
1029 // An additional location containing an index to an array.
1030 // Only used for HArrayGet and the UnsafeGetObject &
1031 // UnsafeGetObjectVolatile intrinsics.
1032 const Location index_;
1033
1034 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
1035};
1036
1037// Slow path generating a read barrier for a GC root.
1038class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1039 public:
1040 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1041 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1042 DCHECK(kEmitCompilerReadBarrier);
1043 }
1044
1045 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1046 LocationSummary* locations = instruction_->GetLocations();
1047 Register reg_out = out_.AsRegister<Register>();
1048 DCHECK(locations->CanCall());
1049 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1050 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1051 << "Unexpected instruction in read barrier for GC root slow path: "
1052 << instruction_->DebugName();
1053
1054 __ Bind(GetEntryLabel());
1055 SaveLiveRegisters(codegen, locations);
1056
1057 InvokeRuntimeCallingConvention calling_convention;
1058 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001059 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1060 root_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001061 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001062 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1063 instruction_,
1064 instruction_->GetDexPc(),
1065 this);
1066 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001067 mips_codegen->MoveLocation(out_,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001068 calling_convention.GetReturnLocation(DataType::Type::kReference),
1069 DataType::Type::kReference);
Alexey Frunze15958152017-02-09 19:08:30 -08001070
1071 RestoreLiveRegisters(codegen, locations);
1072 __ B(GetExitLabel());
1073 }
1074
1075 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1076
1077 private:
1078 const Location out_;
1079 const Location root_;
1080
1081 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1082};
1083
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001084CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1085 const MipsInstructionSetFeatures& isa_features,
1086 const CompilerOptions& compiler_options,
1087 OptimizingCompilerStats* stats)
1088 : CodeGenerator(graph,
1089 kNumberOfCoreRegisters,
1090 kNumberOfFRegisters,
1091 kNumberOfRegisterPairs,
1092 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1093 arraysize(kCoreCalleeSaves)),
1094 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1095 arraysize(kFpuCalleeSaves)),
1096 compiler_options,
1097 stats),
1098 block_labels_(nullptr),
1099 location_builder_(graph, this),
1100 instruction_visitor_(graph, this),
1101 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001102 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001103 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001104 uint32_literals_(std::less<uint32_t>(),
1105 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001106 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001107 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001108 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001109 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001110 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001111 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001112 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1113 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001114 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001115 // Save RA (containing the return address) to mimic Quick.
1116 AddAllocatedRegister(Location::RegisterLocation(RA));
1117}
1118
1119#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001120// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1121#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001122#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001123
1124void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1125 // Ensure that we fix up branches.
1126 __ FinalizeCode();
1127
1128 // Adjust native pc offsets in stack maps.
1129 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001130 uint32_t old_position =
1131 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001132 uint32_t new_position = __ GetAdjustedPosition(old_position);
1133 DCHECK_GE(new_position, old_position);
1134 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1135 }
1136
1137 // Adjust pc offsets for the disassembly information.
1138 if (disasm_info_ != nullptr) {
1139 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1140 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1141 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1142 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1143 it.second.start = __ GetAdjustedPosition(it.second.start);
1144 it.second.end = __ GetAdjustedPosition(it.second.end);
1145 }
1146 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1147 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1148 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1149 }
1150 }
1151
1152 CodeGenerator::Finalize(allocator);
1153}
1154
1155MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1156 return codegen_->GetAssembler();
1157}
1158
1159void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1160 DCHECK_LT(index, moves_.size());
1161 MoveOperands* move = moves_[index];
1162 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1163}
1164
1165void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1166 DCHECK_LT(index, moves_.size());
1167 MoveOperands* move = moves_[index];
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001168 DataType::Type type = move->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001169 Location loc1 = move->GetDestination();
1170 Location loc2 = move->GetSource();
1171
1172 DCHECK(!loc1.IsConstant());
1173 DCHECK(!loc2.IsConstant());
1174
1175 if (loc1.Equals(loc2)) {
1176 return;
1177 }
1178
1179 if (loc1.IsRegister() && loc2.IsRegister()) {
1180 // Swap 2 GPRs.
1181 Register r1 = loc1.AsRegister<Register>();
1182 Register r2 = loc2.AsRegister<Register>();
1183 __ Move(TMP, r2);
1184 __ Move(r2, r1);
1185 __ Move(r1, TMP);
1186 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1187 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1188 FRegister f2 = loc2.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001189 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001190 __ MovS(FTMP, f2);
1191 __ MovS(f2, f1);
1192 __ MovS(f1, FTMP);
1193 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001194 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001195 __ MovD(FTMP, f2);
1196 __ MovD(f2, f1);
1197 __ MovD(f1, FTMP);
1198 }
1199 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1200 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1201 // Swap FPR and GPR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001202 DCHECK_EQ(type, DataType::Type::kFloat32); // Can only swap a float.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001203 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1204 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001205 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001206 __ Move(TMP, r2);
1207 __ Mfc1(r2, f1);
1208 __ Mtc1(TMP, f1);
1209 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1210 // Swap 2 GPR register pairs.
1211 Register r1 = loc1.AsRegisterPairLow<Register>();
1212 Register r2 = loc2.AsRegisterPairLow<Register>();
1213 __ Move(TMP, r2);
1214 __ Move(r2, r1);
1215 __ Move(r1, TMP);
1216 r1 = loc1.AsRegisterPairHigh<Register>();
1217 r2 = loc2.AsRegisterPairHigh<Register>();
1218 __ Move(TMP, r2);
1219 __ Move(r2, r1);
1220 __ Move(r1, TMP);
1221 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1222 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1223 // Swap FPR and GPR register pair.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001224 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001225 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1226 : loc2.AsFpuRegister<FRegister>();
1227 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1228 : loc2.AsRegisterPairLow<Register>();
1229 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1230 : loc2.AsRegisterPairHigh<Register>();
1231 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1232 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1233 // unpredictable and the following mfch1 will fail.
1234 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001235 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001236 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001237 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001238 __ Move(r2_l, TMP);
1239 __ Move(r2_h, AT);
1240 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1241 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1242 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1243 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001244 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1245 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001246 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1247 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001248 __ Move(TMP, reg);
1249 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1250 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1251 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1252 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1253 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1254 : loc2.AsRegisterPairLow<Register>();
1255 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1256 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001257 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001258 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1259 : loc2.GetHighStackIndex(kMipsWordSize);
1260 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001261 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001262 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001263 __ Move(TMP, reg_h);
1264 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1265 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001266 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1267 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1268 : loc2.AsFpuRegister<FRegister>();
1269 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001270 if (type == DataType::Type::kFloat32) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001271 __ MovS(FTMP, reg);
1272 __ LoadSFromOffset(reg, SP, offset);
1273 __ StoreSToOffset(FTMP, SP, offset);
1274 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001275 DCHECK_EQ(type, DataType::Type::kFloat64);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001276 __ MovD(FTMP, reg);
1277 __ LoadDFromOffset(reg, SP, offset);
1278 __ StoreDToOffset(FTMP, SP, offset);
1279 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001280 } else {
1281 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1282 }
1283}
1284
1285void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1286 __ Pop(static_cast<Register>(reg));
1287}
1288
1289void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1290 __ Push(static_cast<Register>(reg));
1291}
1292
1293void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1294 // Allocate a scratch register other than TMP, if available.
1295 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1296 // automatically unspilled when the scratch scope object is destroyed).
1297 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1298 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1299 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1300 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1301 __ LoadFromOffset(kLoadWord,
1302 Register(ensure_scratch.GetRegister()),
1303 SP,
1304 index1 + stack_offset);
1305 __ LoadFromOffset(kLoadWord,
1306 TMP,
1307 SP,
1308 index2 + stack_offset);
1309 __ StoreToOffset(kStoreWord,
1310 Register(ensure_scratch.GetRegister()),
1311 SP,
1312 index2 + stack_offset);
1313 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1314 }
1315}
1316
Alexey Frunze73296a72016-06-03 22:51:46 -07001317void CodeGeneratorMIPS::ComputeSpillMask() {
1318 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1319 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1320 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1321 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1322 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1323 // within the stack frame.
1324 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1325 core_spill_mask_ |= (1 << ZERO);
1326 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001327}
1328
1329bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001330 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001331 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1332 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1333 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001334 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001335}
1336
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001337static dwarf::Reg DWARFReg(Register reg) {
1338 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1339}
1340
1341// TODO: mapping of floating-point registers to DWARF.
1342
1343void CodeGeneratorMIPS::GenerateFrameEntry() {
1344 __ Bind(&frame_entry_label_);
1345
1346 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1347
1348 if (do_overflow_check) {
1349 __ LoadFromOffset(kLoadWord,
1350 ZERO,
1351 SP,
1352 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1353 RecordPcInfo(nullptr, 0);
1354 }
1355
1356 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001357 CHECK_EQ(fpu_spill_mask_, 0u);
1358 CHECK_EQ(core_spill_mask_, 1u << RA);
1359 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001360 return;
1361 }
1362
1363 // Make sure the frame size isn't unreasonably large.
1364 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1365 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1366 }
1367
1368 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001369
Alexey Frunze73296a72016-06-03 22:51:46 -07001370 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001371 __ IncreaseFrameSize(ofs);
1372
Alexey Frunze73296a72016-06-03 22:51:46 -07001373 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1374 Register reg = static_cast<Register>(MostSignificantBit(mask));
1375 mask ^= 1u << reg;
1376 ofs -= kMipsWordSize;
1377 // The ZERO register is only included for alignment.
1378 if (reg != ZERO) {
1379 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001380 __ cfi().RelOffset(DWARFReg(reg), ofs);
1381 }
1382 }
1383
Alexey Frunze73296a72016-06-03 22:51:46 -07001384 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1385 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1386 mask ^= 1u << reg;
1387 ofs -= kMipsDoublewordSize;
1388 __ StoreDToOffset(reg, SP, ofs);
1389 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001390 }
1391
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001392 // Save the current method if we need it. Note that we do not
1393 // do this in HCurrentMethod, as the instruction might have been removed
1394 // in the SSA graph.
1395 if (RequiresCurrentMethod()) {
1396 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1397 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001398
1399 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1400 // Initialize should deoptimize flag to 0.
1401 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1402 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001403}
1404
1405void CodeGeneratorMIPS::GenerateFrameExit() {
1406 __ cfi().RememberState();
1407
1408 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001409 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001410
Alexey Frunze73296a72016-06-03 22:51:46 -07001411 // For better instruction scheduling restore RA before other registers.
1412 uint32_t ofs = GetFrameSize();
1413 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1414 Register reg = static_cast<Register>(MostSignificantBit(mask));
1415 mask ^= 1u << reg;
1416 ofs -= kMipsWordSize;
1417 // The ZERO register is only included for alignment.
1418 if (reg != ZERO) {
1419 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001420 __ cfi().Restore(DWARFReg(reg));
1421 }
1422 }
1423
Alexey Frunze73296a72016-06-03 22:51:46 -07001424 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1425 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1426 mask ^= 1u << reg;
1427 ofs -= kMipsDoublewordSize;
1428 __ LoadDFromOffset(reg, SP, ofs);
1429 // TODO: __ cfi().Restore(DWARFReg(reg));
1430 }
1431
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001432 size_t frame_size = GetFrameSize();
1433 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1434 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1435 bool reordering = __ SetReorder(false);
1436 if (exchange) {
1437 __ Jr(RA);
1438 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1439 } else {
1440 __ DecreaseFrameSize(frame_size);
1441 __ Jr(RA);
1442 __ Nop(); // In delay slot.
1443 }
1444 __ SetReorder(reordering);
1445 } else {
1446 __ Jr(RA);
1447 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001448 }
1449
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001450 __ cfi().RestoreState();
1451 __ cfi().DefCFAOffset(GetFrameSize());
1452}
1453
1454void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1455 __ Bind(GetLabelOf(block));
1456}
1457
Lena Djokicca8c2952017-05-29 11:31:46 +02001458VectorRegister VectorRegisterFrom(Location location) {
1459 DCHECK(location.IsFpuRegister());
1460 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1461}
1462
Lena Djokic8098da92017-06-28 12:07:50 +02001463void CodeGeneratorMIPS::MoveLocation(Location destination,
1464 Location source,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001465 DataType::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001466 if (source.Equals(destination)) {
1467 return;
1468 }
1469
Lena Djokic8098da92017-06-28 12:07:50 +02001470 if (source.IsConstant()) {
1471 MoveConstant(destination, source.GetConstant());
1472 } else {
1473 if (destination.IsRegister()) {
1474 if (source.IsRegister()) {
1475 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1476 } else if (source.IsFpuRegister()) {
1477 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1478 } else {
1479 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001480 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001481 }
1482 } else if (destination.IsRegisterPair()) {
1483 if (source.IsRegisterPair()) {
1484 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1485 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1486 } else if (source.IsFpuRegister()) {
1487 Register dst_high = destination.AsRegisterPairHigh<Register>();
1488 Register dst_low = destination.AsRegisterPairLow<Register>();
1489 FRegister src = source.AsFpuRegister<FRegister>();
1490 __ Mfc1(dst_low, src);
1491 __ MoveFromFpuHigh(dst_high, src);
1492 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001493 DCHECK(source.IsDoubleStackSlot())
1494 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001495 int32_t off = source.GetStackIndex();
1496 Register r = destination.AsRegisterPairLow<Register>();
1497 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1498 }
1499 } else if (destination.IsFpuRegister()) {
1500 if (source.IsRegister()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001501 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001502 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1503 } else if (source.IsRegisterPair()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001504 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001505 FRegister dst = destination.AsFpuRegister<FRegister>();
1506 Register src_high = source.AsRegisterPairHigh<Register>();
1507 Register src_low = source.AsRegisterPairLow<Register>();
1508 __ Mtc1(src_low, dst);
1509 __ MoveToFpuHigh(src_high, dst);
1510 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001511 if (GetGraph()->HasSIMD()) {
1512 __ MoveV(VectorRegisterFrom(destination),
1513 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001514 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001515 if (DataType::Is64BitType(dst_type)) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001516 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1517 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001518 DCHECK_EQ(dst_type, DataType::Type::kFloat32);
Lena Djokicca8c2952017-05-29 11:31:46 +02001519 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1520 }
Lena Djokic8098da92017-06-28 12:07:50 +02001521 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001522 } else if (source.IsSIMDStackSlot()) {
1523 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001524 } else if (source.IsDoubleStackSlot()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001525 DCHECK(DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001526 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1527 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001528 DCHECK(!DataType::Is64BitType(dst_type));
Lena Djokic8098da92017-06-28 12:07:50 +02001529 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1530 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1531 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001532 } else if (destination.IsSIMDStackSlot()) {
1533 if (source.IsFpuRegister()) {
1534 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1535 } else {
1536 DCHECK(source.IsSIMDStackSlot());
1537 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1538 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1539 }
Lena Djokic8098da92017-06-28 12:07:50 +02001540 } else if (destination.IsDoubleStackSlot()) {
1541 int32_t dst_offset = destination.GetStackIndex();
1542 if (source.IsRegisterPair()) {
1543 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1544 } else if (source.IsFpuRegister()) {
1545 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1546 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001547 DCHECK(source.IsDoubleStackSlot())
1548 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001549 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1550 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1551 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1552 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1553 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001554 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001555 DCHECK(destination.IsStackSlot()) << destination;
1556 int32_t dst_offset = destination.GetStackIndex();
1557 if (source.IsRegister()) {
1558 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1559 } else if (source.IsFpuRegister()) {
1560 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1561 } else {
1562 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1563 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1564 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1565 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001566 }
1567 }
1568}
1569
1570void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1571 if (c->IsIntConstant() || c->IsNullConstant()) {
1572 // Move 32 bit constant.
1573 int32_t value = GetInt32ValueOf(c);
1574 if (destination.IsRegister()) {
1575 Register dst = destination.AsRegister<Register>();
1576 __ LoadConst32(dst, value);
1577 } else {
1578 DCHECK(destination.IsStackSlot())
1579 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001580 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001581 }
1582 } else if (c->IsLongConstant()) {
1583 // Move 64 bit constant.
1584 int64_t value = GetInt64ValueOf(c);
1585 if (destination.IsRegisterPair()) {
1586 Register r_h = destination.AsRegisterPairHigh<Register>();
1587 Register r_l = destination.AsRegisterPairLow<Register>();
1588 __ LoadConst64(r_h, r_l, value);
1589 } else {
1590 DCHECK(destination.IsDoubleStackSlot())
1591 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001592 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001593 }
1594 } else if (c->IsFloatConstant()) {
1595 // Move 32 bit float constant.
1596 int32_t value = GetInt32ValueOf(c);
1597 if (destination.IsFpuRegister()) {
1598 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1599 } else {
1600 DCHECK(destination.IsStackSlot())
1601 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001602 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001603 }
1604 } else {
1605 // Move 64 bit double constant.
1606 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1607 int64_t value = GetInt64ValueOf(c);
1608 if (destination.IsFpuRegister()) {
1609 FRegister fd = destination.AsFpuRegister<FRegister>();
1610 __ LoadDConst64(fd, value, TMP);
1611 } else {
1612 DCHECK(destination.IsDoubleStackSlot())
1613 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001614 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001615 }
1616 }
1617}
1618
1619void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1620 DCHECK(destination.IsRegister());
1621 Register dst = destination.AsRegister<Register>();
1622 __ LoadConst32(dst, value);
1623}
1624
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001625void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1626 if (location.IsRegister()) {
1627 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001628 } else if (location.IsRegisterPair()) {
1629 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1630 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001631 } else {
1632 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1633 }
1634}
1635
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001636template <linker::LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
Vladimir Markoaad75c62016-10-03 08:46:48 +00001637inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1638 const ArenaDeque<PcRelativePatchInfo>& infos,
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001639 ArenaVector<linker::LinkerPatch>* linker_patches) {
Vladimir Markoaad75c62016-10-03 08:46:48 +00001640 for (const PcRelativePatchInfo& info : infos) {
1641 const DexFile& dex_file = info.target_dex_file;
1642 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001643 DCHECK(info.label.IsBound());
1644 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001645 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1646 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001647 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1648 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1649 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001650 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001651 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001652 }
1653}
1654
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001655void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<linker::LinkerPatch>* linker_patches) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001656 DCHECK(linker_patches->empty());
1657 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001658 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001659 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001660 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001661 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001662 pc_relative_string_patches_.size() +
1663 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001664 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001665 if (GetCompilerOptions().IsBootImage()) {
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001666 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeMethodPatch>(
1667 pc_relative_method_patches_, linker_patches);
1668 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeTypePatch>(
1669 pc_relative_type_patches_, linker_patches);
1670 EmitPcRelativeLinkerPatches<linker::LinkerPatch::RelativeStringPatch>(
1671 pc_relative_string_patches_, linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001672 } else {
1673 DCHECK(pc_relative_method_patches_.empty());
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001674 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeClassTablePatch>(
1675 pc_relative_type_patches_, linker_patches);
1676 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringInternTablePatch>(
1677 pc_relative_string_patches_, linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001678 }
Vladimir Markod8dbc8d2017-09-20 13:37:47 +01001679 EmitPcRelativeLinkerPatches<linker::LinkerPatch::MethodBssEntryPatch>(
1680 method_bss_entry_patches_, linker_patches);
1681 EmitPcRelativeLinkerPatches<linker::LinkerPatch::TypeBssEntryPatch>(
1682 type_bss_entry_patches_, linker_patches);
1683 EmitPcRelativeLinkerPatches<linker::LinkerPatch::StringBssEntryPatch>(
1684 string_bss_entry_patches_, linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001685 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001686}
1687
Vladimir Marko65979462017-05-19 17:25:12 +01001688CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001689 MethodReference target_method,
1690 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001691 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001692 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001693 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001694 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001695}
1696
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001697CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001698 MethodReference target_method,
1699 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001700 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001701 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001702 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001703 &method_bss_entry_patches_);
1704}
1705
Alexey Frunze06a46c42016-07-19 15:00:40 -07001706CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001707 const DexFile& dex_file,
1708 dex::TypeIndex type_index,
1709 const PcRelativePatchInfo* info_high) {
1710 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001711}
1712
Vladimir Marko1998cd02017-01-13 13:02:58 +00001713CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001714 const DexFile& dex_file,
1715 dex::TypeIndex type_index,
1716 const PcRelativePatchInfo* info_high) {
1717 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001718}
1719
Vladimir Marko65979462017-05-19 17:25:12 +01001720CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001721 const DexFile& dex_file,
1722 dex::StringIndex string_index,
1723 const PcRelativePatchInfo* info_high) {
1724 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001725}
1726
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001727CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1728 const DexFile& dex_file,
1729 dex::StringIndex string_index,
1730 const PcRelativePatchInfo* info_high) {
1731 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1732}
1733
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001734CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001735 const DexFile& dex_file,
1736 uint32_t offset_or_index,
1737 const PcRelativePatchInfo* info_high,
1738 ArenaDeque<PcRelativePatchInfo>* patches) {
1739 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001740 return &patches->back();
1741}
1742
Alexey Frunze06a46c42016-07-19 15:00:40 -07001743Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1744 return map->GetOrCreate(
1745 value,
1746 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1747}
1748
Alexey Frunze06a46c42016-07-19 15:00:40 -07001749Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001750 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001751}
1752
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001753void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001754 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001755 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001756 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001757 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001758 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001759 if (GetInstructionSetFeatures().IsR6()) {
1760 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001761 __ Bind(&info_high->label);
1762 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001763 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001764 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001765 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001766 } else {
1767 // If base is ZERO, emit NAL to obtain the actual base.
1768 if (base == ZERO) {
1769 // Generate a dummy PC-relative call to obtain PC.
1770 __ Nal();
1771 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001772 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001773 __ Lui(out, /* placeholder */ 0x1234);
1774 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1775 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1776 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001777 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001778 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001779 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001780 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001781 __ Addu(out, out, (base == ZERO) ? RA : base);
1782 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001783 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001784 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001785}
1786
Alexey Frunze627c1a02017-01-30 19:28:14 -08001787CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1788 const DexFile& dex_file,
1789 dex::StringIndex dex_index,
1790 Handle<mirror::String> handle) {
1791 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1792 reinterpret_cast64<uint64_t>(handle.GetReference()));
1793 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1794 return &jit_string_patches_.back();
1795}
1796
1797CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1798 const DexFile& dex_file,
1799 dex::TypeIndex dex_index,
1800 Handle<mirror::Class> handle) {
1801 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1802 reinterpret_cast64<uint64_t>(handle.GetReference()));
1803 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1804 return &jit_class_patches_.back();
1805}
1806
1807void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1808 const uint8_t* roots_data,
1809 const CodeGeneratorMIPS::JitPatchInfo& info,
1810 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001811 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1812 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001813 uintptr_t address =
1814 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1815 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1816 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001817 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1818 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1819 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1820 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001821 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001822 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1823 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001824 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001825 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001826 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1827 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001828 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001829 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1830 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001831}
1832
1833void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1834 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001835 const auto it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1836 dex::StringIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001837 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001838 uint64_t index_in_table = it->second;
1839 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001840 }
1841 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001842 const auto it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1843 dex::TypeIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001844 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001845 uint64_t index_in_table = it->second;
1846 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001847 }
1848}
1849
Goran Jakovljevice114da22016-12-26 14:21:43 +01001850void CodeGeneratorMIPS::MarkGCCard(Register object,
1851 Register value,
1852 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001853 MipsLabel done;
1854 Register card = AT;
1855 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001856 if (value_can_be_null) {
1857 __ Beqz(value, &done);
1858 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001859 __ LoadFromOffset(kLoadWord,
1860 card,
1861 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001862 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001863 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1864 __ Addu(temp, card, temp);
1865 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001866 if (value_can_be_null) {
1867 __ Bind(&done);
1868 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001869}
1870
David Brazdil58282f42016-01-14 12:45:10 +00001871void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001872 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1873 blocked_core_registers_[ZERO] = true;
1874 blocked_core_registers_[K0] = true;
1875 blocked_core_registers_[K1] = true;
1876 blocked_core_registers_[GP] = true;
1877 blocked_core_registers_[SP] = true;
1878 blocked_core_registers_[RA] = true;
1879
1880 // AT and TMP(T8) are used as temporary/scratch registers
1881 // (similar to how AT is used by MIPS assemblers).
1882 blocked_core_registers_[AT] = true;
1883 blocked_core_registers_[TMP] = true;
1884 blocked_fpu_registers_[FTMP] = true;
1885
1886 // Reserve suspend and thread registers.
1887 blocked_core_registers_[S0] = true;
1888 blocked_core_registers_[TR] = true;
1889
1890 // Reserve T9 for function calls
1891 blocked_core_registers_[T9] = true;
1892
1893 // Reserve odd-numbered FPU registers.
1894 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1895 blocked_fpu_registers_[i] = true;
1896 }
1897
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001898 if (GetGraph()->IsDebuggable()) {
1899 // Stubs do not save callee-save floating point registers. If the graph
1900 // is debuggable, we need to deal with these registers differently. For
1901 // now, just block them.
1902 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1903 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1904 }
1905 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001906}
1907
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001908size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1909 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1910 return kMipsWordSize;
1911}
1912
1913size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1914 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1915 return kMipsWordSize;
1916}
1917
1918size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001919 if (GetGraph()->HasSIMD()) {
1920 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1921 } else {
1922 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1923 }
1924 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001925}
1926
1927size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001928 if (GetGraph()->HasSIMD()) {
1929 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1930 } else {
1931 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1932 }
1933 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001934}
1935
1936void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001937 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001938}
1939
1940void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001941 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001942}
1943
Serban Constantinescufca16662016-07-14 09:21:59 +01001944constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1945
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001946void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1947 HInstruction* instruction,
1948 uint32_t dex_pc,
1949 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001950 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001951 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1952 IsDirectEntrypoint(entrypoint));
1953 if (EntrypointRequiresStackMap(entrypoint)) {
1954 RecordPcInfo(instruction, dex_pc, slow_path);
1955 }
1956}
1957
1958void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1959 HInstruction* instruction,
1960 SlowPathCode* slow_path,
1961 bool direct) {
1962 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1963 GenerateInvokeRuntime(entry_point_offset, direct);
1964}
1965
1966void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001967 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001968 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001969 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001970 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001971 // Reserve argument space on stack (for $a0-$a3) for
1972 // entrypoints that directly reference native implementations.
1973 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001974 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001975 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001976 } else {
1977 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001978 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001979 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001980}
1981
1982void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1983 Register class_reg) {
1984 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1985 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1986 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1987 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1988 __ Sync(0);
1989 __ Bind(slow_path->GetExitLabel());
1990}
1991
1992void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1993 __ Sync(0); // Only stype 0 is supported.
1994}
1995
1996void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1997 HBasicBlock* successor) {
1998 SuspendCheckSlowPathMIPS* slow_path =
1999 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
2000 codegen_->AddSlowPath(slow_path);
2001
2002 __ LoadFromOffset(kLoadUnsignedHalfword,
2003 TMP,
2004 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002005 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002006 if (successor == nullptr) {
2007 __ Bnez(TMP, slow_path->GetEntryLabel());
2008 __ Bind(slow_path->GetReturnLabel());
2009 } else {
2010 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2011 __ B(slow_path->GetEntryLabel());
2012 // slow_path will return to GetLabelOf(successor).
2013 }
2014}
2015
2016InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2017 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002018 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002019 assembler_(codegen->GetAssembler()),
2020 codegen_(codegen) {}
2021
2022void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2023 DCHECK_EQ(instruction->InputCount(), 2U);
2024 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002025 DataType::Type type = instruction->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002027 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002028 locations->SetInAt(0, Location::RequiresRegister());
2029 HInstruction* right = instruction->InputAt(1);
2030 bool can_use_imm = false;
2031 if (right->IsConstant()) {
2032 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2033 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2034 can_use_imm = IsUint<16>(imm);
2035 } else if (instruction->IsAdd()) {
2036 can_use_imm = IsInt<16>(imm);
2037 } else {
2038 DCHECK(instruction->IsSub());
2039 can_use_imm = IsInt<16>(-imm);
2040 }
2041 }
2042 if (can_use_imm)
2043 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2044 else
2045 locations->SetInAt(1, Location::RequiresRegister());
2046 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2047 break;
2048 }
2049
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002050 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002051 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002052 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2053 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002054 break;
2055 }
2056
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002057 case DataType::Type::kFloat32:
2058 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002059 DCHECK(instruction->IsAdd() || instruction->IsSub());
2060 locations->SetInAt(0, Location::RequiresFpuRegister());
2061 locations->SetInAt(1, Location::RequiresFpuRegister());
2062 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2063 break;
2064
2065 default:
2066 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2067 }
2068}
2069
2070void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002071 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002072 LocationSummary* locations = instruction->GetLocations();
2073
2074 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002075 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002076 Register dst = locations->Out().AsRegister<Register>();
2077 Register lhs = locations->InAt(0).AsRegister<Register>();
2078 Location rhs_location = locations->InAt(1);
2079
2080 Register rhs_reg = ZERO;
2081 int32_t rhs_imm = 0;
2082 bool use_imm = rhs_location.IsConstant();
2083 if (use_imm) {
2084 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2085 } else {
2086 rhs_reg = rhs_location.AsRegister<Register>();
2087 }
2088
2089 if (instruction->IsAnd()) {
2090 if (use_imm)
2091 __ Andi(dst, lhs, rhs_imm);
2092 else
2093 __ And(dst, lhs, rhs_reg);
2094 } else if (instruction->IsOr()) {
2095 if (use_imm)
2096 __ Ori(dst, lhs, rhs_imm);
2097 else
2098 __ Or(dst, lhs, rhs_reg);
2099 } else if (instruction->IsXor()) {
2100 if (use_imm)
2101 __ Xori(dst, lhs, rhs_imm);
2102 else
2103 __ Xor(dst, lhs, rhs_reg);
2104 } else if (instruction->IsAdd()) {
2105 if (use_imm)
2106 __ Addiu(dst, lhs, rhs_imm);
2107 else
2108 __ Addu(dst, lhs, rhs_reg);
2109 } else {
2110 DCHECK(instruction->IsSub());
2111 if (use_imm)
2112 __ Addiu(dst, lhs, -rhs_imm);
2113 else
2114 __ Subu(dst, lhs, rhs_reg);
2115 }
2116 break;
2117 }
2118
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002119 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002120 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2121 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2122 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2123 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002124 Location rhs_location = locations->InAt(1);
2125 bool use_imm = rhs_location.IsConstant();
2126 if (!use_imm) {
2127 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2128 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2129 if (instruction->IsAnd()) {
2130 __ And(dst_low, lhs_low, rhs_low);
2131 __ And(dst_high, lhs_high, rhs_high);
2132 } else if (instruction->IsOr()) {
2133 __ Or(dst_low, lhs_low, rhs_low);
2134 __ Or(dst_high, lhs_high, rhs_high);
2135 } else if (instruction->IsXor()) {
2136 __ Xor(dst_low, lhs_low, rhs_low);
2137 __ Xor(dst_high, lhs_high, rhs_high);
2138 } else if (instruction->IsAdd()) {
2139 if (lhs_low == rhs_low) {
2140 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2141 __ Slt(TMP, lhs_low, ZERO);
2142 __ Addu(dst_low, lhs_low, rhs_low);
2143 } else {
2144 __ Addu(dst_low, lhs_low, rhs_low);
2145 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2146 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2147 }
2148 __ Addu(dst_high, lhs_high, rhs_high);
2149 __ Addu(dst_high, dst_high, TMP);
2150 } else {
2151 DCHECK(instruction->IsSub());
2152 __ Sltu(TMP, lhs_low, rhs_low);
2153 __ Subu(dst_low, lhs_low, rhs_low);
2154 __ Subu(dst_high, lhs_high, rhs_high);
2155 __ Subu(dst_high, dst_high, TMP);
2156 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002157 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002158 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2159 if (instruction->IsOr()) {
2160 uint32_t low = Low32Bits(value);
2161 uint32_t high = High32Bits(value);
2162 if (IsUint<16>(low)) {
2163 if (dst_low != lhs_low || low != 0) {
2164 __ Ori(dst_low, lhs_low, low);
2165 }
2166 } else {
2167 __ LoadConst32(TMP, low);
2168 __ Or(dst_low, lhs_low, TMP);
2169 }
2170 if (IsUint<16>(high)) {
2171 if (dst_high != lhs_high || high != 0) {
2172 __ Ori(dst_high, lhs_high, high);
2173 }
2174 } else {
2175 if (high != low) {
2176 __ LoadConst32(TMP, high);
2177 }
2178 __ Or(dst_high, lhs_high, TMP);
2179 }
2180 } else if (instruction->IsXor()) {
2181 uint32_t low = Low32Bits(value);
2182 uint32_t high = High32Bits(value);
2183 if (IsUint<16>(low)) {
2184 if (dst_low != lhs_low || low != 0) {
2185 __ Xori(dst_low, lhs_low, low);
2186 }
2187 } else {
2188 __ LoadConst32(TMP, low);
2189 __ Xor(dst_low, lhs_low, TMP);
2190 }
2191 if (IsUint<16>(high)) {
2192 if (dst_high != lhs_high || high != 0) {
2193 __ Xori(dst_high, lhs_high, high);
2194 }
2195 } else {
2196 if (high != low) {
2197 __ LoadConst32(TMP, high);
2198 }
2199 __ Xor(dst_high, lhs_high, TMP);
2200 }
2201 } else if (instruction->IsAnd()) {
2202 uint32_t low = Low32Bits(value);
2203 uint32_t high = High32Bits(value);
2204 if (IsUint<16>(low)) {
2205 __ Andi(dst_low, lhs_low, low);
2206 } else if (low != 0xFFFFFFFF) {
2207 __ LoadConst32(TMP, low);
2208 __ And(dst_low, lhs_low, TMP);
2209 } else if (dst_low != lhs_low) {
2210 __ Move(dst_low, lhs_low);
2211 }
2212 if (IsUint<16>(high)) {
2213 __ Andi(dst_high, lhs_high, high);
2214 } else if (high != 0xFFFFFFFF) {
2215 if (high != low) {
2216 __ LoadConst32(TMP, high);
2217 }
2218 __ And(dst_high, lhs_high, TMP);
2219 } else if (dst_high != lhs_high) {
2220 __ Move(dst_high, lhs_high);
2221 }
2222 } else {
2223 if (instruction->IsSub()) {
2224 value = -value;
2225 } else {
2226 DCHECK(instruction->IsAdd());
2227 }
2228 int32_t low = Low32Bits(value);
2229 int32_t high = High32Bits(value);
2230 if (IsInt<16>(low)) {
2231 if (dst_low != lhs_low || low != 0) {
2232 __ Addiu(dst_low, lhs_low, low);
2233 }
2234 if (low != 0) {
2235 __ Sltiu(AT, dst_low, low);
2236 }
2237 } else {
2238 __ LoadConst32(TMP, low);
2239 __ Addu(dst_low, lhs_low, TMP);
2240 __ Sltu(AT, dst_low, TMP);
2241 }
2242 if (IsInt<16>(high)) {
2243 if (dst_high != lhs_high || high != 0) {
2244 __ Addiu(dst_high, lhs_high, high);
2245 }
2246 } else {
2247 if (high != low) {
2248 __ LoadConst32(TMP, high);
2249 }
2250 __ Addu(dst_high, lhs_high, TMP);
2251 }
2252 if (low != 0) {
2253 __ Addu(dst_high, dst_high, AT);
2254 }
2255 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002256 }
2257 break;
2258 }
2259
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002260 case DataType::Type::kFloat32:
2261 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002262 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2263 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2264 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2265 if (instruction->IsAdd()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002266 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002267 __ AddS(dst, lhs, rhs);
2268 } else {
2269 __ AddD(dst, lhs, rhs);
2270 }
2271 } else {
2272 DCHECK(instruction->IsSub());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002273 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002274 __ SubS(dst, lhs, rhs);
2275 } else {
2276 __ SubD(dst, lhs, rhs);
2277 }
2278 }
2279 break;
2280 }
2281
2282 default:
2283 LOG(FATAL) << "Unexpected binary operation type " << type;
2284 }
2285}
2286
2287void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002288 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002289
2290 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002291 DataType::Type type = instr->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002292 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002293 case DataType::Type::kInt32:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002294 locations->SetInAt(0, Location::RequiresRegister());
2295 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2296 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2297 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002298 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002299 locations->SetInAt(0, Location::RequiresRegister());
2300 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2301 locations->SetOut(Location::RequiresRegister());
2302 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002303 default:
2304 LOG(FATAL) << "Unexpected shift type " << type;
2305 }
2306}
2307
2308static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2309
2310void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002311 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002312 LocationSummary* locations = instr->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002313 DataType::Type type = instr->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002314
2315 Location rhs_location = locations->InAt(1);
2316 bool use_imm = rhs_location.IsConstant();
2317 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2318 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002319 const uint32_t shift_mask =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002320 (type == DataType::Type::kInt32) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002321 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002322 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2323 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002324
2325 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002326 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002327 Register dst = locations->Out().AsRegister<Register>();
2328 Register lhs = locations->InAt(0).AsRegister<Register>();
2329 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002330 if (shift_value == 0) {
2331 if (dst != lhs) {
2332 __ Move(dst, lhs);
2333 }
2334 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002335 __ Sll(dst, lhs, shift_value);
2336 } else if (instr->IsShr()) {
2337 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002338 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002339 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002340 } else {
2341 if (has_ins_rotr) {
2342 __ Rotr(dst, lhs, shift_value);
2343 } else {
2344 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2345 __ Srl(dst, lhs, shift_value);
2346 __ Or(dst, dst, TMP);
2347 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002348 }
2349 } else {
2350 if (instr->IsShl()) {
2351 __ Sllv(dst, lhs, rhs_reg);
2352 } else if (instr->IsShr()) {
2353 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002354 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002355 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002356 } else {
2357 if (has_ins_rotr) {
2358 __ Rotrv(dst, lhs, rhs_reg);
2359 } else {
2360 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002361 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2362 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2363 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2364 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2365 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002366 __ Sllv(TMP, lhs, TMP);
2367 __ Srlv(dst, lhs, rhs_reg);
2368 __ Or(dst, dst, TMP);
2369 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002370 }
2371 }
2372 break;
2373 }
2374
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002375 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002376 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2377 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2378 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2379 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2380 if (use_imm) {
2381 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002382 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002383 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002384 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002385 if (instr->IsShl()) {
2386 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2387 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2388 __ Sll(dst_low, lhs_low, shift_value);
2389 } else if (instr->IsShr()) {
2390 __ Srl(dst_low, lhs_low, shift_value);
2391 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2392 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002393 } else if (instr->IsUShr()) {
2394 __ Srl(dst_low, lhs_low, shift_value);
2395 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2396 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002397 } else {
2398 __ Srl(dst_low, lhs_low, shift_value);
2399 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2400 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002401 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002402 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002403 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002404 if (instr->IsShl()) {
2405 __ Sll(dst_low, lhs_low, shift_value);
2406 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2407 __ Sll(dst_high, lhs_high, shift_value);
2408 __ Or(dst_high, dst_high, TMP);
2409 } else if (instr->IsShr()) {
2410 __ Sra(dst_high, lhs_high, shift_value);
2411 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2412 __ Srl(dst_low, lhs_low, shift_value);
2413 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002414 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002415 __ Srl(dst_high, lhs_high, shift_value);
2416 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2417 __ Srl(dst_low, lhs_low, shift_value);
2418 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002419 } else {
2420 __ Srl(TMP, lhs_low, shift_value);
2421 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2422 __ Or(dst_low, dst_low, TMP);
2423 __ Srl(TMP, lhs_high, shift_value);
2424 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2425 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002426 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002427 }
2428 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002429 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002430 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002431 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002432 __ Move(dst_low, ZERO);
2433 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002434 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002435 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002436 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002437 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002438 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002439 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002440 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002441 // 64-bit rotation by 32 is just a swap.
2442 __ Move(dst_low, lhs_high);
2443 __ Move(dst_high, lhs_low);
2444 } else {
2445 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002446 __ Srl(dst_low, lhs_high, shift_value_high);
2447 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2448 __ Srl(dst_high, lhs_low, shift_value_high);
2449 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002450 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002451 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2452 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002453 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002454 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2455 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002456 __ Or(dst_high, dst_high, TMP);
2457 }
2458 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002459 }
2460 }
2461 } else {
2462 MipsLabel done;
2463 if (instr->IsShl()) {
2464 __ Sllv(dst_low, lhs_low, rhs_reg);
2465 __ Nor(AT, ZERO, rhs_reg);
2466 __ Srl(TMP, lhs_low, 1);
2467 __ Srlv(TMP, TMP, AT);
2468 __ Sllv(dst_high, lhs_high, rhs_reg);
2469 __ Or(dst_high, dst_high, TMP);
2470 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2471 __ Beqz(TMP, &done);
2472 __ Move(dst_high, dst_low);
2473 __ Move(dst_low, ZERO);
2474 } else if (instr->IsShr()) {
2475 __ Srav(dst_high, lhs_high, rhs_reg);
2476 __ Nor(AT, ZERO, rhs_reg);
2477 __ Sll(TMP, lhs_high, 1);
2478 __ Sllv(TMP, TMP, AT);
2479 __ Srlv(dst_low, lhs_low, rhs_reg);
2480 __ Or(dst_low, dst_low, TMP);
2481 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2482 __ Beqz(TMP, &done);
2483 __ Move(dst_low, dst_high);
2484 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002485 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002486 __ Srlv(dst_high, lhs_high, rhs_reg);
2487 __ Nor(AT, ZERO, rhs_reg);
2488 __ Sll(TMP, lhs_high, 1);
2489 __ Sllv(TMP, TMP, AT);
2490 __ Srlv(dst_low, lhs_low, rhs_reg);
2491 __ Or(dst_low, dst_low, TMP);
2492 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2493 __ Beqz(TMP, &done);
2494 __ Move(dst_low, dst_high);
2495 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002496 } else {
2497 __ Nor(AT, ZERO, rhs_reg);
2498 __ Srlv(TMP, lhs_low, rhs_reg);
2499 __ Sll(dst_low, lhs_high, 1);
2500 __ Sllv(dst_low, dst_low, AT);
2501 __ Or(dst_low, dst_low, TMP);
2502 __ Srlv(TMP, lhs_high, rhs_reg);
2503 __ Sll(dst_high, lhs_low, 1);
2504 __ Sllv(dst_high, dst_high, AT);
2505 __ Or(dst_high, dst_high, TMP);
2506 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2507 __ Beqz(TMP, &done);
2508 __ Move(TMP, dst_high);
2509 __ Move(dst_high, dst_low);
2510 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002511 }
2512 __ Bind(&done);
2513 }
2514 break;
2515 }
2516
2517 default:
2518 LOG(FATAL) << "Unexpected shift operation type " << type;
2519 }
2520}
2521
2522void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2523 HandleBinaryOp(instruction);
2524}
2525
2526void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2527 HandleBinaryOp(instruction);
2528}
2529
2530void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2531 HandleBinaryOp(instruction);
2532}
2533
2534void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2535 HandleBinaryOp(instruction);
2536}
2537
2538void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002539 DataType::Type type = instruction->GetType();
Alexey Frunze15958152017-02-09 19:08:30 -08002540 bool object_array_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002541 kEmitCompilerReadBarrier && (type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002542 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002543 new (GetGraph()->GetArena()) LocationSummary(instruction,
2544 object_array_get_with_read_barrier
2545 ? LocationSummary::kCallOnSlowPath
2546 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002547 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2548 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2549 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002550 locations->SetInAt(0, Location::RequiresRegister());
2551 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002552 if (DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002553 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2554 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002555 // The output overlaps in the case of an object array get with
2556 // read barriers enabled: we do not want the move to overwrite the
2557 // array's location, as we need it to emit the read barrier.
2558 locations->SetOut(Location::RequiresRegister(),
2559 object_array_get_with_read_barrier
2560 ? Location::kOutputOverlap
2561 : Location::kNoOutputOverlap);
2562 }
2563 // We need a temporary register for the read barrier marking slow
2564 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2565 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002566 bool temp_needed = instruction->GetIndex()->IsConstant()
2567 ? !kBakerReadBarrierThunksEnableForFields
2568 : !kBakerReadBarrierThunksEnableForArrays;
2569 if (temp_needed) {
2570 locations->AddTemp(Location::RequiresRegister());
2571 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002572 }
2573}
2574
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002575static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2576 auto null_checker = [codegen, instruction]() {
2577 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002578 };
2579 return null_checker;
2580}
2581
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002582void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2583 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002584 Location obj_loc = locations->InAt(0);
2585 Register obj = obj_loc.AsRegister<Register>();
2586 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002587 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002588 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002589 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002590
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002591 DataType::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002592 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2593 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002594 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002595 case DataType::Type::kBool: {
Alexey Frunze15958152017-02-09 19:08:30 -08002596 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002597 if (index.IsConstant()) {
2598 size_t offset =
2599 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002600 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002601 } else {
2602 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002603 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002604 }
2605 break;
2606 }
2607
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002608 case DataType::Type::kInt8: {
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(kLoadSignedByte, 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(kLoadSignedByte, 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::kInt16: {
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_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002626 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002627 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002628 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002629 __ LoadFromOffset(kLoadSignedHalfword, 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);
2678 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002679 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002680 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2681 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002682 }
2683 break;
2684 }
2685
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002686 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002687 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002688 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002689 if (index.IsConstant()) {
2690 size_t offset =
2691 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002692 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002693 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002694 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002695 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002696 }
2697 break;
2698 }
2699
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002700 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002701 static_assert(
2702 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2703 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2704 // /* HeapReference<Object> */ out =
2705 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2706 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002707 bool temp_needed = index.IsConstant()
2708 ? !kBakerReadBarrierThunksEnableForFields
2709 : !kBakerReadBarrierThunksEnableForArrays;
2710 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002711 // Note that a potential implicit null check is handled in this
2712 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002713 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2714 if (index.IsConstant()) {
2715 // Array load with a constant index can be treated as a field load.
2716 size_t offset =
2717 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2718 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2719 out_loc,
2720 obj,
2721 offset,
2722 temp,
2723 /* needs_null_check */ false);
2724 } else {
2725 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2726 out_loc,
2727 obj,
2728 data_offset,
2729 index,
2730 temp,
2731 /* needs_null_check */ false);
2732 }
Alexey Frunze15958152017-02-09 19:08:30 -08002733 } else {
2734 Register out = out_loc.AsRegister<Register>();
2735 if (index.IsConstant()) {
2736 size_t offset =
2737 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2738 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2739 // If read barriers are enabled, emit read barriers other than
2740 // Baker's using a slow path (and also unpoison the loaded
2741 // reference, if heap poisoning is enabled).
2742 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2743 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002744 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002745 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2746 // If read barriers are enabled, emit read barriers other than
2747 // Baker's using a slow path (and also unpoison the loaded
2748 // reference, if heap poisoning is enabled).
2749 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2750 out_loc,
2751 out_loc,
2752 obj_loc,
2753 data_offset,
2754 index);
2755 }
2756 }
2757 break;
2758 }
2759
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002760 case DataType::Type::kInt64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002761 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002762 if (index.IsConstant()) {
2763 size_t offset =
2764 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002765 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002766 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002767 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002768 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002769 }
2770 break;
2771 }
2772
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002773 case DataType::Type::kFloat32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002774 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002775 if (index.IsConstant()) {
2776 size_t offset =
2777 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002778 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002779 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002780 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002781 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002782 }
2783 break;
2784 }
2785
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002786 case DataType::Type::kFloat64: {
Alexey Frunze15958152017-02-09 19:08:30 -08002787 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002788 if (index.IsConstant()) {
2789 size_t offset =
2790 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002791 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002792 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002793 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002794 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002795 }
2796 break;
2797 }
2798
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002799 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002800 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2801 UNREACHABLE();
2802 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002803}
2804
2805void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2806 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2807 locations->SetInAt(0, Location::RequiresRegister());
2808 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2809}
2810
2811void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2812 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002813 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002814 Register obj = locations->InAt(0).AsRegister<Register>();
2815 Register out = locations->Out().AsRegister<Register>();
2816 __ LoadFromOffset(kLoadWord, out, obj, offset);
2817 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002818 // Mask out compression flag from String's array length.
2819 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2820 __ Srl(out, out, 1u);
2821 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002822}
2823
Alexey Frunzef58b2482016-09-02 22:14:06 -07002824Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2825 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2826 ? Location::ConstantLocation(instruction->AsConstant())
2827 : Location::RequiresRegister();
2828}
2829
2830Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2831 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2832 // We can store a non-zero float or double constant without first loading it into the FPU,
2833 // but we should only prefer this if the constant has a single use.
2834 if (instruction->IsConstant() &&
2835 (instruction->AsConstant()->IsZeroBitPattern() ||
2836 instruction->GetUses().HasExactlyOneElement())) {
2837 return Location::ConstantLocation(instruction->AsConstant());
2838 // Otherwise fall through and require an FPU register for the constant.
2839 }
2840 return Location::RequiresFpuRegister();
2841}
2842
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002843void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002844 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002845
2846 bool needs_write_barrier =
2847 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2848 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2849
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002850 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2851 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002852 may_need_runtime_call_for_type_check ?
2853 LocationSummary::kCallOnSlowPath :
2854 LocationSummary::kNoCall);
2855
2856 locations->SetInAt(0, Location::RequiresRegister());
2857 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002858 if (DataType::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
Alexey Frunze15958152017-02-09 19:08:30 -08002859 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002860 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002861 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2862 }
2863 if (needs_write_barrier) {
2864 // Temporary register for the write barrier.
2865 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002866 }
2867}
2868
2869void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2870 LocationSummary* locations = instruction->GetLocations();
2871 Register obj = locations->InAt(0).AsRegister<Register>();
2872 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002873 Location value_location = locations->InAt(2);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002874 DataType::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002875 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002876 bool needs_write_barrier =
2877 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002878 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002879 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002880
2881 switch (value_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002882 case DataType::Type::kBool:
2883 case DataType::Type::kInt8: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002884 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002885 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002886 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002887 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002888 __ Addu(base_reg, obj, index.AsRegister<Register>());
2889 }
2890 if (value_location.IsConstant()) {
2891 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2892 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2893 } else {
2894 Register value = value_location.AsRegister<Register>();
2895 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002896 }
2897 break;
2898 }
2899
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002900 case DataType::Type::kInt16:
2901 case DataType::Type::kUint16: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002902 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002903 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002904 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002905 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002906 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002907 }
2908 if (value_location.IsConstant()) {
2909 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2910 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2911 } else {
2912 Register value = value_location.AsRegister<Register>();
2913 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002914 }
2915 break;
2916 }
2917
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002918 case DataType::Type::kInt32: {
Alexey Frunze15958152017-02-09 19:08:30 -08002919 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2920 if (index.IsConstant()) {
2921 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2922 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002923 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002924 }
2925 if (value_location.IsConstant()) {
2926 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2927 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2928 } else {
2929 Register value = value_location.AsRegister<Register>();
2930 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2931 }
2932 break;
2933 }
2934
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01002935 case DataType::Type::kReference: {
Alexey Frunze15958152017-02-09 19:08:30 -08002936 if (value_location.IsConstant()) {
2937 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002938 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002939 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002940 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
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_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002943 }
Alexey Frunze15958152017-02-09 19:08:30 -08002944 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2945 DCHECK_EQ(value, 0);
2946 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2947 DCHECK(!needs_write_barrier);
2948 DCHECK(!may_need_runtime_call_for_type_check);
2949 break;
2950 }
2951
2952 DCHECK(needs_write_barrier);
2953 Register value = value_location.AsRegister<Register>();
2954 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2955 Register temp2 = TMP; // Doesn't need to survive slow path.
2956 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2957 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2958 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2959 MipsLabel done;
2960 SlowPathCodeMIPS* slow_path = nullptr;
2961
2962 if (may_need_runtime_call_for_type_check) {
2963 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2964 codegen_->AddSlowPath(slow_path);
2965 if (instruction->GetValueCanBeNull()) {
2966 MipsLabel non_zero;
2967 __ Bnez(value, &non_zero);
2968 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2969 if (index.IsConstant()) {
2970 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002971 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002972 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002973 }
Alexey Frunze15958152017-02-09 19:08:30 -08002974 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2975 __ B(&done);
2976 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002977 }
Alexey Frunze15958152017-02-09 19:08:30 -08002978
2979 // Note that when read barriers are enabled, the type checks
2980 // are performed without read barriers. This is fine, even in
2981 // the case where a class object is in the from-space after
2982 // the flip, as a comparison involving such a type would not
2983 // produce a false positive; it may of course produce a false
2984 // negative, in which case we would take the ArraySet slow
2985 // path.
2986
2987 // /* HeapReference<Class> */ temp1 = obj->klass_
2988 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2989 __ MaybeUnpoisonHeapReference(temp1);
2990
2991 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2992 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2993 // /* HeapReference<Class> */ temp2 = value->klass_
2994 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2995 // If heap poisoning is enabled, no need to unpoison `temp1`
2996 // nor `temp2`, as we are comparing two poisoned references.
2997
2998 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2999 MipsLabel do_put;
3000 __ Beq(temp1, temp2, &do_put);
3001 // If heap poisoning is enabled, the `temp1` reference has
3002 // not been unpoisoned yet; unpoison it now.
3003 __ MaybeUnpoisonHeapReference(temp1);
3004
3005 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3006 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3007 // If heap poisoning is enabled, no need to unpoison
3008 // `temp1`, as we are comparing against null below.
3009 __ Bnez(temp1, slow_path->GetEntryLabel());
3010 __ Bind(&do_put);
3011 } else {
3012 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3013 }
3014 }
3015
3016 Register source = value;
3017 if (kPoisonHeapReferences) {
3018 // Note that in the case where `value` is a null reference,
3019 // we do not enter this block, as a null reference does not
3020 // need poisoning.
3021 __ Move(temp1, value);
3022 __ PoisonHeapReference(temp1);
3023 source = temp1;
3024 }
3025
3026 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3027 if (index.IsConstant()) {
3028 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003029 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003030 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003031 }
3032 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3033
3034 if (!may_need_runtime_call_for_type_check) {
3035 codegen_->MaybeRecordImplicitNullCheck(instruction);
3036 }
3037
3038 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3039
3040 if (done.IsLinked()) {
3041 __ Bind(&done);
3042 }
3043
3044 if (slow_path != nullptr) {
3045 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003046 }
3047 break;
3048 }
3049
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003050 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003051 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003052 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003053 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003054 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003055 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003056 }
3057 if (value_location.IsConstant()) {
3058 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3059 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3060 } else {
3061 Register value = value_location.AsRegisterPairLow<Register>();
3062 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003063 }
3064 break;
3065 }
3066
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003067 case DataType::Type::kFloat32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003068 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003069 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003070 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003071 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003072 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003073 }
3074 if (value_location.IsConstant()) {
3075 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3076 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3077 } else {
3078 FRegister value = value_location.AsFpuRegister<FRegister>();
3079 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003080 }
3081 break;
3082 }
3083
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003084 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003085 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003086 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003087 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003088 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003089 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003090 }
3091 if (value_location.IsConstant()) {
3092 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3093 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3094 } else {
3095 FRegister value = value_location.AsFpuRegister<FRegister>();
3096 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003097 }
3098 break;
3099 }
3100
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003101 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003102 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3103 UNREACHABLE();
3104 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003105}
3106
3107void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003108 RegisterSet caller_saves = RegisterSet::Empty();
3109 InvokeRuntimeCallingConvention calling_convention;
3110 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3111 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3112 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003113 locations->SetInAt(0, Location::RequiresRegister());
3114 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003115}
3116
3117void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3118 LocationSummary* locations = instruction->GetLocations();
3119 BoundsCheckSlowPathMIPS* slow_path =
3120 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3121 codegen_->AddSlowPath(slow_path);
3122
3123 Register index = locations->InAt(0).AsRegister<Register>();
3124 Register length = locations->InAt(1).AsRegister<Register>();
3125
3126 // length is limited by the maximum positive signed 32-bit integer.
3127 // Unsigned comparison of length and index checks for index < 0
3128 // and for length <= index simultaneously.
3129 __ Bgeu(index, length, slow_path->GetEntryLabel());
3130}
3131
Alexey Frunze15958152017-02-09 19:08:30 -08003132// Temp is used for read barrier.
3133static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3134 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003135 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003136 (kUseBakerReadBarrier ||
3137 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3138 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3139 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3140 return 1;
3141 }
3142 return 0;
3143}
3144
3145// Extra temp is used for read barrier.
3146static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3147 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3148}
3149
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003150void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003151 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3152 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3153
3154 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3155 switch (type_check_kind) {
3156 case TypeCheckKind::kExactCheck:
3157 case TypeCheckKind::kAbstractClassCheck:
3158 case TypeCheckKind::kClassHierarchyCheck:
3159 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003160 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003161 ? LocationSummary::kCallOnSlowPath
3162 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3163 break;
3164 case TypeCheckKind::kArrayCheck:
3165 case TypeCheckKind::kUnresolvedCheck:
3166 case TypeCheckKind::kInterfaceCheck:
3167 call_kind = LocationSummary::kCallOnSlowPath;
3168 break;
3169 }
3170
3171 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003172 locations->SetInAt(0, Location::RequiresRegister());
3173 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003174 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003175}
3176
3177void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003178 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003179 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003180 Location obj_loc = locations->InAt(0);
3181 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003182 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003183 Location temp_loc = locations->GetTemp(0);
3184 Register temp = temp_loc.AsRegister<Register>();
3185 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3186 DCHECK_LE(num_temps, 2u);
3187 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003188 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3189 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3190 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3191 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3192 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3193 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3194 const uint32_t object_array_data_offset =
3195 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3196 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003197
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003198 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3199 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3200 // read barriers is done for performance and code size reasons.
3201 bool is_type_check_slow_path_fatal = false;
3202 if (!kEmitCompilerReadBarrier) {
3203 is_type_check_slow_path_fatal =
3204 (type_check_kind == TypeCheckKind::kExactCheck ||
3205 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3206 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3207 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3208 !instruction->CanThrowIntoCatchBlock();
3209 }
3210 SlowPathCodeMIPS* slow_path =
3211 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3212 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003213 codegen_->AddSlowPath(slow_path);
3214
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003215 // Avoid this check if we know `obj` is not null.
3216 if (instruction->MustDoNullCheck()) {
3217 __ Beqz(obj, &done);
3218 }
3219
3220 switch (type_check_kind) {
3221 case TypeCheckKind::kExactCheck:
3222 case TypeCheckKind::kArrayCheck: {
3223 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003224 GenerateReferenceLoadTwoRegisters(instruction,
3225 temp_loc,
3226 obj_loc,
3227 class_offset,
3228 maybe_temp2_loc,
3229 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003230 // Jump to slow path for throwing the exception or doing a
3231 // more involved array check.
3232 __ Bne(temp, cls, slow_path->GetEntryLabel());
3233 break;
3234 }
3235
3236 case TypeCheckKind::kAbstractClassCheck: {
3237 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003238 GenerateReferenceLoadTwoRegisters(instruction,
3239 temp_loc,
3240 obj_loc,
3241 class_offset,
3242 maybe_temp2_loc,
3243 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003244 // If the class is abstract, we eagerly fetch the super class of the
3245 // object to avoid doing a comparison we know will fail.
3246 MipsLabel loop;
3247 __ Bind(&loop);
3248 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003249 GenerateReferenceLoadOneRegister(instruction,
3250 temp_loc,
3251 super_offset,
3252 maybe_temp2_loc,
3253 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003254 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3255 // exception.
3256 __ Beqz(temp, slow_path->GetEntryLabel());
3257 // Otherwise, compare the classes.
3258 __ Bne(temp, cls, &loop);
3259 break;
3260 }
3261
3262 case TypeCheckKind::kClassHierarchyCheck: {
3263 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003264 GenerateReferenceLoadTwoRegisters(instruction,
3265 temp_loc,
3266 obj_loc,
3267 class_offset,
3268 maybe_temp2_loc,
3269 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003270 // Walk over the class hierarchy to find a match.
3271 MipsLabel loop;
3272 __ Bind(&loop);
3273 __ Beq(temp, cls, &done);
3274 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003275 GenerateReferenceLoadOneRegister(instruction,
3276 temp_loc,
3277 super_offset,
3278 maybe_temp2_loc,
3279 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003280 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3281 // exception. Otherwise, jump to the beginning of the loop.
3282 __ Bnez(temp, &loop);
3283 __ B(slow_path->GetEntryLabel());
3284 break;
3285 }
3286
3287 case TypeCheckKind::kArrayObjectCheck: {
3288 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003289 GenerateReferenceLoadTwoRegisters(instruction,
3290 temp_loc,
3291 obj_loc,
3292 class_offset,
3293 maybe_temp2_loc,
3294 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003295 // Do an exact check.
3296 __ Beq(temp, cls, &done);
3297 // Otherwise, we need to check that the object's class is a non-primitive array.
3298 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003299 GenerateReferenceLoadOneRegister(instruction,
3300 temp_loc,
3301 component_offset,
3302 maybe_temp2_loc,
3303 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003304 // If the component type is null, jump to the slow path to throw the exception.
3305 __ Beqz(temp, slow_path->GetEntryLabel());
3306 // Otherwise, the object is indeed an array, further check that this component
3307 // type is not a primitive type.
3308 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3309 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3310 __ Bnez(temp, slow_path->GetEntryLabel());
3311 break;
3312 }
3313
3314 case TypeCheckKind::kUnresolvedCheck:
3315 // We always go into the type check slow path for the unresolved check case.
3316 // We cannot directly call the CheckCast runtime entry point
3317 // without resorting to a type checking slow path here (i.e. by
3318 // calling InvokeRuntime directly), as it would require to
3319 // assign fixed registers for the inputs of this HInstanceOf
3320 // instruction (following the runtime calling convention), which
3321 // might be cluttered by the potential first read barrier
3322 // emission at the beginning of this method.
3323 __ B(slow_path->GetEntryLabel());
3324 break;
3325
3326 case TypeCheckKind::kInterfaceCheck: {
3327 // Avoid read barriers to improve performance of the fast path. We can not get false
3328 // positives by doing this.
3329 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003330 GenerateReferenceLoadTwoRegisters(instruction,
3331 temp_loc,
3332 obj_loc,
3333 class_offset,
3334 maybe_temp2_loc,
3335 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003336 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003337 GenerateReferenceLoadTwoRegisters(instruction,
3338 temp_loc,
3339 temp_loc,
3340 iftable_offset,
3341 maybe_temp2_loc,
3342 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003343 // Iftable is never null.
3344 __ Lw(TMP, temp, array_length_offset);
3345 // Loop through the iftable and check if any class matches.
3346 MipsLabel loop;
3347 __ Bind(&loop);
3348 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3349 __ Beqz(TMP, slow_path->GetEntryLabel());
3350 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3351 __ MaybeUnpoisonHeapReference(AT);
3352 // Go to next interface.
3353 __ Addiu(TMP, TMP, -2);
3354 // Compare the classes and continue the loop if they do not match.
3355 __ Bne(AT, cls, &loop);
3356 break;
3357 }
3358 }
3359
3360 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003361 __ Bind(slow_path->GetExitLabel());
3362}
3363
3364void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3365 LocationSummary* locations =
3366 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3367 locations->SetInAt(0, Location::RequiresRegister());
3368 if (check->HasUses()) {
3369 locations->SetOut(Location::SameAsFirstInput());
3370 }
3371}
3372
3373void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3374 // We assume the class is not null.
3375 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3376 check->GetLoadClass(),
3377 check,
3378 check->GetDexPc(),
3379 true);
3380 codegen_->AddSlowPath(slow_path);
3381 GenerateClassInitializationCheck(slow_path,
3382 check->GetLocations()->InAt(0).AsRegister<Register>());
3383}
3384
3385void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003386 DataType::Type in_type = compare->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003387
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003388 LocationSummary* locations =
3389 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003390
3391 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003392 case DataType::Type::kBool:
3393 case DataType::Type::kInt8:
3394 case DataType::Type::kInt16:
3395 case DataType::Type::kUint16:
3396 case DataType::Type::kInt32:
Alexey Frunzee7697712016-09-15 21:37:49 -07003397 locations->SetInAt(0, Location::RequiresRegister());
3398 locations->SetInAt(1, Location::RequiresRegister());
3399 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3400 break;
3401
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003402 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003403 locations->SetInAt(0, Location::RequiresRegister());
3404 locations->SetInAt(1, Location::RequiresRegister());
3405 // Output overlaps because it is written before doing the low comparison.
3406 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3407 break;
3408
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003409 case DataType::Type::kFloat32:
3410 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003411 locations->SetInAt(0, Location::RequiresFpuRegister());
3412 locations->SetInAt(1, Location::RequiresFpuRegister());
3413 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003414 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003415
3416 default:
3417 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3418 }
3419}
3420
3421void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3422 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003423 Register res = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003424 DataType::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003425 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003426
3427 // 0 if: left == right
3428 // 1 if: left > right
3429 // -1 if: left < right
3430 switch (in_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003431 case DataType::Type::kBool:
3432 case DataType::Type::kInt8:
3433 case DataType::Type::kInt16:
3434 case DataType::Type::kUint16:
3435 case DataType::Type::kInt32: {
Aart Bika19616e2016-02-01 18:57:58 -08003436 Register lhs = locations->InAt(0).AsRegister<Register>();
3437 Register rhs = locations->InAt(1).AsRegister<Register>();
3438 __ Slt(TMP, lhs, rhs);
3439 __ Slt(res, rhs, lhs);
3440 __ Subu(res, res, TMP);
3441 break;
3442 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003443 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003444 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003445 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3446 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3447 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3448 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3449 // TODO: more efficient (direct) comparison with a constant.
3450 __ Slt(TMP, lhs_high, rhs_high);
3451 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3452 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3453 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3454 __ Sltu(TMP, lhs_low, rhs_low);
3455 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3456 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3457 __ Bind(&done);
3458 break;
3459 }
3460
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003461 case DataType::Type::kFloat32: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003462 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003463 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3464 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3465 MipsLabel done;
3466 if (isR6) {
3467 __ CmpEqS(FTMP, lhs, rhs);
3468 __ LoadConst32(res, 0);
3469 __ Bc1nez(FTMP, &done);
3470 if (gt_bias) {
3471 __ CmpLtS(FTMP, lhs, rhs);
3472 __ LoadConst32(res, -1);
3473 __ Bc1nez(FTMP, &done);
3474 __ LoadConst32(res, 1);
3475 } else {
3476 __ CmpLtS(FTMP, rhs, lhs);
3477 __ LoadConst32(res, 1);
3478 __ Bc1nez(FTMP, &done);
3479 __ LoadConst32(res, -1);
3480 }
3481 } else {
3482 if (gt_bias) {
3483 __ ColtS(0, lhs, rhs);
3484 __ LoadConst32(res, -1);
3485 __ Bc1t(0, &done);
3486 __ CeqS(0, lhs, rhs);
3487 __ LoadConst32(res, 1);
3488 __ Movt(res, ZERO, 0);
3489 } else {
3490 __ ColtS(0, rhs, lhs);
3491 __ LoadConst32(res, 1);
3492 __ Bc1t(0, &done);
3493 __ CeqS(0, lhs, rhs);
3494 __ LoadConst32(res, -1);
3495 __ Movt(res, ZERO, 0);
3496 }
3497 }
3498 __ Bind(&done);
3499 break;
3500 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003501 case DataType::Type::kFloat64: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003502 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003503 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3504 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3505 MipsLabel done;
3506 if (isR6) {
3507 __ CmpEqD(FTMP, lhs, rhs);
3508 __ LoadConst32(res, 0);
3509 __ Bc1nez(FTMP, &done);
3510 if (gt_bias) {
3511 __ CmpLtD(FTMP, lhs, rhs);
3512 __ LoadConst32(res, -1);
3513 __ Bc1nez(FTMP, &done);
3514 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003515 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003516 __ CmpLtD(FTMP, rhs, lhs);
3517 __ LoadConst32(res, 1);
3518 __ Bc1nez(FTMP, &done);
3519 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003520 }
3521 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003522 if (gt_bias) {
3523 __ ColtD(0, lhs, rhs);
3524 __ LoadConst32(res, -1);
3525 __ Bc1t(0, &done);
3526 __ CeqD(0, lhs, rhs);
3527 __ LoadConst32(res, 1);
3528 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003529 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003530 __ ColtD(0, rhs, lhs);
3531 __ LoadConst32(res, 1);
3532 __ Bc1t(0, &done);
3533 __ CeqD(0, lhs, rhs);
3534 __ LoadConst32(res, -1);
3535 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003536 }
3537 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003538 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003539 break;
3540 }
3541
3542 default:
3543 LOG(FATAL) << "Unimplemented compare type " << in_type;
3544 }
3545}
3546
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003547void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003548 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003549 switch (instruction->InputAt(0)->GetType()) {
3550 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003551 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003552 locations->SetInAt(0, Location::RequiresRegister());
3553 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3554 break;
3555
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003556 case DataType::Type::kFloat32:
3557 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003558 locations->SetInAt(0, Location::RequiresFpuRegister());
3559 locations->SetInAt(1, Location::RequiresFpuRegister());
3560 break;
3561 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003562 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003563 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3564 }
3565}
3566
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003567void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003568 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003569 return;
3570 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003571
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003572 DataType::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003573 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003574
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003575 switch (type) {
3576 default:
3577 // Integer case.
3578 GenerateIntCompare(instruction->GetCondition(), locations);
3579 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003580
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003581 case DataType::Type::kInt64:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003582 GenerateLongCompare(instruction->GetCondition(), locations);
3583 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003584
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003585 case DataType::Type::kFloat32:
3586 case DataType::Type::kFloat64:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003587 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3588 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003589 }
3590}
3591
Alexey Frunze7e99e052015-11-24 19:28:01 -08003592void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3593 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003594 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003595
3596 LocationSummary* locations = instruction->GetLocations();
3597 Location second = locations->InAt(1);
3598 DCHECK(second.IsConstant());
3599
3600 Register out = locations->Out().AsRegister<Register>();
3601 Register dividend = locations->InAt(0).AsRegister<Register>();
3602 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3603 DCHECK(imm == 1 || imm == -1);
3604
3605 if (instruction->IsRem()) {
3606 __ Move(out, ZERO);
3607 } else {
3608 if (imm == -1) {
3609 __ Subu(out, ZERO, dividend);
3610 } else if (out != dividend) {
3611 __ Move(out, dividend);
3612 }
3613 }
3614}
3615
3616void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3617 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003618 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003619
3620 LocationSummary* locations = instruction->GetLocations();
3621 Location second = locations->InAt(1);
3622 DCHECK(second.IsConstant());
3623
3624 Register out = locations->Out().AsRegister<Register>();
3625 Register dividend = locations->InAt(0).AsRegister<Register>();
3626 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003627 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003628 int ctz_imm = CTZ(abs_imm);
3629
3630 if (instruction->IsDiv()) {
3631 if (ctz_imm == 1) {
3632 // Fast path for division by +/-2, which is very common.
3633 __ Srl(TMP, dividend, 31);
3634 } else {
3635 __ Sra(TMP, dividend, 31);
3636 __ Srl(TMP, TMP, 32 - ctz_imm);
3637 }
3638 __ Addu(out, dividend, TMP);
3639 __ Sra(out, out, ctz_imm);
3640 if (imm < 0) {
3641 __ Subu(out, ZERO, out);
3642 }
3643 } else {
3644 if (ctz_imm == 1) {
3645 // Fast path for modulo +/-2, which is very common.
3646 __ Sra(TMP, dividend, 31);
3647 __ Subu(out, dividend, TMP);
3648 __ Andi(out, out, 1);
3649 __ Addu(out, out, TMP);
3650 } else {
3651 __ Sra(TMP, dividend, 31);
3652 __ Srl(TMP, TMP, 32 - ctz_imm);
3653 __ Addu(out, dividend, TMP);
3654 if (IsUint<16>(abs_imm - 1)) {
3655 __ Andi(out, out, abs_imm - 1);
3656 } else {
3657 __ Sll(out, out, 32 - ctz_imm);
3658 __ Srl(out, out, 32 - ctz_imm);
3659 }
3660 __ Subu(out, out, TMP);
3661 }
3662 }
3663}
3664
3665void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3666 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003667 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003668
3669 LocationSummary* locations = instruction->GetLocations();
3670 Location second = locations->InAt(1);
3671 DCHECK(second.IsConstant());
3672
3673 Register out = locations->Out().AsRegister<Register>();
3674 Register dividend = locations->InAt(0).AsRegister<Register>();
3675 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3676
3677 int64_t magic;
3678 int shift;
3679 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3680
3681 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3682
3683 __ LoadConst32(TMP, magic);
3684 if (isR6) {
3685 __ MuhR6(TMP, dividend, TMP);
3686 } else {
3687 __ MultR2(dividend, TMP);
3688 __ Mfhi(TMP);
3689 }
3690 if (imm > 0 && magic < 0) {
3691 __ Addu(TMP, TMP, dividend);
3692 } else if (imm < 0 && magic > 0) {
3693 __ Subu(TMP, TMP, dividend);
3694 }
3695
3696 if (shift != 0) {
3697 __ Sra(TMP, TMP, shift);
3698 }
3699
3700 if (instruction->IsDiv()) {
3701 __ Sra(out, TMP, 31);
3702 __ Subu(out, TMP, out);
3703 } else {
3704 __ Sra(AT, TMP, 31);
3705 __ Subu(AT, TMP, AT);
3706 __ LoadConst32(TMP, imm);
3707 if (isR6) {
3708 __ MulR6(TMP, AT, TMP);
3709 } else {
3710 __ MulR2(TMP, AT, TMP);
3711 }
3712 __ Subu(out, dividend, TMP);
3713 }
3714}
3715
3716void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3717 DCHECK(instruction->IsDiv() || instruction->IsRem());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003718 DCHECK_EQ(instruction->GetResultType(), DataType::Type::kInt32);
Alexey Frunze7e99e052015-11-24 19:28:01 -08003719
3720 LocationSummary* locations = instruction->GetLocations();
3721 Register out = locations->Out().AsRegister<Register>();
3722 Location second = locations->InAt(1);
3723
3724 if (second.IsConstant()) {
3725 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3726 if (imm == 0) {
3727 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3728 } else if (imm == 1 || imm == -1) {
3729 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003730 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003731 DivRemByPowerOfTwo(instruction);
3732 } else {
3733 DCHECK(imm <= -2 || imm >= 2);
3734 GenerateDivRemWithAnyConstant(instruction);
3735 }
3736 } else {
3737 Register dividend = locations->InAt(0).AsRegister<Register>();
3738 Register divisor = second.AsRegister<Register>();
3739 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3740 if (instruction->IsDiv()) {
3741 if (isR6) {
3742 __ DivR6(out, dividend, divisor);
3743 } else {
3744 __ DivR2(out, dividend, divisor);
3745 }
3746 } else {
3747 if (isR6) {
3748 __ ModR6(out, dividend, divisor);
3749 } else {
3750 __ ModR2(out, dividend, divisor);
3751 }
3752 }
3753 }
3754}
3755
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003756void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003757 DataType::Type type = div->GetResultType();
3758 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt64)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003759 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003760 : LocationSummary::kNoCall;
3761
3762 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3763
3764 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003765 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003766 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003767 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003768 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3769 break;
3770
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003771 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003772 InvokeRuntimeCallingConvention calling_convention;
3773 locations->SetInAt(0, Location::RegisterPairLocation(
3774 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3775 locations->SetInAt(1, Location::RegisterPairLocation(
3776 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3777 locations->SetOut(calling_convention.GetReturnLocation(type));
3778 break;
3779 }
3780
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003781 case DataType::Type::kFloat32:
3782 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003783 locations->SetInAt(0, Location::RequiresFpuRegister());
3784 locations->SetInAt(1, Location::RequiresFpuRegister());
3785 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3786 break;
3787
3788 default:
3789 LOG(FATAL) << "Unexpected div type " << type;
3790 }
3791}
3792
3793void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003794 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003795 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003796
3797 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003798 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08003799 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003800 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003801 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003802 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003803 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3804 break;
3805 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003806 case DataType::Type::kFloat32:
3807 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003808 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3809 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3810 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003811 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003812 __ DivS(dst, lhs, rhs);
3813 } else {
3814 __ DivD(dst, lhs, rhs);
3815 }
3816 break;
3817 }
3818 default:
3819 LOG(FATAL) << "Unexpected div type " << type;
3820 }
3821}
3822
3823void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003824 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003825 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003826}
3827
3828void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3829 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3830 codegen_->AddSlowPath(slow_path);
3831 Location value = instruction->GetLocations()->InAt(0);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003832 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003833
3834 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003835 case DataType::Type::kBool:
3836 case DataType::Type::kInt8:
3837 case DataType::Type::kUint16:
3838 case DataType::Type::kInt16:
3839 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003840 if (value.IsConstant()) {
3841 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3842 __ B(slow_path->GetEntryLabel());
3843 } else {
3844 // A division by a non-null constant is valid. We don't need to perform
3845 // any check, so simply fall through.
3846 }
3847 } else {
3848 DCHECK(value.IsRegister()) << value;
3849 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3850 }
3851 break;
3852 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01003853 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003854 if (value.IsConstant()) {
3855 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3856 __ B(slow_path->GetEntryLabel());
3857 } else {
3858 // A division by a non-null constant is valid. We don't need to perform
3859 // any check, so simply fall through.
3860 }
3861 } else {
3862 DCHECK(value.IsRegisterPair()) << value;
3863 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3864 __ Beqz(TMP, slow_path->GetEntryLabel());
3865 }
3866 break;
3867 }
3868 default:
3869 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3870 }
3871}
3872
3873void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3874 LocationSummary* locations =
3875 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3876 locations->SetOut(Location::ConstantLocation(constant));
3877}
3878
3879void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3880 // Will be generated at use site.
3881}
3882
3883void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3884 exit->SetLocations(nullptr);
3885}
3886
3887void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3888}
3889
3890void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3891 LocationSummary* locations =
3892 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3893 locations->SetOut(Location::ConstantLocation(constant));
3894}
3895
3896void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3897 // Will be generated at use site.
3898}
3899
3900void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3901 got->SetLocations(nullptr);
3902}
3903
3904void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3905 DCHECK(!successor->IsExitBlock());
3906 HBasicBlock* block = got->GetBlock();
3907 HInstruction* previous = got->GetPrevious();
3908 HLoopInformation* info = block->GetLoopInformation();
3909
3910 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3911 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3912 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3913 return;
3914 }
3915 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3916 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3917 }
3918 if (!codegen_->GoesToNextBlock(block, successor)) {
3919 __ B(codegen_->GetLabelOf(successor));
3920 }
3921}
3922
3923void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3924 HandleGoto(got, got->GetSuccessor());
3925}
3926
3927void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3928 try_boundary->SetLocations(nullptr);
3929}
3930
3931void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3932 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3933 if (!successor->IsExitBlock()) {
3934 HandleGoto(try_boundary, successor);
3935 }
3936}
3937
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003938void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3939 LocationSummary* locations) {
3940 Register dst = locations->Out().AsRegister<Register>();
3941 Register lhs = locations->InAt(0).AsRegister<Register>();
3942 Location rhs_location = locations->InAt(1);
3943 Register rhs_reg = ZERO;
3944 int64_t rhs_imm = 0;
3945 bool use_imm = rhs_location.IsConstant();
3946 if (use_imm) {
3947 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3948 } else {
3949 rhs_reg = rhs_location.AsRegister<Register>();
3950 }
3951
3952 switch (cond) {
3953 case kCondEQ:
3954 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003955 if (use_imm && IsInt<16>(-rhs_imm)) {
3956 if (rhs_imm == 0) {
3957 if (cond == kCondEQ) {
3958 __ Sltiu(dst, lhs, 1);
3959 } else {
3960 __ Sltu(dst, ZERO, lhs);
3961 }
3962 } else {
3963 __ Addiu(dst, lhs, -rhs_imm);
3964 if (cond == kCondEQ) {
3965 __ Sltiu(dst, dst, 1);
3966 } else {
3967 __ Sltu(dst, ZERO, dst);
3968 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003969 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003970 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003971 if (use_imm && IsUint<16>(rhs_imm)) {
3972 __ Xori(dst, lhs, rhs_imm);
3973 } else {
3974 if (use_imm) {
3975 rhs_reg = TMP;
3976 __ LoadConst32(rhs_reg, rhs_imm);
3977 }
3978 __ Xor(dst, lhs, rhs_reg);
3979 }
3980 if (cond == kCondEQ) {
3981 __ Sltiu(dst, dst, 1);
3982 } else {
3983 __ Sltu(dst, ZERO, dst);
3984 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003985 }
3986 break;
3987
3988 case kCondLT:
3989 case kCondGE:
3990 if (use_imm && IsInt<16>(rhs_imm)) {
3991 __ Slti(dst, lhs, rhs_imm);
3992 } else {
3993 if (use_imm) {
3994 rhs_reg = TMP;
3995 __ LoadConst32(rhs_reg, rhs_imm);
3996 }
3997 __ Slt(dst, lhs, rhs_reg);
3998 }
3999 if (cond == kCondGE) {
4000 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4001 // only the slt instruction but no sge.
4002 __ Xori(dst, dst, 1);
4003 }
4004 break;
4005
4006 case kCondLE:
4007 case kCondGT:
4008 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4009 // Simulate lhs <= rhs via lhs < rhs + 1.
4010 __ Slti(dst, lhs, rhs_imm + 1);
4011 if (cond == kCondGT) {
4012 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4013 // only the slti instruction but no sgti.
4014 __ Xori(dst, dst, 1);
4015 }
4016 } else {
4017 if (use_imm) {
4018 rhs_reg = TMP;
4019 __ LoadConst32(rhs_reg, rhs_imm);
4020 }
4021 __ Slt(dst, rhs_reg, lhs);
4022 if (cond == kCondLE) {
4023 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4024 // only the slt instruction but no sle.
4025 __ Xori(dst, dst, 1);
4026 }
4027 }
4028 break;
4029
4030 case kCondB:
4031 case kCondAE:
4032 if (use_imm && IsInt<16>(rhs_imm)) {
4033 // Sltiu sign-extends its 16-bit immediate operand before
4034 // the comparison and thus lets us compare directly with
4035 // unsigned values in the ranges [0, 0x7fff] and
4036 // [0xffff8000, 0xffffffff].
4037 __ Sltiu(dst, lhs, rhs_imm);
4038 } else {
4039 if (use_imm) {
4040 rhs_reg = TMP;
4041 __ LoadConst32(rhs_reg, rhs_imm);
4042 }
4043 __ Sltu(dst, lhs, rhs_reg);
4044 }
4045 if (cond == kCondAE) {
4046 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4047 // only the sltu instruction but no sgeu.
4048 __ Xori(dst, dst, 1);
4049 }
4050 break;
4051
4052 case kCondBE:
4053 case kCondA:
4054 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4055 // Simulate lhs <= rhs via lhs < rhs + 1.
4056 // Note that this only works if rhs + 1 does not overflow
4057 // to 0, hence the check above.
4058 // Sltiu sign-extends its 16-bit immediate operand before
4059 // the comparison and thus lets us compare directly with
4060 // unsigned values in the ranges [0, 0x7fff] and
4061 // [0xffff8000, 0xffffffff].
4062 __ Sltiu(dst, lhs, rhs_imm + 1);
4063 if (cond == kCondA) {
4064 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4065 // only the sltiu instruction but no sgtiu.
4066 __ Xori(dst, dst, 1);
4067 }
4068 } else {
4069 if (use_imm) {
4070 rhs_reg = TMP;
4071 __ LoadConst32(rhs_reg, rhs_imm);
4072 }
4073 __ Sltu(dst, rhs_reg, lhs);
4074 if (cond == kCondBE) {
4075 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4076 // only the sltu instruction but no sleu.
4077 __ Xori(dst, dst, 1);
4078 }
4079 }
4080 break;
4081 }
4082}
4083
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004084bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4085 LocationSummary* input_locations,
4086 Register dst) {
4087 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4088 Location rhs_location = input_locations->InAt(1);
4089 Register rhs_reg = ZERO;
4090 int64_t rhs_imm = 0;
4091 bool use_imm = rhs_location.IsConstant();
4092 if (use_imm) {
4093 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4094 } else {
4095 rhs_reg = rhs_location.AsRegister<Register>();
4096 }
4097
4098 switch (cond) {
4099 case kCondEQ:
4100 case kCondNE:
4101 if (use_imm && IsInt<16>(-rhs_imm)) {
4102 __ Addiu(dst, lhs, -rhs_imm);
4103 } else if (use_imm && IsUint<16>(rhs_imm)) {
4104 __ Xori(dst, lhs, rhs_imm);
4105 } else {
4106 if (use_imm) {
4107 rhs_reg = TMP;
4108 __ LoadConst32(rhs_reg, rhs_imm);
4109 }
4110 __ Xor(dst, lhs, rhs_reg);
4111 }
4112 return (cond == kCondEQ);
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 return (cond == kCondGE);
4126
4127 case kCondLE:
4128 case kCondGT:
4129 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4130 // Simulate lhs <= rhs via lhs < rhs + 1.
4131 __ Slti(dst, lhs, rhs_imm + 1);
4132 return (cond == kCondGT);
4133 } else {
4134 if (use_imm) {
4135 rhs_reg = TMP;
4136 __ LoadConst32(rhs_reg, rhs_imm);
4137 }
4138 __ Slt(dst, rhs_reg, lhs);
4139 return (cond == kCondLE);
4140 }
4141
4142 case kCondB:
4143 case kCondAE:
4144 if (use_imm && IsInt<16>(rhs_imm)) {
4145 // Sltiu sign-extends its 16-bit immediate operand before
4146 // the comparison and thus lets us compare directly with
4147 // unsigned values in the ranges [0, 0x7fff] and
4148 // [0xffff8000, 0xffffffff].
4149 __ Sltiu(dst, lhs, rhs_imm);
4150 } else {
4151 if (use_imm) {
4152 rhs_reg = TMP;
4153 __ LoadConst32(rhs_reg, rhs_imm);
4154 }
4155 __ Sltu(dst, lhs, rhs_reg);
4156 }
4157 return (cond == kCondAE);
4158
4159 case kCondBE:
4160 case kCondA:
4161 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4162 // Simulate lhs <= rhs via lhs < rhs + 1.
4163 // Note that this only works if rhs + 1 does not overflow
4164 // to 0, hence the check above.
4165 // Sltiu sign-extends its 16-bit immediate operand before
4166 // the comparison and thus lets us compare directly with
4167 // unsigned values in the ranges [0, 0x7fff] and
4168 // [0xffff8000, 0xffffffff].
4169 __ Sltiu(dst, lhs, rhs_imm + 1);
4170 return (cond == kCondA);
4171 } else {
4172 if (use_imm) {
4173 rhs_reg = TMP;
4174 __ LoadConst32(rhs_reg, rhs_imm);
4175 }
4176 __ Sltu(dst, rhs_reg, lhs);
4177 return (cond == kCondBE);
4178 }
4179 }
4180}
4181
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004182void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4183 LocationSummary* locations,
4184 MipsLabel* label) {
4185 Register lhs = locations->InAt(0).AsRegister<Register>();
4186 Location rhs_location = locations->InAt(1);
4187 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004188 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004189 bool use_imm = rhs_location.IsConstant();
4190 if (use_imm) {
4191 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4192 } else {
4193 rhs_reg = rhs_location.AsRegister<Register>();
4194 }
4195
4196 if (use_imm && rhs_imm == 0) {
4197 switch (cond) {
4198 case kCondEQ:
4199 case kCondBE: // <= 0 if zero
4200 __ Beqz(lhs, label);
4201 break;
4202 case kCondNE:
4203 case kCondA: // > 0 if non-zero
4204 __ Bnez(lhs, label);
4205 break;
4206 case kCondLT:
4207 __ Bltz(lhs, label);
4208 break;
4209 case kCondGE:
4210 __ Bgez(lhs, label);
4211 break;
4212 case kCondLE:
4213 __ Blez(lhs, label);
4214 break;
4215 case kCondGT:
4216 __ Bgtz(lhs, label);
4217 break;
4218 case kCondB: // always false
4219 break;
4220 case kCondAE: // always true
4221 __ B(label);
4222 break;
4223 }
4224 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004225 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4226 if (isR6 || !use_imm) {
4227 if (use_imm) {
4228 rhs_reg = TMP;
4229 __ LoadConst32(rhs_reg, rhs_imm);
4230 }
4231 switch (cond) {
4232 case kCondEQ:
4233 __ Beq(lhs, rhs_reg, label);
4234 break;
4235 case kCondNE:
4236 __ Bne(lhs, rhs_reg, label);
4237 break;
4238 case kCondLT:
4239 __ Blt(lhs, rhs_reg, label);
4240 break;
4241 case kCondGE:
4242 __ Bge(lhs, rhs_reg, label);
4243 break;
4244 case kCondLE:
4245 __ Bge(rhs_reg, lhs, label);
4246 break;
4247 case kCondGT:
4248 __ Blt(rhs_reg, lhs, label);
4249 break;
4250 case kCondB:
4251 __ Bltu(lhs, rhs_reg, label);
4252 break;
4253 case kCondAE:
4254 __ Bgeu(lhs, rhs_reg, label);
4255 break;
4256 case kCondBE:
4257 __ Bgeu(rhs_reg, lhs, label);
4258 break;
4259 case kCondA:
4260 __ Bltu(rhs_reg, lhs, label);
4261 break;
4262 }
4263 } else {
4264 // Special cases for more efficient comparison with constants on R2.
4265 switch (cond) {
4266 case kCondEQ:
4267 __ LoadConst32(TMP, rhs_imm);
4268 __ Beq(lhs, TMP, label);
4269 break;
4270 case kCondNE:
4271 __ LoadConst32(TMP, rhs_imm);
4272 __ Bne(lhs, TMP, label);
4273 break;
4274 case kCondLT:
4275 if (IsInt<16>(rhs_imm)) {
4276 __ Slti(TMP, lhs, rhs_imm);
4277 __ Bnez(TMP, label);
4278 } else {
4279 __ LoadConst32(TMP, rhs_imm);
4280 __ Blt(lhs, TMP, label);
4281 }
4282 break;
4283 case kCondGE:
4284 if (IsInt<16>(rhs_imm)) {
4285 __ Slti(TMP, lhs, rhs_imm);
4286 __ Beqz(TMP, label);
4287 } else {
4288 __ LoadConst32(TMP, rhs_imm);
4289 __ Bge(lhs, TMP, label);
4290 }
4291 break;
4292 case kCondLE:
4293 if (IsInt<16>(rhs_imm + 1)) {
4294 // Simulate lhs <= rhs via lhs < rhs + 1.
4295 __ Slti(TMP, lhs, rhs_imm + 1);
4296 __ Bnez(TMP, label);
4297 } else {
4298 __ LoadConst32(TMP, rhs_imm);
4299 __ Bge(TMP, lhs, label);
4300 }
4301 break;
4302 case kCondGT:
4303 if (IsInt<16>(rhs_imm + 1)) {
4304 // Simulate lhs > rhs via !(lhs < rhs + 1).
4305 __ Slti(TMP, lhs, rhs_imm + 1);
4306 __ Beqz(TMP, label);
4307 } else {
4308 __ LoadConst32(TMP, rhs_imm);
4309 __ Blt(TMP, lhs, label);
4310 }
4311 break;
4312 case kCondB:
4313 if (IsInt<16>(rhs_imm)) {
4314 __ Sltiu(TMP, lhs, rhs_imm);
4315 __ Bnez(TMP, label);
4316 } else {
4317 __ LoadConst32(TMP, rhs_imm);
4318 __ Bltu(lhs, TMP, label);
4319 }
4320 break;
4321 case kCondAE:
4322 if (IsInt<16>(rhs_imm)) {
4323 __ Sltiu(TMP, lhs, rhs_imm);
4324 __ Beqz(TMP, label);
4325 } else {
4326 __ LoadConst32(TMP, rhs_imm);
4327 __ Bgeu(lhs, TMP, label);
4328 }
4329 break;
4330 case kCondBE:
4331 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4332 // Simulate lhs <= rhs via lhs < rhs + 1.
4333 // Note that this only works if rhs + 1 does not overflow
4334 // to 0, hence the check above.
4335 __ Sltiu(TMP, lhs, rhs_imm + 1);
4336 __ Bnez(TMP, label);
4337 } else {
4338 __ LoadConst32(TMP, rhs_imm);
4339 __ Bgeu(TMP, lhs, label);
4340 }
4341 break;
4342 case kCondA:
4343 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4344 // Simulate lhs > rhs via !(lhs < rhs + 1).
4345 // Note that this only works if rhs + 1 does not overflow
4346 // to 0, hence the check above.
4347 __ Sltiu(TMP, lhs, rhs_imm + 1);
4348 __ Beqz(TMP, label);
4349 } else {
4350 __ LoadConst32(TMP, rhs_imm);
4351 __ Bltu(TMP, lhs, label);
4352 }
4353 break;
4354 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004355 }
4356 }
4357}
4358
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004359void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4360 LocationSummary* locations) {
4361 Register dst = locations->Out().AsRegister<Register>();
4362 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4363 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4364 Location rhs_location = locations->InAt(1);
4365 Register rhs_high = ZERO;
4366 Register rhs_low = ZERO;
4367 int64_t imm = 0;
4368 uint32_t imm_high = 0;
4369 uint32_t imm_low = 0;
4370 bool use_imm = rhs_location.IsConstant();
4371 if (use_imm) {
4372 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4373 imm_high = High32Bits(imm);
4374 imm_low = Low32Bits(imm);
4375 } else {
4376 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4377 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4378 }
4379 if (use_imm && imm == 0) {
4380 switch (cond) {
4381 case kCondEQ:
4382 case kCondBE: // <= 0 if zero
4383 __ Or(dst, lhs_high, lhs_low);
4384 __ Sltiu(dst, dst, 1);
4385 break;
4386 case kCondNE:
4387 case kCondA: // > 0 if non-zero
4388 __ Or(dst, lhs_high, lhs_low);
4389 __ Sltu(dst, ZERO, dst);
4390 break;
4391 case kCondLT:
4392 __ Slt(dst, lhs_high, ZERO);
4393 break;
4394 case kCondGE:
4395 __ Slt(dst, lhs_high, ZERO);
4396 __ Xori(dst, dst, 1);
4397 break;
4398 case kCondLE:
4399 __ Or(TMP, lhs_high, lhs_low);
4400 __ Sra(AT, lhs_high, 31);
4401 __ Sltu(dst, AT, TMP);
4402 __ Xori(dst, dst, 1);
4403 break;
4404 case kCondGT:
4405 __ Or(TMP, lhs_high, lhs_low);
4406 __ Sra(AT, lhs_high, 31);
4407 __ Sltu(dst, AT, TMP);
4408 break;
4409 case kCondB: // always false
4410 __ Andi(dst, dst, 0);
4411 break;
4412 case kCondAE: // always true
4413 __ Ori(dst, ZERO, 1);
4414 break;
4415 }
4416 } else if (use_imm) {
4417 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4418 switch (cond) {
4419 case kCondEQ:
4420 __ LoadConst32(TMP, imm_high);
4421 __ Xor(TMP, TMP, lhs_high);
4422 __ LoadConst32(AT, imm_low);
4423 __ Xor(AT, AT, lhs_low);
4424 __ Or(dst, TMP, AT);
4425 __ Sltiu(dst, dst, 1);
4426 break;
4427 case kCondNE:
4428 __ LoadConst32(TMP, imm_high);
4429 __ Xor(TMP, TMP, lhs_high);
4430 __ LoadConst32(AT, imm_low);
4431 __ Xor(AT, AT, lhs_low);
4432 __ Or(dst, TMP, AT);
4433 __ Sltu(dst, ZERO, dst);
4434 break;
4435 case kCondLT:
4436 case kCondGE:
4437 if (dst == lhs_low) {
4438 __ LoadConst32(TMP, imm_low);
4439 __ Sltu(dst, lhs_low, TMP);
4440 }
4441 __ LoadConst32(TMP, imm_high);
4442 __ Slt(AT, lhs_high, TMP);
4443 __ Slt(TMP, TMP, lhs_high);
4444 if (dst != lhs_low) {
4445 __ LoadConst32(dst, imm_low);
4446 __ Sltu(dst, lhs_low, dst);
4447 }
4448 __ Slt(dst, TMP, dst);
4449 __ Or(dst, dst, AT);
4450 if (cond == kCondGE) {
4451 __ Xori(dst, dst, 1);
4452 }
4453 break;
4454 case kCondGT:
4455 case kCondLE:
4456 if (dst == lhs_low) {
4457 __ LoadConst32(TMP, imm_low);
4458 __ Sltu(dst, TMP, lhs_low);
4459 }
4460 __ LoadConst32(TMP, imm_high);
4461 __ Slt(AT, TMP, lhs_high);
4462 __ Slt(TMP, lhs_high, TMP);
4463 if (dst != lhs_low) {
4464 __ LoadConst32(dst, imm_low);
4465 __ Sltu(dst, dst, lhs_low);
4466 }
4467 __ Slt(dst, TMP, dst);
4468 __ Or(dst, dst, AT);
4469 if (cond == kCondLE) {
4470 __ Xori(dst, dst, 1);
4471 }
4472 break;
4473 case kCondB:
4474 case kCondAE:
4475 if (dst == lhs_low) {
4476 __ LoadConst32(TMP, imm_low);
4477 __ Sltu(dst, lhs_low, TMP);
4478 }
4479 __ LoadConst32(TMP, imm_high);
4480 __ Sltu(AT, lhs_high, TMP);
4481 __ Sltu(TMP, TMP, lhs_high);
4482 if (dst != lhs_low) {
4483 __ LoadConst32(dst, imm_low);
4484 __ Sltu(dst, lhs_low, dst);
4485 }
4486 __ Slt(dst, TMP, dst);
4487 __ Or(dst, dst, AT);
4488 if (cond == kCondAE) {
4489 __ Xori(dst, dst, 1);
4490 }
4491 break;
4492 case kCondA:
4493 case kCondBE:
4494 if (dst == lhs_low) {
4495 __ LoadConst32(TMP, imm_low);
4496 __ Sltu(dst, TMP, lhs_low);
4497 }
4498 __ LoadConst32(TMP, imm_high);
4499 __ Sltu(AT, TMP, lhs_high);
4500 __ Sltu(TMP, lhs_high, TMP);
4501 if (dst != lhs_low) {
4502 __ LoadConst32(dst, imm_low);
4503 __ Sltu(dst, dst, lhs_low);
4504 }
4505 __ Slt(dst, TMP, dst);
4506 __ Or(dst, dst, AT);
4507 if (cond == kCondBE) {
4508 __ Xori(dst, dst, 1);
4509 }
4510 break;
4511 }
4512 } else {
4513 switch (cond) {
4514 case kCondEQ:
4515 __ Xor(TMP, lhs_high, rhs_high);
4516 __ Xor(AT, lhs_low, rhs_low);
4517 __ Or(dst, TMP, AT);
4518 __ Sltiu(dst, dst, 1);
4519 break;
4520 case kCondNE:
4521 __ Xor(TMP, lhs_high, rhs_high);
4522 __ Xor(AT, lhs_low, rhs_low);
4523 __ Or(dst, TMP, AT);
4524 __ Sltu(dst, ZERO, dst);
4525 break;
4526 case kCondLT:
4527 case kCondGE:
4528 __ Slt(TMP, rhs_high, lhs_high);
4529 __ Sltu(AT, lhs_low, rhs_low);
4530 __ Slt(TMP, TMP, AT);
4531 __ Slt(AT, lhs_high, rhs_high);
4532 __ Or(dst, AT, TMP);
4533 if (cond == kCondGE) {
4534 __ Xori(dst, dst, 1);
4535 }
4536 break;
4537 case kCondGT:
4538 case kCondLE:
4539 __ Slt(TMP, lhs_high, rhs_high);
4540 __ Sltu(AT, rhs_low, lhs_low);
4541 __ Slt(TMP, TMP, AT);
4542 __ Slt(AT, rhs_high, lhs_high);
4543 __ Or(dst, AT, TMP);
4544 if (cond == kCondLE) {
4545 __ Xori(dst, dst, 1);
4546 }
4547 break;
4548 case kCondB:
4549 case kCondAE:
4550 __ Sltu(TMP, rhs_high, lhs_high);
4551 __ Sltu(AT, lhs_low, rhs_low);
4552 __ Slt(TMP, TMP, AT);
4553 __ Sltu(AT, lhs_high, rhs_high);
4554 __ Or(dst, AT, TMP);
4555 if (cond == kCondAE) {
4556 __ Xori(dst, dst, 1);
4557 }
4558 break;
4559 case kCondA:
4560 case kCondBE:
4561 __ Sltu(TMP, lhs_high, rhs_high);
4562 __ Sltu(AT, rhs_low, lhs_low);
4563 __ Slt(TMP, TMP, AT);
4564 __ Sltu(AT, rhs_high, lhs_high);
4565 __ Or(dst, AT, TMP);
4566 if (cond == kCondBE) {
4567 __ Xori(dst, dst, 1);
4568 }
4569 break;
4570 }
4571 }
4572}
4573
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004574void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4575 LocationSummary* locations,
4576 MipsLabel* label) {
4577 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4578 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4579 Location rhs_location = locations->InAt(1);
4580 Register rhs_high = ZERO;
4581 Register rhs_low = ZERO;
4582 int64_t imm = 0;
4583 uint32_t imm_high = 0;
4584 uint32_t imm_low = 0;
4585 bool use_imm = rhs_location.IsConstant();
4586 if (use_imm) {
4587 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4588 imm_high = High32Bits(imm);
4589 imm_low = Low32Bits(imm);
4590 } else {
4591 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4592 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4593 }
4594
4595 if (use_imm && imm == 0) {
4596 switch (cond) {
4597 case kCondEQ:
4598 case kCondBE: // <= 0 if zero
4599 __ Or(TMP, lhs_high, lhs_low);
4600 __ Beqz(TMP, label);
4601 break;
4602 case kCondNE:
4603 case kCondA: // > 0 if non-zero
4604 __ Or(TMP, lhs_high, lhs_low);
4605 __ Bnez(TMP, label);
4606 break;
4607 case kCondLT:
4608 __ Bltz(lhs_high, label);
4609 break;
4610 case kCondGE:
4611 __ Bgez(lhs_high, label);
4612 break;
4613 case kCondLE:
4614 __ Or(TMP, lhs_high, lhs_low);
4615 __ Sra(AT, lhs_high, 31);
4616 __ Bgeu(AT, TMP, label);
4617 break;
4618 case kCondGT:
4619 __ Or(TMP, lhs_high, lhs_low);
4620 __ Sra(AT, lhs_high, 31);
4621 __ Bltu(AT, TMP, label);
4622 break;
4623 case kCondB: // always false
4624 break;
4625 case kCondAE: // always true
4626 __ B(label);
4627 break;
4628 }
4629 } else if (use_imm) {
4630 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4631 switch (cond) {
4632 case kCondEQ:
4633 __ LoadConst32(TMP, imm_high);
4634 __ Xor(TMP, TMP, lhs_high);
4635 __ LoadConst32(AT, imm_low);
4636 __ Xor(AT, AT, lhs_low);
4637 __ Or(TMP, TMP, AT);
4638 __ Beqz(TMP, label);
4639 break;
4640 case kCondNE:
4641 __ LoadConst32(TMP, imm_high);
4642 __ Xor(TMP, TMP, lhs_high);
4643 __ LoadConst32(AT, imm_low);
4644 __ Xor(AT, AT, lhs_low);
4645 __ Or(TMP, TMP, AT);
4646 __ Bnez(TMP, label);
4647 break;
4648 case kCondLT:
4649 __ LoadConst32(TMP, imm_high);
4650 __ Blt(lhs_high, TMP, label);
4651 __ Slt(TMP, TMP, lhs_high);
4652 __ LoadConst32(AT, imm_low);
4653 __ Sltu(AT, lhs_low, AT);
4654 __ Blt(TMP, AT, label);
4655 break;
4656 case kCondGE:
4657 __ LoadConst32(TMP, imm_high);
4658 __ Blt(TMP, lhs_high, label);
4659 __ Slt(TMP, lhs_high, TMP);
4660 __ LoadConst32(AT, imm_low);
4661 __ Sltu(AT, lhs_low, AT);
4662 __ Or(TMP, TMP, AT);
4663 __ Beqz(TMP, label);
4664 break;
4665 case kCondLE:
4666 __ LoadConst32(TMP, imm_high);
4667 __ Blt(lhs_high, TMP, label);
4668 __ Slt(TMP, TMP, lhs_high);
4669 __ LoadConst32(AT, imm_low);
4670 __ Sltu(AT, AT, lhs_low);
4671 __ Or(TMP, TMP, AT);
4672 __ Beqz(TMP, label);
4673 break;
4674 case kCondGT:
4675 __ LoadConst32(TMP, imm_high);
4676 __ Blt(TMP, lhs_high, label);
4677 __ Slt(TMP, lhs_high, TMP);
4678 __ LoadConst32(AT, imm_low);
4679 __ Sltu(AT, AT, lhs_low);
4680 __ Blt(TMP, AT, label);
4681 break;
4682 case kCondB:
4683 __ LoadConst32(TMP, imm_high);
4684 __ Bltu(lhs_high, TMP, label);
4685 __ Sltu(TMP, TMP, lhs_high);
4686 __ LoadConst32(AT, imm_low);
4687 __ Sltu(AT, lhs_low, AT);
4688 __ Blt(TMP, AT, label);
4689 break;
4690 case kCondAE:
4691 __ LoadConst32(TMP, imm_high);
4692 __ Bltu(TMP, lhs_high, label);
4693 __ Sltu(TMP, lhs_high, TMP);
4694 __ LoadConst32(AT, imm_low);
4695 __ Sltu(AT, lhs_low, AT);
4696 __ Or(TMP, TMP, AT);
4697 __ Beqz(TMP, label);
4698 break;
4699 case kCondBE:
4700 __ LoadConst32(TMP, imm_high);
4701 __ Bltu(lhs_high, TMP, label);
4702 __ Sltu(TMP, TMP, lhs_high);
4703 __ LoadConst32(AT, imm_low);
4704 __ Sltu(AT, AT, lhs_low);
4705 __ Or(TMP, TMP, AT);
4706 __ Beqz(TMP, label);
4707 break;
4708 case kCondA:
4709 __ LoadConst32(TMP, imm_high);
4710 __ Bltu(TMP, lhs_high, label);
4711 __ Sltu(TMP, lhs_high, TMP);
4712 __ LoadConst32(AT, imm_low);
4713 __ Sltu(AT, AT, lhs_low);
4714 __ Blt(TMP, AT, label);
4715 break;
4716 }
4717 } else {
4718 switch (cond) {
4719 case kCondEQ:
4720 __ Xor(TMP, lhs_high, rhs_high);
4721 __ Xor(AT, lhs_low, rhs_low);
4722 __ Or(TMP, TMP, AT);
4723 __ Beqz(TMP, label);
4724 break;
4725 case kCondNE:
4726 __ Xor(TMP, lhs_high, rhs_high);
4727 __ Xor(AT, lhs_low, rhs_low);
4728 __ Or(TMP, TMP, AT);
4729 __ Bnez(TMP, label);
4730 break;
4731 case kCondLT:
4732 __ Blt(lhs_high, rhs_high, label);
4733 __ Slt(TMP, rhs_high, lhs_high);
4734 __ Sltu(AT, lhs_low, rhs_low);
4735 __ Blt(TMP, AT, label);
4736 break;
4737 case kCondGE:
4738 __ Blt(rhs_high, lhs_high, label);
4739 __ Slt(TMP, lhs_high, rhs_high);
4740 __ Sltu(AT, lhs_low, rhs_low);
4741 __ Or(TMP, TMP, AT);
4742 __ Beqz(TMP, label);
4743 break;
4744 case kCondLE:
4745 __ Blt(lhs_high, rhs_high, label);
4746 __ Slt(TMP, rhs_high, lhs_high);
4747 __ Sltu(AT, rhs_low, lhs_low);
4748 __ Or(TMP, TMP, AT);
4749 __ Beqz(TMP, label);
4750 break;
4751 case kCondGT:
4752 __ Blt(rhs_high, lhs_high, label);
4753 __ Slt(TMP, lhs_high, rhs_high);
4754 __ Sltu(AT, rhs_low, lhs_low);
4755 __ Blt(TMP, AT, label);
4756 break;
4757 case kCondB:
4758 __ Bltu(lhs_high, rhs_high, label);
4759 __ Sltu(TMP, rhs_high, lhs_high);
4760 __ Sltu(AT, lhs_low, rhs_low);
4761 __ Blt(TMP, AT, label);
4762 break;
4763 case kCondAE:
4764 __ Bltu(rhs_high, lhs_high, label);
4765 __ Sltu(TMP, lhs_high, rhs_high);
4766 __ Sltu(AT, lhs_low, rhs_low);
4767 __ Or(TMP, TMP, AT);
4768 __ Beqz(TMP, label);
4769 break;
4770 case kCondBE:
4771 __ Bltu(lhs_high, rhs_high, label);
4772 __ Sltu(TMP, rhs_high, lhs_high);
4773 __ Sltu(AT, rhs_low, lhs_low);
4774 __ Or(TMP, TMP, AT);
4775 __ Beqz(TMP, label);
4776 break;
4777 case kCondA:
4778 __ Bltu(rhs_high, lhs_high, label);
4779 __ Sltu(TMP, lhs_high, rhs_high);
4780 __ Sltu(AT, rhs_low, lhs_low);
4781 __ Blt(TMP, AT, label);
4782 break;
4783 }
4784 }
4785}
4786
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004787void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4788 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004789 DataType::Type type,
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004790 LocationSummary* locations) {
4791 Register dst = locations->Out().AsRegister<Register>();
4792 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4793 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4794 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004795 if (type == DataType::Type::kFloat32) {
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004796 if (isR6) {
4797 switch (cond) {
4798 case kCondEQ:
4799 __ CmpEqS(FTMP, lhs, rhs);
4800 __ Mfc1(dst, FTMP);
4801 __ Andi(dst, dst, 1);
4802 break;
4803 case kCondNE:
4804 __ CmpEqS(FTMP, lhs, rhs);
4805 __ Mfc1(dst, FTMP);
4806 __ Addiu(dst, dst, 1);
4807 break;
4808 case kCondLT:
4809 if (gt_bias) {
4810 __ CmpLtS(FTMP, lhs, rhs);
4811 } else {
4812 __ CmpUltS(FTMP, lhs, rhs);
4813 }
4814 __ Mfc1(dst, FTMP);
4815 __ Andi(dst, dst, 1);
4816 break;
4817 case kCondLE:
4818 if (gt_bias) {
4819 __ CmpLeS(FTMP, lhs, rhs);
4820 } else {
4821 __ CmpUleS(FTMP, lhs, rhs);
4822 }
4823 __ Mfc1(dst, FTMP);
4824 __ Andi(dst, dst, 1);
4825 break;
4826 case kCondGT:
4827 if (gt_bias) {
4828 __ CmpUltS(FTMP, rhs, lhs);
4829 } else {
4830 __ CmpLtS(FTMP, rhs, lhs);
4831 }
4832 __ Mfc1(dst, FTMP);
4833 __ Andi(dst, dst, 1);
4834 break;
4835 case kCondGE:
4836 if (gt_bias) {
4837 __ CmpUleS(FTMP, rhs, lhs);
4838 } else {
4839 __ CmpLeS(FTMP, rhs, lhs);
4840 }
4841 __ Mfc1(dst, FTMP);
4842 __ Andi(dst, dst, 1);
4843 break;
4844 default:
4845 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4846 UNREACHABLE();
4847 }
4848 } else {
4849 switch (cond) {
4850 case kCondEQ:
4851 __ CeqS(0, lhs, rhs);
4852 __ LoadConst32(dst, 1);
4853 __ Movf(dst, ZERO, 0);
4854 break;
4855 case kCondNE:
4856 __ CeqS(0, lhs, rhs);
4857 __ LoadConst32(dst, 1);
4858 __ Movt(dst, ZERO, 0);
4859 break;
4860 case kCondLT:
4861 if (gt_bias) {
4862 __ ColtS(0, lhs, rhs);
4863 } else {
4864 __ CultS(0, lhs, rhs);
4865 }
4866 __ LoadConst32(dst, 1);
4867 __ Movf(dst, ZERO, 0);
4868 break;
4869 case kCondLE:
4870 if (gt_bias) {
4871 __ ColeS(0, lhs, rhs);
4872 } else {
4873 __ CuleS(0, lhs, rhs);
4874 }
4875 __ LoadConst32(dst, 1);
4876 __ Movf(dst, ZERO, 0);
4877 break;
4878 case kCondGT:
4879 if (gt_bias) {
4880 __ CultS(0, rhs, lhs);
4881 } else {
4882 __ ColtS(0, rhs, lhs);
4883 }
4884 __ LoadConst32(dst, 1);
4885 __ Movf(dst, ZERO, 0);
4886 break;
4887 case kCondGE:
4888 if (gt_bias) {
4889 __ CuleS(0, rhs, lhs);
4890 } else {
4891 __ ColeS(0, rhs, lhs);
4892 }
4893 __ LoadConst32(dst, 1);
4894 __ Movf(dst, ZERO, 0);
4895 break;
4896 default:
4897 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4898 UNREACHABLE();
4899 }
4900 }
4901 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01004902 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004903 if (isR6) {
4904 switch (cond) {
4905 case kCondEQ:
4906 __ CmpEqD(FTMP, lhs, rhs);
4907 __ Mfc1(dst, FTMP);
4908 __ Andi(dst, dst, 1);
4909 break;
4910 case kCondNE:
4911 __ CmpEqD(FTMP, lhs, rhs);
4912 __ Mfc1(dst, FTMP);
4913 __ Addiu(dst, dst, 1);
4914 break;
4915 case kCondLT:
4916 if (gt_bias) {
4917 __ CmpLtD(FTMP, lhs, rhs);
4918 } else {
4919 __ CmpUltD(FTMP, lhs, rhs);
4920 }
4921 __ Mfc1(dst, FTMP);
4922 __ Andi(dst, dst, 1);
4923 break;
4924 case kCondLE:
4925 if (gt_bias) {
4926 __ CmpLeD(FTMP, lhs, rhs);
4927 } else {
4928 __ CmpUleD(FTMP, lhs, rhs);
4929 }
4930 __ Mfc1(dst, FTMP);
4931 __ Andi(dst, dst, 1);
4932 break;
4933 case kCondGT:
4934 if (gt_bias) {
4935 __ CmpUltD(FTMP, rhs, lhs);
4936 } else {
4937 __ CmpLtD(FTMP, rhs, lhs);
4938 }
4939 __ Mfc1(dst, FTMP);
4940 __ Andi(dst, dst, 1);
4941 break;
4942 case kCondGE:
4943 if (gt_bias) {
4944 __ CmpUleD(FTMP, rhs, lhs);
4945 } else {
4946 __ CmpLeD(FTMP, rhs, lhs);
4947 }
4948 __ Mfc1(dst, FTMP);
4949 __ Andi(dst, dst, 1);
4950 break;
4951 default:
4952 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4953 UNREACHABLE();
4954 }
4955 } else {
4956 switch (cond) {
4957 case kCondEQ:
4958 __ CeqD(0, lhs, rhs);
4959 __ LoadConst32(dst, 1);
4960 __ Movf(dst, ZERO, 0);
4961 break;
4962 case kCondNE:
4963 __ CeqD(0, lhs, rhs);
4964 __ LoadConst32(dst, 1);
4965 __ Movt(dst, ZERO, 0);
4966 break;
4967 case kCondLT:
4968 if (gt_bias) {
4969 __ ColtD(0, lhs, rhs);
4970 } else {
4971 __ CultD(0, lhs, rhs);
4972 }
4973 __ LoadConst32(dst, 1);
4974 __ Movf(dst, ZERO, 0);
4975 break;
4976 case kCondLE:
4977 if (gt_bias) {
4978 __ ColeD(0, lhs, rhs);
4979 } else {
4980 __ CuleD(0, lhs, rhs);
4981 }
4982 __ LoadConst32(dst, 1);
4983 __ Movf(dst, ZERO, 0);
4984 break;
4985 case kCondGT:
4986 if (gt_bias) {
4987 __ CultD(0, rhs, lhs);
4988 } else {
4989 __ ColtD(0, rhs, lhs);
4990 }
4991 __ LoadConst32(dst, 1);
4992 __ Movf(dst, ZERO, 0);
4993 break;
4994 case kCondGE:
4995 if (gt_bias) {
4996 __ CuleD(0, rhs, lhs);
4997 } else {
4998 __ ColeD(0, rhs, lhs);
4999 }
5000 __ LoadConst32(dst, 1);
5001 __ Movf(dst, ZERO, 0);
5002 break;
5003 default:
5004 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5005 UNREACHABLE();
5006 }
5007 }
5008 }
5009}
5010
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005011bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5012 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005013 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005014 LocationSummary* input_locations,
5015 int cc) {
5016 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5017 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5018 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005019 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005020 switch (cond) {
5021 case kCondEQ:
5022 __ CeqS(cc, lhs, rhs);
5023 return false;
5024 case kCondNE:
5025 __ CeqS(cc, lhs, rhs);
5026 return true;
5027 case kCondLT:
5028 if (gt_bias) {
5029 __ ColtS(cc, lhs, rhs);
5030 } else {
5031 __ CultS(cc, lhs, rhs);
5032 }
5033 return false;
5034 case kCondLE:
5035 if (gt_bias) {
5036 __ ColeS(cc, lhs, rhs);
5037 } else {
5038 __ CuleS(cc, lhs, rhs);
5039 }
5040 return false;
5041 case kCondGT:
5042 if (gt_bias) {
5043 __ CultS(cc, rhs, lhs);
5044 } else {
5045 __ ColtS(cc, rhs, lhs);
5046 }
5047 return false;
5048 case kCondGE:
5049 if (gt_bias) {
5050 __ CuleS(cc, rhs, lhs);
5051 } else {
5052 __ ColeS(cc, rhs, lhs);
5053 }
5054 return false;
5055 default:
5056 LOG(FATAL) << "Unexpected non-floating-point condition";
5057 UNREACHABLE();
5058 }
5059 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005060 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005061 switch (cond) {
5062 case kCondEQ:
5063 __ CeqD(cc, lhs, rhs);
5064 return false;
5065 case kCondNE:
5066 __ CeqD(cc, lhs, rhs);
5067 return true;
5068 case kCondLT:
5069 if (gt_bias) {
5070 __ ColtD(cc, lhs, rhs);
5071 } else {
5072 __ CultD(cc, lhs, rhs);
5073 }
5074 return false;
5075 case kCondLE:
5076 if (gt_bias) {
5077 __ ColeD(cc, lhs, rhs);
5078 } else {
5079 __ CuleD(cc, lhs, rhs);
5080 }
5081 return false;
5082 case kCondGT:
5083 if (gt_bias) {
5084 __ CultD(cc, rhs, lhs);
5085 } else {
5086 __ ColtD(cc, rhs, lhs);
5087 }
5088 return false;
5089 case kCondGE:
5090 if (gt_bias) {
5091 __ CuleD(cc, rhs, lhs);
5092 } else {
5093 __ ColeD(cc, rhs, lhs);
5094 }
5095 return false;
5096 default:
5097 LOG(FATAL) << "Unexpected non-floating-point condition";
5098 UNREACHABLE();
5099 }
5100 }
5101}
5102
5103bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5104 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005105 DataType::Type type,
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005106 LocationSummary* input_locations,
5107 FRegister dst) {
5108 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5109 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5110 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005111 if (type == DataType::Type::kFloat32) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005112 switch (cond) {
5113 case kCondEQ:
5114 __ CmpEqS(dst, lhs, rhs);
5115 return false;
5116 case kCondNE:
5117 __ CmpEqS(dst, lhs, rhs);
5118 return true;
5119 case kCondLT:
5120 if (gt_bias) {
5121 __ CmpLtS(dst, lhs, rhs);
5122 } else {
5123 __ CmpUltS(dst, lhs, rhs);
5124 }
5125 return false;
5126 case kCondLE:
5127 if (gt_bias) {
5128 __ CmpLeS(dst, lhs, rhs);
5129 } else {
5130 __ CmpUleS(dst, lhs, rhs);
5131 }
5132 return false;
5133 case kCondGT:
5134 if (gt_bias) {
5135 __ CmpUltS(dst, rhs, lhs);
5136 } else {
5137 __ CmpLtS(dst, rhs, lhs);
5138 }
5139 return false;
5140 case kCondGE:
5141 if (gt_bias) {
5142 __ CmpUleS(dst, rhs, lhs);
5143 } else {
5144 __ CmpLeS(dst, rhs, lhs);
5145 }
5146 return false;
5147 default:
5148 LOG(FATAL) << "Unexpected non-floating-point condition";
5149 UNREACHABLE();
5150 }
5151 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005152 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005153 switch (cond) {
5154 case kCondEQ:
5155 __ CmpEqD(dst, lhs, rhs);
5156 return false;
5157 case kCondNE:
5158 __ CmpEqD(dst, lhs, rhs);
5159 return true;
5160 case kCondLT:
5161 if (gt_bias) {
5162 __ CmpLtD(dst, lhs, rhs);
5163 } else {
5164 __ CmpUltD(dst, lhs, rhs);
5165 }
5166 return false;
5167 case kCondLE:
5168 if (gt_bias) {
5169 __ CmpLeD(dst, lhs, rhs);
5170 } else {
5171 __ CmpUleD(dst, lhs, rhs);
5172 }
5173 return false;
5174 case kCondGT:
5175 if (gt_bias) {
5176 __ CmpUltD(dst, rhs, lhs);
5177 } else {
5178 __ CmpLtD(dst, rhs, lhs);
5179 }
5180 return false;
5181 case kCondGE:
5182 if (gt_bias) {
5183 __ CmpUleD(dst, rhs, lhs);
5184 } else {
5185 __ CmpLeD(dst, rhs, lhs);
5186 }
5187 return false;
5188 default:
5189 LOG(FATAL) << "Unexpected non-floating-point condition";
5190 UNREACHABLE();
5191 }
5192 }
5193}
5194
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005195void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5196 bool gt_bias,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005197 DataType::Type type,
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005198 LocationSummary* locations,
5199 MipsLabel* label) {
5200 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5201 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5202 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005203 if (type == DataType::Type::kFloat32) {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005204 if (isR6) {
5205 switch (cond) {
5206 case kCondEQ:
5207 __ CmpEqS(FTMP, lhs, rhs);
5208 __ Bc1nez(FTMP, label);
5209 break;
5210 case kCondNE:
5211 __ CmpEqS(FTMP, lhs, rhs);
5212 __ Bc1eqz(FTMP, label);
5213 break;
5214 case kCondLT:
5215 if (gt_bias) {
5216 __ CmpLtS(FTMP, lhs, rhs);
5217 } else {
5218 __ CmpUltS(FTMP, lhs, rhs);
5219 }
5220 __ Bc1nez(FTMP, label);
5221 break;
5222 case kCondLE:
5223 if (gt_bias) {
5224 __ CmpLeS(FTMP, lhs, rhs);
5225 } else {
5226 __ CmpUleS(FTMP, lhs, rhs);
5227 }
5228 __ Bc1nez(FTMP, label);
5229 break;
5230 case kCondGT:
5231 if (gt_bias) {
5232 __ CmpUltS(FTMP, rhs, lhs);
5233 } else {
5234 __ CmpLtS(FTMP, rhs, lhs);
5235 }
5236 __ Bc1nez(FTMP, label);
5237 break;
5238 case kCondGE:
5239 if (gt_bias) {
5240 __ CmpUleS(FTMP, rhs, lhs);
5241 } else {
5242 __ CmpLeS(FTMP, rhs, lhs);
5243 }
5244 __ Bc1nez(FTMP, label);
5245 break;
5246 default:
5247 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005248 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005249 }
5250 } else {
5251 switch (cond) {
5252 case kCondEQ:
5253 __ CeqS(0, lhs, rhs);
5254 __ Bc1t(0, label);
5255 break;
5256 case kCondNE:
5257 __ CeqS(0, lhs, rhs);
5258 __ Bc1f(0, label);
5259 break;
5260 case kCondLT:
5261 if (gt_bias) {
5262 __ ColtS(0, lhs, rhs);
5263 } else {
5264 __ CultS(0, lhs, rhs);
5265 }
5266 __ Bc1t(0, label);
5267 break;
5268 case kCondLE:
5269 if (gt_bias) {
5270 __ ColeS(0, lhs, rhs);
5271 } else {
5272 __ CuleS(0, lhs, rhs);
5273 }
5274 __ Bc1t(0, label);
5275 break;
5276 case kCondGT:
5277 if (gt_bias) {
5278 __ CultS(0, rhs, lhs);
5279 } else {
5280 __ ColtS(0, rhs, lhs);
5281 }
5282 __ Bc1t(0, label);
5283 break;
5284 case kCondGE:
5285 if (gt_bias) {
5286 __ CuleS(0, rhs, lhs);
5287 } else {
5288 __ ColeS(0, rhs, lhs);
5289 }
5290 __ Bc1t(0, label);
5291 break;
5292 default:
5293 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005294 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005295 }
5296 }
5297 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005298 DCHECK_EQ(type, DataType::Type::kFloat64);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005299 if (isR6) {
5300 switch (cond) {
5301 case kCondEQ:
5302 __ CmpEqD(FTMP, lhs, rhs);
5303 __ Bc1nez(FTMP, label);
5304 break;
5305 case kCondNE:
5306 __ CmpEqD(FTMP, lhs, rhs);
5307 __ Bc1eqz(FTMP, label);
5308 break;
5309 case kCondLT:
5310 if (gt_bias) {
5311 __ CmpLtD(FTMP, lhs, rhs);
5312 } else {
5313 __ CmpUltD(FTMP, lhs, rhs);
5314 }
5315 __ Bc1nez(FTMP, label);
5316 break;
5317 case kCondLE:
5318 if (gt_bias) {
5319 __ CmpLeD(FTMP, lhs, rhs);
5320 } else {
5321 __ CmpUleD(FTMP, lhs, rhs);
5322 }
5323 __ Bc1nez(FTMP, label);
5324 break;
5325 case kCondGT:
5326 if (gt_bias) {
5327 __ CmpUltD(FTMP, rhs, lhs);
5328 } else {
5329 __ CmpLtD(FTMP, rhs, lhs);
5330 }
5331 __ Bc1nez(FTMP, label);
5332 break;
5333 case kCondGE:
5334 if (gt_bias) {
5335 __ CmpUleD(FTMP, rhs, lhs);
5336 } else {
5337 __ CmpLeD(FTMP, rhs, lhs);
5338 }
5339 __ Bc1nez(FTMP, label);
5340 break;
5341 default:
5342 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005343 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005344 }
5345 } else {
5346 switch (cond) {
5347 case kCondEQ:
5348 __ CeqD(0, lhs, rhs);
5349 __ Bc1t(0, label);
5350 break;
5351 case kCondNE:
5352 __ CeqD(0, lhs, rhs);
5353 __ Bc1f(0, label);
5354 break;
5355 case kCondLT:
5356 if (gt_bias) {
5357 __ ColtD(0, lhs, rhs);
5358 } else {
5359 __ CultD(0, lhs, rhs);
5360 }
5361 __ Bc1t(0, label);
5362 break;
5363 case kCondLE:
5364 if (gt_bias) {
5365 __ ColeD(0, lhs, rhs);
5366 } else {
5367 __ CuleD(0, lhs, rhs);
5368 }
5369 __ Bc1t(0, label);
5370 break;
5371 case kCondGT:
5372 if (gt_bias) {
5373 __ CultD(0, rhs, lhs);
5374 } else {
5375 __ ColtD(0, rhs, lhs);
5376 }
5377 __ Bc1t(0, label);
5378 break;
5379 case kCondGE:
5380 if (gt_bias) {
5381 __ CuleD(0, rhs, lhs);
5382 } else {
5383 __ ColeD(0, rhs, lhs);
5384 }
5385 __ Bc1t(0, label);
5386 break;
5387 default:
5388 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005389 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005390 }
5391 }
5392 }
5393}
5394
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005395void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005396 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005397 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005398 MipsLabel* false_target) {
5399 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005400
David Brazdil0debae72015-11-12 18:37:00 +00005401 if (true_target == nullptr && false_target == nullptr) {
5402 // Nothing to do. The code always falls through.
5403 return;
5404 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005405 // Constant condition, statically compared against "true" (integer value 1).
5406 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005407 if (true_target != nullptr) {
5408 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005409 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005410 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005411 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005412 if (false_target != nullptr) {
5413 __ B(false_target);
5414 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005415 }
David Brazdil0debae72015-11-12 18:37:00 +00005416 return;
5417 }
5418
5419 // The following code generates these patterns:
5420 // (1) true_target == nullptr && false_target != nullptr
5421 // - opposite condition true => branch to false_target
5422 // (2) true_target != nullptr && false_target == nullptr
5423 // - condition true => branch to true_target
5424 // (3) true_target != nullptr && false_target != nullptr
5425 // - condition true => branch to true_target
5426 // - branch to false_target
5427 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005428 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005429 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005430 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005431 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005432 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5433 } else {
5434 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5435 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005436 } else {
5437 // The condition instruction has not been materialized, use its inputs as
5438 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005439 HCondition* condition = cond->AsCondition();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005440 DataType::Type type = condition->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005441 LocationSummary* locations = cond->GetLocations();
5442 IfCondition if_cond = condition->GetCondition();
5443 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005444
David Brazdil0debae72015-11-12 18:37:00 +00005445 if (true_target == nullptr) {
5446 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005447 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005448 }
5449
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005450 switch (type) {
5451 default:
5452 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5453 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005454 case DataType::Type::kInt64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005455 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5456 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005457 case DataType::Type::kFloat32:
5458 case DataType::Type::kFloat64:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005459 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5460 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005461 }
5462 }
David Brazdil0debae72015-11-12 18:37:00 +00005463
5464 // If neither branch falls through (case 3), the conditional branch to `true_target`
5465 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5466 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005467 __ B(false_target);
5468 }
5469}
5470
5471void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5472 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005473 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005474 locations->SetInAt(0, Location::RequiresRegister());
5475 }
5476}
5477
5478void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005479 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5480 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5481 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5482 nullptr : codegen_->GetLabelOf(true_successor);
5483 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5484 nullptr : codegen_->GetLabelOf(false_successor);
5485 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005486}
5487
5488void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5489 LocationSummary* locations = new (GetGraph()->GetArena())
5490 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005491 InvokeRuntimeCallingConvention calling_convention;
5492 RegisterSet caller_saves = RegisterSet::Empty();
5493 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5494 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005495 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005496 locations->SetInAt(0, Location::RequiresRegister());
5497 }
5498}
5499
5500void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005501 SlowPathCodeMIPS* slow_path =
5502 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005503 GenerateTestAndBranch(deoptimize,
5504 /* condition_input_index */ 0,
5505 slow_path->GetEntryLabel(),
5506 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005507}
5508
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005509// This function returns true if a conditional move can be generated for HSelect.
5510// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5511// branches and regular moves.
5512//
5513// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5514//
5515// While determining feasibility of a conditional move and setting inputs/outputs
5516// are two distinct tasks, this function does both because they share quite a bit
5517// of common logic.
5518static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5519 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5520 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5521 HCondition* condition = cond->AsCondition();
5522
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005523 DataType::Type cond_type =
5524 materialized ? DataType::Type::kInt32 : condition->InputAt(0)->GetType();
5525 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005526
5527 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5528 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5529 bool is_true_value_zero_constant =
5530 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5531 bool is_false_value_zero_constant =
5532 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5533
5534 bool can_move_conditionally = false;
5535 bool use_const_for_false_in = false;
5536 bool use_const_for_true_in = false;
5537
5538 if (!cond->IsConstant()) {
5539 switch (cond_type) {
5540 default:
5541 switch (dst_type) {
5542 default:
5543 // Moving int on int condition.
5544 if (is_r6) {
5545 if (is_true_value_zero_constant) {
5546 // seleqz out_reg, false_reg, cond_reg
5547 can_move_conditionally = true;
5548 use_const_for_true_in = true;
5549 } else if (is_false_value_zero_constant) {
5550 // selnez out_reg, true_reg, cond_reg
5551 can_move_conditionally = true;
5552 use_const_for_false_in = true;
5553 } else if (materialized) {
5554 // Not materializing unmaterialized int conditions
5555 // to keep the instruction count low.
5556 // selnez AT, true_reg, cond_reg
5557 // seleqz TMP, false_reg, cond_reg
5558 // or out_reg, AT, TMP
5559 can_move_conditionally = true;
5560 }
5561 } else {
5562 // movn out_reg, true_reg/ZERO, cond_reg
5563 can_move_conditionally = true;
5564 use_const_for_true_in = is_true_value_zero_constant;
5565 }
5566 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005567 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005568 // Moving long on int condition.
5569 if (is_r6) {
5570 if (is_true_value_zero_constant) {
5571 // seleqz out_reg_lo, false_reg_lo, cond_reg
5572 // seleqz out_reg_hi, false_reg_hi, cond_reg
5573 can_move_conditionally = true;
5574 use_const_for_true_in = true;
5575 } else if (is_false_value_zero_constant) {
5576 // selnez out_reg_lo, true_reg_lo, cond_reg
5577 // selnez out_reg_hi, true_reg_hi, cond_reg
5578 can_move_conditionally = true;
5579 use_const_for_false_in = true;
5580 }
5581 // Other long conditional moves would generate 6+ instructions,
5582 // which is too many.
5583 } else {
5584 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5585 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5586 can_move_conditionally = true;
5587 use_const_for_true_in = is_true_value_zero_constant;
5588 }
5589 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005590 case DataType::Type::kFloat32:
5591 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005592 // Moving float/double on int condition.
5593 if (is_r6) {
5594 if (materialized) {
5595 // Not materializing unmaterialized int conditions
5596 // to keep the instruction count low.
5597 can_move_conditionally = true;
5598 if (is_true_value_zero_constant) {
5599 // sltu TMP, ZERO, cond_reg
5600 // mtc1 TMP, temp_cond_reg
5601 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5602 use_const_for_true_in = true;
5603 } else if (is_false_value_zero_constant) {
5604 // sltu TMP, ZERO, cond_reg
5605 // mtc1 TMP, temp_cond_reg
5606 // selnez.fmt out_reg, true_reg, temp_cond_reg
5607 use_const_for_false_in = true;
5608 } else {
5609 // sltu TMP, ZERO, cond_reg
5610 // mtc1 TMP, temp_cond_reg
5611 // sel.fmt temp_cond_reg, false_reg, true_reg
5612 // mov.fmt out_reg, temp_cond_reg
5613 }
5614 }
5615 } else {
5616 // movn.fmt out_reg, true_reg, cond_reg
5617 can_move_conditionally = true;
5618 }
5619 break;
5620 }
5621 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005622 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005623 // We don't materialize long comparison now
5624 // and use conditional branches instead.
5625 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005626 case DataType::Type::kFloat32:
5627 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005628 switch (dst_type) {
5629 default:
5630 // Moving int on float/double condition.
5631 if (is_r6) {
5632 if (is_true_value_zero_constant) {
5633 // mfc1 TMP, temp_cond_reg
5634 // seleqz out_reg, false_reg, TMP
5635 can_move_conditionally = true;
5636 use_const_for_true_in = true;
5637 } else if (is_false_value_zero_constant) {
5638 // mfc1 TMP, temp_cond_reg
5639 // selnez out_reg, true_reg, TMP
5640 can_move_conditionally = true;
5641 use_const_for_false_in = true;
5642 } else {
5643 // mfc1 TMP, temp_cond_reg
5644 // selnez AT, true_reg, TMP
5645 // seleqz TMP, false_reg, TMP
5646 // or out_reg, AT, TMP
5647 can_move_conditionally = true;
5648 }
5649 } else {
5650 // movt out_reg, true_reg/ZERO, cc
5651 can_move_conditionally = true;
5652 use_const_for_true_in = is_true_value_zero_constant;
5653 }
5654 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005655 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005656 // Moving long on float/double condition.
5657 if (is_r6) {
5658 if (is_true_value_zero_constant) {
5659 // mfc1 TMP, temp_cond_reg
5660 // seleqz out_reg_lo, false_reg_lo, TMP
5661 // seleqz out_reg_hi, false_reg_hi, TMP
5662 can_move_conditionally = true;
5663 use_const_for_true_in = true;
5664 } else if (is_false_value_zero_constant) {
5665 // mfc1 TMP, temp_cond_reg
5666 // selnez out_reg_lo, true_reg_lo, TMP
5667 // selnez out_reg_hi, true_reg_hi, TMP
5668 can_move_conditionally = true;
5669 use_const_for_false_in = true;
5670 }
5671 // Other long conditional moves would generate 6+ instructions,
5672 // which is too many.
5673 } else {
5674 // movt out_reg_lo, true_reg_lo/ZERO, cc
5675 // movt out_reg_hi, true_reg_hi/ZERO, cc
5676 can_move_conditionally = true;
5677 use_const_for_true_in = is_true_value_zero_constant;
5678 }
5679 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005680 case DataType::Type::kFloat32:
5681 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005682 // Moving float/double on float/double condition.
5683 if (is_r6) {
5684 can_move_conditionally = true;
5685 if (is_true_value_zero_constant) {
5686 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5687 use_const_for_true_in = true;
5688 } else if (is_false_value_zero_constant) {
5689 // selnez.fmt out_reg, true_reg, temp_cond_reg
5690 use_const_for_false_in = true;
5691 } else {
5692 // sel.fmt temp_cond_reg, false_reg, true_reg
5693 // mov.fmt out_reg, temp_cond_reg
5694 }
5695 } else {
5696 // movt.fmt out_reg, true_reg, cc
5697 can_move_conditionally = true;
5698 }
5699 break;
5700 }
5701 break;
5702 }
5703 }
5704
5705 if (can_move_conditionally) {
5706 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5707 } else {
5708 DCHECK(!use_const_for_false_in);
5709 DCHECK(!use_const_for_true_in);
5710 }
5711
5712 if (locations_to_set != nullptr) {
5713 if (use_const_for_false_in) {
5714 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5715 } else {
5716 locations_to_set->SetInAt(0,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005717 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005718 ? Location::RequiresFpuRegister()
5719 : Location::RequiresRegister());
5720 }
5721 if (use_const_for_true_in) {
5722 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5723 } else {
5724 locations_to_set->SetInAt(1,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005725 DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005726 ? Location::RequiresFpuRegister()
5727 : Location::RequiresRegister());
5728 }
5729 if (materialized) {
5730 locations_to_set->SetInAt(2, Location::RequiresRegister());
5731 }
5732 // On R6 we don't require the output to be the same as the
5733 // first input for conditional moves unlike on R2.
5734 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5735 if (is_out_same_as_first_in) {
5736 locations_to_set->SetOut(Location::SameAsFirstInput());
5737 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005738 locations_to_set->SetOut(DataType::IsFloatingPointType(dst_type)
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005739 ? Location::RequiresFpuRegister()
5740 : Location::RequiresRegister());
5741 }
5742 }
5743
5744 return can_move_conditionally;
5745}
5746
5747void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5748 LocationSummary* locations = select->GetLocations();
5749 Location dst = locations->Out();
5750 Location src = locations->InAt(1);
5751 Register src_reg = ZERO;
5752 Register src_reg_high = ZERO;
5753 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5754 Register cond_reg = TMP;
5755 int cond_cc = 0;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005756 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005757 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005758 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005759
5760 if (IsBooleanValueOrMaterializedCondition(cond)) {
5761 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5762 } else {
5763 HCondition* condition = cond->AsCondition();
5764 LocationSummary* cond_locations = cond->GetLocations();
5765 IfCondition if_cond = condition->GetCondition();
5766 cond_type = condition->InputAt(0)->GetType();
5767 switch (cond_type) {
5768 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005769 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005770 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5771 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005772 case DataType::Type::kFloat32:
5773 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005774 cond_inverted = MaterializeFpCompareR2(if_cond,
5775 condition->IsGtBias(),
5776 cond_type,
5777 cond_locations,
5778 cond_cc);
5779 break;
5780 }
5781 }
5782
5783 DCHECK(dst.Equals(locations->InAt(0)));
5784 if (src.IsRegister()) {
5785 src_reg = src.AsRegister<Register>();
5786 } else if (src.IsRegisterPair()) {
5787 src_reg = src.AsRegisterPairLow<Register>();
5788 src_reg_high = src.AsRegisterPairHigh<Register>();
5789 } else if (src.IsConstant()) {
5790 DCHECK(src.GetConstant()->IsZeroBitPattern());
5791 }
5792
5793 switch (cond_type) {
5794 default:
5795 switch (dst_type) {
5796 default:
5797 if (cond_inverted) {
5798 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5799 } else {
5800 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5801 }
5802 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005803 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005804 if (cond_inverted) {
5805 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5806 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5807 } else {
5808 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5809 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5810 }
5811 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005812 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005813 if (cond_inverted) {
5814 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5815 } else {
5816 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5817 }
5818 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005819 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005820 if (cond_inverted) {
5821 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5822 } else {
5823 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5824 }
5825 break;
5826 }
5827 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005828 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005829 LOG(FATAL) << "Unreachable";
5830 UNREACHABLE();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005831 case DataType::Type::kFloat32:
5832 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005833 switch (dst_type) {
5834 default:
5835 if (cond_inverted) {
5836 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5837 } else {
5838 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5839 }
5840 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005841 case DataType::Type::kInt64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005842 if (cond_inverted) {
5843 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5844 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5845 } else {
5846 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5847 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5848 }
5849 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005850 case DataType::Type::kFloat32:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005851 if (cond_inverted) {
5852 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5853 } else {
5854 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5855 }
5856 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005857 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005858 if (cond_inverted) {
5859 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5860 } else {
5861 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5862 }
5863 break;
5864 }
5865 break;
5866 }
5867}
5868
5869void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5870 LocationSummary* locations = select->GetLocations();
5871 Location dst = locations->Out();
5872 Location false_src = locations->InAt(0);
5873 Location true_src = locations->InAt(1);
5874 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5875 Register cond_reg = TMP;
5876 FRegister fcond_reg = FTMP;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005877 DataType::Type cond_type = DataType::Type::kInt32;
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005878 bool cond_inverted = false;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005879 DataType::Type dst_type = select->GetType();
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005880
5881 if (IsBooleanValueOrMaterializedCondition(cond)) {
5882 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5883 } else {
5884 HCondition* condition = cond->AsCondition();
5885 LocationSummary* cond_locations = cond->GetLocations();
5886 IfCondition if_cond = condition->GetCondition();
5887 cond_type = condition->InputAt(0)->GetType();
5888 switch (cond_type) {
5889 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005890 DCHECK_NE(cond_type, DataType::Type::kInt64);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005891 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5892 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005893 case DataType::Type::kFloat32:
5894 case DataType::Type::kFloat64:
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005895 cond_inverted = MaterializeFpCompareR6(if_cond,
5896 condition->IsGtBias(),
5897 cond_type,
5898 cond_locations,
5899 fcond_reg);
5900 break;
5901 }
5902 }
5903
5904 if (true_src.IsConstant()) {
5905 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5906 }
5907 if (false_src.IsConstant()) {
5908 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5909 }
5910
5911 switch (dst_type) {
5912 default:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005913 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005914 __ Mfc1(cond_reg, fcond_reg);
5915 }
5916 if (true_src.IsConstant()) {
5917 if (cond_inverted) {
5918 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5919 } else {
5920 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5921 }
5922 } else if (false_src.IsConstant()) {
5923 if (cond_inverted) {
5924 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5925 } else {
5926 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5927 }
5928 } else {
5929 DCHECK_NE(cond_reg, AT);
5930 if (cond_inverted) {
5931 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5932 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5933 } else {
5934 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5935 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5936 }
5937 __ Or(dst.AsRegister<Register>(), AT, TMP);
5938 }
5939 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005940 case DataType::Type::kInt64: {
5941 if (DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005942 __ Mfc1(cond_reg, fcond_reg);
5943 }
5944 Register dst_lo = dst.AsRegisterPairLow<Register>();
5945 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5946 if (true_src.IsConstant()) {
5947 Register src_lo = false_src.AsRegisterPairLow<Register>();
5948 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5949 if (cond_inverted) {
5950 __ Selnez(dst_lo, src_lo, cond_reg);
5951 __ Selnez(dst_hi, src_hi, cond_reg);
5952 } else {
5953 __ Seleqz(dst_lo, src_lo, cond_reg);
5954 __ Seleqz(dst_hi, src_hi, cond_reg);
5955 }
5956 } else {
5957 DCHECK(false_src.IsConstant());
5958 Register src_lo = true_src.AsRegisterPairLow<Register>();
5959 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5960 if (cond_inverted) {
5961 __ Seleqz(dst_lo, src_lo, cond_reg);
5962 __ Seleqz(dst_hi, src_hi, cond_reg);
5963 } else {
5964 __ Selnez(dst_lo, src_lo, cond_reg);
5965 __ Selnez(dst_hi, src_hi, cond_reg);
5966 }
5967 }
5968 break;
5969 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01005970 case DataType::Type::kFloat32: {
5971 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005972 // sel*.fmt tests bit 0 of the condition register, account for that.
5973 __ Sltu(TMP, ZERO, cond_reg);
5974 __ Mtc1(TMP, fcond_reg);
5975 }
5976 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5977 if (true_src.IsConstant()) {
5978 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5979 if (cond_inverted) {
5980 __ SelnezS(dst_reg, src_reg, fcond_reg);
5981 } else {
5982 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5983 }
5984 } else if (false_src.IsConstant()) {
5985 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5986 if (cond_inverted) {
5987 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5988 } else {
5989 __ SelnezS(dst_reg, src_reg, fcond_reg);
5990 }
5991 } else {
5992 if (cond_inverted) {
5993 __ SelS(fcond_reg,
5994 true_src.AsFpuRegister<FRegister>(),
5995 false_src.AsFpuRegister<FRegister>());
5996 } else {
5997 __ SelS(fcond_reg,
5998 false_src.AsFpuRegister<FRegister>(),
5999 true_src.AsFpuRegister<FRegister>());
6000 }
6001 __ MovS(dst_reg, fcond_reg);
6002 }
6003 break;
6004 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006005 case DataType::Type::kFloat64: {
6006 if (!DataType::IsFloatingPointType(cond_type)) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006007 // sel*.fmt tests bit 0 of the condition register, account for that.
6008 __ Sltu(TMP, ZERO, cond_reg);
6009 __ Mtc1(TMP, fcond_reg);
6010 }
6011 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6012 if (true_src.IsConstant()) {
6013 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6014 if (cond_inverted) {
6015 __ SelnezD(dst_reg, src_reg, fcond_reg);
6016 } else {
6017 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6018 }
6019 } else if (false_src.IsConstant()) {
6020 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6021 if (cond_inverted) {
6022 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6023 } else {
6024 __ SelnezD(dst_reg, src_reg, fcond_reg);
6025 }
6026 } else {
6027 if (cond_inverted) {
6028 __ SelD(fcond_reg,
6029 true_src.AsFpuRegister<FRegister>(),
6030 false_src.AsFpuRegister<FRegister>());
6031 } else {
6032 __ SelD(fcond_reg,
6033 false_src.AsFpuRegister<FRegister>(),
6034 true_src.AsFpuRegister<FRegister>());
6035 }
6036 __ MovD(dst_reg, fcond_reg);
6037 }
6038 break;
6039 }
6040 }
6041}
6042
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006043void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6044 LocationSummary* locations = new (GetGraph()->GetArena())
6045 LocationSummary(flag, LocationSummary::kNoCall);
6046 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006047}
6048
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006049void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6050 __ LoadFromOffset(kLoadWord,
6051 flag->GetLocations()->Out().AsRegister<Register>(),
6052 SP,
6053 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006054}
6055
David Brazdil74eb1b22015-12-14 11:44:01 +00006056void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
6057 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006058 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006059}
6060
6061void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006062 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6063 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6064 if (is_r6) {
6065 GenConditionalMoveR6(select);
6066 } else {
6067 GenConditionalMoveR2(select);
6068 }
6069 } else {
6070 LocationSummary* locations = select->GetLocations();
6071 MipsLabel false_target;
6072 GenerateTestAndBranch(select,
6073 /* condition_input_index */ 2,
6074 /* true_target */ nullptr,
6075 &false_target);
6076 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6077 __ Bind(&false_target);
6078 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006079}
6080
David Srbecky0cf44932015-12-09 14:09:59 +00006081void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
6082 new (GetGraph()->GetArena()) LocationSummary(info);
6083}
6084
David Srbeckyd28f4a02016-03-14 17:14:24 +00006085void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6086 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006087}
6088
6089void CodeGeneratorMIPS::GenerateNop() {
6090 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006091}
6092
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006093void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006094 DataType::Type field_type = field_info.GetFieldType();
6095 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006096 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006097 bool object_field_get_with_read_barrier =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006098 kEmitCompilerReadBarrier && (field_type == DataType::Type::kReference);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006099 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006100 instruction,
6101 generate_volatile
6102 ? LocationSummary::kCallOnMainOnly
6103 : (object_field_get_with_read_barrier
6104 ? LocationSummary::kCallOnSlowPath
6105 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006106
Alexey Frunzec61c0762017-04-10 13:54:23 -07006107 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6108 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6109 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006110 locations->SetInAt(0, Location::RequiresRegister());
6111 if (generate_volatile) {
6112 InvokeRuntimeCallingConvention calling_convention;
6113 // need A0 to hold base + offset
6114 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006115 if (field_type == DataType::Type::kInt64) {
6116 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kInt64));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006117 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006118 // Use Location::Any() to prevent situations when running out of available fp registers.
6119 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006120 // Need some temp core regs since FP results are returned in core registers
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006121 Location reg = calling_convention.GetReturnLocation(DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006122 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6123 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6124 }
6125 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006126 if (DataType::IsFloatingPointType(instruction->GetType())) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006127 locations->SetOut(Location::RequiresFpuRegister());
6128 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006129 // The output overlaps in the case of an object field get with
6130 // read barriers enabled: we do not want the move to overwrite the
6131 // object's location, as we need it to emit the read barrier.
6132 locations->SetOut(Location::RequiresRegister(),
6133 object_field_get_with_read_barrier
6134 ? Location::kOutputOverlap
6135 : Location::kNoOutputOverlap);
6136 }
6137 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6138 // We need a temporary register for the read barrier marking slow
6139 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006140 if (!kBakerReadBarrierThunksEnableForFields) {
6141 locations->AddTemp(Location::RequiresRegister());
6142 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006143 }
6144 }
6145}
6146
6147void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6148 const FieldInfo& field_info,
6149 uint32_t dex_pc) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006150 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006151 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006152 Location obj_loc = locations->InAt(0);
6153 Register obj = obj_loc.AsRegister<Register>();
6154 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006155 LoadOperandType load_type = kLoadUnsignedByte;
6156 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006157 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006158 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006159
6160 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006161 case DataType::Type::kBool:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006162 load_type = kLoadUnsignedByte;
6163 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006164 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006165 load_type = kLoadSignedByte;
6166 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006167 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006168 load_type = kLoadSignedHalfword;
6169 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006170 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006171 load_type = kLoadUnsignedHalfword;
6172 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006173 case DataType::Type::kInt32:
6174 case DataType::Type::kFloat32:
6175 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006176 load_type = kLoadWord;
6177 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006178 case DataType::Type::kInt64:
6179 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006180 load_type = kLoadDoubleword;
6181 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006182 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006183 LOG(FATAL) << "Unreachable type " << type;
6184 UNREACHABLE();
6185 }
6186
6187 if (is_volatile && load_type == kLoadDoubleword) {
6188 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006189 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006190 // Do implicit Null check
6191 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6192 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006193 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006194 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006195 if (type == DataType::Type::kFloat64) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006196 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006197 if (dst_loc.IsFpuRegister()) {
6198 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006199 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006200 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006201 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006202 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006203 __ StoreToOffset(kStoreWord,
6204 locations->GetTemp(1).AsRegister<Register>(),
6205 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006206 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006207 __ StoreToOffset(kStoreWord,
6208 locations->GetTemp(2).AsRegister<Register>(),
6209 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006210 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006211 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006212 }
6213 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006214 if (type == DataType::Type::kReference) {
Alexey Frunze15958152017-02-09 19:08:30 -08006215 // /* HeapReference<Object> */ dst = *(obj + offset)
6216 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006217 Location temp_loc =
6218 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006219 // Note that a potential implicit null check is handled in this
6220 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6221 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6222 dst_loc,
6223 obj,
6224 offset,
6225 temp_loc,
6226 /* needs_null_check */ true);
6227 if (is_volatile) {
6228 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6229 }
6230 } else {
6231 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6232 if (is_volatile) {
6233 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6234 }
6235 // If read barriers are enabled, emit read barriers other than
6236 // Baker's using a slow path (and also unpoison the loaded
6237 // reference, if heap poisoning is enabled).
6238 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6239 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006240 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006241 Register dst;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006242 if (type == DataType::Type::kInt64) {
Alexey Frunze15958152017-02-09 19:08:30 -08006243 DCHECK(dst_loc.IsRegisterPair());
6244 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006245 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006246 DCHECK(dst_loc.IsRegister());
6247 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006248 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006249 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006250 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006251 DCHECK(dst_loc.IsFpuRegister());
6252 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006253 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006254 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006255 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006256 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006257 }
6258 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006259 }
6260
Alexey Frunze15958152017-02-09 19:08:30 -08006261 // Memory barriers, in the case of references, are handled in the
6262 // previous switch statement.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006263 if (is_volatile && (type != DataType::Type::kReference)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006264 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6265 }
6266}
6267
6268void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006269 DataType::Type field_type = field_info.GetFieldType();
6270 bool is_wide = (field_type == DataType::Type::kInt64) || (field_type == DataType::Type::kFloat64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006271 bool generate_volatile = field_info.IsVolatile() && is_wide;
6272 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006273 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006274
6275 locations->SetInAt(0, Location::RequiresRegister());
6276 if (generate_volatile) {
6277 InvokeRuntimeCallingConvention calling_convention;
6278 // need A0 to hold base + offset
6279 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006280 if (field_type == DataType::Type::kInt64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006281 locations->SetInAt(1, Location::RegisterPairLocation(
6282 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6283 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006284 // Use Location::Any() to prevent situations when running out of available fp registers.
6285 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006286 // Pass FP parameters in core registers.
6287 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6288 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6289 }
6290 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006291 if (DataType::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006292 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006293 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006294 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006295 }
6296 }
6297}
6298
6299void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6300 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006301 uint32_t dex_pc,
6302 bool value_can_be_null) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006303 DataType::Type type = field_info.GetFieldType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006304 LocationSummary* locations = instruction->GetLocations();
6305 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006306 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006307 StoreOperandType store_type = kStoreByte;
6308 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006309 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006310 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006311 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006312
6313 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006314 case DataType::Type::kBool:
6315 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006316 store_type = kStoreByte;
6317 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006318 case DataType::Type::kInt16:
6319 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006320 store_type = kStoreHalfword;
6321 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006322 case DataType::Type::kInt32:
6323 case DataType::Type::kFloat32:
6324 case DataType::Type::kReference:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006325 store_type = kStoreWord;
6326 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006327 case DataType::Type::kInt64:
6328 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006329 store_type = kStoreDoubleword;
6330 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006331 case DataType::Type::kVoid:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006332 LOG(FATAL) << "Unreachable type " << type;
6333 UNREACHABLE();
6334 }
6335
6336 if (is_volatile) {
6337 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6338 }
6339
6340 if (is_volatile && store_type == kStoreDoubleword) {
6341 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006342 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006343 // Do implicit Null check.
6344 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6345 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006346 if (type == DataType::Type::kFloat64) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006347 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006348 if (value_location.IsFpuRegister()) {
6349 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6350 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006351 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006352 value_location.AsFpuRegister<FRegister>());
6353 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006354 __ LoadFromOffset(kLoadWord,
6355 locations->GetTemp(1).AsRegister<Register>(),
6356 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006357 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006358 __ LoadFromOffset(kLoadWord,
6359 locations->GetTemp(2).AsRegister<Register>(),
6360 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006361 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006362 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006363 DCHECK(value_location.IsConstant());
6364 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6365 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006366 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6367 locations->GetTemp(1).AsRegister<Register>(),
6368 value);
6369 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006370 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006371 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006372 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6373 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006374 if (value_location.IsConstant()) {
6375 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6376 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006377 } else if (!DataType::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006378 Register src;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006379 if (type == DataType::Type::kInt64) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006380 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006381 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006382 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006383 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006384 if (kPoisonHeapReferences && needs_write_barrier) {
6385 // Note that in the case where `value` is a null reference,
6386 // we do not enter this block, as a null reference does not
6387 // need poisoning.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006388 DCHECK_EQ(type, DataType::Type::kReference);
Alexey Frunzec061de12017-02-14 13:27:23 -08006389 __ PoisonHeapReference(TMP, src);
6390 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6391 } else {
6392 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6393 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006394 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006395 FRegister src = value_location.AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01006396 if (type == DataType::Type::kFloat32) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006397 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006398 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006399 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006400 }
6401 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006402 }
6403
Alexey Frunzec061de12017-02-14 13:27:23 -08006404 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006405 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006406 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006407 }
6408
6409 if (is_volatile) {
6410 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6411 }
6412}
6413
6414void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6415 HandleFieldGet(instruction, instruction->GetFieldInfo());
6416}
6417
6418void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6419 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6420}
6421
6422void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6423 HandleFieldSet(instruction, instruction->GetFieldInfo());
6424}
6425
6426void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006427 HandleFieldSet(instruction,
6428 instruction->GetFieldInfo(),
6429 instruction->GetDexPc(),
6430 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006431}
6432
Alexey Frunze15958152017-02-09 19:08:30 -08006433void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6434 HInstruction* instruction,
6435 Location out,
6436 uint32_t offset,
6437 Location maybe_temp,
6438 ReadBarrierOption read_barrier_option) {
6439 Register out_reg = out.AsRegister<Register>();
6440 if (read_barrier_option == kWithReadBarrier) {
6441 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006442 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6443 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6444 }
Alexey Frunze15958152017-02-09 19:08:30 -08006445 if (kUseBakerReadBarrier) {
6446 // Load with fast path based Baker's read barrier.
6447 // /* HeapReference<Object> */ out = *(out + offset)
6448 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6449 out,
6450 out_reg,
6451 offset,
6452 maybe_temp,
6453 /* needs_null_check */ false);
6454 } else {
6455 // Load with slow path based read barrier.
6456 // Save the value of `out` into `maybe_temp` before overwriting it
6457 // in the following move operation, as we will need it for the
6458 // read barrier below.
6459 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6460 // /* HeapReference<Object> */ out = *(out + offset)
6461 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6462 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6463 }
6464 } else {
6465 // Plain load with no read barrier.
6466 // /* HeapReference<Object> */ out = *(out + offset)
6467 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6468 __ MaybeUnpoisonHeapReference(out_reg);
6469 }
6470}
6471
6472void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6473 HInstruction* instruction,
6474 Location out,
6475 Location obj,
6476 uint32_t offset,
6477 Location maybe_temp,
6478 ReadBarrierOption read_barrier_option) {
6479 Register out_reg = out.AsRegister<Register>();
6480 Register obj_reg = obj.AsRegister<Register>();
6481 if (read_barrier_option == kWithReadBarrier) {
6482 CHECK(kEmitCompilerReadBarrier);
6483 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006484 if (!kBakerReadBarrierThunksEnableForFields) {
6485 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6486 }
Alexey Frunze15958152017-02-09 19:08:30 -08006487 // Load with fast path based Baker's read barrier.
6488 // /* HeapReference<Object> */ out = *(obj + offset)
6489 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6490 out,
6491 obj_reg,
6492 offset,
6493 maybe_temp,
6494 /* needs_null_check */ false);
6495 } else {
6496 // Load with slow path based read barrier.
6497 // /* HeapReference<Object> */ out = *(obj + offset)
6498 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6499 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6500 }
6501 } else {
6502 // Plain load with no read barrier.
6503 // /* HeapReference<Object> */ out = *(obj + offset)
6504 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6505 __ MaybeUnpoisonHeapReference(out_reg);
6506 }
6507}
6508
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006509static inline int GetBakerMarkThunkNumber(Register reg) {
6510 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6511 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6512 return reg - V0;
6513 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6514 return 14 + (reg - S2);
6515 } else if (reg == FP) { // One more.
6516 return 20;
6517 }
6518 LOG(FATAL) << "Unexpected register " << reg;
6519 UNREACHABLE();
6520}
6521
6522static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6523 int num = GetBakerMarkThunkNumber(reg) +
6524 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6525 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6526}
6527
6528static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6529 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6530 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6531}
6532
Alexey Frunze15958152017-02-09 19:08:30 -08006533void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6534 Location root,
6535 Register obj,
6536 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006537 ReadBarrierOption read_barrier_option,
6538 MipsLabel* label_low) {
6539 bool reordering;
6540 if (label_low != nullptr) {
6541 DCHECK_EQ(offset, 0x5678u);
6542 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006543 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006544 if (read_barrier_option == kWithReadBarrier) {
6545 DCHECK(kEmitCompilerReadBarrier);
6546 if (kUseBakerReadBarrier) {
6547 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6548 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006549 if (kBakerReadBarrierThunksEnableForGcRoots) {
6550 // Note that we do not actually check the value of `GetIsGcMarking()`
6551 // to decide whether to mark the loaded GC root or not. Instead, we
6552 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6553 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6554 // vice versa.
6555 //
6556 // We use thunks for the slow path. That thunk checks the reference
6557 // and jumps to the entrypoint if needed.
6558 //
6559 // temp = Thread::Current()->pReadBarrierMarkReg00
6560 // // AKA &art_quick_read_barrier_mark_introspection.
6561 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6562 // if (temp != nullptr) {
6563 // temp = &gc_root_thunk<root_reg>
6564 // root = temp(root)
6565 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006566
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006567 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6568 const int32_t entry_point_offset =
6569 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6570 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6571 int16_t offset_low = Low16Bits(offset);
6572 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6573 // extension in lw.
6574 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6575 Register base = short_offset ? obj : TMP;
6576 // Loading the entrypoint does not require a load acquire since it is only changed when
6577 // threads are suspended or running a checkpoint.
6578 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6579 reordering = __ SetReorder(false);
6580 if (!short_offset) {
6581 DCHECK(!label_low);
6582 __ AddUpper(base, obj, offset_high);
6583 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006584 MipsLabel skip_call;
6585 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006586 if (label_low != nullptr) {
6587 DCHECK(short_offset);
6588 __ Bind(label_low);
6589 }
6590 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6591 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6592 // in delay slot.
6593 if (isR6) {
6594 __ Jialc(T9, thunk_disp);
6595 } else {
6596 __ Addiu(T9, T9, thunk_disp);
6597 __ Jalr(T9);
6598 __ Nop();
6599 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006600 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006601 __ SetReorder(reordering);
6602 } else {
6603 // Note that we do not actually check the value of `GetIsGcMarking()`
6604 // to decide whether to mark the loaded GC root or not. Instead, we
6605 // load into `temp` (T9) the read barrier mark entry point corresponding
6606 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6607 // is false, and vice versa.
6608 //
6609 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6610 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6611 // if (temp != null) {
6612 // root = temp(root)
6613 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006614
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006615 if (label_low != nullptr) {
6616 reordering = __ SetReorder(false);
6617 __ Bind(label_low);
6618 }
6619 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6620 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6621 if (label_low != nullptr) {
6622 __ SetReorder(reordering);
6623 }
6624 static_assert(
6625 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6626 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6627 "have different sizes.");
6628 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6629 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6630 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006631
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006632 // Slow path marking the GC root `root`.
6633 Location temp = Location::RegisterLocation(T9);
6634 SlowPathCodeMIPS* slow_path =
6635 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6636 instruction,
6637 root,
6638 /*entrypoint*/ temp);
6639 codegen_->AddSlowPath(slow_path);
6640
6641 const int32_t entry_point_offset =
6642 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6643 // Loading the entrypoint does not require a load acquire since it is only changed when
6644 // threads are suspended or running a checkpoint.
6645 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6646 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6647 __ Bind(slow_path->GetExitLabel());
6648 }
Alexey Frunze15958152017-02-09 19:08:30 -08006649 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006650 if (label_low != nullptr) {
6651 reordering = __ SetReorder(false);
6652 __ Bind(label_low);
6653 }
Alexey Frunze15958152017-02-09 19:08:30 -08006654 // GC root loaded through a slow path for read barriers other
6655 // than Baker's.
6656 // /* GcRoot<mirror::Object>* */ root = obj + offset
6657 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006658 if (label_low != nullptr) {
6659 __ SetReorder(reordering);
6660 }
Alexey Frunze15958152017-02-09 19:08:30 -08006661 // /* mirror::Object* */ root = root->Read()
6662 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6663 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006664 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006665 if (label_low != nullptr) {
6666 reordering = __ SetReorder(false);
6667 __ Bind(label_low);
6668 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006669 // Plain GC root load with no read barrier.
6670 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6671 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6672 // Note that GC roots are not affected by heap poisoning, thus we
6673 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006674 if (label_low != nullptr) {
6675 __ SetReorder(reordering);
6676 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006677 }
6678}
6679
Alexey Frunze15958152017-02-09 19:08:30 -08006680void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6681 Location ref,
6682 Register obj,
6683 uint32_t offset,
6684 Location temp,
6685 bool needs_null_check) {
6686 DCHECK(kEmitCompilerReadBarrier);
6687 DCHECK(kUseBakerReadBarrier);
6688
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006689 if (kBakerReadBarrierThunksEnableForFields) {
6690 // Note that we do not actually check the value of `GetIsGcMarking()`
6691 // to decide whether to mark the loaded reference or not. Instead, we
6692 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6693 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6694 // vice versa.
6695 //
6696 // We use thunks for the slow path. That thunk checks the reference
6697 // and jumps to the entrypoint if needed. If the holder is not gray,
6698 // it issues a load-load memory barrier and returns to the original
6699 // reference load.
6700 //
6701 // temp = Thread::Current()->pReadBarrierMarkReg00
6702 // // AKA &art_quick_read_barrier_mark_introspection.
6703 // if (temp != nullptr) {
6704 // temp = &field_array_thunk<holder_reg>
6705 // temp()
6706 // }
6707 // not_gray_return_address:
6708 // // If the offset is too large to fit into the lw instruction, we
6709 // // use an adjusted base register (TMP) here. This register
6710 // // receives bits 16 ... 31 of the offset before the thunk invocation
6711 // // and the thunk benefits from it.
6712 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6713 // gray_return_address:
6714
6715 DCHECK(temp.IsInvalid());
6716 bool isR6 = GetInstructionSetFeatures().IsR6();
6717 int16_t offset_low = Low16Bits(offset);
6718 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6719 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6720 bool reordering = __ SetReorder(false);
6721 const int32_t entry_point_offset =
6722 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6723 // There may have or may have not been a null check if the field offset is smaller than
6724 // the page size.
6725 // There must've been a null check in case it's actually a load from an array.
6726 // We will, however, perform an explicit null check in the thunk as it's easier to
6727 // do it than not.
6728 if (instruction->IsArrayGet()) {
6729 DCHECK(!needs_null_check);
6730 }
6731 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6732 // Loading the entrypoint does not require a load acquire since it is only changed when
6733 // threads are suspended or running a checkpoint.
6734 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6735 Register ref_reg = ref.AsRegister<Register>();
6736 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006737 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006738 if (short_offset) {
6739 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006740 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006741 __ Nop(); // In forbidden slot.
6742 __ Jialc(T9, thunk_disp);
6743 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006744 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006745 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6746 __ Jalr(T9);
6747 __ Nop(); // In delay slot.
6748 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006749 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006750 } else {
6751 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006752 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006753 __ Aui(base, obj, offset_high); // In delay slot.
6754 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006755 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006756 } else {
6757 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006758 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006759 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6760 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006761 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006762 __ Addu(base, base, obj); // In delay slot.
6763 }
6764 }
6765 // /* HeapReference<Object> */ ref = *(obj + offset)
6766 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6767 if (needs_null_check) {
6768 MaybeRecordImplicitNullCheck(instruction);
6769 }
6770 __ MaybeUnpoisonHeapReference(ref_reg);
6771 __ SetReorder(reordering);
6772 return;
6773 }
6774
Alexey Frunze15958152017-02-09 19:08:30 -08006775 // /* HeapReference<Object> */ ref = *(obj + offset)
6776 Location no_index = Location::NoLocation();
6777 ScaleFactor no_scale_factor = TIMES_1;
6778 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6779 ref,
6780 obj,
6781 offset,
6782 no_index,
6783 no_scale_factor,
6784 temp,
6785 needs_null_check);
6786}
6787
6788void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6789 Location ref,
6790 Register obj,
6791 uint32_t data_offset,
6792 Location index,
6793 Location temp,
6794 bool needs_null_check) {
6795 DCHECK(kEmitCompilerReadBarrier);
6796 DCHECK(kUseBakerReadBarrier);
6797
6798 static_assert(
6799 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6800 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006801 ScaleFactor scale_factor = TIMES_4;
6802
6803 if (kBakerReadBarrierThunksEnableForArrays) {
6804 // Note that we do not actually check the value of `GetIsGcMarking()`
6805 // to decide whether to mark the loaded reference or not. Instead, we
6806 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6807 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6808 // vice versa.
6809 //
6810 // We use thunks for the slow path. That thunk checks the reference
6811 // and jumps to the entrypoint if needed. If the holder is not gray,
6812 // it issues a load-load memory barrier and returns to the original
6813 // reference load.
6814 //
6815 // temp = Thread::Current()->pReadBarrierMarkReg00
6816 // // AKA &art_quick_read_barrier_mark_introspection.
6817 // if (temp != nullptr) {
6818 // temp = &field_array_thunk<holder_reg>
6819 // temp()
6820 // }
6821 // not_gray_return_address:
6822 // // The element address is pre-calculated in the TMP register before the
6823 // // thunk invocation and the thunk benefits from it.
6824 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6825 // gray_return_address:
6826
6827 DCHECK(temp.IsInvalid());
6828 DCHECK(index.IsValid());
6829 bool reordering = __ SetReorder(false);
6830 const int32_t entry_point_offset =
6831 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6832 // We will not do the explicit null check in the thunk as some form of a null check
6833 // must've been done earlier.
6834 DCHECK(!needs_null_check);
6835 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6836 // Loading the entrypoint does not require a load acquire since it is only changed when
6837 // threads are suspended or running a checkpoint.
6838 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6839 Register ref_reg = ref.AsRegister<Register>();
6840 Register index_reg = index.IsRegisterPair()
6841 ? index.AsRegisterPairLow<Register>()
6842 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006843 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006844 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006845 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006846 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6847 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006848 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006849 } else {
6850 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006851 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006852 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6853 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006854 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006855 __ Addu(TMP, TMP, obj); // In delay slot.
6856 }
6857 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6858 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6859 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6860 __ MaybeUnpoisonHeapReference(ref_reg);
6861 __ SetReorder(reordering);
6862 return;
6863 }
6864
Alexey Frunze15958152017-02-09 19:08:30 -08006865 // /* HeapReference<Object> */ ref =
6866 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006867 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6868 ref,
6869 obj,
6870 data_offset,
6871 index,
6872 scale_factor,
6873 temp,
6874 needs_null_check);
6875}
6876
6877void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6878 Location ref,
6879 Register obj,
6880 uint32_t offset,
6881 Location index,
6882 ScaleFactor scale_factor,
6883 Location temp,
6884 bool needs_null_check,
6885 bool always_update_field) {
6886 DCHECK(kEmitCompilerReadBarrier);
6887 DCHECK(kUseBakerReadBarrier);
6888
6889 // In slow path based read barriers, the read barrier call is
6890 // inserted after the original load. However, in fast path based
6891 // Baker's read barriers, we need to perform the load of
6892 // mirror::Object::monitor_ *before* the original reference load.
6893 // This load-load ordering is required by the read barrier.
6894 // The fast path/slow path (for Baker's algorithm) should look like:
6895 //
6896 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6897 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6898 // HeapReference<Object> ref = *src; // Original reference load.
6899 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6900 // if (is_gray) {
6901 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6902 // }
6903 //
6904 // Note: the original implementation in ReadBarrier::Barrier is
6905 // slightly more complex as it performs additional checks that we do
6906 // not do here for performance reasons.
6907
6908 Register ref_reg = ref.AsRegister<Register>();
6909 Register temp_reg = temp.AsRegister<Register>();
6910 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6911
6912 // /* int32_t */ monitor = obj->monitor_
6913 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6914 if (needs_null_check) {
6915 MaybeRecordImplicitNullCheck(instruction);
6916 }
6917 // /* LockWord */ lock_word = LockWord(monitor)
6918 static_assert(sizeof(LockWord) == sizeof(int32_t),
6919 "art::LockWord and int32_t have different sizes.");
6920
6921 __ Sync(0); // Barrier to prevent load-load reordering.
6922
6923 // The actual reference load.
6924 if (index.IsValid()) {
6925 // Load types involving an "index": ArrayGet,
6926 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6927 // intrinsics.
6928 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6929 if (index.IsConstant()) {
6930 size_t computed_offset =
6931 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6932 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6933 } else {
6934 // Handle the special case of the
6935 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6936 // intrinsics, which use a register pair as index ("long
6937 // offset"), of which only the low part contains data.
6938 Register index_reg = index.IsRegisterPair()
6939 ? index.AsRegisterPairLow<Register>()
6940 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006941 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006942 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6943 }
6944 } else {
6945 // /* HeapReference<Object> */ ref = *(obj + offset)
6946 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6947 }
6948
6949 // Object* ref = ref_addr->AsMirrorPtr()
6950 __ MaybeUnpoisonHeapReference(ref_reg);
6951
6952 // Slow path marking the object `ref` when it is gray.
6953 SlowPathCodeMIPS* slow_path;
6954 if (always_update_field) {
6955 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6956 // of the form `obj + field_offset`, where `obj` is a register and
6957 // `field_offset` is a register pair (of which only the lower half
6958 // is used). Thus `offset` and `scale_factor` above are expected
6959 // to be null in this code path.
6960 DCHECK_EQ(offset, 0u);
6961 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6962 slow_path = new (GetGraph()->GetArena())
6963 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6964 ref,
6965 obj,
6966 /* field_offset */ index,
6967 temp_reg);
6968 } else {
6969 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6970 }
6971 AddSlowPath(slow_path);
6972
6973 // if (rb_state == ReadBarrier::GrayState())
6974 // ref = ReadBarrier::Mark(ref);
6975 // Given the numeric representation, it's enough to check the low bit of the
6976 // rb_state. We do that by shifting the bit into the sign bit (31) and
6977 // performing a branch on less than zero.
6978 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6979 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6980 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6981 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6982 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6983 __ Bind(slow_path->GetExitLabel());
6984}
6985
6986void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6987 Location out,
6988 Location ref,
6989 Location obj,
6990 uint32_t offset,
6991 Location index) {
6992 DCHECK(kEmitCompilerReadBarrier);
6993
6994 // Insert a slow path based read barrier *after* the reference load.
6995 //
6996 // If heap poisoning is enabled, the unpoisoning of the loaded
6997 // reference will be carried out by the runtime within the slow
6998 // path.
6999 //
7000 // Note that `ref` currently does not get unpoisoned (when heap
7001 // poisoning is enabled), which is alright as the `ref` argument is
7002 // not used by the artReadBarrierSlow entry point.
7003 //
7004 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7005 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
7006 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7007 AddSlowPath(slow_path);
7008
7009 __ B(slow_path->GetEntryLabel());
7010 __ Bind(slow_path->GetExitLabel());
7011}
7012
7013void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7014 Location out,
7015 Location ref,
7016 Location obj,
7017 uint32_t offset,
7018 Location index) {
7019 if (kEmitCompilerReadBarrier) {
7020 // Baker's read barriers shall be handled by the fast path
7021 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7022 DCHECK(!kUseBakerReadBarrier);
7023 // If heap poisoning is enabled, unpoisoning will be taken care of
7024 // by the runtime within the slow path.
7025 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7026 } else if (kPoisonHeapReferences) {
7027 __ UnpoisonHeapReference(out.AsRegister<Register>());
7028 }
7029}
7030
7031void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7032 Location out,
7033 Location root) {
7034 DCHECK(kEmitCompilerReadBarrier);
7035
7036 // Insert a slow path based read barrier *after* the GC root load.
7037 //
7038 // Note that GC roots are not affected by heap poisoning, so we do
7039 // not need to do anything special for this here.
7040 SlowPathCodeMIPS* slow_path =
7041 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
7042 AddSlowPath(slow_path);
7043
7044 __ B(slow_path->GetEntryLabel());
7045 __ Bind(slow_path->GetExitLabel());
7046}
7047
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007048void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007049 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7050 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007051 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007052 switch (type_check_kind) {
7053 case TypeCheckKind::kExactCheck:
7054 case TypeCheckKind::kAbstractClassCheck:
7055 case TypeCheckKind::kClassHierarchyCheck:
7056 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007057 call_kind =
7058 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007059 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007060 break;
7061 case TypeCheckKind::kArrayCheck:
7062 case TypeCheckKind::kUnresolvedCheck:
7063 case TypeCheckKind::kInterfaceCheck:
7064 call_kind = LocationSummary::kCallOnSlowPath;
7065 break;
7066 }
7067
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007068 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007069 if (baker_read_barrier_slow_path) {
7070 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7071 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007072 locations->SetInAt(0, Location::RequiresRegister());
7073 locations->SetInAt(1, Location::RequiresRegister());
7074 // The output does overlap inputs.
7075 // Note that TypeCheckSlowPathMIPS uses this register too.
7076 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007077 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007078}
7079
7080void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007081 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007082 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007083 Location obj_loc = locations->InAt(0);
7084 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007085 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007086 Location out_loc = locations->Out();
7087 Register out = out_loc.AsRegister<Register>();
7088 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7089 DCHECK_LE(num_temps, 1u);
7090 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007091 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7092 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7093 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7094 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007095 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007096 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007097
7098 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007099 // Avoid this check if we know `obj` is not null.
7100 if (instruction->MustDoNullCheck()) {
7101 __ Move(out, ZERO);
7102 __ Beqz(obj, &done);
7103 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007104
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007105 switch (type_check_kind) {
7106 case TypeCheckKind::kExactCheck: {
7107 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007108 GenerateReferenceLoadTwoRegisters(instruction,
7109 out_loc,
7110 obj_loc,
7111 class_offset,
7112 maybe_temp_loc,
7113 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007114 // Classes must be equal for the instanceof to succeed.
7115 __ Xor(out, out, cls);
7116 __ Sltiu(out, out, 1);
7117 break;
7118 }
7119
7120 case TypeCheckKind::kAbstractClassCheck: {
7121 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007122 GenerateReferenceLoadTwoRegisters(instruction,
7123 out_loc,
7124 obj_loc,
7125 class_offset,
7126 maybe_temp_loc,
7127 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007128 // If the class is abstract, we eagerly fetch the super class of the
7129 // object to avoid doing a comparison we know will fail.
7130 MipsLabel loop;
7131 __ Bind(&loop);
7132 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007133 GenerateReferenceLoadOneRegister(instruction,
7134 out_loc,
7135 super_offset,
7136 maybe_temp_loc,
7137 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007138 // If `out` is null, we use it for the result, and jump to `done`.
7139 __ Beqz(out, &done);
7140 __ Bne(out, cls, &loop);
7141 __ LoadConst32(out, 1);
7142 break;
7143 }
7144
7145 case TypeCheckKind::kClassHierarchyCheck: {
7146 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007147 GenerateReferenceLoadTwoRegisters(instruction,
7148 out_loc,
7149 obj_loc,
7150 class_offset,
7151 maybe_temp_loc,
7152 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007153 // Walk over the class hierarchy to find a match.
7154 MipsLabel loop, success;
7155 __ Bind(&loop);
7156 __ Beq(out, cls, &success);
7157 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007158 GenerateReferenceLoadOneRegister(instruction,
7159 out_loc,
7160 super_offset,
7161 maybe_temp_loc,
7162 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007163 __ Bnez(out, &loop);
7164 // If `out` is null, we use it for the result, and jump to `done`.
7165 __ B(&done);
7166 __ Bind(&success);
7167 __ LoadConst32(out, 1);
7168 break;
7169 }
7170
7171 case TypeCheckKind::kArrayObjectCheck: {
7172 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007173 GenerateReferenceLoadTwoRegisters(instruction,
7174 out_loc,
7175 obj_loc,
7176 class_offset,
7177 maybe_temp_loc,
7178 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007179 // Do an exact check.
7180 MipsLabel success;
7181 __ Beq(out, cls, &success);
7182 // Otherwise, we need to check that the object's class is a non-primitive array.
7183 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007184 GenerateReferenceLoadOneRegister(instruction,
7185 out_loc,
7186 component_offset,
7187 maybe_temp_loc,
7188 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007189 // If `out` is null, we use it for the result, and jump to `done`.
7190 __ Beqz(out, &done);
7191 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7192 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7193 __ Sltiu(out, out, 1);
7194 __ B(&done);
7195 __ Bind(&success);
7196 __ LoadConst32(out, 1);
7197 break;
7198 }
7199
7200 case TypeCheckKind::kArrayCheck: {
7201 // No read barrier since the slow path will retry upon failure.
7202 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007203 GenerateReferenceLoadTwoRegisters(instruction,
7204 out_loc,
7205 obj_loc,
7206 class_offset,
7207 maybe_temp_loc,
7208 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007209 DCHECK(locations->OnlyCallsOnSlowPath());
7210 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7211 /* is_fatal */ false);
7212 codegen_->AddSlowPath(slow_path);
7213 __ Bne(out, cls, slow_path->GetEntryLabel());
7214 __ LoadConst32(out, 1);
7215 break;
7216 }
7217
7218 case TypeCheckKind::kUnresolvedCheck:
7219 case TypeCheckKind::kInterfaceCheck: {
7220 // Note that we indeed only call on slow path, but we always go
7221 // into the slow path for the unresolved and interface check
7222 // cases.
7223 //
7224 // We cannot directly call the InstanceofNonTrivial runtime
7225 // entry point without resorting to a type checking slow path
7226 // here (i.e. by calling InvokeRuntime directly), as it would
7227 // require to assign fixed registers for the inputs of this
7228 // HInstanceOf instruction (following the runtime calling
7229 // convention), which might be cluttered by the potential first
7230 // read barrier emission at the beginning of this method.
7231 //
7232 // TODO: Introduce a new runtime entry point taking the object
7233 // to test (instead of its class) as argument, and let it deal
7234 // with the read barrier issues. This will let us refactor this
7235 // case of the `switch` code as it was previously (with a direct
7236 // call to the runtime not using a type checking slow path).
7237 // This should also be beneficial for the other cases above.
7238 DCHECK(locations->OnlyCallsOnSlowPath());
7239 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7240 /* is_fatal */ false);
7241 codegen_->AddSlowPath(slow_path);
7242 __ B(slow_path->GetEntryLabel());
7243 break;
7244 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007245 }
7246
7247 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007248
7249 if (slow_path != nullptr) {
7250 __ Bind(slow_path->GetExitLabel());
7251 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007252}
7253
7254void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
7255 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7256 locations->SetOut(Location::ConstantLocation(constant));
7257}
7258
7259void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7260 // Will be generated at use site.
7261}
7262
7263void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
7264 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7265 locations->SetOut(Location::ConstantLocation(constant));
7266}
7267
7268void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7269 // Will be generated at use site.
7270}
7271
7272void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7273 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7274 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7275}
7276
7277void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7278 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007279 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007280 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007281 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007282}
7283
7284void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7285 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7286 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007287 Location receiver = invoke->GetLocations()->InAt(0);
7288 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007289 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007290
7291 // Set the hidden argument.
7292 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7293 invoke->GetDexMethodIndex());
7294
7295 // temp = object->GetClass();
7296 if (receiver.IsStackSlot()) {
7297 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7298 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7299 } else {
7300 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7301 }
7302 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007303 // Instead of simply (possibly) unpoisoning `temp` here, we should
7304 // emit a read barrier for the previous class reference load.
7305 // However this is not required in practice, as this is an
7306 // intermediate/temporary reference and because the current
7307 // concurrent copying collector keeps the from-space memory
7308 // intact/accessible until the end of the marking phase (the
7309 // concurrent copying collector may not in the future).
7310 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007311 __ LoadFromOffset(kLoadWord, temp, temp,
7312 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7313 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007314 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007315 // temp = temp->GetImtEntryAt(method_offset);
7316 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7317 // T9 = temp->GetEntryPoint();
7318 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7319 // T9();
7320 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007321 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007322 DCHECK(!codegen_->IsLeafMethod());
7323 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7324}
7325
7326void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007327 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7328 if (intrinsic.TryDispatch(invoke)) {
7329 return;
7330 }
7331
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007332 HandleInvoke(invoke);
7333}
7334
7335void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007336 // Explicit clinit checks triggered by static invokes must have been pruned by
7337 // art::PrepareForRegisterAllocation.
7338 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007339
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007340 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007341 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7342 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007343
Chris Larsen701566a2015-10-27 15:29:13 -07007344 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7345 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007346 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7347 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7348 }
Chris Larsen701566a2015-10-27 15:29:13 -07007349 return;
7350 }
7351
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007352 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007353
7354 // Add the extra input register if either the dex cache array base register
7355 // or the PC-relative base register for accessing literals is needed.
7356 if (has_extra_input) {
7357 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7358 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007359}
7360
Orion Hodsonac141392017-01-13 11:53:47 +00007361void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7362 HandleInvoke(invoke);
7363}
7364
7365void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7366 codegen_->GenerateInvokePolymorphicCall(invoke);
7367}
7368
Chris Larsen701566a2015-10-27 15:29:13 -07007369static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007370 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007371 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7372 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007373 return true;
7374 }
7375 return false;
7376}
7377
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007378HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007379 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007380 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007381 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007382 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007383 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007384 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007385 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007386 case HLoadString::LoadKind::kJitTableAddress:
7387 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007388 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007389 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007390 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007391 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007392 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007393 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007394}
7395
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007396HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7397 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007398 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007399 case HLoadClass::LoadKind::kInvalid:
7400 LOG(FATAL) << "UNREACHABLE";
7401 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007402 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007403 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007404 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007405 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007406 case HLoadClass::LoadKind::kBssEntry:
7407 DCHECK(!Runtime::Current()->UseJitCompilation());
7408 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007409 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007410 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007411 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007412 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007413 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007414 break;
7415 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007416 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007417}
7418
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007419Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7420 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007421 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007422 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007423 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7424 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7425 if (!invoke->GetLocations()->Intrinsified()) {
7426 return location.AsRegister<Register>();
7427 }
7428 // For intrinsics we allow any location, so it may be on the stack.
7429 if (!location.IsRegister()) {
7430 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7431 return temp;
7432 }
7433 // For register locations, check if the register was saved. If so, get it from the stack.
7434 // Note: There is a chance that the register was saved but not overwritten, so we could
7435 // save one load. However, since this is just an intrinsic slow path we prefer this
7436 // simple and more robust approach rather that trying to determine if that's the case.
7437 SlowPathCode* slow_path = GetCurrentSlowPath();
7438 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7439 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7440 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7441 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7442 return temp;
7443 }
7444 return location.AsRegister<Register>();
7445}
7446
Vladimir Markodc151b22015-10-15 18:02:30 +01007447HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7448 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007449 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007450 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007451}
7452
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007453void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7454 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007455 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007456 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007457 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7458 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007459 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007460 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7461 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007462 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7463 : ZERO;
7464
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007465 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007466 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007467 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007468 uint32_t offset =
7469 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007470 __ LoadFromOffset(kLoadWord,
7471 temp.AsRegister<Register>(),
7472 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007473 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007474 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007475 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007476 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007477 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007478 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007479 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7480 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007481 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7482 PcRelativePatchInfo* info_low =
7483 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007484 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007485 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7486 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007487 break;
7488 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007489 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7490 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7491 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007492 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007493 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007494 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007495 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7496 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007497 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007498 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7499 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007500 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007501 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007502 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7503 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7504 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007505 }
7506 }
7507
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007508 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007509 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007510 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007511 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007512 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7513 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007514 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007515 T9,
7516 callee_method.AsRegister<Register>(),
7517 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007518 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007519 // T9()
7520 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007521 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007522 break;
7523 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007524 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7525
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007526 DCHECK(!IsLeafMethod());
7527}
7528
7529void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007530 // Explicit clinit checks triggered by static invokes must have been pruned by
7531 // art::PrepareForRegisterAllocation.
7532 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007533
7534 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7535 return;
7536 }
7537
7538 LocationSummary* locations = invoke->GetLocations();
7539 codegen_->GenerateStaticOrDirectCall(invoke,
7540 locations->HasTemps()
7541 ? locations->GetTemp(0)
7542 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007543}
7544
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007545void CodeGeneratorMIPS::GenerateVirtualCall(
7546 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007547 // Use the calling convention instead of the location of the receiver, as
7548 // intrinsics may have put the receiver in a different register. In the intrinsics
7549 // slow path, the arguments have been moved to the right place, so here we are
7550 // guaranteed that the receiver is the first register of the calling convention.
7551 InvokeDexCallingConvention calling_convention;
7552 Register receiver = calling_convention.GetRegisterAt(0);
7553
Chris Larsen3acee732015-11-18 13:31:08 -08007554 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007555 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7556 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7557 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007558 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007559
7560 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007561 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007562 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007563 // Instead of simply (possibly) unpoisoning `temp` here, we should
7564 // emit a read barrier for the previous class reference load.
7565 // However this is not required in practice, as this is an
7566 // intermediate/temporary reference and because the current
7567 // concurrent copying collector keeps the from-space memory
7568 // intact/accessible until the end of the marking phase (the
7569 // concurrent copying collector may not in the future).
7570 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007571 // temp = temp->GetMethodAt(method_offset);
7572 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7573 // T9 = temp->GetEntryPoint();
7574 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7575 // T9();
7576 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007577 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007578 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007579}
7580
7581void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7582 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7583 return;
7584 }
7585
7586 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007587 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007588}
7589
7590void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007591 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007592 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007593 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007594 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7595 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007596 return;
7597 }
Vladimir Marko41559982017-01-06 14:04:23 +00007598 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007599 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007600 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007601 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7602 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007603 ? LocationSummary::kCallOnSlowPath
7604 : LocationSummary::kNoCall;
7605 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007606 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7607 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7608 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007609 switch (load_kind) {
7610 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007611 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007612 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007613 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007614 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007615 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007616 break;
7617 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007618 if (has_irreducible_loops) {
7619 codegen_->ClobberRA();
7620 break;
7621 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007622 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007623 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007624 locations->SetInAt(0, Location::RequiresRegister());
7625 break;
7626 default:
7627 break;
7628 }
7629 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007630 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7631 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7632 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007633 // Request a temp to hold the BSS entry location for the slow path.
7634 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007635 RegisterSet caller_saves = RegisterSet::Empty();
7636 InvokeRuntimeCallingConvention calling_convention;
7637 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7638 locations->SetCustomSlowPathCallerSaves(caller_saves);
7639 } else {
7640 // For non-Baker read barriers we have a temp-clobbering call.
7641 }
7642 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007643}
7644
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007645// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7646// move.
7647void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007648 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007649 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007650 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007651 return;
7652 }
Vladimir Marko41559982017-01-06 14:04:23 +00007653 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007654
Vladimir Marko41559982017-01-06 14:04:23 +00007655 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007656 Location out_loc = locations->Out();
7657 Register out = out_loc.AsRegister<Register>();
7658 Register base_or_current_method_reg;
7659 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007660 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007661 switch (load_kind) {
7662 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007663 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007664 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007665 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007666 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007667 base_or_current_method_reg =
7668 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007669 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007670 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007671 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007672 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7673 break;
7674 default:
7675 base_or_current_method_reg = ZERO;
7676 break;
7677 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007678
Alexey Frunze15958152017-02-09 19:08:30 -08007679 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7680 ? kWithoutReadBarrier
7681 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007682 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007683 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007684 switch (load_kind) {
7685 case HLoadClass::LoadKind::kReferrersClass: {
7686 DCHECK(!cls->CanCallRuntime());
7687 DCHECK(!cls->MustGenerateClinitCheck());
7688 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7689 GenerateGcRootFieldLoad(cls,
7690 out_loc,
7691 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007692 ArtMethod::DeclaringClassOffset().Int32Value(),
7693 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007694 break;
7695 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007696 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007697 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007698 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007699 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007700 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007701 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7702 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007703 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7704 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007705 base_or_current_method_reg);
7706 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007707 break;
7708 }
7709 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007710 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007711 uint32_t address = dchecked_integral_cast<uint32_t>(
7712 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7713 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007714 if (isR6 || !has_irreducible_loops) {
7715 __ LoadLiteral(out,
7716 base_or_current_method_reg,
7717 codegen_->DeduplicateBootImageAddressLiteral(address));
7718 } else {
7719 __ LoadConst32(out, address);
7720 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007721 break;
7722 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007723 case HLoadClass::LoadKind::kBootImageClassTable: {
7724 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7725 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7726 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7727 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7728 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7729 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7730 out,
7731 base_or_current_method_reg);
7732 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7733 // Extract the reference from the slot data, i.e. clear the hash bits.
7734 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7735 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7736 if (masked_hash != 0) {
7737 __ Addiu(out, out, -masked_hash);
7738 }
7739 break;
7740 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007741 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007742 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7743 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7744 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007745 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007746 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007747 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7748 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007749 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007750 GenerateGcRootFieldLoad(cls,
7751 out_loc,
7752 temp,
7753 /* placeholder */ 0x5678,
7754 read_barrier_option,
7755 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007756 generate_null_check = true;
7757 break;
7758 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007759 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007760 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7761 cls->GetTypeIndex(),
7762 cls->GetClass());
7763 bool reordering = __ SetReorder(false);
7764 __ Bind(&info->high_label);
7765 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007766 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007767 GenerateGcRootFieldLoad(cls,
7768 out_loc,
7769 out,
7770 /* placeholder */ 0x5678,
7771 read_barrier_option,
7772 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007773 break;
7774 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007775 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007776 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007777 LOG(FATAL) << "UNREACHABLE";
7778 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007779 }
7780
7781 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7782 DCHECK(cls->CanCallRuntime());
7783 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007784 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007785 codegen_->AddSlowPath(slow_path);
7786 if (generate_null_check) {
7787 __ Beqz(out, slow_path->GetEntryLabel());
7788 }
7789 if (cls->MustGenerateClinitCheck()) {
7790 GenerateClassInitializationCheck(slow_path, out);
7791 } else {
7792 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007793 }
7794 }
7795}
7796
7797static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007798 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007799}
7800
7801void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7802 LocationSummary* locations =
7803 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7804 locations->SetOut(Location::RequiresRegister());
7805}
7806
7807void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7808 Register out = load->GetLocations()->Out().AsRegister<Register>();
7809 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7810}
7811
7812void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7813 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7814}
7815
7816void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7817 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7818}
7819
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007820void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007821 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007822 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007823 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007824 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007825 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007826 switch (load_kind) {
7827 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007828 case HLoadString::LoadKind::kBootImageAddress:
7829 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007830 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007831 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007832 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007833 break;
7834 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007835 if (has_irreducible_loops) {
7836 codegen_->ClobberRA();
7837 break;
7838 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007839 FALLTHROUGH_INTENDED;
7840 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007841 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007842 locations->SetInAt(0, Location::RequiresRegister());
7843 break;
7844 default:
7845 break;
7846 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007847 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007848 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007849 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007850 } else {
7851 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007852 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7853 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7854 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007855 // Request a temp to hold the BSS entry location for the slow path.
7856 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007857 RegisterSet caller_saves = RegisterSet::Empty();
7858 InvokeRuntimeCallingConvention calling_convention;
7859 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7860 locations->SetCustomSlowPathCallerSaves(caller_saves);
7861 } else {
7862 // For non-Baker read barriers we have a temp-clobbering call.
7863 }
7864 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007865 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007866}
7867
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007868// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7869// move.
7870void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007871 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007872 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007873 Location out_loc = locations->Out();
7874 Register out = out_loc.AsRegister<Register>();
7875 Register base_or_current_method_reg;
7876 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007877 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007878 switch (load_kind) {
7879 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007880 case HLoadString::LoadKind::kBootImageAddress:
7881 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007882 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007883 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007884 base_or_current_method_reg =
7885 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007886 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007887 default:
7888 base_or_current_method_reg = ZERO;
7889 break;
7890 }
7891
7892 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007893 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007894 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007895 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007896 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007897 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7898 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007899 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7900 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007901 base_or_current_method_reg);
7902 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007903 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007904 }
7905 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007906 uint32_t address = dchecked_integral_cast<uint32_t>(
7907 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7908 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007909 if (isR6 || !has_irreducible_loops) {
7910 __ LoadLiteral(out,
7911 base_or_current_method_reg,
7912 codegen_->DeduplicateBootImageAddressLiteral(address));
7913 } else {
7914 __ LoadConst32(out, address);
7915 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007916 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007917 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007918 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007919 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007920 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007921 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007922 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7923 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007924 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7925 out,
7926 base_or_current_method_reg);
7927 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7928 return;
7929 }
7930 case HLoadString::LoadKind::kBssEntry: {
7931 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7932 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7933 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
7934 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7935 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007936 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007937 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007938 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7939 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007940 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007941 GenerateGcRootFieldLoad(load,
7942 out_loc,
7943 temp,
7944 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007945 kCompilerReadBarrierOption,
7946 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007947 SlowPathCodeMIPS* slow_path =
7948 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007949 codegen_->AddSlowPath(slow_path);
7950 __ Beqz(out, slow_path->GetEntryLabel());
7951 __ Bind(slow_path->GetExitLabel());
7952 return;
7953 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007954 case HLoadString::LoadKind::kJitTableAddress: {
7955 CodeGeneratorMIPS::JitPatchInfo* info =
7956 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7957 load->GetStringIndex(),
7958 load->GetString());
7959 bool reordering = __ SetReorder(false);
7960 __ Bind(&info->high_label);
7961 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007962 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08007963 GenerateGcRootFieldLoad(load,
7964 out_loc,
7965 out,
7966 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007967 kCompilerReadBarrierOption,
7968 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007969 return;
7970 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007971 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007972 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007973 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007974
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007975 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007976 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007977 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007978 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007979 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007980 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7981 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007982}
7983
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007984void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7985 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7986 locations->SetOut(Location::ConstantLocation(constant));
7987}
7988
7989void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7990 // Will be generated at use site.
7991}
7992
7993void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7994 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007995 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007996 InvokeRuntimeCallingConvention calling_convention;
7997 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7998}
7999
8000void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8001 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008002 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008003 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8004 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008005 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008006 }
8007 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8008}
8009
8010void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8011 LocationSummary* locations =
8012 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
8013 switch (mul->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008014 case DataType::Type::kInt32:
8015 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008016 locations->SetInAt(0, Location::RequiresRegister());
8017 locations->SetInAt(1, Location::RequiresRegister());
8018 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8019 break;
8020
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008021 case DataType::Type::kFloat32:
8022 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008023 locations->SetInAt(0, Location::RequiresFpuRegister());
8024 locations->SetInAt(1, Location::RequiresFpuRegister());
8025 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8026 break;
8027
8028 default:
8029 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8030 }
8031}
8032
8033void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008034 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008035 LocationSummary* locations = instruction->GetLocations();
8036 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8037
8038 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008039 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008040 Register dst = locations->Out().AsRegister<Register>();
8041 Register lhs = locations->InAt(0).AsRegister<Register>();
8042 Register rhs = locations->InAt(1).AsRegister<Register>();
8043
8044 if (isR6) {
8045 __ MulR6(dst, lhs, rhs);
8046 } else {
8047 __ MulR2(dst, lhs, rhs);
8048 }
8049 break;
8050 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008051 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008052 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8053 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8054 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8055 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8056 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8057 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8058
8059 // Extra checks to protect caused by the existance of A1_A2.
8060 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8061 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8062 DCHECK_NE(dst_high, lhs_low);
8063 DCHECK_NE(dst_high, rhs_low);
8064
8065 // A_B * C_D
8066 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8067 // dst_lo: [ low(B*D) ]
8068 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8069
8070 if (isR6) {
8071 __ MulR6(TMP, lhs_high, rhs_low);
8072 __ MulR6(dst_high, lhs_low, rhs_high);
8073 __ Addu(dst_high, dst_high, TMP);
8074 __ MuhuR6(TMP, lhs_low, rhs_low);
8075 __ Addu(dst_high, dst_high, TMP);
8076 __ MulR6(dst_low, lhs_low, rhs_low);
8077 } else {
8078 __ MulR2(TMP, lhs_high, rhs_low);
8079 __ MulR2(dst_high, lhs_low, rhs_high);
8080 __ Addu(dst_high, dst_high, TMP);
8081 __ MultuR2(lhs_low, rhs_low);
8082 __ Mfhi(TMP);
8083 __ Addu(dst_high, dst_high, TMP);
8084 __ Mflo(dst_low);
8085 }
8086 break;
8087 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008088 case DataType::Type::kFloat32:
8089 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008090 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8091 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8092 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008093 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008094 __ MulS(dst, lhs, rhs);
8095 } else {
8096 __ MulD(dst, lhs, rhs);
8097 }
8098 break;
8099 }
8100 default:
8101 LOG(FATAL) << "Unexpected mul type " << type;
8102 }
8103}
8104
8105void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8106 LocationSummary* locations =
8107 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
8108 switch (neg->GetResultType()) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008109 case DataType::Type::kInt32:
8110 case DataType::Type::kInt64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008111 locations->SetInAt(0, Location::RequiresRegister());
8112 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8113 break;
8114
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008115 case DataType::Type::kFloat32:
8116 case DataType::Type::kFloat64:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008117 locations->SetInAt(0, Location::RequiresFpuRegister());
8118 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8119 break;
8120
8121 default:
8122 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8123 }
8124}
8125
8126void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008127 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008128 LocationSummary* locations = instruction->GetLocations();
8129
8130 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008131 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008132 Register dst = locations->Out().AsRegister<Register>();
8133 Register src = locations->InAt(0).AsRegister<Register>();
8134 __ Subu(dst, ZERO, src);
8135 break;
8136 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008137 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008138 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8139 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8140 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8141 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8142 __ Subu(dst_low, ZERO, src_low);
8143 __ Sltu(TMP, ZERO, dst_low);
8144 __ Subu(dst_high, ZERO, src_high);
8145 __ Subu(dst_high, dst_high, TMP);
8146 break;
8147 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008148 case DataType::Type::kFloat32:
8149 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008150 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8151 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008152 if (type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008153 __ NegS(dst, src);
8154 } else {
8155 __ NegD(dst, src);
8156 }
8157 break;
8158 }
8159 default:
8160 LOG(FATAL) << "Unexpected neg type " << type;
8161 }
8162}
8163
8164void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
8165 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008166 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008167 InvokeRuntimeCallingConvention calling_convention;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008168 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008169 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8170 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008171}
8172
8173void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008174 // Note: if heap poisoning is enabled, the entry point takes care
8175 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008176 QuickEntrypointEnum entrypoint =
8177 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8178 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008179 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008180 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008181}
8182
8183void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
8184 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008185 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008186 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008187 if (instruction->IsStringAlloc()) {
8188 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8189 } else {
8190 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008191 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008192 locations->SetOut(calling_convention.GetReturnLocation(DataType::Type::kReference));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008193}
8194
8195void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008196 // Note: if heap poisoning is enabled, the entry point takes care
8197 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008198 if (instruction->IsStringAlloc()) {
8199 // String is allocated through StringFactory. Call NewEmptyString entry point.
8200 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008201 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008202 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8203 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8204 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008205 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008206 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8207 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008208 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008209 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008210 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008211}
8212
8213void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
8214 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8215 locations->SetInAt(0, Location::RequiresRegister());
8216 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8217}
8218
8219void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008220 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008221 LocationSummary* locations = instruction->GetLocations();
8222
8223 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008224 case DataType::Type::kInt32: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008225 Register dst = locations->Out().AsRegister<Register>();
8226 Register src = locations->InAt(0).AsRegister<Register>();
8227 __ Nor(dst, src, ZERO);
8228 break;
8229 }
8230
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008231 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008232 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8233 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8234 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8235 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8236 __ Nor(dst_high, src_high, ZERO);
8237 __ Nor(dst_low, src_low, ZERO);
8238 break;
8239 }
8240
8241 default:
8242 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8243 }
8244}
8245
8246void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8247 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8248 locations->SetInAt(0, Location::RequiresRegister());
8249 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8250}
8251
8252void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8253 LocationSummary* locations = instruction->GetLocations();
8254 __ Xori(locations->Out().AsRegister<Register>(),
8255 locations->InAt(0).AsRegister<Register>(),
8256 1);
8257}
8258
8259void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008260 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8261 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008262}
8263
Calin Juravle2ae48182016-03-16 14:05:09 +00008264void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8265 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008266 return;
8267 }
8268 Location obj = instruction->GetLocations()->InAt(0);
8269
8270 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008271 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008272}
8273
Calin Juravle2ae48182016-03-16 14:05:09 +00008274void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008275 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008276 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008277
8278 Location obj = instruction->GetLocations()->InAt(0);
8279
8280 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8281}
8282
8283void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008284 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008285}
8286
8287void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8288 HandleBinaryOp(instruction);
8289}
8290
8291void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8292 HandleBinaryOp(instruction);
8293}
8294
8295void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8296 LOG(FATAL) << "Unreachable";
8297}
8298
8299void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
8300 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8301}
8302
8303void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
8304 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8305 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8306 if (location.IsStackSlot()) {
8307 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8308 } else if (location.IsDoubleStackSlot()) {
8309 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8310 }
8311 locations->SetOut(location);
8312}
8313
8314void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8315 ATTRIBUTE_UNUSED) {
8316 // Nothing to do, the parameter is already at its location.
8317}
8318
8319void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8320 LocationSummary* locations =
8321 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8322 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8323}
8324
8325void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8326 ATTRIBUTE_UNUSED) {
8327 // Nothing to do, the method is already at its location.
8328}
8329
8330void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
8331 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008332 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008333 locations->SetInAt(i, Location::Any());
8334 }
8335 locations->SetOut(Location::Any());
8336}
8337
8338void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8339 LOG(FATAL) << "Unreachable";
8340}
8341
8342void LocationsBuilderMIPS::VisitRem(HRem* rem) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008343 DataType::Type type = rem->GetResultType();
8344 LocationSummary::CallKind call_kind = (type == DataType::Type::kInt32)
8345 ? LocationSummary::kNoCall
8346 : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008347 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
8348
8349 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008350 case DataType::Type::kInt32:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008351 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008352 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008353 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8354 break;
8355
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008356 case DataType::Type::kInt64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008357 InvokeRuntimeCallingConvention calling_convention;
8358 locations->SetInAt(0, Location::RegisterPairLocation(
8359 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8360 locations->SetInAt(1, Location::RegisterPairLocation(
8361 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8362 locations->SetOut(calling_convention.GetReturnLocation(type));
8363 break;
8364 }
8365
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008366 case DataType::Type::kFloat32:
8367 case DataType::Type::kFloat64: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008368 InvokeRuntimeCallingConvention calling_convention;
8369 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8370 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8371 locations->SetOut(calling_convention.GetReturnLocation(type));
8372 break;
8373 }
8374
8375 default:
8376 LOG(FATAL) << "Unexpected rem type " << type;
8377 }
8378}
8379
8380void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008381 DataType::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008382
8383 switch (type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008384 case DataType::Type::kInt32:
Alexey Frunze7e99e052015-11-24 19:28:01 -08008385 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008386 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008387 case DataType::Type::kInt64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008388 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008389 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8390 break;
8391 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008392 case DataType::Type::kFloat32: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008393 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008394 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008395 break;
8396 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008397 case DataType::Type::kFloat64: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008398 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008399 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008400 break;
8401 }
8402 default:
8403 LOG(FATAL) << "Unexpected rem type " << type;
8404 }
8405}
8406
Igor Murashkind01745e2017-04-05 16:40:31 -07008407void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8408 constructor_fence->SetLocations(nullptr);
8409}
8410
8411void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8412 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8413 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8414}
8415
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008416void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8417 memory_barrier->SetLocations(nullptr);
8418}
8419
8420void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8421 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8422}
8423
8424void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8425 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008426 DataType::Type return_type = ret->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008427 locations->SetInAt(0, MipsReturnLocation(return_type));
8428}
8429
8430void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8431 codegen_->GenerateFrameExit();
8432}
8433
8434void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8435 ret->SetLocations(nullptr);
8436}
8437
8438void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8439 codegen_->GenerateFrameExit();
8440}
8441
Alexey Frunze92d90602015-12-18 18:16:36 -08008442void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8443 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008444}
8445
Alexey Frunze92d90602015-12-18 18:16:36 -08008446void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8447 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008448}
8449
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008450void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8451 HandleShift(shl);
8452}
8453
8454void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8455 HandleShift(shl);
8456}
8457
8458void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8459 HandleShift(shr);
8460}
8461
8462void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8463 HandleShift(shr);
8464}
8465
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008466void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8467 HandleBinaryOp(instruction);
8468}
8469
8470void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8471 HandleBinaryOp(instruction);
8472}
8473
8474void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8475 HandleFieldGet(instruction, instruction->GetFieldInfo());
8476}
8477
8478void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8479 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8480}
8481
8482void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8483 HandleFieldSet(instruction, instruction->GetFieldInfo());
8484}
8485
8486void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008487 HandleFieldSet(instruction,
8488 instruction->GetFieldInfo(),
8489 instruction->GetDexPc(),
8490 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008491}
8492
8493void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8494 HUnresolvedInstanceFieldGet* instruction) {
8495 FieldAccessCallingConventionMIPS calling_convention;
8496 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8497 instruction->GetFieldType(),
8498 calling_convention);
8499}
8500
8501void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8502 HUnresolvedInstanceFieldGet* instruction) {
8503 FieldAccessCallingConventionMIPS calling_convention;
8504 codegen_->GenerateUnresolvedFieldAccess(instruction,
8505 instruction->GetFieldType(),
8506 instruction->GetFieldIndex(),
8507 instruction->GetDexPc(),
8508 calling_convention);
8509}
8510
8511void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8512 HUnresolvedInstanceFieldSet* instruction) {
8513 FieldAccessCallingConventionMIPS calling_convention;
8514 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8515 instruction->GetFieldType(),
8516 calling_convention);
8517}
8518
8519void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8520 HUnresolvedInstanceFieldSet* instruction) {
8521 FieldAccessCallingConventionMIPS calling_convention;
8522 codegen_->GenerateUnresolvedFieldAccess(instruction,
8523 instruction->GetFieldType(),
8524 instruction->GetFieldIndex(),
8525 instruction->GetDexPc(),
8526 calling_convention);
8527}
8528
8529void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8530 HUnresolvedStaticFieldGet* instruction) {
8531 FieldAccessCallingConventionMIPS calling_convention;
8532 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8533 instruction->GetFieldType(),
8534 calling_convention);
8535}
8536
8537void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8538 HUnresolvedStaticFieldGet* instruction) {
8539 FieldAccessCallingConventionMIPS calling_convention;
8540 codegen_->GenerateUnresolvedFieldAccess(instruction,
8541 instruction->GetFieldType(),
8542 instruction->GetFieldIndex(),
8543 instruction->GetDexPc(),
8544 calling_convention);
8545}
8546
8547void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8548 HUnresolvedStaticFieldSet* instruction) {
8549 FieldAccessCallingConventionMIPS calling_convention;
8550 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8551 instruction->GetFieldType(),
8552 calling_convention);
8553}
8554
8555void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8556 HUnresolvedStaticFieldSet* instruction) {
8557 FieldAccessCallingConventionMIPS calling_convention;
8558 codegen_->GenerateUnresolvedFieldAccess(instruction,
8559 instruction->GetFieldType(),
8560 instruction->GetFieldIndex(),
8561 instruction->GetDexPc(),
8562 calling_convention);
8563}
8564
8565void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008566 LocationSummary* locations =
8567 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008568 // In suspend check slow path, usually there are no caller-save registers at all.
8569 // If SIMD instructions are present, however, we force spilling all live SIMD
8570 // registers in full width (since the runtime only saves/restores lower part).
8571 locations->SetCustomSlowPathCallerSaves(
8572 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008573}
8574
8575void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8576 HBasicBlock* block = instruction->GetBlock();
8577 if (block->GetLoopInformation() != nullptr) {
8578 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8579 // The back edge will generate the suspend check.
8580 return;
8581 }
8582 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8583 // The goto will generate the suspend check.
8584 return;
8585 }
8586 GenerateSuspendCheck(instruction, nullptr);
8587}
8588
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008589void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8590 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008591 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008592 InvokeRuntimeCallingConvention calling_convention;
8593 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8594}
8595
8596void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008597 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008598 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8599}
8600
8601void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008602 DataType::Type input_type = conversion->GetInputType();
8603 DataType::Type result_type = conversion->GetResultType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008604 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008605 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008606
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008607 if ((input_type == DataType::Type::kReference) || (input_type == DataType::Type::kVoid) ||
8608 (result_type == DataType::Type::kReference) || (result_type == DataType::Type::kVoid)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008609 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8610 }
8611
8612 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008613 if (!isR6 &&
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008614 ((DataType::IsFloatingPointType(result_type) && input_type == DataType::Type::kInt64) ||
8615 (result_type == DataType::Type::kInt64 && DataType::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008616 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008617 }
8618
8619 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8620
8621 if (call_kind == LocationSummary::kNoCall) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008622 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008623 locations->SetInAt(0, Location::RequiresFpuRegister());
8624 } else {
8625 locations->SetInAt(0, Location::RequiresRegister());
8626 }
8627
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008628 if (DataType::IsFloatingPointType(result_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008629 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8630 } else {
8631 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8632 }
8633 } else {
8634 InvokeRuntimeCallingConvention calling_convention;
8635
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008636 if (DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008637 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8638 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008639 DCHECK_EQ(input_type, DataType::Type::kInt64);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008640 locations->SetInAt(0, Location::RegisterPairLocation(
8641 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8642 }
8643
8644 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8645 }
8646}
8647
8648void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8649 LocationSummary* locations = conversion->GetLocations();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008650 DataType::Type result_type = conversion->GetResultType();
8651 DataType::Type input_type = conversion->GetInputType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008652 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008653 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008654
8655 DCHECK_NE(input_type, result_type);
8656
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008657 if (result_type == DataType::Type::kInt64 && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008658 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8659 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8660 Register src = locations->InAt(0).AsRegister<Register>();
8661
Alexey Frunzea871ef12016-06-27 15:20:11 -07008662 if (dst_low != src) {
8663 __ Move(dst_low, src);
8664 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008665 __ Sra(dst_high, src, 31);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008666 } else if (DataType::IsIntegralType(result_type) && DataType::IsIntegralType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008667 Register dst = locations->Out().AsRegister<Register>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008668 Register src = (input_type == DataType::Type::kInt64)
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008669 ? locations->InAt(0).AsRegisterPairLow<Register>()
8670 : locations->InAt(0).AsRegister<Register>();
8671
8672 switch (result_type) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008673 case DataType::Type::kUint16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008674 __ Andi(dst, src, 0xFFFF);
8675 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008676 case DataType::Type::kInt8:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008677 if (has_sign_extension) {
8678 __ Seb(dst, src);
8679 } else {
8680 __ Sll(dst, src, 24);
8681 __ Sra(dst, dst, 24);
8682 }
8683 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008684 case DataType::Type::kInt16:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008685 if (has_sign_extension) {
8686 __ Seh(dst, src);
8687 } else {
8688 __ Sll(dst, src, 16);
8689 __ Sra(dst, dst, 16);
8690 }
8691 break;
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008692 case DataType::Type::kInt32:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008693 if (dst != src) {
8694 __ Move(dst, src);
8695 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008696 break;
8697
8698 default:
8699 LOG(FATAL) << "Unexpected type conversion from " << input_type
8700 << " to " << result_type;
8701 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008702 } else if (DataType::IsFloatingPointType(result_type) && DataType::IsIntegralType(input_type)) {
8703 if (input_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008704 if (isR6) {
8705 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8706 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8707 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8708 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8709 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8710 __ Mtc1(src_low, FTMP);
8711 __ Mthc1(src_high, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008712 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008713 __ Cvtsl(dst, FTMP);
8714 } else {
8715 __ Cvtdl(dst, FTMP);
8716 }
8717 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008718 QuickEntrypointEnum entrypoint =
8719 (result_type == DataType::Type::kFloat32) ? kQuickL2f : kQuickL2d;
Serban Constantinescufca16662016-07-14 09:21:59 +01008720 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008721 if (result_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008722 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8723 } else {
8724 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8725 }
8726 }
8727 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008728 Register src = locations->InAt(0).AsRegister<Register>();
8729 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8730 __ Mtc1(src, FTMP);
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008731 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008732 __ Cvtsw(dst, FTMP);
8733 } else {
8734 __ Cvtdw(dst, FTMP);
8735 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008736 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008737 } else if (DataType::IsIntegralType(result_type) && DataType::IsFloatingPointType(input_type)) {
8738 CHECK(result_type == DataType::Type::kInt32 || result_type == DataType::Type::kInt64);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008739
8740 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8741 // value of the output type if the input is outside of the range after the truncation or
8742 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8743 // results. This matches the desired float/double-to-int/long conversion exactly.
8744 //
8745 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8746 // value when the input is either a NaN or is outside of the range of the output type
8747 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8748 // the same result.
8749 //
8750 // The code takes care of the different behaviors by first comparing the input to the
8751 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8752 // If the input is greater than or equal to the minimum, it procedes to the truncate
8753 // instruction, which will handle such an input the same way irrespective of NAN2008.
8754 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8755 // in order to return either zero or the minimum value.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008756 if (result_type == DataType::Type::kInt64) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008757 if (isR6) {
8758 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8759 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8760 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8761 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8762 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008763
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008764 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008765 __ TruncLS(FTMP, src);
8766 } else {
8767 __ TruncLD(FTMP, src);
8768 }
8769 __ Mfc1(dst_low, FTMP);
8770 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008771 } else {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008772 QuickEntrypointEnum entrypoint =
8773 (input_type == DataType::Type::kFloat32) ? kQuickF2l : kQuickD2l;
Serban Constantinescufca16662016-07-14 09:21:59 +01008774 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008775 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008776 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8777 } else {
8778 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8779 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008780 }
8781 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008782 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8783 Register dst = locations->Out().AsRegister<Register>();
8784 MipsLabel truncate;
8785 MipsLabel done;
8786
Lena Djokicf4e23a82017-05-09 15:43:45 +02008787 if (!isR6) {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008788 if (input_type == DataType::Type::kFloat32) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008789 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8790 __ LoadConst32(TMP, min_val);
8791 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008792 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008793 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8794 __ LoadConst32(TMP, High32Bits(min_val));
8795 __ Mtc1(ZERO, FTMP);
8796 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008797 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008798
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008799 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008800 __ ColeS(0, FTMP, src);
8801 } else {
8802 __ ColeD(0, FTMP, src);
8803 }
8804 __ Bc1t(0, &truncate);
8805
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008806 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008807 __ CeqS(0, src, src);
8808 } else {
8809 __ CeqD(0, src, src);
8810 }
8811 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8812 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008813
8814 __ B(&done);
8815
8816 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008817 }
8818
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008819 if (input_type == DataType::Type::kFloat32) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008820 __ TruncWS(FTMP, src);
8821 } else {
8822 __ TruncWD(FTMP, src);
8823 }
8824 __ Mfc1(dst, FTMP);
8825
Lena Djokicf4e23a82017-05-09 15:43:45 +02008826 if (!isR6) {
8827 __ Bind(&done);
8828 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008829 }
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008830 } else if (DataType::IsFloatingPointType(result_type) &&
8831 DataType::IsFloatingPointType(input_type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008832 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8833 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01008834 if (result_type == DataType::Type::kFloat32) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008835 __ Cvtsd(dst, src);
8836 } else {
8837 __ Cvtds(dst, src);
8838 }
8839 } else {
8840 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8841 << " to " << result_type;
8842 }
8843}
8844
8845void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8846 HandleShift(ushr);
8847}
8848
8849void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8850 HandleShift(ushr);
8851}
8852
8853void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8854 HandleBinaryOp(instruction);
8855}
8856
8857void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8858 HandleBinaryOp(instruction);
8859}
8860
8861void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8862 // Nothing to do, this should be removed during prepare for register allocator.
8863 LOG(FATAL) << "Unreachable";
8864}
8865
8866void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8867 // Nothing to do, this should be removed during prepare for register allocator.
8868 LOG(FATAL) << "Unreachable";
8869}
8870
8871void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008872 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008873}
8874
8875void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008876 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008877}
8878
8879void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008880 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008881}
8882
8883void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008884 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008885}
8886
8887void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008888 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008889}
8890
8891void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008892 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008893}
8894
8895void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008896 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008897}
8898
8899void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008900 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008901}
8902
8903void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008904 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008905}
8906
8907void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008908 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008909}
8910
8911void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008912 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008913}
8914
8915void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008916 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008917}
8918
8919void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008920 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008921}
8922
8923void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008924 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008925}
8926
8927void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008928 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008929}
8930
8931void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008932 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008933}
8934
8935void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008936 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008937}
8938
8939void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008940 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008941}
8942
8943void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008944 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008945}
8946
8947void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008948 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008949}
8950
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008951void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8952 LocationSummary* locations =
8953 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8954 locations->SetInAt(0, Location::RequiresRegister());
8955}
8956
Alexey Frunze96b66822016-09-10 02:32:44 -07008957void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8958 int32_t lower_bound,
8959 uint32_t num_entries,
8960 HBasicBlock* switch_block,
8961 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008962 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008963 Register temp_reg = TMP;
8964 __ Addiu32(temp_reg, value_reg, -lower_bound);
8965 // Jump to default if index is negative
8966 // Note: We don't check the case that index is positive while value < lower_bound, because in
8967 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8968 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8969
Alexey Frunze96b66822016-09-10 02:32:44 -07008970 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008971 // Jump to successors[0] if value == lower_bound.
8972 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8973 int32_t last_index = 0;
8974 for (; num_entries - last_index > 2; last_index += 2) {
8975 __ Addiu(temp_reg, temp_reg, -2);
8976 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8977 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8978 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8979 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8980 }
8981 if (num_entries - last_index == 2) {
8982 // The last missing case_value.
8983 __ Addiu(temp_reg, temp_reg, -1);
8984 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008985 }
8986
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008987 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008988 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008989 __ B(codegen_->GetLabelOf(default_block));
8990 }
8991}
8992
Alexey Frunze96b66822016-09-10 02:32:44 -07008993void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8994 Register constant_area,
8995 int32_t lower_bound,
8996 uint32_t num_entries,
8997 HBasicBlock* switch_block,
8998 HBasicBlock* default_block) {
8999 // Create a jump table.
9000 std::vector<MipsLabel*> labels(num_entries);
9001 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9002 for (uint32_t i = 0; i < num_entries; i++) {
9003 labels[i] = codegen_->GetLabelOf(successors[i]);
9004 }
9005 JumpTable* table = __ CreateJumpTable(std::move(labels));
9006
9007 // Is the value in range?
9008 __ Addiu32(TMP, value_reg, -lower_bound);
9009 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9010 __ Sltiu(AT, TMP, num_entries);
9011 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9012 } else {
9013 __ LoadConst32(AT, num_entries);
9014 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9015 }
9016
9017 // We are in the range of the table.
9018 // Load the target address from the jump table, indexing by the value.
9019 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009020 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009021 __ Lw(TMP, TMP, 0);
9022 // Compute the absolute target address by adding the table start address
9023 // (the table contains offsets to targets relative to its start).
9024 __ Addu(TMP, TMP, AT);
9025 // And jump.
9026 __ Jr(TMP);
9027 __ NopIfNoReordering();
9028}
9029
9030void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9031 int32_t lower_bound = switch_instr->GetStartValue();
9032 uint32_t num_entries = switch_instr->GetNumEntries();
9033 LocationSummary* locations = switch_instr->GetLocations();
9034 Register value_reg = locations->InAt(0).AsRegister<Register>();
9035 HBasicBlock* switch_block = switch_instr->GetBlock();
9036 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9037
9038 if (codegen_->GetInstructionSetFeatures().IsR6() &&
9039 num_entries > kPackedSwitchJumpTableThreshold) {
9040 // R6 uses PC-relative addressing to access the jump table.
9041 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
9042 // the jump table and it is implemented by changing HPackedSwitch to
9043 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
9044 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
9045 GenTableBasedPackedSwitch(value_reg,
9046 ZERO,
9047 lower_bound,
9048 num_entries,
9049 switch_block,
9050 default_block);
9051 } else {
9052 GenPackedSwitchWithCompares(value_reg,
9053 lower_bound,
9054 num_entries,
9055 switch_block,
9056 default_block);
9057 }
9058}
9059
9060void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9061 LocationSummary* locations =
9062 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9063 locations->SetInAt(0, Location::RequiresRegister());
9064 // Constant area pointer (HMipsComputeBaseMethodAddress).
9065 locations->SetInAt(1, Location::RequiresRegister());
9066}
9067
9068void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9069 int32_t lower_bound = switch_instr->GetStartValue();
9070 uint32_t num_entries = switch_instr->GetNumEntries();
9071 LocationSummary* locations = switch_instr->GetLocations();
9072 Register value_reg = locations->InAt(0).AsRegister<Register>();
9073 Register constant_area = locations->InAt(1).AsRegister<Register>();
9074 HBasicBlock* switch_block = switch_instr->GetBlock();
9075 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9076
9077 // This is an R2-only path. HPackedSwitch has been changed to
9078 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9079 // required to address the jump table relative to PC.
9080 GenTableBasedPackedSwitch(value_reg,
9081 constant_area,
9082 lower_bound,
9083 num_entries,
9084 switch_block,
9085 default_block);
9086}
9087
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009088void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9089 HMipsComputeBaseMethodAddress* insn) {
9090 LocationSummary* locations =
9091 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
9092 locations->SetOut(Location::RequiresRegister());
9093}
9094
9095void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9096 HMipsComputeBaseMethodAddress* insn) {
9097 LocationSummary* locations = insn->GetLocations();
9098 Register reg = locations->Out().AsRegister<Register>();
9099
9100 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9101
9102 // Generate a dummy PC-relative call to obtain PC.
9103 __ Nal();
9104 // Grab the return address off RA.
9105 __ Move(reg, RA);
9106
9107 // Remember this offset (the obtained PC value) for later use with constant area.
9108 __ BindPcRelBaseLabel();
9109}
9110
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009111void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9112 // The trampoline uses the same calling convention as dex calling conventions,
9113 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9114 // the method_idx.
9115 HandleInvoke(invoke);
9116}
9117
9118void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9119 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9120}
9121
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009122void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9123 LocationSummary* locations =
9124 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9125 locations->SetInAt(0, Location::RequiresRegister());
9126 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009127}
9128
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009129void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9130 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009131 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009132 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009133 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009134 __ LoadFromOffset(kLoadWord,
9135 locations->Out().AsRegister<Register>(),
9136 locations->InAt(0).AsRegister<Register>(),
9137 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009138 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009139 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009140 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009141 __ LoadFromOffset(kLoadWord,
9142 locations->Out().AsRegister<Register>(),
9143 locations->InAt(0).AsRegister<Register>(),
9144 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009145 __ LoadFromOffset(kLoadWord,
9146 locations->Out().AsRegister<Register>(),
9147 locations->Out().AsRegister<Register>(),
9148 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009149 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009150}
9151
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009152#undef __
9153#undef QUICK_ENTRY_POINT
9154
9155} // namespace mips
9156} // namespace art