blob: a733fa2c87501980750ca2587a983068d3828a84 [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();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200293 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
294 Register base =
295 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700296 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000297 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700298 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
299 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700300 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
301 __ Sw(out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000302 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200303 __ B(GetExitLabel());
304 }
305
306 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
307
308 private:
309 // The class this slow path will load.
310 HLoadClass* const cls_;
311
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200312 // The dex PC of `at_`.
313 const uint32_t dex_pc_;
314
315 // Whether to initialize the class.
316 const bool do_clinit_;
317
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700318 // Pointer to the high half PC-relative patch info for HLoadClass/kBssEntry.
319 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
320
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200321 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
322};
323
324class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
325 public:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700326 explicit LoadStringSlowPathMIPS(HLoadString* instruction,
327 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high)
328 : SlowPathCodeMIPS(instruction), bss_info_high_(bss_info_high) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200329
330 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700331 DCHECK(instruction_->IsLoadString());
332 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200333 LocationSummary* locations = instruction_->GetLocations();
334 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700335 HLoadString* load = instruction_->AsLoadString();
336 const dex::StringIndex string_index = load->GetStringIndex();
337 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200338 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700339 const bool baker_or_no_read_barriers = (!kUseReadBarrier || kUseBakerReadBarrier);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700340 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200341 __ Bind(GetEntryLabel());
342 SaveLiveRegisters(codegen, locations);
343
Alexey Frunzec61c0762017-04-10 13:54:23 -0700344 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
345 Register entry_address = kNoRegister;
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700346 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700347 Register temp = locations->GetTemp(0).AsRegister<Register>();
348 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
349 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
350 // kSaveEverything call.
351 entry_address = temp_is_a0 ? out : temp;
352 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
353 if (temp_is_a0) {
354 __ Move(entry_address, temp);
355 }
356 }
357
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000358 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100359 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200360 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700361
362 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700363 if (baker_or_no_read_barriers) {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700364 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700365 DCHECK(bss_info_high_);
366 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100367 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, bss_info_high_);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700368 __ Sw(calling_convention.GetRegisterAt(0),
369 entry_address,
370 /* placeholder */ 0x5678,
371 &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700372 }
373
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200374 Primitive::Type type = instruction_->GetType();
375 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700376 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200377 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200378 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000379
Alexey Frunzec61c0762017-04-10 13:54:23 -0700380 // Store the resolved string to the BSS entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700381 if (!baker_or_no_read_barriers) {
382 // For non-Baker read barriers we need to re-calculate the address of
Alexey Frunzec61c0762017-04-10 13:54:23 -0700383 // the string entry.
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700384 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +0200385 const bool has_irreducible_loops = codegen->GetGraph()->HasIrreducibleLoops();
386 Register base =
387 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700388 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100389 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index);
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700390 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +0100391 mips_codegen->NewStringBssEntryPatch(load->GetDexFile(), string_index, info_high);
Alexey Frunzea663d9d2017-07-31 18:43:18 -0700392 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base);
393 __ Sw(out, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700394 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200395 __ B(GetExitLabel());
396 }
397
398 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
399
400 private:
Alexey Frunze5fa5c042017-06-01 21:07:52 -0700401 // Pointer to the high half PC-relative patch info.
402 const CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high_;
403
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200404 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
405};
406
407class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
408 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000409 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200410
411 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
412 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
413 __ Bind(GetEntryLabel());
414 if (instruction_->CanThrowIntoCatchBlock()) {
415 // Live registers will be restored in the catch block if caught.
416 SaveLiveRegisters(codegen, instruction_->GetLocations());
417 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100418 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200419 instruction_,
420 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100421 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200422 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
423 }
424
425 bool IsFatal() const OVERRIDE { return true; }
426
427 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
428
429 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200430 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
431};
432
433class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
434 public:
435 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000436 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200437
438 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Lena Djokicca8c2952017-05-29 11:31:46 +0200439 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200440 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
441 __ Bind(GetEntryLabel());
Lena Djokicca8c2952017-05-29 11:31:46 +0200442 SaveLiveRegisters(codegen, locations); // Only saves live vector registers for SIMD.
Serban Constantinescufca16662016-07-14 09:21:59 +0100443 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200444 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Lena Djokicca8c2952017-05-29 11:31:46 +0200445 RestoreLiveRegisters(codegen, locations); // Only restores live vector registers for SIMD.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446 if (successor_ == nullptr) {
447 __ B(GetReturnLabel());
448 } else {
449 __ B(mips_codegen->GetLabelOf(successor_));
450 }
451 }
452
453 MipsLabel* GetReturnLabel() {
454 DCHECK(successor_ == nullptr);
455 return &return_label_;
456 }
457
458 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
459
460 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200461 // If not null, the block to branch to after the suspend check.
462 HBasicBlock* const successor_;
463
464 // If `successor_` is null, the label to branch to after the suspend check.
465 MipsLabel return_label_;
466
467 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
468};
469
470class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
471 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800472 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
473 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200474
475 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
476 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200477 uint32_t dex_pc = instruction_->GetDexPc();
478 DCHECK(instruction_->IsCheckCast()
479 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
480 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
481
482 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800483 if (!is_fatal_) {
484 SaveLiveRegisters(codegen, locations);
485 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200486
487 // We're moving two locations to locations that could overlap, so we need a parallel
488 // move resolver.
489 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800490 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200491 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
492 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800493 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200494 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
495 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200496 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100497 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800498 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200499 Primitive::Type ret_type = instruction_->GetType();
500 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
501 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200502 } else {
503 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800504 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
505 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200506 }
507
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800508 if (!is_fatal_) {
509 RestoreLiveRegisters(codegen, locations);
510 __ B(GetExitLabel());
511 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200512 }
513
514 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
515
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800516 bool IsFatal() const OVERRIDE { return is_fatal_; }
517
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200518 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800519 const bool is_fatal_;
520
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200521 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
522};
523
524class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
525 public:
Aart Bik42249c32016-01-07 15:33:50 -0800526 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000527 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200528
529 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800530 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200531 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100532 LocationSummary* locations = instruction_->GetLocations();
533 SaveLiveRegisters(codegen, locations);
534 InvokeRuntimeCallingConvention calling_convention;
535 __ LoadConst32(calling_convention.GetRegisterAt(0),
536 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100537 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100538 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200539 }
540
541 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
542
543 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200544 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
545};
546
Alexey Frunze15958152017-02-09 19:08:30 -0800547class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
548 public:
549 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
550
551 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
552 LocationSummary* locations = instruction_->GetLocations();
553 __ Bind(GetEntryLabel());
554 SaveLiveRegisters(codegen, locations);
555
556 InvokeRuntimeCallingConvention calling_convention;
557 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
558 parallel_move.AddMove(
559 locations->InAt(0),
560 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
561 Primitive::kPrimNot,
562 nullptr);
563 parallel_move.AddMove(
564 locations->InAt(1),
565 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
566 Primitive::kPrimInt,
567 nullptr);
568 parallel_move.AddMove(
569 locations->InAt(2),
570 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
571 Primitive::kPrimNot,
572 nullptr);
573 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
574
575 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
576 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
577 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
578 RestoreLiveRegisters(codegen, locations);
579 __ B(GetExitLabel());
580 }
581
582 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
583
584 private:
585 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
586};
587
588// Slow path marking an object reference `ref` during a read
589// barrier. The field `obj.field` in the object `obj` holding this
590// reference does not get updated by this slow path after marking (see
591// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
592//
593// This means that after the execution of this slow path, `ref` will
594// always be up-to-date, but `obj.field` may not; i.e., after the
595// flip, `ref` will be a to-space reference, but `obj.field` will
596// probably still be a from-space reference (unless it gets updated by
597// another thread, or if another thread installed another object
598// reference (different from `ref`) in `obj.field`).
599//
600// If `entrypoint` is a valid location it is assumed to already be
601// holding the entrypoint. The case where the entrypoint is passed in
602// is for the GcRoot read barrier.
603class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
604 public:
605 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
606 Location ref,
607 Location entrypoint = Location::NoLocation())
608 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
609 DCHECK(kEmitCompilerReadBarrier);
610 }
611
612 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
613
614 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
615 LocationSummary* locations = instruction_->GetLocations();
616 Register ref_reg = ref_.AsRegister<Register>();
617 DCHECK(locations->CanCall());
618 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
619 DCHECK(instruction_->IsInstanceFieldGet() ||
620 instruction_->IsStaticFieldGet() ||
621 instruction_->IsArrayGet() ||
622 instruction_->IsArraySet() ||
623 instruction_->IsLoadClass() ||
624 instruction_->IsLoadString() ||
625 instruction_->IsInstanceOf() ||
626 instruction_->IsCheckCast() ||
627 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
628 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
629 << "Unexpected instruction in read barrier marking slow path: "
630 << instruction_->DebugName();
631
632 __ Bind(GetEntryLabel());
633 // No need to save live registers; it's taken care of by the
634 // entrypoint. Also, there is no need to update the stack mask,
635 // as this runtime call will not trigger a garbage collection.
636 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
637 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
638 (S2 <= ref_reg && ref_reg <= S7) ||
639 (ref_reg == FP)) << ref_reg;
640 // "Compact" slow path, saving two moves.
641 //
642 // Instead of using the standard runtime calling convention (input
643 // and output in A0 and V0 respectively):
644 //
645 // A0 <- ref
646 // V0 <- ReadBarrierMark(A0)
647 // ref <- V0
648 //
649 // we just use rX (the register containing `ref`) as input and output
650 // of a dedicated entrypoint:
651 //
652 // rX <- ReadBarrierMarkRegX(rX)
653 //
654 if (entrypoint_.IsValid()) {
655 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
656 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
657 __ Jalr(entrypoint_.AsRegister<Register>());
658 __ NopIfNoReordering();
659 } else {
660 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100661 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800662 // This runtime call does not require a stack map.
663 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
664 instruction_,
665 this,
666 /* direct */ false);
667 }
668 __ B(GetExitLabel());
669 }
670
671 private:
672 // The location (register) of the marked object reference.
673 const Location ref_;
674
675 // The location of the entrypoint if already loaded.
676 const Location entrypoint_;
677
678 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
679};
680
681// Slow path marking an object reference `ref` during a read barrier,
682// and if needed, atomically updating the field `obj.field` in the
683// object `obj` holding this reference after marking (contrary to
684// ReadBarrierMarkSlowPathMIPS above, which never tries to update
685// `obj.field`).
686//
687// This means that after the execution of this slow path, both `ref`
688// and `obj.field` will be up-to-date; i.e., after the flip, both will
689// hold the same to-space reference (unless another thread installed
690// another object reference (different from `ref`) in `obj.field`).
691class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
692 public:
693 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
694 Location ref,
695 Register obj,
696 Location field_offset,
697 Register temp1)
698 : SlowPathCodeMIPS(instruction),
699 ref_(ref),
700 obj_(obj),
701 field_offset_(field_offset),
702 temp1_(temp1) {
703 DCHECK(kEmitCompilerReadBarrier);
704 }
705
706 const char* GetDescription() const OVERRIDE {
707 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
708 }
709
710 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
711 LocationSummary* locations = instruction_->GetLocations();
712 Register ref_reg = ref_.AsRegister<Register>();
713 DCHECK(locations->CanCall());
714 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
715 // This slow path is only used by the UnsafeCASObject intrinsic.
716 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
717 << "Unexpected instruction in read barrier marking and field updating slow path: "
718 << instruction_->DebugName();
719 DCHECK(instruction_->GetLocations()->Intrinsified());
720 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
721 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
722
723 __ Bind(GetEntryLabel());
724
725 // Save the old reference.
726 // Note that we cannot use AT or TMP to save the old reference, as those
727 // are used by the code that follows, but we need the old reference after
728 // the call to the ReadBarrierMarkRegX entry point.
729 DCHECK_NE(temp1_, AT);
730 DCHECK_NE(temp1_, TMP);
731 __ Move(temp1_, ref_reg);
732
733 // No need to save live registers; it's taken care of by the
734 // entrypoint. Also, there is no need to update the stack mask,
735 // as this runtime call will not trigger a garbage collection.
736 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
737 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
738 (S2 <= ref_reg && ref_reg <= S7) ||
739 (ref_reg == FP)) << ref_reg;
740 // "Compact" slow path, saving two moves.
741 //
742 // Instead of using the standard runtime calling convention (input
743 // and output in A0 and V0 respectively):
744 //
745 // A0 <- ref
746 // V0 <- ReadBarrierMark(A0)
747 // ref <- V0
748 //
749 // we just use rX (the register containing `ref`) as input and output
750 // of a dedicated entrypoint:
751 //
752 // rX <- ReadBarrierMarkRegX(rX)
753 //
754 int32_t entry_point_offset =
Roland Levillain97c46462017-05-11 14:04:03 +0100755 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
Alexey Frunze15958152017-02-09 19:08:30 -0800756 // This runtime call does not require a stack map.
757 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
758 instruction_,
759 this,
760 /* direct */ false);
761
762 // If the new reference is different from the old reference,
763 // update the field in the holder (`*(obj_ + field_offset_)`).
764 //
765 // Note that this field could also hold a different object, if
766 // another thread had concurrently changed it. In that case, the
767 // the compare-and-set (CAS) loop below would abort, leaving the
768 // field as-is.
769 MipsLabel done;
770 __ Beq(temp1_, ref_reg, &done);
771
772 // Update the the holder's field atomically. This may fail if
773 // mutator updates before us, but it's OK. This is achieved
774 // using a strong compare-and-set (CAS) operation with relaxed
775 // memory synchronization ordering, where the expected value is
776 // the old reference and the desired value is the new reference.
777
778 // Convenience aliases.
779 Register base = obj_;
780 // The UnsafeCASObject intrinsic uses a register pair as field
781 // offset ("long offset"), of which only the low part contains
782 // data.
783 Register offset = field_offset_.AsRegisterPairLow<Register>();
784 Register expected = temp1_;
785 Register value = ref_reg;
786 Register tmp_ptr = TMP; // Pointer to actual memory.
787 Register tmp = AT; // Value in memory.
788
789 __ Addu(tmp_ptr, base, offset);
790
791 if (kPoisonHeapReferences) {
792 __ PoisonHeapReference(expected);
793 // Do not poison `value` if it is the same register as
794 // `expected`, which has just been poisoned.
795 if (value != expected) {
796 __ PoisonHeapReference(value);
797 }
798 }
799
800 // do {
801 // tmp = [r_ptr] - expected;
802 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
803
804 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
805 MipsLabel loop_head, exit_loop;
806 __ Bind(&loop_head);
807 if (is_r6) {
808 __ LlR6(tmp, tmp_ptr);
809 } else {
810 __ LlR2(tmp, tmp_ptr);
811 }
812 __ Bne(tmp, expected, &exit_loop);
813 __ Move(tmp, value);
814 if (is_r6) {
815 __ ScR6(tmp, tmp_ptr);
816 } else {
817 __ ScR2(tmp, tmp_ptr);
818 }
819 __ Beqz(tmp, &loop_head);
820 __ Bind(&exit_loop);
821
822 if (kPoisonHeapReferences) {
823 __ UnpoisonHeapReference(expected);
824 // Do not unpoison `value` if it is the same register as
825 // `expected`, which has just been unpoisoned.
826 if (value != expected) {
827 __ UnpoisonHeapReference(value);
828 }
829 }
830
831 __ Bind(&done);
832 __ B(GetExitLabel());
833 }
834
835 private:
836 // The location (register) of the marked object reference.
837 const Location ref_;
838 // The register containing the object holding the marked object reference field.
839 const Register obj_;
840 // The location of the offset of the marked reference field within `obj_`.
841 Location field_offset_;
842
843 const Register temp1_;
844
845 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
846};
847
848// Slow path generating a read barrier for a heap reference.
849class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
850 public:
851 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
852 Location out,
853 Location ref,
854 Location obj,
855 uint32_t offset,
856 Location index)
857 : SlowPathCodeMIPS(instruction),
858 out_(out),
859 ref_(ref),
860 obj_(obj),
861 offset_(offset),
862 index_(index) {
863 DCHECK(kEmitCompilerReadBarrier);
864 // If `obj` is equal to `out` or `ref`, it means the initial object
865 // has been overwritten by (or after) the heap object reference load
866 // to be instrumented, e.g.:
867 //
868 // __ LoadFromOffset(kLoadWord, out, out, offset);
869 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
870 //
871 // In that case, we have lost the information about the original
872 // object, and the emitted read barrier cannot work properly.
873 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
874 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
875 }
876
877 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
878 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
879 LocationSummary* locations = instruction_->GetLocations();
880 Register reg_out = out_.AsRegister<Register>();
881 DCHECK(locations->CanCall());
882 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
883 DCHECK(instruction_->IsInstanceFieldGet() ||
884 instruction_->IsStaticFieldGet() ||
885 instruction_->IsArrayGet() ||
886 instruction_->IsInstanceOf() ||
887 instruction_->IsCheckCast() ||
888 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
889 << "Unexpected instruction in read barrier for heap reference slow path: "
890 << instruction_->DebugName();
891
892 __ Bind(GetEntryLabel());
893 SaveLiveRegisters(codegen, locations);
894
895 // We may have to change the index's value, but as `index_` is a
896 // constant member (like other "inputs" of this slow path),
897 // introduce a copy of it, `index`.
898 Location index = index_;
899 if (index_.IsValid()) {
900 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
901 if (instruction_->IsArrayGet()) {
902 // Compute the actual memory offset and store it in `index`.
903 Register index_reg = index_.AsRegister<Register>();
904 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
905 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
906 // We are about to change the value of `index_reg` (see the
907 // calls to art::mips::MipsAssembler::Sll and
908 // art::mips::MipsAssembler::Addiu32 below), but it has
909 // not been saved by the previous call to
910 // art::SlowPathCode::SaveLiveRegisters, as it is a
911 // callee-save register --
912 // art::SlowPathCode::SaveLiveRegisters does not consider
913 // callee-save registers, as it has been designed with the
914 // assumption that callee-save registers are supposed to be
915 // handled by the called function. So, as a callee-save
916 // register, `index_reg` _would_ eventually be saved onto
917 // the stack, but it would be too late: we would have
918 // changed its value earlier. Therefore, we manually save
919 // it here into another freely available register,
920 // `free_reg`, chosen of course among the caller-save
921 // registers (as a callee-save `free_reg` register would
922 // exhibit the same problem).
923 //
924 // Note we could have requested a temporary register from
925 // the register allocator instead; but we prefer not to, as
926 // this is a slow path, and we know we can find a
927 // caller-save register that is available.
928 Register free_reg = FindAvailableCallerSaveRegister(codegen);
929 __ Move(free_reg, index_reg);
930 index_reg = free_reg;
931 index = Location::RegisterLocation(index_reg);
932 } else {
933 // The initial register stored in `index_` has already been
934 // saved in the call to art::SlowPathCode::SaveLiveRegisters
935 // (as it is not a callee-save register), so we can freely
936 // use it.
937 }
938 // Shifting the index value contained in `index_reg` by the scale
939 // factor (2) cannot overflow in practice, as the runtime is
940 // unable to allocate object arrays with a size larger than
941 // 2^26 - 1 (that is, 2^28 - 4 bytes).
942 __ Sll(index_reg, index_reg, TIMES_4);
943 static_assert(
944 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
945 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
946 __ Addiu32(index_reg, index_reg, offset_);
947 } else {
948 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
949 // intrinsics, `index_` is not shifted by a scale factor of 2
950 // (as in the case of ArrayGet), as it is actually an offset
951 // to an object field within an object.
952 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
953 DCHECK(instruction_->GetLocations()->Intrinsified());
954 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
955 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
956 << instruction_->AsInvoke()->GetIntrinsic();
957 DCHECK_EQ(offset_, 0U);
958 DCHECK(index_.IsRegisterPair());
959 // UnsafeGet's offset location is a register pair, the low
960 // part contains the correct offset.
961 index = index_.ToLow();
962 }
963 }
964
965 // We're moving two or three locations to locations that could
966 // overlap, so we need a parallel move resolver.
967 InvokeRuntimeCallingConvention calling_convention;
968 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
969 parallel_move.AddMove(ref_,
970 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
971 Primitive::kPrimNot,
972 nullptr);
973 parallel_move.AddMove(obj_,
974 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
975 Primitive::kPrimNot,
976 nullptr);
977 if (index.IsValid()) {
978 parallel_move.AddMove(index,
979 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
980 Primitive::kPrimInt,
981 nullptr);
982 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
983 } else {
984 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
985 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
986 }
987 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
988 instruction_,
989 instruction_->GetDexPc(),
990 this);
991 CheckEntrypointTypes<
992 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
Lena Djokic8098da92017-06-28 12:07:50 +0200993 mips_codegen->MoveLocation(out_,
994 calling_convention.GetReturnLocation(Primitive::kPrimNot),
995 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -0800996
997 RestoreLiveRegisters(codegen, locations);
998 __ B(GetExitLabel());
999 }
1000
1001 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
1002
1003 private:
1004 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
1005 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
1006 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
1007 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
1008 if (i != ref &&
1009 i != obj &&
1010 !codegen->IsCoreCalleeSaveRegister(i) &&
1011 !codegen->IsBlockedCoreRegister(i)) {
1012 return static_cast<Register>(i);
1013 }
1014 }
1015 // We shall never fail to find a free caller-save register, as
1016 // there are more than two core caller-save registers on MIPS
1017 // (meaning it is possible to find one which is different from
1018 // `ref` and `obj`).
1019 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
1020 LOG(FATAL) << "Could not find a free caller-save register";
1021 UNREACHABLE();
1022 }
1023
1024 const Location out_;
1025 const Location ref_;
1026 const Location obj_;
1027 const uint32_t offset_;
1028 // An additional location containing an index to an array.
1029 // Only used for HArrayGet and the UnsafeGetObject &
1030 // UnsafeGetObjectVolatile intrinsics.
1031 const Location index_;
1032
1033 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
1034};
1035
1036// Slow path generating a read barrier for a GC root.
1037class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
1038 public:
1039 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
1040 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
1041 DCHECK(kEmitCompilerReadBarrier);
1042 }
1043
1044 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1045 LocationSummary* locations = instruction_->GetLocations();
1046 Register reg_out = out_.AsRegister<Register>();
1047 DCHECK(locations->CanCall());
1048 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1049 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1050 << "Unexpected instruction in read barrier for GC root slow path: "
1051 << instruction_->DebugName();
1052
1053 __ Bind(GetEntryLabel());
1054 SaveLiveRegisters(codegen, locations);
1055
1056 InvokeRuntimeCallingConvention calling_convention;
1057 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Lena Djokic8098da92017-06-28 12:07:50 +02001058 mips_codegen->MoveLocation(Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
1059 root_,
1060 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -08001061 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1062 instruction_,
1063 instruction_->GetDexPc(),
1064 this);
1065 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
Lena Djokic8098da92017-06-28 12:07:50 +02001066 mips_codegen->MoveLocation(out_,
1067 calling_convention.GetReturnLocation(Primitive::kPrimNot),
1068 Primitive::kPrimNot);
Alexey Frunze15958152017-02-09 19:08:30 -08001069
1070 RestoreLiveRegisters(codegen, locations);
1071 __ B(GetExitLabel());
1072 }
1073
1074 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1075
1076 private:
1077 const Location out_;
1078 const Location root_;
1079
1080 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1081};
1082
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001083CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1084 const MipsInstructionSetFeatures& isa_features,
1085 const CompilerOptions& compiler_options,
1086 OptimizingCompilerStats* stats)
1087 : CodeGenerator(graph,
1088 kNumberOfCoreRegisters,
1089 kNumberOfFRegisters,
1090 kNumberOfRegisterPairs,
1091 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1092 arraysize(kCoreCalleeSaves)),
1093 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1094 arraysize(kFpuCalleeSaves)),
1095 compiler_options,
1096 stats),
1097 block_labels_(nullptr),
1098 location_builder_(graph, this),
1099 instruction_visitor_(graph, this),
1100 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001101 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001102 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001103 uint32_literals_(std::less<uint32_t>(),
1104 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001105 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001106 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001107 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001108 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001109 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001110 string_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001111 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1112 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001113 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001114 // Save RA (containing the return address) to mimic Quick.
1115 AddAllocatedRegister(Location::RegisterLocation(RA));
1116}
1117
1118#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001119// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1120#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001121#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001122
1123void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1124 // Ensure that we fix up branches.
1125 __ FinalizeCode();
1126
1127 // Adjust native pc offsets in stack maps.
1128 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001129 uint32_t old_position =
1130 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001131 uint32_t new_position = __ GetAdjustedPosition(old_position);
1132 DCHECK_GE(new_position, old_position);
1133 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1134 }
1135
1136 // Adjust pc offsets for the disassembly information.
1137 if (disasm_info_ != nullptr) {
1138 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1139 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1140 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1141 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1142 it.second.start = __ GetAdjustedPosition(it.second.start);
1143 it.second.end = __ GetAdjustedPosition(it.second.end);
1144 }
1145 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1146 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1147 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1148 }
1149 }
1150
1151 CodeGenerator::Finalize(allocator);
1152}
1153
1154MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1155 return codegen_->GetAssembler();
1156}
1157
1158void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1159 DCHECK_LT(index, moves_.size());
1160 MoveOperands* move = moves_[index];
1161 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1162}
1163
1164void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1165 DCHECK_LT(index, moves_.size());
1166 MoveOperands* move = moves_[index];
1167 Primitive::Type type = move->GetType();
1168 Location loc1 = move->GetDestination();
1169 Location loc2 = move->GetSource();
1170
1171 DCHECK(!loc1.IsConstant());
1172 DCHECK(!loc2.IsConstant());
1173
1174 if (loc1.Equals(loc2)) {
1175 return;
1176 }
1177
1178 if (loc1.IsRegister() && loc2.IsRegister()) {
1179 // Swap 2 GPRs.
1180 Register r1 = loc1.AsRegister<Register>();
1181 Register r2 = loc2.AsRegister<Register>();
1182 __ Move(TMP, r2);
1183 __ Move(r2, r1);
1184 __ Move(r1, TMP);
1185 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1186 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1187 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1188 if (type == Primitive::kPrimFloat) {
1189 __ MovS(FTMP, f2);
1190 __ MovS(f2, f1);
1191 __ MovS(f1, FTMP);
1192 } else {
1193 DCHECK_EQ(type, Primitive::kPrimDouble);
1194 __ MovD(FTMP, f2);
1195 __ MovD(f2, f1);
1196 __ MovD(f1, FTMP);
1197 }
1198 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1199 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1200 // Swap FPR and GPR.
1201 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
1202 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1203 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001204 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001205 __ Move(TMP, r2);
1206 __ Mfc1(r2, f1);
1207 __ Mtc1(TMP, f1);
1208 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1209 // Swap 2 GPR register pairs.
1210 Register r1 = loc1.AsRegisterPairLow<Register>();
1211 Register r2 = loc2.AsRegisterPairLow<Register>();
1212 __ Move(TMP, r2);
1213 __ Move(r2, r1);
1214 __ Move(r1, TMP);
1215 r1 = loc1.AsRegisterPairHigh<Register>();
1216 r2 = loc2.AsRegisterPairHigh<Register>();
1217 __ Move(TMP, r2);
1218 __ Move(r2, r1);
1219 __ Move(r1, TMP);
1220 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1221 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1222 // Swap FPR and GPR register pair.
1223 DCHECK_EQ(type, Primitive::kPrimDouble);
1224 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1225 : loc2.AsFpuRegister<FRegister>();
1226 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1227 : loc2.AsRegisterPairLow<Register>();
1228 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1229 : loc2.AsRegisterPairHigh<Register>();
1230 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1231 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1232 // unpredictable and the following mfch1 will fail.
1233 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001234 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001235 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001236 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001237 __ Move(r2_l, TMP);
1238 __ Move(r2_h, AT);
1239 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1240 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1241 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1242 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001243 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1244 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001245 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1246 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001247 __ Move(TMP, reg);
1248 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1249 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1250 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1251 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1252 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1253 : loc2.AsRegisterPairLow<Register>();
1254 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1255 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001256 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001257 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1258 : loc2.GetHighStackIndex(kMipsWordSize);
1259 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001260 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001261 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001262 __ Move(TMP, reg_h);
1263 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1264 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001265 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1266 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1267 : loc2.AsFpuRegister<FRegister>();
1268 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1269 if (type == Primitive::kPrimFloat) {
1270 __ MovS(FTMP, reg);
1271 __ LoadSFromOffset(reg, SP, offset);
1272 __ StoreSToOffset(FTMP, SP, offset);
1273 } else {
1274 DCHECK_EQ(type, Primitive::kPrimDouble);
1275 __ MovD(FTMP, reg);
1276 __ LoadDFromOffset(reg, SP, offset);
1277 __ StoreDToOffset(FTMP, SP, offset);
1278 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001279 } else {
1280 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1281 }
1282}
1283
1284void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1285 __ Pop(static_cast<Register>(reg));
1286}
1287
1288void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1289 __ Push(static_cast<Register>(reg));
1290}
1291
1292void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1293 // Allocate a scratch register other than TMP, if available.
1294 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1295 // automatically unspilled when the scratch scope object is destroyed).
1296 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1297 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1298 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1299 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1300 __ LoadFromOffset(kLoadWord,
1301 Register(ensure_scratch.GetRegister()),
1302 SP,
1303 index1 + stack_offset);
1304 __ LoadFromOffset(kLoadWord,
1305 TMP,
1306 SP,
1307 index2 + stack_offset);
1308 __ StoreToOffset(kStoreWord,
1309 Register(ensure_scratch.GetRegister()),
1310 SP,
1311 index2 + stack_offset);
1312 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1313 }
1314}
1315
Alexey Frunze73296a72016-06-03 22:51:46 -07001316void CodeGeneratorMIPS::ComputeSpillMask() {
1317 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1318 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1319 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1320 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1321 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1322 // within the stack frame.
1323 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1324 core_spill_mask_ |= (1 << ZERO);
1325 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001326}
1327
1328bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001329 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001330 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1331 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1332 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001333 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001334}
1335
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001336static dwarf::Reg DWARFReg(Register reg) {
1337 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1338}
1339
1340// TODO: mapping of floating-point registers to DWARF.
1341
1342void CodeGeneratorMIPS::GenerateFrameEntry() {
1343 __ Bind(&frame_entry_label_);
1344
1345 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1346
1347 if (do_overflow_check) {
1348 __ LoadFromOffset(kLoadWord,
1349 ZERO,
1350 SP,
1351 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1352 RecordPcInfo(nullptr, 0);
1353 }
1354
1355 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001356 CHECK_EQ(fpu_spill_mask_, 0u);
1357 CHECK_EQ(core_spill_mask_, 1u << RA);
1358 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001359 return;
1360 }
1361
1362 // Make sure the frame size isn't unreasonably large.
1363 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1364 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1365 }
1366
1367 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001368
Alexey Frunze73296a72016-06-03 22:51:46 -07001369 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001370 __ IncreaseFrameSize(ofs);
1371
Alexey Frunze73296a72016-06-03 22:51:46 -07001372 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1373 Register reg = static_cast<Register>(MostSignificantBit(mask));
1374 mask ^= 1u << reg;
1375 ofs -= kMipsWordSize;
1376 // The ZERO register is only included for alignment.
1377 if (reg != ZERO) {
1378 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001379 __ cfi().RelOffset(DWARFReg(reg), ofs);
1380 }
1381 }
1382
Alexey Frunze73296a72016-06-03 22:51:46 -07001383 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1384 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1385 mask ^= 1u << reg;
1386 ofs -= kMipsDoublewordSize;
1387 __ StoreDToOffset(reg, SP, ofs);
1388 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001389 }
1390
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001391 // Save the current method if we need it. Note that we do not
1392 // do this in HCurrentMethod, as the instruction might have been removed
1393 // in the SSA graph.
1394 if (RequiresCurrentMethod()) {
1395 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1396 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001397
1398 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1399 // Initialize should deoptimize flag to 0.
1400 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1401 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001402}
1403
1404void CodeGeneratorMIPS::GenerateFrameExit() {
1405 __ cfi().RememberState();
1406
1407 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001408 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001409
Alexey Frunze73296a72016-06-03 22:51:46 -07001410 // For better instruction scheduling restore RA before other registers.
1411 uint32_t ofs = GetFrameSize();
1412 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1413 Register reg = static_cast<Register>(MostSignificantBit(mask));
1414 mask ^= 1u << reg;
1415 ofs -= kMipsWordSize;
1416 // The ZERO register is only included for alignment.
1417 if (reg != ZERO) {
1418 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001419 __ cfi().Restore(DWARFReg(reg));
1420 }
1421 }
1422
Alexey Frunze73296a72016-06-03 22:51:46 -07001423 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1424 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1425 mask ^= 1u << reg;
1426 ofs -= kMipsDoublewordSize;
1427 __ LoadDFromOffset(reg, SP, ofs);
1428 // TODO: __ cfi().Restore(DWARFReg(reg));
1429 }
1430
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001431 size_t frame_size = GetFrameSize();
1432 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1433 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1434 bool reordering = __ SetReorder(false);
1435 if (exchange) {
1436 __ Jr(RA);
1437 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1438 } else {
1439 __ DecreaseFrameSize(frame_size);
1440 __ Jr(RA);
1441 __ Nop(); // In delay slot.
1442 }
1443 __ SetReorder(reordering);
1444 } else {
1445 __ Jr(RA);
1446 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001447 }
1448
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001449 __ cfi().RestoreState();
1450 __ cfi().DefCFAOffset(GetFrameSize());
1451}
1452
1453void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1454 __ Bind(GetLabelOf(block));
1455}
1456
Lena Djokicca8c2952017-05-29 11:31:46 +02001457VectorRegister VectorRegisterFrom(Location location) {
1458 DCHECK(location.IsFpuRegister());
1459 return static_cast<VectorRegister>(location.AsFpuRegister<FRegister>());
1460}
1461
Lena Djokic8098da92017-06-28 12:07:50 +02001462void CodeGeneratorMIPS::MoveLocation(Location destination,
1463 Location source,
1464 Primitive::Type dst_type) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001465 if (source.Equals(destination)) {
1466 return;
1467 }
1468
Lena Djokic8098da92017-06-28 12:07:50 +02001469 if (source.IsConstant()) {
1470 MoveConstant(destination, source.GetConstant());
1471 } else {
1472 if (destination.IsRegister()) {
1473 if (source.IsRegister()) {
1474 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1475 } else if (source.IsFpuRegister()) {
1476 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1477 } else {
1478 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001479 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001480 }
1481 } else if (destination.IsRegisterPair()) {
1482 if (source.IsRegisterPair()) {
1483 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1484 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1485 } else if (source.IsFpuRegister()) {
1486 Register dst_high = destination.AsRegisterPairHigh<Register>();
1487 Register dst_low = destination.AsRegisterPairLow<Register>();
1488 FRegister src = source.AsFpuRegister<FRegister>();
1489 __ Mfc1(dst_low, src);
1490 __ MoveFromFpuHigh(dst_high, src);
1491 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001492 DCHECK(source.IsDoubleStackSlot())
1493 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001494 int32_t off = source.GetStackIndex();
1495 Register r = destination.AsRegisterPairLow<Register>();
1496 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1497 }
1498 } else if (destination.IsFpuRegister()) {
1499 if (source.IsRegister()) {
1500 DCHECK(!Primitive::Is64BitType(dst_type));
1501 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1502 } else if (source.IsRegisterPair()) {
1503 DCHECK(Primitive::Is64BitType(dst_type));
1504 FRegister dst = destination.AsFpuRegister<FRegister>();
1505 Register src_high = source.AsRegisterPairHigh<Register>();
1506 Register src_low = source.AsRegisterPairLow<Register>();
1507 __ Mtc1(src_low, dst);
1508 __ MoveToFpuHigh(src_high, dst);
1509 } else if (source.IsFpuRegister()) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001510 if (GetGraph()->HasSIMD()) {
1511 __ MoveV(VectorRegisterFrom(destination),
1512 VectorRegisterFrom(source));
Lena Djokic8098da92017-06-28 12:07:50 +02001513 } else {
Lena Djokicca8c2952017-05-29 11:31:46 +02001514 if (Primitive::Is64BitType(dst_type)) {
1515 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1516 } else {
1517 DCHECK_EQ(dst_type, Primitive::kPrimFloat);
1518 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1519 }
Lena Djokic8098da92017-06-28 12:07:50 +02001520 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001521 } else if (source.IsSIMDStackSlot()) {
1522 __ LoadQFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
Lena Djokic8098da92017-06-28 12:07:50 +02001523 } else if (source.IsDoubleStackSlot()) {
1524 DCHECK(Primitive::Is64BitType(dst_type));
1525 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1526 } else {
1527 DCHECK(!Primitive::Is64BitType(dst_type));
1528 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1529 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1530 }
Lena Djokicca8c2952017-05-29 11:31:46 +02001531 } else if (destination.IsSIMDStackSlot()) {
1532 if (source.IsFpuRegister()) {
1533 __ StoreQToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1534 } else {
1535 DCHECK(source.IsSIMDStackSlot());
1536 __ LoadQFromOffset(FTMP, SP, source.GetStackIndex());
1537 __ StoreQToOffset(FTMP, SP, destination.GetStackIndex());
1538 }
Lena Djokic8098da92017-06-28 12:07:50 +02001539 } else if (destination.IsDoubleStackSlot()) {
1540 int32_t dst_offset = destination.GetStackIndex();
1541 if (source.IsRegisterPair()) {
1542 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, dst_offset);
1543 } else if (source.IsFpuRegister()) {
1544 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1545 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001546 DCHECK(source.IsDoubleStackSlot())
1547 << "Cannot move from " << source << " to " << destination;
Lena Djokic8098da92017-06-28 12:07:50 +02001548 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1549 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1550 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1551 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset + 4);
1552 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001553 } else {
Lena Djokic8098da92017-06-28 12:07:50 +02001554 DCHECK(destination.IsStackSlot()) << destination;
1555 int32_t dst_offset = destination.GetStackIndex();
1556 if (source.IsRegister()) {
1557 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, dst_offset);
1558 } else if (source.IsFpuRegister()) {
1559 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, dst_offset);
1560 } else {
1561 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1562 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1563 __ StoreToOffset(kStoreWord, TMP, SP, dst_offset);
1564 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001565 }
1566 }
1567}
1568
1569void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1570 if (c->IsIntConstant() || c->IsNullConstant()) {
1571 // Move 32 bit constant.
1572 int32_t value = GetInt32ValueOf(c);
1573 if (destination.IsRegister()) {
1574 Register dst = destination.AsRegister<Register>();
1575 __ LoadConst32(dst, value);
1576 } else {
1577 DCHECK(destination.IsStackSlot())
1578 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001579 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001580 }
1581 } else if (c->IsLongConstant()) {
1582 // Move 64 bit constant.
1583 int64_t value = GetInt64ValueOf(c);
1584 if (destination.IsRegisterPair()) {
1585 Register r_h = destination.AsRegisterPairHigh<Register>();
1586 Register r_l = destination.AsRegisterPairLow<Register>();
1587 __ LoadConst64(r_h, r_l, value);
1588 } else {
1589 DCHECK(destination.IsDoubleStackSlot())
1590 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001591 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001592 }
1593 } else if (c->IsFloatConstant()) {
1594 // Move 32 bit float constant.
1595 int32_t value = GetInt32ValueOf(c);
1596 if (destination.IsFpuRegister()) {
1597 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1598 } else {
1599 DCHECK(destination.IsStackSlot())
1600 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001601 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001602 }
1603 } else {
1604 // Move 64 bit double constant.
1605 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1606 int64_t value = GetInt64ValueOf(c);
1607 if (destination.IsFpuRegister()) {
1608 FRegister fd = destination.AsFpuRegister<FRegister>();
1609 __ LoadDConst64(fd, value, TMP);
1610 } else {
1611 DCHECK(destination.IsDoubleStackSlot())
1612 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001613 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001614 }
1615 }
1616}
1617
1618void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1619 DCHECK(destination.IsRegister());
1620 Register dst = destination.AsRegister<Register>();
1621 __ LoadConst32(dst, value);
1622}
1623
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001624void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1625 if (location.IsRegister()) {
1626 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001627 } else if (location.IsRegisterPair()) {
1628 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1629 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001630 } else {
1631 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1632 }
1633}
1634
Vladimir Markoaad75c62016-10-03 08:46:48 +00001635template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1636inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1637 const ArenaDeque<PcRelativePatchInfo>& infos,
1638 ArenaVector<LinkerPatch>* linker_patches) {
1639 for (const PcRelativePatchInfo& info : infos) {
1640 const DexFile& dex_file = info.target_dex_file;
1641 size_t offset_or_index = info.offset_or_index;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001642 DCHECK(info.label.IsBound());
1643 uint32_t literal_offset = __ GetLabelLocation(&info.label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001644 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1645 // the assembler's base label used for PC-relative addressing.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001646 const PcRelativePatchInfo& info_high = info.patch_info_high ? *info.patch_info_high : info;
1647 uint32_t pc_rel_offset = info_high.pc_rel_label.IsBound()
1648 ? __ GetLabelLocation(&info_high.pc_rel_label)
Vladimir Markoaad75c62016-10-03 08:46:48 +00001649 : __ GetPcRelBaseLabelLocation();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001650 linker_patches->push_back(Factory(literal_offset, &dex_file, pc_rel_offset, offset_or_index));
Vladimir Markoaad75c62016-10-03 08:46:48 +00001651 }
1652}
1653
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001654void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1655 DCHECK(linker_patches->empty());
1656 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001657 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001658 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001659 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001660 type_bss_entry_patches_.size() +
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001661 pc_relative_string_patches_.size() +
1662 string_bss_entry_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001663 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001664 if (GetCompilerOptions().IsBootImage()) {
1665 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00001666 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001667 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1668 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001669 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1670 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001671 } else {
1672 DCHECK(pc_relative_method_patches_.empty());
Vladimir Marko94ec2db2017-09-06 17:21:03 +01001673 EmitPcRelativeLinkerPatches<LinkerPatch::TypeClassTablePatch>(pc_relative_type_patches_,
1674 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001675 EmitPcRelativeLinkerPatches<LinkerPatch::StringInternTablePatch>(pc_relative_string_patches_,
1676 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001677 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001678 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1679 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001680 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1681 linker_patches);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001682 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(string_bss_entry_patches_,
1683 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001684 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001685}
1686
Vladimir Marko65979462017-05-19 17:25:12 +01001687CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001688 MethodReference target_method,
1689 const PcRelativePatchInfo* info_high) {
Vladimir Marko65979462017-05-19 17:25:12 +01001690 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001691 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001692 info_high,
Vladimir Marko65979462017-05-19 17:25:12 +01001693 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001694}
1695
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001696CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001697 MethodReference target_method,
1698 const PcRelativePatchInfo* info_high) {
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001699 return NewPcRelativePatch(*target_method.dex_file,
Mathieu Chartierfc8b4222017-09-17 13:44:24 -07001700 target_method.index,
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001701 info_high,
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001702 &method_bss_entry_patches_);
1703}
1704
Alexey Frunze06a46c42016-07-19 15:00:40 -07001705CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001706 const DexFile& dex_file,
1707 dex::TypeIndex type_index,
1708 const PcRelativePatchInfo* info_high) {
1709 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001710}
1711
Vladimir Marko1998cd02017-01-13 13:02:58 +00001712CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001713 const DexFile& dex_file,
1714 dex::TypeIndex type_index,
1715 const PcRelativePatchInfo* info_high) {
1716 return NewPcRelativePatch(dex_file, type_index.index_, info_high, &type_bss_entry_patches_);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001717}
1718
Vladimir Marko65979462017-05-19 17:25:12 +01001719CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001720 const DexFile& dex_file,
1721 dex::StringIndex string_index,
1722 const PcRelativePatchInfo* info_high) {
1723 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &pc_relative_string_patches_);
Vladimir Marko65979462017-05-19 17:25:12 +01001724}
1725
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01001726CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewStringBssEntryPatch(
1727 const DexFile& dex_file,
1728 dex::StringIndex string_index,
1729 const PcRelativePatchInfo* info_high) {
1730 return NewPcRelativePatch(dex_file, string_index.index_, info_high, &string_bss_entry_patches_);
1731}
1732
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001733CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001734 const DexFile& dex_file,
1735 uint32_t offset_or_index,
1736 const PcRelativePatchInfo* info_high,
1737 ArenaDeque<PcRelativePatchInfo>* patches) {
1738 patches->emplace_back(dex_file, offset_or_index, info_high);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001739 return &patches->back();
1740}
1741
Alexey Frunze06a46c42016-07-19 15:00:40 -07001742Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1743 return map->GetOrCreate(
1744 value,
1745 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1746}
1747
Alexey Frunze06a46c42016-07-19 15:00:40 -07001748Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001749 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001750}
1751
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001752void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info_high,
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001753 Register out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001754 Register base) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001755 DCHECK(!info_high->patch_info_high);
Alexey Frunze6079dca2017-05-28 19:10:28 -07001756 DCHECK_NE(out, base);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001757 bool reordering = __ SetReorder(false);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001758 if (GetInstructionSetFeatures().IsR6()) {
1759 DCHECK_EQ(base, ZERO);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001760 __ Bind(&info_high->label);
1761 __ Bind(&info_high->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001762 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001763 __ Auipc(out, /* placeholder */ 0x1234);
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001764 __ SetReorder(reordering);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001765 } else {
1766 // If base is ZERO, emit NAL to obtain the actual base.
1767 if (base == ZERO) {
1768 // Generate a dummy PC-relative call to obtain PC.
1769 __ Nal();
1770 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001771 __ Bind(&info_high->label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001772 __ Lui(out, /* placeholder */ 0x1234);
1773 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1774 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1775 if (base == ZERO) {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001776 __ Bind(&info_high->pc_rel_label);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001777 }
Alexey Frunzea663d9d2017-07-31 18:43:18 -07001778 __ SetReorder(reordering);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001779 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001780 __ Addu(out, out, (base == ZERO) ? RA : base);
1781 }
Alexey Frunze5fa5c042017-06-01 21:07:52 -07001782 // A following instruction will add the sign-extended low half of the 32-bit
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001783 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001784}
1785
Alexey Frunze627c1a02017-01-30 19:28:14 -08001786CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1787 const DexFile& dex_file,
1788 dex::StringIndex dex_index,
1789 Handle<mirror::String> handle) {
1790 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1791 reinterpret_cast64<uint64_t>(handle.GetReference()));
1792 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1793 return &jit_string_patches_.back();
1794}
1795
1796CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1797 const DexFile& dex_file,
1798 dex::TypeIndex dex_index,
1799 Handle<mirror::Class> handle) {
1800 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1801 reinterpret_cast64<uint64_t>(handle.GetReference()));
1802 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1803 return &jit_class_patches_.back();
1804}
1805
1806void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1807 const uint8_t* roots_data,
1808 const CodeGeneratorMIPS::JitPatchInfo& info,
1809 uint64_t index_in_table) const {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001810 uint32_t high_literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1811 uint32_t low_literal_offset = GetAssembler().GetLabelLocation(&info.low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001812 uintptr_t address =
1813 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1814 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1815 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001816 DCHECK_EQ(code[high_literal_offset + 0], 0x34);
1817 DCHECK_EQ(code[high_literal_offset + 1], 0x12);
1818 DCHECK_EQ((code[high_literal_offset + 2] & 0xE0), 0x00);
1819 DCHECK_EQ(code[high_literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001820 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001821 DCHECK_EQ(code[low_literal_offset + 0], 0x78);
1822 DCHECK_EQ(code[low_literal_offset + 1], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001823 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001824 // lui reg, addr32_high
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001825 code[high_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1826 code[high_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001827 // instr reg, reg, addr32_low
Alexey Frunze4147fcc2017-06-17 19:57:27 -07001828 code[low_literal_offset + 0] = static_cast<uint8_t>(addr32 >> 0);
1829 code[low_literal_offset + 1] = static_cast<uint8_t>(addr32 >> 8);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001830}
1831
1832void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1833 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001834 const auto it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1835 dex::StringIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001836 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001837 uint64_t index_in_table = it->second;
1838 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001839 }
1840 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001841 const auto it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1842 dex::TypeIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001843 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001844 uint64_t index_in_table = it->second;
1845 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001846 }
1847}
1848
Goran Jakovljevice114da22016-12-26 14:21:43 +01001849void CodeGeneratorMIPS::MarkGCCard(Register object,
1850 Register value,
1851 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001852 MipsLabel done;
1853 Register card = AT;
1854 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001855 if (value_can_be_null) {
1856 __ Beqz(value, &done);
1857 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001858 __ LoadFromOffset(kLoadWord,
1859 card,
1860 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001861 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001862 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1863 __ Addu(temp, card, temp);
1864 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001865 if (value_can_be_null) {
1866 __ Bind(&done);
1867 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001868}
1869
David Brazdil58282f42016-01-14 12:45:10 +00001870void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001871 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1872 blocked_core_registers_[ZERO] = true;
1873 blocked_core_registers_[K0] = true;
1874 blocked_core_registers_[K1] = true;
1875 blocked_core_registers_[GP] = true;
1876 blocked_core_registers_[SP] = true;
1877 blocked_core_registers_[RA] = true;
1878
1879 // AT and TMP(T8) are used as temporary/scratch registers
1880 // (similar to how AT is used by MIPS assemblers).
1881 blocked_core_registers_[AT] = true;
1882 blocked_core_registers_[TMP] = true;
1883 blocked_fpu_registers_[FTMP] = true;
1884
1885 // Reserve suspend and thread registers.
1886 blocked_core_registers_[S0] = true;
1887 blocked_core_registers_[TR] = true;
1888
1889 // Reserve T9 for function calls
1890 blocked_core_registers_[T9] = true;
1891
1892 // Reserve odd-numbered FPU registers.
1893 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1894 blocked_fpu_registers_[i] = true;
1895 }
1896
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001897 if (GetGraph()->IsDebuggable()) {
1898 // Stubs do not save callee-save floating point registers. If the graph
1899 // is debuggable, we need to deal with these registers differently. For
1900 // now, just block them.
1901 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1902 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1903 }
1904 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001905}
1906
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001907size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1908 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1909 return kMipsWordSize;
1910}
1911
1912size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1913 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1914 return kMipsWordSize;
1915}
1916
1917size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001918 if (GetGraph()->HasSIMD()) {
1919 __ StoreQToOffset(FRegister(reg_id), SP, stack_index);
1920 } else {
1921 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1922 }
1923 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001924}
1925
1926size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
Lena Djokicca8c2952017-05-29 11:31:46 +02001927 if (GetGraph()->HasSIMD()) {
1928 __ LoadQFromOffset(FRegister(reg_id), SP, stack_index);
1929 } else {
1930 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1931 }
1932 return GetFloatingPointSpillSlotSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001933}
1934
1935void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001936 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001937}
1938
1939void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001940 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001941}
1942
Serban Constantinescufca16662016-07-14 09:21:59 +01001943constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1944
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001945void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1946 HInstruction* instruction,
1947 uint32_t dex_pc,
1948 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001949 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001950 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1951 IsDirectEntrypoint(entrypoint));
1952 if (EntrypointRequiresStackMap(entrypoint)) {
1953 RecordPcInfo(instruction, dex_pc, slow_path);
1954 }
1955}
1956
1957void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1958 HInstruction* instruction,
1959 SlowPathCode* slow_path,
1960 bool direct) {
1961 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1962 GenerateInvokeRuntime(entry_point_offset, direct);
1963}
1964
1965void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001966 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001967 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001968 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001969 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001970 // Reserve argument space on stack (for $a0-$a3) for
1971 // entrypoints that directly reference native implementations.
1972 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001973 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001974 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001975 } else {
1976 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001977 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001978 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001979}
1980
1981void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1982 Register class_reg) {
1983 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1984 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1985 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1986 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1987 __ Sync(0);
1988 __ Bind(slow_path->GetExitLabel());
1989}
1990
1991void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1992 __ Sync(0); // Only stype 0 is supported.
1993}
1994
1995void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1996 HBasicBlock* successor) {
1997 SuspendCheckSlowPathMIPS* slow_path =
1998 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1999 codegen_->AddSlowPath(slow_path);
2000
2001 __ LoadFromOffset(kLoadUnsignedHalfword,
2002 TMP,
2003 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07002004 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002005 if (successor == nullptr) {
2006 __ Bnez(TMP, slow_path->GetEntryLabel());
2007 __ Bind(slow_path->GetReturnLabel());
2008 } else {
2009 __ Beqz(TMP, codegen_->GetLabelOf(successor));
2010 __ B(slow_path->GetEntryLabel());
2011 // slow_path will return to GetLabelOf(successor).
2012 }
2013}
2014
2015InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
2016 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08002017 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002018 assembler_(codegen->GetAssembler()),
2019 codegen_(codegen) {}
2020
2021void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2022 DCHECK_EQ(instruction->InputCount(), 2U);
2023 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2024 Primitive::Type type = instruction->GetResultType();
2025 switch (type) {
2026 case Primitive::kPrimInt: {
2027 locations->SetInAt(0, Location::RequiresRegister());
2028 HInstruction* right = instruction->InputAt(1);
2029 bool can_use_imm = false;
2030 if (right->IsConstant()) {
2031 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
2032 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
2033 can_use_imm = IsUint<16>(imm);
2034 } else if (instruction->IsAdd()) {
2035 can_use_imm = IsInt<16>(imm);
2036 } else {
2037 DCHECK(instruction->IsSub());
2038 can_use_imm = IsInt<16>(-imm);
2039 }
2040 }
2041 if (can_use_imm)
2042 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
2043 else
2044 locations->SetInAt(1, Location::RequiresRegister());
2045 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2046 break;
2047 }
2048
2049 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002050 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002051 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2052 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002053 break;
2054 }
2055
2056 case Primitive::kPrimFloat:
2057 case Primitive::kPrimDouble:
2058 DCHECK(instruction->IsAdd() || instruction->IsSub());
2059 locations->SetInAt(0, Location::RequiresFpuRegister());
2060 locations->SetInAt(1, Location::RequiresFpuRegister());
2061 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2062 break;
2063
2064 default:
2065 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
2066 }
2067}
2068
2069void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
2070 Primitive::Type type = instruction->GetType();
2071 LocationSummary* locations = instruction->GetLocations();
2072
2073 switch (type) {
2074 case Primitive::kPrimInt: {
2075 Register dst = locations->Out().AsRegister<Register>();
2076 Register lhs = locations->InAt(0).AsRegister<Register>();
2077 Location rhs_location = locations->InAt(1);
2078
2079 Register rhs_reg = ZERO;
2080 int32_t rhs_imm = 0;
2081 bool use_imm = rhs_location.IsConstant();
2082 if (use_imm) {
2083 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
2084 } else {
2085 rhs_reg = rhs_location.AsRegister<Register>();
2086 }
2087
2088 if (instruction->IsAnd()) {
2089 if (use_imm)
2090 __ Andi(dst, lhs, rhs_imm);
2091 else
2092 __ And(dst, lhs, rhs_reg);
2093 } else if (instruction->IsOr()) {
2094 if (use_imm)
2095 __ Ori(dst, lhs, rhs_imm);
2096 else
2097 __ Or(dst, lhs, rhs_reg);
2098 } else if (instruction->IsXor()) {
2099 if (use_imm)
2100 __ Xori(dst, lhs, rhs_imm);
2101 else
2102 __ Xor(dst, lhs, rhs_reg);
2103 } else if (instruction->IsAdd()) {
2104 if (use_imm)
2105 __ Addiu(dst, lhs, rhs_imm);
2106 else
2107 __ Addu(dst, lhs, rhs_reg);
2108 } else {
2109 DCHECK(instruction->IsSub());
2110 if (use_imm)
2111 __ Addiu(dst, lhs, -rhs_imm);
2112 else
2113 __ Subu(dst, lhs, rhs_reg);
2114 }
2115 break;
2116 }
2117
2118 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002119 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2120 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2121 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2122 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002123 Location rhs_location = locations->InAt(1);
2124 bool use_imm = rhs_location.IsConstant();
2125 if (!use_imm) {
2126 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2127 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2128 if (instruction->IsAnd()) {
2129 __ And(dst_low, lhs_low, rhs_low);
2130 __ And(dst_high, lhs_high, rhs_high);
2131 } else if (instruction->IsOr()) {
2132 __ Or(dst_low, lhs_low, rhs_low);
2133 __ Or(dst_high, lhs_high, rhs_high);
2134 } else if (instruction->IsXor()) {
2135 __ Xor(dst_low, lhs_low, rhs_low);
2136 __ Xor(dst_high, lhs_high, rhs_high);
2137 } else if (instruction->IsAdd()) {
2138 if (lhs_low == rhs_low) {
2139 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2140 __ Slt(TMP, lhs_low, ZERO);
2141 __ Addu(dst_low, lhs_low, rhs_low);
2142 } else {
2143 __ Addu(dst_low, lhs_low, rhs_low);
2144 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2145 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2146 }
2147 __ Addu(dst_high, lhs_high, rhs_high);
2148 __ Addu(dst_high, dst_high, TMP);
2149 } else {
2150 DCHECK(instruction->IsSub());
2151 __ Sltu(TMP, lhs_low, rhs_low);
2152 __ Subu(dst_low, lhs_low, rhs_low);
2153 __ Subu(dst_high, lhs_high, rhs_high);
2154 __ Subu(dst_high, dst_high, TMP);
2155 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002156 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002157 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2158 if (instruction->IsOr()) {
2159 uint32_t low = Low32Bits(value);
2160 uint32_t high = High32Bits(value);
2161 if (IsUint<16>(low)) {
2162 if (dst_low != lhs_low || low != 0) {
2163 __ Ori(dst_low, lhs_low, low);
2164 }
2165 } else {
2166 __ LoadConst32(TMP, low);
2167 __ Or(dst_low, lhs_low, TMP);
2168 }
2169 if (IsUint<16>(high)) {
2170 if (dst_high != lhs_high || high != 0) {
2171 __ Ori(dst_high, lhs_high, high);
2172 }
2173 } else {
2174 if (high != low) {
2175 __ LoadConst32(TMP, high);
2176 }
2177 __ Or(dst_high, lhs_high, TMP);
2178 }
2179 } else if (instruction->IsXor()) {
2180 uint32_t low = Low32Bits(value);
2181 uint32_t high = High32Bits(value);
2182 if (IsUint<16>(low)) {
2183 if (dst_low != lhs_low || low != 0) {
2184 __ Xori(dst_low, lhs_low, low);
2185 }
2186 } else {
2187 __ LoadConst32(TMP, low);
2188 __ Xor(dst_low, lhs_low, TMP);
2189 }
2190 if (IsUint<16>(high)) {
2191 if (dst_high != lhs_high || high != 0) {
2192 __ Xori(dst_high, lhs_high, high);
2193 }
2194 } else {
2195 if (high != low) {
2196 __ LoadConst32(TMP, high);
2197 }
2198 __ Xor(dst_high, lhs_high, TMP);
2199 }
2200 } else if (instruction->IsAnd()) {
2201 uint32_t low = Low32Bits(value);
2202 uint32_t high = High32Bits(value);
2203 if (IsUint<16>(low)) {
2204 __ Andi(dst_low, lhs_low, low);
2205 } else if (low != 0xFFFFFFFF) {
2206 __ LoadConst32(TMP, low);
2207 __ And(dst_low, lhs_low, TMP);
2208 } else if (dst_low != lhs_low) {
2209 __ Move(dst_low, lhs_low);
2210 }
2211 if (IsUint<16>(high)) {
2212 __ Andi(dst_high, lhs_high, high);
2213 } else if (high != 0xFFFFFFFF) {
2214 if (high != low) {
2215 __ LoadConst32(TMP, high);
2216 }
2217 __ And(dst_high, lhs_high, TMP);
2218 } else if (dst_high != lhs_high) {
2219 __ Move(dst_high, lhs_high);
2220 }
2221 } else {
2222 if (instruction->IsSub()) {
2223 value = -value;
2224 } else {
2225 DCHECK(instruction->IsAdd());
2226 }
2227 int32_t low = Low32Bits(value);
2228 int32_t high = High32Bits(value);
2229 if (IsInt<16>(low)) {
2230 if (dst_low != lhs_low || low != 0) {
2231 __ Addiu(dst_low, lhs_low, low);
2232 }
2233 if (low != 0) {
2234 __ Sltiu(AT, dst_low, low);
2235 }
2236 } else {
2237 __ LoadConst32(TMP, low);
2238 __ Addu(dst_low, lhs_low, TMP);
2239 __ Sltu(AT, dst_low, TMP);
2240 }
2241 if (IsInt<16>(high)) {
2242 if (dst_high != lhs_high || high != 0) {
2243 __ Addiu(dst_high, lhs_high, high);
2244 }
2245 } else {
2246 if (high != low) {
2247 __ LoadConst32(TMP, high);
2248 }
2249 __ Addu(dst_high, lhs_high, TMP);
2250 }
2251 if (low != 0) {
2252 __ Addu(dst_high, dst_high, AT);
2253 }
2254 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002255 }
2256 break;
2257 }
2258
2259 case Primitive::kPrimFloat:
2260 case Primitive::kPrimDouble: {
2261 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2262 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2263 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2264 if (instruction->IsAdd()) {
2265 if (type == Primitive::kPrimFloat) {
2266 __ AddS(dst, lhs, rhs);
2267 } else {
2268 __ AddD(dst, lhs, rhs);
2269 }
2270 } else {
2271 DCHECK(instruction->IsSub());
2272 if (type == Primitive::kPrimFloat) {
2273 __ SubS(dst, lhs, rhs);
2274 } else {
2275 __ SubD(dst, lhs, rhs);
2276 }
2277 }
2278 break;
2279 }
2280
2281 default:
2282 LOG(FATAL) << "Unexpected binary operation type " << type;
2283 }
2284}
2285
2286void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002287 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002288
2289 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2290 Primitive::Type type = instr->GetResultType();
2291 switch (type) {
2292 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002293 locations->SetInAt(0, Location::RequiresRegister());
2294 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2295 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2296 break;
2297 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002298 locations->SetInAt(0, Location::RequiresRegister());
2299 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2300 locations->SetOut(Location::RequiresRegister());
2301 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002302 default:
2303 LOG(FATAL) << "Unexpected shift type " << type;
2304 }
2305}
2306
2307static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2308
2309void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002310 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002311 LocationSummary* locations = instr->GetLocations();
2312 Primitive::Type type = instr->GetType();
2313
2314 Location rhs_location = locations->InAt(1);
2315 bool use_imm = rhs_location.IsConstant();
2316 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2317 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002318 const uint32_t shift_mask =
2319 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002320 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002321 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2322 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002323
2324 switch (type) {
2325 case Primitive::kPrimInt: {
2326 Register dst = locations->Out().AsRegister<Register>();
2327 Register lhs = locations->InAt(0).AsRegister<Register>();
2328 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002329 if (shift_value == 0) {
2330 if (dst != lhs) {
2331 __ Move(dst, lhs);
2332 }
2333 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002334 __ Sll(dst, lhs, shift_value);
2335 } else if (instr->IsShr()) {
2336 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002337 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002338 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002339 } else {
2340 if (has_ins_rotr) {
2341 __ Rotr(dst, lhs, shift_value);
2342 } else {
2343 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2344 __ Srl(dst, lhs, shift_value);
2345 __ Or(dst, dst, TMP);
2346 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002347 }
2348 } else {
2349 if (instr->IsShl()) {
2350 __ Sllv(dst, lhs, rhs_reg);
2351 } else if (instr->IsShr()) {
2352 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002353 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002354 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002355 } else {
2356 if (has_ins_rotr) {
2357 __ Rotrv(dst, lhs, rhs_reg);
2358 } else {
2359 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002360 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2361 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2362 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2363 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2364 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002365 __ Sllv(TMP, lhs, TMP);
2366 __ Srlv(dst, lhs, rhs_reg);
2367 __ Or(dst, dst, TMP);
2368 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002369 }
2370 }
2371 break;
2372 }
2373
2374 case Primitive::kPrimLong: {
2375 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2376 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2377 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2378 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2379 if (use_imm) {
2380 if (shift_value == 0) {
Lena Djokic8098da92017-06-28 12:07:50 +02002381 codegen_->MoveLocation(locations->Out(), locations->InAt(0), type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002382 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002383 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002384 if (instr->IsShl()) {
2385 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2386 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2387 __ Sll(dst_low, lhs_low, shift_value);
2388 } else if (instr->IsShr()) {
2389 __ Srl(dst_low, lhs_low, shift_value);
2390 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2391 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002392 } else if (instr->IsUShr()) {
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 Frunze5c7aed32015-11-25 19:41:54 -08002396 } else {
2397 __ Srl(dst_low, lhs_low, shift_value);
2398 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2399 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002400 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002401 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002402 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002403 if (instr->IsShl()) {
2404 __ Sll(dst_low, lhs_low, shift_value);
2405 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2406 __ Sll(dst_high, lhs_high, shift_value);
2407 __ Or(dst_high, dst_high, TMP);
2408 } else if (instr->IsShr()) {
2409 __ Sra(dst_high, lhs_high, shift_value);
2410 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2411 __ Srl(dst_low, lhs_low, shift_value);
2412 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002413 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002414 __ Srl(dst_high, lhs_high, shift_value);
2415 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2416 __ Srl(dst_low, lhs_low, shift_value);
2417 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002418 } else {
2419 __ Srl(TMP, lhs_low, shift_value);
2420 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2421 __ Or(dst_low, dst_low, TMP);
2422 __ Srl(TMP, lhs_high, shift_value);
2423 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2424 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002425 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002426 }
2427 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002428 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002429 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002430 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002431 __ Move(dst_low, ZERO);
2432 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002433 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002434 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002435 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002436 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002437 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002438 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002439 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002440 // 64-bit rotation by 32 is just a swap.
2441 __ Move(dst_low, lhs_high);
2442 __ Move(dst_high, lhs_low);
2443 } else {
2444 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002445 __ Srl(dst_low, lhs_high, shift_value_high);
2446 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2447 __ Srl(dst_high, lhs_low, shift_value_high);
2448 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002449 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002450 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2451 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002452 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002453 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2454 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002455 __ Or(dst_high, dst_high, TMP);
2456 }
2457 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002458 }
2459 }
2460 } else {
2461 MipsLabel done;
2462 if (instr->IsShl()) {
2463 __ Sllv(dst_low, lhs_low, rhs_reg);
2464 __ Nor(AT, ZERO, rhs_reg);
2465 __ Srl(TMP, lhs_low, 1);
2466 __ Srlv(TMP, TMP, AT);
2467 __ Sllv(dst_high, lhs_high, rhs_reg);
2468 __ Or(dst_high, dst_high, TMP);
2469 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2470 __ Beqz(TMP, &done);
2471 __ Move(dst_high, dst_low);
2472 __ Move(dst_low, ZERO);
2473 } else if (instr->IsShr()) {
2474 __ Srav(dst_high, lhs_high, rhs_reg);
2475 __ Nor(AT, ZERO, rhs_reg);
2476 __ Sll(TMP, lhs_high, 1);
2477 __ Sllv(TMP, TMP, AT);
2478 __ Srlv(dst_low, lhs_low, rhs_reg);
2479 __ Or(dst_low, dst_low, TMP);
2480 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2481 __ Beqz(TMP, &done);
2482 __ Move(dst_low, dst_high);
2483 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002484 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002485 __ Srlv(dst_high, lhs_high, rhs_reg);
2486 __ Nor(AT, ZERO, rhs_reg);
2487 __ Sll(TMP, lhs_high, 1);
2488 __ Sllv(TMP, TMP, AT);
2489 __ Srlv(dst_low, lhs_low, rhs_reg);
2490 __ Or(dst_low, dst_low, TMP);
2491 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2492 __ Beqz(TMP, &done);
2493 __ Move(dst_low, dst_high);
2494 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002495 } else {
2496 __ Nor(AT, ZERO, rhs_reg);
2497 __ Srlv(TMP, lhs_low, rhs_reg);
2498 __ Sll(dst_low, lhs_high, 1);
2499 __ Sllv(dst_low, dst_low, AT);
2500 __ Or(dst_low, dst_low, TMP);
2501 __ Srlv(TMP, lhs_high, rhs_reg);
2502 __ Sll(dst_high, lhs_low, 1);
2503 __ Sllv(dst_high, dst_high, AT);
2504 __ Or(dst_high, dst_high, TMP);
2505 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2506 __ Beqz(TMP, &done);
2507 __ Move(TMP, dst_high);
2508 __ Move(dst_high, dst_low);
2509 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002510 }
2511 __ Bind(&done);
2512 }
2513 break;
2514 }
2515
2516 default:
2517 LOG(FATAL) << "Unexpected shift operation type " << type;
2518 }
2519}
2520
2521void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2522 HandleBinaryOp(instruction);
2523}
2524
2525void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2526 HandleBinaryOp(instruction);
2527}
2528
2529void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2530 HandleBinaryOp(instruction);
2531}
2532
2533void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2534 HandleBinaryOp(instruction);
2535}
2536
2537void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002538 Primitive::Type type = instruction->GetType();
2539 bool object_array_get_with_read_barrier =
2540 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002541 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002542 new (GetGraph()->GetArena()) LocationSummary(instruction,
2543 object_array_get_with_read_barrier
2544 ? LocationSummary::kCallOnSlowPath
2545 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002546 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2547 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2548 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002549 locations->SetInAt(0, Location::RequiresRegister());
2550 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002551 if (Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002552 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2553 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002554 // The output overlaps in the case of an object array get with
2555 // read barriers enabled: we do not want the move to overwrite the
2556 // array's location, as we need it to emit the read barrier.
2557 locations->SetOut(Location::RequiresRegister(),
2558 object_array_get_with_read_barrier
2559 ? Location::kOutputOverlap
2560 : Location::kNoOutputOverlap);
2561 }
2562 // We need a temporary register for the read barrier marking slow
2563 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2564 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002565 bool temp_needed = instruction->GetIndex()->IsConstant()
2566 ? !kBakerReadBarrierThunksEnableForFields
2567 : !kBakerReadBarrierThunksEnableForArrays;
2568 if (temp_needed) {
2569 locations->AddTemp(Location::RequiresRegister());
2570 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002571 }
2572}
2573
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002574static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2575 auto null_checker = [codegen, instruction]() {
2576 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002577 };
2578 return null_checker;
2579}
2580
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002581void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2582 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002583 Location obj_loc = locations->InAt(0);
2584 Register obj = obj_loc.AsRegister<Register>();
2585 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002586 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002587 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002588 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002589
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002590 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002591 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2592 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002593 switch (type) {
2594 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002595 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002596 if (index.IsConstant()) {
2597 size_t offset =
2598 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002599 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002600 } else {
2601 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002602 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002603 }
2604 break;
2605 }
2606
2607 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002608 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002609 if (index.IsConstant()) {
2610 size_t offset =
2611 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002612 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002613 } else {
2614 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002615 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002616 }
2617 break;
2618 }
2619
2620 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002621 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002622 if (index.IsConstant()) {
2623 size_t offset =
2624 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002625 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002626 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002627 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002628 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002629 }
2630 break;
2631 }
2632
2633 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002634 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002635 if (maybe_compressed_char_at) {
2636 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2637 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2638 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2639 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2640 "Expecting 0=compressed, 1=uncompressed");
2641 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002642 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002643 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2644 if (maybe_compressed_char_at) {
2645 MipsLabel uncompressed_load, done;
2646 __ Bnez(TMP, &uncompressed_load);
2647 __ LoadFromOffset(kLoadUnsignedByte,
2648 out,
2649 obj,
2650 data_offset + (const_index << TIMES_1));
2651 __ B(&done);
2652 __ Bind(&uncompressed_load);
2653 __ LoadFromOffset(kLoadUnsignedHalfword,
2654 out,
2655 obj,
2656 data_offset + (const_index << TIMES_2));
2657 __ Bind(&done);
2658 } else {
2659 __ LoadFromOffset(kLoadUnsignedHalfword,
2660 out,
2661 obj,
2662 data_offset + (const_index << TIMES_2),
2663 null_checker);
2664 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002665 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002666 Register index_reg = index.AsRegister<Register>();
2667 if (maybe_compressed_char_at) {
2668 MipsLabel uncompressed_load, done;
2669 __ Bnez(TMP, &uncompressed_load);
2670 __ Addu(TMP, obj, index_reg);
2671 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2672 __ B(&done);
2673 __ Bind(&uncompressed_load);
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);
2676 __ Bind(&done);
2677 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002678 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002679 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2680 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002681 }
2682 break;
2683 }
2684
Alexey Frunze15958152017-02-09 19:08:30 -08002685 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002686 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002687 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002688 if (index.IsConstant()) {
2689 size_t offset =
2690 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002691 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002692 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002693 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002694 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002695 }
2696 break;
2697 }
2698
Alexey Frunze15958152017-02-09 19:08:30 -08002699 case Primitive::kPrimNot: {
2700 static_assert(
2701 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2702 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2703 // /* HeapReference<Object> */ out =
2704 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2705 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002706 bool temp_needed = index.IsConstant()
2707 ? !kBakerReadBarrierThunksEnableForFields
2708 : !kBakerReadBarrierThunksEnableForArrays;
2709 Location temp = temp_needed ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze15958152017-02-09 19:08:30 -08002710 // Note that a potential implicit null check is handled in this
2711 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07002712 DCHECK(!instruction->CanDoImplicitNullCheckOn(instruction->InputAt(0)));
2713 if (index.IsConstant()) {
2714 // Array load with a constant index can be treated as a field load.
2715 size_t offset =
2716 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2717 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
2718 out_loc,
2719 obj,
2720 offset,
2721 temp,
2722 /* needs_null_check */ false);
2723 } else {
2724 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2725 out_loc,
2726 obj,
2727 data_offset,
2728 index,
2729 temp,
2730 /* needs_null_check */ false);
2731 }
Alexey Frunze15958152017-02-09 19:08:30 -08002732 } else {
2733 Register out = out_loc.AsRegister<Register>();
2734 if (index.IsConstant()) {
2735 size_t offset =
2736 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2737 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2738 // If read barriers are enabled, emit read barriers other than
2739 // Baker's using a slow path (and also unpoison the loaded
2740 // reference, if heap poisoning is enabled).
2741 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2742 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002743 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002744 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2745 // If read barriers are enabled, emit read barriers other than
2746 // Baker's using a slow path (and also unpoison the loaded
2747 // reference, if heap poisoning is enabled).
2748 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2749 out_loc,
2750 out_loc,
2751 obj_loc,
2752 data_offset,
2753 index);
2754 }
2755 }
2756 break;
2757 }
2758
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002759 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002760 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002761 if (index.IsConstant()) {
2762 size_t offset =
2763 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002764 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002765 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002766 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002767 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002768 }
2769 break;
2770 }
2771
2772 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002773 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002774 if (index.IsConstant()) {
2775 size_t offset =
2776 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002777 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002778 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002779 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002780 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002781 }
2782 break;
2783 }
2784
2785 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002786 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002787 if (index.IsConstant()) {
2788 size_t offset =
2789 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002790 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002791 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002792 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002793 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002794 }
2795 break;
2796 }
2797
2798 case Primitive::kPrimVoid:
2799 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2800 UNREACHABLE();
2801 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002802}
2803
2804void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2805 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2806 locations->SetInAt(0, Location::RequiresRegister());
2807 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2808}
2809
2810void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2811 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002812 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002813 Register obj = locations->InAt(0).AsRegister<Register>();
2814 Register out = locations->Out().AsRegister<Register>();
2815 __ LoadFromOffset(kLoadWord, out, obj, offset);
2816 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002817 // Mask out compression flag from String's array length.
2818 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2819 __ Srl(out, out, 1u);
2820 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002821}
2822
Alexey Frunzef58b2482016-09-02 22:14:06 -07002823Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2824 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2825 ? Location::ConstantLocation(instruction->AsConstant())
2826 : Location::RequiresRegister();
2827}
2828
2829Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2830 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2831 // We can store a non-zero float or double constant without first loading it into the FPU,
2832 // but we should only prefer this if the constant has a single use.
2833 if (instruction->IsConstant() &&
2834 (instruction->AsConstant()->IsZeroBitPattern() ||
2835 instruction->GetUses().HasExactlyOneElement())) {
2836 return Location::ConstantLocation(instruction->AsConstant());
2837 // Otherwise fall through and require an FPU register for the constant.
2838 }
2839 return Location::RequiresFpuRegister();
2840}
2841
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002842void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002843 Primitive::Type value_type = instruction->GetComponentType();
2844
2845 bool needs_write_barrier =
2846 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2847 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2848
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002849 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2850 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002851 may_need_runtime_call_for_type_check ?
2852 LocationSummary::kCallOnSlowPath :
2853 LocationSummary::kNoCall);
2854
2855 locations->SetInAt(0, Location::RequiresRegister());
2856 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2857 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2858 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002859 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002860 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2861 }
2862 if (needs_write_barrier) {
2863 // Temporary register for the write barrier.
2864 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002865 }
2866}
2867
2868void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2869 LocationSummary* locations = instruction->GetLocations();
2870 Register obj = locations->InAt(0).AsRegister<Register>();
2871 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002872 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002873 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002874 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002875 bool needs_write_barrier =
2876 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002877 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002878 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002879
2880 switch (value_type) {
2881 case Primitive::kPrimBoolean:
2882 case Primitive::kPrimByte: {
2883 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002884 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002885 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002886 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002887 __ Addu(base_reg, obj, index.AsRegister<Register>());
2888 }
2889 if (value_location.IsConstant()) {
2890 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2891 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2892 } else {
2893 Register value = value_location.AsRegister<Register>();
2894 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002895 }
2896 break;
2897 }
2898
2899 case Primitive::kPrimShort:
2900 case Primitive::kPrimChar: {
2901 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002902 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002903 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002904 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002905 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002906 }
2907 if (value_location.IsConstant()) {
2908 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2909 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2910 } else {
2911 Register value = value_location.AsRegister<Register>();
2912 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002913 }
2914 break;
2915 }
2916
Alexey Frunze15958152017-02-09 19:08:30 -08002917 case Primitive::kPrimInt: {
2918 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2919 if (index.IsConstant()) {
2920 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2921 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002922 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002923 }
2924 if (value_location.IsConstant()) {
2925 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2926 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2927 } else {
2928 Register value = value_location.AsRegister<Register>();
2929 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2930 }
2931 break;
2932 }
2933
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002934 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002935 if (value_location.IsConstant()) {
2936 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002937 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002938 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002939 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002940 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002941 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002942 }
Alexey Frunze15958152017-02-09 19:08:30 -08002943 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2944 DCHECK_EQ(value, 0);
2945 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2946 DCHECK(!needs_write_barrier);
2947 DCHECK(!may_need_runtime_call_for_type_check);
2948 break;
2949 }
2950
2951 DCHECK(needs_write_barrier);
2952 Register value = value_location.AsRegister<Register>();
2953 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2954 Register temp2 = TMP; // Doesn't need to survive slow path.
2955 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2956 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2957 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2958 MipsLabel done;
2959 SlowPathCodeMIPS* slow_path = nullptr;
2960
2961 if (may_need_runtime_call_for_type_check) {
2962 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2963 codegen_->AddSlowPath(slow_path);
2964 if (instruction->GetValueCanBeNull()) {
2965 MipsLabel non_zero;
2966 __ Bnez(value, &non_zero);
2967 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2968 if (index.IsConstant()) {
2969 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002970 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002971 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002972 }
Alexey Frunze15958152017-02-09 19:08:30 -08002973 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2974 __ B(&done);
2975 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002976 }
Alexey Frunze15958152017-02-09 19:08:30 -08002977
2978 // Note that when read barriers are enabled, the type checks
2979 // are performed without read barriers. This is fine, even in
2980 // the case where a class object is in the from-space after
2981 // the flip, as a comparison involving such a type would not
2982 // produce a false positive; it may of course produce a false
2983 // negative, in which case we would take the ArraySet slow
2984 // path.
2985
2986 // /* HeapReference<Class> */ temp1 = obj->klass_
2987 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2988 __ MaybeUnpoisonHeapReference(temp1);
2989
2990 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2991 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2992 // /* HeapReference<Class> */ temp2 = value->klass_
2993 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2994 // If heap poisoning is enabled, no need to unpoison `temp1`
2995 // nor `temp2`, as we are comparing two poisoned references.
2996
2997 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2998 MipsLabel do_put;
2999 __ Beq(temp1, temp2, &do_put);
3000 // If heap poisoning is enabled, the `temp1` reference has
3001 // not been unpoisoned yet; unpoison it now.
3002 __ MaybeUnpoisonHeapReference(temp1);
3003
3004 // /* HeapReference<Class> */ temp1 = temp1->super_class_
3005 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
3006 // If heap poisoning is enabled, no need to unpoison
3007 // `temp1`, as we are comparing against null below.
3008 __ Bnez(temp1, slow_path->GetEntryLabel());
3009 __ Bind(&do_put);
3010 } else {
3011 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
3012 }
3013 }
3014
3015 Register source = value;
3016 if (kPoisonHeapReferences) {
3017 // Note that in the case where `value` is a null reference,
3018 // we do not enter this block, as a null reference does not
3019 // need poisoning.
3020 __ Move(temp1, value);
3021 __ PoisonHeapReference(temp1);
3022 source = temp1;
3023 }
3024
3025 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
3026 if (index.IsConstant()) {
3027 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003028 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003029 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08003030 }
3031 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
3032
3033 if (!may_need_runtime_call_for_type_check) {
3034 codegen_->MaybeRecordImplicitNullCheck(instruction);
3035 }
3036
3037 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
3038
3039 if (done.IsLinked()) {
3040 __ Bind(&done);
3041 }
3042
3043 if (slow_path != nullptr) {
3044 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003045 }
3046 break;
3047 }
3048
3049 case Primitive::kPrimLong: {
3050 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003051 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003052 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003053 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003054 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003055 }
3056 if (value_location.IsConstant()) {
3057 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3058 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3059 } else {
3060 Register value = value_location.AsRegisterPairLow<Register>();
3061 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003062 }
3063 break;
3064 }
3065
3066 case Primitive::kPrimFloat: {
3067 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003068 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003069 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003070 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003071 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003072 }
3073 if (value_location.IsConstant()) {
3074 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
3075 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
3076 } else {
3077 FRegister value = value_location.AsFpuRegister<FRegister>();
3078 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003079 }
3080 break;
3081 }
3082
3083 case Primitive::kPrimDouble: {
3084 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003085 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07003086 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003087 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07003088 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07003089 }
3090 if (value_location.IsConstant()) {
3091 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
3092 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
3093 } else {
3094 FRegister value = value_location.AsFpuRegister<FRegister>();
3095 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003096 }
3097 break;
3098 }
3099
3100 case Primitive::kPrimVoid:
3101 LOG(FATAL) << "Unreachable type " << instruction->GetType();
3102 UNREACHABLE();
3103 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003104}
3105
3106void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003107 RegisterSet caller_saves = RegisterSet::Empty();
3108 InvokeRuntimeCallingConvention calling_convention;
3109 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
3110 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
3111 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003112 locations->SetInAt(0, Location::RequiresRegister());
3113 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003114}
3115
3116void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3117 LocationSummary* locations = instruction->GetLocations();
3118 BoundsCheckSlowPathMIPS* slow_path =
3119 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3120 codegen_->AddSlowPath(slow_path);
3121
3122 Register index = locations->InAt(0).AsRegister<Register>();
3123 Register length = locations->InAt(1).AsRegister<Register>();
3124
3125 // length is limited by the maximum positive signed 32-bit integer.
3126 // Unsigned comparison of length and index checks for index < 0
3127 // and for length <= index simultaneously.
3128 __ Bgeu(index, length, slow_path->GetEntryLabel());
3129}
3130
Alexey Frunze15958152017-02-09 19:08:30 -08003131// Temp is used for read barrier.
3132static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3133 if (kEmitCompilerReadBarrier &&
Alexey Frunze4147fcc2017-06-17 19:57:27 -07003134 !(kUseBakerReadBarrier && kBakerReadBarrierThunksEnableForFields) &&
Alexey Frunze15958152017-02-09 19:08:30 -08003135 (kUseBakerReadBarrier ||
3136 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3137 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3138 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3139 return 1;
3140 }
3141 return 0;
3142}
3143
3144// Extra temp is used for read barrier.
3145static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3146 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3147}
3148
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003149void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003150 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3151 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3152
3153 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3154 switch (type_check_kind) {
3155 case TypeCheckKind::kExactCheck:
3156 case TypeCheckKind::kAbstractClassCheck:
3157 case TypeCheckKind::kClassHierarchyCheck:
3158 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003159 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003160 ? LocationSummary::kCallOnSlowPath
3161 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3162 break;
3163 case TypeCheckKind::kArrayCheck:
3164 case TypeCheckKind::kUnresolvedCheck:
3165 case TypeCheckKind::kInterfaceCheck:
3166 call_kind = LocationSummary::kCallOnSlowPath;
3167 break;
3168 }
3169
3170 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003171 locations->SetInAt(0, Location::RequiresRegister());
3172 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003173 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003174}
3175
3176void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003177 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003178 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003179 Location obj_loc = locations->InAt(0);
3180 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003181 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003182 Location temp_loc = locations->GetTemp(0);
3183 Register temp = temp_loc.AsRegister<Register>();
3184 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3185 DCHECK_LE(num_temps, 2u);
3186 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003187 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3188 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3189 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3190 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3191 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3192 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3193 const uint32_t object_array_data_offset =
3194 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3195 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003196
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003197 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3198 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3199 // read barriers is done for performance and code size reasons.
3200 bool is_type_check_slow_path_fatal = false;
3201 if (!kEmitCompilerReadBarrier) {
3202 is_type_check_slow_path_fatal =
3203 (type_check_kind == TypeCheckKind::kExactCheck ||
3204 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3205 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3206 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3207 !instruction->CanThrowIntoCatchBlock();
3208 }
3209 SlowPathCodeMIPS* slow_path =
3210 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3211 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003212 codegen_->AddSlowPath(slow_path);
3213
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003214 // Avoid this check if we know `obj` is not null.
3215 if (instruction->MustDoNullCheck()) {
3216 __ Beqz(obj, &done);
3217 }
3218
3219 switch (type_check_kind) {
3220 case TypeCheckKind::kExactCheck:
3221 case TypeCheckKind::kArrayCheck: {
3222 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003223 GenerateReferenceLoadTwoRegisters(instruction,
3224 temp_loc,
3225 obj_loc,
3226 class_offset,
3227 maybe_temp2_loc,
3228 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003229 // Jump to slow path for throwing the exception or doing a
3230 // more involved array check.
3231 __ Bne(temp, cls, slow_path->GetEntryLabel());
3232 break;
3233 }
3234
3235 case TypeCheckKind::kAbstractClassCheck: {
3236 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003237 GenerateReferenceLoadTwoRegisters(instruction,
3238 temp_loc,
3239 obj_loc,
3240 class_offset,
3241 maybe_temp2_loc,
3242 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003243 // If the class is abstract, we eagerly fetch the super class of the
3244 // object to avoid doing a comparison we know will fail.
3245 MipsLabel loop;
3246 __ Bind(&loop);
3247 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003248 GenerateReferenceLoadOneRegister(instruction,
3249 temp_loc,
3250 super_offset,
3251 maybe_temp2_loc,
3252 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003253 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3254 // exception.
3255 __ Beqz(temp, slow_path->GetEntryLabel());
3256 // Otherwise, compare the classes.
3257 __ Bne(temp, cls, &loop);
3258 break;
3259 }
3260
3261 case TypeCheckKind::kClassHierarchyCheck: {
3262 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003263 GenerateReferenceLoadTwoRegisters(instruction,
3264 temp_loc,
3265 obj_loc,
3266 class_offset,
3267 maybe_temp2_loc,
3268 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003269 // Walk over the class hierarchy to find a match.
3270 MipsLabel loop;
3271 __ Bind(&loop);
3272 __ Beq(temp, cls, &done);
3273 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003274 GenerateReferenceLoadOneRegister(instruction,
3275 temp_loc,
3276 super_offset,
3277 maybe_temp2_loc,
3278 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003279 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3280 // exception. Otherwise, jump to the beginning of the loop.
3281 __ Bnez(temp, &loop);
3282 __ B(slow_path->GetEntryLabel());
3283 break;
3284 }
3285
3286 case TypeCheckKind::kArrayObjectCheck: {
3287 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003288 GenerateReferenceLoadTwoRegisters(instruction,
3289 temp_loc,
3290 obj_loc,
3291 class_offset,
3292 maybe_temp2_loc,
3293 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003294 // Do an exact check.
3295 __ Beq(temp, cls, &done);
3296 // Otherwise, we need to check that the object's class is a non-primitive array.
3297 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003298 GenerateReferenceLoadOneRegister(instruction,
3299 temp_loc,
3300 component_offset,
3301 maybe_temp2_loc,
3302 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003303 // If the component type is null, jump to the slow path to throw the exception.
3304 __ Beqz(temp, slow_path->GetEntryLabel());
3305 // Otherwise, the object is indeed an array, further check that this component
3306 // type is not a primitive type.
3307 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3308 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3309 __ Bnez(temp, slow_path->GetEntryLabel());
3310 break;
3311 }
3312
3313 case TypeCheckKind::kUnresolvedCheck:
3314 // We always go into the type check slow path for the unresolved check case.
3315 // We cannot directly call the CheckCast runtime entry point
3316 // without resorting to a type checking slow path here (i.e. by
3317 // calling InvokeRuntime directly), as it would require to
3318 // assign fixed registers for the inputs of this HInstanceOf
3319 // instruction (following the runtime calling convention), which
3320 // might be cluttered by the potential first read barrier
3321 // emission at the beginning of this method.
3322 __ B(slow_path->GetEntryLabel());
3323 break;
3324
3325 case TypeCheckKind::kInterfaceCheck: {
3326 // Avoid read barriers to improve performance of the fast path. We can not get false
3327 // positives by doing this.
3328 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003329 GenerateReferenceLoadTwoRegisters(instruction,
3330 temp_loc,
3331 obj_loc,
3332 class_offset,
3333 maybe_temp2_loc,
3334 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003335 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003336 GenerateReferenceLoadTwoRegisters(instruction,
3337 temp_loc,
3338 temp_loc,
3339 iftable_offset,
3340 maybe_temp2_loc,
3341 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003342 // Iftable is never null.
3343 __ Lw(TMP, temp, array_length_offset);
3344 // Loop through the iftable and check if any class matches.
3345 MipsLabel loop;
3346 __ Bind(&loop);
3347 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3348 __ Beqz(TMP, slow_path->GetEntryLabel());
3349 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3350 __ MaybeUnpoisonHeapReference(AT);
3351 // Go to next interface.
3352 __ Addiu(TMP, TMP, -2);
3353 // Compare the classes and continue the loop if they do not match.
3354 __ Bne(AT, cls, &loop);
3355 break;
3356 }
3357 }
3358
3359 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003360 __ Bind(slow_path->GetExitLabel());
3361}
3362
3363void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3364 LocationSummary* locations =
3365 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3366 locations->SetInAt(0, Location::RequiresRegister());
3367 if (check->HasUses()) {
3368 locations->SetOut(Location::SameAsFirstInput());
3369 }
3370}
3371
3372void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3373 // We assume the class is not null.
3374 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3375 check->GetLoadClass(),
3376 check,
3377 check->GetDexPc(),
3378 true);
3379 codegen_->AddSlowPath(slow_path);
3380 GenerateClassInitializationCheck(slow_path,
3381 check->GetLocations()->InAt(0).AsRegister<Register>());
3382}
3383
3384void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3385 Primitive::Type in_type = compare->InputAt(0)->GetType();
3386
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003387 LocationSummary* locations =
3388 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003389
3390 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003391 case Primitive::kPrimBoolean:
3392 case Primitive::kPrimByte:
3393 case Primitive::kPrimShort:
3394 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003395 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003396 locations->SetInAt(0, Location::RequiresRegister());
3397 locations->SetInAt(1, Location::RequiresRegister());
3398 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3399 break;
3400
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003401 case Primitive::kPrimLong:
3402 locations->SetInAt(0, Location::RequiresRegister());
3403 locations->SetInAt(1, Location::RequiresRegister());
3404 // Output overlaps because it is written before doing the low comparison.
3405 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3406 break;
3407
3408 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003409 case Primitive::kPrimDouble:
3410 locations->SetInAt(0, Location::RequiresFpuRegister());
3411 locations->SetInAt(1, Location::RequiresFpuRegister());
3412 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003413 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003414
3415 default:
3416 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3417 }
3418}
3419
3420void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3421 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003422 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003423 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003424 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003425
3426 // 0 if: left == right
3427 // 1 if: left > right
3428 // -1 if: left < right
3429 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003430 case Primitive::kPrimBoolean:
3431 case Primitive::kPrimByte:
3432 case Primitive::kPrimShort:
3433 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003434 case Primitive::kPrimInt: {
3435 Register lhs = locations->InAt(0).AsRegister<Register>();
3436 Register rhs = locations->InAt(1).AsRegister<Register>();
3437 __ Slt(TMP, lhs, rhs);
3438 __ Slt(res, rhs, lhs);
3439 __ Subu(res, res, TMP);
3440 break;
3441 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003442 case Primitive::kPrimLong: {
3443 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003444 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3445 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3446 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3447 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3448 // TODO: more efficient (direct) comparison with a constant.
3449 __ Slt(TMP, lhs_high, rhs_high);
3450 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3451 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3452 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3453 __ Sltu(TMP, lhs_low, rhs_low);
3454 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3455 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3456 __ Bind(&done);
3457 break;
3458 }
3459
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003460 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003461 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003462 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3463 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3464 MipsLabel done;
3465 if (isR6) {
3466 __ CmpEqS(FTMP, lhs, rhs);
3467 __ LoadConst32(res, 0);
3468 __ Bc1nez(FTMP, &done);
3469 if (gt_bias) {
3470 __ CmpLtS(FTMP, lhs, rhs);
3471 __ LoadConst32(res, -1);
3472 __ Bc1nez(FTMP, &done);
3473 __ LoadConst32(res, 1);
3474 } else {
3475 __ CmpLtS(FTMP, rhs, lhs);
3476 __ LoadConst32(res, 1);
3477 __ Bc1nez(FTMP, &done);
3478 __ LoadConst32(res, -1);
3479 }
3480 } else {
3481 if (gt_bias) {
3482 __ ColtS(0, lhs, rhs);
3483 __ LoadConst32(res, -1);
3484 __ Bc1t(0, &done);
3485 __ CeqS(0, lhs, rhs);
3486 __ LoadConst32(res, 1);
3487 __ Movt(res, ZERO, 0);
3488 } else {
3489 __ ColtS(0, rhs, lhs);
3490 __ LoadConst32(res, 1);
3491 __ Bc1t(0, &done);
3492 __ CeqS(0, lhs, rhs);
3493 __ LoadConst32(res, -1);
3494 __ Movt(res, ZERO, 0);
3495 }
3496 }
3497 __ Bind(&done);
3498 break;
3499 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003500 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003501 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003502 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3503 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3504 MipsLabel done;
3505 if (isR6) {
3506 __ CmpEqD(FTMP, lhs, rhs);
3507 __ LoadConst32(res, 0);
3508 __ Bc1nez(FTMP, &done);
3509 if (gt_bias) {
3510 __ CmpLtD(FTMP, lhs, rhs);
3511 __ LoadConst32(res, -1);
3512 __ Bc1nez(FTMP, &done);
3513 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003514 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003515 __ CmpLtD(FTMP, rhs, lhs);
3516 __ LoadConst32(res, 1);
3517 __ Bc1nez(FTMP, &done);
3518 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003519 }
3520 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003521 if (gt_bias) {
3522 __ ColtD(0, lhs, rhs);
3523 __ LoadConst32(res, -1);
3524 __ Bc1t(0, &done);
3525 __ CeqD(0, lhs, rhs);
3526 __ LoadConst32(res, 1);
3527 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003528 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003529 __ ColtD(0, rhs, lhs);
3530 __ LoadConst32(res, 1);
3531 __ Bc1t(0, &done);
3532 __ CeqD(0, lhs, rhs);
3533 __ LoadConst32(res, -1);
3534 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003535 }
3536 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003537 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003538 break;
3539 }
3540
3541 default:
3542 LOG(FATAL) << "Unimplemented compare type " << in_type;
3543 }
3544}
3545
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003546void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003547 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003548 switch (instruction->InputAt(0)->GetType()) {
3549 default:
3550 case Primitive::kPrimLong:
3551 locations->SetInAt(0, Location::RequiresRegister());
3552 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3553 break;
3554
3555 case Primitive::kPrimFloat:
3556 case Primitive::kPrimDouble:
3557 locations->SetInAt(0, Location::RequiresFpuRegister());
3558 locations->SetInAt(1, Location::RequiresFpuRegister());
3559 break;
3560 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003561 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003562 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3563 }
3564}
3565
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003566void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003567 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003568 return;
3569 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003570
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003571 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003572 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003573
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003574 switch (type) {
3575 default:
3576 // Integer case.
3577 GenerateIntCompare(instruction->GetCondition(), locations);
3578 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003579
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003580 case Primitive::kPrimLong:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003581 GenerateLongCompare(instruction->GetCondition(), locations);
3582 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003583
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003584 case Primitive::kPrimFloat:
3585 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003586 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3587 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003588 }
3589}
3590
Alexey Frunze7e99e052015-11-24 19:28:01 -08003591void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3592 DCHECK(instruction->IsDiv() || instruction->IsRem());
3593 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3594
3595 LocationSummary* locations = instruction->GetLocations();
3596 Location second = locations->InAt(1);
3597 DCHECK(second.IsConstant());
3598
3599 Register out = locations->Out().AsRegister<Register>();
3600 Register dividend = locations->InAt(0).AsRegister<Register>();
3601 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3602 DCHECK(imm == 1 || imm == -1);
3603
3604 if (instruction->IsRem()) {
3605 __ Move(out, ZERO);
3606 } else {
3607 if (imm == -1) {
3608 __ Subu(out, ZERO, dividend);
3609 } else if (out != dividend) {
3610 __ Move(out, dividend);
3611 }
3612 }
3613}
3614
3615void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3616 DCHECK(instruction->IsDiv() || instruction->IsRem());
3617 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3618
3619 LocationSummary* locations = instruction->GetLocations();
3620 Location second = locations->InAt(1);
3621 DCHECK(second.IsConstant());
3622
3623 Register out = locations->Out().AsRegister<Register>();
3624 Register dividend = locations->InAt(0).AsRegister<Register>();
3625 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003626 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003627 int ctz_imm = CTZ(abs_imm);
3628
3629 if (instruction->IsDiv()) {
3630 if (ctz_imm == 1) {
3631 // Fast path for division by +/-2, which is very common.
3632 __ Srl(TMP, dividend, 31);
3633 } else {
3634 __ Sra(TMP, dividend, 31);
3635 __ Srl(TMP, TMP, 32 - ctz_imm);
3636 }
3637 __ Addu(out, dividend, TMP);
3638 __ Sra(out, out, ctz_imm);
3639 if (imm < 0) {
3640 __ Subu(out, ZERO, out);
3641 }
3642 } else {
3643 if (ctz_imm == 1) {
3644 // Fast path for modulo +/-2, which is very common.
3645 __ Sra(TMP, dividend, 31);
3646 __ Subu(out, dividend, TMP);
3647 __ Andi(out, out, 1);
3648 __ Addu(out, out, TMP);
3649 } else {
3650 __ Sra(TMP, dividend, 31);
3651 __ Srl(TMP, TMP, 32 - ctz_imm);
3652 __ Addu(out, dividend, TMP);
3653 if (IsUint<16>(abs_imm - 1)) {
3654 __ Andi(out, out, abs_imm - 1);
3655 } else {
3656 __ Sll(out, out, 32 - ctz_imm);
3657 __ Srl(out, out, 32 - ctz_imm);
3658 }
3659 __ Subu(out, out, TMP);
3660 }
3661 }
3662}
3663
3664void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3665 DCHECK(instruction->IsDiv() || instruction->IsRem());
3666 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3667
3668 LocationSummary* locations = instruction->GetLocations();
3669 Location second = locations->InAt(1);
3670 DCHECK(second.IsConstant());
3671
3672 Register out = locations->Out().AsRegister<Register>();
3673 Register dividend = locations->InAt(0).AsRegister<Register>();
3674 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3675
3676 int64_t magic;
3677 int shift;
3678 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3679
3680 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3681
3682 __ LoadConst32(TMP, magic);
3683 if (isR6) {
3684 __ MuhR6(TMP, dividend, TMP);
3685 } else {
3686 __ MultR2(dividend, TMP);
3687 __ Mfhi(TMP);
3688 }
3689 if (imm > 0 && magic < 0) {
3690 __ Addu(TMP, TMP, dividend);
3691 } else if (imm < 0 && magic > 0) {
3692 __ Subu(TMP, TMP, dividend);
3693 }
3694
3695 if (shift != 0) {
3696 __ Sra(TMP, TMP, shift);
3697 }
3698
3699 if (instruction->IsDiv()) {
3700 __ Sra(out, TMP, 31);
3701 __ Subu(out, TMP, out);
3702 } else {
3703 __ Sra(AT, TMP, 31);
3704 __ Subu(AT, TMP, AT);
3705 __ LoadConst32(TMP, imm);
3706 if (isR6) {
3707 __ MulR6(TMP, AT, TMP);
3708 } else {
3709 __ MulR2(TMP, AT, TMP);
3710 }
3711 __ Subu(out, dividend, TMP);
3712 }
3713}
3714
3715void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3716 DCHECK(instruction->IsDiv() || instruction->IsRem());
3717 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3718
3719 LocationSummary* locations = instruction->GetLocations();
3720 Register out = locations->Out().AsRegister<Register>();
3721 Location second = locations->InAt(1);
3722
3723 if (second.IsConstant()) {
3724 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3725 if (imm == 0) {
3726 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3727 } else if (imm == 1 || imm == -1) {
3728 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003729 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003730 DivRemByPowerOfTwo(instruction);
3731 } else {
3732 DCHECK(imm <= -2 || imm >= 2);
3733 GenerateDivRemWithAnyConstant(instruction);
3734 }
3735 } else {
3736 Register dividend = locations->InAt(0).AsRegister<Register>();
3737 Register divisor = second.AsRegister<Register>();
3738 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3739 if (instruction->IsDiv()) {
3740 if (isR6) {
3741 __ DivR6(out, dividend, divisor);
3742 } else {
3743 __ DivR2(out, dividend, divisor);
3744 }
3745 } else {
3746 if (isR6) {
3747 __ ModR6(out, dividend, divisor);
3748 } else {
3749 __ ModR2(out, dividend, divisor);
3750 }
3751 }
3752 }
3753}
3754
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003755void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3756 Primitive::Type type = div->GetResultType();
3757 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003758 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003759 : LocationSummary::kNoCall;
3760
3761 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3762
3763 switch (type) {
3764 case Primitive::kPrimInt:
3765 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003766 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003767 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3768 break;
3769
3770 case Primitive::kPrimLong: {
3771 InvokeRuntimeCallingConvention calling_convention;
3772 locations->SetInAt(0, Location::RegisterPairLocation(
3773 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3774 locations->SetInAt(1, Location::RegisterPairLocation(
3775 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3776 locations->SetOut(calling_convention.GetReturnLocation(type));
3777 break;
3778 }
3779
3780 case Primitive::kPrimFloat:
3781 case Primitive::kPrimDouble:
3782 locations->SetInAt(0, Location::RequiresFpuRegister());
3783 locations->SetInAt(1, Location::RequiresFpuRegister());
3784 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3785 break;
3786
3787 default:
3788 LOG(FATAL) << "Unexpected div type " << type;
3789 }
3790}
3791
3792void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3793 Primitive::Type type = instruction->GetType();
3794 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003795
3796 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003797 case Primitive::kPrimInt:
3798 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003799 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003800 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003801 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003802 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3803 break;
3804 }
3805 case Primitive::kPrimFloat:
3806 case Primitive::kPrimDouble: {
3807 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3808 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3809 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3810 if (type == Primitive::kPrimFloat) {
3811 __ DivS(dst, lhs, rhs);
3812 } else {
3813 __ DivD(dst, lhs, rhs);
3814 }
3815 break;
3816 }
3817 default:
3818 LOG(FATAL) << "Unexpected div type " << type;
3819 }
3820}
3821
3822void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003823 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003824 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003825}
3826
3827void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3828 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3829 codegen_->AddSlowPath(slow_path);
3830 Location value = instruction->GetLocations()->InAt(0);
3831 Primitive::Type type = instruction->GetType();
3832
3833 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003834 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003835 case Primitive::kPrimByte:
3836 case Primitive::kPrimChar:
3837 case Primitive::kPrimShort:
3838 case Primitive::kPrimInt: {
3839 if (value.IsConstant()) {
3840 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3841 __ B(slow_path->GetEntryLabel());
3842 } else {
3843 // A division by a non-null constant is valid. We don't need to perform
3844 // any check, so simply fall through.
3845 }
3846 } else {
3847 DCHECK(value.IsRegister()) << value;
3848 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3849 }
3850 break;
3851 }
3852 case Primitive::kPrimLong: {
3853 if (value.IsConstant()) {
3854 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3855 __ B(slow_path->GetEntryLabel());
3856 } else {
3857 // A division by a non-null constant is valid. We don't need to perform
3858 // any check, so simply fall through.
3859 }
3860 } else {
3861 DCHECK(value.IsRegisterPair()) << value;
3862 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3863 __ Beqz(TMP, slow_path->GetEntryLabel());
3864 }
3865 break;
3866 }
3867 default:
3868 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3869 }
3870}
3871
3872void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3873 LocationSummary* locations =
3874 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3875 locations->SetOut(Location::ConstantLocation(constant));
3876}
3877
3878void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3879 // Will be generated at use site.
3880}
3881
3882void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3883 exit->SetLocations(nullptr);
3884}
3885
3886void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3887}
3888
3889void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3890 LocationSummary* locations =
3891 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3892 locations->SetOut(Location::ConstantLocation(constant));
3893}
3894
3895void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3896 // Will be generated at use site.
3897}
3898
3899void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3900 got->SetLocations(nullptr);
3901}
3902
3903void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3904 DCHECK(!successor->IsExitBlock());
3905 HBasicBlock* block = got->GetBlock();
3906 HInstruction* previous = got->GetPrevious();
3907 HLoopInformation* info = block->GetLoopInformation();
3908
3909 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3910 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3911 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3912 return;
3913 }
3914 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3915 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3916 }
3917 if (!codegen_->GoesToNextBlock(block, successor)) {
3918 __ B(codegen_->GetLabelOf(successor));
3919 }
3920}
3921
3922void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3923 HandleGoto(got, got->GetSuccessor());
3924}
3925
3926void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3927 try_boundary->SetLocations(nullptr);
3928}
3929
3930void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3931 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3932 if (!successor->IsExitBlock()) {
3933 HandleGoto(try_boundary, successor);
3934 }
3935}
3936
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003937void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3938 LocationSummary* locations) {
3939 Register dst = locations->Out().AsRegister<Register>();
3940 Register lhs = locations->InAt(0).AsRegister<Register>();
3941 Location rhs_location = locations->InAt(1);
3942 Register rhs_reg = ZERO;
3943 int64_t rhs_imm = 0;
3944 bool use_imm = rhs_location.IsConstant();
3945 if (use_imm) {
3946 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3947 } else {
3948 rhs_reg = rhs_location.AsRegister<Register>();
3949 }
3950
3951 switch (cond) {
3952 case kCondEQ:
3953 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003954 if (use_imm && IsInt<16>(-rhs_imm)) {
3955 if (rhs_imm == 0) {
3956 if (cond == kCondEQ) {
3957 __ Sltiu(dst, lhs, 1);
3958 } else {
3959 __ Sltu(dst, ZERO, lhs);
3960 }
3961 } else {
3962 __ Addiu(dst, lhs, -rhs_imm);
3963 if (cond == kCondEQ) {
3964 __ Sltiu(dst, dst, 1);
3965 } else {
3966 __ Sltu(dst, ZERO, dst);
3967 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003968 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003969 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003970 if (use_imm && IsUint<16>(rhs_imm)) {
3971 __ Xori(dst, lhs, rhs_imm);
3972 } else {
3973 if (use_imm) {
3974 rhs_reg = TMP;
3975 __ LoadConst32(rhs_reg, rhs_imm);
3976 }
3977 __ Xor(dst, lhs, rhs_reg);
3978 }
3979 if (cond == kCondEQ) {
3980 __ Sltiu(dst, dst, 1);
3981 } else {
3982 __ Sltu(dst, ZERO, dst);
3983 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003984 }
3985 break;
3986
3987 case kCondLT:
3988 case kCondGE:
3989 if (use_imm && IsInt<16>(rhs_imm)) {
3990 __ Slti(dst, lhs, rhs_imm);
3991 } else {
3992 if (use_imm) {
3993 rhs_reg = TMP;
3994 __ LoadConst32(rhs_reg, rhs_imm);
3995 }
3996 __ Slt(dst, lhs, rhs_reg);
3997 }
3998 if (cond == kCondGE) {
3999 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4000 // only the slt instruction but no sge.
4001 __ Xori(dst, dst, 1);
4002 }
4003 break;
4004
4005 case kCondLE:
4006 case kCondGT:
4007 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4008 // Simulate lhs <= rhs via lhs < rhs + 1.
4009 __ Slti(dst, lhs, rhs_imm + 1);
4010 if (cond == kCondGT) {
4011 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4012 // only the slti instruction but no sgti.
4013 __ Xori(dst, dst, 1);
4014 }
4015 } else {
4016 if (use_imm) {
4017 rhs_reg = TMP;
4018 __ LoadConst32(rhs_reg, rhs_imm);
4019 }
4020 __ Slt(dst, rhs_reg, lhs);
4021 if (cond == kCondLE) {
4022 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4023 // only the slt instruction but no sle.
4024 __ Xori(dst, dst, 1);
4025 }
4026 }
4027 break;
4028
4029 case kCondB:
4030 case kCondAE:
4031 if (use_imm && IsInt<16>(rhs_imm)) {
4032 // Sltiu sign-extends its 16-bit immediate operand before
4033 // the comparison and thus lets us compare directly with
4034 // unsigned values in the ranges [0, 0x7fff] and
4035 // [0xffff8000, 0xffffffff].
4036 __ Sltiu(dst, lhs, rhs_imm);
4037 } else {
4038 if (use_imm) {
4039 rhs_reg = TMP;
4040 __ LoadConst32(rhs_reg, rhs_imm);
4041 }
4042 __ Sltu(dst, lhs, rhs_reg);
4043 }
4044 if (cond == kCondAE) {
4045 // Simulate lhs >= rhs via !(lhs < rhs) since there's
4046 // only the sltu instruction but no sgeu.
4047 __ Xori(dst, dst, 1);
4048 }
4049 break;
4050
4051 case kCondBE:
4052 case kCondA:
4053 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4054 // Simulate lhs <= rhs via lhs < rhs + 1.
4055 // Note that this only works if rhs + 1 does not overflow
4056 // to 0, hence the check above.
4057 // Sltiu sign-extends its 16-bit immediate operand before
4058 // the comparison and thus lets us compare directly with
4059 // unsigned values in the ranges [0, 0x7fff] and
4060 // [0xffff8000, 0xffffffff].
4061 __ Sltiu(dst, lhs, rhs_imm + 1);
4062 if (cond == kCondA) {
4063 // Simulate lhs > rhs via !(lhs <= rhs) since there's
4064 // only the sltiu instruction but no sgtiu.
4065 __ Xori(dst, dst, 1);
4066 }
4067 } else {
4068 if (use_imm) {
4069 rhs_reg = TMP;
4070 __ LoadConst32(rhs_reg, rhs_imm);
4071 }
4072 __ Sltu(dst, rhs_reg, lhs);
4073 if (cond == kCondBE) {
4074 // Simulate lhs <= rhs via !(rhs < lhs) since there's
4075 // only the sltu instruction but no sleu.
4076 __ Xori(dst, dst, 1);
4077 }
4078 }
4079 break;
4080 }
4081}
4082
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004083bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
4084 LocationSummary* input_locations,
4085 Register dst) {
4086 Register lhs = input_locations->InAt(0).AsRegister<Register>();
4087 Location rhs_location = input_locations->InAt(1);
4088 Register rhs_reg = ZERO;
4089 int64_t rhs_imm = 0;
4090 bool use_imm = rhs_location.IsConstant();
4091 if (use_imm) {
4092 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4093 } else {
4094 rhs_reg = rhs_location.AsRegister<Register>();
4095 }
4096
4097 switch (cond) {
4098 case kCondEQ:
4099 case kCondNE:
4100 if (use_imm && IsInt<16>(-rhs_imm)) {
4101 __ Addiu(dst, lhs, -rhs_imm);
4102 } else if (use_imm && IsUint<16>(rhs_imm)) {
4103 __ Xori(dst, lhs, rhs_imm);
4104 } else {
4105 if (use_imm) {
4106 rhs_reg = TMP;
4107 __ LoadConst32(rhs_reg, rhs_imm);
4108 }
4109 __ Xor(dst, lhs, rhs_reg);
4110 }
4111 return (cond == kCondEQ);
4112
4113 case kCondLT:
4114 case kCondGE:
4115 if (use_imm && IsInt<16>(rhs_imm)) {
4116 __ Slti(dst, lhs, rhs_imm);
4117 } else {
4118 if (use_imm) {
4119 rhs_reg = TMP;
4120 __ LoadConst32(rhs_reg, rhs_imm);
4121 }
4122 __ Slt(dst, lhs, rhs_reg);
4123 }
4124 return (cond == kCondGE);
4125
4126 case kCondLE:
4127 case kCondGT:
4128 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4129 // Simulate lhs <= rhs via lhs < rhs + 1.
4130 __ Slti(dst, lhs, rhs_imm + 1);
4131 return (cond == kCondGT);
4132 } else {
4133 if (use_imm) {
4134 rhs_reg = TMP;
4135 __ LoadConst32(rhs_reg, rhs_imm);
4136 }
4137 __ Slt(dst, rhs_reg, lhs);
4138 return (cond == kCondLE);
4139 }
4140
4141 case kCondB:
4142 case kCondAE:
4143 if (use_imm && IsInt<16>(rhs_imm)) {
4144 // Sltiu sign-extends its 16-bit immediate operand before
4145 // the comparison and thus lets us compare directly with
4146 // unsigned values in the ranges [0, 0x7fff] and
4147 // [0xffff8000, 0xffffffff].
4148 __ Sltiu(dst, lhs, rhs_imm);
4149 } else {
4150 if (use_imm) {
4151 rhs_reg = TMP;
4152 __ LoadConst32(rhs_reg, rhs_imm);
4153 }
4154 __ Sltu(dst, lhs, rhs_reg);
4155 }
4156 return (cond == kCondAE);
4157
4158 case kCondBE:
4159 case kCondA:
4160 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4161 // Simulate lhs <= rhs via lhs < rhs + 1.
4162 // Note that this only works if rhs + 1 does not overflow
4163 // to 0, hence the check above.
4164 // Sltiu sign-extends its 16-bit immediate operand before
4165 // the comparison and thus lets us compare directly with
4166 // unsigned values in the ranges [0, 0x7fff] and
4167 // [0xffff8000, 0xffffffff].
4168 __ Sltiu(dst, lhs, rhs_imm + 1);
4169 return (cond == kCondA);
4170 } else {
4171 if (use_imm) {
4172 rhs_reg = TMP;
4173 __ LoadConst32(rhs_reg, rhs_imm);
4174 }
4175 __ Sltu(dst, rhs_reg, lhs);
4176 return (cond == kCondBE);
4177 }
4178 }
4179}
4180
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004181void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4182 LocationSummary* locations,
4183 MipsLabel* label) {
4184 Register lhs = locations->InAt(0).AsRegister<Register>();
4185 Location rhs_location = locations->InAt(1);
4186 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004187 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004188 bool use_imm = rhs_location.IsConstant();
4189 if (use_imm) {
4190 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4191 } else {
4192 rhs_reg = rhs_location.AsRegister<Register>();
4193 }
4194
4195 if (use_imm && rhs_imm == 0) {
4196 switch (cond) {
4197 case kCondEQ:
4198 case kCondBE: // <= 0 if zero
4199 __ Beqz(lhs, label);
4200 break;
4201 case kCondNE:
4202 case kCondA: // > 0 if non-zero
4203 __ Bnez(lhs, label);
4204 break;
4205 case kCondLT:
4206 __ Bltz(lhs, label);
4207 break;
4208 case kCondGE:
4209 __ Bgez(lhs, label);
4210 break;
4211 case kCondLE:
4212 __ Blez(lhs, label);
4213 break;
4214 case kCondGT:
4215 __ Bgtz(lhs, label);
4216 break;
4217 case kCondB: // always false
4218 break;
4219 case kCondAE: // always true
4220 __ B(label);
4221 break;
4222 }
4223 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004224 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4225 if (isR6 || !use_imm) {
4226 if (use_imm) {
4227 rhs_reg = TMP;
4228 __ LoadConst32(rhs_reg, rhs_imm);
4229 }
4230 switch (cond) {
4231 case kCondEQ:
4232 __ Beq(lhs, rhs_reg, label);
4233 break;
4234 case kCondNE:
4235 __ Bne(lhs, rhs_reg, label);
4236 break;
4237 case kCondLT:
4238 __ Blt(lhs, rhs_reg, label);
4239 break;
4240 case kCondGE:
4241 __ Bge(lhs, rhs_reg, label);
4242 break;
4243 case kCondLE:
4244 __ Bge(rhs_reg, lhs, label);
4245 break;
4246 case kCondGT:
4247 __ Blt(rhs_reg, lhs, label);
4248 break;
4249 case kCondB:
4250 __ Bltu(lhs, rhs_reg, label);
4251 break;
4252 case kCondAE:
4253 __ Bgeu(lhs, rhs_reg, label);
4254 break;
4255 case kCondBE:
4256 __ Bgeu(rhs_reg, lhs, label);
4257 break;
4258 case kCondA:
4259 __ Bltu(rhs_reg, lhs, label);
4260 break;
4261 }
4262 } else {
4263 // Special cases for more efficient comparison with constants on R2.
4264 switch (cond) {
4265 case kCondEQ:
4266 __ LoadConst32(TMP, rhs_imm);
4267 __ Beq(lhs, TMP, label);
4268 break;
4269 case kCondNE:
4270 __ LoadConst32(TMP, rhs_imm);
4271 __ Bne(lhs, TMP, label);
4272 break;
4273 case kCondLT:
4274 if (IsInt<16>(rhs_imm)) {
4275 __ Slti(TMP, lhs, rhs_imm);
4276 __ Bnez(TMP, label);
4277 } else {
4278 __ LoadConst32(TMP, rhs_imm);
4279 __ Blt(lhs, TMP, label);
4280 }
4281 break;
4282 case kCondGE:
4283 if (IsInt<16>(rhs_imm)) {
4284 __ Slti(TMP, lhs, rhs_imm);
4285 __ Beqz(TMP, label);
4286 } else {
4287 __ LoadConst32(TMP, rhs_imm);
4288 __ Bge(lhs, TMP, label);
4289 }
4290 break;
4291 case kCondLE:
4292 if (IsInt<16>(rhs_imm + 1)) {
4293 // Simulate lhs <= rhs via lhs < rhs + 1.
4294 __ Slti(TMP, lhs, rhs_imm + 1);
4295 __ Bnez(TMP, label);
4296 } else {
4297 __ LoadConst32(TMP, rhs_imm);
4298 __ Bge(TMP, lhs, label);
4299 }
4300 break;
4301 case kCondGT:
4302 if (IsInt<16>(rhs_imm + 1)) {
4303 // Simulate lhs > rhs via !(lhs < rhs + 1).
4304 __ Slti(TMP, lhs, rhs_imm + 1);
4305 __ Beqz(TMP, label);
4306 } else {
4307 __ LoadConst32(TMP, rhs_imm);
4308 __ Blt(TMP, lhs, label);
4309 }
4310 break;
4311 case kCondB:
4312 if (IsInt<16>(rhs_imm)) {
4313 __ Sltiu(TMP, lhs, rhs_imm);
4314 __ Bnez(TMP, label);
4315 } else {
4316 __ LoadConst32(TMP, rhs_imm);
4317 __ Bltu(lhs, TMP, label);
4318 }
4319 break;
4320 case kCondAE:
4321 if (IsInt<16>(rhs_imm)) {
4322 __ Sltiu(TMP, lhs, rhs_imm);
4323 __ Beqz(TMP, label);
4324 } else {
4325 __ LoadConst32(TMP, rhs_imm);
4326 __ Bgeu(lhs, TMP, label);
4327 }
4328 break;
4329 case kCondBE:
4330 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4331 // Simulate lhs <= rhs via lhs < rhs + 1.
4332 // Note that this only works if rhs + 1 does not overflow
4333 // to 0, hence the check above.
4334 __ Sltiu(TMP, lhs, rhs_imm + 1);
4335 __ Bnez(TMP, label);
4336 } else {
4337 __ LoadConst32(TMP, rhs_imm);
4338 __ Bgeu(TMP, lhs, label);
4339 }
4340 break;
4341 case kCondA:
4342 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4343 // Simulate lhs > rhs via !(lhs < rhs + 1).
4344 // Note that this only works if rhs + 1 does not overflow
4345 // to 0, hence the check above.
4346 __ Sltiu(TMP, lhs, rhs_imm + 1);
4347 __ Beqz(TMP, label);
4348 } else {
4349 __ LoadConst32(TMP, rhs_imm);
4350 __ Bltu(TMP, lhs, label);
4351 }
4352 break;
4353 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004354 }
4355 }
4356}
4357
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004358void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4359 LocationSummary* locations) {
4360 Register dst = locations->Out().AsRegister<Register>();
4361 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4362 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4363 Location rhs_location = locations->InAt(1);
4364 Register rhs_high = ZERO;
4365 Register rhs_low = ZERO;
4366 int64_t imm = 0;
4367 uint32_t imm_high = 0;
4368 uint32_t imm_low = 0;
4369 bool use_imm = rhs_location.IsConstant();
4370 if (use_imm) {
4371 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4372 imm_high = High32Bits(imm);
4373 imm_low = Low32Bits(imm);
4374 } else {
4375 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4376 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4377 }
4378 if (use_imm && imm == 0) {
4379 switch (cond) {
4380 case kCondEQ:
4381 case kCondBE: // <= 0 if zero
4382 __ Or(dst, lhs_high, lhs_low);
4383 __ Sltiu(dst, dst, 1);
4384 break;
4385 case kCondNE:
4386 case kCondA: // > 0 if non-zero
4387 __ Or(dst, lhs_high, lhs_low);
4388 __ Sltu(dst, ZERO, dst);
4389 break;
4390 case kCondLT:
4391 __ Slt(dst, lhs_high, ZERO);
4392 break;
4393 case kCondGE:
4394 __ Slt(dst, lhs_high, ZERO);
4395 __ Xori(dst, dst, 1);
4396 break;
4397 case kCondLE:
4398 __ Or(TMP, lhs_high, lhs_low);
4399 __ Sra(AT, lhs_high, 31);
4400 __ Sltu(dst, AT, TMP);
4401 __ Xori(dst, dst, 1);
4402 break;
4403 case kCondGT:
4404 __ Or(TMP, lhs_high, lhs_low);
4405 __ Sra(AT, lhs_high, 31);
4406 __ Sltu(dst, AT, TMP);
4407 break;
4408 case kCondB: // always false
4409 __ Andi(dst, dst, 0);
4410 break;
4411 case kCondAE: // always true
4412 __ Ori(dst, ZERO, 1);
4413 break;
4414 }
4415 } else if (use_imm) {
4416 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4417 switch (cond) {
4418 case kCondEQ:
4419 __ LoadConst32(TMP, imm_high);
4420 __ Xor(TMP, TMP, lhs_high);
4421 __ LoadConst32(AT, imm_low);
4422 __ Xor(AT, AT, lhs_low);
4423 __ Or(dst, TMP, AT);
4424 __ Sltiu(dst, dst, 1);
4425 break;
4426 case kCondNE:
4427 __ LoadConst32(TMP, imm_high);
4428 __ Xor(TMP, TMP, lhs_high);
4429 __ LoadConst32(AT, imm_low);
4430 __ Xor(AT, AT, lhs_low);
4431 __ Or(dst, TMP, AT);
4432 __ Sltu(dst, ZERO, dst);
4433 break;
4434 case kCondLT:
4435 case kCondGE:
4436 if (dst == lhs_low) {
4437 __ LoadConst32(TMP, imm_low);
4438 __ Sltu(dst, lhs_low, TMP);
4439 }
4440 __ LoadConst32(TMP, imm_high);
4441 __ Slt(AT, lhs_high, TMP);
4442 __ Slt(TMP, TMP, lhs_high);
4443 if (dst != lhs_low) {
4444 __ LoadConst32(dst, imm_low);
4445 __ Sltu(dst, lhs_low, dst);
4446 }
4447 __ Slt(dst, TMP, dst);
4448 __ Or(dst, dst, AT);
4449 if (cond == kCondGE) {
4450 __ Xori(dst, dst, 1);
4451 }
4452 break;
4453 case kCondGT:
4454 case kCondLE:
4455 if (dst == lhs_low) {
4456 __ LoadConst32(TMP, imm_low);
4457 __ Sltu(dst, TMP, lhs_low);
4458 }
4459 __ LoadConst32(TMP, imm_high);
4460 __ Slt(AT, TMP, lhs_high);
4461 __ Slt(TMP, lhs_high, TMP);
4462 if (dst != lhs_low) {
4463 __ LoadConst32(dst, imm_low);
4464 __ Sltu(dst, dst, lhs_low);
4465 }
4466 __ Slt(dst, TMP, dst);
4467 __ Or(dst, dst, AT);
4468 if (cond == kCondLE) {
4469 __ Xori(dst, dst, 1);
4470 }
4471 break;
4472 case kCondB:
4473 case kCondAE:
4474 if (dst == lhs_low) {
4475 __ LoadConst32(TMP, imm_low);
4476 __ Sltu(dst, lhs_low, TMP);
4477 }
4478 __ LoadConst32(TMP, imm_high);
4479 __ Sltu(AT, lhs_high, TMP);
4480 __ Sltu(TMP, TMP, lhs_high);
4481 if (dst != lhs_low) {
4482 __ LoadConst32(dst, imm_low);
4483 __ Sltu(dst, lhs_low, dst);
4484 }
4485 __ Slt(dst, TMP, dst);
4486 __ Or(dst, dst, AT);
4487 if (cond == kCondAE) {
4488 __ Xori(dst, dst, 1);
4489 }
4490 break;
4491 case kCondA:
4492 case kCondBE:
4493 if (dst == lhs_low) {
4494 __ LoadConst32(TMP, imm_low);
4495 __ Sltu(dst, TMP, lhs_low);
4496 }
4497 __ LoadConst32(TMP, imm_high);
4498 __ Sltu(AT, TMP, lhs_high);
4499 __ Sltu(TMP, lhs_high, TMP);
4500 if (dst != lhs_low) {
4501 __ LoadConst32(dst, imm_low);
4502 __ Sltu(dst, dst, lhs_low);
4503 }
4504 __ Slt(dst, TMP, dst);
4505 __ Or(dst, dst, AT);
4506 if (cond == kCondBE) {
4507 __ Xori(dst, dst, 1);
4508 }
4509 break;
4510 }
4511 } else {
4512 switch (cond) {
4513 case kCondEQ:
4514 __ Xor(TMP, lhs_high, rhs_high);
4515 __ Xor(AT, lhs_low, rhs_low);
4516 __ Or(dst, TMP, AT);
4517 __ Sltiu(dst, dst, 1);
4518 break;
4519 case kCondNE:
4520 __ Xor(TMP, lhs_high, rhs_high);
4521 __ Xor(AT, lhs_low, rhs_low);
4522 __ Or(dst, TMP, AT);
4523 __ Sltu(dst, ZERO, dst);
4524 break;
4525 case kCondLT:
4526 case kCondGE:
4527 __ Slt(TMP, rhs_high, lhs_high);
4528 __ Sltu(AT, lhs_low, rhs_low);
4529 __ Slt(TMP, TMP, AT);
4530 __ Slt(AT, lhs_high, rhs_high);
4531 __ Or(dst, AT, TMP);
4532 if (cond == kCondGE) {
4533 __ Xori(dst, dst, 1);
4534 }
4535 break;
4536 case kCondGT:
4537 case kCondLE:
4538 __ Slt(TMP, lhs_high, rhs_high);
4539 __ Sltu(AT, rhs_low, lhs_low);
4540 __ Slt(TMP, TMP, AT);
4541 __ Slt(AT, rhs_high, lhs_high);
4542 __ Or(dst, AT, TMP);
4543 if (cond == kCondLE) {
4544 __ Xori(dst, dst, 1);
4545 }
4546 break;
4547 case kCondB:
4548 case kCondAE:
4549 __ Sltu(TMP, rhs_high, lhs_high);
4550 __ Sltu(AT, lhs_low, rhs_low);
4551 __ Slt(TMP, TMP, AT);
4552 __ Sltu(AT, lhs_high, rhs_high);
4553 __ Or(dst, AT, TMP);
4554 if (cond == kCondAE) {
4555 __ Xori(dst, dst, 1);
4556 }
4557 break;
4558 case kCondA:
4559 case kCondBE:
4560 __ Sltu(TMP, lhs_high, rhs_high);
4561 __ Sltu(AT, rhs_low, lhs_low);
4562 __ Slt(TMP, TMP, AT);
4563 __ Sltu(AT, rhs_high, lhs_high);
4564 __ Or(dst, AT, TMP);
4565 if (cond == kCondBE) {
4566 __ Xori(dst, dst, 1);
4567 }
4568 break;
4569 }
4570 }
4571}
4572
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004573void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4574 LocationSummary* locations,
4575 MipsLabel* label) {
4576 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4577 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4578 Location rhs_location = locations->InAt(1);
4579 Register rhs_high = ZERO;
4580 Register rhs_low = ZERO;
4581 int64_t imm = 0;
4582 uint32_t imm_high = 0;
4583 uint32_t imm_low = 0;
4584 bool use_imm = rhs_location.IsConstant();
4585 if (use_imm) {
4586 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4587 imm_high = High32Bits(imm);
4588 imm_low = Low32Bits(imm);
4589 } else {
4590 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4591 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4592 }
4593
4594 if (use_imm && imm == 0) {
4595 switch (cond) {
4596 case kCondEQ:
4597 case kCondBE: // <= 0 if zero
4598 __ Or(TMP, lhs_high, lhs_low);
4599 __ Beqz(TMP, label);
4600 break;
4601 case kCondNE:
4602 case kCondA: // > 0 if non-zero
4603 __ Or(TMP, lhs_high, lhs_low);
4604 __ Bnez(TMP, label);
4605 break;
4606 case kCondLT:
4607 __ Bltz(lhs_high, label);
4608 break;
4609 case kCondGE:
4610 __ Bgez(lhs_high, label);
4611 break;
4612 case kCondLE:
4613 __ Or(TMP, lhs_high, lhs_low);
4614 __ Sra(AT, lhs_high, 31);
4615 __ Bgeu(AT, TMP, label);
4616 break;
4617 case kCondGT:
4618 __ Or(TMP, lhs_high, lhs_low);
4619 __ Sra(AT, lhs_high, 31);
4620 __ Bltu(AT, TMP, label);
4621 break;
4622 case kCondB: // always false
4623 break;
4624 case kCondAE: // always true
4625 __ B(label);
4626 break;
4627 }
4628 } else if (use_imm) {
4629 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4630 switch (cond) {
4631 case kCondEQ:
4632 __ LoadConst32(TMP, imm_high);
4633 __ Xor(TMP, TMP, lhs_high);
4634 __ LoadConst32(AT, imm_low);
4635 __ Xor(AT, AT, lhs_low);
4636 __ Or(TMP, TMP, AT);
4637 __ Beqz(TMP, label);
4638 break;
4639 case kCondNE:
4640 __ LoadConst32(TMP, imm_high);
4641 __ Xor(TMP, TMP, lhs_high);
4642 __ LoadConst32(AT, imm_low);
4643 __ Xor(AT, AT, lhs_low);
4644 __ Or(TMP, TMP, AT);
4645 __ Bnez(TMP, label);
4646 break;
4647 case kCondLT:
4648 __ LoadConst32(TMP, imm_high);
4649 __ Blt(lhs_high, TMP, label);
4650 __ Slt(TMP, TMP, lhs_high);
4651 __ LoadConst32(AT, imm_low);
4652 __ Sltu(AT, lhs_low, AT);
4653 __ Blt(TMP, AT, label);
4654 break;
4655 case kCondGE:
4656 __ LoadConst32(TMP, imm_high);
4657 __ Blt(TMP, lhs_high, label);
4658 __ Slt(TMP, lhs_high, TMP);
4659 __ LoadConst32(AT, imm_low);
4660 __ Sltu(AT, lhs_low, AT);
4661 __ Or(TMP, TMP, AT);
4662 __ Beqz(TMP, label);
4663 break;
4664 case kCondLE:
4665 __ LoadConst32(TMP, imm_high);
4666 __ Blt(lhs_high, TMP, label);
4667 __ Slt(TMP, TMP, lhs_high);
4668 __ LoadConst32(AT, imm_low);
4669 __ Sltu(AT, AT, lhs_low);
4670 __ Or(TMP, TMP, AT);
4671 __ Beqz(TMP, label);
4672 break;
4673 case kCondGT:
4674 __ LoadConst32(TMP, imm_high);
4675 __ Blt(TMP, lhs_high, label);
4676 __ Slt(TMP, lhs_high, TMP);
4677 __ LoadConst32(AT, imm_low);
4678 __ Sltu(AT, AT, lhs_low);
4679 __ Blt(TMP, AT, label);
4680 break;
4681 case kCondB:
4682 __ LoadConst32(TMP, imm_high);
4683 __ Bltu(lhs_high, TMP, label);
4684 __ Sltu(TMP, TMP, lhs_high);
4685 __ LoadConst32(AT, imm_low);
4686 __ Sltu(AT, lhs_low, AT);
4687 __ Blt(TMP, AT, label);
4688 break;
4689 case kCondAE:
4690 __ LoadConst32(TMP, imm_high);
4691 __ Bltu(TMP, lhs_high, label);
4692 __ Sltu(TMP, lhs_high, TMP);
4693 __ LoadConst32(AT, imm_low);
4694 __ Sltu(AT, lhs_low, AT);
4695 __ Or(TMP, TMP, AT);
4696 __ Beqz(TMP, label);
4697 break;
4698 case kCondBE:
4699 __ LoadConst32(TMP, imm_high);
4700 __ Bltu(lhs_high, TMP, label);
4701 __ Sltu(TMP, TMP, lhs_high);
4702 __ LoadConst32(AT, imm_low);
4703 __ Sltu(AT, AT, lhs_low);
4704 __ Or(TMP, TMP, AT);
4705 __ Beqz(TMP, label);
4706 break;
4707 case kCondA:
4708 __ LoadConst32(TMP, imm_high);
4709 __ Bltu(TMP, lhs_high, label);
4710 __ Sltu(TMP, lhs_high, TMP);
4711 __ LoadConst32(AT, imm_low);
4712 __ Sltu(AT, AT, lhs_low);
4713 __ Blt(TMP, AT, label);
4714 break;
4715 }
4716 } else {
4717 switch (cond) {
4718 case kCondEQ:
4719 __ Xor(TMP, lhs_high, rhs_high);
4720 __ Xor(AT, lhs_low, rhs_low);
4721 __ Or(TMP, TMP, AT);
4722 __ Beqz(TMP, label);
4723 break;
4724 case kCondNE:
4725 __ Xor(TMP, lhs_high, rhs_high);
4726 __ Xor(AT, lhs_low, rhs_low);
4727 __ Or(TMP, TMP, AT);
4728 __ Bnez(TMP, label);
4729 break;
4730 case kCondLT:
4731 __ Blt(lhs_high, rhs_high, label);
4732 __ Slt(TMP, rhs_high, lhs_high);
4733 __ Sltu(AT, lhs_low, rhs_low);
4734 __ Blt(TMP, AT, label);
4735 break;
4736 case kCondGE:
4737 __ Blt(rhs_high, lhs_high, label);
4738 __ Slt(TMP, lhs_high, rhs_high);
4739 __ Sltu(AT, lhs_low, rhs_low);
4740 __ Or(TMP, TMP, AT);
4741 __ Beqz(TMP, label);
4742 break;
4743 case kCondLE:
4744 __ Blt(lhs_high, rhs_high, label);
4745 __ Slt(TMP, rhs_high, lhs_high);
4746 __ Sltu(AT, rhs_low, lhs_low);
4747 __ Or(TMP, TMP, AT);
4748 __ Beqz(TMP, label);
4749 break;
4750 case kCondGT:
4751 __ Blt(rhs_high, lhs_high, label);
4752 __ Slt(TMP, lhs_high, rhs_high);
4753 __ Sltu(AT, rhs_low, lhs_low);
4754 __ Blt(TMP, AT, label);
4755 break;
4756 case kCondB:
4757 __ Bltu(lhs_high, rhs_high, label);
4758 __ Sltu(TMP, rhs_high, lhs_high);
4759 __ Sltu(AT, lhs_low, rhs_low);
4760 __ Blt(TMP, AT, label);
4761 break;
4762 case kCondAE:
4763 __ Bltu(rhs_high, lhs_high, label);
4764 __ Sltu(TMP, lhs_high, rhs_high);
4765 __ Sltu(AT, lhs_low, rhs_low);
4766 __ Or(TMP, TMP, AT);
4767 __ Beqz(TMP, label);
4768 break;
4769 case kCondBE:
4770 __ Bltu(lhs_high, rhs_high, label);
4771 __ Sltu(TMP, rhs_high, lhs_high);
4772 __ Sltu(AT, rhs_low, lhs_low);
4773 __ Or(TMP, TMP, AT);
4774 __ Beqz(TMP, label);
4775 break;
4776 case kCondA:
4777 __ Bltu(rhs_high, lhs_high, label);
4778 __ Sltu(TMP, lhs_high, rhs_high);
4779 __ Sltu(AT, rhs_low, lhs_low);
4780 __ Blt(TMP, AT, label);
4781 break;
4782 }
4783 }
4784}
4785
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004786void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4787 bool gt_bias,
4788 Primitive::Type type,
4789 LocationSummary* locations) {
4790 Register dst = locations->Out().AsRegister<Register>();
4791 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4792 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4793 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4794 if (type == Primitive::kPrimFloat) {
4795 if (isR6) {
4796 switch (cond) {
4797 case kCondEQ:
4798 __ CmpEqS(FTMP, lhs, rhs);
4799 __ Mfc1(dst, FTMP);
4800 __ Andi(dst, dst, 1);
4801 break;
4802 case kCondNE:
4803 __ CmpEqS(FTMP, lhs, rhs);
4804 __ Mfc1(dst, FTMP);
4805 __ Addiu(dst, dst, 1);
4806 break;
4807 case kCondLT:
4808 if (gt_bias) {
4809 __ CmpLtS(FTMP, lhs, rhs);
4810 } else {
4811 __ CmpUltS(FTMP, lhs, rhs);
4812 }
4813 __ Mfc1(dst, FTMP);
4814 __ Andi(dst, dst, 1);
4815 break;
4816 case kCondLE:
4817 if (gt_bias) {
4818 __ CmpLeS(FTMP, lhs, rhs);
4819 } else {
4820 __ CmpUleS(FTMP, lhs, rhs);
4821 }
4822 __ Mfc1(dst, FTMP);
4823 __ Andi(dst, dst, 1);
4824 break;
4825 case kCondGT:
4826 if (gt_bias) {
4827 __ CmpUltS(FTMP, rhs, lhs);
4828 } else {
4829 __ CmpLtS(FTMP, rhs, lhs);
4830 }
4831 __ Mfc1(dst, FTMP);
4832 __ Andi(dst, dst, 1);
4833 break;
4834 case kCondGE:
4835 if (gt_bias) {
4836 __ CmpUleS(FTMP, rhs, lhs);
4837 } else {
4838 __ CmpLeS(FTMP, rhs, lhs);
4839 }
4840 __ Mfc1(dst, FTMP);
4841 __ Andi(dst, dst, 1);
4842 break;
4843 default:
4844 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4845 UNREACHABLE();
4846 }
4847 } else {
4848 switch (cond) {
4849 case kCondEQ:
4850 __ CeqS(0, lhs, rhs);
4851 __ LoadConst32(dst, 1);
4852 __ Movf(dst, ZERO, 0);
4853 break;
4854 case kCondNE:
4855 __ CeqS(0, lhs, rhs);
4856 __ LoadConst32(dst, 1);
4857 __ Movt(dst, ZERO, 0);
4858 break;
4859 case kCondLT:
4860 if (gt_bias) {
4861 __ ColtS(0, lhs, rhs);
4862 } else {
4863 __ CultS(0, lhs, rhs);
4864 }
4865 __ LoadConst32(dst, 1);
4866 __ Movf(dst, ZERO, 0);
4867 break;
4868 case kCondLE:
4869 if (gt_bias) {
4870 __ ColeS(0, lhs, rhs);
4871 } else {
4872 __ CuleS(0, lhs, rhs);
4873 }
4874 __ LoadConst32(dst, 1);
4875 __ Movf(dst, ZERO, 0);
4876 break;
4877 case kCondGT:
4878 if (gt_bias) {
4879 __ CultS(0, rhs, lhs);
4880 } else {
4881 __ ColtS(0, rhs, lhs);
4882 }
4883 __ LoadConst32(dst, 1);
4884 __ Movf(dst, ZERO, 0);
4885 break;
4886 case kCondGE:
4887 if (gt_bias) {
4888 __ CuleS(0, rhs, lhs);
4889 } else {
4890 __ ColeS(0, rhs, lhs);
4891 }
4892 __ LoadConst32(dst, 1);
4893 __ Movf(dst, ZERO, 0);
4894 break;
4895 default:
4896 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4897 UNREACHABLE();
4898 }
4899 }
4900 } else {
4901 DCHECK_EQ(type, Primitive::kPrimDouble);
4902 if (isR6) {
4903 switch (cond) {
4904 case kCondEQ:
4905 __ CmpEqD(FTMP, lhs, rhs);
4906 __ Mfc1(dst, FTMP);
4907 __ Andi(dst, dst, 1);
4908 break;
4909 case kCondNE:
4910 __ CmpEqD(FTMP, lhs, rhs);
4911 __ Mfc1(dst, FTMP);
4912 __ Addiu(dst, dst, 1);
4913 break;
4914 case kCondLT:
4915 if (gt_bias) {
4916 __ CmpLtD(FTMP, lhs, rhs);
4917 } else {
4918 __ CmpUltD(FTMP, lhs, rhs);
4919 }
4920 __ Mfc1(dst, FTMP);
4921 __ Andi(dst, dst, 1);
4922 break;
4923 case kCondLE:
4924 if (gt_bias) {
4925 __ CmpLeD(FTMP, lhs, rhs);
4926 } else {
4927 __ CmpUleD(FTMP, lhs, rhs);
4928 }
4929 __ Mfc1(dst, FTMP);
4930 __ Andi(dst, dst, 1);
4931 break;
4932 case kCondGT:
4933 if (gt_bias) {
4934 __ CmpUltD(FTMP, rhs, lhs);
4935 } else {
4936 __ CmpLtD(FTMP, rhs, lhs);
4937 }
4938 __ Mfc1(dst, FTMP);
4939 __ Andi(dst, dst, 1);
4940 break;
4941 case kCondGE:
4942 if (gt_bias) {
4943 __ CmpUleD(FTMP, rhs, lhs);
4944 } else {
4945 __ CmpLeD(FTMP, rhs, lhs);
4946 }
4947 __ Mfc1(dst, FTMP);
4948 __ Andi(dst, dst, 1);
4949 break;
4950 default:
4951 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4952 UNREACHABLE();
4953 }
4954 } else {
4955 switch (cond) {
4956 case kCondEQ:
4957 __ CeqD(0, lhs, rhs);
4958 __ LoadConst32(dst, 1);
4959 __ Movf(dst, ZERO, 0);
4960 break;
4961 case kCondNE:
4962 __ CeqD(0, lhs, rhs);
4963 __ LoadConst32(dst, 1);
4964 __ Movt(dst, ZERO, 0);
4965 break;
4966 case kCondLT:
4967 if (gt_bias) {
4968 __ ColtD(0, lhs, rhs);
4969 } else {
4970 __ CultD(0, lhs, rhs);
4971 }
4972 __ LoadConst32(dst, 1);
4973 __ Movf(dst, ZERO, 0);
4974 break;
4975 case kCondLE:
4976 if (gt_bias) {
4977 __ ColeD(0, lhs, rhs);
4978 } else {
4979 __ CuleD(0, lhs, rhs);
4980 }
4981 __ LoadConst32(dst, 1);
4982 __ Movf(dst, ZERO, 0);
4983 break;
4984 case kCondGT:
4985 if (gt_bias) {
4986 __ CultD(0, rhs, lhs);
4987 } else {
4988 __ ColtD(0, rhs, lhs);
4989 }
4990 __ LoadConst32(dst, 1);
4991 __ Movf(dst, ZERO, 0);
4992 break;
4993 case kCondGE:
4994 if (gt_bias) {
4995 __ CuleD(0, rhs, lhs);
4996 } else {
4997 __ ColeD(0, rhs, lhs);
4998 }
4999 __ LoadConst32(dst, 1);
5000 __ Movf(dst, ZERO, 0);
5001 break;
5002 default:
5003 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
5004 UNREACHABLE();
5005 }
5006 }
5007 }
5008}
5009
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005010bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
5011 bool gt_bias,
5012 Primitive::Type type,
5013 LocationSummary* input_locations,
5014 int cc) {
5015 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5016 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5017 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
5018 if (type == Primitive::kPrimFloat) {
5019 switch (cond) {
5020 case kCondEQ:
5021 __ CeqS(cc, lhs, rhs);
5022 return false;
5023 case kCondNE:
5024 __ CeqS(cc, lhs, rhs);
5025 return true;
5026 case kCondLT:
5027 if (gt_bias) {
5028 __ ColtS(cc, lhs, rhs);
5029 } else {
5030 __ CultS(cc, lhs, rhs);
5031 }
5032 return false;
5033 case kCondLE:
5034 if (gt_bias) {
5035 __ ColeS(cc, lhs, rhs);
5036 } else {
5037 __ CuleS(cc, lhs, rhs);
5038 }
5039 return false;
5040 case kCondGT:
5041 if (gt_bias) {
5042 __ CultS(cc, rhs, lhs);
5043 } else {
5044 __ ColtS(cc, rhs, lhs);
5045 }
5046 return false;
5047 case kCondGE:
5048 if (gt_bias) {
5049 __ CuleS(cc, rhs, lhs);
5050 } else {
5051 __ ColeS(cc, rhs, lhs);
5052 }
5053 return false;
5054 default:
5055 LOG(FATAL) << "Unexpected non-floating-point condition";
5056 UNREACHABLE();
5057 }
5058 } else {
5059 DCHECK_EQ(type, Primitive::kPrimDouble);
5060 switch (cond) {
5061 case kCondEQ:
5062 __ CeqD(cc, lhs, rhs);
5063 return false;
5064 case kCondNE:
5065 __ CeqD(cc, lhs, rhs);
5066 return true;
5067 case kCondLT:
5068 if (gt_bias) {
5069 __ ColtD(cc, lhs, rhs);
5070 } else {
5071 __ CultD(cc, lhs, rhs);
5072 }
5073 return false;
5074 case kCondLE:
5075 if (gt_bias) {
5076 __ ColeD(cc, lhs, rhs);
5077 } else {
5078 __ CuleD(cc, lhs, rhs);
5079 }
5080 return false;
5081 case kCondGT:
5082 if (gt_bias) {
5083 __ CultD(cc, rhs, lhs);
5084 } else {
5085 __ ColtD(cc, rhs, lhs);
5086 }
5087 return false;
5088 case kCondGE:
5089 if (gt_bias) {
5090 __ CuleD(cc, rhs, lhs);
5091 } else {
5092 __ ColeD(cc, rhs, lhs);
5093 }
5094 return false;
5095 default:
5096 LOG(FATAL) << "Unexpected non-floating-point condition";
5097 UNREACHABLE();
5098 }
5099 }
5100}
5101
5102bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
5103 bool gt_bias,
5104 Primitive::Type type,
5105 LocationSummary* input_locations,
5106 FRegister dst) {
5107 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
5108 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
5109 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
5110 if (type == Primitive::kPrimFloat) {
5111 switch (cond) {
5112 case kCondEQ:
5113 __ CmpEqS(dst, lhs, rhs);
5114 return false;
5115 case kCondNE:
5116 __ CmpEqS(dst, lhs, rhs);
5117 return true;
5118 case kCondLT:
5119 if (gt_bias) {
5120 __ CmpLtS(dst, lhs, rhs);
5121 } else {
5122 __ CmpUltS(dst, lhs, rhs);
5123 }
5124 return false;
5125 case kCondLE:
5126 if (gt_bias) {
5127 __ CmpLeS(dst, lhs, rhs);
5128 } else {
5129 __ CmpUleS(dst, lhs, rhs);
5130 }
5131 return false;
5132 case kCondGT:
5133 if (gt_bias) {
5134 __ CmpUltS(dst, rhs, lhs);
5135 } else {
5136 __ CmpLtS(dst, rhs, lhs);
5137 }
5138 return false;
5139 case kCondGE:
5140 if (gt_bias) {
5141 __ CmpUleS(dst, rhs, lhs);
5142 } else {
5143 __ CmpLeS(dst, rhs, lhs);
5144 }
5145 return false;
5146 default:
5147 LOG(FATAL) << "Unexpected non-floating-point condition";
5148 UNREACHABLE();
5149 }
5150 } else {
5151 DCHECK_EQ(type, Primitive::kPrimDouble);
5152 switch (cond) {
5153 case kCondEQ:
5154 __ CmpEqD(dst, lhs, rhs);
5155 return false;
5156 case kCondNE:
5157 __ CmpEqD(dst, lhs, rhs);
5158 return true;
5159 case kCondLT:
5160 if (gt_bias) {
5161 __ CmpLtD(dst, lhs, rhs);
5162 } else {
5163 __ CmpUltD(dst, lhs, rhs);
5164 }
5165 return false;
5166 case kCondLE:
5167 if (gt_bias) {
5168 __ CmpLeD(dst, lhs, rhs);
5169 } else {
5170 __ CmpUleD(dst, lhs, rhs);
5171 }
5172 return false;
5173 case kCondGT:
5174 if (gt_bias) {
5175 __ CmpUltD(dst, rhs, lhs);
5176 } else {
5177 __ CmpLtD(dst, rhs, lhs);
5178 }
5179 return false;
5180 case kCondGE:
5181 if (gt_bias) {
5182 __ CmpUleD(dst, rhs, lhs);
5183 } else {
5184 __ CmpLeD(dst, rhs, lhs);
5185 }
5186 return false;
5187 default:
5188 LOG(FATAL) << "Unexpected non-floating-point condition";
5189 UNREACHABLE();
5190 }
5191 }
5192}
5193
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005194void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5195 bool gt_bias,
5196 Primitive::Type type,
5197 LocationSummary* locations,
5198 MipsLabel* label) {
5199 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5200 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5201 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5202 if (type == Primitive::kPrimFloat) {
5203 if (isR6) {
5204 switch (cond) {
5205 case kCondEQ:
5206 __ CmpEqS(FTMP, lhs, rhs);
5207 __ Bc1nez(FTMP, label);
5208 break;
5209 case kCondNE:
5210 __ CmpEqS(FTMP, lhs, rhs);
5211 __ Bc1eqz(FTMP, label);
5212 break;
5213 case kCondLT:
5214 if (gt_bias) {
5215 __ CmpLtS(FTMP, lhs, rhs);
5216 } else {
5217 __ CmpUltS(FTMP, lhs, rhs);
5218 }
5219 __ Bc1nez(FTMP, label);
5220 break;
5221 case kCondLE:
5222 if (gt_bias) {
5223 __ CmpLeS(FTMP, lhs, rhs);
5224 } else {
5225 __ CmpUleS(FTMP, lhs, rhs);
5226 }
5227 __ Bc1nez(FTMP, label);
5228 break;
5229 case kCondGT:
5230 if (gt_bias) {
5231 __ CmpUltS(FTMP, rhs, lhs);
5232 } else {
5233 __ CmpLtS(FTMP, rhs, lhs);
5234 }
5235 __ Bc1nez(FTMP, label);
5236 break;
5237 case kCondGE:
5238 if (gt_bias) {
5239 __ CmpUleS(FTMP, rhs, lhs);
5240 } else {
5241 __ CmpLeS(FTMP, rhs, lhs);
5242 }
5243 __ Bc1nez(FTMP, label);
5244 break;
5245 default:
5246 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005247 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005248 }
5249 } else {
5250 switch (cond) {
5251 case kCondEQ:
5252 __ CeqS(0, lhs, rhs);
5253 __ Bc1t(0, label);
5254 break;
5255 case kCondNE:
5256 __ CeqS(0, lhs, rhs);
5257 __ Bc1f(0, label);
5258 break;
5259 case kCondLT:
5260 if (gt_bias) {
5261 __ ColtS(0, lhs, rhs);
5262 } else {
5263 __ CultS(0, lhs, rhs);
5264 }
5265 __ Bc1t(0, label);
5266 break;
5267 case kCondLE:
5268 if (gt_bias) {
5269 __ ColeS(0, lhs, rhs);
5270 } else {
5271 __ CuleS(0, lhs, rhs);
5272 }
5273 __ Bc1t(0, label);
5274 break;
5275 case kCondGT:
5276 if (gt_bias) {
5277 __ CultS(0, rhs, lhs);
5278 } else {
5279 __ ColtS(0, rhs, lhs);
5280 }
5281 __ Bc1t(0, label);
5282 break;
5283 case kCondGE:
5284 if (gt_bias) {
5285 __ CuleS(0, rhs, lhs);
5286 } else {
5287 __ ColeS(0, rhs, lhs);
5288 }
5289 __ Bc1t(0, label);
5290 break;
5291 default:
5292 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005293 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005294 }
5295 }
5296 } else {
5297 DCHECK_EQ(type, Primitive::kPrimDouble);
5298 if (isR6) {
5299 switch (cond) {
5300 case kCondEQ:
5301 __ CmpEqD(FTMP, lhs, rhs);
5302 __ Bc1nez(FTMP, label);
5303 break;
5304 case kCondNE:
5305 __ CmpEqD(FTMP, lhs, rhs);
5306 __ Bc1eqz(FTMP, label);
5307 break;
5308 case kCondLT:
5309 if (gt_bias) {
5310 __ CmpLtD(FTMP, lhs, rhs);
5311 } else {
5312 __ CmpUltD(FTMP, lhs, rhs);
5313 }
5314 __ Bc1nez(FTMP, label);
5315 break;
5316 case kCondLE:
5317 if (gt_bias) {
5318 __ CmpLeD(FTMP, lhs, rhs);
5319 } else {
5320 __ CmpUleD(FTMP, lhs, rhs);
5321 }
5322 __ Bc1nez(FTMP, label);
5323 break;
5324 case kCondGT:
5325 if (gt_bias) {
5326 __ CmpUltD(FTMP, rhs, lhs);
5327 } else {
5328 __ CmpLtD(FTMP, rhs, lhs);
5329 }
5330 __ Bc1nez(FTMP, label);
5331 break;
5332 case kCondGE:
5333 if (gt_bias) {
5334 __ CmpUleD(FTMP, rhs, lhs);
5335 } else {
5336 __ CmpLeD(FTMP, rhs, lhs);
5337 }
5338 __ Bc1nez(FTMP, label);
5339 break;
5340 default:
5341 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005342 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005343 }
5344 } else {
5345 switch (cond) {
5346 case kCondEQ:
5347 __ CeqD(0, lhs, rhs);
5348 __ Bc1t(0, label);
5349 break;
5350 case kCondNE:
5351 __ CeqD(0, lhs, rhs);
5352 __ Bc1f(0, label);
5353 break;
5354 case kCondLT:
5355 if (gt_bias) {
5356 __ ColtD(0, lhs, rhs);
5357 } else {
5358 __ CultD(0, lhs, rhs);
5359 }
5360 __ Bc1t(0, label);
5361 break;
5362 case kCondLE:
5363 if (gt_bias) {
5364 __ ColeD(0, lhs, rhs);
5365 } else {
5366 __ CuleD(0, lhs, rhs);
5367 }
5368 __ Bc1t(0, label);
5369 break;
5370 case kCondGT:
5371 if (gt_bias) {
5372 __ CultD(0, rhs, lhs);
5373 } else {
5374 __ ColtD(0, rhs, lhs);
5375 }
5376 __ Bc1t(0, label);
5377 break;
5378 case kCondGE:
5379 if (gt_bias) {
5380 __ CuleD(0, rhs, lhs);
5381 } else {
5382 __ ColeD(0, rhs, lhs);
5383 }
5384 __ Bc1t(0, label);
5385 break;
5386 default:
5387 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005388 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005389 }
5390 }
5391 }
5392}
5393
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005394void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005395 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005396 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005397 MipsLabel* false_target) {
5398 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005399
David Brazdil0debae72015-11-12 18:37:00 +00005400 if (true_target == nullptr && false_target == nullptr) {
5401 // Nothing to do. The code always falls through.
5402 return;
5403 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005404 // Constant condition, statically compared against "true" (integer value 1).
5405 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005406 if (true_target != nullptr) {
5407 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005408 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005409 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005410 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005411 if (false_target != nullptr) {
5412 __ B(false_target);
5413 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005414 }
David Brazdil0debae72015-11-12 18:37:00 +00005415 return;
5416 }
5417
5418 // The following code generates these patterns:
5419 // (1) true_target == nullptr && false_target != nullptr
5420 // - opposite condition true => branch to false_target
5421 // (2) true_target != nullptr && false_target == nullptr
5422 // - condition true => branch to true_target
5423 // (3) true_target != nullptr && false_target != nullptr
5424 // - condition true => branch to true_target
5425 // - branch to false_target
5426 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005427 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005428 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005429 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005430 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005431 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5432 } else {
5433 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5434 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005435 } else {
5436 // The condition instruction has not been materialized, use its inputs as
5437 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005438 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005439 Primitive::Type type = condition->InputAt(0)->GetType();
5440 LocationSummary* locations = cond->GetLocations();
5441 IfCondition if_cond = condition->GetCondition();
5442 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005443
David Brazdil0debae72015-11-12 18:37:00 +00005444 if (true_target == nullptr) {
5445 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005446 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005447 }
5448
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005449 switch (type) {
5450 default:
5451 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5452 break;
5453 case Primitive::kPrimLong:
5454 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5455 break;
5456 case Primitive::kPrimFloat:
5457 case Primitive::kPrimDouble:
5458 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5459 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005460 }
5461 }
David Brazdil0debae72015-11-12 18:37:00 +00005462
5463 // If neither branch falls through (case 3), the conditional branch to `true_target`
5464 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5465 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005466 __ B(false_target);
5467 }
5468}
5469
5470void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5471 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005472 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005473 locations->SetInAt(0, Location::RequiresRegister());
5474 }
5475}
5476
5477void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005478 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5479 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5480 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5481 nullptr : codegen_->GetLabelOf(true_successor);
5482 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5483 nullptr : codegen_->GetLabelOf(false_successor);
5484 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005485}
5486
5487void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5488 LocationSummary* locations = new (GetGraph()->GetArena())
5489 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005490 InvokeRuntimeCallingConvention calling_convention;
5491 RegisterSet caller_saves = RegisterSet::Empty();
5492 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5493 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005494 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005495 locations->SetInAt(0, Location::RequiresRegister());
5496 }
5497}
5498
5499void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005500 SlowPathCodeMIPS* slow_path =
5501 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005502 GenerateTestAndBranch(deoptimize,
5503 /* condition_input_index */ 0,
5504 slow_path->GetEntryLabel(),
5505 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005506}
5507
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005508// This function returns true if a conditional move can be generated for HSelect.
5509// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5510// branches and regular moves.
5511//
5512// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5513//
5514// While determining feasibility of a conditional move and setting inputs/outputs
5515// are two distinct tasks, this function does both because they share quite a bit
5516// of common logic.
5517static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5518 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5519 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5520 HCondition* condition = cond->AsCondition();
5521
5522 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5523 Primitive::Type dst_type = select->GetType();
5524
5525 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5526 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5527 bool is_true_value_zero_constant =
5528 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5529 bool is_false_value_zero_constant =
5530 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5531
5532 bool can_move_conditionally = false;
5533 bool use_const_for_false_in = false;
5534 bool use_const_for_true_in = false;
5535
5536 if (!cond->IsConstant()) {
5537 switch (cond_type) {
5538 default:
5539 switch (dst_type) {
5540 default:
5541 // Moving int on int condition.
5542 if (is_r6) {
5543 if (is_true_value_zero_constant) {
5544 // seleqz out_reg, false_reg, cond_reg
5545 can_move_conditionally = true;
5546 use_const_for_true_in = true;
5547 } else if (is_false_value_zero_constant) {
5548 // selnez out_reg, true_reg, cond_reg
5549 can_move_conditionally = true;
5550 use_const_for_false_in = true;
5551 } else if (materialized) {
5552 // Not materializing unmaterialized int conditions
5553 // to keep the instruction count low.
5554 // selnez AT, true_reg, cond_reg
5555 // seleqz TMP, false_reg, cond_reg
5556 // or out_reg, AT, TMP
5557 can_move_conditionally = true;
5558 }
5559 } else {
5560 // movn out_reg, true_reg/ZERO, cond_reg
5561 can_move_conditionally = true;
5562 use_const_for_true_in = is_true_value_zero_constant;
5563 }
5564 break;
5565 case Primitive::kPrimLong:
5566 // Moving long on int condition.
5567 if (is_r6) {
5568 if (is_true_value_zero_constant) {
5569 // seleqz out_reg_lo, false_reg_lo, cond_reg
5570 // seleqz out_reg_hi, false_reg_hi, cond_reg
5571 can_move_conditionally = true;
5572 use_const_for_true_in = true;
5573 } else if (is_false_value_zero_constant) {
5574 // selnez out_reg_lo, true_reg_lo, cond_reg
5575 // selnez out_reg_hi, true_reg_hi, cond_reg
5576 can_move_conditionally = true;
5577 use_const_for_false_in = true;
5578 }
5579 // Other long conditional moves would generate 6+ instructions,
5580 // which is too many.
5581 } else {
5582 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5583 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5584 can_move_conditionally = true;
5585 use_const_for_true_in = is_true_value_zero_constant;
5586 }
5587 break;
5588 case Primitive::kPrimFloat:
5589 case Primitive::kPrimDouble:
5590 // Moving float/double on int condition.
5591 if (is_r6) {
5592 if (materialized) {
5593 // Not materializing unmaterialized int conditions
5594 // to keep the instruction count low.
5595 can_move_conditionally = true;
5596 if (is_true_value_zero_constant) {
5597 // sltu TMP, ZERO, cond_reg
5598 // mtc1 TMP, temp_cond_reg
5599 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5600 use_const_for_true_in = true;
5601 } else if (is_false_value_zero_constant) {
5602 // sltu TMP, ZERO, cond_reg
5603 // mtc1 TMP, temp_cond_reg
5604 // selnez.fmt out_reg, true_reg, temp_cond_reg
5605 use_const_for_false_in = true;
5606 } else {
5607 // sltu TMP, ZERO, cond_reg
5608 // mtc1 TMP, temp_cond_reg
5609 // sel.fmt temp_cond_reg, false_reg, true_reg
5610 // mov.fmt out_reg, temp_cond_reg
5611 }
5612 }
5613 } else {
5614 // movn.fmt out_reg, true_reg, cond_reg
5615 can_move_conditionally = true;
5616 }
5617 break;
5618 }
5619 break;
5620 case Primitive::kPrimLong:
5621 // We don't materialize long comparison now
5622 // and use conditional branches instead.
5623 break;
5624 case Primitive::kPrimFloat:
5625 case Primitive::kPrimDouble:
5626 switch (dst_type) {
5627 default:
5628 // Moving int on float/double condition.
5629 if (is_r6) {
5630 if (is_true_value_zero_constant) {
5631 // mfc1 TMP, temp_cond_reg
5632 // seleqz out_reg, false_reg, TMP
5633 can_move_conditionally = true;
5634 use_const_for_true_in = true;
5635 } else if (is_false_value_zero_constant) {
5636 // mfc1 TMP, temp_cond_reg
5637 // selnez out_reg, true_reg, TMP
5638 can_move_conditionally = true;
5639 use_const_for_false_in = true;
5640 } else {
5641 // mfc1 TMP, temp_cond_reg
5642 // selnez AT, true_reg, TMP
5643 // seleqz TMP, false_reg, TMP
5644 // or out_reg, AT, TMP
5645 can_move_conditionally = true;
5646 }
5647 } else {
5648 // movt out_reg, true_reg/ZERO, cc
5649 can_move_conditionally = true;
5650 use_const_for_true_in = is_true_value_zero_constant;
5651 }
5652 break;
5653 case Primitive::kPrimLong:
5654 // Moving long on float/double condition.
5655 if (is_r6) {
5656 if (is_true_value_zero_constant) {
5657 // mfc1 TMP, temp_cond_reg
5658 // seleqz out_reg_lo, false_reg_lo, TMP
5659 // seleqz out_reg_hi, false_reg_hi, TMP
5660 can_move_conditionally = true;
5661 use_const_for_true_in = true;
5662 } else if (is_false_value_zero_constant) {
5663 // mfc1 TMP, temp_cond_reg
5664 // selnez out_reg_lo, true_reg_lo, TMP
5665 // selnez out_reg_hi, true_reg_hi, TMP
5666 can_move_conditionally = true;
5667 use_const_for_false_in = true;
5668 }
5669 // Other long conditional moves would generate 6+ instructions,
5670 // which is too many.
5671 } else {
5672 // movt out_reg_lo, true_reg_lo/ZERO, cc
5673 // movt out_reg_hi, true_reg_hi/ZERO, cc
5674 can_move_conditionally = true;
5675 use_const_for_true_in = is_true_value_zero_constant;
5676 }
5677 break;
5678 case Primitive::kPrimFloat:
5679 case Primitive::kPrimDouble:
5680 // Moving float/double on float/double condition.
5681 if (is_r6) {
5682 can_move_conditionally = true;
5683 if (is_true_value_zero_constant) {
5684 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5685 use_const_for_true_in = true;
5686 } else if (is_false_value_zero_constant) {
5687 // selnez.fmt out_reg, true_reg, temp_cond_reg
5688 use_const_for_false_in = true;
5689 } else {
5690 // sel.fmt temp_cond_reg, false_reg, true_reg
5691 // mov.fmt out_reg, temp_cond_reg
5692 }
5693 } else {
5694 // movt.fmt out_reg, true_reg, cc
5695 can_move_conditionally = true;
5696 }
5697 break;
5698 }
5699 break;
5700 }
5701 }
5702
5703 if (can_move_conditionally) {
5704 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5705 } else {
5706 DCHECK(!use_const_for_false_in);
5707 DCHECK(!use_const_for_true_in);
5708 }
5709
5710 if (locations_to_set != nullptr) {
5711 if (use_const_for_false_in) {
5712 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5713 } else {
5714 locations_to_set->SetInAt(0,
5715 Primitive::IsFloatingPointType(dst_type)
5716 ? Location::RequiresFpuRegister()
5717 : Location::RequiresRegister());
5718 }
5719 if (use_const_for_true_in) {
5720 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5721 } else {
5722 locations_to_set->SetInAt(1,
5723 Primitive::IsFloatingPointType(dst_type)
5724 ? Location::RequiresFpuRegister()
5725 : Location::RequiresRegister());
5726 }
5727 if (materialized) {
5728 locations_to_set->SetInAt(2, Location::RequiresRegister());
5729 }
5730 // On R6 we don't require the output to be the same as the
5731 // first input for conditional moves unlike on R2.
5732 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5733 if (is_out_same_as_first_in) {
5734 locations_to_set->SetOut(Location::SameAsFirstInput());
5735 } else {
5736 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5737 ? Location::RequiresFpuRegister()
5738 : Location::RequiresRegister());
5739 }
5740 }
5741
5742 return can_move_conditionally;
5743}
5744
5745void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5746 LocationSummary* locations = select->GetLocations();
5747 Location dst = locations->Out();
5748 Location src = locations->InAt(1);
5749 Register src_reg = ZERO;
5750 Register src_reg_high = ZERO;
5751 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5752 Register cond_reg = TMP;
5753 int cond_cc = 0;
5754 Primitive::Type cond_type = Primitive::kPrimInt;
5755 bool cond_inverted = false;
5756 Primitive::Type dst_type = select->GetType();
5757
5758 if (IsBooleanValueOrMaterializedCondition(cond)) {
5759 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5760 } else {
5761 HCondition* condition = cond->AsCondition();
5762 LocationSummary* cond_locations = cond->GetLocations();
5763 IfCondition if_cond = condition->GetCondition();
5764 cond_type = condition->InputAt(0)->GetType();
5765 switch (cond_type) {
5766 default:
5767 DCHECK_NE(cond_type, Primitive::kPrimLong);
5768 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5769 break;
5770 case Primitive::kPrimFloat:
5771 case Primitive::kPrimDouble:
5772 cond_inverted = MaterializeFpCompareR2(if_cond,
5773 condition->IsGtBias(),
5774 cond_type,
5775 cond_locations,
5776 cond_cc);
5777 break;
5778 }
5779 }
5780
5781 DCHECK(dst.Equals(locations->InAt(0)));
5782 if (src.IsRegister()) {
5783 src_reg = src.AsRegister<Register>();
5784 } else if (src.IsRegisterPair()) {
5785 src_reg = src.AsRegisterPairLow<Register>();
5786 src_reg_high = src.AsRegisterPairHigh<Register>();
5787 } else if (src.IsConstant()) {
5788 DCHECK(src.GetConstant()->IsZeroBitPattern());
5789 }
5790
5791 switch (cond_type) {
5792 default:
5793 switch (dst_type) {
5794 default:
5795 if (cond_inverted) {
5796 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5797 } else {
5798 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5799 }
5800 break;
5801 case Primitive::kPrimLong:
5802 if (cond_inverted) {
5803 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5804 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5805 } else {
5806 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5807 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5808 }
5809 break;
5810 case Primitive::kPrimFloat:
5811 if (cond_inverted) {
5812 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5813 } else {
5814 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5815 }
5816 break;
5817 case Primitive::kPrimDouble:
5818 if (cond_inverted) {
5819 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5820 } else {
5821 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5822 }
5823 break;
5824 }
5825 break;
5826 case Primitive::kPrimLong:
5827 LOG(FATAL) << "Unreachable";
5828 UNREACHABLE();
5829 case Primitive::kPrimFloat:
5830 case Primitive::kPrimDouble:
5831 switch (dst_type) {
5832 default:
5833 if (cond_inverted) {
5834 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5835 } else {
5836 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5837 }
5838 break;
5839 case Primitive::kPrimLong:
5840 if (cond_inverted) {
5841 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5842 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5843 } else {
5844 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5845 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5846 }
5847 break;
5848 case Primitive::kPrimFloat:
5849 if (cond_inverted) {
5850 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5851 } else {
5852 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5853 }
5854 break;
5855 case Primitive::kPrimDouble:
5856 if (cond_inverted) {
5857 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5858 } else {
5859 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5860 }
5861 break;
5862 }
5863 break;
5864 }
5865}
5866
5867void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5868 LocationSummary* locations = select->GetLocations();
5869 Location dst = locations->Out();
5870 Location false_src = locations->InAt(0);
5871 Location true_src = locations->InAt(1);
5872 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5873 Register cond_reg = TMP;
5874 FRegister fcond_reg = FTMP;
5875 Primitive::Type cond_type = Primitive::kPrimInt;
5876 bool cond_inverted = false;
5877 Primitive::Type dst_type = select->GetType();
5878
5879 if (IsBooleanValueOrMaterializedCondition(cond)) {
5880 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5881 } else {
5882 HCondition* condition = cond->AsCondition();
5883 LocationSummary* cond_locations = cond->GetLocations();
5884 IfCondition if_cond = condition->GetCondition();
5885 cond_type = condition->InputAt(0)->GetType();
5886 switch (cond_type) {
5887 default:
5888 DCHECK_NE(cond_type, Primitive::kPrimLong);
5889 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5890 break;
5891 case Primitive::kPrimFloat:
5892 case Primitive::kPrimDouble:
5893 cond_inverted = MaterializeFpCompareR6(if_cond,
5894 condition->IsGtBias(),
5895 cond_type,
5896 cond_locations,
5897 fcond_reg);
5898 break;
5899 }
5900 }
5901
5902 if (true_src.IsConstant()) {
5903 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5904 }
5905 if (false_src.IsConstant()) {
5906 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5907 }
5908
5909 switch (dst_type) {
5910 default:
5911 if (Primitive::IsFloatingPointType(cond_type)) {
5912 __ Mfc1(cond_reg, fcond_reg);
5913 }
5914 if (true_src.IsConstant()) {
5915 if (cond_inverted) {
5916 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5917 } else {
5918 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5919 }
5920 } else if (false_src.IsConstant()) {
5921 if (cond_inverted) {
5922 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5923 } else {
5924 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5925 }
5926 } else {
5927 DCHECK_NE(cond_reg, AT);
5928 if (cond_inverted) {
5929 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5930 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5931 } else {
5932 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5933 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5934 }
5935 __ Or(dst.AsRegister<Register>(), AT, TMP);
5936 }
5937 break;
5938 case Primitive::kPrimLong: {
5939 if (Primitive::IsFloatingPointType(cond_type)) {
5940 __ Mfc1(cond_reg, fcond_reg);
5941 }
5942 Register dst_lo = dst.AsRegisterPairLow<Register>();
5943 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5944 if (true_src.IsConstant()) {
5945 Register src_lo = false_src.AsRegisterPairLow<Register>();
5946 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5947 if (cond_inverted) {
5948 __ Selnez(dst_lo, src_lo, cond_reg);
5949 __ Selnez(dst_hi, src_hi, cond_reg);
5950 } else {
5951 __ Seleqz(dst_lo, src_lo, cond_reg);
5952 __ Seleqz(dst_hi, src_hi, cond_reg);
5953 }
5954 } else {
5955 DCHECK(false_src.IsConstant());
5956 Register src_lo = true_src.AsRegisterPairLow<Register>();
5957 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5958 if (cond_inverted) {
5959 __ Seleqz(dst_lo, src_lo, cond_reg);
5960 __ Seleqz(dst_hi, src_hi, cond_reg);
5961 } else {
5962 __ Selnez(dst_lo, src_lo, cond_reg);
5963 __ Selnez(dst_hi, src_hi, cond_reg);
5964 }
5965 }
5966 break;
5967 }
5968 case Primitive::kPrimFloat: {
5969 if (!Primitive::IsFloatingPointType(cond_type)) {
5970 // sel*.fmt tests bit 0 of the condition register, account for that.
5971 __ Sltu(TMP, ZERO, cond_reg);
5972 __ Mtc1(TMP, fcond_reg);
5973 }
5974 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5975 if (true_src.IsConstant()) {
5976 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5977 if (cond_inverted) {
5978 __ SelnezS(dst_reg, src_reg, fcond_reg);
5979 } else {
5980 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5981 }
5982 } else if (false_src.IsConstant()) {
5983 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5984 if (cond_inverted) {
5985 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5986 } else {
5987 __ SelnezS(dst_reg, src_reg, fcond_reg);
5988 }
5989 } else {
5990 if (cond_inverted) {
5991 __ SelS(fcond_reg,
5992 true_src.AsFpuRegister<FRegister>(),
5993 false_src.AsFpuRegister<FRegister>());
5994 } else {
5995 __ SelS(fcond_reg,
5996 false_src.AsFpuRegister<FRegister>(),
5997 true_src.AsFpuRegister<FRegister>());
5998 }
5999 __ MovS(dst_reg, fcond_reg);
6000 }
6001 break;
6002 }
6003 case Primitive::kPrimDouble: {
6004 if (!Primitive::IsFloatingPointType(cond_type)) {
6005 // sel*.fmt tests bit 0 of the condition register, account for that.
6006 __ Sltu(TMP, ZERO, cond_reg);
6007 __ Mtc1(TMP, fcond_reg);
6008 }
6009 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
6010 if (true_src.IsConstant()) {
6011 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
6012 if (cond_inverted) {
6013 __ SelnezD(dst_reg, src_reg, fcond_reg);
6014 } else {
6015 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6016 }
6017 } else if (false_src.IsConstant()) {
6018 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
6019 if (cond_inverted) {
6020 __ SeleqzD(dst_reg, src_reg, fcond_reg);
6021 } else {
6022 __ SelnezD(dst_reg, src_reg, fcond_reg);
6023 }
6024 } else {
6025 if (cond_inverted) {
6026 __ SelD(fcond_reg,
6027 true_src.AsFpuRegister<FRegister>(),
6028 false_src.AsFpuRegister<FRegister>());
6029 } else {
6030 __ SelD(fcond_reg,
6031 false_src.AsFpuRegister<FRegister>(),
6032 true_src.AsFpuRegister<FRegister>());
6033 }
6034 __ MovD(dst_reg, fcond_reg);
6035 }
6036 break;
6037 }
6038 }
6039}
6040
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006041void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6042 LocationSummary* locations = new (GetGraph()->GetArena())
6043 LocationSummary(flag, LocationSummary::kNoCall);
6044 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07006045}
6046
Goran Jakovljevicc6418422016-12-05 16:31:55 +01006047void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
6048 __ LoadFromOffset(kLoadWord,
6049 flag->GetLocations()->Out().AsRegister<Register>(),
6050 SP,
6051 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07006052}
6053
David Brazdil74eb1b22015-12-14 11:44:01 +00006054void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
6055 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006056 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00006057}
6058
6059void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07006060 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
6061 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
6062 if (is_r6) {
6063 GenConditionalMoveR6(select);
6064 } else {
6065 GenConditionalMoveR2(select);
6066 }
6067 } else {
6068 LocationSummary* locations = select->GetLocations();
6069 MipsLabel false_target;
6070 GenerateTestAndBranch(select,
6071 /* condition_input_index */ 2,
6072 /* true_target */ nullptr,
6073 &false_target);
6074 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
6075 __ Bind(&false_target);
6076 }
David Brazdil74eb1b22015-12-14 11:44:01 +00006077}
6078
David Srbecky0cf44932015-12-09 14:09:59 +00006079void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
6080 new (GetGraph()->GetArena()) LocationSummary(info);
6081}
6082
David Srbeckyd28f4a02016-03-14 17:14:24 +00006083void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
6084 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00006085}
6086
6087void CodeGeneratorMIPS::GenerateNop() {
6088 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00006089}
6090
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006091void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
6092 Primitive::Type field_type = field_info.GetFieldType();
6093 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6094 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08006095 bool object_field_get_with_read_barrier =
6096 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006097 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08006098 instruction,
6099 generate_volatile
6100 ? LocationSummary::kCallOnMainOnly
6101 : (object_field_get_with_read_barrier
6102 ? LocationSummary::kCallOnSlowPath
6103 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006104
Alexey Frunzec61c0762017-04-10 13:54:23 -07006105 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6106 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6107 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006108 locations->SetInAt(0, Location::RequiresRegister());
6109 if (generate_volatile) {
6110 InvokeRuntimeCallingConvention calling_convention;
6111 // need A0 to hold base + offset
6112 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6113 if (field_type == Primitive::kPrimLong) {
6114 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
6115 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006116 // Use Location::Any() to prevent situations when running out of available fp registers.
6117 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006118 // Need some temp core regs since FP results are returned in core registers
6119 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
6120 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6121 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6122 }
6123 } else {
6124 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6125 locations->SetOut(Location::RequiresFpuRegister());
6126 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006127 // The output overlaps in the case of an object field get with
6128 // read barriers enabled: we do not want the move to overwrite the
6129 // object's location, as we need it to emit the read barrier.
6130 locations->SetOut(Location::RequiresRegister(),
6131 object_field_get_with_read_barrier
6132 ? Location::kOutputOverlap
6133 : Location::kNoOutputOverlap);
6134 }
6135 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6136 // We need a temporary register for the read barrier marking slow
6137 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006138 if (!kBakerReadBarrierThunksEnableForFields) {
6139 locations->AddTemp(Location::RequiresRegister());
6140 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006141 }
6142 }
6143}
6144
6145void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6146 const FieldInfo& field_info,
6147 uint32_t dex_pc) {
6148 Primitive::Type type = field_info.GetFieldType();
6149 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006150 Location obj_loc = locations->InAt(0);
6151 Register obj = obj_loc.AsRegister<Register>();
6152 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006153 LoadOperandType load_type = kLoadUnsignedByte;
6154 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006155 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006156 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006157
6158 switch (type) {
6159 case Primitive::kPrimBoolean:
6160 load_type = kLoadUnsignedByte;
6161 break;
6162 case Primitive::kPrimByte:
6163 load_type = kLoadSignedByte;
6164 break;
6165 case Primitive::kPrimShort:
6166 load_type = kLoadSignedHalfword;
6167 break;
6168 case Primitive::kPrimChar:
6169 load_type = kLoadUnsignedHalfword;
6170 break;
6171 case Primitive::kPrimInt:
6172 case Primitive::kPrimFloat:
6173 case Primitive::kPrimNot:
6174 load_type = kLoadWord;
6175 break;
6176 case Primitive::kPrimLong:
6177 case Primitive::kPrimDouble:
6178 load_type = kLoadDoubleword;
6179 break;
6180 case Primitive::kPrimVoid:
6181 LOG(FATAL) << "Unreachable type " << type;
6182 UNREACHABLE();
6183 }
6184
6185 if (is_volatile && load_type == kLoadDoubleword) {
6186 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006187 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006188 // Do implicit Null check
6189 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6190 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006191 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006192 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
6193 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006194 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006195 if (dst_loc.IsFpuRegister()) {
6196 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006197 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006198 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006199 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006200 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006201 __ StoreToOffset(kStoreWord,
6202 locations->GetTemp(1).AsRegister<Register>(),
6203 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006204 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006205 __ StoreToOffset(kStoreWord,
6206 locations->GetTemp(2).AsRegister<Register>(),
6207 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006208 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006209 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006210 }
6211 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006212 if (type == Primitive::kPrimNot) {
6213 // /* HeapReference<Object> */ dst = *(obj + offset)
6214 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006215 Location temp_loc =
6216 kBakerReadBarrierThunksEnableForFields ? Location::NoLocation() : locations->GetTemp(0);
Alexey Frunze15958152017-02-09 19:08:30 -08006217 // Note that a potential implicit null check is handled in this
6218 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6219 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6220 dst_loc,
6221 obj,
6222 offset,
6223 temp_loc,
6224 /* needs_null_check */ true);
6225 if (is_volatile) {
6226 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6227 }
6228 } else {
6229 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6230 if (is_volatile) {
6231 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6232 }
6233 // If read barriers are enabled, emit read barriers other than
6234 // Baker's using a slow path (and also unpoison the loaded
6235 // reference, if heap poisoning is enabled).
6236 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6237 }
6238 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006239 Register dst;
6240 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08006241 DCHECK(dst_loc.IsRegisterPair());
6242 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006243 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006244 DCHECK(dst_loc.IsRegister());
6245 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006246 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006247 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006248 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006249 DCHECK(dst_loc.IsFpuRegister());
6250 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006251 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006252 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006253 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006254 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006255 }
6256 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006257 }
6258
Alexey Frunze15958152017-02-09 19:08:30 -08006259 // Memory barriers, in the case of references, are handled in the
6260 // previous switch statement.
6261 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006262 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6263 }
6264}
6265
6266void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
6267 Primitive::Type field_type = field_info.GetFieldType();
6268 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6269 bool generate_volatile = field_info.IsVolatile() && is_wide;
6270 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006271 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006272
6273 locations->SetInAt(0, Location::RequiresRegister());
6274 if (generate_volatile) {
6275 InvokeRuntimeCallingConvention calling_convention;
6276 // need A0 to hold base + offset
6277 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6278 if (field_type == Primitive::kPrimLong) {
6279 locations->SetInAt(1, Location::RegisterPairLocation(
6280 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6281 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006282 // Use Location::Any() to prevent situations when running out of available fp registers.
6283 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006284 // Pass FP parameters in core registers.
6285 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6286 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6287 }
6288 } else {
6289 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006290 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006291 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006292 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006293 }
6294 }
6295}
6296
6297void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6298 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006299 uint32_t dex_pc,
6300 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006301 Primitive::Type type = field_info.GetFieldType();
6302 LocationSummary* locations = instruction->GetLocations();
6303 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006304 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006305 StoreOperandType store_type = kStoreByte;
6306 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006307 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006308 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006309 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006310
6311 switch (type) {
6312 case Primitive::kPrimBoolean:
6313 case Primitive::kPrimByte:
6314 store_type = kStoreByte;
6315 break;
6316 case Primitive::kPrimShort:
6317 case Primitive::kPrimChar:
6318 store_type = kStoreHalfword;
6319 break;
6320 case Primitive::kPrimInt:
6321 case Primitive::kPrimFloat:
6322 case Primitive::kPrimNot:
6323 store_type = kStoreWord;
6324 break;
6325 case Primitive::kPrimLong:
6326 case Primitive::kPrimDouble:
6327 store_type = kStoreDoubleword;
6328 break;
6329 case Primitive::kPrimVoid:
6330 LOG(FATAL) << "Unreachable type " << type;
6331 UNREACHABLE();
6332 }
6333
6334 if (is_volatile) {
6335 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6336 }
6337
6338 if (is_volatile && store_type == kStoreDoubleword) {
6339 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006340 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006341 // Do implicit Null check.
6342 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6343 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6344 if (type == Primitive::kPrimDouble) {
6345 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006346 if (value_location.IsFpuRegister()) {
6347 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6348 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006349 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006350 value_location.AsFpuRegister<FRegister>());
6351 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006352 __ LoadFromOffset(kLoadWord,
6353 locations->GetTemp(1).AsRegister<Register>(),
6354 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006355 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006356 __ LoadFromOffset(kLoadWord,
6357 locations->GetTemp(2).AsRegister<Register>(),
6358 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006359 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006360 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006361 DCHECK(value_location.IsConstant());
6362 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6363 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006364 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6365 locations->GetTemp(1).AsRegister<Register>(),
6366 value);
6367 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006368 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006369 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006370 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6371 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006372 if (value_location.IsConstant()) {
6373 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6374 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6375 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006376 Register src;
6377 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006378 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006379 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006380 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006381 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006382 if (kPoisonHeapReferences && needs_write_barrier) {
6383 // Note that in the case where `value` is a null reference,
6384 // we do not enter this block, as a null reference does not
6385 // need poisoning.
6386 DCHECK_EQ(type, Primitive::kPrimNot);
6387 __ PoisonHeapReference(TMP, src);
6388 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6389 } else {
6390 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6391 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006392 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006393 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006394 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006395 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006396 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006397 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006398 }
6399 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006400 }
6401
Alexey Frunzec061de12017-02-14 13:27:23 -08006402 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006403 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006404 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006405 }
6406
6407 if (is_volatile) {
6408 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6409 }
6410}
6411
6412void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6413 HandleFieldGet(instruction, instruction->GetFieldInfo());
6414}
6415
6416void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6417 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6418}
6419
6420void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6421 HandleFieldSet(instruction, instruction->GetFieldInfo());
6422}
6423
6424void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006425 HandleFieldSet(instruction,
6426 instruction->GetFieldInfo(),
6427 instruction->GetDexPc(),
6428 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006429}
6430
Alexey Frunze15958152017-02-09 19:08:30 -08006431void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6432 HInstruction* instruction,
6433 Location out,
6434 uint32_t offset,
6435 Location maybe_temp,
6436 ReadBarrierOption read_barrier_option) {
6437 Register out_reg = out.AsRegister<Register>();
6438 if (read_barrier_option == kWithReadBarrier) {
6439 CHECK(kEmitCompilerReadBarrier);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006440 if (!kUseBakerReadBarrier || !kBakerReadBarrierThunksEnableForFields) {
6441 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6442 }
Alexey Frunze15958152017-02-09 19:08:30 -08006443 if (kUseBakerReadBarrier) {
6444 // Load with fast path based Baker's read barrier.
6445 // /* HeapReference<Object> */ out = *(out + offset)
6446 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6447 out,
6448 out_reg,
6449 offset,
6450 maybe_temp,
6451 /* needs_null_check */ false);
6452 } else {
6453 // Load with slow path based read barrier.
6454 // Save the value of `out` into `maybe_temp` before overwriting it
6455 // in the following move operation, as we will need it for the
6456 // read barrier below.
6457 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6458 // /* HeapReference<Object> */ out = *(out + offset)
6459 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6460 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6461 }
6462 } else {
6463 // Plain load with no read barrier.
6464 // /* HeapReference<Object> */ out = *(out + offset)
6465 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6466 __ MaybeUnpoisonHeapReference(out_reg);
6467 }
6468}
6469
6470void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6471 HInstruction* instruction,
6472 Location out,
6473 Location obj,
6474 uint32_t offset,
6475 Location maybe_temp,
6476 ReadBarrierOption read_barrier_option) {
6477 Register out_reg = out.AsRegister<Register>();
6478 Register obj_reg = obj.AsRegister<Register>();
6479 if (read_barrier_option == kWithReadBarrier) {
6480 CHECK(kEmitCompilerReadBarrier);
6481 if (kUseBakerReadBarrier) {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006482 if (!kBakerReadBarrierThunksEnableForFields) {
6483 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6484 }
Alexey Frunze15958152017-02-09 19:08:30 -08006485 // Load with fast path based Baker's read barrier.
6486 // /* HeapReference<Object> */ out = *(obj + offset)
6487 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6488 out,
6489 obj_reg,
6490 offset,
6491 maybe_temp,
6492 /* needs_null_check */ false);
6493 } else {
6494 // Load with slow path based read barrier.
6495 // /* HeapReference<Object> */ out = *(obj + offset)
6496 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6497 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6498 }
6499 } else {
6500 // Plain load with no read barrier.
6501 // /* HeapReference<Object> */ out = *(obj + offset)
6502 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6503 __ MaybeUnpoisonHeapReference(out_reg);
6504 }
6505}
6506
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006507static inline int GetBakerMarkThunkNumber(Register reg) {
6508 static_assert(BAKER_MARK_INTROSPECTION_REGISTER_COUNT == 21, "Expecting equal");
6509 if (reg >= V0 && reg <= T7) { // 14 consequtive regs.
6510 return reg - V0;
6511 } else if (reg >= S2 && reg <= S7) { // 6 consequtive regs.
6512 return 14 + (reg - S2);
6513 } else if (reg == FP) { // One more.
6514 return 20;
6515 }
6516 LOG(FATAL) << "Unexpected register " << reg;
6517 UNREACHABLE();
6518}
6519
6520static inline int GetBakerMarkFieldArrayThunkDisplacement(Register reg, bool short_offset) {
6521 int num = GetBakerMarkThunkNumber(reg) +
6522 (short_offset ? BAKER_MARK_INTROSPECTION_REGISTER_COUNT : 0);
6523 return num * BAKER_MARK_INTROSPECTION_FIELD_ARRAY_ENTRY_SIZE;
6524}
6525
6526static inline int GetBakerMarkGcRootThunkDisplacement(Register reg) {
6527 return GetBakerMarkThunkNumber(reg) * BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRY_SIZE +
6528 BAKER_MARK_INTROSPECTION_GC_ROOT_ENTRIES_OFFSET;
6529}
6530
Alexey Frunze15958152017-02-09 19:08:30 -08006531void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6532 Location root,
6533 Register obj,
6534 uint32_t offset,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006535 ReadBarrierOption read_barrier_option,
6536 MipsLabel* label_low) {
6537 bool reordering;
6538 if (label_low != nullptr) {
6539 DCHECK_EQ(offset, 0x5678u);
6540 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006541 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006542 if (read_barrier_option == kWithReadBarrier) {
6543 DCHECK(kEmitCompilerReadBarrier);
6544 if (kUseBakerReadBarrier) {
6545 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6546 // Baker's read barrier are used:
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006547 if (kBakerReadBarrierThunksEnableForGcRoots) {
6548 // Note that we do not actually check the value of `GetIsGcMarking()`
6549 // to decide whether to mark the loaded GC root or not. Instead, we
6550 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6551 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6552 // vice versa.
6553 //
6554 // We use thunks for the slow path. That thunk checks the reference
6555 // and jumps to the entrypoint if needed.
6556 //
6557 // temp = Thread::Current()->pReadBarrierMarkReg00
6558 // // AKA &art_quick_read_barrier_mark_introspection.
6559 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6560 // if (temp != nullptr) {
6561 // temp = &gc_root_thunk<root_reg>
6562 // root = temp(root)
6563 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006564
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006565 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
6566 const int32_t entry_point_offset =
6567 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6568 const int thunk_disp = GetBakerMarkGcRootThunkDisplacement(root_reg);
6569 int16_t offset_low = Low16Bits(offset);
6570 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign
6571 // extension in lw.
6572 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6573 Register base = short_offset ? obj : TMP;
6574 // Loading the entrypoint does not require a load acquire since it is only changed when
6575 // threads are suspended or running a checkpoint.
6576 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6577 reordering = __ SetReorder(false);
6578 if (!short_offset) {
6579 DCHECK(!label_low);
6580 __ AddUpper(base, obj, offset_high);
6581 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006582 MipsLabel skip_call;
6583 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006584 if (label_low != nullptr) {
6585 DCHECK(short_offset);
6586 __ Bind(label_low);
6587 }
6588 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6589 __ LoadFromOffset(kLoadWord, root_reg, base, offset_low); // Single instruction
6590 // in delay slot.
6591 if (isR6) {
6592 __ Jialc(T9, thunk_disp);
6593 } else {
6594 __ Addiu(T9, T9, thunk_disp);
6595 __ Jalr(T9);
6596 __ Nop();
6597 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006598 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006599 __ SetReorder(reordering);
6600 } else {
6601 // Note that we do not actually check the value of `GetIsGcMarking()`
6602 // to decide whether to mark the loaded GC root or not. Instead, we
6603 // load into `temp` (T9) the read barrier mark entry point corresponding
6604 // to register `root`. If `temp` is null, it means that `GetIsGcMarking()`
6605 // is false, and vice versa.
6606 //
6607 // GcRoot<mirror::Object> root = *(obj+offset); // Original reference load.
6608 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6609 // if (temp != null) {
6610 // root = temp(root)
6611 // }
Alexey Frunze15958152017-02-09 19:08:30 -08006612
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006613 if (label_low != nullptr) {
6614 reordering = __ SetReorder(false);
6615 __ Bind(label_low);
6616 }
6617 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6618 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6619 if (label_low != nullptr) {
6620 __ SetReorder(reordering);
6621 }
6622 static_assert(
6623 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6624 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6625 "have different sizes.");
6626 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6627 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6628 "have different sizes.");
Alexey Frunze15958152017-02-09 19:08:30 -08006629
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006630 // Slow path marking the GC root `root`.
6631 Location temp = Location::RegisterLocation(T9);
6632 SlowPathCodeMIPS* slow_path =
6633 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6634 instruction,
6635 root,
6636 /*entrypoint*/ temp);
6637 codegen_->AddSlowPath(slow_path);
6638
6639 const int32_t entry_point_offset =
6640 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6641 // Loading the entrypoint does not require a load acquire since it is only changed when
6642 // threads are suspended or running a checkpoint.
6643 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6644 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6645 __ Bind(slow_path->GetExitLabel());
6646 }
Alexey Frunze15958152017-02-09 19:08:30 -08006647 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006648 if (label_low != nullptr) {
6649 reordering = __ SetReorder(false);
6650 __ Bind(label_low);
6651 }
Alexey Frunze15958152017-02-09 19:08:30 -08006652 // GC root loaded through a slow path for read barriers other
6653 // than Baker's.
6654 // /* GcRoot<mirror::Object>* */ root = obj + offset
6655 __ Addiu32(root_reg, obj, offset);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006656 if (label_low != nullptr) {
6657 __ SetReorder(reordering);
6658 }
Alexey Frunze15958152017-02-09 19:08:30 -08006659 // /* mirror::Object* */ root = root->Read()
6660 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6661 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006662 } else {
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006663 if (label_low != nullptr) {
6664 reordering = __ SetReorder(false);
6665 __ Bind(label_low);
6666 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006667 // Plain GC root load with no read barrier.
6668 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6669 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6670 // Note that GC roots are not affected by heap poisoning, thus we
6671 // do not have to unpoison `root_reg` here.
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006672 if (label_low != nullptr) {
6673 __ SetReorder(reordering);
6674 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006675 }
6676}
6677
Alexey Frunze15958152017-02-09 19:08:30 -08006678void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6679 Location ref,
6680 Register obj,
6681 uint32_t offset,
6682 Location temp,
6683 bool needs_null_check) {
6684 DCHECK(kEmitCompilerReadBarrier);
6685 DCHECK(kUseBakerReadBarrier);
6686
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006687 if (kBakerReadBarrierThunksEnableForFields) {
6688 // Note that we do not actually check the value of `GetIsGcMarking()`
6689 // to decide whether to mark the loaded reference or not. Instead, we
6690 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6691 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6692 // vice versa.
6693 //
6694 // We use thunks for the slow path. That thunk checks the reference
6695 // and jumps to the entrypoint if needed. If the holder is not gray,
6696 // it issues a load-load memory barrier and returns to the original
6697 // reference load.
6698 //
6699 // temp = Thread::Current()->pReadBarrierMarkReg00
6700 // // AKA &art_quick_read_barrier_mark_introspection.
6701 // if (temp != nullptr) {
6702 // temp = &field_array_thunk<holder_reg>
6703 // temp()
6704 // }
6705 // not_gray_return_address:
6706 // // If the offset is too large to fit into the lw instruction, we
6707 // // use an adjusted base register (TMP) here. This register
6708 // // receives bits 16 ... 31 of the offset before the thunk invocation
6709 // // and the thunk benefits from it.
6710 // HeapReference<mirror::Object> reference = *(obj+offset); // Original reference load.
6711 // gray_return_address:
6712
6713 DCHECK(temp.IsInvalid());
6714 bool isR6 = GetInstructionSetFeatures().IsR6();
6715 int16_t offset_low = Low16Bits(offset);
6716 int16_t offset_high = High16Bits(offset - offset_low); // Accounts for sign extension in lw.
6717 bool short_offset = IsInt<16>(static_cast<int32_t>(offset));
6718 bool reordering = __ SetReorder(false);
6719 const int32_t entry_point_offset =
6720 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6721 // There may have or may have not been a null check if the field offset is smaller than
6722 // the page size.
6723 // There must've been a null check in case it's actually a load from an array.
6724 // We will, however, perform an explicit null check in the thunk as it's easier to
6725 // do it than not.
6726 if (instruction->IsArrayGet()) {
6727 DCHECK(!needs_null_check);
6728 }
6729 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, short_offset);
6730 // Loading the entrypoint does not require a load acquire since it is only changed when
6731 // threads are suspended or running a checkpoint.
6732 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6733 Register ref_reg = ref.AsRegister<Register>();
6734 Register base = short_offset ? obj : TMP;
Alexey Frunze0cab6562017-07-25 15:19:36 -07006735 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006736 if (short_offset) {
6737 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006738 __ Beqzc(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006739 __ Nop(); // In forbidden slot.
6740 __ Jialc(T9, thunk_disp);
6741 } else {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006742 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006743 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6744 __ Jalr(T9);
6745 __ Nop(); // In delay slot.
6746 }
Alexey Frunze0cab6562017-07-25 15:19:36 -07006747 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006748 } else {
6749 if (isR6) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006750 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006751 __ Aui(base, obj, offset_high); // In delay slot.
6752 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006753 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006754 } else {
6755 __ Lui(base, offset_high);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006756 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006757 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6758 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006759 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006760 __ Addu(base, base, obj); // In delay slot.
6761 }
6762 }
6763 // /* HeapReference<Object> */ ref = *(obj + offset)
6764 __ LoadFromOffset(kLoadWord, ref_reg, base, offset_low); // Single instruction.
6765 if (needs_null_check) {
6766 MaybeRecordImplicitNullCheck(instruction);
6767 }
6768 __ MaybeUnpoisonHeapReference(ref_reg);
6769 __ SetReorder(reordering);
6770 return;
6771 }
6772
Alexey Frunze15958152017-02-09 19:08:30 -08006773 // /* HeapReference<Object> */ ref = *(obj + offset)
6774 Location no_index = Location::NoLocation();
6775 ScaleFactor no_scale_factor = TIMES_1;
6776 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6777 ref,
6778 obj,
6779 offset,
6780 no_index,
6781 no_scale_factor,
6782 temp,
6783 needs_null_check);
6784}
6785
6786void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6787 Location ref,
6788 Register obj,
6789 uint32_t data_offset,
6790 Location index,
6791 Location temp,
6792 bool needs_null_check) {
6793 DCHECK(kEmitCompilerReadBarrier);
6794 DCHECK(kUseBakerReadBarrier);
6795
6796 static_assert(
6797 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6798 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006799 ScaleFactor scale_factor = TIMES_4;
6800
6801 if (kBakerReadBarrierThunksEnableForArrays) {
6802 // Note that we do not actually check the value of `GetIsGcMarking()`
6803 // to decide whether to mark the loaded reference or not. Instead, we
6804 // load into `temp` (T9) the read barrier mark introspection entrypoint.
6805 // If `temp` is null, it means that `GetIsGcMarking()` is false, and
6806 // vice versa.
6807 //
6808 // We use thunks for the slow path. That thunk checks the reference
6809 // and jumps to the entrypoint if needed. If the holder is not gray,
6810 // it issues a load-load memory barrier and returns to the original
6811 // reference load.
6812 //
6813 // temp = Thread::Current()->pReadBarrierMarkReg00
6814 // // AKA &art_quick_read_barrier_mark_introspection.
6815 // if (temp != nullptr) {
6816 // temp = &field_array_thunk<holder_reg>
6817 // temp()
6818 // }
6819 // not_gray_return_address:
6820 // // The element address is pre-calculated in the TMP register before the
6821 // // thunk invocation and the thunk benefits from it.
6822 // HeapReference<mirror::Object> reference = data[index]; // Original reference load.
6823 // gray_return_address:
6824
6825 DCHECK(temp.IsInvalid());
6826 DCHECK(index.IsValid());
6827 bool reordering = __ SetReorder(false);
6828 const int32_t entry_point_offset =
6829 Thread::ReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(0);
6830 // We will not do the explicit null check in the thunk as some form of a null check
6831 // must've been done earlier.
6832 DCHECK(!needs_null_check);
6833 const int thunk_disp = GetBakerMarkFieldArrayThunkDisplacement(obj, /* short_offset */ false);
6834 // Loading the entrypoint does not require a load acquire since it is only changed when
6835 // threads are suspended or running a checkpoint.
6836 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
6837 Register ref_reg = ref.AsRegister<Register>();
6838 Register index_reg = index.IsRegisterPair()
6839 ? index.AsRegisterPairLow<Register>()
6840 : index.AsRegister<Register>();
Alexey Frunze0cab6562017-07-25 15:19:36 -07006841 MipsLabel skip_call;
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006842 if (GetInstructionSetFeatures().IsR6()) {
Alexey Frunze0cab6562017-07-25 15:19:36 -07006843 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006844 __ Lsa(TMP, index_reg, obj, scale_factor); // In delay slot.
6845 __ Jialc(T9, thunk_disp);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006846 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006847 } else {
6848 __ Sll(TMP, index_reg, scale_factor);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006849 __ Beqz(T9, &skip_call, /* is_bare */ true);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006850 __ Addiu(T9, T9, thunk_disp); // In delay slot.
6851 __ Jalr(T9);
Alexey Frunze0cab6562017-07-25 15:19:36 -07006852 __ Bind(&skip_call);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07006853 __ Addu(TMP, TMP, obj); // In delay slot.
6854 }
6855 // /* HeapReference<Object> */ ref = *(obj + data_offset + (index << scale_factor))
6856 DCHECK(IsInt<16>(static_cast<int32_t>(data_offset))) << data_offset;
6857 __ LoadFromOffset(kLoadWord, ref_reg, TMP, data_offset); // Single instruction.
6858 __ MaybeUnpoisonHeapReference(ref_reg);
6859 __ SetReorder(reordering);
6860 return;
6861 }
6862
Alexey Frunze15958152017-02-09 19:08:30 -08006863 // /* HeapReference<Object> */ ref =
6864 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
Alexey Frunze15958152017-02-09 19:08:30 -08006865 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6866 ref,
6867 obj,
6868 data_offset,
6869 index,
6870 scale_factor,
6871 temp,
6872 needs_null_check);
6873}
6874
6875void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6876 Location ref,
6877 Register obj,
6878 uint32_t offset,
6879 Location index,
6880 ScaleFactor scale_factor,
6881 Location temp,
6882 bool needs_null_check,
6883 bool always_update_field) {
6884 DCHECK(kEmitCompilerReadBarrier);
6885 DCHECK(kUseBakerReadBarrier);
6886
6887 // In slow path based read barriers, the read barrier call is
6888 // inserted after the original load. However, in fast path based
6889 // Baker's read barriers, we need to perform the load of
6890 // mirror::Object::monitor_ *before* the original reference load.
6891 // This load-load ordering is required by the read barrier.
6892 // The fast path/slow path (for Baker's algorithm) should look like:
6893 //
6894 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6895 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6896 // HeapReference<Object> ref = *src; // Original reference load.
6897 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6898 // if (is_gray) {
6899 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6900 // }
6901 //
6902 // Note: the original implementation in ReadBarrier::Barrier is
6903 // slightly more complex as it performs additional checks that we do
6904 // not do here for performance reasons.
6905
6906 Register ref_reg = ref.AsRegister<Register>();
6907 Register temp_reg = temp.AsRegister<Register>();
6908 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6909
6910 // /* int32_t */ monitor = obj->monitor_
6911 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6912 if (needs_null_check) {
6913 MaybeRecordImplicitNullCheck(instruction);
6914 }
6915 // /* LockWord */ lock_word = LockWord(monitor)
6916 static_assert(sizeof(LockWord) == sizeof(int32_t),
6917 "art::LockWord and int32_t have different sizes.");
6918
6919 __ Sync(0); // Barrier to prevent load-load reordering.
6920
6921 // The actual reference load.
6922 if (index.IsValid()) {
6923 // Load types involving an "index": ArrayGet,
6924 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6925 // intrinsics.
6926 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6927 if (index.IsConstant()) {
6928 size_t computed_offset =
6929 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6930 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6931 } else {
6932 // Handle the special case of the
6933 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6934 // intrinsics, which use a register pair as index ("long
6935 // offset"), of which only the low part contains data.
6936 Register index_reg = index.IsRegisterPair()
6937 ? index.AsRegisterPairLow<Register>()
6938 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006939 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006940 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6941 }
6942 } else {
6943 // /* HeapReference<Object> */ ref = *(obj + offset)
6944 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6945 }
6946
6947 // Object* ref = ref_addr->AsMirrorPtr()
6948 __ MaybeUnpoisonHeapReference(ref_reg);
6949
6950 // Slow path marking the object `ref` when it is gray.
6951 SlowPathCodeMIPS* slow_path;
6952 if (always_update_field) {
6953 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6954 // of the form `obj + field_offset`, where `obj` is a register and
6955 // `field_offset` is a register pair (of which only the lower half
6956 // is used). Thus `offset` and `scale_factor` above are expected
6957 // to be null in this code path.
6958 DCHECK_EQ(offset, 0u);
6959 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6960 slow_path = new (GetGraph()->GetArena())
6961 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6962 ref,
6963 obj,
6964 /* field_offset */ index,
6965 temp_reg);
6966 } else {
6967 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6968 }
6969 AddSlowPath(slow_path);
6970
6971 // if (rb_state == ReadBarrier::GrayState())
6972 // ref = ReadBarrier::Mark(ref);
6973 // Given the numeric representation, it's enough to check the low bit of the
6974 // rb_state. We do that by shifting the bit into the sign bit (31) and
6975 // performing a branch on less than zero.
6976 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6977 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6978 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6979 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6980 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6981 __ Bind(slow_path->GetExitLabel());
6982}
6983
6984void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6985 Location out,
6986 Location ref,
6987 Location obj,
6988 uint32_t offset,
6989 Location index) {
6990 DCHECK(kEmitCompilerReadBarrier);
6991
6992 // Insert a slow path based read barrier *after* the reference load.
6993 //
6994 // If heap poisoning is enabled, the unpoisoning of the loaded
6995 // reference will be carried out by the runtime within the slow
6996 // path.
6997 //
6998 // Note that `ref` currently does not get unpoisoned (when heap
6999 // poisoning is enabled), which is alright as the `ref` argument is
7000 // not used by the artReadBarrierSlow entry point.
7001 //
7002 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
7003 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
7004 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
7005 AddSlowPath(slow_path);
7006
7007 __ B(slow_path->GetEntryLabel());
7008 __ Bind(slow_path->GetExitLabel());
7009}
7010
7011void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
7012 Location out,
7013 Location ref,
7014 Location obj,
7015 uint32_t offset,
7016 Location index) {
7017 if (kEmitCompilerReadBarrier) {
7018 // Baker's read barriers shall be handled by the fast path
7019 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
7020 DCHECK(!kUseBakerReadBarrier);
7021 // If heap poisoning is enabled, unpoisoning will be taken care of
7022 // by the runtime within the slow path.
7023 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
7024 } else if (kPoisonHeapReferences) {
7025 __ UnpoisonHeapReference(out.AsRegister<Register>());
7026 }
7027}
7028
7029void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
7030 Location out,
7031 Location root) {
7032 DCHECK(kEmitCompilerReadBarrier);
7033
7034 // Insert a slow path based read barrier *after* the GC root load.
7035 //
7036 // Note that GC roots are not affected by heap poisoning, so we do
7037 // not need to do anything special for this here.
7038 SlowPathCodeMIPS* slow_path =
7039 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
7040 AddSlowPath(slow_path);
7041
7042 __ B(slow_path->GetEntryLabel());
7043 __ Bind(slow_path->GetExitLabel());
7044}
7045
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007046void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007047 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
7048 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007049 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007050 switch (type_check_kind) {
7051 case TypeCheckKind::kExactCheck:
7052 case TypeCheckKind::kAbstractClassCheck:
7053 case TypeCheckKind::kClassHierarchyCheck:
7054 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08007055 call_kind =
7056 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007057 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007058 break;
7059 case TypeCheckKind::kArrayCheck:
7060 case TypeCheckKind::kUnresolvedCheck:
7061 case TypeCheckKind::kInterfaceCheck:
7062 call_kind = LocationSummary::kCallOnSlowPath;
7063 break;
7064 }
7065
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007066 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007067 if (baker_read_barrier_slow_path) {
7068 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7069 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007070 locations->SetInAt(0, Location::RequiresRegister());
7071 locations->SetInAt(1, Location::RequiresRegister());
7072 // The output does overlap inputs.
7073 // Note that TypeCheckSlowPathMIPS uses this register too.
7074 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08007075 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007076}
7077
7078void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007079 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007080 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08007081 Location obj_loc = locations->InAt(0);
7082 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007083 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08007084 Location out_loc = locations->Out();
7085 Register out = out_loc.AsRegister<Register>();
7086 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
7087 DCHECK_LE(num_temps, 1u);
7088 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007089 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
7090 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
7091 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
7092 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007093 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007094 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007095
7096 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007097 // Avoid this check if we know `obj` is not null.
7098 if (instruction->MustDoNullCheck()) {
7099 __ Move(out, ZERO);
7100 __ Beqz(obj, &done);
7101 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007102
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007103 switch (type_check_kind) {
7104 case TypeCheckKind::kExactCheck: {
7105 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007106 GenerateReferenceLoadTwoRegisters(instruction,
7107 out_loc,
7108 obj_loc,
7109 class_offset,
7110 maybe_temp_loc,
7111 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007112 // Classes must be equal for the instanceof to succeed.
7113 __ Xor(out, out, cls);
7114 __ Sltiu(out, out, 1);
7115 break;
7116 }
7117
7118 case TypeCheckKind::kAbstractClassCheck: {
7119 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007120 GenerateReferenceLoadTwoRegisters(instruction,
7121 out_loc,
7122 obj_loc,
7123 class_offset,
7124 maybe_temp_loc,
7125 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007126 // If the class is abstract, we eagerly fetch the super class of the
7127 // object to avoid doing a comparison we know will fail.
7128 MipsLabel loop;
7129 __ Bind(&loop);
7130 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007131 GenerateReferenceLoadOneRegister(instruction,
7132 out_loc,
7133 super_offset,
7134 maybe_temp_loc,
7135 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007136 // If `out` is null, we use it for the result, and jump to `done`.
7137 __ Beqz(out, &done);
7138 __ Bne(out, cls, &loop);
7139 __ LoadConst32(out, 1);
7140 break;
7141 }
7142
7143 case TypeCheckKind::kClassHierarchyCheck: {
7144 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007145 GenerateReferenceLoadTwoRegisters(instruction,
7146 out_loc,
7147 obj_loc,
7148 class_offset,
7149 maybe_temp_loc,
7150 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007151 // Walk over the class hierarchy to find a match.
7152 MipsLabel loop, success;
7153 __ Bind(&loop);
7154 __ Beq(out, cls, &success);
7155 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08007156 GenerateReferenceLoadOneRegister(instruction,
7157 out_loc,
7158 super_offset,
7159 maybe_temp_loc,
7160 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007161 __ Bnez(out, &loop);
7162 // If `out` is null, we use it for the result, and jump to `done`.
7163 __ B(&done);
7164 __ Bind(&success);
7165 __ LoadConst32(out, 1);
7166 break;
7167 }
7168
7169 case TypeCheckKind::kArrayObjectCheck: {
7170 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007171 GenerateReferenceLoadTwoRegisters(instruction,
7172 out_loc,
7173 obj_loc,
7174 class_offset,
7175 maybe_temp_loc,
7176 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007177 // Do an exact check.
7178 MipsLabel success;
7179 __ Beq(out, cls, &success);
7180 // Otherwise, we need to check that the object's class is a non-primitive array.
7181 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08007182 GenerateReferenceLoadOneRegister(instruction,
7183 out_loc,
7184 component_offset,
7185 maybe_temp_loc,
7186 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007187 // If `out` is null, we use it for the result, and jump to `done`.
7188 __ Beqz(out, &done);
7189 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
7190 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
7191 __ Sltiu(out, out, 1);
7192 __ B(&done);
7193 __ Bind(&success);
7194 __ LoadConst32(out, 1);
7195 break;
7196 }
7197
7198 case TypeCheckKind::kArrayCheck: {
7199 // No read barrier since the slow path will retry upon failure.
7200 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08007201 GenerateReferenceLoadTwoRegisters(instruction,
7202 out_loc,
7203 obj_loc,
7204 class_offset,
7205 maybe_temp_loc,
7206 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007207 DCHECK(locations->OnlyCallsOnSlowPath());
7208 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7209 /* is_fatal */ false);
7210 codegen_->AddSlowPath(slow_path);
7211 __ Bne(out, cls, slow_path->GetEntryLabel());
7212 __ LoadConst32(out, 1);
7213 break;
7214 }
7215
7216 case TypeCheckKind::kUnresolvedCheck:
7217 case TypeCheckKind::kInterfaceCheck: {
7218 // Note that we indeed only call on slow path, but we always go
7219 // into the slow path for the unresolved and interface check
7220 // cases.
7221 //
7222 // We cannot directly call the InstanceofNonTrivial runtime
7223 // entry point without resorting to a type checking slow path
7224 // here (i.e. by calling InvokeRuntime directly), as it would
7225 // require to assign fixed registers for the inputs of this
7226 // HInstanceOf instruction (following the runtime calling
7227 // convention), which might be cluttered by the potential first
7228 // read barrier emission at the beginning of this method.
7229 //
7230 // TODO: Introduce a new runtime entry point taking the object
7231 // to test (instead of its class) as argument, and let it deal
7232 // with the read barrier issues. This will let us refactor this
7233 // case of the `switch` code as it was previously (with a direct
7234 // call to the runtime not using a type checking slow path).
7235 // This should also be beneficial for the other cases above.
7236 DCHECK(locations->OnlyCallsOnSlowPath());
7237 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
7238 /* is_fatal */ false);
7239 codegen_->AddSlowPath(slow_path);
7240 __ B(slow_path->GetEntryLabel());
7241 break;
7242 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007243 }
7244
7245 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08007246
7247 if (slow_path != nullptr) {
7248 __ Bind(slow_path->GetExitLabel());
7249 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007250}
7251
7252void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
7253 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7254 locations->SetOut(Location::ConstantLocation(constant));
7255}
7256
7257void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
7258 // Will be generated at use site.
7259}
7260
7261void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
7262 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7263 locations->SetOut(Location::ConstantLocation(constant));
7264}
7265
7266void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
7267 // Will be generated at use site.
7268}
7269
7270void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
7271 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
7272 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
7273}
7274
7275void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7276 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007277 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007278 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08007279 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007280}
7281
7282void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
7283 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
7284 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007285 Location receiver = invoke->GetLocations()->InAt(0);
7286 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007287 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007288
7289 // Set the hidden argument.
7290 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
7291 invoke->GetDexMethodIndex());
7292
7293 // temp = object->GetClass();
7294 if (receiver.IsStackSlot()) {
7295 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
7296 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
7297 } else {
7298 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
7299 }
7300 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007301 // Instead of simply (possibly) unpoisoning `temp` here, we should
7302 // emit a read barrier for the previous class reference load.
7303 // However this is not required in practice, as this is an
7304 // intermediate/temporary reference and because the current
7305 // concurrent copying collector keeps the from-space memory
7306 // intact/accessible until the end of the marking phase (the
7307 // concurrent copying collector may not in the future).
7308 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00007309 __ LoadFromOffset(kLoadWord, temp, temp,
7310 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
7311 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00007312 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007313 // temp = temp->GetImtEntryAt(method_offset);
7314 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7315 // T9 = temp->GetEntryPoint();
7316 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7317 // T9();
7318 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007319 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007320 DCHECK(!codegen_->IsLeafMethod());
7321 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
7322}
7323
7324void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07007325 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7326 if (intrinsic.TryDispatch(invoke)) {
7327 return;
7328 }
7329
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007330 HandleInvoke(invoke);
7331}
7332
7333void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007334 // Explicit clinit checks triggered by static invokes must have been pruned by
7335 // art::PrepareForRegisterAllocation.
7336 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007337
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007338 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007339 bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
7340 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007341
Chris Larsen701566a2015-10-27 15:29:13 -07007342 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
7343 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007344 if (invoke->GetLocations()->CanCall() && has_extra_input) {
7345 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
7346 }
Chris Larsen701566a2015-10-27 15:29:13 -07007347 return;
7348 }
7349
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007350 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007351
7352 // Add the extra input register if either the dex cache array base register
7353 // or the PC-relative base register for accessing literals is needed.
7354 if (has_extra_input) {
7355 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
7356 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007357}
7358
Orion Hodsonac141392017-01-13 11:53:47 +00007359void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7360 HandleInvoke(invoke);
7361}
7362
7363void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
7364 codegen_->GenerateInvokePolymorphicCall(invoke);
7365}
7366
Chris Larsen701566a2015-10-27 15:29:13 -07007367static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007368 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07007369 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
7370 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007371 return true;
7372 }
7373 return false;
7374}
7375
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007376HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07007377 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007378 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007379 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007380 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007381 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007382 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007383 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007384 case HLoadString::LoadKind::kJitTableAddress:
7385 DCHECK(Runtime::Current()->UseJitCompilation());
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007386 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007387 case HLoadString::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007388 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007389 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007390 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007391 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007392}
7393
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007394HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7395 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007396 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007397 case HLoadClass::LoadKind::kInvalid:
7398 LOG(FATAL) << "UNREACHABLE";
7399 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007400 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007401 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007402 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007403 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007404 case HLoadClass::LoadKind::kBssEntry:
7405 DCHECK(!Runtime::Current()->UseJitCompilation());
7406 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007407 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007408 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007409 break;
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007410 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007411 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007412 break;
7413 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007414 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007415}
7416
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007417Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7418 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007419 CHECK(!GetInstructionSetFeatures().IsR6());
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007420 CHECK(!GetGraph()->HasIrreducibleLoops());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007421 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7422 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7423 if (!invoke->GetLocations()->Intrinsified()) {
7424 return location.AsRegister<Register>();
7425 }
7426 // For intrinsics we allow any location, so it may be on the stack.
7427 if (!location.IsRegister()) {
7428 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7429 return temp;
7430 }
7431 // For register locations, check if the register was saved. If so, get it from the stack.
7432 // Note: There is a chance that the register was saved but not overwritten, so we could
7433 // save one load. However, since this is just an intrinsic slow path we prefer this
7434 // simple and more robust approach rather that trying to determine if that's the case.
7435 SlowPathCode* slow_path = GetCurrentSlowPath();
7436 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7437 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7438 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7439 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7440 return temp;
7441 }
7442 return location.AsRegister<Register>();
7443}
7444
Vladimir Markodc151b22015-10-15 18:02:30 +01007445HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7446 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007447 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007448 return desired_dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007449}
7450
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007451void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7452 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007453 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007454 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007455 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7456 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007457 bool is_r6 = GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007458 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
7459 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6 && !has_irreducible_loops)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007460 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7461 : ZERO;
7462
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007463 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007464 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007465 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007466 uint32_t offset =
7467 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007468 __ LoadFromOffset(kLoadWord,
7469 temp.AsRegister<Register>(),
7470 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007471 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007472 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007473 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007474 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007475 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007476 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007477 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7478 DCHECK(GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007479 PcRelativePatchInfo* info_high = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7480 PcRelativePatchInfo* info_low =
7481 NewPcRelativeMethodPatch(invoke->GetTargetMethod(), info_high);
Vladimir Marko65979462017-05-19 17:25:12 +01007482 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007483 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7484 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko65979462017-05-19 17:25:12 +01007485 break;
7486 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007487 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7488 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7489 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007490 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007491 PcRelativePatchInfo* info_high = NewMethodBssEntryPatch(
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007492 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007493 PcRelativePatchInfo* info_low = NewMethodBssEntryPatch(
7494 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()), info_high);
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007495 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007496 EmitPcRelativeAddressPlaceholderHigh(info_high, TMP, base_reg);
7497 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007498 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007499 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007500 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7501 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7502 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007503 }
7504 }
7505
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007506 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007507 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007508 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007509 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007510 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7511 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007512 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007513 T9,
7514 callee_method.AsRegister<Register>(),
7515 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007516 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007517 // T9()
7518 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007519 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007520 break;
7521 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007522 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7523
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007524 DCHECK(!IsLeafMethod());
7525}
7526
7527void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007528 // Explicit clinit checks triggered by static invokes must have been pruned by
7529 // art::PrepareForRegisterAllocation.
7530 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007531
7532 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7533 return;
7534 }
7535
7536 LocationSummary* locations = invoke->GetLocations();
7537 codegen_->GenerateStaticOrDirectCall(invoke,
7538 locations->HasTemps()
7539 ? locations->GetTemp(0)
7540 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007541}
7542
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007543void CodeGeneratorMIPS::GenerateVirtualCall(
7544 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007545 // Use the calling convention instead of the location of the receiver, as
7546 // intrinsics may have put the receiver in a different register. In the intrinsics
7547 // slow path, the arguments have been moved to the right place, so here we are
7548 // guaranteed that the receiver is the first register of the calling convention.
7549 InvokeDexCallingConvention calling_convention;
7550 Register receiver = calling_convention.GetRegisterAt(0);
7551
Chris Larsen3acee732015-11-18 13:31:08 -08007552 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007553 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7554 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7555 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007556 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007557
7558 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007559 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007560 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007561 // Instead of simply (possibly) unpoisoning `temp` here, we should
7562 // emit a read barrier for the previous class reference load.
7563 // However this is not required in practice, as this is an
7564 // intermediate/temporary reference and because the current
7565 // concurrent copying collector keeps the from-space memory
7566 // intact/accessible until the end of the marking phase (the
7567 // concurrent copying collector may not in the future).
7568 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007569 // temp = temp->GetMethodAt(method_offset);
7570 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7571 // T9 = temp->GetEntryPoint();
7572 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7573 // T9();
7574 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007575 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007576 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007577}
7578
7579void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7580 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7581 return;
7582 }
7583
7584 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007585 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007586}
7587
7588void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007589 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007590 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007591 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007592 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7593 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007594 return;
7595 }
Vladimir Marko41559982017-01-06 14:04:23 +00007596 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007597 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007598 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze15958152017-02-09 19:08:30 -08007599 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7600 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007601 ? LocationSummary::kCallOnSlowPath
7602 : LocationSummary::kNoCall;
7603 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007604 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7605 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7606 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007607 switch (load_kind) {
7608 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007609 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007610 case HLoadClass::LoadKind::kBootImageAddress:
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007611 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007612 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007613 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007614 break;
7615 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007616 if (has_irreducible_loops) {
7617 codegen_->ClobberRA();
7618 break;
7619 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007620 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007621 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007622 locations->SetInAt(0, Location::RequiresRegister());
7623 break;
7624 default:
7625 break;
7626 }
7627 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007628 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7629 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7630 // Rely on the type resolution or initialization and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007631 // Request a temp to hold the BSS entry location for the slow path.
7632 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007633 RegisterSet caller_saves = RegisterSet::Empty();
7634 InvokeRuntimeCallingConvention calling_convention;
7635 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7636 locations->SetCustomSlowPathCallerSaves(caller_saves);
7637 } else {
7638 // For non-Baker read barriers we have a temp-clobbering call.
7639 }
7640 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007641}
7642
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007643// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7644// move.
7645void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007646 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007647 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007648 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007649 return;
7650 }
Vladimir Marko41559982017-01-06 14:04:23 +00007651 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007652
Vladimir Marko41559982017-01-06 14:04:23 +00007653 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007654 Location out_loc = locations->Out();
7655 Register out = out_loc.AsRegister<Register>();
7656 Register base_or_current_method_reg;
7657 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007658 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007659 switch (load_kind) {
7660 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007661 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007662 case HLoadClass::LoadKind::kBootImageAddress:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007663 case HLoadClass::LoadKind::kBootImageClassTable:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007664 case HLoadClass::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007665 base_or_current_method_reg =
7666 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007667 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007668 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007669 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007670 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7671 break;
7672 default:
7673 base_or_current_method_reg = ZERO;
7674 break;
7675 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007676
Alexey Frunze15958152017-02-09 19:08:30 -08007677 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7678 ? kWithoutReadBarrier
7679 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007680 bool generate_null_check = false;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007681 CodeGeneratorMIPS::PcRelativePatchInfo* bss_info_high = nullptr;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007682 switch (load_kind) {
7683 case HLoadClass::LoadKind::kReferrersClass: {
7684 DCHECK(!cls->CanCallRuntime());
7685 DCHECK(!cls->MustGenerateClinitCheck());
7686 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7687 GenerateGcRootFieldLoad(cls,
7688 out_loc,
7689 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007690 ArtMethod::DeclaringClassOffset().Int32Value(),
7691 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007692 break;
7693 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007694 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007695 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007696 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007697 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Alexey Frunze06a46c42016-07-19 15:00:40 -07007698 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007699 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7700 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007701 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7702 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007703 base_or_current_method_reg);
7704 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007705 break;
7706 }
7707 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007708 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007709 uint32_t address = dchecked_integral_cast<uint32_t>(
7710 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7711 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007712 if (isR6 || !has_irreducible_loops) {
7713 __ LoadLiteral(out,
7714 base_or_current_method_reg,
7715 codegen_->DeduplicateBootImageAddressLiteral(address));
7716 } else {
7717 __ LoadConst32(out, address);
7718 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007719 break;
7720 }
Vladimir Marko94ec2db2017-09-06 17:21:03 +01007721 case HLoadClass::LoadKind::kBootImageClassTable: {
7722 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7723 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7724 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
7725 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7726 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex(), info_high);
7727 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7728 out,
7729 base_or_current_method_reg);
7730 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7731 // Extract the reference from the slot data, i.e. clear the hash bits.
7732 int32_t masked_hash = ClassTable::TableSlot::MaskHash(
7733 ComputeModifiedUtf8Hash(cls->GetDexFile().StringByTypeIdx(cls->GetTypeIndex())));
7734 if (masked_hash != 0) {
7735 __ Addiu(out, out, -masked_hash);
7736 }
7737 break;
7738 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007739 case HLoadClass::LoadKind::kBssEntry: {
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007740 bss_info_high = codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
7741 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7742 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex(), bss_info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007743 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007744 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007745 codegen_->EmitPcRelativeAddressPlaceholderHigh(bss_info_high,
7746 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007747 base_or_current_method_reg);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007748 GenerateGcRootFieldLoad(cls,
7749 out_loc,
7750 temp,
7751 /* placeholder */ 0x5678,
7752 read_barrier_option,
7753 &info_low->label);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007754 generate_null_check = true;
7755 break;
7756 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007757 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007758 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7759 cls->GetTypeIndex(),
7760 cls->GetClass());
7761 bool reordering = __ SetReorder(false);
7762 __ Bind(&info->high_label);
7763 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007764 __ SetReorder(reordering);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007765 GenerateGcRootFieldLoad(cls,
7766 out_loc,
7767 out,
7768 /* placeholder */ 0x5678,
7769 read_barrier_option,
7770 &info->low_label);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007771 break;
7772 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007773 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007774 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007775 LOG(FATAL) << "UNREACHABLE";
7776 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007777 }
7778
7779 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7780 DCHECK(cls->CanCallRuntime());
7781 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007782 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck(), bss_info_high);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007783 codegen_->AddSlowPath(slow_path);
7784 if (generate_null_check) {
7785 __ Beqz(out, slow_path->GetEntryLabel());
7786 }
7787 if (cls->MustGenerateClinitCheck()) {
7788 GenerateClassInitializationCheck(slow_path, out);
7789 } else {
7790 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007791 }
7792 }
7793}
7794
7795static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007796 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007797}
7798
7799void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7800 LocationSummary* locations =
7801 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7802 locations->SetOut(Location::RequiresRegister());
7803}
7804
7805void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7806 Register out = load->GetLocations()->Out().AsRegister<Register>();
7807 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7808}
7809
7810void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7811 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7812}
7813
7814void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7815 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7816}
7817
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007818void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007819 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007820 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007821 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007822 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007823 const bool has_irreducible_loops = codegen_->GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007824 switch (load_kind) {
7825 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007826 case HLoadString::LoadKind::kBootImageAddress:
7827 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007828 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007829 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007830 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007831 break;
7832 }
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007833 if (has_irreducible_loops) {
7834 codegen_->ClobberRA();
7835 break;
7836 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007837 FALLTHROUGH_INTENDED;
7838 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007839 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007840 locations->SetInAt(0, Location::RequiresRegister());
7841 break;
7842 default:
7843 break;
7844 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007845 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007846 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007847 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007848 } else {
7849 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007850 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7851 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7852 // Rely on the pResolveString and marking to save everything we need.
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007853 // Request a temp to hold the BSS entry location for the slow path.
7854 locations->AddTemp(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007855 RegisterSet caller_saves = RegisterSet::Empty();
7856 InvokeRuntimeCallingConvention calling_convention;
7857 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7858 locations->SetCustomSlowPathCallerSaves(caller_saves);
7859 } else {
7860 // For non-Baker read barriers we have a temp-clobbering call.
7861 }
7862 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007863 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007864}
7865
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007866// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7867// move.
7868void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007869 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007870 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007871 Location out_loc = locations->Out();
7872 Register out = out_loc.AsRegister<Register>();
7873 Register base_or_current_method_reg;
7874 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007875 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007876 switch (load_kind) {
7877 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007878 case HLoadString::LoadKind::kBootImageAddress:
7879 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007880 case HLoadString::LoadKind::kBootImageInternTable:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007881 case HLoadString::LoadKind::kBssEntry:
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007882 base_or_current_method_reg =
7883 (isR6 || has_irreducible_loops) ? ZERO : locations->InAt(0).AsRegister<Register>();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007884 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007885 default:
7886 base_or_current_method_reg = ZERO;
7887 break;
7888 }
7889
7890 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007891 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007892 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007893 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007894 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007895 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7896 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007897 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7898 out,
Alexey Frunzea663d9d2017-07-31 18:43:18 -07007899 base_or_current_method_reg);
7900 __ Addiu(out, out, /* placeholder */ 0x5678, &info_low->label);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007901 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007902 }
7903 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007904 uint32_t address = dchecked_integral_cast<uint32_t>(
7905 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7906 DCHECK_NE(address, 0u);
Goran Jakovljevicdebb5102017-09-21 14:24:06 +02007907 if (isR6 || !has_irreducible_loops) {
7908 __ LoadLiteral(out,
7909 base_or_current_method_reg,
7910 codegen_->DeduplicateBootImageAddressLiteral(address));
7911 } else {
7912 __ LoadConst32(out, address);
7913 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007914 return;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007915 }
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007916 case HLoadString::LoadKind::kBootImageInternTable: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007917 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007918 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007919 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007920 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7921 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Vladimir Marko6cfbdbc2017-07-25 13:26:39 +01007922 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7923 out,
7924 base_or_current_method_reg);
7925 __ Lw(out, out, /* placeholder */ 0x5678, &info_low->label);
7926 return;
7927 }
7928 case HLoadString::LoadKind::kBssEntry: {
7929 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7930 CodeGeneratorMIPS::PcRelativePatchInfo* info_high =
7931 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex());
7932 CodeGeneratorMIPS::PcRelativePatchInfo* info_low =
7933 codegen_->NewStringBssEntryPatch(load->GetDexFile(), load->GetStringIndex(), info_high);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007934 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007935 Register temp = non_baker_read_barrier ? out : locations->GetTemp(0).AsRegister<Register>();
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007936 codegen_->EmitPcRelativeAddressPlaceholderHigh(info_high,
7937 temp,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007938 base_or_current_method_reg);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007939 GenerateGcRootFieldLoad(load,
7940 out_loc,
7941 temp,
7942 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007943 kCompilerReadBarrierOption,
7944 &info_low->label);
Alexey Frunze5fa5c042017-06-01 21:07:52 -07007945 SlowPathCodeMIPS* slow_path =
7946 new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load, info_high);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007947 codegen_->AddSlowPath(slow_path);
7948 __ Beqz(out, slow_path->GetEntryLabel());
7949 __ Bind(slow_path->GetExitLabel());
7950 return;
7951 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007952 case HLoadString::LoadKind::kJitTableAddress: {
7953 CodeGeneratorMIPS::JitPatchInfo* info =
7954 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7955 load->GetStringIndex(),
7956 load->GetString());
7957 bool reordering = __ SetReorder(false);
7958 __ Bind(&info->high_label);
7959 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007960 __ SetReorder(reordering);
Alexey Frunze15958152017-02-09 19:08:30 -08007961 GenerateGcRootFieldLoad(load,
7962 out_loc,
7963 out,
7964 /* placeholder */ 0x5678,
Alexey Frunze4147fcc2017-06-17 19:57:27 -07007965 kCompilerReadBarrierOption,
7966 &info->low_label);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007967 return;
7968 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007969 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007970 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007971 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007972
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007973 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007974 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007975 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007976 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007977 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007978 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7979 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007980}
7981
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007982void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7983 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7984 locations->SetOut(Location::ConstantLocation(constant));
7985}
7986
7987void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7988 // Will be generated at use site.
7989}
7990
7991void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7992 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007993 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007994 InvokeRuntimeCallingConvention calling_convention;
7995 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7996}
7997
7998void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7999 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008000 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008001 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
8002 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008003 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008004 }
8005 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
8006}
8007
8008void LocationsBuilderMIPS::VisitMul(HMul* mul) {
8009 LocationSummary* locations =
8010 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
8011 switch (mul->GetResultType()) {
8012 case Primitive::kPrimInt:
8013 case Primitive::kPrimLong:
8014 locations->SetInAt(0, Location::RequiresRegister());
8015 locations->SetInAt(1, Location::RequiresRegister());
8016 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8017 break;
8018
8019 case Primitive::kPrimFloat:
8020 case Primitive::kPrimDouble:
8021 locations->SetInAt(0, Location::RequiresFpuRegister());
8022 locations->SetInAt(1, Location::RequiresFpuRegister());
8023 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8024 break;
8025
8026 default:
8027 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
8028 }
8029}
8030
8031void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
8032 Primitive::Type type = instruction->GetType();
8033 LocationSummary* locations = instruction->GetLocations();
8034 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
8035
8036 switch (type) {
8037 case Primitive::kPrimInt: {
8038 Register dst = locations->Out().AsRegister<Register>();
8039 Register lhs = locations->InAt(0).AsRegister<Register>();
8040 Register rhs = locations->InAt(1).AsRegister<Register>();
8041
8042 if (isR6) {
8043 __ MulR6(dst, lhs, rhs);
8044 } else {
8045 __ MulR2(dst, lhs, rhs);
8046 }
8047 break;
8048 }
8049 case Primitive::kPrimLong: {
8050 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8051 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8052 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8053 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
8054 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
8055 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
8056
8057 // Extra checks to protect caused by the existance of A1_A2.
8058 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
8059 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
8060 DCHECK_NE(dst_high, lhs_low);
8061 DCHECK_NE(dst_high, rhs_low);
8062
8063 // A_B * C_D
8064 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
8065 // dst_lo: [ low(B*D) ]
8066 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
8067
8068 if (isR6) {
8069 __ MulR6(TMP, lhs_high, rhs_low);
8070 __ MulR6(dst_high, lhs_low, rhs_high);
8071 __ Addu(dst_high, dst_high, TMP);
8072 __ MuhuR6(TMP, lhs_low, rhs_low);
8073 __ Addu(dst_high, dst_high, TMP);
8074 __ MulR6(dst_low, lhs_low, rhs_low);
8075 } else {
8076 __ MulR2(TMP, lhs_high, rhs_low);
8077 __ MulR2(dst_high, lhs_low, rhs_high);
8078 __ Addu(dst_high, dst_high, TMP);
8079 __ MultuR2(lhs_low, rhs_low);
8080 __ Mfhi(TMP);
8081 __ Addu(dst_high, dst_high, TMP);
8082 __ Mflo(dst_low);
8083 }
8084 break;
8085 }
8086 case Primitive::kPrimFloat:
8087 case Primitive::kPrimDouble: {
8088 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8089 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
8090 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
8091 if (type == Primitive::kPrimFloat) {
8092 __ MulS(dst, lhs, rhs);
8093 } else {
8094 __ MulD(dst, lhs, rhs);
8095 }
8096 break;
8097 }
8098 default:
8099 LOG(FATAL) << "Unexpected mul type " << type;
8100 }
8101}
8102
8103void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
8104 LocationSummary* locations =
8105 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
8106 switch (neg->GetResultType()) {
8107 case Primitive::kPrimInt:
8108 case Primitive::kPrimLong:
8109 locations->SetInAt(0, Location::RequiresRegister());
8110 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8111 break;
8112
8113 case Primitive::kPrimFloat:
8114 case Primitive::kPrimDouble:
8115 locations->SetInAt(0, Location::RequiresFpuRegister());
8116 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8117 break;
8118
8119 default:
8120 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
8121 }
8122}
8123
8124void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
8125 Primitive::Type type = instruction->GetType();
8126 LocationSummary* locations = instruction->GetLocations();
8127
8128 switch (type) {
8129 case Primitive::kPrimInt: {
8130 Register dst = locations->Out().AsRegister<Register>();
8131 Register src = locations->InAt(0).AsRegister<Register>();
8132 __ Subu(dst, ZERO, src);
8133 break;
8134 }
8135 case Primitive::kPrimLong: {
8136 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8137 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8138 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8139 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8140 __ Subu(dst_low, ZERO, src_low);
8141 __ Sltu(TMP, ZERO, dst_low);
8142 __ Subu(dst_high, ZERO, src_high);
8143 __ Subu(dst_high, dst_high, TMP);
8144 break;
8145 }
8146 case Primitive::kPrimFloat:
8147 case Primitive::kPrimDouble: {
8148 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8149 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8150 if (type == Primitive::kPrimFloat) {
8151 __ NegS(dst, src);
8152 } else {
8153 __ NegD(dst, src);
8154 }
8155 break;
8156 }
8157 default:
8158 LOG(FATAL) << "Unexpected neg type " << type;
8159 }
8160}
8161
8162void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
8163 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008164 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008165 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008166 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008167 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8168 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008169}
8170
8171void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008172 // Note: if heap poisoning is enabled, the entry point takes care
8173 // of poisoning the reference.
Goran Jakovljevic854df412017-06-27 14:41:39 +02008174 QuickEntrypointEnum entrypoint =
8175 CodeGenerator::GetArrayAllocationEntrypoint(instruction->GetLoadClass()->GetClass());
8176 codegen_->InvokeRuntime(entrypoint, instruction, instruction->GetDexPc());
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00008177 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevic854df412017-06-27 14:41:39 +02008178 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008179}
8180
8181void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
8182 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008183 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008184 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00008185 if (instruction->IsStringAlloc()) {
8186 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
8187 } else {
8188 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00008189 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008190 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
8191}
8192
8193void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08008194 // Note: if heap poisoning is enabled, the entry point takes care
8195 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00008196 if (instruction->IsStringAlloc()) {
8197 // String is allocated through StringFactory. Call NewEmptyString entry point.
8198 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07008199 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00008200 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
8201 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
8202 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07008203 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00008204 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
8205 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008206 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00008207 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00008208 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008209}
8210
8211void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
8212 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8213 locations->SetInAt(0, Location::RequiresRegister());
8214 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8215}
8216
8217void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
8218 Primitive::Type type = instruction->GetType();
8219 LocationSummary* locations = instruction->GetLocations();
8220
8221 switch (type) {
8222 case Primitive::kPrimInt: {
8223 Register dst = locations->Out().AsRegister<Register>();
8224 Register src = locations->InAt(0).AsRegister<Register>();
8225 __ Nor(dst, src, ZERO);
8226 break;
8227 }
8228
8229 case Primitive::kPrimLong: {
8230 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8231 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8232 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8233 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8234 __ Nor(dst_high, src_high, ZERO);
8235 __ Nor(dst_low, src_low, ZERO);
8236 break;
8237 }
8238
8239 default:
8240 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
8241 }
8242}
8243
8244void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8245 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8246 locations->SetInAt(0, Location::RequiresRegister());
8247 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8248}
8249
8250void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
8251 LocationSummary* locations = instruction->GetLocations();
8252 __ Xori(locations->Out().AsRegister<Register>(),
8253 locations->InAt(0).AsRegister<Register>(),
8254 1);
8255}
8256
8257void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01008258 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
8259 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008260}
8261
Calin Juravle2ae48182016-03-16 14:05:09 +00008262void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
8263 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008264 return;
8265 }
8266 Location obj = instruction->GetLocations()->InAt(0);
8267
8268 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00008269 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008270}
8271
Calin Juravle2ae48182016-03-16 14:05:09 +00008272void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008273 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00008274 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008275
8276 Location obj = instruction->GetLocations()->InAt(0);
8277
8278 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
8279}
8280
8281void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00008282 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008283}
8284
8285void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
8286 HandleBinaryOp(instruction);
8287}
8288
8289void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
8290 HandleBinaryOp(instruction);
8291}
8292
8293void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
8294 LOG(FATAL) << "Unreachable";
8295}
8296
8297void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
8298 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
8299}
8300
8301void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
8302 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
8303 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
8304 if (location.IsStackSlot()) {
8305 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8306 } else if (location.IsDoubleStackSlot()) {
8307 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
8308 }
8309 locations->SetOut(location);
8310}
8311
8312void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
8313 ATTRIBUTE_UNUSED) {
8314 // Nothing to do, the parameter is already at its location.
8315}
8316
8317void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
8318 LocationSummary* locations =
8319 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8320 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
8321}
8322
8323void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
8324 ATTRIBUTE_UNUSED) {
8325 // Nothing to do, the method is already at its location.
8326}
8327
8328void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
8329 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01008330 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008331 locations->SetInAt(i, Location::Any());
8332 }
8333 locations->SetOut(Location::Any());
8334}
8335
8336void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
8337 LOG(FATAL) << "Unreachable";
8338}
8339
8340void LocationsBuilderMIPS::VisitRem(HRem* rem) {
8341 Primitive::Type type = rem->GetResultType();
8342 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008343 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008344 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
8345
8346 switch (type) {
8347 case Primitive::kPrimInt:
8348 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08008349 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008350 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8351 break;
8352
8353 case Primitive::kPrimLong: {
8354 InvokeRuntimeCallingConvention calling_convention;
8355 locations->SetInAt(0, Location::RegisterPairLocation(
8356 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8357 locations->SetInAt(1, Location::RegisterPairLocation(
8358 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
8359 locations->SetOut(calling_convention.GetReturnLocation(type));
8360 break;
8361 }
8362
8363 case Primitive::kPrimFloat:
8364 case Primitive::kPrimDouble: {
8365 InvokeRuntimeCallingConvention calling_convention;
8366 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8367 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
8368 locations->SetOut(calling_convention.GetReturnLocation(type));
8369 break;
8370 }
8371
8372 default:
8373 LOG(FATAL) << "Unexpected rem type " << type;
8374 }
8375}
8376
8377void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
8378 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008379
8380 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08008381 case Primitive::kPrimInt:
8382 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008383 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008384 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008385 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008386 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
8387 break;
8388 }
8389 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008390 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008391 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008392 break;
8393 }
8394 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008395 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008396 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008397 break;
8398 }
8399 default:
8400 LOG(FATAL) << "Unexpected rem type " << type;
8401 }
8402}
8403
Igor Murashkind01745e2017-04-05 16:40:31 -07008404void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8405 constructor_fence->SetLocations(nullptr);
8406}
8407
8408void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8409 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8410 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8411}
8412
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008413void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8414 memory_barrier->SetLocations(nullptr);
8415}
8416
8417void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8418 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8419}
8420
8421void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8422 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
8423 Primitive::Type return_type = ret->InputAt(0)->GetType();
8424 locations->SetInAt(0, MipsReturnLocation(return_type));
8425}
8426
8427void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8428 codegen_->GenerateFrameExit();
8429}
8430
8431void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8432 ret->SetLocations(nullptr);
8433}
8434
8435void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8436 codegen_->GenerateFrameExit();
8437}
8438
Alexey Frunze92d90602015-12-18 18:16:36 -08008439void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8440 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008441}
8442
Alexey Frunze92d90602015-12-18 18:16:36 -08008443void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8444 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008445}
8446
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008447void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8448 HandleShift(shl);
8449}
8450
8451void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8452 HandleShift(shl);
8453}
8454
8455void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8456 HandleShift(shr);
8457}
8458
8459void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8460 HandleShift(shr);
8461}
8462
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008463void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8464 HandleBinaryOp(instruction);
8465}
8466
8467void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8468 HandleBinaryOp(instruction);
8469}
8470
8471void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8472 HandleFieldGet(instruction, instruction->GetFieldInfo());
8473}
8474
8475void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8476 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8477}
8478
8479void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8480 HandleFieldSet(instruction, instruction->GetFieldInfo());
8481}
8482
8483void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008484 HandleFieldSet(instruction,
8485 instruction->GetFieldInfo(),
8486 instruction->GetDexPc(),
8487 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008488}
8489
8490void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8491 HUnresolvedInstanceFieldGet* instruction) {
8492 FieldAccessCallingConventionMIPS calling_convention;
8493 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8494 instruction->GetFieldType(),
8495 calling_convention);
8496}
8497
8498void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8499 HUnresolvedInstanceFieldGet* instruction) {
8500 FieldAccessCallingConventionMIPS calling_convention;
8501 codegen_->GenerateUnresolvedFieldAccess(instruction,
8502 instruction->GetFieldType(),
8503 instruction->GetFieldIndex(),
8504 instruction->GetDexPc(),
8505 calling_convention);
8506}
8507
8508void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8509 HUnresolvedInstanceFieldSet* instruction) {
8510 FieldAccessCallingConventionMIPS calling_convention;
8511 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8512 instruction->GetFieldType(),
8513 calling_convention);
8514}
8515
8516void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8517 HUnresolvedInstanceFieldSet* instruction) {
8518 FieldAccessCallingConventionMIPS calling_convention;
8519 codegen_->GenerateUnresolvedFieldAccess(instruction,
8520 instruction->GetFieldType(),
8521 instruction->GetFieldIndex(),
8522 instruction->GetDexPc(),
8523 calling_convention);
8524}
8525
8526void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8527 HUnresolvedStaticFieldGet* instruction) {
8528 FieldAccessCallingConventionMIPS calling_convention;
8529 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8530 instruction->GetFieldType(),
8531 calling_convention);
8532}
8533
8534void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8535 HUnresolvedStaticFieldGet* instruction) {
8536 FieldAccessCallingConventionMIPS calling_convention;
8537 codegen_->GenerateUnresolvedFieldAccess(instruction,
8538 instruction->GetFieldType(),
8539 instruction->GetFieldIndex(),
8540 instruction->GetDexPc(),
8541 calling_convention);
8542}
8543
8544void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8545 HUnresolvedStaticFieldSet* instruction) {
8546 FieldAccessCallingConventionMIPS calling_convention;
8547 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8548 instruction->GetFieldType(),
8549 calling_convention);
8550}
8551
8552void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8553 HUnresolvedStaticFieldSet* instruction) {
8554 FieldAccessCallingConventionMIPS calling_convention;
8555 codegen_->GenerateUnresolvedFieldAccess(instruction,
8556 instruction->GetFieldType(),
8557 instruction->GetFieldIndex(),
8558 instruction->GetDexPc(),
8559 calling_convention);
8560}
8561
8562void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008563 LocationSummary* locations =
8564 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Lena Djokicca8c2952017-05-29 11:31:46 +02008565 // In suspend check slow path, usually there are no caller-save registers at all.
8566 // If SIMD instructions are present, however, we force spilling all live SIMD
8567 // registers in full width (since the runtime only saves/restores lower part).
8568 locations->SetCustomSlowPathCallerSaves(
8569 GetGraph()->HasSIMD() ? RegisterSet::AllFpu() : RegisterSet::Empty());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008570}
8571
8572void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8573 HBasicBlock* block = instruction->GetBlock();
8574 if (block->GetLoopInformation() != nullptr) {
8575 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8576 // The back edge will generate the suspend check.
8577 return;
8578 }
8579 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8580 // The goto will generate the suspend check.
8581 return;
8582 }
8583 GenerateSuspendCheck(instruction, nullptr);
8584}
8585
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008586void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8587 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008588 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008589 InvokeRuntimeCallingConvention calling_convention;
8590 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8591}
8592
8593void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008594 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008595 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8596}
8597
8598void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8599 Primitive::Type input_type = conversion->GetInputType();
8600 Primitive::Type result_type = conversion->GetResultType();
8601 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008602 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008603
8604 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
8605 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
8606 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8607 }
8608
8609 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008610 if (!isR6 &&
8611 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
8612 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008613 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008614 }
8615
8616 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8617
8618 if (call_kind == LocationSummary::kNoCall) {
8619 if (Primitive::IsFloatingPointType(input_type)) {
8620 locations->SetInAt(0, Location::RequiresFpuRegister());
8621 } else {
8622 locations->SetInAt(0, Location::RequiresRegister());
8623 }
8624
8625 if (Primitive::IsFloatingPointType(result_type)) {
8626 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8627 } else {
8628 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8629 }
8630 } else {
8631 InvokeRuntimeCallingConvention calling_convention;
8632
8633 if (Primitive::IsFloatingPointType(input_type)) {
8634 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8635 } else {
8636 DCHECK_EQ(input_type, Primitive::kPrimLong);
8637 locations->SetInAt(0, Location::RegisterPairLocation(
8638 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8639 }
8640
8641 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8642 }
8643}
8644
8645void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8646 LocationSummary* locations = conversion->GetLocations();
8647 Primitive::Type result_type = conversion->GetResultType();
8648 Primitive::Type input_type = conversion->GetInputType();
8649 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008650 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008651
8652 DCHECK_NE(input_type, result_type);
8653
8654 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8655 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8656 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8657 Register src = locations->InAt(0).AsRegister<Register>();
8658
Alexey Frunzea871ef12016-06-27 15:20:11 -07008659 if (dst_low != src) {
8660 __ Move(dst_low, src);
8661 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008662 __ Sra(dst_high, src, 31);
8663 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8664 Register dst = locations->Out().AsRegister<Register>();
8665 Register src = (input_type == Primitive::kPrimLong)
8666 ? locations->InAt(0).AsRegisterPairLow<Register>()
8667 : locations->InAt(0).AsRegister<Register>();
8668
8669 switch (result_type) {
8670 case Primitive::kPrimChar:
8671 __ Andi(dst, src, 0xFFFF);
8672 break;
8673 case Primitive::kPrimByte:
8674 if (has_sign_extension) {
8675 __ Seb(dst, src);
8676 } else {
8677 __ Sll(dst, src, 24);
8678 __ Sra(dst, dst, 24);
8679 }
8680 break;
8681 case Primitive::kPrimShort:
8682 if (has_sign_extension) {
8683 __ Seh(dst, src);
8684 } else {
8685 __ Sll(dst, src, 16);
8686 __ Sra(dst, dst, 16);
8687 }
8688 break;
8689 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008690 if (dst != src) {
8691 __ Move(dst, src);
8692 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008693 break;
8694
8695 default:
8696 LOG(FATAL) << "Unexpected type conversion from " << input_type
8697 << " to " << result_type;
8698 }
8699 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008700 if (input_type == Primitive::kPrimLong) {
8701 if (isR6) {
8702 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8703 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8704 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8705 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8706 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8707 __ Mtc1(src_low, FTMP);
8708 __ Mthc1(src_high, FTMP);
8709 if (result_type == Primitive::kPrimFloat) {
8710 __ Cvtsl(dst, FTMP);
8711 } else {
8712 __ Cvtdl(dst, FTMP);
8713 }
8714 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008715 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8716 : kQuickL2d;
8717 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008718 if (result_type == Primitive::kPrimFloat) {
8719 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8720 } else {
8721 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8722 }
8723 }
8724 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008725 Register src = locations->InAt(0).AsRegister<Register>();
8726 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8727 __ Mtc1(src, FTMP);
8728 if (result_type == Primitive::kPrimFloat) {
8729 __ Cvtsw(dst, FTMP);
8730 } else {
8731 __ Cvtdw(dst, FTMP);
8732 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008733 }
8734 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8735 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008736
8737 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8738 // value of the output type if the input is outside of the range after the truncation or
8739 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8740 // results. This matches the desired float/double-to-int/long conversion exactly.
8741 //
8742 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8743 // value when the input is either a NaN or is outside of the range of the output type
8744 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8745 // the same result.
8746 //
8747 // The code takes care of the different behaviors by first comparing the input to the
8748 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8749 // If the input is greater than or equal to the minimum, it procedes to the truncate
8750 // instruction, which will handle such an input the same way irrespective of NAN2008.
8751 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8752 // in order to return either zero or the minimum value.
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008753 if (result_type == Primitive::kPrimLong) {
8754 if (isR6) {
8755 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8756 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8757 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8758 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8759 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008760
8761 if (input_type == Primitive::kPrimFloat) {
8762 __ TruncLS(FTMP, src);
8763 } else {
8764 __ TruncLD(FTMP, src);
8765 }
8766 __ Mfc1(dst_low, FTMP);
8767 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008768 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008769 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8770 : kQuickD2l;
8771 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008772 if (input_type == Primitive::kPrimFloat) {
8773 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8774 } else {
8775 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8776 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008777 }
8778 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008779 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8780 Register dst = locations->Out().AsRegister<Register>();
8781 MipsLabel truncate;
8782 MipsLabel done;
8783
Lena Djokicf4e23a82017-05-09 15:43:45 +02008784 if (!isR6) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008785 if (input_type == Primitive::kPrimFloat) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008786 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8787 __ LoadConst32(TMP, min_val);
8788 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008789 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008790 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8791 __ LoadConst32(TMP, High32Bits(min_val));
8792 __ Mtc1(ZERO, FTMP);
8793 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008794 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008795
8796 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008797 __ ColeS(0, FTMP, src);
8798 } else {
8799 __ ColeD(0, FTMP, src);
8800 }
8801 __ Bc1t(0, &truncate);
8802
8803 if (input_type == Primitive::kPrimFloat) {
8804 __ CeqS(0, src, src);
8805 } else {
8806 __ CeqD(0, src, src);
8807 }
8808 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8809 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008810
8811 __ B(&done);
8812
8813 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008814 }
8815
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008816 if (input_type == Primitive::kPrimFloat) {
8817 __ TruncWS(FTMP, src);
8818 } else {
8819 __ TruncWD(FTMP, src);
8820 }
8821 __ Mfc1(dst, FTMP);
8822
Lena Djokicf4e23a82017-05-09 15:43:45 +02008823 if (!isR6) {
8824 __ Bind(&done);
8825 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008826 }
8827 } else if (Primitive::IsFloatingPointType(result_type) &&
8828 Primitive::IsFloatingPointType(input_type)) {
8829 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8830 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8831 if (result_type == Primitive::kPrimFloat) {
8832 __ Cvtsd(dst, src);
8833 } else {
8834 __ Cvtds(dst, src);
8835 }
8836 } else {
8837 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8838 << " to " << result_type;
8839 }
8840}
8841
8842void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8843 HandleShift(ushr);
8844}
8845
8846void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8847 HandleShift(ushr);
8848}
8849
8850void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8851 HandleBinaryOp(instruction);
8852}
8853
8854void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8855 HandleBinaryOp(instruction);
8856}
8857
8858void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8859 // Nothing to do, this should be removed during prepare for register allocator.
8860 LOG(FATAL) << "Unreachable";
8861}
8862
8863void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8864 // Nothing to do, this should be removed during prepare for register allocator.
8865 LOG(FATAL) << "Unreachable";
8866}
8867
8868void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008869 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008870}
8871
8872void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008873 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008874}
8875
8876void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008877 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008878}
8879
8880void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008881 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008882}
8883
8884void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008885 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008886}
8887
8888void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008889 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008890}
8891
8892void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008893 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008894}
8895
8896void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008897 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008898}
8899
8900void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008901 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008902}
8903
8904void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008905 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008906}
8907
8908void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008909 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008910}
8911
8912void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008913 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008914}
8915
8916void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008917 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008918}
8919
8920void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008921 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008922}
8923
8924void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008925 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008926}
8927
8928void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008929 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008930}
8931
8932void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008933 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008934}
8935
8936void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008937 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008938}
8939
8940void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008941 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008942}
8943
8944void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008945 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008946}
8947
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008948void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8949 LocationSummary* locations =
8950 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8951 locations->SetInAt(0, Location::RequiresRegister());
8952}
8953
Alexey Frunze96b66822016-09-10 02:32:44 -07008954void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8955 int32_t lower_bound,
8956 uint32_t num_entries,
8957 HBasicBlock* switch_block,
8958 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008959 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008960 Register temp_reg = TMP;
8961 __ Addiu32(temp_reg, value_reg, -lower_bound);
8962 // Jump to default if index is negative
8963 // Note: We don't check the case that index is positive while value < lower_bound, because in
8964 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8965 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8966
Alexey Frunze96b66822016-09-10 02:32:44 -07008967 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008968 // Jump to successors[0] if value == lower_bound.
8969 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8970 int32_t last_index = 0;
8971 for (; num_entries - last_index > 2; last_index += 2) {
8972 __ Addiu(temp_reg, temp_reg, -2);
8973 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8974 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8975 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8976 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8977 }
8978 if (num_entries - last_index == 2) {
8979 // The last missing case_value.
8980 __ Addiu(temp_reg, temp_reg, -1);
8981 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008982 }
8983
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008984 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008985 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008986 __ B(codegen_->GetLabelOf(default_block));
8987 }
8988}
8989
Alexey Frunze96b66822016-09-10 02:32:44 -07008990void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8991 Register constant_area,
8992 int32_t lower_bound,
8993 uint32_t num_entries,
8994 HBasicBlock* switch_block,
8995 HBasicBlock* default_block) {
8996 // Create a jump table.
8997 std::vector<MipsLabel*> labels(num_entries);
8998 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8999 for (uint32_t i = 0; i < num_entries; i++) {
9000 labels[i] = codegen_->GetLabelOf(successors[i]);
9001 }
9002 JumpTable* table = __ CreateJumpTable(std::move(labels));
9003
9004 // Is the value in range?
9005 __ Addiu32(TMP, value_reg, -lower_bound);
9006 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
9007 __ Sltiu(AT, TMP, num_entries);
9008 __ Beqz(AT, codegen_->GetLabelOf(default_block));
9009 } else {
9010 __ LoadConst32(AT, num_entries);
9011 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
9012 }
9013
9014 // We are in the range of the table.
9015 // Load the target address from the jump table, indexing by the value.
9016 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07009017 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07009018 __ Lw(TMP, TMP, 0);
9019 // Compute the absolute target address by adding the table start address
9020 // (the table contains offsets to targets relative to its start).
9021 __ Addu(TMP, TMP, AT);
9022 // And jump.
9023 __ Jr(TMP);
9024 __ NopIfNoReordering();
9025}
9026
9027void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
9028 int32_t lower_bound = switch_instr->GetStartValue();
9029 uint32_t num_entries = switch_instr->GetNumEntries();
9030 LocationSummary* locations = switch_instr->GetLocations();
9031 Register value_reg = locations->InAt(0).AsRegister<Register>();
9032 HBasicBlock* switch_block = switch_instr->GetBlock();
9033 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9034
9035 if (codegen_->GetInstructionSetFeatures().IsR6() &&
9036 num_entries > kPackedSwitchJumpTableThreshold) {
9037 // R6 uses PC-relative addressing to access the jump table.
9038 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
9039 // the jump table and it is implemented by changing HPackedSwitch to
9040 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
9041 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
9042 GenTableBasedPackedSwitch(value_reg,
9043 ZERO,
9044 lower_bound,
9045 num_entries,
9046 switch_block,
9047 default_block);
9048 } else {
9049 GenPackedSwitchWithCompares(value_reg,
9050 lower_bound,
9051 num_entries,
9052 switch_block,
9053 default_block);
9054 }
9055}
9056
9057void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9058 LocationSummary* locations =
9059 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
9060 locations->SetInAt(0, Location::RequiresRegister());
9061 // Constant area pointer (HMipsComputeBaseMethodAddress).
9062 locations->SetInAt(1, Location::RequiresRegister());
9063}
9064
9065void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
9066 int32_t lower_bound = switch_instr->GetStartValue();
9067 uint32_t num_entries = switch_instr->GetNumEntries();
9068 LocationSummary* locations = switch_instr->GetLocations();
9069 Register value_reg = locations->InAt(0).AsRegister<Register>();
9070 Register constant_area = locations->InAt(1).AsRegister<Register>();
9071 HBasicBlock* switch_block = switch_instr->GetBlock();
9072 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
9073
9074 // This is an R2-only path. HPackedSwitch has been changed to
9075 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
9076 // required to address the jump table relative to PC.
9077 GenTableBasedPackedSwitch(value_reg,
9078 constant_area,
9079 lower_bound,
9080 num_entries,
9081 switch_block,
9082 default_block);
9083}
9084
Alexey Frunzee3fb2452016-05-10 16:08:05 -07009085void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
9086 HMipsComputeBaseMethodAddress* insn) {
9087 LocationSummary* locations =
9088 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
9089 locations->SetOut(Location::RequiresRegister());
9090}
9091
9092void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
9093 HMipsComputeBaseMethodAddress* insn) {
9094 LocationSummary* locations = insn->GetLocations();
9095 Register reg = locations->Out().AsRegister<Register>();
9096
9097 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
9098
9099 // Generate a dummy PC-relative call to obtain PC.
9100 __ Nal();
9101 // Grab the return address off RA.
9102 __ Move(reg, RA);
9103
9104 // Remember this offset (the obtained PC value) for later use with constant area.
9105 __ BindPcRelBaseLabel();
9106}
9107
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009108void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9109 // The trampoline uses the same calling convention as dex calling conventions,
9110 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
9111 // the method_idx.
9112 HandleInvoke(invoke);
9113}
9114
9115void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
9116 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
9117}
9118
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009119void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9120 LocationSummary* locations =
9121 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
9122 locations->SetInAt(0, Location::RequiresRegister());
9123 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009124}
9125
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009126void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
9127 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00009128 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009129 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009130 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009131 __ LoadFromOffset(kLoadWord,
9132 locations->Out().AsRegister<Register>(),
9133 locations->InAt(0).AsRegister<Register>(),
9134 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009135 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009136 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00009137 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00009138 __ LoadFromOffset(kLoadWord,
9139 locations->Out().AsRegister<Register>(),
9140 locations->InAt(0).AsRegister<Register>(),
9141 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01009142 __ LoadFromOffset(kLoadWord,
9143 locations->Out().AsRegister<Register>(),
9144 locations->Out().AsRegister<Register>(),
9145 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00009146 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00009147}
9148
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02009149#undef __
9150#undef QUICK_ENTRY_POINT
9151
9152} // namespace mips
9153} // namespace art