blob: 6256722661f403229d057d057dfade91bff5d627 [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"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020032#include "mirror/array-inl.h"
33#include "mirror/class-inl.h"
34#include "offsets.h"
35#include "thread.h"
36#include "utils/assembler.h"
37#include "utils/mips/assembler_mips.h"
38#include "utils/stack_checks.h"
39
40namespace art {
41namespace mips {
42
43static constexpr int kCurrentMethodStackOffset = 0;
44static constexpr Register kMethodRegisterArgument = A0;
45
Alexey Frunze4147fcc2017-06-17 19:57:27 -070046// Flags controlling the use of thunks for Baker read barriers.
47constexpr bool kBakerReadBarrierThunksEnableForFields = true;
48constexpr bool kBakerReadBarrierThunksEnableForArrays = true;
49constexpr bool kBakerReadBarrierThunksEnableForGcRoots = true;
50
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020051Location MipsReturnLocation(Primitive::Type return_type) {
52 switch (return_type) {
53 case Primitive::kPrimBoolean:
54 case Primitive::kPrimByte:
55 case Primitive::kPrimChar:
56 case Primitive::kPrimShort:
57 case Primitive::kPrimInt:
58 case Primitive::kPrimNot:
59 return Location::RegisterLocation(V0);
60
61 case Primitive::kPrimLong:
62 return Location::RegisterPairLocation(V0, V1);
63
64 case Primitive::kPrimFloat:
65 case Primitive::kPrimDouble:
66 return Location::FpuRegisterLocation(F0);
67
68 case Primitive::kPrimVoid:
69 return Location();
70 }
71 UNREACHABLE();
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
75 return MipsReturnLocation(type);
76}
77
78Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
79 return Location::RegisterLocation(kMethodRegisterArgument);
80}
81
82Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
83 Location next_location;
84
85 switch (type) {
86 case Primitive::kPrimBoolean:
87 case Primitive::kPrimByte:
88 case Primitive::kPrimChar:
89 case Primitive::kPrimShort:
90 case Primitive::kPrimInt:
91 case Primitive::kPrimNot: {
92 uint32_t gp_index = gp_index_++;
93 if (gp_index < calling_convention.GetNumberOfRegisters()) {
94 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
95 } else {
96 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
97 next_location = Location::StackSlot(stack_offset);
98 }
99 break;
100 }
101
102 case Primitive::kPrimLong: {
103 uint32_t gp_index = gp_index_;
104 gp_index_ += 2;
105 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -0800106 Register reg = calling_convention.GetRegisterAt(gp_index);
107 if (reg == A1 || reg == A3) {
108 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200109 gp_index++;
110 }
111 Register low_even = calling_convention.GetRegisterAt(gp_index);
112 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
113 DCHECK_EQ(low_even + 1, high_odd);
114 next_location = Location::RegisterPairLocation(low_even, high_odd);
115 } else {
116 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
117 next_location = Location::DoubleStackSlot(stack_offset);
118 }
119 break;
120 }
121
122 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
123 // will take up the even/odd pair, while floats are stored in even regs only.
124 // On 64 bit FPU, both double and float are stored in even registers only.
125 case Primitive::kPrimFloat:
126 case Primitive::kPrimDouble: {
127 uint32_t float_index = float_index_++;
128 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
129 next_location = Location::FpuRegisterLocation(
130 calling_convention.GetFpuRegisterAt(float_index));
131 } else {
132 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
133 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
134 : Location::StackSlot(stack_offset);
135 }
136 break;
137 }
138
139 case Primitive::kPrimVoid:
140 LOG(FATAL) << "Unexpected parameter type " << type;
141 break;
142 }
143
144 // Space on the stack is reserved for all arguments.
145 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
146
147 return next_location;
148}
149
150Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
151 return MipsReturnLocation(type);
152}
153
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100154// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
155#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700156#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200157
158class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
159 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000160 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200161
162 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
163 LocationSummary* locations = instruction_->GetLocations();
164 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
165 __ Bind(GetEntryLabel());
166 if (instruction_->CanThrowIntoCatchBlock()) {
167 // Live registers will be restored in the catch block if caught.
168 SaveLiveRegisters(codegen, instruction_->GetLocations());
169 }
170 // We're moving two locations to locations that could overlap, so we need a parallel
171 // move resolver.
172 InvokeRuntimeCallingConvention calling_convention;
173 codegen->EmitParallelMoves(locations->InAt(0),
174 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
175 Primitive::kPrimInt,
176 locations->InAt(1),
177 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
178 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100179 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
180 ? kQuickThrowStringBounds
181 : kQuickThrowArrayBounds;
182 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100183 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200184 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
185 }
186
187 bool IsFatal() const OVERRIDE { return true; }
188
189 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
190
191 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200192 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
193};
194
195class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
196 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000197 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200198
199 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
200 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
201 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100202 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200203 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
204 }
205
206 bool IsFatal() const OVERRIDE { return true; }
207
208 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
209
210 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200211 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
212};
213
214class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
215 public:
216 LoadClassSlowPathMIPS(HLoadClass* cls,
217 HInstruction* at,
218 uint32_t dex_pc,
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700219 bool do_clinit,
220 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr)
221 : SlowPathCodeMIPS(at),
222 cls_(cls),
223 dex_pc_(dex_pc),
224 do_clinit_(do_clinit),
225 bss_info_high_(bss_info_high) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200226 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
227 }
228
229 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000230 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700231 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200232 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700233 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700234 InvokeRuntimeCallingConvention calling_convention;
235 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
236 const bool is_load_class_bss_entry =
237 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200238 __ Bind(GetEntryLabel());
239 SaveLiveRegisters(codegen, locations);
240
Alexey Frunzec61c0762017-04-10 13:54:23 -0700241 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
242 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700243 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700244 Register temp = locations->GetTemp(0).AsRegister<Register>();
245 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
246 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
247 // kSaveEverything call.
248 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
249 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
250 if (temp_is_a0) {
251 __ Move(entry_address, temp);
252 }
253 }
254
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000255 dex::TypeIndex type_index = cls_->GetTypeIndex();
256 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100257 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
258 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000259 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200260 if (do_clinit_) {
261 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
262 } else {
263 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
264 }
265
Alexey Frunzec61c0762017-04-10 13:54:23 -0700266 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700267 if (is_load_class_bss_entry && baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700268 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700269 DCHECK(bss_info_high_);
270 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
271 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700272 __ Sw(calling_convention.GetRegisterAt(0),
273 entry_address,
274 /* placeholder */ 0x5678,
275 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700276 }
277
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200278 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200279 if (out.IsValid()) {
280 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000281 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700282 mips_codegen->MoveLocation(out,
283 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
284 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200285 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200286 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700287
288 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700289 if (is_load_class_bss_entry && !baker_or_no_read_barriers) {
290 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700291 // the class entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700292 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000293 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700294 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000295 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700296 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
297 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700298 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
299 __ Sw(out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000300 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200301 __ B(GetExitLabel());
302 }
303
304 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
305
306 private:
307 // The class this slow path will load.
308 HLoadClass* const cls_;
309
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200310 // The dex PC of `at_`.
311 const uint32_t dex_pc_;
312
313 // Whether to initialize the class.
314 const bool do_clinit_;
315
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700316 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
317 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
318
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200319 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
320};
321
322class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
323 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700324 explicit LoadStringSlowPathMIPS(HLoadString* instruction,
325 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high)
326 : SlowPathCodeMIPS(instruction), bss_info_high_(bss_info_high) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200327
328 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700329 DCHECK(instruction_->IsLoadString());
330 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200331 LocationSummary* locations = instruction_->GetLocations();
332 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700333 HLoadString* load = instruction_->AsLoadString();
334 const dex::StringIndex string_index = load->GetStringIndex();
335 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200336 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700337 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700338 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200339 __ Bind(GetEntryLabel());
340 SaveLiveRegisters(codegen, locations);
341
Alexey Frunzec61c0762017-04-10 13:54:23 -0700342 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
343 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700344 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700345 Register temp = locations->GetTemp(0).AsRegister<Register>();
346 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
347 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
348 // kSaveEverything call.
349 entry_address = temp_is_a0 ? out : temp;
350 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
351 if (temp_is_a0) {
352 __ Move(entry_address, temp);
353 }
354 }
355
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000356 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100357 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200358 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700359
360 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700361 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700362 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700363 DCHECK(bss_info_high_);
364 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100365 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700366 __ Sw(calling_convention.GetRegisterAt(0),
367 entry_address,
368 /* placeholder */ 0x5678,
369 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700370 }
371
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200372 Primitive::Type type = instruction_->GetType();
373 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700374 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200375 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200376 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000377
Alexey Frunzec61c0762017-04-10 13:54:23 -0700378 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700379 if (!baker_or_no_read_barriers) {
380 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700381 // the string entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700382 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700383 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700384 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100385 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700386 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100387 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700388 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
389 __ Sw(out, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700390 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200391 __ B(GetExitLabel());
392 }
393
394 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
395
396 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700397 // Pointer to the high half PC-relative patch info.
398 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
399
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200400 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
401};
402
403class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
404 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000405 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200406
407 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
408 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
409 __ Bind(GetEntryLabel());
410 if (instruction_->CanThrowIntoCatchBlock()) {
411 // Live registers will be restored in the catch block if caught.
412 SaveLiveRegisters(codegen, instruction_->GetLocations());
413 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100414 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200415 instruction_,
416 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100417 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200418 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
419 }
420
421 bool IsFatal() const OVERRIDE { return true; }
422
423 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
424
425 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200426 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
427};
428
429class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
430 public:
431 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000432 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200433
434 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200435 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200436 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
437 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200438 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100439 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200441 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200442 if (successor_ == nullptr) {
443 __ B(GetReturnLabel());
444 } else {
445 __ B(mips_codegen->GetLabelOf(successor_));
446 }
447 }
448
449 MipsLabel* GetReturnLabel() {
450 DCHECK(successor_ == nullptr);
451 return &return_label_;
452 }
453
454 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
455
456 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200457 // If not null, the block to branch to after the suspend check.
458 HBasicBlock* const successor_;
459
460 // If `successor_` is null, the label to branch to after the suspend check.
461 MipsLabel return_label_;
462
463 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
464};
465
466class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
467 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800468 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
469 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200470
471 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
472 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200473 uint32_t dex_pc = instruction_->GetDexPc();
474 DCHECK(instruction_->IsCheckCast()
475 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
476 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
477
478 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800479 if (!is_fatal_) {
480 SaveLiveRegisters(codegen, locations);
481 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200482
483 // We're moving two locations to locations that could overlap, so we need a parallel
484 // move resolver.
485 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800486 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200487 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
488 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800489 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200490 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
491 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200492 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100493 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800494 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200495 Primitive::Type ret_type = instruction_->GetType();
496 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
497 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200498 } else {
499 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800500 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
501 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200502 }
503
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800504 if (!is_fatal_) {
505 RestoreLiveRegisters(codegen, locations);
506 __ B(GetExitLabel());
507 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200508 }
509
510 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
511
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800512 bool IsFatal() const OVERRIDE { return is_fatal_; }
513
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200514 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800515 const bool is_fatal_;
516
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200517 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
518};
519
520class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
521 public:
Aart Bik42249c32016-01-07 15:33:50 -0800522 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000523 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200524
525 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800526 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200527 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100528 LocationSummary* locations = instruction_->GetLocations();
529 SaveLiveRegisters(codegen, locations);
530 InvokeRuntimeCallingConvention calling_convention;
531 __ LoadConst32(calling_convention.GetRegisterAt(0),
532 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100533 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100534 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200535 }
536
537 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
538
539 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200540 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
541};
542
Alexey Frunze15958152017-02-09 19:08:30 -0800543class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
544 public:
545 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
546
547 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
548 LocationSummary* locations = instruction_->GetLocations();
549 __ Bind(GetEntryLabel());
550 SaveLiveRegisters(codegen, locations);
551
552 InvokeRuntimeCallingConvention calling_convention;
553 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
554 parallel_move.AddMove(
555 locations->InAt(0),
556 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
557 Primitive::kPrimNot,
558 nullptr);
559 parallel_move.AddMove(
560 locations->InAt(1),
561 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
562 Primitive::kPrimInt,
563 nullptr);
564 parallel_move.AddMove(
565 locations->InAt(2),
566 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
567 Primitive::kPrimNot,
568 nullptr);
569 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
570
571 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
572 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
573 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
574 RestoreLiveRegisters(codegen, locations);
575 __ B(GetExitLabel());
576 }
577
578 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
579
580 private:
581 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
582};
583
584// Slow path marking an object reference `ref` during a read
585// barrier. The field `obj.field` in the object `obj` holding this
586// reference does not get updated by this slow path after marking (see
587// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
588//
589// This means that after the execution of this slow path, `ref` will
590// always be up-to-date, but `obj.field` may not; i.e., after the
591// flip, `ref` will be a to-space reference, but `obj.field` will
592// probably still be a from-space reference (unless it gets updated by
593// another thread, or if another thread installed another object
594// reference (different from `ref`) in `obj.field`).
595//
596// If `entrypoint` is a valid location it is assumed to already be
597// holding the entrypoint. The case where the entrypoint is passed in
598// is for the GcRoot read barrier.
599class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
600 public:
601 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
602 Location ref,
603 Location entrypoint = Location::NoLocation())
604 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
605 DCHECK(kEmitCompilerReadBarrier);
606 }
607
608 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
609
610 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
611 LocationSummary* locations = instruction_->GetLocations();
612 Register ref_reg = ref_.AsRegister<Register>();
613 DCHECK(locations->CanCall());
614 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
615 DCHECK(instruction_->IsInstanceFieldGet() ||
616 instruction_->IsStaticFieldGet() ||
617 instruction_->IsArrayGet() ||
618 instruction_->IsArraySet() ||
619 instruction_->IsLoadClass() ||
620 instruction_->IsLoadString() ||
621 instruction_->IsInstanceOf() ||
622 instruction_->IsCheckCast() ||
623 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
624 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
625 << "Unexpected instruction in read barrier marking slow path: "
626 << instruction_->DebugName();
627
628 __ Bind(GetEntryLabel());
629 // No need to save live registers; it's taken care of by the
630 // entrypoint. Also, there is no need to update the stack mask,
631 // as this runtime call will not trigger a garbage collection.
632 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
633 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
634 (S2 <= ref_reg && ref_reg <= S7) ||
635 (ref_reg == FP)) << ref_reg;
636 // "Compact" slow path, saving two moves.
637 //
638 // Instead of using the standard runtime calling convention (input
639 // and output in A0 and V0 respectively):
640 //
641 // A0 <- ref
642 // V0 <- ReadBarrierMark(A0)
643 // ref <- V0
644 //
645 // we just use rX (the register containing `ref`) as input and output
646 // of a dedicated entrypoint:
647 //
648 // rX <- ReadBarrierMarkRegX(rX)
649 //
650 if (entrypoint_.IsValid()) {
651 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
652 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
653 __ Jalr(entrypoint_.AsRegister<Register>());
654 __ NopIfNoReordering();
655 } else {
656 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100657 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800658 // This runtime call does not require a stack map.
659 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
660 instruction_,
661 this,
662 /* direct */ false);
663 }
664 __ B(GetExitLabel());
665 }
666
667 private:
668 // The location (register) of the marked object reference.
669 const Location ref_;
670
671 // The location of the entrypoint if already loaded.
672 const Location entrypoint_;
673
674 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
675};
676
677// Slow path marking an object reference `ref` during a read barrier,
678// and if needed, atomically updating the field `obj.field` in the
679// object `obj` holding this reference after marking (contrary to
680// ReadBarrierMarkSlowPathMIPS above, which never tries to update
681// `obj.field`).
682//
683// This means that after the execution of this slow path, both `ref`
684// and `obj.field` will be up-to-date; i.e., after the flip, both will
685// hold the same to-space reference (unless another thread installed
686// another object reference (different from `ref`) in `obj.field`).
687class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
688 public:
689 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
690 Location ref,
691 Register obj,
692 Location field_offset,
693 Register temp1)
694 : SlowPathCodeMIPS(instruction),
695 ref_(ref),
696 obj_(obj),
697 field_offset_(field_offset),
698 temp1_(temp1) {
699 DCHECK(kEmitCompilerReadBarrier);
700 }
701
702 const char* GetDescription() const OVERRIDE {
703 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
704 }
705
706 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
707 LocationSummary* locations = instruction_->GetLocations();
708 Register ref_reg = ref_.AsRegister<Register>();
709 DCHECK(locations->CanCall());
710 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
711 // This slow path is only used by the UnsafeCASObject intrinsic.
712 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
713 << "Unexpected instruction in read barrier marking and field updating slow path: "
714 << instruction_->DebugName();
715 DCHECK(instruction_->GetLocations()->Intrinsified());
716 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
717 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
718
719 __ Bind(GetEntryLabel());
720
721 // Save the old reference.
722 // Note that we cannot use AT or TMP to save the old reference, as those
723 // are used by the code that follows, but we need the old reference after
724 // the call to the ReadBarrierMarkRegX entry point.
725 DCHECK_NE(temp1_, AT);
726 DCHECK_NE(temp1_, TMP);
727 __ Move(temp1_, ref_reg);
728
729 // No need to save live registers; it's taken care of by the
730 // entrypoint. Also, there is no need to update the stack mask,
731 // as this runtime call will not trigger a garbage collection.
732 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
733 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
734 (S2 <= ref_reg && ref_reg <= S7) ||
735 (ref_reg == FP)) << ref_reg;
736 // "Compact" slow path, saving two moves.
737 //
738 // Instead of using the standard runtime calling convention (input
739 // and output in A0 and V0 respectively):
740 //
741 // A0 <- ref
742 // V0 <- ReadBarrierMark(A0)
743 // ref <- V0
744 //
745 // we just use rX (the register containing `ref`) as input and output
746 // of a dedicated entrypoint:
747 //
748 // rX <- ReadBarrierMarkRegX(rX)
749 //
750 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100751 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800752 // This runtime call does not require a stack map.
753 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
754 instruction_,
755 this,
756 /* direct */ false);
757
758 // If the new reference is different from the old reference,
759 // update the field in the holder (`*(obj_ + field_offset_)`).
760 //
761 // Note that this field could also hold a different object, if
762 // another thread had concurrently changed it. In that case, the
763 // the compare-and-set (CAS) loop below would abort, leaving the
764 // field as-is.
765 MipsLabel done;
766 __ Beq(temp1_, ref_reg, &done);
767
768 // Update the the holder's field atomically. This may fail if
769 // mutator updates before us, but it's OK. This is achieved
770 // using a strong compare-and-set (CAS) operation with relaxed
771 // memory synchronization ordering, where the expected value is
772 // the old reference and the desired value is the new reference.
773
774 // Convenience aliases.
775 Register base = obj_;
776 // The UnsafeCASObject intrinsic uses a register pair as field
777 // offset ("long offset"), of which only the low part contains
778 // data.
779 Register offset = field_offset_.AsRegisterPairLow<Register>();
780 Register expected = temp1_;
781 Register value = ref_reg;
782 Register tmp_ptr = TMP; // Pointer to actual memory.
783 Register tmp = AT; // Value in memory.
784
785 __ Addu(tmp_ptr, base, offset);
786
787 if (kPoisonHeapReferences) {
788 __ PoisonHeapReference(expected);
789 // Do not poison `value` if it is the same register as
790 // `expected`, which has just been poisoned.
791 if (value != expected) {
792 __ PoisonHeapReference(value);
793 }
794 }
795
796 // do {
797 // tmp = [r_ptr] - expected;
798 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
799
800 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
801 MipsLabel loop_head, exit_loop;
802 __ Bind(&loop_head);
803 if (is_r6) {
804 __ LlR6(tmp, tmp_ptr);
805 } else {
806 __ LlR2(tmp, tmp_ptr);
807 }
808 __ Bne(tmp, expected, &exit_loop);
809 __ Move(tmp, value);
810 if (is_r6) {
811 __ ScR6(tmp, tmp_ptr);
812 } else {
813 __ ScR2(tmp, tmp_ptr);
814 }
815 __ Beqz(tmp, &loop_head);
816 __ Bind(&exit_loop);
817
818 if (kPoisonHeapReferences) {
819 __ UnpoisonHeapReference(expected);
820 // Do not unpoison `value` if it is the same register as
821 // `expected`, which has just been unpoisoned.
822 if (value != expected) {
823 __ UnpoisonHeapReference(value);
824 }
825 }
826
827 __ Bind(&done);
828 __ B(GetExitLabel());
829 }
830
831 private:
832 // The location (register) of the marked object reference.
833 const Location ref_;
834 // The register containing the object holding the marked object reference field.
835 const Register obj_;
836 // The location of the offset of the marked reference field within `obj_`.
837 Location field_offset_;
838
839 const Register temp1_;
840
841 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
842};
843
844// Slow path generating a read barrier for a heap reference.
845class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
846 public:
847 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
848 Location out,
849 Location ref,
850 Location obj,
851 uint32_t offset,
852 Location index)
853 : SlowPathCodeMIPS(instruction),
854 out_(out),
855 ref_(ref),
856 obj_(obj),
857 offset_(offset),
858 index_(index) {
859 DCHECK(kEmitCompilerReadBarrier);
860 // If `obj` is equal to `out` or `ref`, it means the initial object
861 // has been overwritten by (or after) the heap object reference load
862 // to be instrumented, e.g.:
863 //
864 // __ LoadFromOffset(kLoadWord, out, out, offset);
865 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
866 //
867 // In that case, we have lost the information about the original
868 // object, and the emitted read barrier cannot work properly.
869 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
870 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
871 }
872
873 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
874 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
875 LocationSummary* locations = instruction_->GetLocations();
876 Register reg_out = out_.AsRegister<Register>();
877 DCHECK(locations->CanCall());
878 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
879 DCHECK(instruction_->IsInstanceFieldGet() ||
880 instruction_->IsStaticFieldGet() ||
881 instruction_->IsArrayGet() ||
882 instruction_->IsInstanceOf() ||
883 instruction_->IsCheckCast() ||
884 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
885 << "Unexpected instruction in read barrier for heap reference slow path: "
886 << instruction_->DebugName();
887
888 __ Bind(GetEntryLabel());
889 SaveLiveRegisters(codegen, locations);
890
891 // We may have to change the index's value, but as `index_` is a
892 // constant member (like other "inputs" of this slow path),
893 // introduce a copy of it, `index`.
894 Location index = index_;
895 if (index_.IsValid()) {
896 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
897 if (instruction_->IsArrayGet()) {
898 // Compute the actual memory offset and store it in `index`.
899 Register index_reg = index_.AsRegister<Register>();
900 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
901 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
902 // We are about to change the value of `index_reg` (see the
903 // calls to art::mips::MipsAssembler::Sll and
904 // art::mips::MipsAssembler::Addiu32 below), but it has
905 // not been saved by the previous call to
906 // art::SlowPathCode::SaveLiveRegisters, as it is a
907 // callee-save register --
908 // art::SlowPathCode::SaveLiveRegisters does not consider
909 // callee-save registers, as it has been designed with the
910 // assumption that callee-save registers are supposed to be
911 // handled by the called function. So, as a callee-save
912 // register, `index_reg` _would_ eventually be saved onto
913 // the stack, but it would be too late: we would have
914 // changed its value earlier. Therefore, we manually save
915 // it here into another freely available register,
916 // `free_reg`, chosen of course among the caller-save
917 // registers (as a callee-save `free_reg` register would
918 // exhibit the same problem).
919 //
920 // Note we could have requested a temporary register from
921 // the register allocator instead; but we prefer not to, as
922 // this is a slow path, and we know we can find a
923 // caller-save register that is available.
924 Register free_reg = FindAvailableCallerSaveRegister(codegen);
925 __ Move(free_reg, index_reg);
926 index_reg = free_reg;
927 index = Location::RegisterLocation(index_reg);
928 } else {
929 // The initial register stored in `index_` has already been
930 // saved in the call to art::SlowPathCode::SaveLiveRegisters
931 // (as it is not a callee-save register), so we can freely
932 // use it.
933 }
934 // Shifting the index value contained in `index_reg` by the scale
935 // factor (2) cannot overflow in practice, as the runtime is
936 // unable to allocate object arrays with a size larger than
937 // 2^26 - 1 (that is, 2^28 - 4 bytes).
938 __ Sll(index_reg, index_reg, TIMES_4);
939 static_assert(
940 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
941 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
942 __ Addiu32(index_reg, index_reg, offset_);
943 } else {
944 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
945 // intrinsics, `index_` is not shifted by a scale factor of 2
946 // (as in the case of ArrayGet), as it is actually an offset
947 // to an object field within an object.
948 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
949 DCHECK(instruction_->GetLocations()->Intrinsified());
950 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
951 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
952 << instruction_->AsInvoke()->GetIntrinsic();
953 DCHECK_EQ(offset_, 0U);
954 DCHECK(index_.IsRegisterPair());
955 // UnsafeGet's offset location is a register pair, the low
956 // part contains the correct offset.
957 index = index_.ToLow();
958 }
959 }
960
961 // We're moving two or three locations to locations that could
962 // overlap, so we need a parallel move resolver.
963 InvokeRuntimeCallingConvention calling_convention;
964 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
965 parallel_move.AddMove(ref_,
966 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
967 Primitive::kPrimNot,
968 nullptr);
969 parallel_move.AddMove(obj_,
970 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
971 Primitive::kPrimNot,
972 nullptr);
973 if (index.IsValid()) {
974 parallel_move.AddMove(index,
975 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
976 Primitive::kPrimInt,
977 nullptr);
978 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
979 } else {
980 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
981 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
982 }
983 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
984 instruction_,
985 instruction_->GetDexPc(),
986 this);
987 CheckEntrypointTypes<
988 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200989 mips_codegen->MoveLocation(out_,
990 calling_convention.GetReturnLocation(Primitive::kPrimNot),
991 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -0800992
993 RestoreLiveRegisters(codegen, locations);
994 __ B(GetExitLabel());
995 }
996
997 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
998
999 private:
1000 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1001 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1002 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1003 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1004 if (i != ref &&
1005 i != obj &&
1006 !codegen->IsCoreCalleeSaveRegister(i) &&
1007 !codegen->IsBlockedCoreRegister(i)) {
1008 return static_cast<Register>(i);
1009 }
1010 }
1011 // We shall never fail to find a free caller-save register, as
1012 // there are more than two core caller-save registers on MIPS
1013 // (meaning it is possible to find one which is different from
1014 // `ref` and `obj`).
1015 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1016 LOG(FATAL) << "Could not find a free caller-save register";
1017 UNREACHABLE();
1018 }
1019
1020 const Location out_;
1021 const Location ref_;
1022 const Location obj_;
1023 const uint32_t offset_;
1024 // An additional location containing an index to an array.
1025 // Only used for HArrayGet and the UnsafeGetObject &
1026 // UnsafeGetObjectVolatile intrinsics.
1027 const Location index_;
1028
1029 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
1030};
1031
1032// Slow path generating a read barrier for a GC root.
1033class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1034 public:
1035 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1036 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1037 DCHECK(kEmitCompilerReadBarrier);
1038 }
1039
1040 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1041 LocationSummary* locations = instruction_->GetLocations();
1042 Register reg_out = out_.AsRegister<Register>();
1043 DCHECK(locations->CanCall());
1044 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1045 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1046 << "Unexpected instruction in read barrier for GC root slow path: "
1047 << instruction_->DebugName();
1048
1049 __ Bind(GetEntryLabel());
1050 SaveLiveRegisters(codegen, locations);
1051
1052 InvokeRuntimeCallingConvention calling_convention;
1053 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001054 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1055 root_,
1056 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -08001057 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1058 instruction_,
1059 instruction_->GetDexPc(),
1060 this);
1061 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001062 mips_codegen->MoveLocation(out_,
1063 calling_convention.GetReturnLocation(Primitive::kPrimNot),
1064 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -08001065
1066 RestoreLiveRegisters(codegen, locations);
1067 __ B(GetExitLabel());
1068 }
1069
1070 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1071
1072 private:
1073 const Location out_;
1074 const Location root_;
1075
1076 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1077};
1078
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001079CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1080 const MipsInstructionSetFeatures& isa_features,
1081 const CompilerOptions& compiler_options,
1082 OptimizingCompilerStats* stats)
1083 : CodeGenerator(graph,
1084 kNumberOfCoreRegisters,
1085 kNumberOfFRegisters,
1086 kNumberOfRegisterPairs,
1087 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1088 arraysize(kCoreCalleeSaves)),
1089 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1090 arraysize(kFpuCalleeSaves)),
1091 compiler_options,
1092 stats),
1093 block_labels_(nullptr),
1094 location_builder_(graph, this),
1095 instruction_visitor_(graph, this),
1096 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001097 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001098 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001099 uint32_literals_(std::less<uint32_t>(),
1100 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001101 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001102 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001103 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001104 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001105 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001106 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001107 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1108 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001109 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001110 // Save RA (containing the return address) to mimic Quick.
1111 AddAllocatedRegister(Location::RegisterLocation(RA));
1112}
1113
1114#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001115// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1116#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001117#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001118
1119void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1120 // Ensure that we fix up branches.
1121 __ FinalizeCode();
1122
1123 // Adjust native pc offsets in stack maps.
1124 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001125 uint32_t old_position =
1126 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001127 uint32_t new_position = __ GetAdjustedPosition(old_position);
1128 DCHECK_GE(new_position, old_position);
1129 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1130 }
1131
1132 // Adjust pc offsets for the disassembly information.
1133 if (disasm_info_ != nullptr) {
1134 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1135 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1136 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1137 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1138 it.second.start = __ GetAdjustedPosition(it.second.start);
1139 it.second.end = __ GetAdjustedPosition(it.second.end);
1140 }
1141 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1142 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1143 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1144 }
1145 }
1146
1147 CodeGenerator::Finalize(allocator);
1148}
1149
1150MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1151 return codegen_->GetAssembler();
1152}
1153
1154void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1155 DCHECK_LT(index, moves_.size());
1156 MoveOperands* move = moves_[index];
1157 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1158}
1159
1160void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1161 DCHECK_LT(index, moves_.size());
1162 MoveOperands* move = moves_[index];
1163 Primitive::Type type = move->GetType();
1164 Location loc1 = move->GetDestination();
1165 Location loc2 = move->GetSource();
1166
1167 DCHECK(!loc1.IsConstant());
1168 DCHECK(!loc2.IsConstant());
1169
1170 if (loc1.Equals(loc2)) {
1171 return;
1172 }
1173
1174 if (loc1.IsRegister() && loc2.IsRegister()) {
1175 // Swap 2 GPRs.
1176 Register r1 = loc1.AsRegister<Register>();
1177 Register r2 = loc2.AsRegister<Register>();
1178 __ Move(TMP, r2);
1179 __ Move(r2, r1);
1180 __ Move(r1, TMP);
1181 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1182 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1183 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1184 if (type == Primitive::kPrimFloat) {
1185 __ MovS(FTMP, f2);
1186 __ MovS(f2, f1);
1187 __ MovS(f1, FTMP);
1188 } else {
1189 DCHECK_EQ(type, Primitive::kPrimDouble);
1190 __ MovD(FTMP, f2);
1191 __ MovD(f2, f1);
1192 __ MovD(f1, FTMP);
1193 }
1194 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1195 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1196 // Swap FPR and GPR.
1197 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
1198 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1199 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001200 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001201 __ Move(TMP, r2);
1202 __ Mfc1(r2, f1);
1203 __ Mtc1(TMP, f1);
1204 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1205 // Swap 2 GPR register pairs.
1206 Register r1 = loc1.AsRegisterPairLow<Register>();
1207 Register r2 = loc2.AsRegisterPairLow<Register>();
1208 __ Move(TMP, r2);
1209 __ Move(r2, r1);
1210 __ Move(r1, TMP);
1211 r1 = loc1.AsRegisterPairHigh<Register>();
1212 r2 = loc2.AsRegisterPairHigh<Register>();
1213 __ Move(TMP, r2);
1214 __ Move(r2, r1);
1215 __ Move(r1, TMP);
1216 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1217 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1218 // Swap FPR and GPR register pair.
1219 DCHECK_EQ(type, Primitive::kPrimDouble);
1220 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1221 : loc2.AsFpuRegister<FRegister>();
1222 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1223 : loc2.AsRegisterPairLow<Register>();
1224 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1225 : loc2.AsRegisterPairHigh<Register>();
1226 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1227 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1228 // unpredictable and the following mfch1 will fail.
1229 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001230 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001231 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001232 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001233 __ Move(r2_l, TMP);
1234 __ Move(r2_h, AT);
1235 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1236 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1237 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1238 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001239 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1240 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001241 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1242 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001243 __ Move(TMP, reg);
1244 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1245 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1246 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1247 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1248 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1249 : loc2.AsRegisterPairLow<Register>();
1250 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1251 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001252 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001253 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1254 : loc2.GetHighStackIndex(kMipsWordSize);
1255 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001256 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001257 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001258 __ Move(TMP, reg_h);
1259 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1260 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001261 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1262 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1263 : loc2.AsFpuRegister<FRegister>();
1264 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1265 if (type == Primitive::kPrimFloat) {
1266 __ MovS(FTMP, reg);
1267 __ LoadSFromOffset(reg, SP, offset);
1268 __ StoreSToOffset(FTMP, SP, offset);
1269 } else {
1270 DCHECK_EQ(type, Primitive::kPrimDouble);
1271 __ MovD(FTMP, reg);
1272 __ LoadDFromOffset(reg, SP, offset);
1273 __ StoreDToOffset(FTMP, SP, offset);
1274 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001275 } else {
1276 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1277 }
1278}
1279
1280void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1281 __ Pop(static_cast<Register>(reg));
1282}
1283
1284void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1285 __ Push(static_cast<Register>(reg));
1286}
1287
1288void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1289 // Allocate a scratch register other than TMP, if available.
1290 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1291 // automatically unspilled when the scratch scope object is destroyed).
1292 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1293 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1294 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1295 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1296 __ LoadFromOffset(kLoadWord,
1297 Register(ensure_scratch.GetRegister()),
1298 SP,
1299 index1 + stack_offset);
1300 __ LoadFromOffset(kLoadWord,
1301 TMP,
1302 SP,
1303 index2 + stack_offset);
1304 __ StoreToOffset(kStoreWord,
1305 Register(ensure_scratch.GetRegister()),
1306 SP,
1307 index2 + stack_offset);
1308 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1309 }
1310}
1311
Alexey Frunze73296a72016-06-03 22:51:46 -07001312void CodeGeneratorMIPS::ComputeSpillMask() {
1313 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1314 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1315 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1316 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1317 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1318 // within the stack frame.
1319 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1320 core_spill_mask_ |= (1 << ZERO);
1321 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001322}
1323
1324bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001325 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001326 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1327 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1328 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001329 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001330}
1331
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001332static dwarf::Reg DWARFReg(Register reg) {
1333 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1334}
1335
1336// TODO: mapping of floating-point registers to DWARF.
1337
1338void CodeGeneratorMIPS::GenerateFrameEntry() {
1339 __ Bind(&frame_entry_label_);
1340
1341 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1342
1343 if (do_overflow_check) {
1344 __ LoadFromOffset(kLoadWord,
1345 ZERO,
1346 SP,
1347 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1348 RecordPcInfo(nullptr, 0);
1349 }
1350
1351 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001352 CHECK_EQ(fpu_spill_mask_, 0u);
1353 CHECK_EQ(core_spill_mask_, 1u << RA);
1354 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355 return;
1356 }
1357
1358 // Make sure the frame size isn't unreasonably large.
1359 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1360 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1361 }
1362
1363 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001364
Alexey Frunze73296a72016-06-03 22:51:46 -07001365 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001366 __ IncreaseFrameSize(ofs);
1367
Alexey Frunze73296a72016-06-03 22:51:46 -07001368 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1369 Register reg = static_cast<Register>(MostSignificantBit(mask));
1370 mask ^= 1u << reg;
1371 ofs -= kMipsWordSize;
1372 // The ZERO register is only included for alignment.
1373 if (reg != ZERO) {
1374 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001375 __ cfi().RelOffset(DWARFReg(reg), ofs);
1376 }
1377 }
1378
Alexey Frunze73296a72016-06-03 22:51:46 -07001379 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1380 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1381 mask ^= 1u << reg;
1382 ofs -= kMipsDoublewordSize;
1383 __ StoreDToOffset(reg, SP, ofs);
1384 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001385 }
1386
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001387 // Save the current method if we need it. Note that we do not
1388 // do this in HCurrentMethod, as the instruction might have been removed
1389 // in the SSA graph.
1390 if (RequiresCurrentMethod()) {
1391 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1392 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001393
1394 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1395 // Initialize should deoptimize flag to 0.
1396 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1397 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001398}
1399
1400void CodeGeneratorMIPS::GenerateFrameExit() {
1401 __ cfi().RememberState();
1402
1403 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001404 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001405
Alexey Frunze73296a72016-06-03 22:51:46 -07001406 // For better instruction scheduling restore RA before other registers.
1407 uint32_t ofs = GetFrameSize();
1408 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1409 Register reg = static_cast<Register>(MostSignificantBit(mask));
1410 mask ^= 1u << reg;
1411 ofs -= kMipsWordSize;
1412 // The ZERO register is only included for alignment.
1413 if (reg != ZERO) {
1414 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001415 __ cfi().Restore(DWARFReg(reg));
1416 }
1417 }
1418
Alexey Frunze73296a72016-06-03 22:51:46 -07001419 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1420 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1421 mask ^= 1u << reg;
1422 ofs -= kMipsDoublewordSize;
1423 __ LoadDFromOffset(reg, SP, ofs);
1424 // TODO: __ cfi().Restore(DWARFReg(reg));
1425 }
1426
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001427 size_t frame_size = GetFrameSize();
1428 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1429 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1430 bool reordering = __ SetReorder(false);
1431 if (exchange) {
1432 __ Jr(RA);
1433 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1434 } else {
1435 __ DecreaseFrameSize(frame_size);
1436 __ Jr(RA);
1437 __ Nop(); // In delay slot.
1438 }
1439 __ SetReorder(reordering);
1440 } else {
1441 __ Jr(RA);
1442 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001443 }
1444
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001445 __ cfi().RestoreState();
1446 __ cfi().DefCFAOffset(GetFrameSize());
1447}
1448
1449void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1450 __ Bind(GetLabelOf(block));
1451}
1452
Lena Djokicca8c2952017-05-29 11:31:46 +02001453VectorRegister VectorRegisterFrom(Location location) {
1454 DCHECK(location.IsFpuRegister());
1455 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1456}
1457
Lena Djokic8098da92017-06-28 12:07:50 +02001458void CodeGeneratorMIPS::MoveLocation(Location destination,
1459 Location source,
1460 Primitive::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001461 if (source.Equals(destination)) {
1462 return;
1463 }
1464
Lena Djokic8098da92017-06-28 12:07:50 +02001465 if (source.IsConstant()) {
1466 MoveConstant(destination, source.GetConstant());
1467 } else {
1468 if (destination.IsRegister()) {
1469 if (source.IsRegister()) {
1470 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1471 } else if (source.IsFpuRegister()) {
1472 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1473 } else {
1474 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001475 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001476 }
1477 } else if (destination.IsRegisterPair()) {
1478 if (source.IsRegisterPair()) {
1479 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1480 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1481 } else if (source.IsFpuRegister()) {
1482 Register dst_high = destination.AsRegisterPairHigh<Register>();
1483 Register dst_low = destination.AsRegisterPairLow<Register>();
1484 FRegister src = source.AsFpuRegister<FRegister>();
1485 __ Mfc1(dst_low, src);
1486 __ MoveFromFpuHigh(dst_high, src);
1487 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001488 DCHECK(source.IsDoubleStackSlot())
1489 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001490 int32_t off = source.GetStackIndex();
1491 Register r = destination.AsRegisterPairLow<Register>();
1492 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1493 }
1494 } else if (destination.IsFpuRegister()) {
1495 if (source.IsRegister()) {
1496 DCHECK(!Primitive::Is64BitType(dst_type));
1497 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1498 } else if (source.IsRegisterPair()) {
1499 DCHECK(Primitive::Is64BitType(dst_type));
1500 FRegister dst = destination.AsFpuRegister<FRegister>();
1501 Register src_high = source.AsRegisterPairHigh<Register>();
1502 Register src_low = source.AsRegisterPairLow<Register>();
1503 __ Mtc1(src_low, dst);
1504 __ MoveToFpuHigh(src_high, dst);
1505 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001506 if (GetGraph()->HasSIMD()) {
1507 __ MoveV(VectorRegisterFrom(destination),
1508 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001509 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001510 if (Primitive::Is64BitType(dst_type)) {
1511 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1512 } else {
1513 DCHECK_EQ(dst_type, Primitive::kPrimFloat);
1514 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1515 }
Lena Djokic8098da92017-06-28 12:07:50 +02001516 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001517 } else if (source.IsSIMDStackSlot()) {
1518 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001519 } else if (source.IsDoubleStackSlot()) {
1520 DCHECK(Primitive::Is64BitType(dst_type));
1521 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1522 } else {
1523 DCHECK(!Primitive::Is64BitType(dst_type));
1524 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1525 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1526 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001527 } else if (destination.IsSIMDStackSlot()) {
1528 if (source.IsFpuRegister()) {
1529 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1530 } else {
1531 DCHECK(source.IsSIMDStackSlot());
1532 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1533 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1534 }
Lena Djokic8098da92017-06-28 12:07:50 +02001535 } else if (destination.IsDoubleStackSlot()) {
1536 int32_t dst_offset = destination.GetStackIndex();
1537 if (source.IsRegisterPair()) {
1538 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1539 } else if (source.IsFpuRegister()) {
1540 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1541 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001542 DCHECK(source.IsDoubleStackSlot())
1543 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001544 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1545 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1546 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1547 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1548 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001549 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001550 DCHECK(destination.IsStackSlot()) << destination;
1551 int32_t dst_offset = destination.GetStackIndex();
1552 if (source.IsRegister()) {
1553 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1554 } else if (source.IsFpuRegister()) {
1555 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1556 } else {
1557 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1558 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1559 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1560 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001561 }
1562 }
1563}
1564
1565void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1566 if (c->IsIntConstant() || c->IsNullConstant()) {
1567 // Move 32 bit constant.
1568 int32_t value = GetInt32ValueOf(c);
1569 if (destination.IsRegister()) {
1570 Register dst = destination.AsRegister<Register>();
1571 __ LoadConst32(dst, value);
1572 } else {
1573 DCHECK(destination.IsStackSlot())
1574 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001575 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001576 }
1577 } else if (c->IsLongConstant()) {
1578 // Move 64 bit constant.
1579 int64_t value = GetInt64ValueOf(c);
1580 if (destination.IsRegisterPair()) {
1581 Register r_h = destination.AsRegisterPairHigh<Register>();
1582 Register r_l = destination.AsRegisterPairLow<Register>();
1583 __ LoadConst64(r_h, r_l, value);
1584 } else {
1585 DCHECK(destination.IsDoubleStackSlot())
1586 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001587 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001588 }
1589 } else if (c->IsFloatConstant()) {
1590 // Move 32 bit float constant.
1591 int32_t value = GetInt32ValueOf(c);
1592 if (destination.IsFpuRegister()) {
1593 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1594 } else {
1595 DCHECK(destination.IsStackSlot())
1596 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001597 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001598 }
1599 } else {
1600 // Move 64 bit double constant.
1601 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1602 int64_t value = GetInt64ValueOf(c);
1603 if (destination.IsFpuRegister()) {
1604 FRegister fd = destination.AsFpuRegister<FRegister>();
1605 __ LoadDConst64(fd, value, TMP);
1606 } else {
1607 DCHECK(destination.IsDoubleStackSlot())
1608 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001609 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001610 }
1611 }
1612}
1613
1614void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1615 DCHECK(destination.IsRegister());
1616 Register dst = destination.AsRegister<Register>();
1617 __ LoadConst32(dst, value);
1618}
1619
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001620void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1621 if (location.IsRegister()) {
1622 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001623 } else if (location.IsRegisterPair()) {
1624 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1625 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001626 } else {
1627 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1628 }
1629}
1630
Vladimir Markoaad75c62016-10-03 08:46:48 +00001631template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1632inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1633 const ArenaDeque<PcRelativePatchInfo>& infos,
1634 ArenaVector<LinkerPatch>* linker_patches) {
1635 for (const PcRelativePatchInfo& info : infos) {
1636 const DexFile& dex_file = info.target_dex_file;
1637 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001638 DCHECK(info.label.IsBound());
1639 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001640 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1641 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001642 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1643 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1644 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001645 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001646 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001647 }
1648}
1649
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001650void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1651 DCHECK(linker_patches->empty());
1652 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001653 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001654 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001655 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001656 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001657 pc_relative_string_patches_.size() +
1658 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001659 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001660 if (GetCompilerOptions().IsBootImage()) {
1661 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00001662 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001663 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1664 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001665 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1666 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001667 } else {
1668 DCHECK(pc_relative_method_patches_.empty());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01001669 EmitPcRelativeLinkerPatches<LinkerPatch::TypeClassTablePatch>(pc_relative_type_patches_,
1670 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001671 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(pc_relative_string_patches_,
1672 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001673 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001674 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1675 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001676 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1677 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001678 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
1679 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001680 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001681}
1682
Vladimir Marko65979462017-05-19 17:25:12 +01001683CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001684 MethodReference target_method,
1685 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001686 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001687 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001688 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001689 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001690}
1691
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001692CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001693 MethodReference target_method,
1694 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001695 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001696 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001697 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001698 &method_bss_entry_patches_);
1699}
1700
Alexey Frunze06a46c42016-07-19 15:00:40 -07001701CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001702 const DexFile& dex_file,
1703 dex::TypeIndex type_index,
1704 const PcRelativePatchInfo* info_high) {
1705 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001706}
1707
Vladimir Marko1998cd02017-01-13 13:02:58 +00001708CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001709 const DexFile& dex_file,
1710 dex::TypeIndex type_index,
1711 const PcRelativePatchInfo* info_high) {
1712 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001713}
1714
Vladimir Marko65979462017-05-19 17:25:12 +01001715CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001716 const DexFile& dex_file,
1717 dex::StringIndex string_index,
1718 const PcRelativePatchInfo* info_high) {
1719 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001720}
1721
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001722CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1723 const DexFile& dex_file,
1724 dex::StringIndex string_index,
1725 const PcRelativePatchInfo* info_high) {
1726 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1727}
1728
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001729CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001730 const DexFile& dex_file,
1731 uint32_t offset_or_index,
1732 const PcRelativePatchInfo* info_high,
1733 ArenaDeque<PcRelativePatchInfo>* patches) {
1734 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001735 return &patches->back();
1736}
1737
Alexey Frunze06a46c42016-07-19 15:00:40 -07001738Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1739 return map->GetOrCreate(
1740 value,
1741 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1742}
1743
Alexey Frunze06a46c42016-07-19 15:00:40 -07001744Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001745 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001746}
1747
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001748void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001749 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001750 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001751 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001752 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001753 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001754 if (GetInstructionSetFeatures().IsR6()) {
1755 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001756 __ Bind(&info_high->label);
1757 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001758 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001759 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001760 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001761 } else {
1762 // If base is ZERO, emit NAL to obtain the actual base.
1763 if (base == ZERO) {
1764 // Generate a dummy PC-relative call to obtain PC.
1765 __ Nal();
1766 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001767 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001768 __ Lui(out, /* placeholder */ 0x1234);
1769 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1770 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1771 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001772 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001773 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001774 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001775 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001776 __ Addu(out, out, (base == ZERO) ? RA : base);
1777 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001778 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001779 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001780}
1781
Alexey Frunze627c1a02017-01-30 19:28:14 -08001782CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1783 const DexFile& dex_file,
1784 dex::StringIndex dex_index,
1785 Handle<mirror::String> handle) {
1786 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1787 reinterpret_cast64<uint64_t>(handle.GetReference()));
1788 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1789 return &jit_string_patches_.back();
1790}
1791
1792CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1793 const DexFile& dex_file,
1794 dex::TypeIndex dex_index,
1795 Handle<mirror::Class> handle) {
1796 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1797 reinterpret_cast64<uint64_t>(handle.GetReference()));
1798 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1799 return &jit_class_patches_.back();
1800}
1801
1802void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1803 const uint8_t* roots_data,
1804 const CodeGeneratorMIPS::JitPatchInfo& info,
1805 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001806 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1807 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001808 uintptr_t address =
1809 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1810 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1811 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001812 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1813 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1814 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1815 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001816 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001817 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1818 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001819 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001820 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001821 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1822 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001823 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001824 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1825 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001826}
1827
1828void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1829 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001830 const auto it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1831 dex::StringIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001832 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001833 uint64_t index_in_table = it->second;
1834 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001835 }
1836 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001837 const auto it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1838 dex::TypeIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001839 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001840 uint64_t index_in_table = it->second;
1841 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001842 }
1843}
1844
Goran Jakovljevice114da22016-12-26 14:21:43 +01001845void CodeGeneratorMIPS::MarkGCCard(Register object,
1846 Register value,
1847 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001848 MipsLabel done;
1849 Register card = AT;
1850 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001851 if (value_can_be_null) {
1852 __ Beqz(value, &done);
1853 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001854 __ LoadFromOffset(kLoadWord,
1855 card,
1856 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001857 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1859 __ Addu(temp, card, temp);
1860 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001861 if (value_can_be_null) {
1862 __ Bind(&done);
1863 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001864}
1865
David Brazdil58282f42016-01-14 12:45:10 +00001866void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001867 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1868 blocked_core_registers_[ZERO] = true;
1869 blocked_core_registers_[K0] = true;
1870 blocked_core_registers_[K1] = true;
1871 blocked_core_registers_[GP] = true;
1872 blocked_core_registers_[SP] = true;
1873 blocked_core_registers_[RA] = true;
1874
1875 // AT and TMP(T8) are used as temporary/scratch registers
1876 // (similar to how AT is used by MIPS assemblers).
1877 blocked_core_registers_[AT] = true;
1878 blocked_core_registers_[TMP] = true;
1879 blocked_fpu_registers_[FTMP] = true;
1880
1881 // Reserve suspend and thread registers.
1882 blocked_core_registers_[S0] = true;
1883 blocked_core_registers_[TR] = true;
1884
1885 // Reserve T9 for function calls
1886 blocked_core_registers_[T9] = true;
1887
1888 // Reserve odd-numbered FPU registers.
1889 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1890 blocked_fpu_registers_[i] = true;
1891 }
1892
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001893 if (GetGraph()->IsDebuggable()) {
1894 // Stubs do not save callee-save floating point registers. If the graph
1895 // is debuggable, we need to deal with these registers differently. For
1896 // now, just block them.
1897 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1898 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1899 }
1900 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001901}
1902
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001903size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1904 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1905 return kMipsWordSize;
1906}
1907
1908size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1909 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1910 return kMipsWordSize;
1911}
1912
1913size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001914 if (GetGraph()->HasSIMD()) {
1915 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1916 } else {
1917 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1918 }
1919 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001920}
1921
1922size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001923 if (GetGraph()->HasSIMD()) {
1924 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1925 } else {
1926 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1927 }
1928 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001929}
1930
1931void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001932 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933}
1934
1935void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001936 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937}
1938
Serban Constantinescufca16662016-07-14 09:21:59 +01001939constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1940
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001941void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1942 HInstruction* instruction,
1943 uint32_t dex_pc,
1944 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001945 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001946 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1947 IsDirectEntrypoint(entrypoint));
1948 if (EntrypointRequiresStackMap(entrypoint)) {
1949 RecordPcInfo(instruction, dex_pc, slow_path);
1950 }
1951}
1952
1953void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1954 HInstruction* instruction,
1955 SlowPathCode* slow_path,
1956 bool direct) {
1957 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1958 GenerateInvokeRuntime(entry_point_offset, direct);
1959}
1960
1961void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001962 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001963 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001964 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001965 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001966 // Reserve argument space on stack (for $a0-$a3) for
1967 // entrypoints that directly reference native implementations.
1968 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001969 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001970 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001971 } else {
1972 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001973 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001974 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001975}
1976
1977void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1978 Register class_reg) {
1979 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1980 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1981 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1982 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1983 __ Sync(0);
1984 __ Bind(slow_path->GetExitLabel());
1985}
1986
1987void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1988 __ Sync(0); // Only stype 0 is supported.
1989}
1990
1991void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1992 HBasicBlock* successor) {
1993 SuspendCheckSlowPathMIPS* slow_path =
1994 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1995 codegen_->AddSlowPath(slow_path);
1996
1997 __ LoadFromOffset(kLoadUnsignedHalfword,
1998 TMP,
1999 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002000 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002001 if (successor == nullptr) {
2002 __ Bnez(TMP, slow_path->GetEntryLabel());
2003 __ Bind(slow_path->GetReturnLabel());
2004 } else {
2005 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2006 __ B(slow_path->GetEntryLabel());
2007 // slow_path will return to GetLabelOf(successor).
2008 }
2009}
2010
2011InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2012 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002013 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002014 assembler_(codegen->GetAssembler()),
2015 codegen_(codegen) {}
2016
2017void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2018 DCHECK_EQ(instruction->InputCount(), 2U);
2019 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2020 Primitive::Type type = instruction->GetResultType();
2021 switch (type) {
2022 case Primitive::kPrimInt: {
2023 locations->SetInAt(0, Location::RequiresRegister());
2024 HInstruction* right = instruction->InputAt(1);
2025 bool can_use_imm = false;
2026 if (right->IsConstant()) {
2027 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2028 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2029 can_use_imm = IsUint<16>(imm);
2030 } else if (instruction->IsAdd()) {
2031 can_use_imm = IsInt<16>(imm);
2032 } else {
2033 DCHECK(instruction->IsSub());
2034 can_use_imm = IsInt<16>(-imm);
2035 }
2036 }
2037 if (can_use_imm)
2038 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2039 else
2040 locations->SetInAt(1, Location::RequiresRegister());
2041 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2042 break;
2043 }
2044
2045 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002046 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002047 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2048 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002049 break;
2050 }
2051
2052 case Primitive::kPrimFloat:
2053 case Primitive::kPrimDouble:
2054 DCHECK(instruction->IsAdd() || instruction->IsSub());
2055 locations->SetInAt(0, Location::RequiresFpuRegister());
2056 locations->SetInAt(1, Location::RequiresFpuRegister());
2057 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2058 break;
2059
2060 default:
2061 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2062 }
2063}
2064
2065void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2066 Primitive::Type type = instruction->GetType();
2067 LocationSummary* locations = instruction->GetLocations();
2068
2069 switch (type) {
2070 case Primitive::kPrimInt: {
2071 Register dst = locations->Out().AsRegister<Register>();
2072 Register lhs = locations->InAt(0).AsRegister<Register>();
2073 Location rhs_location = locations->InAt(1);
2074
2075 Register rhs_reg = ZERO;
2076 int32_t rhs_imm = 0;
2077 bool use_imm = rhs_location.IsConstant();
2078 if (use_imm) {
2079 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2080 } else {
2081 rhs_reg = rhs_location.AsRegister<Register>();
2082 }
2083
2084 if (instruction->IsAnd()) {
2085 if (use_imm)
2086 __ Andi(dst, lhs, rhs_imm);
2087 else
2088 __ And(dst, lhs, rhs_reg);
2089 } else if (instruction->IsOr()) {
2090 if (use_imm)
2091 __ Ori(dst, lhs, rhs_imm);
2092 else
2093 __ Or(dst, lhs, rhs_reg);
2094 } else if (instruction->IsXor()) {
2095 if (use_imm)
2096 __ Xori(dst, lhs, rhs_imm);
2097 else
2098 __ Xor(dst, lhs, rhs_reg);
2099 } else if (instruction->IsAdd()) {
2100 if (use_imm)
2101 __ Addiu(dst, lhs, rhs_imm);
2102 else
2103 __ Addu(dst, lhs, rhs_reg);
2104 } else {
2105 DCHECK(instruction->IsSub());
2106 if (use_imm)
2107 __ Addiu(dst, lhs, -rhs_imm);
2108 else
2109 __ Subu(dst, lhs, rhs_reg);
2110 }
2111 break;
2112 }
2113
2114 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002115 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2116 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2117 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2118 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002119 Location rhs_location = locations->InAt(1);
2120 bool use_imm = rhs_location.IsConstant();
2121 if (!use_imm) {
2122 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2123 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2124 if (instruction->IsAnd()) {
2125 __ And(dst_low, lhs_low, rhs_low);
2126 __ And(dst_high, lhs_high, rhs_high);
2127 } else if (instruction->IsOr()) {
2128 __ Or(dst_low, lhs_low, rhs_low);
2129 __ Or(dst_high, lhs_high, rhs_high);
2130 } else if (instruction->IsXor()) {
2131 __ Xor(dst_low, lhs_low, rhs_low);
2132 __ Xor(dst_high, lhs_high, rhs_high);
2133 } else if (instruction->IsAdd()) {
2134 if (lhs_low == rhs_low) {
2135 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2136 __ Slt(TMP, lhs_low, ZERO);
2137 __ Addu(dst_low, lhs_low, rhs_low);
2138 } else {
2139 __ Addu(dst_low, lhs_low, rhs_low);
2140 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2141 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2142 }
2143 __ Addu(dst_high, lhs_high, rhs_high);
2144 __ Addu(dst_high, dst_high, TMP);
2145 } else {
2146 DCHECK(instruction->IsSub());
2147 __ Sltu(TMP, lhs_low, rhs_low);
2148 __ Subu(dst_low, lhs_low, rhs_low);
2149 __ Subu(dst_high, lhs_high, rhs_high);
2150 __ Subu(dst_high, dst_high, TMP);
2151 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002152 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002153 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2154 if (instruction->IsOr()) {
2155 uint32_t low = Low32Bits(value);
2156 uint32_t high = High32Bits(value);
2157 if (IsUint<16>(low)) {
2158 if (dst_low != lhs_low || low != 0) {
2159 __ Ori(dst_low, lhs_low, low);
2160 }
2161 } else {
2162 __ LoadConst32(TMP, low);
2163 __ Or(dst_low, lhs_low, TMP);
2164 }
2165 if (IsUint<16>(high)) {
2166 if (dst_high != lhs_high || high != 0) {
2167 __ Ori(dst_high, lhs_high, high);
2168 }
2169 } else {
2170 if (high != low) {
2171 __ LoadConst32(TMP, high);
2172 }
2173 __ Or(dst_high, lhs_high, TMP);
2174 }
2175 } else if (instruction->IsXor()) {
2176 uint32_t low = Low32Bits(value);
2177 uint32_t high = High32Bits(value);
2178 if (IsUint<16>(low)) {
2179 if (dst_low != lhs_low || low != 0) {
2180 __ Xori(dst_low, lhs_low, low);
2181 }
2182 } else {
2183 __ LoadConst32(TMP, low);
2184 __ Xor(dst_low, lhs_low, TMP);
2185 }
2186 if (IsUint<16>(high)) {
2187 if (dst_high != lhs_high || high != 0) {
2188 __ Xori(dst_high, lhs_high, high);
2189 }
2190 } else {
2191 if (high != low) {
2192 __ LoadConst32(TMP, high);
2193 }
2194 __ Xor(dst_high, lhs_high, TMP);
2195 }
2196 } else if (instruction->IsAnd()) {
2197 uint32_t low = Low32Bits(value);
2198 uint32_t high = High32Bits(value);
2199 if (IsUint<16>(low)) {
2200 __ Andi(dst_low, lhs_low, low);
2201 } else if (low != 0xFFFFFFFF) {
2202 __ LoadConst32(TMP, low);
2203 __ And(dst_low, lhs_low, TMP);
2204 } else if (dst_low != lhs_low) {
2205 __ Move(dst_low, lhs_low);
2206 }
2207 if (IsUint<16>(high)) {
2208 __ Andi(dst_high, lhs_high, high);
2209 } else if (high != 0xFFFFFFFF) {
2210 if (high != low) {
2211 __ LoadConst32(TMP, high);
2212 }
2213 __ And(dst_high, lhs_high, TMP);
2214 } else if (dst_high != lhs_high) {
2215 __ Move(dst_high, lhs_high);
2216 }
2217 } else {
2218 if (instruction->IsSub()) {
2219 value = -value;
2220 } else {
2221 DCHECK(instruction->IsAdd());
2222 }
2223 int32_t low = Low32Bits(value);
2224 int32_t high = High32Bits(value);
2225 if (IsInt<16>(low)) {
2226 if (dst_low != lhs_low || low != 0) {
2227 __ Addiu(dst_low, lhs_low, low);
2228 }
2229 if (low != 0) {
2230 __ Sltiu(AT, dst_low, low);
2231 }
2232 } else {
2233 __ LoadConst32(TMP, low);
2234 __ Addu(dst_low, lhs_low, TMP);
2235 __ Sltu(AT, dst_low, TMP);
2236 }
2237 if (IsInt<16>(high)) {
2238 if (dst_high != lhs_high || high != 0) {
2239 __ Addiu(dst_high, lhs_high, high);
2240 }
2241 } else {
2242 if (high != low) {
2243 __ LoadConst32(TMP, high);
2244 }
2245 __ Addu(dst_high, lhs_high, TMP);
2246 }
2247 if (low != 0) {
2248 __ Addu(dst_high, dst_high, AT);
2249 }
2250 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002251 }
2252 break;
2253 }
2254
2255 case Primitive::kPrimFloat:
2256 case Primitive::kPrimDouble: {
2257 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2258 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2259 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2260 if (instruction->IsAdd()) {
2261 if (type == Primitive::kPrimFloat) {
2262 __ AddS(dst, lhs, rhs);
2263 } else {
2264 __ AddD(dst, lhs, rhs);
2265 }
2266 } else {
2267 DCHECK(instruction->IsSub());
2268 if (type == Primitive::kPrimFloat) {
2269 __ SubS(dst, lhs, rhs);
2270 } else {
2271 __ SubD(dst, lhs, rhs);
2272 }
2273 }
2274 break;
2275 }
2276
2277 default:
2278 LOG(FATAL) << "Unexpected binary operation type " << type;
2279 }
2280}
2281
2282void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002283 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002284
2285 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2286 Primitive::Type type = instr->GetResultType();
2287 switch (type) {
2288 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002289 locations->SetInAt(0, Location::RequiresRegister());
2290 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2291 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2292 break;
2293 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002294 locations->SetInAt(0, Location::RequiresRegister());
2295 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2296 locations->SetOut(Location::RequiresRegister());
2297 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002298 default:
2299 LOG(FATAL) << "Unexpected shift type " << type;
2300 }
2301}
2302
2303static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2304
2305void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002306 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002307 LocationSummary* locations = instr->GetLocations();
2308 Primitive::Type type = instr->GetType();
2309
2310 Location rhs_location = locations->InAt(1);
2311 bool use_imm = rhs_location.IsConstant();
2312 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2313 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002314 const uint32_t shift_mask =
2315 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002316 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002317 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2318 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002319
2320 switch (type) {
2321 case Primitive::kPrimInt: {
2322 Register dst = locations->Out().AsRegister<Register>();
2323 Register lhs = locations->InAt(0).AsRegister<Register>();
2324 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002325 if (shift_value == 0) {
2326 if (dst != lhs) {
2327 __ Move(dst, lhs);
2328 }
2329 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002330 __ Sll(dst, lhs, shift_value);
2331 } else if (instr->IsShr()) {
2332 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002333 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002334 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002335 } else {
2336 if (has_ins_rotr) {
2337 __ Rotr(dst, lhs, shift_value);
2338 } else {
2339 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2340 __ Srl(dst, lhs, shift_value);
2341 __ Or(dst, dst, TMP);
2342 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002343 }
2344 } else {
2345 if (instr->IsShl()) {
2346 __ Sllv(dst, lhs, rhs_reg);
2347 } else if (instr->IsShr()) {
2348 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002349 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002350 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002351 } else {
2352 if (has_ins_rotr) {
2353 __ Rotrv(dst, lhs, rhs_reg);
2354 } else {
2355 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002356 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2357 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2358 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2359 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2360 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002361 __ Sllv(TMP, lhs, TMP);
2362 __ Srlv(dst, lhs, rhs_reg);
2363 __ Or(dst, dst, TMP);
2364 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365 }
2366 }
2367 break;
2368 }
2369
2370 case Primitive::kPrimLong: {
2371 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2372 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2373 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2374 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2375 if (use_imm) {
2376 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002377 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002378 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002379 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002380 if (instr->IsShl()) {
2381 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2382 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2383 __ Sll(dst_low, lhs_low, shift_value);
2384 } else if (instr->IsShr()) {
2385 __ Srl(dst_low, lhs_low, shift_value);
2386 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2387 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002388 } else if (instr->IsUShr()) {
2389 __ Srl(dst_low, lhs_low, shift_value);
2390 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2391 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002392 } else {
2393 __ Srl(dst_low, lhs_low, shift_value);
2394 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2395 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002396 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002397 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002398 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002399 if (instr->IsShl()) {
2400 __ Sll(dst_low, lhs_low, shift_value);
2401 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2402 __ Sll(dst_high, lhs_high, shift_value);
2403 __ Or(dst_high, dst_high, TMP);
2404 } else if (instr->IsShr()) {
2405 __ Sra(dst_high, lhs_high, shift_value);
2406 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2407 __ Srl(dst_low, lhs_low, shift_value);
2408 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002409 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002410 __ Srl(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 {
2415 __ Srl(TMP, lhs_low, shift_value);
2416 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2417 __ Or(dst_low, dst_low, TMP);
2418 __ Srl(TMP, lhs_high, shift_value);
2419 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2420 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002421 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002422 }
2423 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002424 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002425 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002426 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002427 __ Move(dst_low, ZERO);
2428 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002429 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002430 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002431 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002432 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002433 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002434 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002435 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002436 // 64-bit rotation by 32 is just a swap.
2437 __ Move(dst_low, lhs_high);
2438 __ Move(dst_high, lhs_low);
2439 } else {
2440 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002441 __ Srl(dst_low, lhs_high, shift_value_high);
2442 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2443 __ Srl(dst_high, lhs_low, shift_value_high);
2444 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002445 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002446 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2447 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002448 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002449 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2450 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002451 __ Or(dst_high, dst_high, TMP);
2452 }
2453 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002454 }
2455 }
2456 } else {
2457 MipsLabel done;
2458 if (instr->IsShl()) {
2459 __ Sllv(dst_low, lhs_low, rhs_reg);
2460 __ Nor(AT, ZERO, rhs_reg);
2461 __ Srl(TMP, lhs_low, 1);
2462 __ Srlv(TMP, TMP, AT);
2463 __ Sllv(dst_high, lhs_high, rhs_reg);
2464 __ Or(dst_high, dst_high, TMP);
2465 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2466 __ Beqz(TMP, &done);
2467 __ Move(dst_high, dst_low);
2468 __ Move(dst_low, ZERO);
2469 } else if (instr->IsShr()) {
2470 __ Srav(dst_high, lhs_high, rhs_reg);
2471 __ Nor(AT, ZERO, rhs_reg);
2472 __ Sll(TMP, lhs_high, 1);
2473 __ Sllv(TMP, TMP, AT);
2474 __ Srlv(dst_low, lhs_low, rhs_reg);
2475 __ Or(dst_low, dst_low, TMP);
2476 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2477 __ Beqz(TMP, &done);
2478 __ Move(dst_low, dst_high);
2479 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002480 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002481 __ Srlv(dst_high, lhs_high, rhs_reg);
2482 __ Nor(AT, ZERO, rhs_reg);
2483 __ Sll(TMP, lhs_high, 1);
2484 __ Sllv(TMP, TMP, AT);
2485 __ Srlv(dst_low, lhs_low, rhs_reg);
2486 __ Or(dst_low, dst_low, TMP);
2487 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2488 __ Beqz(TMP, &done);
2489 __ Move(dst_low, dst_high);
2490 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002491 } else {
2492 __ Nor(AT, ZERO, rhs_reg);
2493 __ Srlv(TMP, lhs_low, rhs_reg);
2494 __ Sll(dst_low, lhs_high, 1);
2495 __ Sllv(dst_low, dst_low, AT);
2496 __ Or(dst_low, dst_low, TMP);
2497 __ Srlv(TMP, lhs_high, rhs_reg);
2498 __ Sll(dst_high, lhs_low, 1);
2499 __ Sllv(dst_high, dst_high, AT);
2500 __ Or(dst_high, dst_high, TMP);
2501 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2502 __ Beqz(TMP, &done);
2503 __ Move(TMP, dst_high);
2504 __ Move(dst_high, dst_low);
2505 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002506 }
2507 __ Bind(&done);
2508 }
2509 break;
2510 }
2511
2512 default:
2513 LOG(FATAL) << "Unexpected shift operation type " << type;
2514 }
2515}
2516
2517void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2518 HandleBinaryOp(instruction);
2519}
2520
2521void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2522 HandleBinaryOp(instruction);
2523}
2524
2525void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2526 HandleBinaryOp(instruction);
2527}
2528
2529void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2530 HandleBinaryOp(instruction);
2531}
2532
2533void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002534 Primitive::Type type = instruction->GetType();
2535 bool object_array_get_with_read_barrier =
2536 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002537 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002538 new (GetGraph()->GetArena()) LocationSummary(instruction,
2539 object_array_get_with_read_barrier
2540 ? LocationSummary::kCallOnSlowPath
2541 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002542 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2543 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2544 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002545 locations->SetInAt(0, Location::RequiresRegister());
2546 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002547 if (Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002548 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2549 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002550 // The output overlaps in the case of an object array get with
2551 // read barriers enabled: we do not want the move to overwrite the
2552 // array's location, as we need it to emit the read barrier.
2553 locations->SetOut(Location::RequiresRegister(),
2554 object_array_get_with_read_barrier
2555 ? Location::kOutputOverlap
2556 : Location::kNoOutputOverlap);
2557 }
2558 // We need a temporary register for the read barrier marking slow
2559 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2560 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002561 bool temp_needed = instruction->GetIndex()->IsConstant()
2562 ? !kBakerReadBarrierThunksEnableForFields
2563 : !kBakerReadBarrierThunksEnableForArrays;
2564 if (temp_needed) {
2565 locations->AddTemp(Location::RequiresRegister());
2566 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002567 }
2568}
2569
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002570static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2571 auto null_checker = [codegen, instruction]() {
2572 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002573 };
2574 return null_checker;
2575}
2576
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002577void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2578 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002579 Location obj_loc = locations->InAt(0);
2580 Register obj = obj_loc.AsRegister<Register>();
2581 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002582 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002583 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002584 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002585
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002586 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002587 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2588 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002589 switch (type) {
2590 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002591 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002592 if (index.IsConstant()) {
2593 size_t offset =
2594 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002595 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 } else {
2597 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002598 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002599 }
2600 break;
2601 }
2602
2603 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002604 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002605 if (index.IsConstant()) {
2606 size_t offset =
2607 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002608 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002609 } else {
2610 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002611 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002612 }
2613 break;
2614 }
2615
2616 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002617 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002618 if (index.IsConstant()) {
2619 size_t offset =
2620 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002621 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002622 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002623 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002624 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002625 }
2626 break;
2627 }
2628
2629 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002630 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002631 if (maybe_compressed_char_at) {
2632 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2633 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2634 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2635 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2636 "Expecting 0=compressed, 1=uncompressed");
2637 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002638 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002639 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2640 if (maybe_compressed_char_at) {
2641 MipsLabel uncompressed_load, done;
2642 __ Bnez(TMP, &uncompressed_load);
2643 __ LoadFromOffset(kLoadUnsignedByte,
2644 out,
2645 obj,
2646 data_offset + (const_index << TIMES_1));
2647 __ B(&done);
2648 __ Bind(&uncompressed_load);
2649 __ LoadFromOffset(kLoadUnsignedHalfword,
2650 out,
2651 obj,
2652 data_offset + (const_index << TIMES_2));
2653 __ Bind(&done);
2654 } else {
2655 __ LoadFromOffset(kLoadUnsignedHalfword,
2656 out,
2657 obj,
2658 data_offset + (const_index << TIMES_2),
2659 null_checker);
2660 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002661 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002662 Register index_reg = index.AsRegister<Register>();
2663 if (maybe_compressed_char_at) {
2664 MipsLabel uncompressed_load, done;
2665 __ Bnez(TMP, &uncompressed_load);
2666 __ Addu(TMP, obj, index_reg);
2667 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2668 __ B(&done);
2669 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002670 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002671 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2672 __ Bind(&done);
2673 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002674 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002675 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2676 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002677 }
2678 break;
2679 }
2680
Alexey Frunze15958152017-02-09 19:08:30 -08002681 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002682 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002683 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002684 if (index.IsConstant()) {
2685 size_t offset =
2686 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002687 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002688 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002689 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002690 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002691 }
2692 break;
2693 }
2694
Alexey Frunze15958152017-02-09 19:08:30 -08002695 case Primitive::kPrimNot: {
2696 static_assert(
2697 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2698 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2699 // /* HeapReference<Object> */ out =
2700 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2701 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002702 bool temp_needed = index.IsConstant()
2703 ? !kBakerReadBarrierThunksEnableForFields
2704 : !kBakerReadBarrierThunksEnableForArrays;
2705 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002706 // Note that a potential implicit null check is handled in this
2707 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002708 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2709 if (index.IsConstant()) {
2710 // Array load with a constant index can be treated as a field load.
2711 size_t offset =
2712 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2713 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2714 out_loc,
2715 obj,
2716 offset,
2717 temp,
2718 /* needs_null_check */ false);
2719 } else {
2720 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2721 out_loc,
2722 obj,
2723 data_offset,
2724 index,
2725 temp,
2726 /* needs_null_check */ false);
2727 }
Alexey Frunze15958152017-02-09 19:08:30 -08002728 } else {
2729 Register out = out_loc.AsRegister<Register>();
2730 if (index.IsConstant()) {
2731 size_t offset =
2732 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2733 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2734 // If read barriers are enabled, emit read barriers other than
2735 // Baker's using a slow path (and also unpoison the loaded
2736 // reference, if heap poisoning is enabled).
2737 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2738 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002739 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002740 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2741 // If read barriers are enabled, emit read barriers other than
2742 // Baker's using a slow path (and also unpoison the loaded
2743 // reference, if heap poisoning is enabled).
2744 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2745 out_loc,
2746 out_loc,
2747 obj_loc,
2748 data_offset,
2749 index);
2750 }
2751 }
2752 break;
2753 }
2754
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002755 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002756 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002757 if (index.IsConstant()) {
2758 size_t offset =
2759 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002760 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002761 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002762 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002763 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002764 }
2765 break;
2766 }
2767
2768 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002769 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002770 if (index.IsConstant()) {
2771 size_t offset =
2772 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002773 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002774 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002775 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002776 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002777 }
2778 break;
2779 }
2780
2781 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002782 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002783 if (index.IsConstant()) {
2784 size_t offset =
2785 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002786 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002787 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002788 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002789 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002790 }
2791 break;
2792 }
2793
2794 case Primitive::kPrimVoid:
2795 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2796 UNREACHABLE();
2797 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002798}
2799
2800void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2801 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2802 locations->SetInAt(0, Location::RequiresRegister());
2803 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2804}
2805
2806void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2807 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002808 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002809 Register obj = locations->InAt(0).AsRegister<Register>();
2810 Register out = locations->Out().AsRegister<Register>();
2811 __ LoadFromOffset(kLoadWord, out, obj, offset);
2812 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002813 // Mask out compression flag from String's array length.
2814 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2815 __ Srl(out, out, 1u);
2816 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002817}
2818
Alexey Frunzef58b2482016-09-02 22:14:06 -07002819Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2820 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2821 ? Location::ConstantLocation(instruction->AsConstant())
2822 : Location::RequiresRegister();
2823}
2824
2825Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2826 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2827 // We can store a non-zero float or double constant without first loading it into the FPU,
2828 // but we should only prefer this if the constant has a single use.
2829 if (instruction->IsConstant() &&
2830 (instruction->AsConstant()->IsZeroBitPattern() ||
2831 instruction->GetUses().HasExactlyOneElement())) {
2832 return Location::ConstantLocation(instruction->AsConstant());
2833 // Otherwise fall through and require an FPU register for the constant.
2834 }
2835 return Location::RequiresFpuRegister();
2836}
2837
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002838void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002839 Primitive::Type value_type = instruction->GetComponentType();
2840
2841 bool needs_write_barrier =
2842 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2843 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2844
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002845 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2846 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002847 may_need_runtime_call_for_type_check ?
2848 LocationSummary::kCallOnSlowPath :
2849 LocationSummary::kNoCall);
2850
2851 locations->SetInAt(0, Location::RequiresRegister());
2852 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2853 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2854 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002855 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002856 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2857 }
2858 if (needs_write_barrier) {
2859 // Temporary register for the write barrier.
2860 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002861 }
2862}
2863
2864void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2865 LocationSummary* locations = instruction->GetLocations();
2866 Register obj = locations->InAt(0).AsRegister<Register>();
2867 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002868 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002869 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002870 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002871 bool needs_write_barrier =
2872 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002873 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002874 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002875
2876 switch (value_type) {
2877 case Primitive::kPrimBoolean:
2878 case Primitive::kPrimByte: {
2879 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002880 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002881 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002882 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002883 __ Addu(base_reg, obj, index.AsRegister<Register>());
2884 }
2885 if (value_location.IsConstant()) {
2886 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2887 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2888 } else {
2889 Register value = value_location.AsRegister<Register>();
2890 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002891 }
2892 break;
2893 }
2894
2895 case Primitive::kPrimShort:
2896 case Primitive::kPrimChar: {
2897 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002898 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002899 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002900 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002901 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002902 }
2903 if (value_location.IsConstant()) {
2904 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2905 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2906 } else {
2907 Register value = value_location.AsRegister<Register>();
2908 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002909 }
2910 break;
2911 }
2912
Alexey Frunze15958152017-02-09 19:08:30 -08002913 case Primitive::kPrimInt: {
2914 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2915 if (index.IsConstant()) {
2916 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2917 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002918 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002919 }
2920 if (value_location.IsConstant()) {
2921 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2922 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2923 } else {
2924 Register value = value_location.AsRegister<Register>();
2925 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2926 }
2927 break;
2928 }
2929
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002930 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002931 if (value_location.IsConstant()) {
2932 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002933 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002934 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002935 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002936 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002937 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002938 }
Alexey Frunze15958152017-02-09 19:08:30 -08002939 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2940 DCHECK_EQ(value, 0);
2941 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2942 DCHECK(!needs_write_barrier);
2943 DCHECK(!may_need_runtime_call_for_type_check);
2944 break;
2945 }
2946
2947 DCHECK(needs_write_barrier);
2948 Register value = value_location.AsRegister<Register>();
2949 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2950 Register temp2 = TMP; // Doesn't need to survive slow path.
2951 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2952 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2953 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2954 MipsLabel done;
2955 SlowPathCodeMIPS* slow_path = nullptr;
2956
2957 if (may_need_runtime_call_for_type_check) {
2958 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2959 codegen_->AddSlowPath(slow_path);
2960 if (instruction->GetValueCanBeNull()) {
2961 MipsLabel non_zero;
2962 __ Bnez(value, &non_zero);
2963 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2964 if (index.IsConstant()) {
2965 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002966 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002967 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002968 }
Alexey Frunze15958152017-02-09 19:08:30 -08002969 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2970 __ B(&done);
2971 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002972 }
Alexey Frunze15958152017-02-09 19:08:30 -08002973
2974 // Note that when read barriers are enabled, the type checks
2975 // are performed without read barriers. This is fine, even in
2976 // the case where a class object is in the from-space after
2977 // the flip, as a comparison involving such a type would not
2978 // produce a false positive; it may of course produce a false
2979 // negative, in which case we would take the ArraySet slow
2980 // path.
2981
2982 // /* HeapReference<Class> */ temp1 = obj->klass_
2983 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2984 __ MaybeUnpoisonHeapReference(temp1);
2985
2986 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2987 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2988 // /* HeapReference<Class> */ temp2 = value->klass_
2989 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2990 // If heap poisoning is enabled, no need to unpoison `temp1`
2991 // nor `temp2`, as we are comparing two poisoned references.
2992
2993 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2994 MipsLabel do_put;
2995 __ Beq(temp1, temp2, &do_put);
2996 // If heap poisoning is enabled, the `temp1` reference has
2997 // not been unpoisoned yet; unpoison it now.
2998 __ MaybeUnpoisonHeapReference(temp1);
2999
3000 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3001 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3002 // If heap poisoning is enabled, no need to unpoison
3003 // `temp1`, as we are comparing against null below.
3004 __ Bnez(temp1, slow_path->GetEntryLabel());
3005 __ Bind(&do_put);
3006 } else {
3007 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3008 }
3009 }
3010
3011 Register source = value;
3012 if (kPoisonHeapReferences) {
3013 // Note that in the case where `value` is a null reference,
3014 // we do not enter this block, as a null reference does not
3015 // need poisoning.
3016 __ Move(temp1, value);
3017 __ PoisonHeapReference(temp1);
3018 source = temp1;
3019 }
3020
3021 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3022 if (index.IsConstant()) {
3023 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003024 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003025 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003026 }
3027 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3028
3029 if (!may_need_runtime_call_for_type_check) {
3030 codegen_->MaybeRecordImplicitNullCheck(instruction);
3031 }
3032
3033 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3034
3035 if (done.IsLinked()) {
3036 __ Bind(&done);
3037 }
3038
3039 if (slow_path != nullptr) {
3040 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003041 }
3042 break;
3043 }
3044
3045 case Primitive::kPrimLong: {
3046 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003047 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003048 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003049 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003050 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003051 }
3052 if (value_location.IsConstant()) {
3053 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3054 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3055 } else {
3056 Register value = value_location.AsRegisterPairLow<Register>();
3057 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003058 }
3059 break;
3060 }
3061
3062 case Primitive::kPrimFloat: {
3063 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003064 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003065 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003066 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003067 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003068 }
3069 if (value_location.IsConstant()) {
3070 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3071 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3072 } else {
3073 FRegister value = value_location.AsFpuRegister<FRegister>();
3074 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003075 }
3076 break;
3077 }
3078
3079 case Primitive::kPrimDouble: {
3080 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003081 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003082 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003083 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003084 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003085 }
3086 if (value_location.IsConstant()) {
3087 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3088 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3089 } else {
3090 FRegister value = value_location.AsFpuRegister<FRegister>();
3091 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003092 }
3093 break;
3094 }
3095
3096 case Primitive::kPrimVoid:
3097 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3098 UNREACHABLE();
3099 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003100}
3101
3102void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003103 RegisterSet caller_saves = RegisterSet::Empty();
3104 InvokeRuntimeCallingConvention calling_convention;
3105 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3106 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3107 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003108 locations->SetInAt(0, Location::RequiresRegister());
3109 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003110}
3111
3112void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3113 LocationSummary* locations = instruction->GetLocations();
3114 BoundsCheckSlowPathMIPS* slow_path =
3115 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3116 codegen_->AddSlowPath(slow_path);
3117
3118 Register index = locations->InAt(0).AsRegister<Register>();
3119 Register length = locations->InAt(1).AsRegister<Register>();
3120
3121 // length is limited by the maximum positive signed 32-bit integer.
3122 // Unsigned comparison of length and index checks for index < 0
3123 // and for length <= index simultaneously.
3124 __ Bgeu(index, length, slow_path->GetEntryLabel());
3125}
3126
Alexey Frunze15958152017-02-09 19:08:30 -08003127// Temp is used for read barrier.
3128static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3129 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003130 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003131 (kUseBakerReadBarrier ||
3132 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3133 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3134 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3135 return 1;
3136 }
3137 return 0;
3138}
3139
3140// Extra temp is used for read barrier.
3141static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3142 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3143}
3144
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003145void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003146 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3147 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3148
3149 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3150 switch (type_check_kind) {
3151 case TypeCheckKind::kExactCheck:
3152 case TypeCheckKind::kAbstractClassCheck:
3153 case TypeCheckKind::kClassHierarchyCheck:
3154 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003155 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003156 ? LocationSummary::kCallOnSlowPath
3157 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3158 break;
3159 case TypeCheckKind::kArrayCheck:
3160 case TypeCheckKind::kUnresolvedCheck:
3161 case TypeCheckKind::kInterfaceCheck:
3162 call_kind = LocationSummary::kCallOnSlowPath;
3163 break;
3164 }
3165
3166 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003167 locations->SetInAt(0, Location::RequiresRegister());
3168 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003169 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003170}
3171
3172void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003173 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003174 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003175 Location obj_loc = locations->InAt(0);
3176 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003177 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003178 Location temp_loc = locations->GetTemp(0);
3179 Register temp = temp_loc.AsRegister<Register>();
3180 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3181 DCHECK_LE(num_temps, 2u);
3182 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003183 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3184 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3185 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3186 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3187 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3188 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3189 const uint32_t object_array_data_offset =
3190 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3191 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003192
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003193 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3194 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3195 // read barriers is done for performance and code size reasons.
3196 bool is_type_check_slow_path_fatal = false;
3197 if (!kEmitCompilerReadBarrier) {
3198 is_type_check_slow_path_fatal =
3199 (type_check_kind == TypeCheckKind::kExactCheck ||
3200 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3201 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3202 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3203 !instruction->CanThrowIntoCatchBlock();
3204 }
3205 SlowPathCodeMIPS* slow_path =
3206 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3207 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003208 codegen_->AddSlowPath(slow_path);
3209
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003210 // Avoid this check if we know `obj` is not null.
3211 if (instruction->MustDoNullCheck()) {
3212 __ Beqz(obj, &done);
3213 }
3214
3215 switch (type_check_kind) {
3216 case TypeCheckKind::kExactCheck:
3217 case TypeCheckKind::kArrayCheck: {
3218 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003219 GenerateReferenceLoadTwoRegisters(instruction,
3220 temp_loc,
3221 obj_loc,
3222 class_offset,
3223 maybe_temp2_loc,
3224 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003225 // Jump to slow path for throwing the exception or doing a
3226 // more involved array check.
3227 __ Bne(temp, cls, slow_path->GetEntryLabel());
3228 break;
3229 }
3230
3231 case TypeCheckKind::kAbstractClassCheck: {
3232 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003233 GenerateReferenceLoadTwoRegisters(instruction,
3234 temp_loc,
3235 obj_loc,
3236 class_offset,
3237 maybe_temp2_loc,
3238 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003239 // If the class is abstract, we eagerly fetch the super class of the
3240 // object to avoid doing a comparison we know will fail.
3241 MipsLabel loop;
3242 __ Bind(&loop);
3243 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003244 GenerateReferenceLoadOneRegister(instruction,
3245 temp_loc,
3246 super_offset,
3247 maybe_temp2_loc,
3248 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003249 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3250 // exception.
3251 __ Beqz(temp, slow_path->GetEntryLabel());
3252 // Otherwise, compare the classes.
3253 __ Bne(temp, cls, &loop);
3254 break;
3255 }
3256
3257 case TypeCheckKind::kClassHierarchyCheck: {
3258 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003259 GenerateReferenceLoadTwoRegisters(instruction,
3260 temp_loc,
3261 obj_loc,
3262 class_offset,
3263 maybe_temp2_loc,
3264 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003265 // Walk over the class hierarchy to find a match.
3266 MipsLabel loop;
3267 __ Bind(&loop);
3268 __ Beq(temp, cls, &done);
3269 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003270 GenerateReferenceLoadOneRegister(instruction,
3271 temp_loc,
3272 super_offset,
3273 maybe_temp2_loc,
3274 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003275 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3276 // exception. Otherwise, jump to the beginning of the loop.
3277 __ Bnez(temp, &loop);
3278 __ B(slow_path->GetEntryLabel());
3279 break;
3280 }
3281
3282 case TypeCheckKind::kArrayObjectCheck: {
3283 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003284 GenerateReferenceLoadTwoRegisters(instruction,
3285 temp_loc,
3286 obj_loc,
3287 class_offset,
3288 maybe_temp2_loc,
3289 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003290 // Do an exact check.
3291 __ Beq(temp, cls, &done);
3292 // Otherwise, we need to check that the object's class is a non-primitive array.
3293 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003294 GenerateReferenceLoadOneRegister(instruction,
3295 temp_loc,
3296 component_offset,
3297 maybe_temp2_loc,
3298 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003299 // If the component type is null, jump to the slow path to throw the exception.
3300 __ Beqz(temp, slow_path->GetEntryLabel());
3301 // Otherwise, the object is indeed an array, further check that this component
3302 // type is not a primitive type.
3303 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3304 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3305 __ Bnez(temp, slow_path->GetEntryLabel());
3306 break;
3307 }
3308
3309 case TypeCheckKind::kUnresolvedCheck:
3310 // We always go into the type check slow path for the unresolved check case.
3311 // We cannot directly call the CheckCast runtime entry point
3312 // without resorting to a type checking slow path here (i.e. by
3313 // calling InvokeRuntime directly), as it would require to
3314 // assign fixed registers for the inputs of this HInstanceOf
3315 // instruction (following the runtime calling convention), which
3316 // might be cluttered by the potential first read barrier
3317 // emission at the beginning of this method.
3318 __ B(slow_path->GetEntryLabel());
3319 break;
3320
3321 case TypeCheckKind::kInterfaceCheck: {
3322 // Avoid read barriers to improve performance of the fast path. We can not get false
3323 // positives by doing this.
3324 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003325 GenerateReferenceLoadTwoRegisters(instruction,
3326 temp_loc,
3327 obj_loc,
3328 class_offset,
3329 maybe_temp2_loc,
3330 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003331 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003332 GenerateReferenceLoadTwoRegisters(instruction,
3333 temp_loc,
3334 temp_loc,
3335 iftable_offset,
3336 maybe_temp2_loc,
3337 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003338 // Iftable is never null.
3339 __ Lw(TMP, temp, array_length_offset);
3340 // Loop through the iftable and check if any class matches.
3341 MipsLabel loop;
3342 __ Bind(&loop);
3343 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3344 __ Beqz(TMP, slow_path->GetEntryLabel());
3345 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3346 __ MaybeUnpoisonHeapReference(AT);
3347 // Go to next interface.
3348 __ Addiu(TMP, TMP, -2);
3349 // Compare the classes and continue the loop if they do not match.
3350 __ Bne(AT, cls, &loop);
3351 break;
3352 }
3353 }
3354
3355 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003356 __ Bind(slow_path->GetExitLabel());
3357}
3358
3359void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3360 LocationSummary* locations =
3361 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3362 locations->SetInAt(0, Location::RequiresRegister());
3363 if (check->HasUses()) {
3364 locations->SetOut(Location::SameAsFirstInput());
3365 }
3366}
3367
3368void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3369 // We assume the class is not null.
3370 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3371 check->GetLoadClass(),
3372 check,
3373 check->GetDexPc(),
3374 true);
3375 codegen_->AddSlowPath(slow_path);
3376 GenerateClassInitializationCheck(slow_path,
3377 check->GetLocations()->InAt(0).AsRegister<Register>());
3378}
3379
3380void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3381 Primitive::Type in_type = compare->InputAt(0)->GetType();
3382
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003383 LocationSummary* locations =
3384 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003385
3386 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003387 case Primitive::kPrimBoolean:
3388 case Primitive::kPrimByte:
3389 case Primitive::kPrimShort:
3390 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003391 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003392 locations->SetInAt(0, Location::RequiresRegister());
3393 locations->SetInAt(1, Location::RequiresRegister());
3394 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3395 break;
3396
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003397 case Primitive::kPrimLong:
3398 locations->SetInAt(0, Location::RequiresRegister());
3399 locations->SetInAt(1, Location::RequiresRegister());
3400 // Output overlaps because it is written before doing the low comparison.
3401 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3402 break;
3403
3404 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003405 case Primitive::kPrimDouble:
3406 locations->SetInAt(0, Location::RequiresFpuRegister());
3407 locations->SetInAt(1, Location::RequiresFpuRegister());
3408 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003409 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003410
3411 default:
3412 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3413 }
3414}
3415
3416void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3417 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003418 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003419 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003420 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003421
3422 // 0 if: left == right
3423 // 1 if: left > right
3424 // -1 if: left < right
3425 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003426 case Primitive::kPrimBoolean:
3427 case Primitive::kPrimByte:
3428 case Primitive::kPrimShort:
3429 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003430 case Primitive::kPrimInt: {
3431 Register lhs = locations->InAt(0).AsRegister<Register>();
3432 Register rhs = locations->InAt(1).AsRegister<Register>();
3433 __ Slt(TMP, lhs, rhs);
3434 __ Slt(res, rhs, lhs);
3435 __ Subu(res, res, TMP);
3436 break;
3437 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003438 case Primitive::kPrimLong: {
3439 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003440 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3441 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3442 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3443 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3444 // TODO: more efficient (direct) comparison with a constant.
3445 __ Slt(TMP, lhs_high, rhs_high);
3446 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3447 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3448 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3449 __ Sltu(TMP, lhs_low, rhs_low);
3450 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3451 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3452 __ Bind(&done);
3453 break;
3454 }
3455
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003456 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003457 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003458 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3459 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3460 MipsLabel done;
3461 if (isR6) {
3462 __ CmpEqS(FTMP, lhs, rhs);
3463 __ LoadConst32(res, 0);
3464 __ Bc1nez(FTMP, &done);
3465 if (gt_bias) {
3466 __ CmpLtS(FTMP, lhs, rhs);
3467 __ LoadConst32(res, -1);
3468 __ Bc1nez(FTMP, &done);
3469 __ LoadConst32(res, 1);
3470 } else {
3471 __ CmpLtS(FTMP, rhs, lhs);
3472 __ LoadConst32(res, 1);
3473 __ Bc1nez(FTMP, &done);
3474 __ LoadConst32(res, -1);
3475 }
3476 } else {
3477 if (gt_bias) {
3478 __ ColtS(0, lhs, rhs);
3479 __ LoadConst32(res, -1);
3480 __ Bc1t(0, &done);
3481 __ CeqS(0, lhs, rhs);
3482 __ LoadConst32(res, 1);
3483 __ Movt(res, ZERO, 0);
3484 } else {
3485 __ ColtS(0, rhs, lhs);
3486 __ LoadConst32(res, 1);
3487 __ Bc1t(0, &done);
3488 __ CeqS(0, lhs, rhs);
3489 __ LoadConst32(res, -1);
3490 __ Movt(res, ZERO, 0);
3491 }
3492 }
3493 __ Bind(&done);
3494 break;
3495 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003496 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003497 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003498 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3499 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3500 MipsLabel done;
3501 if (isR6) {
3502 __ CmpEqD(FTMP, lhs, rhs);
3503 __ LoadConst32(res, 0);
3504 __ Bc1nez(FTMP, &done);
3505 if (gt_bias) {
3506 __ CmpLtD(FTMP, lhs, rhs);
3507 __ LoadConst32(res, -1);
3508 __ Bc1nez(FTMP, &done);
3509 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003510 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003511 __ CmpLtD(FTMP, rhs, lhs);
3512 __ LoadConst32(res, 1);
3513 __ Bc1nez(FTMP, &done);
3514 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003515 }
3516 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003517 if (gt_bias) {
3518 __ ColtD(0, lhs, rhs);
3519 __ LoadConst32(res, -1);
3520 __ Bc1t(0, &done);
3521 __ CeqD(0, lhs, rhs);
3522 __ LoadConst32(res, 1);
3523 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003524 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003525 __ ColtD(0, rhs, lhs);
3526 __ LoadConst32(res, 1);
3527 __ Bc1t(0, &done);
3528 __ CeqD(0, lhs, rhs);
3529 __ LoadConst32(res, -1);
3530 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003531 }
3532 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003533 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003534 break;
3535 }
3536
3537 default:
3538 LOG(FATAL) << "Unimplemented compare type " << in_type;
3539 }
3540}
3541
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003542void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003543 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003544 switch (instruction->InputAt(0)->GetType()) {
3545 default:
3546 case Primitive::kPrimLong:
3547 locations->SetInAt(0, Location::RequiresRegister());
3548 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3549 break;
3550
3551 case Primitive::kPrimFloat:
3552 case Primitive::kPrimDouble:
3553 locations->SetInAt(0, Location::RequiresFpuRegister());
3554 locations->SetInAt(1, Location::RequiresFpuRegister());
3555 break;
3556 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003557 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003558 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3559 }
3560}
3561
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003562void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003563 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003564 return;
3565 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003566
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003567 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003568 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003569
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003570 switch (type) {
3571 default:
3572 // Integer case.
3573 GenerateIntCompare(instruction->GetCondition(), locations);
3574 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003575
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003576 case Primitive::kPrimLong:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003577 GenerateLongCompare(instruction->GetCondition(), locations);
3578 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003579
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003580 case Primitive::kPrimFloat:
3581 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003582 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3583 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003584 }
3585}
3586
Alexey Frunze7e99e052015-11-24 19:28:01 -08003587void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3588 DCHECK(instruction->IsDiv() || instruction->IsRem());
3589 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3590
3591 LocationSummary* locations = instruction->GetLocations();
3592 Location second = locations->InAt(1);
3593 DCHECK(second.IsConstant());
3594
3595 Register out = locations->Out().AsRegister<Register>();
3596 Register dividend = locations->InAt(0).AsRegister<Register>();
3597 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3598 DCHECK(imm == 1 || imm == -1);
3599
3600 if (instruction->IsRem()) {
3601 __ Move(out, ZERO);
3602 } else {
3603 if (imm == -1) {
3604 __ Subu(out, ZERO, dividend);
3605 } else if (out != dividend) {
3606 __ Move(out, dividend);
3607 }
3608 }
3609}
3610
3611void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3612 DCHECK(instruction->IsDiv() || instruction->IsRem());
3613 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3614
3615 LocationSummary* locations = instruction->GetLocations();
3616 Location second = locations->InAt(1);
3617 DCHECK(second.IsConstant());
3618
3619 Register out = locations->Out().AsRegister<Register>();
3620 Register dividend = locations->InAt(0).AsRegister<Register>();
3621 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003622 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003623 int ctz_imm = CTZ(abs_imm);
3624
3625 if (instruction->IsDiv()) {
3626 if (ctz_imm == 1) {
3627 // Fast path for division by +/-2, which is very common.
3628 __ Srl(TMP, dividend, 31);
3629 } else {
3630 __ Sra(TMP, dividend, 31);
3631 __ Srl(TMP, TMP, 32 - ctz_imm);
3632 }
3633 __ Addu(out, dividend, TMP);
3634 __ Sra(out, out, ctz_imm);
3635 if (imm < 0) {
3636 __ Subu(out, ZERO, out);
3637 }
3638 } else {
3639 if (ctz_imm == 1) {
3640 // Fast path for modulo +/-2, which is very common.
3641 __ Sra(TMP, dividend, 31);
3642 __ Subu(out, dividend, TMP);
3643 __ Andi(out, out, 1);
3644 __ Addu(out, out, TMP);
3645 } else {
3646 __ Sra(TMP, dividend, 31);
3647 __ Srl(TMP, TMP, 32 - ctz_imm);
3648 __ Addu(out, dividend, TMP);
3649 if (IsUint<16>(abs_imm - 1)) {
3650 __ Andi(out, out, abs_imm - 1);
3651 } else {
3652 __ Sll(out, out, 32 - ctz_imm);
3653 __ Srl(out, out, 32 - ctz_imm);
3654 }
3655 __ Subu(out, out, TMP);
3656 }
3657 }
3658}
3659
3660void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3661 DCHECK(instruction->IsDiv() || instruction->IsRem());
3662 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3663
3664 LocationSummary* locations = instruction->GetLocations();
3665 Location second = locations->InAt(1);
3666 DCHECK(second.IsConstant());
3667
3668 Register out = locations->Out().AsRegister<Register>();
3669 Register dividend = locations->InAt(0).AsRegister<Register>();
3670 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3671
3672 int64_t magic;
3673 int shift;
3674 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3675
3676 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3677
3678 __ LoadConst32(TMP, magic);
3679 if (isR6) {
3680 __ MuhR6(TMP, dividend, TMP);
3681 } else {
3682 __ MultR2(dividend, TMP);
3683 __ Mfhi(TMP);
3684 }
3685 if (imm > 0 && magic < 0) {
3686 __ Addu(TMP, TMP, dividend);
3687 } else if (imm < 0 && magic > 0) {
3688 __ Subu(TMP, TMP, dividend);
3689 }
3690
3691 if (shift != 0) {
3692 __ Sra(TMP, TMP, shift);
3693 }
3694
3695 if (instruction->IsDiv()) {
3696 __ Sra(out, TMP, 31);
3697 __ Subu(out, TMP, out);
3698 } else {
3699 __ Sra(AT, TMP, 31);
3700 __ Subu(AT, TMP, AT);
3701 __ LoadConst32(TMP, imm);
3702 if (isR6) {
3703 __ MulR6(TMP, AT, TMP);
3704 } else {
3705 __ MulR2(TMP, AT, TMP);
3706 }
3707 __ Subu(out, dividend, TMP);
3708 }
3709}
3710
3711void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3712 DCHECK(instruction->IsDiv() || instruction->IsRem());
3713 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3714
3715 LocationSummary* locations = instruction->GetLocations();
3716 Register out = locations->Out().AsRegister<Register>();
3717 Location second = locations->InAt(1);
3718
3719 if (second.IsConstant()) {
3720 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3721 if (imm == 0) {
3722 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3723 } else if (imm == 1 || imm == -1) {
3724 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003725 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003726 DivRemByPowerOfTwo(instruction);
3727 } else {
3728 DCHECK(imm <= -2 || imm >= 2);
3729 GenerateDivRemWithAnyConstant(instruction);
3730 }
3731 } else {
3732 Register dividend = locations->InAt(0).AsRegister<Register>();
3733 Register divisor = second.AsRegister<Register>();
3734 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3735 if (instruction->IsDiv()) {
3736 if (isR6) {
3737 __ DivR6(out, dividend, divisor);
3738 } else {
3739 __ DivR2(out, dividend, divisor);
3740 }
3741 } else {
3742 if (isR6) {
3743 __ ModR6(out, dividend, divisor);
3744 } else {
3745 __ ModR2(out, dividend, divisor);
3746 }
3747 }
3748 }
3749}
3750
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003751void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3752 Primitive::Type type = div->GetResultType();
3753 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003754 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003755 : LocationSummary::kNoCall;
3756
3757 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3758
3759 switch (type) {
3760 case Primitive::kPrimInt:
3761 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003762 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003763 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3764 break;
3765
3766 case Primitive::kPrimLong: {
3767 InvokeRuntimeCallingConvention calling_convention;
3768 locations->SetInAt(0, Location::RegisterPairLocation(
3769 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3770 locations->SetInAt(1, Location::RegisterPairLocation(
3771 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3772 locations->SetOut(calling_convention.GetReturnLocation(type));
3773 break;
3774 }
3775
3776 case Primitive::kPrimFloat:
3777 case Primitive::kPrimDouble:
3778 locations->SetInAt(0, Location::RequiresFpuRegister());
3779 locations->SetInAt(1, Location::RequiresFpuRegister());
3780 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3781 break;
3782
3783 default:
3784 LOG(FATAL) << "Unexpected div type " << type;
3785 }
3786}
3787
3788void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3789 Primitive::Type type = instruction->GetType();
3790 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003791
3792 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003793 case Primitive::kPrimInt:
3794 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003795 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003796 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003797 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003798 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3799 break;
3800 }
3801 case Primitive::kPrimFloat:
3802 case Primitive::kPrimDouble: {
3803 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3804 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3805 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3806 if (type == Primitive::kPrimFloat) {
3807 __ DivS(dst, lhs, rhs);
3808 } else {
3809 __ DivD(dst, lhs, rhs);
3810 }
3811 break;
3812 }
3813 default:
3814 LOG(FATAL) << "Unexpected div type " << type;
3815 }
3816}
3817
3818void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003819 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003820 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003821}
3822
3823void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3824 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3825 codegen_->AddSlowPath(slow_path);
3826 Location value = instruction->GetLocations()->InAt(0);
3827 Primitive::Type type = instruction->GetType();
3828
3829 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003830 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003831 case Primitive::kPrimByte:
3832 case Primitive::kPrimChar:
3833 case Primitive::kPrimShort:
3834 case Primitive::kPrimInt: {
3835 if (value.IsConstant()) {
3836 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3837 __ B(slow_path->GetEntryLabel());
3838 } else {
3839 // A division by a non-null constant is valid. We don't need to perform
3840 // any check, so simply fall through.
3841 }
3842 } else {
3843 DCHECK(value.IsRegister()) << value;
3844 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3845 }
3846 break;
3847 }
3848 case Primitive::kPrimLong: {
3849 if (value.IsConstant()) {
3850 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3851 __ B(slow_path->GetEntryLabel());
3852 } else {
3853 // A division by a non-null constant is valid. We don't need to perform
3854 // any check, so simply fall through.
3855 }
3856 } else {
3857 DCHECK(value.IsRegisterPair()) << value;
3858 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3859 __ Beqz(TMP, slow_path->GetEntryLabel());
3860 }
3861 break;
3862 }
3863 default:
3864 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3865 }
3866}
3867
3868void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3869 LocationSummary* locations =
3870 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3871 locations->SetOut(Location::ConstantLocation(constant));
3872}
3873
3874void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3875 // Will be generated at use site.
3876}
3877
3878void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3879 exit->SetLocations(nullptr);
3880}
3881
3882void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3883}
3884
3885void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3886 LocationSummary* locations =
3887 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3888 locations->SetOut(Location::ConstantLocation(constant));
3889}
3890
3891void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3892 // Will be generated at use site.
3893}
3894
3895void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3896 got->SetLocations(nullptr);
3897}
3898
3899void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3900 DCHECK(!successor->IsExitBlock());
3901 HBasicBlock* block = got->GetBlock();
3902 HInstruction* previous = got->GetPrevious();
3903 HLoopInformation* info = block->GetLoopInformation();
3904
3905 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3906 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3907 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3908 return;
3909 }
3910 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3911 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3912 }
3913 if (!codegen_->GoesToNextBlock(block, successor)) {
3914 __ B(codegen_->GetLabelOf(successor));
3915 }
3916}
3917
3918void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3919 HandleGoto(got, got->GetSuccessor());
3920}
3921
3922void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3923 try_boundary->SetLocations(nullptr);
3924}
3925
3926void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3927 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3928 if (!successor->IsExitBlock()) {
3929 HandleGoto(try_boundary, successor);
3930 }
3931}
3932
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003933void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3934 LocationSummary* locations) {
3935 Register dst = locations->Out().AsRegister<Register>();
3936 Register lhs = locations->InAt(0).AsRegister<Register>();
3937 Location rhs_location = locations->InAt(1);
3938 Register rhs_reg = ZERO;
3939 int64_t rhs_imm = 0;
3940 bool use_imm = rhs_location.IsConstant();
3941 if (use_imm) {
3942 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3943 } else {
3944 rhs_reg = rhs_location.AsRegister<Register>();
3945 }
3946
3947 switch (cond) {
3948 case kCondEQ:
3949 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003950 if (use_imm && IsInt<16>(-rhs_imm)) {
3951 if (rhs_imm == 0) {
3952 if (cond == kCondEQ) {
3953 __ Sltiu(dst, lhs, 1);
3954 } else {
3955 __ Sltu(dst, ZERO, lhs);
3956 }
3957 } else {
3958 __ Addiu(dst, lhs, -rhs_imm);
3959 if (cond == kCondEQ) {
3960 __ Sltiu(dst, dst, 1);
3961 } else {
3962 __ Sltu(dst, ZERO, dst);
3963 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003964 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003965 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003966 if (use_imm && IsUint<16>(rhs_imm)) {
3967 __ Xori(dst, lhs, rhs_imm);
3968 } else {
3969 if (use_imm) {
3970 rhs_reg = TMP;
3971 __ LoadConst32(rhs_reg, rhs_imm);
3972 }
3973 __ Xor(dst, lhs, rhs_reg);
3974 }
3975 if (cond == kCondEQ) {
3976 __ Sltiu(dst, dst, 1);
3977 } else {
3978 __ Sltu(dst, ZERO, dst);
3979 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003980 }
3981 break;
3982
3983 case kCondLT:
3984 case kCondGE:
3985 if (use_imm && IsInt<16>(rhs_imm)) {
3986 __ Slti(dst, lhs, rhs_imm);
3987 } else {
3988 if (use_imm) {
3989 rhs_reg = TMP;
3990 __ LoadConst32(rhs_reg, rhs_imm);
3991 }
3992 __ Slt(dst, lhs, rhs_reg);
3993 }
3994 if (cond == kCondGE) {
3995 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3996 // only the slt instruction but no sge.
3997 __ Xori(dst, dst, 1);
3998 }
3999 break;
4000
4001 case kCondLE:
4002 case kCondGT:
4003 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4004 // Simulate lhs <= rhs via lhs < rhs + 1.
4005 __ Slti(dst, lhs, rhs_imm + 1);
4006 if (cond == kCondGT) {
4007 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4008 // only the slti instruction but no sgti.
4009 __ Xori(dst, dst, 1);
4010 }
4011 } else {
4012 if (use_imm) {
4013 rhs_reg = TMP;
4014 __ LoadConst32(rhs_reg, rhs_imm);
4015 }
4016 __ Slt(dst, rhs_reg, lhs);
4017 if (cond == kCondLE) {
4018 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4019 // only the slt instruction but no sle.
4020 __ Xori(dst, dst, 1);
4021 }
4022 }
4023 break;
4024
4025 case kCondB:
4026 case kCondAE:
4027 if (use_imm && IsInt<16>(rhs_imm)) {
4028 // Sltiu sign-extends its 16-bit immediate operand before
4029 // the comparison and thus lets us compare directly with
4030 // unsigned values in the ranges [0, 0x7fff] and
4031 // [0xffff8000, 0xffffffff].
4032 __ Sltiu(dst, lhs, rhs_imm);
4033 } else {
4034 if (use_imm) {
4035 rhs_reg = TMP;
4036 __ LoadConst32(rhs_reg, rhs_imm);
4037 }
4038 __ Sltu(dst, lhs, rhs_reg);
4039 }
4040 if (cond == kCondAE) {
4041 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4042 // only the sltu instruction but no sgeu.
4043 __ Xori(dst, dst, 1);
4044 }
4045 break;
4046
4047 case kCondBE:
4048 case kCondA:
4049 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4050 // Simulate lhs <= rhs via lhs < rhs + 1.
4051 // Note that this only works if rhs + 1 does not overflow
4052 // to 0, hence the check above.
4053 // Sltiu sign-extends its 16-bit immediate operand before
4054 // the comparison and thus lets us compare directly with
4055 // unsigned values in the ranges [0, 0x7fff] and
4056 // [0xffff8000, 0xffffffff].
4057 __ Sltiu(dst, lhs, rhs_imm + 1);
4058 if (cond == kCondA) {
4059 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4060 // only the sltiu instruction but no sgtiu.
4061 __ Xori(dst, dst, 1);
4062 }
4063 } else {
4064 if (use_imm) {
4065 rhs_reg = TMP;
4066 __ LoadConst32(rhs_reg, rhs_imm);
4067 }
4068 __ Sltu(dst, rhs_reg, lhs);
4069 if (cond == kCondBE) {
4070 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4071 // only the sltu instruction but no sleu.
4072 __ Xori(dst, dst, 1);
4073 }
4074 }
4075 break;
4076 }
4077}
4078
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004079bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4080 LocationSummary* input_locations,
4081 Register dst) {
4082 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4083 Location rhs_location = input_locations->InAt(1);
4084 Register rhs_reg = ZERO;
4085 int64_t rhs_imm = 0;
4086 bool use_imm = rhs_location.IsConstant();
4087 if (use_imm) {
4088 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4089 } else {
4090 rhs_reg = rhs_location.AsRegister<Register>();
4091 }
4092
4093 switch (cond) {
4094 case kCondEQ:
4095 case kCondNE:
4096 if (use_imm && IsInt<16>(-rhs_imm)) {
4097 __ Addiu(dst, lhs, -rhs_imm);
4098 } else if (use_imm && IsUint<16>(rhs_imm)) {
4099 __ Xori(dst, lhs, rhs_imm);
4100 } else {
4101 if (use_imm) {
4102 rhs_reg = TMP;
4103 __ LoadConst32(rhs_reg, rhs_imm);
4104 }
4105 __ Xor(dst, lhs, rhs_reg);
4106 }
4107 return (cond == kCondEQ);
4108
4109 case kCondLT:
4110 case kCondGE:
4111 if (use_imm && IsInt<16>(rhs_imm)) {
4112 __ Slti(dst, lhs, rhs_imm);
4113 } else {
4114 if (use_imm) {
4115 rhs_reg = TMP;
4116 __ LoadConst32(rhs_reg, rhs_imm);
4117 }
4118 __ Slt(dst, lhs, rhs_reg);
4119 }
4120 return (cond == kCondGE);
4121
4122 case kCondLE:
4123 case kCondGT:
4124 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4125 // Simulate lhs <= rhs via lhs < rhs + 1.
4126 __ Slti(dst, lhs, rhs_imm + 1);
4127 return (cond == kCondGT);
4128 } else {
4129 if (use_imm) {
4130 rhs_reg = TMP;
4131 __ LoadConst32(rhs_reg, rhs_imm);
4132 }
4133 __ Slt(dst, rhs_reg, lhs);
4134 return (cond == kCondLE);
4135 }
4136
4137 case kCondB:
4138 case kCondAE:
4139 if (use_imm && IsInt<16>(rhs_imm)) {
4140 // Sltiu sign-extends its 16-bit immediate operand before
4141 // the comparison and thus lets us compare directly with
4142 // unsigned values in the ranges [0, 0x7fff] and
4143 // [0xffff8000, 0xffffffff].
4144 __ Sltiu(dst, lhs, rhs_imm);
4145 } else {
4146 if (use_imm) {
4147 rhs_reg = TMP;
4148 __ LoadConst32(rhs_reg, rhs_imm);
4149 }
4150 __ Sltu(dst, lhs, rhs_reg);
4151 }
4152 return (cond == kCondAE);
4153
4154 case kCondBE:
4155 case kCondA:
4156 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4157 // Simulate lhs <= rhs via lhs < rhs + 1.
4158 // Note that this only works if rhs + 1 does not overflow
4159 // to 0, hence the check above.
4160 // Sltiu sign-extends its 16-bit immediate operand before
4161 // the comparison and thus lets us compare directly with
4162 // unsigned values in the ranges [0, 0x7fff] and
4163 // [0xffff8000, 0xffffffff].
4164 __ Sltiu(dst, lhs, rhs_imm + 1);
4165 return (cond == kCondA);
4166 } else {
4167 if (use_imm) {
4168 rhs_reg = TMP;
4169 __ LoadConst32(rhs_reg, rhs_imm);
4170 }
4171 __ Sltu(dst, rhs_reg, lhs);
4172 return (cond == kCondBE);
4173 }
4174 }
4175}
4176
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004177void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4178 LocationSummary* locations,
4179 MipsLabel* label) {
4180 Register lhs = locations->InAt(0).AsRegister<Register>();
4181 Location rhs_location = locations->InAt(1);
4182 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004183 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004184 bool use_imm = rhs_location.IsConstant();
4185 if (use_imm) {
4186 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4187 } else {
4188 rhs_reg = rhs_location.AsRegister<Register>();
4189 }
4190
4191 if (use_imm && rhs_imm == 0) {
4192 switch (cond) {
4193 case kCondEQ:
4194 case kCondBE: // <= 0 if zero
4195 __ Beqz(lhs, label);
4196 break;
4197 case kCondNE:
4198 case kCondA: // > 0 if non-zero
4199 __ Bnez(lhs, label);
4200 break;
4201 case kCondLT:
4202 __ Bltz(lhs, label);
4203 break;
4204 case kCondGE:
4205 __ Bgez(lhs, label);
4206 break;
4207 case kCondLE:
4208 __ Blez(lhs, label);
4209 break;
4210 case kCondGT:
4211 __ Bgtz(lhs, label);
4212 break;
4213 case kCondB: // always false
4214 break;
4215 case kCondAE: // always true
4216 __ B(label);
4217 break;
4218 }
4219 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004220 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4221 if (isR6 || !use_imm) {
4222 if (use_imm) {
4223 rhs_reg = TMP;
4224 __ LoadConst32(rhs_reg, rhs_imm);
4225 }
4226 switch (cond) {
4227 case kCondEQ:
4228 __ Beq(lhs, rhs_reg, label);
4229 break;
4230 case kCondNE:
4231 __ Bne(lhs, rhs_reg, label);
4232 break;
4233 case kCondLT:
4234 __ Blt(lhs, rhs_reg, label);
4235 break;
4236 case kCondGE:
4237 __ Bge(lhs, rhs_reg, label);
4238 break;
4239 case kCondLE:
4240 __ Bge(rhs_reg, lhs, label);
4241 break;
4242 case kCondGT:
4243 __ Blt(rhs_reg, lhs, label);
4244 break;
4245 case kCondB:
4246 __ Bltu(lhs, rhs_reg, label);
4247 break;
4248 case kCondAE:
4249 __ Bgeu(lhs, rhs_reg, label);
4250 break;
4251 case kCondBE:
4252 __ Bgeu(rhs_reg, lhs, label);
4253 break;
4254 case kCondA:
4255 __ Bltu(rhs_reg, lhs, label);
4256 break;
4257 }
4258 } else {
4259 // Special cases for more efficient comparison with constants on R2.
4260 switch (cond) {
4261 case kCondEQ:
4262 __ LoadConst32(TMP, rhs_imm);
4263 __ Beq(lhs, TMP, label);
4264 break;
4265 case kCondNE:
4266 __ LoadConst32(TMP, rhs_imm);
4267 __ Bne(lhs, TMP, label);
4268 break;
4269 case kCondLT:
4270 if (IsInt<16>(rhs_imm)) {
4271 __ Slti(TMP, lhs, rhs_imm);
4272 __ Bnez(TMP, label);
4273 } else {
4274 __ LoadConst32(TMP, rhs_imm);
4275 __ Blt(lhs, TMP, label);
4276 }
4277 break;
4278 case kCondGE:
4279 if (IsInt<16>(rhs_imm)) {
4280 __ Slti(TMP, lhs, rhs_imm);
4281 __ Beqz(TMP, label);
4282 } else {
4283 __ LoadConst32(TMP, rhs_imm);
4284 __ Bge(lhs, TMP, label);
4285 }
4286 break;
4287 case kCondLE:
4288 if (IsInt<16>(rhs_imm + 1)) {
4289 // Simulate lhs <= rhs via lhs < rhs + 1.
4290 __ Slti(TMP, lhs, rhs_imm + 1);
4291 __ Bnez(TMP, label);
4292 } else {
4293 __ LoadConst32(TMP, rhs_imm);
4294 __ Bge(TMP, lhs, label);
4295 }
4296 break;
4297 case kCondGT:
4298 if (IsInt<16>(rhs_imm + 1)) {
4299 // Simulate lhs > rhs via !(lhs < rhs + 1).
4300 __ Slti(TMP, lhs, rhs_imm + 1);
4301 __ Beqz(TMP, label);
4302 } else {
4303 __ LoadConst32(TMP, rhs_imm);
4304 __ Blt(TMP, lhs, label);
4305 }
4306 break;
4307 case kCondB:
4308 if (IsInt<16>(rhs_imm)) {
4309 __ Sltiu(TMP, lhs, rhs_imm);
4310 __ Bnez(TMP, label);
4311 } else {
4312 __ LoadConst32(TMP, rhs_imm);
4313 __ Bltu(lhs, TMP, label);
4314 }
4315 break;
4316 case kCondAE:
4317 if (IsInt<16>(rhs_imm)) {
4318 __ Sltiu(TMP, lhs, rhs_imm);
4319 __ Beqz(TMP, label);
4320 } else {
4321 __ LoadConst32(TMP, rhs_imm);
4322 __ Bgeu(lhs, TMP, label);
4323 }
4324 break;
4325 case kCondBE:
4326 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4327 // Simulate lhs <= rhs via lhs < rhs + 1.
4328 // Note that this only works if rhs + 1 does not overflow
4329 // to 0, hence the check above.
4330 __ Sltiu(TMP, lhs, rhs_imm + 1);
4331 __ Bnez(TMP, label);
4332 } else {
4333 __ LoadConst32(TMP, rhs_imm);
4334 __ Bgeu(TMP, lhs, label);
4335 }
4336 break;
4337 case kCondA:
4338 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4339 // Simulate lhs > rhs via !(lhs < rhs + 1).
4340 // Note that this only works if rhs + 1 does not overflow
4341 // to 0, hence the check above.
4342 __ Sltiu(TMP, lhs, rhs_imm + 1);
4343 __ Beqz(TMP, label);
4344 } else {
4345 __ LoadConst32(TMP, rhs_imm);
4346 __ Bltu(TMP, lhs, label);
4347 }
4348 break;
4349 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004350 }
4351 }
4352}
4353
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004354void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4355 LocationSummary* locations) {
4356 Register dst = locations->Out().AsRegister<Register>();
4357 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4358 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4359 Location rhs_location = locations->InAt(1);
4360 Register rhs_high = ZERO;
4361 Register rhs_low = ZERO;
4362 int64_t imm = 0;
4363 uint32_t imm_high = 0;
4364 uint32_t imm_low = 0;
4365 bool use_imm = rhs_location.IsConstant();
4366 if (use_imm) {
4367 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4368 imm_high = High32Bits(imm);
4369 imm_low = Low32Bits(imm);
4370 } else {
4371 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4372 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4373 }
4374 if (use_imm && imm == 0) {
4375 switch (cond) {
4376 case kCondEQ:
4377 case kCondBE: // <= 0 if zero
4378 __ Or(dst, lhs_high, lhs_low);
4379 __ Sltiu(dst, dst, 1);
4380 break;
4381 case kCondNE:
4382 case kCondA: // > 0 if non-zero
4383 __ Or(dst, lhs_high, lhs_low);
4384 __ Sltu(dst, ZERO, dst);
4385 break;
4386 case kCondLT:
4387 __ Slt(dst, lhs_high, ZERO);
4388 break;
4389 case kCondGE:
4390 __ Slt(dst, lhs_high, ZERO);
4391 __ Xori(dst, dst, 1);
4392 break;
4393 case kCondLE:
4394 __ Or(TMP, lhs_high, lhs_low);
4395 __ Sra(AT, lhs_high, 31);
4396 __ Sltu(dst, AT, TMP);
4397 __ Xori(dst, dst, 1);
4398 break;
4399 case kCondGT:
4400 __ Or(TMP, lhs_high, lhs_low);
4401 __ Sra(AT, lhs_high, 31);
4402 __ Sltu(dst, AT, TMP);
4403 break;
4404 case kCondB: // always false
4405 __ Andi(dst, dst, 0);
4406 break;
4407 case kCondAE: // always true
4408 __ Ori(dst, ZERO, 1);
4409 break;
4410 }
4411 } else if (use_imm) {
4412 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4413 switch (cond) {
4414 case kCondEQ:
4415 __ LoadConst32(TMP, imm_high);
4416 __ Xor(TMP, TMP, lhs_high);
4417 __ LoadConst32(AT, imm_low);
4418 __ Xor(AT, AT, lhs_low);
4419 __ Or(dst, TMP, AT);
4420 __ Sltiu(dst, dst, 1);
4421 break;
4422 case kCondNE:
4423 __ LoadConst32(TMP, imm_high);
4424 __ Xor(TMP, TMP, lhs_high);
4425 __ LoadConst32(AT, imm_low);
4426 __ Xor(AT, AT, lhs_low);
4427 __ Or(dst, TMP, AT);
4428 __ Sltu(dst, ZERO, dst);
4429 break;
4430 case kCondLT:
4431 case kCondGE:
4432 if (dst == lhs_low) {
4433 __ LoadConst32(TMP, imm_low);
4434 __ Sltu(dst, lhs_low, TMP);
4435 }
4436 __ LoadConst32(TMP, imm_high);
4437 __ Slt(AT, lhs_high, TMP);
4438 __ Slt(TMP, TMP, lhs_high);
4439 if (dst != lhs_low) {
4440 __ LoadConst32(dst, imm_low);
4441 __ Sltu(dst, lhs_low, dst);
4442 }
4443 __ Slt(dst, TMP, dst);
4444 __ Or(dst, dst, AT);
4445 if (cond == kCondGE) {
4446 __ Xori(dst, dst, 1);
4447 }
4448 break;
4449 case kCondGT:
4450 case kCondLE:
4451 if (dst == lhs_low) {
4452 __ LoadConst32(TMP, imm_low);
4453 __ Sltu(dst, TMP, lhs_low);
4454 }
4455 __ LoadConst32(TMP, imm_high);
4456 __ Slt(AT, TMP, lhs_high);
4457 __ Slt(TMP, lhs_high, TMP);
4458 if (dst != lhs_low) {
4459 __ LoadConst32(dst, imm_low);
4460 __ Sltu(dst, dst, lhs_low);
4461 }
4462 __ Slt(dst, TMP, dst);
4463 __ Or(dst, dst, AT);
4464 if (cond == kCondLE) {
4465 __ Xori(dst, dst, 1);
4466 }
4467 break;
4468 case kCondB:
4469 case kCondAE:
4470 if (dst == lhs_low) {
4471 __ LoadConst32(TMP, imm_low);
4472 __ Sltu(dst, lhs_low, TMP);
4473 }
4474 __ LoadConst32(TMP, imm_high);
4475 __ Sltu(AT, lhs_high, TMP);
4476 __ Sltu(TMP, TMP, lhs_high);
4477 if (dst != lhs_low) {
4478 __ LoadConst32(dst, imm_low);
4479 __ Sltu(dst, lhs_low, dst);
4480 }
4481 __ Slt(dst, TMP, dst);
4482 __ Or(dst, dst, AT);
4483 if (cond == kCondAE) {
4484 __ Xori(dst, dst, 1);
4485 }
4486 break;
4487 case kCondA:
4488 case kCondBE:
4489 if (dst == lhs_low) {
4490 __ LoadConst32(TMP, imm_low);
4491 __ Sltu(dst, TMP, lhs_low);
4492 }
4493 __ LoadConst32(TMP, imm_high);
4494 __ Sltu(AT, TMP, lhs_high);
4495 __ Sltu(TMP, lhs_high, TMP);
4496 if (dst != lhs_low) {
4497 __ LoadConst32(dst, imm_low);
4498 __ Sltu(dst, dst, lhs_low);
4499 }
4500 __ Slt(dst, TMP, dst);
4501 __ Or(dst, dst, AT);
4502 if (cond == kCondBE) {
4503 __ Xori(dst, dst, 1);
4504 }
4505 break;
4506 }
4507 } else {
4508 switch (cond) {
4509 case kCondEQ:
4510 __ Xor(TMP, lhs_high, rhs_high);
4511 __ Xor(AT, lhs_low, rhs_low);
4512 __ Or(dst, TMP, AT);
4513 __ Sltiu(dst, dst, 1);
4514 break;
4515 case kCondNE:
4516 __ Xor(TMP, lhs_high, rhs_high);
4517 __ Xor(AT, lhs_low, rhs_low);
4518 __ Or(dst, TMP, AT);
4519 __ Sltu(dst, ZERO, dst);
4520 break;
4521 case kCondLT:
4522 case kCondGE:
4523 __ Slt(TMP, rhs_high, lhs_high);
4524 __ Sltu(AT, lhs_low, rhs_low);
4525 __ Slt(TMP, TMP, AT);
4526 __ Slt(AT, lhs_high, rhs_high);
4527 __ Or(dst, AT, TMP);
4528 if (cond == kCondGE) {
4529 __ Xori(dst, dst, 1);
4530 }
4531 break;
4532 case kCondGT:
4533 case kCondLE:
4534 __ Slt(TMP, lhs_high, rhs_high);
4535 __ Sltu(AT, rhs_low, lhs_low);
4536 __ Slt(TMP, TMP, AT);
4537 __ Slt(AT, rhs_high, lhs_high);
4538 __ Or(dst, AT, TMP);
4539 if (cond == kCondLE) {
4540 __ Xori(dst, dst, 1);
4541 }
4542 break;
4543 case kCondB:
4544 case kCondAE:
4545 __ Sltu(TMP, rhs_high, lhs_high);
4546 __ Sltu(AT, lhs_low, rhs_low);
4547 __ Slt(TMP, TMP, AT);
4548 __ Sltu(AT, lhs_high, rhs_high);
4549 __ Or(dst, AT, TMP);
4550 if (cond == kCondAE) {
4551 __ Xori(dst, dst, 1);
4552 }
4553 break;
4554 case kCondA:
4555 case kCondBE:
4556 __ Sltu(TMP, lhs_high, rhs_high);
4557 __ Sltu(AT, rhs_low, lhs_low);
4558 __ Slt(TMP, TMP, AT);
4559 __ Sltu(AT, rhs_high, lhs_high);
4560 __ Or(dst, AT, TMP);
4561 if (cond == kCondBE) {
4562 __ Xori(dst, dst, 1);
4563 }
4564 break;
4565 }
4566 }
4567}
4568
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004569void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4570 LocationSummary* locations,
4571 MipsLabel* label) {
4572 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4573 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4574 Location rhs_location = locations->InAt(1);
4575 Register rhs_high = ZERO;
4576 Register rhs_low = ZERO;
4577 int64_t imm = 0;
4578 uint32_t imm_high = 0;
4579 uint32_t imm_low = 0;
4580 bool use_imm = rhs_location.IsConstant();
4581 if (use_imm) {
4582 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4583 imm_high = High32Bits(imm);
4584 imm_low = Low32Bits(imm);
4585 } else {
4586 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4587 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4588 }
4589
4590 if (use_imm && imm == 0) {
4591 switch (cond) {
4592 case kCondEQ:
4593 case kCondBE: // <= 0 if zero
4594 __ Or(TMP, lhs_high, lhs_low);
4595 __ Beqz(TMP, label);
4596 break;
4597 case kCondNE:
4598 case kCondA: // > 0 if non-zero
4599 __ Or(TMP, lhs_high, lhs_low);
4600 __ Bnez(TMP, label);
4601 break;
4602 case kCondLT:
4603 __ Bltz(lhs_high, label);
4604 break;
4605 case kCondGE:
4606 __ Bgez(lhs_high, label);
4607 break;
4608 case kCondLE:
4609 __ Or(TMP, lhs_high, lhs_low);
4610 __ Sra(AT, lhs_high, 31);
4611 __ Bgeu(AT, TMP, label);
4612 break;
4613 case kCondGT:
4614 __ Or(TMP, lhs_high, lhs_low);
4615 __ Sra(AT, lhs_high, 31);
4616 __ Bltu(AT, TMP, label);
4617 break;
4618 case kCondB: // always false
4619 break;
4620 case kCondAE: // always true
4621 __ B(label);
4622 break;
4623 }
4624 } else if (use_imm) {
4625 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4626 switch (cond) {
4627 case kCondEQ:
4628 __ LoadConst32(TMP, imm_high);
4629 __ Xor(TMP, TMP, lhs_high);
4630 __ LoadConst32(AT, imm_low);
4631 __ Xor(AT, AT, lhs_low);
4632 __ Or(TMP, TMP, AT);
4633 __ Beqz(TMP, label);
4634 break;
4635 case kCondNE:
4636 __ LoadConst32(TMP, imm_high);
4637 __ Xor(TMP, TMP, lhs_high);
4638 __ LoadConst32(AT, imm_low);
4639 __ Xor(AT, AT, lhs_low);
4640 __ Or(TMP, TMP, AT);
4641 __ Bnez(TMP, label);
4642 break;
4643 case kCondLT:
4644 __ LoadConst32(TMP, imm_high);
4645 __ Blt(lhs_high, TMP, label);
4646 __ Slt(TMP, TMP, lhs_high);
4647 __ LoadConst32(AT, imm_low);
4648 __ Sltu(AT, lhs_low, AT);
4649 __ Blt(TMP, AT, label);
4650 break;
4651 case kCondGE:
4652 __ LoadConst32(TMP, imm_high);
4653 __ Blt(TMP, lhs_high, label);
4654 __ Slt(TMP, lhs_high, TMP);
4655 __ LoadConst32(AT, imm_low);
4656 __ Sltu(AT, lhs_low, AT);
4657 __ Or(TMP, TMP, AT);
4658 __ Beqz(TMP, label);
4659 break;
4660 case kCondLE:
4661 __ LoadConst32(TMP, imm_high);
4662 __ Blt(lhs_high, TMP, label);
4663 __ Slt(TMP, TMP, lhs_high);
4664 __ LoadConst32(AT, imm_low);
4665 __ Sltu(AT, AT, lhs_low);
4666 __ Or(TMP, TMP, AT);
4667 __ Beqz(TMP, label);
4668 break;
4669 case kCondGT:
4670 __ LoadConst32(TMP, imm_high);
4671 __ Blt(TMP, lhs_high, label);
4672 __ Slt(TMP, lhs_high, TMP);
4673 __ LoadConst32(AT, imm_low);
4674 __ Sltu(AT, AT, lhs_low);
4675 __ Blt(TMP, AT, label);
4676 break;
4677 case kCondB:
4678 __ LoadConst32(TMP, imm_high);
4679 __ Bltu(lhs_high, TMP, label);
4680 __ Sltu(TMP, TMP, lhs_high);
4681 __ LoadConst32(AT, imm_low);
4682 __ Sltu(AT, lhs_low, AT);
4683 __ Blt(TMP, AT, label);
4684 break;
4685 case kCondAE:
4686 __ LoadConst32(TMP, imm_high);
4687 __ Bltu(TMP, lhs_high, label);
4688 __ Sltu(TMP, lhs_high, TMP);
4689 __ LoadConst32(AT, imm_low);
4690 __ Sltu(AT, lhs_low, AT);
4691 __ Or(TMP, TMP, AT);
4692 __ Beqz(TMP, label);
4693 break;
4694 case kCondBE:
4695 __ LoadConst32(TMP, imm_high);
4696 __ Bltu(lhs_high, TMP, label);
4697 __ Sltu(TMP, TMP, lhs_high);
4698 __ LoadConst32(AT, imm_low);
4699 __ Sltu(AT, AT, lhs_low);
4700 __ Or(TMP, TMP, AT);
4701 __ Beqz(TMP, label);
4702 break;
4703 case kCondA:
4704 __ LoadConst32(TMP, imm_high);
4705 __ Bltu(TMP, lhs_high, label);
4706 __ Sltu(TMP, lhs_high, TMP);
4707 __ LoadConst32(AT, imm_low);
4708 __ Sltu(AT, AT, lhs_low);
4709 __ Blt(TMP, AT, label);
4710 break;
4711 }
4712 } else {
4713 switch (cond) {
4714 case kCondEQ:
4715 __ Xor(TMP, lhs_high, rhs_high);
4716 __ Xor(AT, lhs_low, rhs_low);
4717 __ Or(TMP, TMP, AT);
4718 __ Beqz(TMP, label);
4719 break;
4720 case kCondNE:
4721 __ Xor(TMP, lhs_high, rhs_high);
4722 __ Xor(AT, lhs_low, rhs_low);
4723 __ Or(TMP, TMP, AT);
4724 __ Bnez(TMP, label);
4725 break;
4726 case kCondLT:
4727 __ Blt(lhs_high, rhs_high, label);
4728 __ Slt(TMP, rhs_high, lhs_high);
4729 __ Sltu(AT, lhs_low, rhs_low);
4730 __ Blt(TMP, AT, label);
4731 break;
4732 case kCondGE:
4733 __ Blt(rhs_high, lhs_high, label);
4734 __ Slt(TMP, lhs_high, rhs_high);
4735 __ Sltu(AT, lhs_low, rhs_low);
4736 __ Or(TMP, TMP, AT);
4737 __ Beqz(TMP, label);
4738 break;
4739 case kCondLE:
4740 __ Blt(lhs_high, rhs_high, label);
4741 __ Slt(TMP, rhs_high, lhs_high);
4742 __ Sltu(AT, rhs_low, lhs_low);
4743 __ Or(TMP, TMP, AT);
4744 __ Beqz(TMP, label);
4745 break;
4746 case kCondGT:
4747 __ Blt(rhs_high, lhs_high, label);
4748 __ Slt(TMP, lhs_high, rhs_high);
4749 __ Sltu(AT, rhs_low, lhs_low);
4750 __ Blt(TMP, AT, label);
4751 break;
4752 case kCondB:
4753 __ Bltu(lhs_high, rhs_high, label);
4754 __ Sltu(TMP, rhs_high, lhs_high);
4755 __ Sltu(AT, lhs_low, rhs_low);
4756 __ Blt(TMP, AT, label);
4757 break;
4758 case kCondAE:
4759 __ Bltu(rhs_high, lhs_high, label);
4760 __ Sltu(TMP, lhs_high, rhs_high);
4761 __ Sltu(AT, lhs_low, rhs_low);
4762 __ Or(TMP, TMP, AT);
4763 __ Beqz(TMP, label);
4764 break;
4765 case kCondBE:
4766 __ Bltu(lhs_high, rhs_high, label);
4767 __ Sltu(TMP, rhs_high, lhs_high);
4768 __ Sltu(AT, rhs_low, lhs_low);
4769 __ Or(TMP, TMP, AT);
4770 __ Beqz(TMP, label);
4771 break;
4772 case kCondA:
4773 __ Bltu(rhs_high, lhs_high, label);
4774 __ Sltu(TMP, lhs_high, rhs_high);
4775 __ Sltu(AT, rhs_low, lhs_low);
4776 __ Blt(TMP, AT, label);
4777 break;
4778 }
4779 }
4780}
4781
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004782void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4783 bool gt_bias,
4784 Primitive::Type type,
4785 LocationSummary* locations) {
4786 Register dst = locations->Out().AsRegister<Register>();
4787 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4788 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4789 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4790 if (type == Primitive::kPrimFloat) {
4791 if (isR6) {
4792 switch (cond) {
4793 case kCondEQ:
4794 __ CmpEqS(FTMP, lhs, rhs);
4795 __ Mfc1(dst, FTMP);
4796 __ Andi(dst, dst, 1);
4797 break;
4798 case kCondNE:
4799 __ CmpEqS(FTMP, lhs, rhs);
4800 __ Mfc1(dst, FTMP);
4801 __ Addiu(dst, dst, 1);
4802 break;
4803 case kCondLT:
4804 if (gt_bias) {
4805 __ CmpLtS(FTMP, lhs, rhs);
4806 } else {
4807 __ CmpUltS(FTMP, lhs, rhs);
4808 }
4809 __ Mfc1(dst, FTMP);
4810 __ Andi(dst, dst, 1);
4811 break;
4812 case kCondLE:
4813 if (gt_bias) {
4814 __ CmpLeS(FTMP, lhs, rhs);
4815 } else {
4816 __ CmpUleS(FTMP, lhs, rhs);
4817 }
4818 __ Mfc1(dst, FTMP);
4819 __ Andi(dst, dst, 1);
4820 break;
4821 case kCondGT:
4822 if (gt_bias) {
4823 __ CmpUltS(FTMP, rhs, lhs);
4824 } else {
4825 __ CmpLtS(FTMP, rhs, lhs);
4826 }
4827 __ Mfc1(dst, FTMP);
4828 __ Andi(dst, dst, 1);
4829 break;
4830 case kCondGE:
4831 if (gt_bias) {
4832 __ CmpUleS(FTMP, rhs, lhs);
4833 } else {
4834 __ CmpLeS(FTMP, rhs, lhs);
4835 }
4836 __ Mfc1(dst, FTMP);
4837 __ Andi(dst, dst, 1);
4838 break;
4839 default:
4840 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4841 UNREACHABLE();
4842 }
4843 } else {
4844 switch (cond) {
4845 case kCondEQ:
4846 __ CeqS(0, lhs, rhs);
4847 __ LoadConst32(dst, 1);
4848 __ Movf(dst, ZERO, 0);
4849 break;
4850 case kCondNE:
4851 __ CeqS(0, lhs, rhs);
4852 __ LoadConst32(dst, 1);
4853 __ Movt(dst, ZERO, 0);
4854 break;
4855 case kCondLT:
4856 if (gt_bias) {
4857 __ ColtS(0, lhs, rhs);
4858 } else {
4859 __ CultS(0, lhs, rhs);
4860 }
4861 __ LoadConst32(dst, 1);
4862 __ Movf(dst, ZERO, 0);
4863 break;
4864 case kCondLE:
4865 if (gt_bias) {
4866 __ ColeS(0, lhs, rhs);
4867 } else {
4868 __ CuleS(0, lhs, rhs);
4869 }
4870 __ LoadConst32(dst, 1);
4871 __ Movf(dst, ZERO, 0);
4872 break;
4873 case kCondGT:
4874 if (gt_bias) {
4875 __ CultS(0, rhs, lhs);
4876 } else {
4877 __ ColtS(0, rhs, lhs);
4878 }
4879 __ LoadConst32(dst, 1);
4880 __ Movf(dst, ZERO, 0);
4881 break;
4882 case kCondGE:
4883 if (gt_bias) {
4884 __ CuleS(0, rhs, lhs);
4885 } else {
4886 __ ColeS(0, rhs, lhs);
4887 }
4888 __ LoadConst32(dst, 1);
4889 __ Movf(dst, ZERO, 0);
4890 break;
4891 default:
4892 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4893 UNREACHABLE();
4894 }
4895 }
4896 } else {
4897 DCHECK_EQ(type, Primitive::kPrimDouble);
4898 if (isR6) {
4899 switch (cond) {
4900 case kCondEQ:
4901 __ CmpEqD(FTMP, lhs, rhs);
4902 __ Mfc1(dst, FTMP);
4903 __ Andi(dst, dst, 1);
4904 break;
4905 case kCondNE:
4906 __ CmpEqD(FTMP, lhs, rhs);
4907 __ Mfc1(dst, FTMP);
4908 __ Addiu(dst, dst, 1);
4909 break;
4910 case kCondLT:
4911 if (gt_bias) {
4912 __ CmpLtD(FTMP, lhs, rhs);
4913 } else {
4914 __ CmpUltD(FTMP, lhs, rhs);
4915 }
4916 __ Mfc1(dst, FTMP);
4917 __ Andi(dst, dst, 1);
4918 break;
4919 case kCondLE:
4920 if (gt_bias) {
4921 __ CmpLeD(FTMP, lhs, rhs);
4922 } else {
4923 __ CmpUleD(FTMP, lhs, rhs);
4924 }
4925 __ Mfc1(dst, FTMP);
4926 __ Andi(dst, dst, 1);
4927 break;
4928 case kCondGT:
4929 if (gt_bias) {
4930 __ CmpUltD(FTMP, rhs, lhs);
4931 } else {
4932 __ CmpLtD(FTMP, rhs, lhs);
4933 }
4934 __ Mfc1(dst, FTMP);
4935 __ Andi(dst, dst, 1);
4936 break;
4937 case kCondGE:
4938 if (gt_bias) {
4939 __ CmpUleD(FTMP, rhs, lhs);
4940 } else {
4941 __ CmpLeD(FTMP, rhs, lhs);
4942 }
4943 __ Mfc1(dst, FTMP);
4944 __ Andi(dst, dst, 1);
4945 break;
4946 default:
4947 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4948 UNREACHABLE();
4949 }
4950 } else {
4951 switch (cond) {
4952 case kCondEQ:
4953 __ CeqD(0, lhs, rhs);
4954 __ LoadConst32(dst, 1);
4955 __ Movf(dst, ZERO, 0);
4956 break;
4957 case kCondNE:
4958 __ CeqD(0, lhs, rhs);
4959 __ LoadConst32(dst, 1);
4960 __ Movt(dst, ZERO, 0);
4961 break;
4962 case kCondLT:
4963 if (gt_bias) {
4964 __ ColtD(0, lhs, rhs);
4965 } else {
4966 __ CultD(0, lhs, rhs);
4967 }
4968 __ LoadConst32(dst, 1);
4969 __ Movf(dst, ZERO, 0);
4970 break;
4971 case kCondLE:
4972 if (gt_bias) {
4973 __ ColeD(0, lhs, rhs);
4974 } else {
4975 __ CuleD(0, lhs, rhs);
4976 }
4977 __ LoadConst32(dst, 1);
4978 __ Movf(dst, ZERO, 0);
4979 break;
4980 case kCondGT:
4981 if (gt_bias) {
4982 __ CultD(0, rhs, lhs);
4983 } else {
4984 __ ColtD(0, rhs, lhs);
4985 }
4986 __ LoadConst32(dst, 1);
4987 __ Movf(dst, ZERO, 0);
4988 break;
4989 case kCondGE:
4990 if (gt_bias) {
4991 __ CuleD(0, rhs, lhs);
4992 } else {
4993 __ ColeD(0, rhs, lhs);
4994 }
4995 __ LoadConst32(dst, 1);
4996 __ Movf(dst, ZERO, 0);
4997 break;
4998 default:
4999 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5000 UNREACHABLE();
5001 }
5002 }
5003 }
5004}
5005
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005006bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5007 bool gt_bias,
5008 Primitive::Type type,
5009 LocationSummary* input_locations,
5010 int cc) {
5011 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5012 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5013 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5014 if (type == Primitive::kPrimFloat) {
5015 switch (cond) {
5016 case kCondEQ:
5017 __ CeqS(cc, lhs, rhs);
5018 return false;
5019 case kCondNE:
5020 __ CeqS(cc, lhs, rhs);
5021 return true;
5022 case kCondLT:
5023 if (gt_bias) {
5024 __ ColtS(cc, lhs, rhs);
5025 } else {
5026 __ CultS(cc, lhs, rhs);
5027 }
5028 return false;
5029 case kCondLE:
5030 if (gt_bias) {
5031 __ ColeS(cc, lhs, rhs);
5032 } else {
5033 __ CuleS(cc, lhs, rhs);
5034 }
5035 return false;
5036 case kCondGT:
5037 if (gt_bias) {
5038 __ CultS(cc, rhs, lhs);
5039 } else {
5040 __ ColtS(cc, rhs, lhs);
5041 }
5042 return false;
5043 case kCondGE:
5044 if (gt_bias) {
5045 __ CuleS(cc, rhs, lhs);
5046 } else {
5047 __ ColeS(cc, rhs, lhs);
5048 }
5049 return false;
5050 default:
5051 LOG(FATAL) << "Unexpected non-floating-point condition";
5052 UNREACHABLE();
5053 }
5054 } else {
5055 DCHECK_EQ(type, Primitive::kPrimDouble);
5056 switch (cond) {
5057 case kCondEQ:
5058 __ CeqD(cc, lhs, rhs);
5059 return false;
5060 case kCondNE:
5061 __ CeqD(cc, lhs, rhs);
5062 return true;
5063 case kCondLT:
5064 if (gt_bias) {
5065 __ ColtD(cc, lhs, rhs);
5066 } else {
5067 __ CultD(cc, lhs, rhs);
5068 }
5069 return false;
5070 case kCondLE:
5071 if (gt_bias) {
5072 __ ColeD(cc, lhs, rhs);
5073 } else {
5074 __ CuleD(cc, lhs, rhs);
5075 }
5076 return false;
5077 case kCondGT:
5078 if (gt_bias) {
5079 __ CultD(cc, rhs, lhs);
5080 } else {
5081 __ ColtD(cc, rhs, lhs);
5082 }
5083 return false;
5084 case kCondGE:
5085 if (gt_bias) {
5086 __ CuleD(cc, rhs, lhs);
5087 } else {
5088 __ ColeD(cc, rhs, lhs);
5089 }
5090 return false;
5091 default:
5092 LOG(FATAL) << "Unexpected non-floating-point condition";
5093 UNREACHABLE();
5094 }
5095 }
5096}
5097
5098bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5099 bool gt_bias,
5100 Primitive::Type type,
5101 LocationSummary* input_locations,
5102 FRegister dst) {
5103 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5104 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5105 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
5106 if (type == Primitive::kPrimFloat) {
5107 switch (cond) {
5108 case kCondEQ:
5109 __ CmpEqS(dst, lhs, rhs);
5110 return false;
5111 case kCondNE:
5112 __ CmpEqS(dst, lhs, rhs);
5113 return true;
5114 case kCondLT:
5115 if (gt_bias) {
5116 __ CmpLtS(dst, lhs, rhs);
5117 } else {
5118 __ CmpUltS(dst, lhs, rhs);
5119 }
5120 return false;
5121 case kCondLE:
5122 if (gt_bias) {
5123 __ CmpLeS(dst, lhs, rhs);
5124 } else {
5125 __ CmpUleS(dst, lhs, rhs);
5126 }
5127 return false;
5128 case kCondGT:
5129 if (gt_bias) {
5130 __ CmpUltS(dst, rhs, lhs);
5131 } else {
5132 __ CmpLtS(dst, rhs, lhs);
5133 }
5134 return false;
5135 case kCondGE:
5136 if (gt_bias) {
5137 __ CmpUleS(dst, rhs, lhs);
5138 } else {
5139 __ CmpLeS(dst, rhs, lhs);
5140 }
5141 return false;
5142 default:
5143 LOG(FATAL) << "Unexpected non-floating-point condition";
5144 UNREACHABLE();
5145 }
5146 } else {
5147 DCHECK_EQ(type, Primitive::kPrimDouble);
5148 switch (cond) {
5149 case kCondEQ:
5150 __ CmpEqD(dst, lhs, rhs);
5151 return false;
5152 case kCondNE:
5153 __ CmpEqD(dst, lhs, rhs);
5154 return true;
5155 case kCondLT:
5156 if (gt_bias) {
5157 __ CmpLtD(dst, lhs, rhs);
5158 } else {
5159 __ CmpUltD(dst, lhs, rhs);
5160 }
5161 return false;
5162 case kCondLE:
5163 if (gt_bias) {
5164 __ CmpLeD(dst, lhs, rhs);
5165 } else {
5166 __ CmpUleD(dst, lhs, rhs);
5167 }
5168 return false;
5169 case kCondGT:
5170 if (gt_bias) {
5171 __ CmpUltD(dst, rhs, lhs);
5172 } else {
5173 __ CmpLtD(dst, rhs, lhs);
5174 }
5175 return false;
5176 case kCondGE:
5177 if (gt_bias) {
5178 __ CmpUleD(dst, rhs, lhs);
5179 } else {
5180 __ CmpLeD(dst, rhs, lhs);
5181 }
5182 return false;
5183 default:
5184 LOG(FATAL) << "Unexpected non-floating-point condition";
5185 UNREACHABLE();
5186 }
5187 }
5188}
5189
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005190void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5191 bool gt_bias,
5192 Primitive::Type type,
5193 LocationSummary* locations,
5194 MipsLabel* label) {
5195 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5196 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5197 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5198 if (type == Primitive::kPrimFloat) {
5199 if (isR6) {
5200 switch (cond) {
5201 case kCondEQ:
5202 __ CmpEqS(FTMP, lhs, rhs);
5203 __ Bc1nez(FTMP, label);
5204 break;
5205 case kCondNE:
5206 __ CmpEqS(FTMP, lhs, rhs);
5207 __ Bc1eqz(FTMP, label);
5208 break;
5209 case kCondLT:
5210 if (gt_bias) {
5211 __ CmpLtS(FTMP, lhs, rhs);
5212 } else {
5213 __ CmpUltS(FTMP, lhs, rhs);
5214 }
5215 __ Bc1nez(FTMP, label);
5216 break;
5217 case kCondLE:
5218 if (gt_bias) {
5219 __ CmpLeS(FTMP, lhs, rhs);
5220 } else {
5221 __ CmpUleS(FTMP, lhs, rhs);
5222 }
5223 __ Bc1nez(FTMP, label);
5224 break;
5225 case kCondGT:
5226 if (gt_bias) {
5227 __ CmpUltS(FTMP, rhs, lhs);
5228 } else {
5229 __ CmpLtS(FTMP, rhs, lhs);
5230 }
5231 __ Bc1nez(FTMP, label);
5232 break;
5233 case kCondGE:
5234 if (gt_bias) {
5235 __ CmpUleS(FTMP, rhs, lhs);
5236 } else {
5237 __ CmpLeS(FTMP, rhs, lhs);
5238 }
5239 __ Bc1nez(FTMP, label);
5240 break;
5241 default:
5242 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005243 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005244 }
5245 } else {
5246 switch (cond) {
5247 case kCondEQ:
5248 __ CeqS(0, lhs, rhs);
5249 __ Bc1t(0, label);
5250 break;
5251 case kCondNE:
5252 __ CeqS(0, lhs, rhs);
5253 __ Bc1f(0, label);
5254 break;
5255 case kCondLT:
5256 if (gt_bias) {
5257 __ ColtS(0, lhs, rhs);
5258 } else {
5259 __ CultS(0, lhs, rhs);
5260 }
5261 __ Bc1t(0, label);
5262 break;
5263 case kCondLE:
5264 if (gt_bias) {
5265 __ ColeS(0, lhs, rhs);
5266 } else {
5267 __ CuleS(0, lhs, rhs);
5268 }
5269 __ Bc1t(0, label);
5270 break;
5271 case kCondGT:
5272 if (gt_bias) {
5273 __ CultS(0, rhs, lhs);
5274 } else {
5275 __ ColtS(0, rhs, lhs);
5276 }
5277 __ Bc1t(0, label);
5278 break;
5279 case kCondGE:
5280 if (gt_bias) {
5281 __ CuleS(0, rhs, lhs);
5282 } else {
5283 __ ColeS(0, rhs, lhs);
5284 }
5285 __ Bc1t(0, label);
5286 break;
5287 default:
5288 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005289 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005290 }
5291 }
5292 } else {
5293 DCHECK_EQ(type, Primitive::kPrimDouble);
5294 if (isR6) {
5295 switch (cond) {
5296 case kCondEQ:
5297 __ CmpEqD(FTMP, lhs, rhs);
5298 __ Bc1nez(FTMP, label);
5299 break;
5300 case kCondNE:
5301 __ CmpEqD(FTMP, lhs, rhs);
5302 __ Bc1eqz(FTMP, label);
5303 break;
5304 case kCondLT:
5305 if (gt_bias) {
5306 __ CmpLtD(FTMP, lhs, rhs);
5307 } else {
5308 __ CmpUltD(FTMP, lhs, rhs);
5309 }
5310 __ Bc1nez(FTMP, label);
5311 break;
5312 case kCondLE:
5313 if (gt_bias) {
5314 __ CmpLeD(FTMP, lhs, rhs);
5315 } else {
5316 __ CmpUleD(FTMP, lhs, rhs);
5317 }
5318 __ Bc1nez(FTMP, label);
5319 break;
5320 case kCondGT:
5321 if (gt_bias) {
5322 __ CmpUltD(FTMP, rhs, lhs);
5323 } else {
5324 __ CmpLtD(FTMP, rhs, lhs);
5325 }
5326 __ Bc1nez(FTMP, label);
5327 break;
5328 case kCondGE:
5329 if (gt_bias) {
5330 __ CmpUleD(FTMP, rhs, lhs);
5331 } else {
5332 __ CmpLeD(FTMP, rhs, lhs);
5333 }
5334 __ Bc1nez(FTMP, label);
5335 break;
5336 default:
5337 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005338 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005339 }
5340 } else {
5341 switch (cond) {
5342 case kCondEQ:
5343 __ CeqD(0, lhs, rhs);
5344 __ Bc1t(0, label);
5345 break;
5346 case kCondNE:
5347 __ CeqD(0, lhs, rhs);
5348 __ Bc1f(0, label);
5349 break;
5350 case kCondLT:
5351 if (gt_bias) {
5352 __ ColtD(0, lhs, rhs);
5353 } else {
5354 __ CultD(0, lhs, rhs);
5355 }
5356 __ Bc1t(0, label);
5357 break;
5358 case kCondLE:
5359 if (gt_bias) {
5360 __ ColeD(0, lhs, rhs);
5361 } else {
5362 __ CuleD(0, lhs, rhs);
5363 }
5364 __ Bc1t(0, label);
5365 break;
5366 case kCondGT:
5367 if (gt_bias) {
5368 __ CultD(0, rhs, lhs);
5369 } else {
5370 __ ColtD(0, rhs, lhs);
5371 }
5372 __ Bc1t(0, label);
5373 break;
5374 case kCondGE:
5375 if (gt_bias) {
5376 __ CuleD(0, rhs, lhs);
5377 } else {
5378 __ ColeD(0, rhs, lhs);
5379 }
5380 __ Bc1t(0, label);
5381 break;
5382 default:
5383 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005384 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005385 }
5386 }
5387 }
5388}
5389
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005390void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005391 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005392 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005393 MipsLabel* false_target) {
5394 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005395
David Brazdil0debae72015-11-12 18:37:00 +00005396 if (true_target == nullptr && false_target == nullptr) {
5397 // Nothing to do. The code always falls through.
5398 return;
5399 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005400 // Constant condition, statically compared against "true" (integer value 1).
5401 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005402 if (true_target != nullptr) {
5403 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005404 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005405 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005406 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005407 if (false_target != nullptr) {
5408 __ B(false_target);
5409 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005410 }
David Brazdil0debae72015-11-12 18:37:00 +00005411 return;
5412 }
5413
5414 // The following code generates these patterns:
5415 // (1) true_target == nullptr && false_target != nullptr
5416 // - opposite condition true => branch to false_target
5417 // (2) true_target != nullptr && false_target == nullptr
5418 // - condition true => branch to true_target
5419 // (3) true_target != nullptr && false_target != nullptr
5420 // - condition true => branch to true_target
5421 // - branch to false_target
5422 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005423 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005424 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005425 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005426 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005427 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5428 } else {
5429 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5430 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005431 } else {
5432 // The condition instruction has not been materialized, use its inputs as
5433 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005434 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005435 Primitive::Type type = condition->InputAt(0)->GetType();
5436 LocationSummary* locations = cond->GetLocations();
5437 IfCondition if_cond = condition->GetCondition();
5438 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005439
David Brazdil0debae72015-11-12 18:37:00 +00005440 if (true_target == nullptr) {
5441 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005442 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005443 }
5444
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005445 switch (type) {
5446 default:
5447 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5448 break;
5449 case Primitive::kPrimLong:
5450 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5451 break;
5452 case Primitive::kPrimFloat:
5453 case Primitive::kPrimDouble:
5454 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5455 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005456 }
5457 }
David Brazdil0debae72015-11-12 18:37:00 +00005458
5459 // If neither branch falls through (case 3), the conditional branch to `true_target`
5460 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5461 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005462 __ B(false_target);
5463 }
5464}
5465
5466void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5467 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005468 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005469 locations->SetInAt(0, Location::RequiresRegister());
5470 }
5471}
5472
5473void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005474 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5475 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5476 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5477 nullptr : codegen_->GetLabelOf(true_successor);
5478 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5479 nullptr : codegen_->GetLabelOf(false_successor);
5480 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005481}
5482
5483void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5484 LocationSummary* locations = new (GetGraph()->GetArena())
5485 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005486 InvokeRuntimeCallingConvention calling_convention;
5487 RegisterSet caller_saves = RegisterSet::Empty();
5488 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5489 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005490 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005491 locations->SetInAt(0, Location::RequiresRegister());
5492 }
5493}
5494
5495void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005496 SlowPathCodeMIPS* slow_path =
5497 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005498 GenerateTestAndBranch(deoptimize,
5499 /* condition_input_index */ 0,
5500 slow_path->GetEntryLabel(),
5501 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005502}
5503
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005504// This function returns true if a conditional move can be generated for HSelect.
5505// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5506// branches and regular moves.
5507//
5508// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5509//
5510// While determining feasibility of a conditional move and setting inputs/outputs
5511// are two distinct tasks, this function does both because they share quite a bit
5512// of common logic.
5513static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5514 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5515 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5516 HCondition* condition = cond->AsCondition();
5517
5518 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5519 Primitive::Type dst_type = select->GetType();
5520
5521 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5522 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5523 bool is_true_value_zero_constant =
5524 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5525 bool is_false_value_zero_constant =
5526 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5527
5528 bool can_move_conditionally = false;
5529 bool use_const_for_false_in = false;
5530 bool use_const_for_true_in = false;
5531
5532 if (!cond->IsConstant()) {
5533 switch (cond_type) {
5534 default:
5535 switch (dst_type) {
5536 default:
5537 // Moving int on int condition.
5538 if (is_r6) {
5539 if (is_true_value_zero_constant) {
5540 // seleqz out_reg, false_reg, cond_reg
5541 can_move_conditionally = true;
5542 use_const_for_true_in = true;
5543 } else if (is_false_value_zero_constant) {
5544 // selnez out_reg, true_reg, cond_reg
5545 can_move_conditionally = true;
5546 use_const_for_false_in = true;
5547 } else if (materialized) {
5548 // Not materializing unmaterialized int conditions
5549 // to keep the instruction count low.
5550 // selnez AT, true_reg, cond_reg
5551 // seleqz TMP, false_reg, cond_reg
5552 // or out_reg, AT, TMP
5553 can_move_conditionally = true;
5554 }
5555 } else {
5556 // movn out_reg, true_reg/ZERO, cond_reg
5557 can_move_conditionally = true;
5558 use_const_for_true_in = is_true_value_zero_constant;
5559 }
5560 break;
5561 case Primitive::kPrimLong:
5562 // Moving long on int condition.
5563 if (is_r6) {
5564 if (is_true_value_zero_constant) {
5565 // seleqz out_reg_lo, false_reg_lo, cond_reg
5566 // seleqz out_reg_hi, false_reg_hi, cond_reg
5567 can_move_conditionally = true;
5568 use_const_for_true_in = true;
5569 } else if (is_false_value_zero_constant) {
5570 // selnez out_reg_lo, true_reg_lo, cond_reg
5571 // selnez out_reg_hi, true_reg_hi, cond_reg
5572 can_move_conditionally = true;
5573 use_const_for_false_in = true;
5574 }
5575 // Other long conditional moves would generate 6+ instructions,
5576 // which is too many.
5577 } else {
5578 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5579 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5580 can_move_conditionally = true;
5581 use_const_for_true_in = is_true_value_zero_constant;
5582 }
5583 break;
5584 case Primitive::kPrimFloat:
5585 case Primitive::kPrimDouble:
5586 // Moving float/double on int condition.
5587 if (is_r6) {
5588 if (materialized) {
5589 // Not materializing unmaterialized int conditions
5590 // to keep the instruction count low.
5591 can_move_conditionally = true;
5592 if (is_true_value_zero_constant) {
5593 // sltu TMP, ZERO, cond_reg
5594 // mtc1 TMP, temp_cond_reg
5595 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5596 use_const_for_true_in = true;
5597 } else if (is_false_value_zero_constant) {
5598 // sltu TMP, ZERO, cond_reg
5599 // mtc1 TMP, temp_cond_reg
5600 // selnez.fmt out_reg, true_reg, temp_cond_reg
5601 use_const_for_false_in = true;
5602 } else {
5603 // sltu TMP, ZERO, cond_reg
5604 // mtc1 TMP, temp_cond_reg
5605 // sel.fmt temp_cond_reg, false_reg, true_reg
5606 // mov.fmt out_reg, temp_cond_reg
5607 }
5608 }
5609 } else {
5610 // movn.fmt out_reg, true_reg, cond_reg
5611 can_move_conditionally = true;
5612 }
5613 break;
5614 }
5615 break;
5616 case Primitive::kPrimLong:
5617 // We don't materialize long comparison now
5618 // and use conditional branches instead.
5619 break;
5620 case Primitive::kPrimFloat:
5621 case Primitive::kPrimDouble:
5622 switch (dst_type) {
5623 default:
5624 // Moving int on float/double condition.
5625 if (is_r6) {
5626 if (is_true_value_zero_constant) {
5627 // mfc1 TMP, temp_cond_reg
5628 // seleqz out_reg, false_reg, TMP
5629 can_move_conditionally = true;
5630 use_const_for_true_in = true;
5631 } else if (is_false_value_zero_constant) {
5632 // mfc1 TMP, temp_cond_reg
5633 // selnez out_reg, true_reg, TMP
5634 can_move_conditionally = true;
5635 use_const_for_false_in = true;
5636 } else {
5637 // mfc1 TMP, temp_cond_reg
5638 // selnez AT, true_reg, TMP
5639 // seleqz TMP, false_reg, TMP
5640 // or out_reg, AT, TMP
5641 can_move_conditionally = true;
5642 }
5643 } else {
5644 // movt out_reg, true_reg/ZERO, cc
5645 can_move_conditionally = true;
5646 use_const_for_true_in = is_true_value_zero_constant;
5647 }
5648 break;
5649 case Primitive::kPrimLong:
5650 // Moving long on float/double condition.
5651 if (is_r6) {
5652 if (is_true_value_zero_constant) {
5653 // mfc1 TMP, temp_cond_reg
5654 // seleqz out_reg_lo, false_reg_lo, TMP
5655 // seleqz out_reg_hi, false_reg_hi, TMP
5656 can_move_conditionally = true;
5657 use_const_for_true_in = true;
5658 } else if (is_false_value_zero_constant) {
5659 // mfc1 TMP, temp_cond_reg
5660 // selnez out_reg_lo, true_reg_lo, TMP
5661 // selnez out_reg_hi, true_reg_hi, TMP
5662 can_move_conditionally = true;
5663 use_const_for_false_in = true;
5664 }
5665 // Other long conditional moves would generate 6+ instructions,
5666 // which is too many.
5667 } else {
5668 // movt out_reg_lo, true_reg_lo/ZERO, cc
5669 // movt out_reg_hi, true_reg_hi/ZERO, cc
5670 can_move_conditionally = true;
5671 use_const_for_true_in = is_true_value_zero_constant;
5672 }
5673 break;
5674 case Primitive::kPrimFloat:
5675 case Primitive::kPrimDouble:
5676 // Moving float/double on float/double condition.
5677 if (is_r6) {
5678 can_move_conditionally = true;
5679 if (is_true_value_zero_constant) {
5680 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5681 use_const_for_true_in = true;
5682 } else if (is_false_value_zero_constant) {
5683 // selnez.fmt out_reg, true_reg, temp_cond_reg
5684 use_const_for_false_in = true;
5685 } else {
5686 // sel.fmt temp_cond_reg, false_reg, true_reg
5687 // mov.fmt out_reg, temp_cond_reg
5688 }
5689 } else {
5690 // movt.fmt out_reg, true_reg, cc
5691 can_move_conditionally = true;
5692 }
5693 break;
5694 }
5695 break;
5696 }
5697 }
5698
5699 if (can_move_conditionally) {
5700 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5701 } else {
5702 DCHECK(!use_const_for_false_in);
5703 DCHECK(!use_const_for_true_in);
5704 }
5705
5706 if (locations_to_set != nullptr) {
5707 if (use_const_for_false_in) {
5708 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5709 } else {
5710 locations_to_set->SetInAt(0,
5711 Primitive::IsFloatingPointType(dst_type)
5712 ? Location::RequiresFpuRegister()
5713 : Location::RequiresRegister());
5714 }
5715 if (use_const_for_true_in) {
5716 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5717 } else {
5718 locations_to_set->SetInAt(1,
5719 Primitive::IsFloatingPointType(dst_type)
5720 ? Location::RequiresFpuRegister()
5721 : Location::RequiresRegister());
5722 }
5723 if (materialized) {
5724 locations_to_set->SetInAt(2, Location::RequiresRegister());
5725 }
5726 // On R6 we don't require the output to be the same as the
5727 // first input for conditional moves unlike on R2.
5728 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5729 if (is_out_same_as_first_in) {
5730 locations_to_set->SetOut(Location::SameAsFirstInput());
5731 } else {
5732 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5733 ? Location::RequiresFpuRegister()
5734 : Location::RequiresRegister());
5735 }
5736 }
5737
5738 return can_move_conditionally;
5739}
5740
5741void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5742 LocationSummary* locations = select->GetLocations();
5743 Location dst = locations->Out();
5744 Location src = locations->InAt(1);
5745 Register src_reg = ZERO;
5746 Register src_reg_high = ZERO;
5747 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5748 Register cond_reg = TMP;
5749 int cond_cc = 0;
5750 Primitive::Type cond_type = Primitive::kPrimInt;
5751 bool cond_inverted = false;
5752 Primitive::Type dst_type = select->GetType();
5753
5754 if (IsBooleanValueOrMaterializedCondition(cond)) {
5755 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5756 } else {
5757 HCondition* condition = cond->AsCondition();
5758 LocationSummary* cond_locations = cond->GetLocations();
5759 IfCondition if_cond = condition->GetCondition();
5760 cond_type = condition->InputAt(0)->GetType();
5761 switch (cond_type) {
5762 default:
5763 DCHECK_NE(cond_type, Primitive::kPrimLong);
5764 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5765 break;
5766 case Primitive::kPrimFloat:
5767 case Primitive::kPrimDouble:
5768 cond_inverted = MaterializeFpCompareR2(if_cond,
5769 condition->IsGtBias(),
5770 cond_type,
5771 cond_locations,
5772 cond_cc);
5773 break;
5774 }
5775 }
5776
5777 DCHECK(dst.Equals(locations->InAt(0)));
5778 if (src.IsRegister()) {
5779 src_reg = src.AsRegister<Register>();
5780 } else if (src.IsRegisterPair()) {
5781 src_reg = src.AsRegisterPairLow<Register>();
5782 src_reg_high = src.AsRegisterPairHigh<Register>();
5783 } else if (src.IsConstant()) {
5784 DCHECK(src.GetConstant()->IsZeroBitPattern());
5785 }
5786
5787 switch (cond_type) {
5788 default:
5789 switch (dst_type) {
5790 default:
5791 if (cond_inverted) {
5792 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5793 } else {
5794 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5795 }
5796 break;
5797 case Primitive::kPrimLong:
5798 if (cond_inverted) {
5799 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5800 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5801 } else {
5802 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5803 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5804 }
5805 break;
5806 case Primitive::kPrimFloat:
5807 if (cond_inverted) {
5808 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5809 } else {
5810 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5811 }
5812 break;
5813 case Primitive::kPrimDouble:
5814 if (cond_inverted) {
5815 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5816 } else {
5817 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5818 }
5819 break;
5820 }
5821 break;
5822 case Primitive::kPrimLong:
5823 LOG(FATAL) << "Unreachable";
5824 UNREACHABLE();
5825 case Primitive::kPrimFloat:
5826 case Primitive::kPrimDouble:
5827 switch (dst_type) {
5828 default:
5829 if (cond_inverted) {
5830 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5831 } else {
5832 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5833 }
5834 break;
5835 case Primitive::kPrimLong:
5836 if (cond_inverted) {
5837 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5838 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5839 } else {
5840 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5841 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5842 }
5843 break;
5844 case Primitive::kPrimFloat:
5845 if (cond_inverted) {
5846 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5847 } else {
5848 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5849 }
5850 break;
5851 case Primitive::kPrimDouble:
5852 if (cond_inverted) {
5853 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5854 } else {
5855 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5856 }
5857 break;
5858 }
5859 break;
5860 }
5861}
5862
5863void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5864 LocationSummary* locations = select->GetLocations();
5865 Location dst = locations->Out();
5866 Location false_src = locations->InAt(0);
5867 Location true_src = locations->InAt(1);
5868 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5869 Register cond_reg = TMP;
5870 FRegister fcond_reg = FTMP;
5871 Primitive::Type cond_type = Primitive::kPrimInt;
5872 bool cond_inverted = false;
5873 Primitive::Type dst_type = select->GetType();
5874
5875 if (IsBooleanValueOrMaterializedCondition(cond)) {
5876 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5877 } else {
5878 HCondition* condition = cond->AsCondition();
5879 LocationSummary* cond_locations = cond->GetLocations();
5880 IfCondition if_cond = condition->GetCondition();
5881 cond_type = condition->InputAt(0)->GetType();
5882 switch (cond_type) {
5883 default:
5884 DCHECK_NE(cond_type, Primitive::kPrimLong);
5885 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5886 break;
5887 case Primitive::kPrimFloat:
5888 case Primitive::kPrimDouble:
5889 cond_inverted = MaterializeFpCompareR6(if_cond,
5890 condition->IsGtBias(),
5891 cond_type,
5892 cond_locations,
5893 fcond_reg);
5894 break;
5895 }
5896 }
5897
5898 if (true_src.IsConstant()) {
5899 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5900 }
5901 if (false_src.IsConstant()) {
5902 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5903 }
5904
5905 switch (dst_type) {
5906 default:
5907 if (Primitive::IsFloatingPointType(cond_type)) {
5908 __ Mfc1(cond_reg, fcond_reg);
5909 }
5910 if (true_src.IsConstant()) {
5911 if (cond_inverted) {
5912 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5913 } else {
5914 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5915 }
5916 } else if (false_src.IsConstant()) {
5917 if (cond_inverted) {
5918 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5919 } else {
5920 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5921 }
5922 } else {
5923 DCHECK_NE(cond_reg, AT);
5924 if (cond_inverted) {
5925 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5926 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5927 } else {
5928 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5929 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5930 }
5931 __ Or(dst.AsRegister<Register>(), AT, TMP);
5932 }
5933 break;
5934 case Primitive::kPrimLong: {
5935 if (Primitive::IsFloatingPointType(cond_type)) {
5936 __ Mfc1(cond_reg, fcond_reg);
5937 }
5938 Register dst_lo = dst.AsRegisterPairLow<Register>();
5939 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5940 if (true_src.IsConstant()) {
5941 Register src_lo = false_src.AsRegisterPairLow<Register>();
5942 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5943 if (cond_inverted) {
5944 __ Selnez(dst_lo, src_lo, cond_reg);
5945 __ Selnez(dst_hi, src_hi, cond_reg);
5946 } else {
5947 __ Seleqz(dst_lo, src_lo, cond_reg);
5948 __ Seleqz(dst_hi, src_hi, cond_reg);
5949 }
5950 } else {
5951 DCHECK(false_src.IsConstant());
5952 Register src_lo = true_src.AsRegisterPairLow<Register>();
5953 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5954 if (cond_inverted) {
5955 __ Seleqz(dst_lo, src_lo, cond_reg);
5956 __ Seleqz(dst_hi, src_hi, cond_reg);
5957 } else {
5958 __ Selnez(dst_lo, src_lo, cond_reg);
5959 __ Selnez(dst_hi, src_hi, cond_reg);
5960 }
5961 }
5962 break;
5963 }
5964 case Primitive::kPrimFloat: {
5965 if (!Primitive::IsFloatingPointType(cond_type)) {
5966 // sel*.fmt tests bit 0 of the condition register, account for that.
5967 __ Sltu(TMP, ZERO, cond_reg);
5968 __ Mtc1(TMP, fcond_reg);
5969 }
5970 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5971 if (true_src.IsConstant()) {
5972 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5973 if (cond_inverted) {
5974 __ SelnezS(dst_reg, src_reg, fcond_reg);
5975 } else {
5976 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5977 }
5978 } else if (false_src.IsConstant()) {
5979 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5980 if (cond_inverted) {
5981 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5982 } else {
5983 __ SelnezS(dst_reg, src_reg, fcond_reg);
5984 }
5985 } else {
5986 if (cond_inverted) {
5987 __ SelS(fcond_reg,
5988 true_src.AsFpuRegister<FRegister>(),
5989 false_src.AsFpuRegister<FRegister>());
5990 } else {
5991 __ SelS(fcond_reg,
5992 false_src.AsFpuRegister<FRegister>(),
5993 true_src.AsFpuRegister<FRegister>());
5994 }
5995 __ MovS(dst_reg, fcond_reg);
5996 }
5997 break;
5998 }
5999 case Primitive::kPrimDouble: {
6000 if (!Primitive::IsFloatingPointType(cond_type)) {
6001 // sel*.fmt tests bit 0 of the condition register, account for that.
6002 __ Sltu(TMP, ZERO, cond_reg);
6003 __ Mtc1(TMP, fcond_reg);
6004 }
6005 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6006 if (true_src.IsConstant()) {
6007 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6008 if (cond_inverted) {
6009 __ SelnezD(dst_reg, src_reg, fcond_reg);
6010 } else {
6011 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6012 }
6013 } else if (false_src.IsConstant()) {
6014 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6015 if (cond_inverted) {
6016 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6017 } else {
6018 __ SelnezD(dst_reg, src_reg, fcond_reg);
6019 }
6020 } else {
6021 if (cond_inverted) {
6022 __ SelD(fcond_reg,
6023 true_src.AsFpuRegister<FRegister>(),
6024 false_src.AsFpuRegister<FRegister>());
6025 } else {
6026 __ SelD(fcond_reg,
6027 false_src.AsFpuRegister<FRegister>(),
6028 true_src.AsFpuRegister<FRegister>());
6029 }
6030 __ MovD(dst_reg, fcond_reg);
6031 }
6032 break;
6033 }
6034 }
6035}
6036
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006037void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6038 LocationSummary* locations = new (GetGraph()->GetArena())
6039 LocationSummary(flag, LocationSummary::kNoCall);
6040 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006041}
6042
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006043void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6044 __ LoadFromOffset(kLoadWord,
6045 flag->GetLocations()->Out().AsRegister<Register>(),
6046 SP,
6047 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006048}
6049
David Brazdil74eb1b22015-12-14 11:44:01 +00006050void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
6051 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006052 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006053}
6054
6055void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006056 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6057 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6058 if (is_r6) {
6059 GenConditionalMoveR6(select);
6060 } else {
6061 GenConditionalMoveR2(select);
6062 }
6063 } else {
6064 LocationSummary* locations = select->GetLocations();
6065 MipsLabel false_target;
6066 GenerateTestAndBranch(select,
6067 /* condition_input_index */ 2,
6068 /* true_target */ nullptr,
6069 &false_target);
6070 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6071 __ Bind(&false_target);
6072 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006073}
6074
David Srbecky0cf44932015-12-09 14:09:59 +00006075void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
6076 new (GetGraph()->GetArena()) LocationSummary(info);
6077}
6078
David Srbeckyd28f4a02016-03-14 17:14:24 +00006079void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6080 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006081}
6082
6083void CodeGeneratorMIPS::GenerateNop() {
6084 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006085}
6086
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006087void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
6088 Primitive::Type field_type = field_info.GetFieldType();
6089 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6090 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006091 bool object_field_get_with_read_barrier =
6092 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006093 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006094 instruction,
6095 generate_volatile
6096 ? LocationSummary::kCallOnMainOnly
6097 : (object_field_get_with_read_barrier
6098 ? LocationSummary::kCallOnSlowPath
6099 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006100
Alexey Frunzec61c0762017-04-10 13:54:23 -07006101 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6102 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6103 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006104 locations->SetInAt(0, Location::RequiresRegister());
6105 if (generate_volatile) {
6106 InvokeRuntimeCallingConvention calling_convention;
6107 // need A0 to hold base + offset
6108 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6109 if (field_type == Primitive::kPrimLong) {
6110 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
6111 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006112 // Use Location::Any() to prevent situations when running out of available fp registers.
6113 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006114 // Need some temp core regs since FP results are returned in core registers
6115 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
6116 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6117 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6118 }
6119 } else {
6120 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6121 locations->SetOut(Location::RequiresFpuRegister());
6122 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006123 // The output overlaps in the case of an object field get with
6124 // read barriers enabled: we do not want the move to overwrite the
6125 // object's location, as we need it to emit the read barrier.
6126 locations->SetOut(Location::RequiresRegister(),
6127 object_field_get_with_read_barrier
6128 ? Location::kOutputOverlap
6129 : Location::kNoOutputOverlap);
6130 }
6131 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6132 // We need a temporary register for the read barrier marking slow
6133 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006134 if (!kBakerReadBarrierThunksEnableForFields) {
6135 locations->AddTemp(Location::RequiresRegister());
6136 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006137 }
6138 }
6139}
6140
6141void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6142 const FieldInfo& field_info,
6143 uint32_t dex_pc) {
6144 Primitive::Type type = field_info.GetFieldType();
6145 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006146 Location obj_loc = locations->InAt(0);
6147 Register obj = obj_loc.AsRegister<Register>();
6148 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006149 LoadOperandType load_type = kLoadUnsignedByte;
6150 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006151 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006152 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006153
6154 switch (type) {
6155 case Primitive::kPrimBoolean:
6156 load_type = kLoadUnsignedByte;
6157 break;
6158 case Primitive::kPrimByte:
6159 load_type = kLoadSignedByte;
6160 break;
6161 case Primitive::kPrimShort:
6162 load_type = kLoadSignedHalfword;
6163 break;
6164 case Primitive::kPrimChar:
6165 load_type = kLoadUnsignedHalfword;
6166 break;
6167 case Primitive::kPrimInt:
6168 case Primitive::kPrimFloat:
6169 case Primitive::kPrimNot:
6170 load_type = kLoadWord;
6171 break;
6172 case Primitive::kPrimLong:
6173 case Primitive::kPrimDouble:
6174 load_type = kLoadDoubleword;
6175 break;
6176 case Primitive::kPrimVoid:
6177 LOG(FATAL) << "Unreachable type " << type;
6178 UNREACHABLE();
6179 }
6180
6181 if (is_volatile && load_type == kLoadDoubleword) {
6182 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006183 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006184 // Do implicit Null check
6185 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6186 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006187 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006188 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
6189 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006190 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006191 if (dst_loc.IsFpuRegister()) {
6192 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006193 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006194 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006195 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006196 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006197 __ StoreToOffset(kStoreWord,
6198 locations->GetTemp(1).AsRegister<Register>(),
6199 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006200 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006201 __ StoreToOffset(kStoreWord,
6202 locations->GetTemp(2).AsRegister<Register>(),
6203 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006204 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006205 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006206 }
6207 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006208 if (type == Primitive::kPrimNot) {
6209 // /* HeapReference<Object> */ dst = *(obj + offset)
6210 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006211 Location temp_loc =
6212 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006213 // Note that a potential implicit null check is handled in this
6214 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6215 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6216 dst_loc,
6217 obj,
6218 offset,
6219 temp_loc,
6220 /* needs_null_check */ true);
6221 if (is_volatile) {
6222 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6223 }
6224 } else {
6225 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6226 if (is_volatile) {
6227 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6228 }
6229 // If read barriers are enabled, emit read barriers other than
6230 // Baker's using a slow path (and also unpoison the loaded
6231 // reference, if heap poisoning is enabled).
6232 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6233 }
6234 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006235 Register dst;
6236 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08006237 DCHECK(dst_loc.IsRegisterPair());
6238 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006239 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006240 DCHECK(dst_loc.IsRegister());
6241 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006242 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006243 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006244 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006245 DCHECK(dst_loc.IsFpuRegister());
6246 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006247 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006248 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006249 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006250 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006251 }
6252 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006253 }
6254
Alexey Frunze15958152017-02-09 19:08:30 -08006255 // Memory barriers, in the case of references, are handled in the
6256 // previous switch statement.
6257 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006258 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6259 }
6260}
6261
6262void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
6263 Primitive::Type field_type = field_info.GetFieldType();
6264 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6265 bool generate_volatile = field_info.IsVolatile() && is_wide;
6266 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006267 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006268
6269 locations->SetInAt(0, Location::RequiresRegister());
6270 if (generate_volatile) {
6271 InvokeRuntimeCallingConvention calling_convention;
6272 // need A0 to hold base + offset
6273 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6274 if (field_type == Primitive::kPrimLong) {
6275 locations->SetInAt(1, Location::RegisterPairLocation(
6276 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6277 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006278 // Use Location::Any() to prevent situations when running out of available fp registers.
6279 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006280 // Pass FP parameters in core registers.
6281 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6282 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6283 }
6284 } else {
6285 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006286 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006287 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006288 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006289 }
6290 }
6291}
6292
6293void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6294 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006295 uint32_t dex_pc,
6296 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006297 Primitive::Type type = field_info.GetFieldType();
6298 LocationSummary* locations = instruction->GetLocations();
6299 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006300 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006301 StoreOperandType store_type = kStoreByte;
6302 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006303 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006304 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006305 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006306
6307 switch (type) {
6308 case Primitive::kPrimBoolean:
6309 case Primitive::kPrimByte:
6310 store_type = kStoreByte;
6311 break;
6312 case Primitive::kPrimShort:
6313 case Primitive::kPrimChar:
6314 store_type = kStoreHalfword;
6315 break;
6316 case Primitive::kPrimInt:
6317 case Primitive::kPrimFloat:
6318 case Primitive::kPrimNot:
6319 store_type = kStoreWord;
6320 break;
6321 case Primitive::kPrimLong:
6322 case Primitive::kPrimDouble:
6323 store_type = kStoreDoubleword;
6324 break;
6325 case Primitive::kPrimVoid:
6326 LOG(FATAL) << "Unreachable type " << type;
6327 UNREACHABLE();
6328 }
6329
6330 if (is_volatile) {
6331 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6332 }
6333
6334 if (is_volatile && store_type == kStoreDoubleword) {
6335 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006336 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006337 // Do implicit Null check.
6338 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6339 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6340 if (type == Primitive::kPrimDouble) {
6341 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006342 if (value_location.IsFpuRegister()) {
6343 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6344 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006345 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006346 value_location.AsFpuRegister<FRegister>());
6347 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006348 __ LoadFromOffset(kLoadWord,
6349 locations->GetTemp(1).AsRegister<Register>(),
6350 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006351 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006352 __ LoadFromOffset(kLoadWord,
6353 locations->GetTemp(2).AsRegister<Register>(),
6354 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006355 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006356 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006357 DCHECK(value_location.IsConstant());
6358 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6359 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006360 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6361 locations->GetTemp(1).AsRegister<Register>(),
6362 value);
6363 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006364 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006365 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006366 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6367 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006368 if (value_location.IsConstant()) {
6369 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6370 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6371 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006372 Register src;
6373 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006374 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006375 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006376 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006377 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006378 if (kPoisonHeapReferences && needs_write_barrier) {
6379 // Note that in the case where `value` is a null reference,
6380 // we do not enter this block, as a null reference does not
6381 // need poisoning.
6382 DCHECK_EQ(type, Primitive::kPrimNot);
6383 __ PoisonHeapReference(TMP, src);
6384 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6385 } else {
6386 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6387 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006388 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006389 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006390 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006391 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006393 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006394 }
6395 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006396 }
6397
Alexey Frunzec061de12017-02-14 13:27:23 -08006398 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006399 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006400 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006401 }
6402
6403 if (is_volatile) {
6404 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6405 }
6406}
6407
6408void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6409 HandleFieldGet(instruction, instruction->GetFieldInfo());
6410}
6411
6412void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6413 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6414}
6415
6416void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6417 HandleFieldSet(instruction, instruction->GetFieldInfo());
6418}
6419
6420void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006421 HandleFieldSet(instruction,
6422 instruction->GetFieldInfo(),
6423 instruction->GetDexPc(),
6424 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006425}
6426
Alexey Frunze15958152017-02-09 19:08:30 -08006427void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6428 HInstruction* instruction,
6429 Location out,
6430 uint32_t offset,
6431 Location maybe_temp,
6432 ReadBarrierOption read_barrier_option) {
6433 Register out_reg = out.AsRegister<Register>();
6434 if (read_barrier_option == kWithReadBarrier) {
6435 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006436 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6437 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6438 }
Alexey Frunze15958152017-02-09 19:08:30 -08006439 if (kUseBakerReadBarrier) {
6440 // Load with fast path based Baker's read barrier.
6441 // /* HeapReference<Object> */ out = *(out + offset)
6442 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6443 out,
6444 out_reg,
6445 offset,
6446 maybe_temp,
6447 /* needs_null_check */ false);
6448 } else {
6449 // Load with slow path based read barrier.
6450 // Save the value of `out` into `maybe_temp` before overwriting it
6451 // in the following move operation, as we will need it for the
6452 // read barrier below.
6453 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6454 // /* HeapReference<Object> */ out = *(out + offset)
6455 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6456 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6457 }
6458 } else {
6459 // Plain load with no read barrier.
6460 // /* HeapReference<Object> */ out = *(out + offset)
6461 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6462 __ MaybeUnpoisonHeapReference(out_reg);
6463 }
6464}
6465
6466void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6467 HInstruction* instruction,
6468 Location out,
6469 Location obj,
6470 uint32_t offset,
6471 Location maybe_temp,
6472 ReadBarrierOption read_barrier_option) {
6473 Register out_reg = out.AsRegister<Register>();
6474 Register obj_reg = obj.AsRegister<Register>();
6475 if (read_barrier_option == kWithReadBarrier) {
6476 CHECK(kEmitCompilerReadBarrier);
6477 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006478 if (!kBakerReadBarrierThunksEnableForFields) {
6479 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6480 }
Alexey Frunze15958152017-02-09 19:08:30 -08006481 // Load with fast path based Baker's read barrier.
6482 // /* HeapReference<Object> */ out = *(obj + offset)
6483 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6484 out,
6485 obj_reg,
6486 offset,
6487 maybe_temp,
6488 /* needs_null_check */ false);
6489 } else {
6490 // Load with slow path based read barrier.
6491 // /* HeapReference<Object> */ out = *(obj + offset)
6492 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6493 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6494 }
6495 } else {
6496 // Plain load with no read barrier.
6497 // /* HeapReference<Object> */ out = *(obj + offset)
6498 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6499 __ MaybeUnpoisonHeapReference(out_reg);
6500 }
6501}
6502
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006503static inline int GetBakerMarkThunkNumber(Register reg) {
6504 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6505 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6506 return reg - V0;
6507 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6508 return 14 + (reg - S2);
6509 } else if (reg == FP) { // One more.
6510 return 20;
6511 }
6512 LOG(FATAL) << "Unexpected register " << reg;
6513 UNREACHABLE();
6514}
6515
6516static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6517 int num = GetBakerMarkThunkNumber(reg) +
6518 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6519 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6520}
6521
6522static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6523 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6524 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6525}
6526
Alexey Frunze15958152017-02-09 19:08:30 -08006527void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6528 Location root,
6529 Register obj,
6530 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006531 ReadBarrierOption read_barrier_option,
6532 MipsLabel* label_low) {
6533 bool reordering;
6534 if (label_low != nullptr) {
6535 DCHECK_EQ(offset, 0x5678u);
6536 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006537 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006538 if (read_barrier_option == kWithReadBarrier) {
6539 DCHECK(kEmitCompilerReadBarrier);
6540 if (kUseBakerReadBarrier) {
6541 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6542 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006543 if (kBakerReadBarrierThunksEnableForGcRoots) {
6544 // Note that we do not actually check the value of `GetIsGcMarking()`
6545 // to decide whether to mark the loaded GC root or not. Instead, we
6546 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6547 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6548 // vice versa.
6549 //
6550 // We use thunks for the slow path. That thunk checks the reference
6551 // and jumps to the entrypoint if needed.
6552 //
6553 // temp = Thread::Current()->pReadBarrierMarkReg00
6554 // // AKA &art_quick_read_barrier_mark_introspection.
6555 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6556 // if (temp != nullptr) {
6557 // temp = &gc_root_thunk<root_reg>
6558 // root = temp(root)
6559 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006560
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006561 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6562 const int32_t entry_point_offset =
6563 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6564 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6565 int16_t offset_low = Low16Bits(offset);
6566 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6567 // extension in lw.
6568 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6569 Register base = short_offset ? obj : TMP;
6570 // Loading the entrypoint does not require a load acquire since it is only changed when
6571 // threads are suspended or running a checkpoint.
6572 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6573 reordering = __ SetReorder(false);
6574 if (!short_offset) {
6575 DCHECK(!label_low);
6576 __ AddUpper(base, obj, offset_high);
6577 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006578 MipsLabel skip_call;
6579 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006580 if (label_low != nullptr) {
6581 DCHECK(short_offset);
6582 __ Bind(label_low);
6583 }
6584 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6585 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6586 // in delay slot.
6587 if (isR6) {
6588 __ Jialc(T9, thunk_disp);
6589 } else {
6590 __ Addiu(T9, T9, thunk_disp);
6591 __ Jalr(T9);
6592 __ Nop();
6593 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006594 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006595 __ SetReorder(reordering);
6596 } else {
6597 // Note that we do not actually check the value of `GetIsGcMarking()`
6598 // to decide whether to mark the loaded GC root or not. Instead, we
6599 // load into `temp` (T9) the read barrier mark entry point corresponding
6600 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6601 // is false, and vice versa.
6602 //
6603 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6604 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6605 // if (temp != null) {
6606 // root = temp(root)
6607 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006608
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006609 if (label_low != nullptr) {
6610 reordering = __ SetReorder(false);
6611 __ Bind(label_low);
6612 }
6613 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6614 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6615 if (label_low != nullptr) {
6616 __ SetReorder(reordering);
6617 }
6618 static_assert(
6619 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6620 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6621 "have different sizes.");
6622 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6623 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6624 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006625
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006626 // Slow path marking the GC root `root`.
6627 Location temp = Location::RegisterLocation(T9);
6628 SlowPathCodeMIPS* slow_path =
6629 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6630 instruction,
6631 root,
6632 /*entrypoint*/ temp);
6633 codegen_->AddSlowPath(slow_path);
6634
6635 const int32_t entry_point_offset =
6636 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6637 // Loading the entrypoint does not require a load acquire since it is only changed when
6638 // threads are suspended or running a checkpoint.
6639 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6640 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6641 __ Bind(slow_path->GetExitLabel());
6642 }
Alexey Frunze15958152017-02-09 19:08:30 -08006643 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006644 if (label_low != nullptr) {
6645 reordering = __ SetReorder(false);
6646 __ Bind(label_low);
6647 }
Alexey Frunze15958152017-02-09 19:08:30 -08006648 // GC root loaded through a slow path for read barriers other
6649 // than Baker's.
6650 // /* GcRoot<mirror::Object>* */ root = obj + offset
6651 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006652 if (label_low != nullptr) {
6653 __ SetReorder(reordering);
6654 }
Alexey Frunze15958152017-02-09 19:08:30 -08006655 // /* mirror::Object* */ root = root->Read()
6656 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6657 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006658 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006659 if (label_low != nullptr) {
6660 reordering = __ SetReorder(false);
6661 __ Bind(label_low);
6662 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006663 // Plain GC root load with no read barrier.
6664 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6665 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6666 // Note that GC roots are not affected by heap poisoning, thus we
6667 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006668 if (label_low != nullptr) {
6669 __ SetReorder(reordering);
6670 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006671 }
6672}
6673
Alexey Frunze15958152017-02-09 19:08:30 -08006674void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6675 Location ref,
6676 Register obj,
6677 uint32_t offset,
6678 Location temp,
6679 bool needs_null_check) {
6680 DCHECK(kEmitCompilerReadBarrier);
6681 DCHECK(kUseBakerReadBarrier);
6682
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006683 if (kBakerReadBarrierThunksEnableForFields) {
6684 // Note that we do not actually check the value of `GetIsGcMarking()`
6685 // to decide whether to mark the loaded reference or not. Instead, we
6686 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6687 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6688 // vice versa.
6689 //
6690 // We use thunks for the slow path. That thunk checks the reference
6691 // and jumps to the entrypoint if needed. If the holder is not gray,
6692 // it issues a load-load memory barrier and returns to the original
6693 // reference load.
6694 //
6695 // temp = Thread::Current()->pReadBarrierMarkReg00
6696 // // AKA &art_quick_read_barrier_mark_introspection.
6697 // if (temp != nullptr) {
6698 // temp = &field_array_thunk<holder_reg>
6699 // temp()
6700 // }
6701 // not_gray_return_address:
6702 // // If the offset is too large to fit into the lw instruction, we
6703 // // use an adjusted base register (TMP) here. This register
6704 // // receives bits 16 ... 31 of the offset before the thunk invocation
6705 // // and the thunk benefits from it.
6706 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6707 // gray_return_address:
6708
6709 DCHECK(temp.IsInvalid());
6710 bool isR6 = GetInstructionSetFeatures().IsR6();
6711 int16_t offset_low = Low16Bits(offset);
6712 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6713 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6714 bool reordering = __ SetReorder(false);
6715 const int32_t entry_point_offset =
6716 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6717 // There may have or may have not been a null check if the field offset is smaller than
6718 // the page size.
6719 // There must've been a null check in case it's actually a load from an array.
6720 // We will, however, perform an explicit null check in the thunk as it's easier to
6721 // do it than not.
6722 if (instruction->IsArrayGet()) {
6723 DCHECK(!needs_null_check);
6724 }
6725 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6726 // Loading the entrypoint does not require a load acquire since it is only changed when
6727 // threads are suspended or running a checkpoint.
6728 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6729 Register ref_reg = ref.AsRegister<Register>();
6730 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006731 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006732 if (short_offset) {
6733 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006734 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006735 __ Nop(); // In forbidden slot.
6736 __ Jialc(T9, thunk_disp);
6737 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006738 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006739 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6740 __ Jalr(T9);
6741 __ Nop(); // In delay slot.
6742 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006743 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006744 } else {
6745 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006746 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006747 __ Aui(base, obj, offset_high); // In delay slot.
6748 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006749 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006750 } else {
6751 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006752 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006753 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6754 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006755 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006756 __ Addu(base, base, obj); // In delay slot.
6757 }
6758 }
6759 // /* HeapReference<Object> */ ref = *(obj + offset)
6760 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6761 if (needs_null_check) {
6762 MaybeRecordImplicitNullCheck(instruction);
6763 }
6764 __ MaybeUnpoisonHeapReference(ref_reg);
6765 __ SetReorder(reordering);
6766 return;
6767 }
6768
Alexey Frunze15958152017-02-09 19:08:30 -08006769 // /* HeapReference<Object> */ ref = *(obj + offset)
6770 Location no_index = Location::NoLocation();
6771 ScaleFactor no_scale_factor = TIMES_1;
6772 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6773 ref,
6774 obj,
6775 offset,
6776 no_index,
6777 no_scale_factor,
6778 temp,
6779 needs_null_check);
6780}
6781
6782void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6783 Location ref,
6784 Register obj,
6785 uint32_t data_offset,
6786 Location index,
6787 Location temp,
6788 bool needs_null_check) {
6789 DCHECK(kEmitCompilerReadBarrier);
6790 DCHECK(kUseBakerReadBarrier);
6791
6792 static_assert(
6793 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6794 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006795 ScaleFactor scale_factor = TIMES_4;
6796
6797 if (kBakerReadBarrierThunksEnableForArrays) {
6798 // Note that we do not actually check the value of `GetIsGcMarking()`
6799 // to decide whether to mark the loaded reference or not. Instead, we
6800 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6801 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6802 // vice versa.
6803 //
6804 // We use thunks for the slow path. That thunk checks the reference
6805 // and jumps to the entrypoint if needed. If the holder is not gray,
6806 // it issues a load-load memory barrier and returns to the original
6807 // reference load.
6808 //
6809 // temp = Thread::Current()->pReadBarrierMarkReg00
6810 // // AKA &art_quick_read_barrier_mark_introspection.
6811 // if (temp != nullptr) {
6812 // temp = &field_array_thunk<holder_reg>
6813 // temp()
6814 // }
6815 // not_gray_return_address:
6816 // // The element address is pre-calculated in the TMP register before the
6817 // // thunk invocation and the thunk benefits from it.
6818 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6819 // gray_return_address:
6820
6821 DCHECK(temp.IsInvalid());
6822 DCHECK(index.IsValid());
6823 bool reordering = __ SetReorder(false);
6824 const int32_t entry_point_offset =
6825 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6826 // We will not do the explicit null check in the thunk as some form of a null check
6827 // must've been done earlier.
6828 DCHECK(!needs_null_check);
6829 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6830 // Loading the entrypoint does not require a load acquire since it is only changed when
6831 // threads are suspended or running a checkpoint.
6832 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6833 Register ref_reg = ref.AsRegister<Register>();
6834 Register index_reg = index.IsRegisterPair()
6835 ? index.AsRegisterPairLow<Register>()
6836 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006837 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006838 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006839 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006840 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6841 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006842 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006843 } else {
6844 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006845 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006846 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6847 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006848 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006849 __ Addu(TMP, TMP, obj); // In delay slot.
6850 }
6851 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6852 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6853 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6854 __ MaybeUnpoisonHeapReference(ref_reg);
6855 __ SetReorder(reordering);
6856 return;
6857 }
6858
Alexey Frunze15958152017-02-09 19:08:30 -08006859 // /* HeapReference<Object> */ ref =
6860 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006861 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6862 ref,
6863 obj,
6864 data_offset,
6865 index,
6866 scale_factor,
6867 temp,
6868 needs_null_check);
6869}
6870
6871void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6872 Location ref,
6873 Register obj,
6874 uint32_t offset,
6875 Location index,
6876 ScaleFactor scale_factor,
6877 Location temp,
6878 bool needs_null_check,
6879 bool always_update_field) {
6880 DCHECK(kEmitCompilerReadBarrier);
6881 DCHECK(kUseBakerReadBarrier);
6882
6883 // In slow path based read barriers, the read barrier call is
6884 // inserted after the original load. However, in fast path based
6885 // Baker's read barriers, we need to perform the load of
6886 // mirror::Object::monitor_ *before* the original reference load.
6887 // This load-load ordering is required by the read barrier.
6888 // The fast path/slow path (for Baker's algorithm) should look like:
6889 //
6890 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6891 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6892 // HeapReference<Object> ref = *src; // Original reference load.
6893 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6894 // if (is_gray) {
6895 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6896 // }
6897 //
6898 // Note: the original implementation in ReadBarrier::Barrier is
6899 // slightly more complex as it performs additional checks that we do
6900 // not do here for performance reasons.
6901
6902 Register ref_reg = ref.AsRegister<Register>();
6903 Register temp_reg = temp.AsRegister<Register>();
6904 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6905
6906 // /* int32_t */ monitor = obj->monitor_
6907 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6908 if (needs_null_check) {
6909 MaybeRecordImplicitNullCheck(instruction);
6910 }
6911 // /* LockWord */ lock_word = LockWord(monitor)
6912 static_assert(sizeof(LockWord) == sizeof(int32_t),
6913 "art::LockWord and int32_t have different sizes.");
6914
6915 __ Sync(0); // Barrier to prevent load-load reordering.
6916
6917 // The actual reference load.
6918 if (index.IsValid()) {
6919 // Load types involving an "index": ArrayGet,
6920 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6921 // intrinsics.
6922 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6923 if (index.IsConstant()) {
6924 size_t computed_offset =
6925 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6926 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6927 } else {
6928 // Handle the special case of the
6929 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6930 // intrinsics, which use a register pair as index ("long
6931 // offset"), of which only the low part contains data.
6932 Register index_reg = index.IsRegisterPair()
6933 ? index.AsRegisterPairLow<Register>()
6934 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006935 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006936 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6937 }
6938 } else {
6939 // /* HeapReference<Object> */ ref = *(obj + offset)
6940 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6941 }
6942
6943 // Object* ref = ref_addr->AsMirrorPtr()
6944 __ MaybeUnpoisonHeapReference(ref_reg);
6945
6946 // Slow path marking the object `ref` when it is gray.
6947 SlowPathCodeMIPS* slow_path;
6948 if (always_update_field) {
6949 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6950 // of the form `obj + field_offset`, where `obj` is a register and
6951 // `field_offset` is a register pair (of which only the lower half
6952 // is used). Thus `offset` and `scale_factor` above are expected
6953 // to be null in this code path.
6954 DCHECK_EQ(offset, 0u);
6955 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6956 slow_path = new (GetGraph()->GetArena())
6957 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6958 ref,
6959 obj,
6960 /* field_offset */ index,
6961 temp_reg);
6962 } else {
6963 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6964 }
6965 AddSlowPath(slow_path);
6966
6967 // if (rb_state == ReadBarrier::GrayState())
6968 // ref = ReadBarrier::Mark(ref);
6969 // Given the numeric representation, it's enough to check the low bit of the
6970 // rb_state. We do that by shifting the bit into the sign bit (31) and
6971 // performing a branch on less than zero.
6972 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6973 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6974 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6975 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6976 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6977 __ Bind(slow_path->GetExitLabel());
6978}
6979
6980void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6981 Location out,
6982 Location ref,
6983 Location obj,
6984 uint32_t offset,
6985 Location index) {
6986 DCHECK(kEmitCompilerReadBarrier);
6987
6988 // Insert a slow path based read barrier *after* the reference load.
6989 //
6990 // If heap poisoning is enabled, the unpoisoning of the loaded
6991 // reference will be carried out by the runtime within the slow
6992 // path.
6993 //
6994 // Note that `ref` currently does not get unpoisoned (when heap
6995 // poisoning is enabled), which is alright as the `ref` argument is
6996 // not used by the artReadBarrierSlow entry point.
6997 //
6998 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6999 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
7000 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7001 AddSlowPath(slow_path);
7002
7003 __ B(slow_path->GetEntryLabel());
7004 __ Bind(slow_path->GetExitLabel());
7005}
7006
7007void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7008 Location out,
7009 Location ref,
7010 Location obj,
7011 uint32_t offset,
7012 Location index) {
7013 if (kEmitCompilerReadBarrier) {
7014 // Baker's read barriers shall be handled by the fast path
7015 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7016 DCHECK(!kUseBakerReadBarrier);
7017 // If heap poisoning is enabled, unpoisoning will be taken care of
7018 // by the runtime within the slow path.
7019 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7020 } else if (kPoisonHeapReferences) {
7021 __ UnpoisonHeapReference(out.AsRegister<Register>());
7022 }
7023}
7024
7025void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7026 Location out,
7027 Location root) {
7028 DCHECK(kEmitCompilerReadBarrier);
7029
7030 // Insert a slow path based read barrier *after* the GC root load.
7031 //
7032 // Note that GC roots are not affected by heap poisoning, so we do
7033 // not need to do anything special for this here.
7034 SlowPathCodeMIPS* slow_path =
7035 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
7036 AddSlowPath(slow_path);
7037
7038 __ B(slow_path->GetEntryLabel());
7039 __ Bind(slow_path->GetExitLabel());
7040}
7041
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007042void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007043 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7044 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007045 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007046 switch (type_check_kind) {
7047 case TypeCheckKind::kExactCheck:
7048 case TypeCheckKind::kAbstractClassCheck:
7049 case TypeCheckKind::kClassHierarchyCheck:
7050 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007051 call_kind =
7052 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007053 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007054 break;
7055 case TypeCheckKind::kArrayCheck:
7056 case TypeCheckKind::kUnresolvedCheck:
7057 case TypeCheckKind::kInterfaceCheck:
7058 call_kind = LocationSummary::kCallOnSlowPath;
7059 break;
7060 }
7061
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007062 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007063 if (baker_read_barrier_slow_path) {
7064 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7065 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007066 locations->SetInAt(0, Location::RequiresRegister());
7067 locations->SetInAt(1, Location::RequiresRegister());
7068 // The output does overlap inputs.
7069 // Note that TypeCheckSlowPathMIPS uses this register too.
7070 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007071 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007072}
7073
7074void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007075 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007076 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007077 Location obj_loc = locations->InAt(0);
7078 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007079 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007080 Location out_loc = locations->Out();
7081 Register out = out_loc.AsRegister<Register>();
7082 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7083 DCHECK_LE(num_temps, 1u);
7084 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007085 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7086 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7087 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7088 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007089 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007090 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007091
7092 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007093 // Avoid this check if we know `obj` is not null.
7094 if (instruction->MustDoNullCheck()) {
7095 __ Move(out, ZERO);
7096 __ Beqz(obj, &done);
7097 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007098
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007099 switch (type_check_kind) {
7100 case TypeCheckKind::kExactCheck: {
7101 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007102 GenerateReferenceLoadTwoRegisters(instruction,
7103 out_loc,
7104 obj_loc,
7105 class_offset,
7106 maybe_temp_loc,
7107 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007108 // Classes must be equal for the instanceof to succeed.
7109 __ Xor(out, out, cls);
7110 __ Sltiu(out, out, 1);
7111 break;
7112 }
7113
7114 case TypeCheckKind::kAbstractClassCheck: {
7115 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007116 GenerateReferenceLoadTwoRegisters(instruction,
7117 out_loc,
7118 obj_loc,
7119 class_offset,
7120 maybe_temp_loc,
7121 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007122 // If the class is abstract, we eagerly fetch the super class of the
7123 // object to avoid doing a comparison we know will fail.
7124 MipsLabel loop;
7125 __ Bind(&loop);
7126 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007127 GenerateReferenceLoadOneRegister(instruction,
7128 out_loc,
7129 super_offset,
7130 maybe_temp_loc,
7131 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007132 // If `out` is null, we use it for the result, and jump to `done`.
7133 __ Beqz(out, &done);
7134 __ Bne(out, cls, &loop);
7135 __ LoadConst32(out, 1);
7136 break;
7137 }
7138
7139 case TypeCheckKind::kClassHierarchyCheck: {
7140 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007141 GenerateReferenceLoadTwoRegisters(instruction,
7142 out_loc,
7143 obj_loc,
7144 class_offset,
7145 maybe_temp_loc,
7146 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007147 // Walk over the class hierarchy to find a match.
7148 MipsLabel loop, success;
7149 __ Bind(&loop);
7150 __ Beq(out, cls, &success);
7151 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007152 GenerateReferenceLoadOneRegister(instruction,
7153 out_loc,
7154 super_offset,
7155 maybe_temp_loc,
7156 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007157 __ Bnez(out, &loop);
7158 // If `out` is null, we use it for the result, and jump to `done`.
7159 __ B(&done);
7160 __ Bind(&success);
7161 __ LoadConst32(out, 1);
7162 break;
7163 }
7164
7165 case TypeCheckKind::kArrayObjectCheck: {
7166 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007167 GenerateReferenceLoadTwoRegisters(instruction,
7168 out_loc,
7169 obj_loc,
7170 class_offset,
7171 maybe_temp_loc,
7172 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007173 // Do an exact check.
7174 MipsLabel success;
7175 __ Beq(out, cls, &success);
7176 // Otherwise, we need to check that the object's class is a non-primitive array.
7177 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007178 GenerateReferenceLoadOneRegister(instruction,
7179 out_loc,
7180 component_offset,
7181 maybe_temp_loc,
7182 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007183 // If `out` is null, we use it for the result, and jump to `done`.
7184 __ Beqz(out, &done);
7185 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7186 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7187 __ Sltiu(out, out, 1);
7188 __ B(&done);
7189 __ Bind(&success);
7190 __ LoadConst32(out, 1);
7191 break;
7192 }
7193
7194 case TypeCheckKind::kArrayCheck: {
7195 // No read barrier since the slow path will retry upon failure.
7196 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007197 GenerateReferenceLoadTwoRegisters(instruction,
7198 out_loc,
7199 obj_loc,
7200 class_offset,
7201 maybe_temp_loc,
7202 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007203 DCHECK(locations->OnlyCallsOnSlowPath());
7204 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7205 /* is_fatal */ false);
7206 codegen_->AddSlowPath(slow_path);
7207 __ Bne(out, cls, slow_path->GetEntryLabel());
7208 __ LoadConst32(out, 1);
7209 break;
7210 }
7211
7212 case TypeCheckKind::kUnresolvedCheck:
7213 case TypeCheckKind::kInterfaceCheck: {
7214 // Note that we indeed only call on slow path, but we always go
7215 // into the slow path for the unresolved and interface check
7216 // cases.
7217 //
7218 // We cannot directly call the InstanceofNonTrivial runtime
7219 // entry point without resorting to a type checking slow path
7220 // here (i.e. by calling InvokeRuntime directly), as it would
7221 // require to assign fixed registers for the inputs of this
7222 // HInstanceOf instruction (following the runtime calling
7223 // convention), which might be cluttered by the potential first
7224 // read barrier emission at the beginning of this method.
7225 //
7226 // TODO: Introduce a new runtime entry point taking the object
7227 // to test (instead of its class) as argument, and let it deal
7228 // with the read barrier issues. This will let us refactor this
7229 // case of the `switch` code as it was previously (with a direct
7230 // call to the runtime not using a type checking slow path).
7231 // This should also be beneficial for the other cases above.
7232 DCHECK(locations->OnlyCallsOnSlowPath());
7233 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7234 /* is_fatal */ false);
7235 codegen_->AddSlowPath(slow_path);
7236 __ B(slow_path->GetEntryLabel());
7237 break;
7238 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007239 }
7240
7241 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007242
7243 if (slow_path != nullptr) {
7244 __ Bind(slow_path->GetExitLabel());
7245 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007246}
7247
7248void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
7249 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7250 locations->SetOut(Location::ConstantLocation(constant));
7251}
7252
7253void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7254 // Will be generated at use site.
7255}
7256
7257void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
7258 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7259 locations->SetOut(Location::ConstantLocation(constant));
7260}
7261
7262void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7263 // Will be generated at use site.
7264}
7265
7266void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7267 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7268 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7269}
7270
7271void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7272 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007273 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007274 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007275 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007276}
7277
7278void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7279 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7280 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007281 Location receiver = invoke->GetLocations()->InAt(0);
7282 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007283 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007284
7285 // Set the hidden argument.
7286 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7287 invoke->GetDexMethodIndex());
7288
7289 // temp = object->GetClass();
7290 if (receiver.IsStackSlot()) {
7291 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7292 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7293 } else {
7294 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7295 }
7296 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007297 // Instead of simply (possibly) unpoisoning `temp` here, we should
7298 // emit a read barrier for the previous class reference load.
7299 // However this is not required in practice, as this is an
7300 // intermediate/temporary reference and because the current
7301 // concurrent copying collector keeps the from-space memory
7302 // intact/accessible until the end of the marking phase (the
7303 // concurrent copying collector may not in the future).
7304 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007305 __ LoadFromOffset(kLoadWord, temp, temp,
7306 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7307 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007308 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007309 // temp = temp->GetImtEntryAt(method_offset);
7310 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7311 // T9 = temp->GetEntryPoint();
7312 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7313 // T9();
7314 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007315 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007316 DCHECK(!codegen_->IsLeafMethod());
7317 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7318}
7319
7320void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007321 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7322 if (intrinsic.TryDispatch(invoke)) {
7323 return;
7324 }
7325
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007326 HandleInvoke(invoke);
7327}
7328
7329void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007330 // Explicit clinit checks triggered by static invokes must have been pruned by
7331 // art::PrepareForRegisterAllocation.
7332 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007333
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007334 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko65979462017-05-19 17:25:12 +01007335 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007336
Chris Larsen701566a2015-10-27 15:29:13 -07007337 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7338 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007339 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7340 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7341 }
Chris Larsen701566a2015-10-27 15:29:13 -07007342 return;
7343 }
7344
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007345 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007346
7347 // Add the extra input register if either the dex cache array base register
7348 // or the PC-relative base register for accessing literals is needed.
7349 if (has_extra_input) {
7350 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7351 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007352}
7353
Orion Hodsonac141392017-01-13 11:53:47 +00007354void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7355 HandleInvoke(invoke);
7356}
7357
7358void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7359 codegen_->GenerateInvokePolymorphicCall(invoke);
7360}
7361
Chris Larsen701566a2015-10-27 15:29:13 -07007362static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007363 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007364 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7365 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007366 return true;
7367 }
7368 return false;
7369}
7370
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007371HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007372 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007373 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007374 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007375 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
Vladimir Markoaad75c62016-10-03 08:46:48 +00007376 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007377 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007378 bool is_r6 = GetInstructionSetFeatures().IsR6();
7379 bool fallback_load = has_irreducible_loops && !is_r6;
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;
Vladimir Marko764d4542017-05-16 10:31:41 +01007386 case HLoadString::LoadKind::kBootImageAddress:
7387 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007388 case HLoadString::LoadKind::kJitTableAddress:
7389 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007390 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007391 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007392 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007393 fallback_load = false;
7394 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007395 }
7396 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007397 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007398 }
7399 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007400}
7401
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007402HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7403 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007404 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007405 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007406 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
7407 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007408 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007409 bool is_r6 = GetInstructionSetFeatures().IsR6();
7410 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007411 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007412 case HLoadClass::LoadKind::kInvalid:
7413 LOG(FATAL) << "UNREACHABLE";
7414 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007415 case HLoadClass::LoadKind::kReferrersClass:
7416 fallback_load = false;
7417 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007418 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007419 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007420 case HLoadClass::LoadKind::kBssEntry:
7421 DCHECK(!Runtime::Current()->UseJitCompilation());
7422 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007423 case HLoadClass::LoadKind::kBootImageAddress:
7424 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007425 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007426 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007427 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007428 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007429 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007430 fallback_load = false;
7431 break;
7432 }
7433 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007434 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007435 }
7436 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007437}
7438
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007439Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7440 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007441 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007442 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7443 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7444 if (!invoke->GetLocations()->Intrinsified()) {
7445 return location.AsRegister<Register>();
7446 }
7447 // For intrinsics we allow any location, so it may be on the stack.
7448 if (!location.IsRegister()) {
7449 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7450 return temp;
7451 }
7452 // For register locations, check if the register was saved. If so, get it from the stack.
7453 // Note: There is a chance that the register was saved but not overwritten, so we could
7454 // save one load. However, since this is just an intrinsic slow path we prefer this
7455 // simple and more robust approach rather that trying to determine if that's the case.
7456 SlowPathCode* slow_path = GetCurrentSlowPath();
7457 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7458 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7459 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7460 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7461 return temp;
7462 }
7463 return location.AsRegister<Register>();
7464}
7465
Vladimir Markodc151b22015-10-15 18:02:30 +01007466HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7467 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007468 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007469 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007470 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007471 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007472 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
7473 // with irreducible loops.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007474 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007475 bool is_r6 = GetInstructionSetFeatures().IsR6();
7476 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007477 switch (dispatch_info.method_load_kind) {
Vladimir Marko65979462017-05-19 17:25:12 +01007478 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007479 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007480 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01007481 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007482 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01007483 break;
7484 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007485 if (fallback_load) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007486 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007487 dispatch_info.method_load_data = 0;
7488 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007489 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007490}
7491
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007492void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7493 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007494 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007495 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007496 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7497 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007498 bool is_r6 = GetInstructionSetFeatures().IsR6();
Vladimir Marko65979462017-05-19 17:25:12 +01007499 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007500 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7501 : ZERO;
7502
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007503 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007504 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007505 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007506 uint32_t offset =
7507 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007508 __ LoadFromOffset(kLoadWord,
7509 temp.AsRegister<Register>(),
7510 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007511 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007512 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007513 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007514 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007515 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007516 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007517 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7518 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007519 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7520 PcRelativePatchInfo* info_low =
7521 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007522 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007523 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7524 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007525 break;
7526 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007527 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7528 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7529 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007530 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007531 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007532 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007533 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7534 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007535 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007536 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7537 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007538 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007539 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007540 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7541 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7542 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007543 }
7544 }
7545
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007546 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007547 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007548 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007549 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007550 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7551 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007552 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007553 T9,
7554 callee_method.AsRegister<Register>(),
7555 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007556 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007557 // T9()
7558 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007559 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007560 break;
7561 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007562 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7563
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007564 DCHECK(!IsLeafMethod());
7565}
7566
7567void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007568 // Explicit clinit checks triggered by static invokes must have been pruned by
7569 // art::PrepareForRegisterAllocation.
7570 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007571
7572 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7573 return;
7574 }
7575
7576 LocationSummary* locations = invoke->GetLocations();
7577 codegen_->GenerateStaticOrDirectCall(invoke,
7578 locations->HasTemps()
7579 ? locations->GetTemp(0)
7580 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007581}
7582
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007583void CodeGeneratorMIPS::GenerateVirtualCall(
7584 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007585 // Use the calling convention instead of the location of the receiver, as
7586 // intrinsics may have put the receiver in a different register. In the intrinsics
7587 // slow path, the arguments have been moved to the right place, so here we are
7588 // guaranteed that the receiver is the first register of the calling convention.
7589 InvokeDexCallingConvention calling_convention;
7590 Register receiver = calling_convention.GetRegisterAt(0);
7591
Chris Larsen3acee732015-11-18 13:31:08 -08007592 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007593 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7594 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7595 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007596 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007597
7598 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007599 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007600 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007601 // Instead of simply (possibly) unpoisoning `temp` here, we should
7602 // emit a read barrier for the previous class reference load.
7603 // However this is not required in practice, as this is an
7604 // intermediate/temporary reference and because the current
7605 // concurrent copying collector keeps the from-space memory
7606 // intact/accessible until the end of the marking phase (the
7607 // concurrent copying collector may not in the future).
7608 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007609 // temp = temp->GetMethodAt(method_offset);
7610 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7611 // T9 = temp->GetEntryPoint();
7612 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7613 // T9();
7614 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007615 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007616 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007617}
7618
7619void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7620 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7621 return;
7622 }
7623
7624 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007625 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007626}
7627
7628void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007629 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007630 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007631 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007632 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7633 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007634 return;
7635 }
Vladimir Marko41559982017-01-06 14:04:23 +00007636 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007637 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze15958152017-02-09 19:08:30 -08007638 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7639 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007640 ? LocationSummary::kCallOnSlowPath
7641 : LocationSummary::kNoCall;
7642 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007643 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7644 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7645 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007646 switch (load_kind) {
7647 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007648 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007649 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007650 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007651 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007652 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007653 break;
7654 }
7655 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007656 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007657 locations->SetInAt(0, Location::RequiresRegister());
7658 break;
7659 default:
7660 break;
7661 }
7662 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007663 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7664 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7665 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007666 // Request a temp to hold the BSS entry location for the slow path.
7667 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007668 RegisterSet caller_saves = RegisterSet::Empty();
7669 InvokeRuntimeCallingConvention calling_convention;
7670 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7671 locations->SetCustomSlowPathCallerSaves(caller_saves);
7672 } else {
7673 // For non-Baker read barriers we have a temp-clobbering call.
7674 }
7675 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007676}
7677
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007678// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7679// move.
7680void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007681 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007682 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007683 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007684 return;
7685 }
Vladimir Marko41559982017-01-06 14:04:23 +00007686 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007687
Vladimir Marko41559982017-01-06 14:04:23 +00007688 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007689 Location out_loc = locations->Out();
7690 Register out = out_loc.AsRegister<Register>();
7691 Register base_or_current_method_reg;
7692 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7693 switch (load_kind) {
7694 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007695 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007696 case HLoadClass::LoadKind::kBootImageAddress:
7697 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007698 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7699 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007700 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007701 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007702 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7703 break;
7704 default:
7705 base_or_current_method_reg = ZERO;
7706 break;
7707 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007708
Alexey Frunze15958152017-02-09 19:08:30 -08007709 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7710 ? kWithoutReadBarrier
7711 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007712 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007713 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007714 switch (load_kind) {
7715 case HLoadClass::LoadKind::kReferrersClass: {
7716 DCHECK(!cls->CanCallRuntime());
7717 DCHECK(!cls->MustGenerateClinitCheck());
7718 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7719 GenerateGcRootFieldLoad(cls,
7720 out_loc,
7721 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007722 ArtMethod::DeclaringClassOffset().Int32Value(),
7723 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007724 break;
7725 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007726 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007727 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007728 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007729 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007730 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007731 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7732 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007733 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7734 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007735 base_or_current_method_reg);
7736 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007737 break;
7738 }
7739 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007740 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007741 uint32_t address = dchecked_integral_cast<uint32_t>(
7742 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7743 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007744 __ LoadLiteral(out,
7745 base_or_current_method_reg,
7746 codegen_->DeduplicateBootImageAddressLiteral(address));
7747 break;
7748 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007749 case HLoadClass::LoadKind::kBootImageClassTable: {
7750 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7751 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7752 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7753 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7754 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7755 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7756 out,
7757 base_or_current_method_reg);
7758 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7759 // Extract the reference from the slot data, i.e. clear the hash bits.
7760 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7761 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7762 if (masked_hash != 0) {
7763 __ Addiu(out, out, -masked_hash);
7764 }
7765 break;
7766 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007767 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007768 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7769 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7770 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007771 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007772 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007773 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7774 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007775 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007776 GenerateGcRootFieldLoad(cls,
7777 out_loc,
7778 temp,
7779 /* placeholder */ 0x5678,
7780 read_barrier_option,
7781 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007782 generate_null_check = true;
7783 break;
7784 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007785 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007786 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7787 cls->GetTypeIndex(),
7788 cls->GetClass());
7789 bool reordering = __ SetReorder(false);
7790 __ Bind(&info->high_label);
7791 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007792 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007793 GenerateGcRootFieldLoad(cls,
7794 out_loc,
7795 out,
7796 /* placeholder */ 0x5678,
7797 read_barrier_option,
7798 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007799 break;
7800 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007801 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007802 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007803 LOG(FATAL) << "UNREACHABLE";
7804 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007805 }
7806
7807 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7808 DCHECK(cls->CanCallRuntime());
7809 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007810 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007811 codegen_->AddSlowPath(slow_path);
7812 if (generate_null_check) {
7813 __ Beqz(out, slow_path->GetEntryLabel());
7814 }
7815 if (cls->MustGenerateClinitCheck()) {
7816 GenerateClassInitializationCheck(slow_path, out);
7817 } else {
7818 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007819 }
7820 }
7821}
7822
7823static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007824 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007825}
7826
7827void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7828 LocationSummary* locations =
7829 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7830 locations->SetOut(Location::RequiresRegister());
7831}
7832
7833void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7834 Register out = load->GetLocations()->Out().AsRegister<Register>();
7835 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7836}
7837
7838void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7839 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7840}
7841
7842void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7843 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7844}
7845
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007846void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007847 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007848 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007849 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007850 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007851 switch (load_kind) {
7852 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007853 case HLoadString::LoadKind::kBootImageAddress:
7854 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007855 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007856 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007857 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007858 break;
7859 }
7860 FALLTHROUGH_INTENDED;
7861 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007862 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007863 locations->SetInAt(0, Location::RequiresRegister());
7864 break;
7865 default:
7866 break;
7867 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007868 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007869 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007870 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007871 } else {
7872 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007873 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7874 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7875 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007876 // Request a temp to hold the BSS entry location for the slow path.
7877 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007878 RegisterSet caller_saves = RegisterSet::Empty();
7879 InvokeRuntimeCallingConvention calling_convention;
7880 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7881 locations->SetCustomSlowPathCallerSaves(caller_saves);
7882 } else {
7883 // For non-Baker read barriers we have a temp-clobbering call.
7884 }
7885 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007886 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007887}
7888
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007889// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7890// move.
7891void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007892 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007893 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007894 Location out_loc = locations->Out();
7895 Register out = out_loc.AsRegister<Register>();
7896 Register base_or_current_method_reg;
7897 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7898 switch (load_kind) {
7899 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007900 case HLoadString::LoadKind::kBootImageAddress:
7901 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007902 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007903 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007904 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7905 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007906 default:
7907 base_or_current_method_reg = ZERO;
7908 break;
7909 }
7910
7911 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007912 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007913 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007914 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007915 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007916 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7917 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007918 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7919 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007920 base_or_current_method_reg);
7921 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007922 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007923 }
7924 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007925 uint32_t address = dchecked_integral_cast<uint32_t>(
7926 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7927 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007928 __ LoadLiteral(out,
7929 base_or_current_method_reg,
7930 codegen_->DeduplicateBootImageAddressLiteral(address));
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007931 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007932 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007933 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007934 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007935 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007936 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007937 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7938 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007939 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7940 out,
7941 base_or_current_method_reg);
7942 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7943 return;
7944 }
7945 case HLoadString::LoadKind::kBssEntry: {
7946 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7947 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7948 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
7949 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7950 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007951 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007952 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007953 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7954 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007955 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007956 GenerateGcRootFieldLoad(load,
7957 out_loc,
7958 temp,
7959 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007960 kCompilerReadBarrierOption,
7961 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007962 SlowPathCodeMIPS* slow_path =
7963 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007964 codegen_->AddSlowPath(slow_path);
7965 __ Beqz(out, slow_path->GetEntryLabel());
7966 __ Bind(slow_path->GetExitLabel());
7967 return;
7968 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007969 case HLoadString::LoadKind::kJitTableAddress: {
7970 CodeGeneratorMIPS::JitPatchInfo* info =
7971 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7972 load->GetStringIndex(),
7973 load->GetString());
7974 bool reordering = __ SetReorder(false);
7975 __ Bind(&info->high_label);
7976 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007977 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08007978 GenerateGcRootFieldLoad(load,
7979 out_loc,
7980 out,
7981 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007982 kCompilerReadBarrierOption,
7983 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007984 return;
7985 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007986 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007987 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007988 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007989
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007990 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007991 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007992 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007993 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007994 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007995 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7996 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007997}
7998
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007999void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
8000 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
8001 locations->SetOut(Location::ConstantLocation(constant));
8002}
8003
8004void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
8005 // Will be generated at use site.
8006}
8007
8008void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8009 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008010 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008011 InvokeRuntimeCallingConvention calling_convention;
8012 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8013}
8014
8015void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
8016 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008017 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008018 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8019 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008020 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008021 }
8022 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8023}
8024
8025void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8026 LocationSummary* locations =
8027 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
8028 switch (mul->GetResultType()) {
8029 case Primitive::kPrimInt:
8030 case Primitive::kPrimLong:
8031 locations->SetInAt(0, Location::RequiresRegister());
8032 locations->SetInAt(1, Location::RequiresRegister());
8033 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8034 break;
8035
8036 case Primitive::kPrimFloat:
8037 case Primitive::kPrimDouble:
8038 locations->SetInAt(0, Location::RequiresFpuRegister());
8039 locations->SetInAt(1, Location::RequiresFpuRegister());
8040 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8041 break;
8042
8043 default:
8044 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8045 }
8046}
8047
8048void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
8049 Primitive::Type type = instruction->GetType();
8050 LocationSummary* locations = instruction->GetLocations();
8051 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8052
8053 switch (type) {
8054 case Primitive::kPrimInt: {
8055 Register dst = locations->Out().AsRegister<Register>();
8056 Register lhs = locations->InAt(0).AsRegister<Register>();
8057 Register rhs = locations->InAt(1).AsRegister<Register>();
8058
8059 if (isR6) {
8060 __ MulR6(dst, lhs, rhs);
8061 } else {
8062 __ MulR2(dst, lhs, rhs);
8063 }
8064 break;
8065 }
8066 case Primitive::kPrimLong: {
8067 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8068 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8069 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8070 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8071 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8072 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8073
8074 // Extra checks to protect caused by the existance of A1_A2.
8075 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8076 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8077 DCHECK_NE(dst_high, lhs_low);
8078 DCHECK_NE(dst_high, rhs_low);
8079
8080 // A_B * C_D
8081 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8082 // dst_lo: [ low(B*D) ]
8083 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8084
8085 if (isR6) {
8086 __ MulR6(TMP, lhs_high, rhs_low);
8087 __ MulR6(dst_high, lhs_low, rhs_high);
8088 __ Addu(dst_high, dst_high, TMP);
8089 __ MuhuR6(TMP, lhs_low, rhs_low);
8090 __ Addu(dst_high, dst_high, TMP);
8091 __ MulR6(dst_low, lhs_low, rhs_low);
8092 } else {
8093 __ MulR2(TMP, lhs_high, rhs_low);
8094 __ MulR2(dst_high, lhs_low, rhs_high);
8095 __ Addu(dst_high, dst_high, TMP);
8096 __ MultuR2(lhs_low, rhs_low);
8097 __ Mfhi(TMP);
8098 __ Addu(dst_high, dst_high, TMP);
8099 __ Mflo(dst_low);
8100 }
8101 break;
8102 }
8103 case Primitive::kPrimFloat:
8104 case Primitive::kPrimDouble: {
8105 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8106 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8107 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
8108 if (type == Primitive::kPrimFloat) {
8109 __ MulS(dst, lhs, rhs);
8110 } else {
8111 __ MulD(dst, lhs, rhs);
8112 }
8113 break;
8114 }
8115 default:
8116 LOG(FATAL) << "Unexpected mul type " << type;
8117 }
8118}
8119
8120void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8121 LocationSummary* locations =
8122 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
8123 switch (neg->GetResultType()) {
8124 case Primitive::kPrimInt:
8125 case Primitive::kPrimLong:
8126 locations->SetInAt(0, Location::RequiresRegister());
8127 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8128 break;
8129
8130 case Primitive::kPrimFloat:
8131 case Primitive::kPrimDouble:
8132 locations->SetInAt(0, Location::RequiresFpuRegister());
8133 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8134 break;
8135
8136 default:
8137 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8138 }
8139}
8140
8141void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
8142 Primitive::Type type = instruction->GetType();
8143 LocationSummary* locations = instruction->GetLocations();
8144
8145 switch (type) {
8146 case Primitive::kPrimInt: {
8147 Register dst = locations->Out().AsRegister<Register>();
8148 Register src = locations->InAt(0).AsRegister<Register>();
8149 __ Subu(dst, ZERO, src);
8150 break;
8151 }
8152 case Primitive::kPrimLong: {
8153 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8154 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8155 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8156 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8157 __ Subu(dst_low, ZERO, src_low);
8158 __ Sltu(TMP, ZERO, dst_low);
8159 __ Subu(dst_high, ZERO, src_high);
8160 __ Subu(dst_high, dst_high, TMP);
8161 break;
8162 }
8163 case Primitive::kPrimFloat:
8164 case Primitive::kPrimDouble: {
8165 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8166 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8167 if (type == Primitive::kPrimFloat) {
8168 __ NegS(dst, src);
8169 } else {
8170 __ NegD(dst, src);
8171 }
8172 break;
8173 }
8174 default:
8175 LOG(FATAL) << "Unexpected neg type " << type;
8176 }
8177}
8178
8179void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
8180 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008181 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008182 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008183 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008184 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8185 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008186}
8187
8188void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008189 // Note: if heap poisoning is enabled, the entry point takes care
8190 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008191 QuickEntrypointEnum entrypoint =
8192 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8193 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008194 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008195 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008196}
8197
8198void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
8199 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008200 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008201 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008202 if (instruction->IsStringAlloc()) {
8203 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8204 } else {
8205 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008206 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008207 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
8208}
8209
8210void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008211 // Note: if heap poisoning is enabled, the entry point takes care
8212 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008213 if (instruction->IsStringAlloc()) {
8214 // String is allocated through StringFactory. Call NewEmptyString entry point.
8215 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008216 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008217 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8218 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8219 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008220 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008221 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8222 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008223 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008224 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008225 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008226}
8227
8228void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
8229 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8230 locations->SetInAt(0, Location::RequiresRegister());
8231 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8232}
8233
8234void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
8235 Primitive::Type type = instruction->GetType();
8236 LocationSummary* locations = instruction->GetLocations();
8237
8238 switch (type) {
8239 case Primitive::kPrimInt: {
8240 Register dst = locations->Out().AsRegister<Register>();
8241 Register src = locations->InAt(0).AsRegister<Register>();
8242 __ Nor(dst, src, ZERO);
8243 break;
8244 }
8245
8246 case Primitive::kPrimLong: {
8247 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8248 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8249 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8250 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8251 __ Nor(dst_high, src_high, ZERO);
8252 __ Nor(dst_low, src_low, ZERO);
8253 break;
8254 }
8255
8256 default:
8257 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8258 }
8259}
8260
8261void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8262 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8263 locations->SetInAt(0, Location::RequiresRegister());
8264 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8265}
8266
8267void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8268 LocationSummary* locations = instruction->GetLocations();
8269 __ Xori(locations->Out().AsRegister<Register>(),
8270 locations->InAt(0).AsRegister<Register>(),
8271 1);
8272}
8273
8274void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008275 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8276 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008277}
8278
Calin Juravle2ae48182016-03-16 14:05:09 +00008279void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8280 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008281 return;
8282 }
8283 Location obj = instruction->GetLocations()->InAt(0);
8284
8285 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008286 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008287}
8288
Calin Juravle2ae48182016-03-16 14:05:09 +00008289void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008290 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008291 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008292
8293 Location obj = instruction->GetLocations()->InAt(0);
8294
8295 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8296}
8297
8298void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008299 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008300}
8301
8302void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8303 HandleBinaryOp(instruction);
8304}
8305
8306void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8307 HandleBinaryOp(instruction);
8308}
8309
8310void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8311 LOG(FATAL) << "Unreachable";
8312}
8313
8314void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
8315 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8316}
8317
8318void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
8319 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8320 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8321 if (location.IsStackSlot()) {
8322 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8323 } else if (location.IsDoubleStackSlot()) {
8324 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8325 }
8326 locations->SetOut(location);
8327}
8328
8329void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8330 ATTRIBUTE_UNUSED) {
8331 // Nothing to do, the parameter is already at its location.
8332}
8333
8334void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8335 LocationSummary* locations =
8336 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8337 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8338}
8339
8340void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8341 ATTRIBUTE_UNUSED) {
8342 // Nothing to do, the method is already at its location.
8343}
8344
8345void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
8346 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008347 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008348 locations->SetInAt(i, Location::Any());
8349 }
8350 locations->SetOut(Location::Any());
8351}
8352
8353void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8354 LOG(FATAL) << "Unreachable";
8355}
8356
8357void LocationsBuilderMIPS::VisitRem(HRem* rem) {
8358 Primitive::Type type = rem->GetResultType();
8359 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008360 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008361 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
8362
8363 switch (type) {
8364 case Primitive::kPrimInt:
8365 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008366 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008367 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8368 break;
8369
8370 case Primitive::kPrimLong: {
8371 InvokeRuntimeCallingConvention calling_convention;
8372 locations->SetInAt(0, Location::RegisterPairLocation(
8373 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8374 locations->SetInAt(1, Location::RegisterPairLocation(
8375 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8376 locations->SetOut(calling_convention.GetReturnLocation(type));
8377 break;
8378 }
8379
8380 case Primitive::kPrimFloat:
8381 case Primitive::kPrimDouble: {
8382 InvokeRuntimeCallingConvention calling_convention;
8383 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8384 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8385 locations->SetOut(calling_convention.GetReturnLocation(type));
8386 break;
8387 }
8388
8389 default:
8390 LOG(FATAL) << "Unexpected rem type " << type;
8391 }
8392}
8393
8394void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
8395 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008396
8397 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08008398 case Primitive::kPrimInt:
8399 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008400 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008401 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008402 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008403 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8404 break;
8405 }
8406 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008407 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008408 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008409 break;
8410 }
8411 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008412 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008413 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008414 break;
8415 }
8416 default:
8417 LOG(FATAL) << "Unexpected rem type " << type;
8418 }
8419}
8420
Igor Murashkind01745e2017-04-05 16:40:31 -07008421void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8422 constructor_fence->SetLocations(nullptr);
8423}
8424
8425void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8426 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8427 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8428}
8429
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008430void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8431 memory_barrier->SetLocations(nullptr);
8432}
8433
8434void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8435 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8436}
8437
8438void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8439 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
8440 Primitive::Type return_type = ret->InputAt(0)->GetType();
8441 locations->SetInAt(0, MipsReturnLocation(return_type));
8442}
8443
8444void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8445 codegen_->GenerateFrameExit();
8446}
8447
8448void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8449 ret->SetLocations(nullptr);
8450}
8451
8452void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8453 codegen_->GenerateFrameExit();
8454}
8455
Alexey Frunze92d90602015-12-18 18:16:36 -08008456void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8457 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008458}
8459
Alexey Frunze92d90602015-12-18 18:16:36 -08008460void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8461 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008462}
8463
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008464void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8465 HandleShift(shl);
8466}
8467
8468void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8469 HandleShift(shl);
8470}
8471
8472void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8473 HandleShift(shr);
8474}
8475
8476void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8477 HandleShift(shr);
8478}
8479
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008480void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8481 HandleBinaryOp(instruction);
8482}
8483
8484void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8485 HandleBinaryOp(instruction);
8486}
8487
8488void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8489 HandleFieldGet(instruction, instruction->GetFieldInfo());
8490}
8491
8492void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8493 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8494}
8495
8496void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8497 HandleFieldSet(instruction, instruction->GetFieldInfo());
8498}
8499
8500void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008501 HandleFieldSet(instruction,
8502 instruction->GetFieldInfo(),
8503 instruction->GetDexPc(),
8504 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008505}
8506
8507void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8508 HUnresolvedInstanceFieldGet* instruction) {
8509 FieldAccessCallingConventionMIPS calling_convention;
8510 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8511 instruction->GetFieldType(),
8512 calling_convention);
8513}
8514
8515void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8516 HUnresolvedInstanceFieldGet* instruction) {
8517 FieldAccessCallingConventionMIPS calling_convention;
8518 codegen_->GenerateUnresolvedFieldAccess(instruction,
8519 instruction->GetFieldType(),
8520 instruction->GetFieldIndex(),
8521 instruction->GetDexPc(),
8522 calling_convention);
8523}
8524
8525void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8526 HUnresolvedInstanceFieldSet* instruction) {
8527 FieldAccessCallingConventionMIPS calling_convention;
8528 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8529 instruction->GetFieldType(),
8530 calling_convention);
8531}
8532
8533void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8534 HUnresolvedInstanceFieldSet* instruction) {
8535 FieldAccessCallingConventionMIPS calling_convention;
8536 codegen_->GenerateUnresolvedFieldAccess(instruction,
8537 instruction->GetFieldType(),
8538 instruction->GetFieldIndex(),
8539 instruction->GetDexPc(),
8540 calling_convention);
8541}
8542
8543void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8544 HUnresolvedStaticFieldGet* instruction) {
8545 FieldAccessCallingConventionMIPS calling_convention;
8546 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8547 instruction->GetFieldType(),
8548 calling_convention);
8549}
8550
8551void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8552 HUnresolvedStaticFieldGet* instruction) {
8553 FieldAccessCallingConventionMIPS calling_convention;
8554 codegen_->GenerateUnresolvedFieldAccess(instruction,
8555 instruction->GetFieldType(),
8556 instruction->GetFieldIndex(),
8557 instruction->GetDexPc(),
8558 calling_convention);
8559}
8560
8561void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8562 HUnresolvedStaticFieldSet* instruction) {
8563 FieldAccessCallingConventionMIPS calling_convention;
8564 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8565 instruction->GetFieldType(),
8566 calling_convention);
8567}
8568
8569void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8570 HUnresolvedStaticFieldSet* instruction) {
8571 FieldAccessCallingConventionMIPS calling_convention;
8572 codegen_->GenerateUnresolvedFieldAccess(instruction,
8573 instruction->GetFieldType(),
8574 instruction->GetFieldIndex(),
8575 instruction->GetDexPc(),
8576 calling_convention);
8577}
8578
8579void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008580 LocationSummary* locations =
8581 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008582 // In suspend check slow path, usually there are no caller-save registers at all.
8583 // If SIMD instructions are present, however, we force spilling all live SIMD
8584 // registers in full width (since the runtime only saves/restores lower part).
8585 locations->SetCustomSlowPathCallerSaves(
8586 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008587}
8588
8589void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8590 HBasicBlock* block = instruction->GetBlock();
8591 if (block->GetLoopInformation() != nullptr) {
8592 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8593 // The back edge will generate the suspend check.
8594 return;
8595 }
8596 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8597 // The goto will generate the suspend check.
8598 return;
8599 }
8600 GenerateSuspendCheck(instruction, nullptr);
8601}
8602
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008603void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8604 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008605 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008606 InvokeRuntimeCallingConvention calling_convention;
8607 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8608}
8609
8610void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008611 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008612 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8613}
8614
8615void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8616 Primitive::Type input_type = conversion->GetInputType();
8617 Primitive::Type result_type = conversion->GetResultType();
8618 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008619 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008620
8621 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
8622 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
8623 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8624 }
8625
8626 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008627 if (!isR6 &&
8628 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
8629 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008630 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008631 }
8632
8633 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8634
8635 if (call_kind == LocationSummary::kNoCall) {
8636 if (Primitive::IsFloatingPointType(input_type)) {
8637 locations->SetInAt(0, Location::RequiresFpuRegister());
8638 } else {
8639 locations->SetInAt(0, Location::RequiresRegister());
8640 }
8641
8642 if (Primitive::IsFloatingPointType(result_type)) {
8643 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8644 } else {
8645 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8646 }
8647 } else {
8648 InvokeRuntimeCallingConvention calling_convention;
8649
8650 if (Primitive::IsFloatingPointType(input_type)) {
8651 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8652 } else {
8653 DCHECK_EQ(input_type, Primitive::kPrimLong);
8654 locations->SetInAt(0, Location::RegisterPairLocation(
8655 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8656 }
8657
8658 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8659 }
8660}
8661
8662void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8663 LocationSummary* locations = conversion->GetLocations();
8664 Primitive::Type result_type = conversion->GetResultType();
8665 Primitive::Type input_type = conversion->GetInputType();
8666 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008667 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008668
8669 DCHECK_NE(input_type, result_type);
8670
8671 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8672 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8673 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8674 Register src = locations->InAt(0).AsRegister<Register>();
8675
Alexey Frunzea871ef12016-06-27 15:20:11 -07008676 if (dst_low != src) {
8677 __ Move(dst_low, src);
8678 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008679 __ Sra(dst_high, src, 31);
8680 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8681 Register dst = locations->Out().AsRegister<Register>();
8682 Register src = (input_type == Primitive::kPrimLong)
8683 ? locations->InAt(0).AsRegisterPairLow<Register>()
8684 : locations->InAt(0).AsRegister<Register>();
8685
8686 switch (result_type) {
8687 case Primitive::kPrimChar:
8688 __ Andi(dst, src, 0xFFFF);
8689 break;
8690 case Primitive::kPrimByte:
8691 if (has_sign_extension) {
8692 __ Seb(dst, src);
8693 } else {
8694 __ Sll(dst, src, 24);
8695 __ Sra(dst, dst, 24);
8696 }
8697 break;
8698 case Primitive::kPrimShort:
8699 if (has_sign_extension) {
8700 __ Seh(dst, src);
8701 } else {
8702 __ Sll(dst, src, 16);
8703 __ Sra(dst, dst, 16);
8704 }
8705 break;
8706 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008707 if (dst != src) {
8708 __ Move(dst, src);
8709 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008710 break;
8711
8712 default:
8713 LOG(FATAL) << "Unexpected type conversion from " << input_type
8714 << " to " << result_type;
8715 }
8716 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008717 if (input_type == Primitive::kPrimLong) {
8718 if (isR6) {
8719 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8720 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8721 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8722 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8723 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8724 __ Mtc1(src_low, FTMP);
8725 __ Mthc1(src_high, FTMP);
8726 if (result_type == Primitive::kPrimFloat) {
8727 __ Cvtsl(dst, FTMP);
8728 } else {
8729 __ Cvtdl(dst, FTMP);
8730 }
8731 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008732 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8733 : kQuickL2d;
8734 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008735 if (result_type == Primitive::kPrimFloat) {
8736 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8737 } else {
8738 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8739 }
8740 }
8741 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008742 Register src = locations->InAt(0).AsRegister<Register>();
8743 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8744 __ Mtc1(src, FTMP);
8745 if (result_type == Primitive::kPrimFloat) {
8746 __ Cvtsw(dst, FTMP);
8747 } else {
8748 __ Cvtdw(dst, FTMP);
8749 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008750 }
8751 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8752 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008753
8754 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8755 // value of the output type if the input is outside of the range after the truncation or
8756 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8757 // results. This matches the desired float/double-to-int/long conversion exactly.
8758 //
8759 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8760 // value when the input is either a NaN or is outside of the range of the output type
8761 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8762 // the same result.
8763 //
8764 // The code takes care of the different behaviors by first comparing the input to the
8765 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8766 // If the input is greater than or equal to the minimum, it procedes to the truncate
8767 // instruction, which will handle such an input the same way irrespective of NAN2008.
8768 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8769 // in order to return either zero or the minimum value.
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008770 if (result_type == Primitive::kPrimLong) {
8771 if (isR6) {
8772 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8773 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8774 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8775 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8776 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008777
8778 if (input_type == Primitive::kPrimFloat) {
8779 __ TruncLS(FTMP, src);
8780 } else {
8781 __ TruncLD(FTMP, src);
8782 }
8783 __ Mfc1(dst_low, FTMP);
8784 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008785 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008786 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8787 : kQuickD2l;
8788 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008789 if (input_type == Primitive::kPrimFloat) {
8790 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8791 } else {
8792 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8793 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008794 }
8795 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008796 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8797 Register dst = locations->Out().AsRegister<Register>();
8798 MipsLabel truncate;
8799 MipsLabel done;
8800
Lena Djokicf4e23a82017-05-09 15:43:45 +02008801 if (!isR6) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008802 if (input_type == Primitive::kPrimFloat) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008803 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8804 __ LoadConst32(TMP, min_val);
8805 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008806 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008807 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8808 __ LoadConst32(TMP, High32Bits(min_val));
8809 __ Mtc1(ZERO, FTMP);
8810 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008811 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008812
8813 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008814 __ ColeS(0, FTMP, src);
8815 } else {
8816 __ ColeD(0, FTMP, src);
8817 }
8818 __ Bc1t(0, &truncate);
8819
8820 if (input_type == Primitive::kPrimFloat) {
8821 __ CeqS(0, src, src);
8822 } else {
8823 __ CeqD(0, src, src);
8824 }
8825 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8826 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008827
8828 __ B(&done);
8829
8830 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008831 }
8832
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008833 if (input_type == Primitive::kPrimFloat) {
8834 __ TruncWS(FTMP, src);
8835 } else {
8836 __ TruncWD(FTMP, src);
8837 }
8838 __ Mfc1(dst, FTMP);
8839
Lena Djokicf4e23a82017-05-09 15:43:45 +02008840 if (!isR6) {
8841 __ Bind(&done);
8842 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008843 }
8844 } else if (Primitive::IsFloatingPointType(result_type) &&
8845 Primitive::IsFloatingPointType(input_type)) {
8846 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8847 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8848 if (result_type == Primitive::kPrimFloat) {
8849 __ Cvtsd(dst, src);
8850 } else {
8851 __ Cvtds(dst, src);
8852 }
8853 } else {
8854 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8855 << " to " << result_type;
8856 }
8857}
8858
8859void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8860 HandleShift(ushr);
8861}
8862
8863void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8864 HandleShift(ushr);
8865}
8866
8867void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8868 HandleBinaryOp(instruction);
8869}
8870
8871void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8872 HandleBinaryOp(instruction);
8873}
8874
8875void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8876 // Nothing to do, this should be removed during prepare for register allocator.
8877 LOG(FATAL) << "Unreachable";
8878}
8879
8880void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8881 // Nothing to do, this should be removed during prepare for register allocator.
8882 LOG(FATAL) << "Unreachable";
8883}
8884
8885void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008886 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008887}
8888
8889void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008890 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008891}
8892
8893void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008894 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008895}
8896
8897void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008898 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008899}
8900
8901void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008902 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008903}
8904
8905void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008906 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008907}
8908
8909void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008910 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008911}
8912
8913void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008914 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008915}
8916
8917void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008918 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008919}
8920
8921void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008922 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008923}
8924
8925void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008926 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008927}
8928
8929void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008930 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008931}
8932
8933void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008934 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008935}
8936
8937void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008938 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008939}
8940
8941void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008942 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008943}
8944
8945void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008946 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008947}
8948
8949void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008950 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008951}
8952
8953void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008954 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008955}
8956
8957void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008958 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008959}
8960
8961void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008962 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008963}
8964
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008965void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8966 LocationSummary* locations =
8967 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8968 locations->SetInAt(0, Location::RequiresRegister());
8969}
8970
Alexey Frunze96b66822016-09-10 02:32:44 -07008971void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8972 int32_t lower_bound,
8973 uint32_t num_entries,
8974 HBasicBlock* switch_block,
8975 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008976 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008977 Register temp_reg = TMP;
8978 __ Addiu32(temp_reg, value_reg, -lower_bound);
8979 // Jump to default if index is negative
8980 // Note: We don't check the case that index is positive while value < lower_bound, because in
8981 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8982 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8983
Alexey Frunze96b66822016-09-10 02:32:44 -07008984 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008985 // Jump to successors[0] if value == lower_bound.
8986 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8987 int32_t last_index = 0;
8988 for (; num_entries - last_index > 2; last_index += 2) {
8989 __ Addiu(temp_reg, temp_reg, -2);
8990 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8991 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8992 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8993 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8994 }
8995 if (num_entries - last_index == 2) {
8996 // The last missing case_value.
8997 __ Addiu(temp_reg, temp_reg, -1);
8998 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008999 }
9000
Vladimir Markof3e0ee22015-12-17 15:23:13 +00009001 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07009002 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009003 __ B(codegen_->GetLabelOf(default_block));
9004 }
9005}
9006
Alexey Frunze96b66822016-09-10 02:32:44 -07009007void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
9008 Register constant_area,
9009 int32_t lower_bound,
9010 uint32_t num_entries,
9011 HBasicBlock* switch_block,
9012 HBasicBlock* default_block) {
9013 // Create a jump table.
9014 std::vector<MipsLabel*> labels(num_entries);
9015 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
9016 for (uint32_t i = 0; i < num_entries; i++) {
9017 labels[i] = codegen_->GetLabelOf(successors[i]);
9018 }
9019 JumpTable* table = __ CreateJumpTable(std::move(labels));
9020
9021 // Is the value in range?
9022 __ Addiu32(TMP, value_reg, -lower_bound);
9023 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9024 __ Sltiu(AT, TMP, num_entries);
9025 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9026 } else {
9027 __ LoadConst32(AT, num_entries);
9028 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9029 }
9030
9031 // We are in the range of the table.
9032 // Load the target address from the jump table, indexing by the value.
9033 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009034 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009035 __ Lw(TMP, TMP, 0);
9036 // Compute the absolute target address by adding the table start address
9037 // (the table contains offsets to targets relative to its start).
9038 __ Addu(TMP, TMP, AT);
9039 // And jump.
9040 __ Jr(TMP);
9041 __ NopIfNoReordering();
9042}
9043
9044void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9045 int32_t lower_bound = switch_instr->GetStartValue();
9046 uint32_t num_entries = switch_instr->GetNumEntries();
9047 LocationSummary* locations = switch_instr->GetLocations();
9048 Register value_reg = locations->InAt(0).AsRegister<Register>();
9049 HBasicBlock* switch_block = switch_instr->GetBlock();
9050 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9051
9052 if (codegen_->GetInstructionSetFeatures().IsR6() &&
9053 num_entries > kPackedSwitchJumpTableThreshold) {
9054 // R6 uses PC-relative addressing to access the jump table.
9055 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
9056 // the jump table and it is implemented by changing HPackedSwitch to
9057 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
9058 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
9059 GenTableBasedPackedSwitch(value_reg,
9060 ZERO,
9061 lower_bound,
9062 num_entries,
9063 switch_block,
9064 default_block);
9065 } else {
9066 GenPackedSwitchWithCompares(value_reg,
9067 lower_bound,
9068 num_entries,
9069 switch_block,
9070 default_block);
9071 }
9072}
9073
9074void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9075 LocationSummary* locations =
9076 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9077 locations->SetInAt(0, Location::RequiresRegister());
9078 // Constant area pointer (HMipsComputeBaseMethodAddress).
9079 locations->SetInAt(1, Location::RequiresRegister());
9080}
9081
9082void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9083 int32_t lower_bound = switch_instr->GetStartValue();
9084 uint32_t num_entries = switch_instr->GetNumEntries();
9085 LocationSummary* locations = switch_instr->GetLocations();
9086 Register value_reg = locations->InAt(0).AsRegister<Register>();
9087 Register constant_area = locations->InAt(1).AsRegister<Register>();
9088 HBasicBlock* switch_block = switch_instr->GetBlock();
9089 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9090
9091 // This is an R2-only path. HPackedSwitch has been changed to
9092 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9093 // required to address the jump table relative to PC.
9094 GenTableBasedPackedSwitch(value_reg,
9095 constant_area,
9096 lower_bound,
9097 num_entries,
9098 switch_block,
9099 default_block);
9100}
9101
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009102void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9103 HMipsComputeBaseMethodAddress* insn) {
9104 LocationSummary* locations =
9105 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
9106 locations->SetOut(Location::RequiresRegister());
9107}
9108
9109void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9110 HMipsComputeBaseMethodAddress* insn) {
9111 LocationSummary* locations = insn->GetLocations();
9112 Register reg = locations->Out().AsRegister<Register>();
9113
9114 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9115
9116 // Generate a dummy PC-relative call to obtain PC.
9117 __ Nal();
9118 // Grab the return address off RA.
9119 __ Move(reg, RA);
9120
9121 // Remember this offset (the obtained PC value) for later use with constant area.
9122 __ BindPcRelBaseLabel();
9123}
9124
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009125void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9126 // The trampoline uses the same calling convention as dex calling conventions,
9127 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9128 // the method_idx.
9129 HandleInvoke(invoke);
9130}
9131
9132void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9133 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9134}
9135
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009136void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9137 LocationSummary* locations =
9138 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9139 locations->SetInAt(0, Location::RequiresRegister());
9140 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009141}
9142
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009143void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9144 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009145 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009146 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009147 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009148 __ LoadFromOffset(kLoadWord,
9149 locations->Out().AsRegister<Register>(),
9150 locations->InAt(0).AsRegister<Register>(),
9151 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009152 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009153 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009154 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009155 __ LoadFromOffset(kLoadWord,
9156 locations->Out().AsRegister<Register>(),
9157 locations->InAt(0).AsRegister<Register>(),
9158 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009159 __ LoadFromOffset(kLoadWord,
9160 locations->Out().AsRegister<Register>(),
9161 locations->Out().AsRegister<Register>(),
9162 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009163 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009164}
9165
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009166#undef __
9167#undef QUICK_ENTRY_POINT
9168
9169} // namespace mips
9170} // namespace art