blob: b39d412ac27624ec63b4329a616a6c56067d6c6d [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
19#include "arch/mips/entrypoints_direct_mips.h"
20#include "arch/mips/instruction_set_features_mips.h"
21#include "art_method.h"
Chris Larsen701566a2015-10-27 15:29:13 -070022#include "code_generator_utils.h"
Vladimir Marko3a21e382016-09-02 12:38:38 +010023#include "compiled_method.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020024#include "entrypoints/quick/quick_entrypoints.h"
25#include "entrypoints/quick/quick_entrypoints_enum.h"
26#include "gc/accounting/card_table.h"
27#include "intrinsics.h"
Chris Larsen701566a2015-10-27 15:29:13 -070028#include "intrinsics_mips.h"
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020029#include "mirror/array-inl.h"
30#include "mirror/class-inl.h"
31#include "offsets.h"
32#include "thread.h"
33#include "utils/assembler.h"
34#include "utils/mips/assembler_mips.h"
35#include "utils/stack_checks.h"
36
37namespace art {
38namespace mips {
39
40static constexpr int kCurrentMethodStackOffset = 0;
41static constexpr Register kMethodRegisterArgument = A0;
42
Goran Jakovljevicf652cec2015-08-25 16:11:42 +020043Location MipsReturnLocation(Primitive::Type return_type) {
44 switch (return_type) {
45 case Primitive::kPrimBoolean:
46 case Primitive::kPrimByte:
47 case Primitive::kPrimChar:
48 case Primitive::kPrimShort:
49 case Primitive::kPrimInt:
50 case Primitive::kPrimNot:
51 return Location::RegisterLocation(V0);
52
53 case Primitive::kPrimLong:
54 return Location::RegisterPairLocation(V0, V1);
55
56 case Primitive::kPrimFloat:
57 case Primitive::kPrimDouble:
58 return Location::FpuRegisterLocation(F0);
59
60 case Primitive::kPrimVoid:
61 return Location();
62 }
63 UNREACHABLE();
64}
65
66Location InvokeDexCallingConventionVisitorMIPS::GetReturnLocation(Primitive::Type type) const {
67 return MipsReturnLocation(type);
68}
69
70Location InvokeDexCallingConventionVisitorMIPS::GetMethodLocation() const {
71 return Location::RegisterLocation(kMethodRegisterArgument);
72}
73
74Location InvokeDexCallingConventionVisitorMIPS::GetNextLocation(Primitive::Type type) {
75 Location next_location;
76
77 switch (type) {
78 case Primitive::kPrimBoolean:
79 case Primitive::kPrimByte:
80 case Primitive::kPrimChar:
81 case Primitive::kPrimShort:
82 case Primitive::kPrimInt:
83 case Primitive::kPrimNot: {
84 uint32_t gp_index = gp_index_++;
85 if (gp_index < calling_convention.GetNumberOfRegisters()) {
86 next_location = Location::RegisterLocation(calling_convention.GetRegisterAt(gp_index));
87 } else {
88 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
89 next_location = Location::StackSlot(stack_offset);
90 }
91 break;
92 }
93
94 case Primitive::kPrimLong: {
95 uint32_t gp_index = gp_index_;
96 gp_index_ += 2;
97 if (gp_index + 1 < calling_convention.GetNumberOfRegisters()) {
Alexey Frunze1b8464d2016-11-12 17:22:05 -080098 Register reg = calling_convention.GetRegisterAt(gp_index);
99 if (reg == A1 || reg == A3) {
100 gp_index_++; // Skip A1(A3), and use A2_A3(T0_T1) instead.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200101 gp_index++;
102 }
103 Register low_even = calling_convention.GetRegisterAt(gp_index);
104 Register high_odd = calling_convention.GetRegisterAt(gp_index + 1);
105 DCHECK_EQ(low_even + 1, high_odd);
106 next_location = Location::RegisterPairLocation(low_even, high_odd);
107 } else {
108 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
109 next_location = Location::DoubleStackSlot(stack_offset);
110 }
111 break;
112 }
113
114 // Note: both float and double types are stored in even FPU registers. On 32 bit FPU, double
115 // will take up the even/odd pair, while floats are stored in even regs only.
116 // On 64 bit FPU, both double and float are stored in even registers only.
117 case Primitive::kPrimFloat:
118 case Primitive::kPrimDouble: {
119 uint32_t float_index = float_index_++;
120 if (float_index < calling_convention.GetNumberOfFpuRegisters()) {
121 next_location = Location::FpuRegisterLocation(
122 calling_convention.GetFpuRegisterAt(float_index));
123 } else {
124 size_t stack_offset = calling_convention.GetStackOffsetOf(stack_index_);
125 next_location = Primitive::Is64BitType(type) ? Location::DoubleStackSlot(stack_offset)
126 : Location::StackSlot(stack_offset);
127 }
128 break;
129 }
130
131 case Primitive::kPrimVoid:
132 LOG(FATAL) << "Unexpected parameter type " << type;
133 break;
134 }
135
136 // Space on the stack is reserved for all arguments.
137 stack_index_ += Primitive::Is64BitType(type) ? 2 : 1;
138
139 return next_location;
140}
141
142Location InvokeRuntimeCallingConvention::GetReturnLocation(Primitive::Type type) {
143 return MipsReturnLocation(type);
144}
145
Roland Levillain7cbd27f2016-08-11 23:53:33 +0100146// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
147#define __ down_cast<CodeGeneratorMIPS*>(codegen)->GetAssembler()-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -0700148#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200149
150class BoundsCheckSlowPathMIPS : public SlowPathCodeMIPS {
151 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000152 explicit BoundsCheckSlowPathMIPS(HBoundsCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200153
154 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
155 LocationSummary* locations = instruction_->GetLocations();
156 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
157 __ Bind(GetEntryLabel());
158 if (instruction_->CanThrowIntoCatchBlock()) {
159 // Live registers will be restored in the catch block if caught.
160 SaveLiveRegisters(codegen, instruction_->GetLocations());
161 }
162 // We're moving two locations to locations that could overlap, so we need a parallel
163 // move resolver.
164 InvokeRuntimeCallingConvention calling_convention;
165 codegen->EmitParallelMoves(locations->InAt(0),
166 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
167 Primitive::kPrimInt,
168 locations->InAt(1),
169 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
170 Primitive::kPrimInt);
Serban Constantinescufca16662016-07-14 09:21:59 +0100171 QuickEntrypointEnum entrypoint = instruction_->AsBoundsCheck()->IsStringCharAt()
172 ? kQuickThrowStringBounds
173 : kQuickThrowArrayBounds;
174 mips_codegen->InvokeRuntime(entrypoint, instruction_, instruction_->GetDexPc(), this);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +0100175 CheckEntrypointTypes<kQuickThrowStringBounds, void, int32_t, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200176 CheckEntrypointTypes<kQuickThrowArrayBounds, void, int32_t, int32_t>();
177 }
178
179 bool IsFatal() const OVERRIDE { return true; }
180
181 const char* GetDescription() const OVERRIDE { return "BoundsCheckSlowPathMIPS"; }
182
183 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200184 DISALLOW_COPY_AND_ASSIGN(BoundsCheckSlowPathMIPS);
185};
186
187class DivZeroCheckSlowPathMIPS : public SlowPathCodeMIPS {
188 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000189 explicit DivZeroCheckSlowPathMIPS(HDivZeroCheck* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200190
191 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
192 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
193 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100194 mips_codegen->InvokeRuntime(kQuickThrowDivZero, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200195 CheckEntrypointTypes<kQuickThrowDivZero, void, void>();
196 }
197
198 bool IsFatal() const OVERRIDE { return true; }
199
200 const char* GetDescription() const OVERRIDE { return "DivZeroCheckSlowPathMIPS"; }
201
202 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200203 DISALLOW_COPY_AND_ASSIGN(DivZeroCheckSlowPathMIPS);
204};
205
206class LoadClassSlowPathMIPS : public SlowPathCodeMIPS {
207 public:
208 LoadClassSlowPathMIPS(HLoadClass* cls,
209 HInstruction* at,
210 uint32_t dex_pc,
211 bool do_clinit)
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000212 : SlowPathCodeMIPS(at), cls_(cls), dex_pc_(dex_pc), do_clinit_(do_clinit) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200213 DCHECK(at->IsLoadClass() || at->IsClinitCheck());
214 }
215
216 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000217 LocationSummary* locations = instruction_->GetLocations();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700218 Location out = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200219 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700220 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
221 const bool r2_baker_or_no_read_barriers = !isR6 && (!kUseReadBarrier || kUseBakerReadBarrier);
222 InvokeRuntimeCallingConvention calling_convention;
223 DCHECK_EQ(instruction_->IsLoadClass(), cls_ == instruction_);
224 const bool is_load_class_bss_entry =
225 (cls_ == instruction_) && (cls_->GetLoadKind() == HLoadClass::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200226 __ Bind(GetEntryLabel());
227 SaveLiveRegisters(codegen, locations);
228
Alexey Frunzec61c0762017-04-10 13:54:23 -0700229 // For HLoadClass/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
230 Register entry_address = kNoRegister;
231 if (is_load_class_bss_entry && r2_baker_or_no_read_barriers) {
232 Register temp = locations->GetTemp(0).AsRegister<Register>();
233 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
234 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
235 // kSaveEverything call.
236 entry_address = temp_is_a0 ? out.AsRegister<Register>() : temp;
237 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
238 if (temp_is_a0) {
239 __ Move(entry_address, temp);
240 }
241 }
242
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000243 dex::TypeIndex type_index = cls_->GetTypeIndex();
244 __ LoadConst32(calling_convention.GetRegisterAt(0), type_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100245 QuickEntrypointEnum entrypoint = do_clinit_ ? kQuickInitializeStaticStorage
246 : kQuickInitializeType;
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000247 mips_codegen->InvokeRuntime(entrypoint, instruction_, dex_pc_, this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200248 if (do_clinit_) {
249 CheckEntrypointTypes<kQuickInitializeStaticStorage, void*, uint32_t>();
250 } else {
251 CheckEntrypointTypes<kQuickInitializeType, void*, uint32_t>();
252 }
253
Alexey Frunzec61c0762017-04-10 13:54:23 -0700254 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
255 if (is_load_class_bss_entry && r2_baker_or_no_read_barriers) {
256 // The class entry address was preserved in `entry_address` thanks to kSaveEverything.
257 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(0), entry_address, 0);
258 }
259
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200260 // Move the class to the desired location.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200261 if (out.IsValid()) {
262 DCHECK(out.IsRegister() && !locations->GetLiveRegisters()->ContainsCoreRegister(out.reg()));
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000263 Primitive::Type type = instruction_->GetType();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700264 mips_codegen->MoveLocation(out,
265 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
266 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200267 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200268 RestoreLiveRegisters(codegen, locations);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700269
270 // For HLoadClass/kBssEntry, store the resolved class to the BSS entry.
271 if (is_load_class_bss_entry && !r2_baker_or_no_read_barriers) {
272 // For non-Baker read barriers (or on R6), we need to re-calculate the address of
273 // the class entry.
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000274 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000275 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +0000276 mips_codegen->NewTypeBssEntryPatch(cls_->GetDexFile(), type_index);
Alexey Frunze6b892cd2017-01-03 17:11:38 -0800277 bool reordering = __ SetReorder(false);
278 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
279 __ StoreToOffset(kStoreWord, out.AsRegister<Register>(), TMP, /* placeholder */ 0x5678);
280 __ SetReorder(reordering);
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200282 __ B(GetExitLabel());
283 }
284
285 const char* GetDescription() const OVERRIDE { return "LoadClassSlowPathMIPS"; }
286
287 private:
288 // The class this slow path will load.
289 HLoadClass* const cls_;
290
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200291 // The dex PC of `at_`.
292 const uint32_t dex_pc_;
293
294 // Whether to initialize the class.
295 const bool do_clinit_;
296
297 DISALLOW_COPY_AND_ASSIGN(LoadClassSlowPathMIPS);
298};
299
300class LoadStringSlowPathMIPS : public SlowPathCodeMIPS {
301 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000302 explicit LoadStringSlowPathMIPS(HLoadString* instruction) : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200303
304 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Alexey Frunzec61c0762017-04-10 13:54:23 -0700305 DCHECK(instruction_->IsLoadString());
306 DCHECK_EQ(instruction_->AsLoadString()->GetLoadKind(), HLoadString::LoadKind::kBssEntry);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200307 LocationSummary* locations = instruction_->GetLocations();
308 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
Alexey Frunzec61c0762017-04-10 13:54:23 -0700309 HLoadString* load = instruction_->AsLoadString();
310 const dex::StringIndex string_index = load->GetStringIndex();
311 Register out = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200312 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Alexey Frunzec61c0762017-04-10 13:54:23 -0700313 const bool isR6 = mips_codegen->GetInstructionSetFeatures().IsR6();
314 const bool r2_baker_or_no_read_barriers = !isR6 && (!kUseReadBarrier || kUseBakerReadBarrier);
315 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200316 __ Bind(GetEntryLabel());
317 SaveLiveRegisters(codegen, locations);
318
Alexey Frunzec61c0762017-04-10 13:54:23 -0700319 // For HLoadString/kBssEntry/kSaveEverything, make sure we preserve the address of the entry.
320 Register entry_address = kNoRegister;
321 if (r2_baker_or_no_read_barriers) {
322 Register temp = locations->GetTemp(0).AsRegister<Register>();
323 bool temp_is_a0 = (temp == calling_convention.GetRegisterAt(0));
324 // In the unlucky case that `temp` is A0, we preserve the address in `out` across the
325 // kSaveEverything call.
326 entry_address = temp_is_a0 ? out : temp;
327 DCHECK_NE(entry_address, calling_convention.GetRegisterAt(0));
328 if (temp_is_a0) {
329 __ Move(entry_address, temp);
330 }
331 }
332
Vladimir Marko6bec91c2017-01-09 15:03:12 +0000333 __ LoadConst32(calling_convention.GetRegisterAt(0), string_index.index_);
Serban Constantinescufca16662016-07-14 09:21:59 +0100334 mips_codegen->InvokeRuntime(kQuickResolveString, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200335 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Alexey Frunzec61c0762017-04-10 13:54:23 -0700336
337 // Store the resolved string to the BSS entry.
338 if (r2_baker_or_no_read_barriers) {
339 // The string entry address was preserved in `entry_address` thanks to kSaveEverything.
340 __ StoreToOffset(kStoreWord, calling_convention.GetRegisterAt(0), entry_address, 0);
341 }
342
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200343 Primitive::Type type = instruction_->GetType();
344 mips_codegen->MoveLocation(locations->Out(),
Alexey Frunzec61c0762017-04-10 13:54:23 -0700345 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200346 type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200347 RestoreLiveRegisters(codegen, locations);
Vladimir Markoaad75c62016-10-03 08:46:48 +0000348
Alexey Frunzec61c0762017-04-10 13:54:23 -0700349 // Store the resolved string to the BSS entry.
350 if (!r2_baker_or_no_read_barriers) {
351 // For non-Baker read barriers (or on R6), we need to re-calculate the address of
352 // the string entry.
353 Register base = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
354 CodeGeneratorMIPS::PcRelativePatchInfo* info =
355 mips_codegen->NewPcRelativeStringPatch(load->GetDexFile(), string_index);
356 bool reordering = __ SetReorder(false);
357 mips_codegen->EmitPcRelativeAddressPlaceholderHigh(info, TMP, base);
358 __ StoreToOffset(kStoreWord, out, TMP, /* placeholder */ 0x5678);
359 __ SetReorder(reordering);
360 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200361 __ B(GetExitLabel());
362 }
363
364 const char* GetDescription() const OVERRIDE { return "LoadStringSlowPathMIPS"; }
365
366 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200367 DISALLOW_COPY_AND_ASSIGN(LoadStringSlowPathMIPS);
368};
369
370class NullCheckSlowPathMIPS : public SlowPathCodeMIPS {
371 public:
David Srbecky9cd6d372016-02-09 15:24:47 +0000372 explicit NullCheckSlowPathMIPS(HNullCheck* instr) : SlowPathCodeMIPS(instr) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200373
374 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
375 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
376 __ Bind(GetEntryLabel());
377 if (instruction_->CanThrowIntoCatchBlock()) {
378 // Live registers will be restored in the catch block if caught.
379 SaveLiveRegisters(codegen, instruction_->GetLocations());
380 }
Serban Constantinescufca16662016-07-14 09:21:59 +0100381 mips_codegen->InvokeRuntime(kQuickThrowNullPointer,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200382 instruction_,
383 instruction_->GetDexPc(),
Serban Constantinescufca16662016-07-14 09:21:59 +0100384 this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200385 CheckEntrypointTypes<kQuickThrowNullPointer, void, void>();
386 }
387
388 bool IsFatal() const OVERRIDE { return true; }
389
390 const char* GetDescription() const OVERRIDE { return "NullCheckSlowPathMIPS"; }
391
392 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200393 DISALLOW_COPY_AND_ASSIGN(NullCheckSlowPathMIPS);
394};
395
396class SuspendCheckSlowPathMIPS : public SlowPathCodeMIPS {
397 public:
398 SuspendCheckSlowPathMIPS(HSuspendCheck* instruction, HBasicBlock* successor)
David Srbecky9cd6d372016-02-09 15:24:47 +0000399 : SlowPathCodeMIPS(instruction), successor_(successor) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200400
401 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
402 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
403 __ Bind(GetEntryLabel());
Serban Constantinescufca16662016-07-14 09:21:59 +0100404 mips_codegen->InvokeRuntime(kQuickTestSuspend, instruction_, instruction_->GetDexPc(), this);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200405 CheckEntrypointTypes<kQuickTestSuspend, void, void>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200406 if (successor_ == nullptr) {
407 __ B(GetReturnLabel());
408 } else {
409 __ B(mips_codegen->GetLabelOf(successor_));
410 }
411 }
412
413 MipsLabel* GetReturnLabel() {
414 DCHECK(successor_ == nullptr);
415 return &return_label_;
416 }
417
418 const char* GetDescription() const OVERRIDE { return "SuspendCheckSlowPathMIPS"; }
419
420 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200421 // If not null, the block to branch to after the suspend check.
422 HBasicBlock* const successor_;
423
424 // If `successor_` is null, the label to branch to after the suspend check.
425 MipsLabel return_label_;
426
427 DISALLOW_COPY_AND_ASSIGN(SuspendCheckSlowPathMIPS);
428};
429
430class TypeCheckSlowPathMIPS : public SlowPathCodeMIPS {
431 public:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800432 explicit TypeCheckSlowPathMIPS(HInstruction* instruction, bool is_fatal)
433 : SlowPathCodeMIPS(instruction), is_fatal_(is_fatal) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200434
435 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
436 LocationSummary* locations = instruction_->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200437 uint32_t dex_pc = instruction_->GetDexPc();
438 DCHECK(instruction_->IsCheckCast()
439 || !locations->GetLiveRegisters()->ContainsCoreRegister(locations->Out().reg()));
440 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
441
442 __ Bind(GetEntryLabel());
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800443 if (!is_fatal_) {
444 SaveLiveRegisters(codegen, locations);
445 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200446
447 // We're moving two locations to locations that could overlap, so we need a parallel
448 // move resolver.
449 InvokeRuntimeCallingConvention calling_convention;
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800450 codegen->EmitParallelMoves(locations->InAt(0),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200451 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
452 Primitive::kPrimNot,
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800453 locations->InAt(1),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200454 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
455 Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200456 if (instruction_->IsInstanceOf()) {
Serban Constantinescufca16662016-07-14 09:21:59 +0100457 mips_codegen->InvokeRuntime(kQuickInstanceofNonTrivial, instruction_, dex_pc, this);
Mathieu Chartier9fd8c602016-11-14 14:38:53 -0800458 CheckEntrypointTypes<kQuickInstanceofNonTrivial, size_t, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200459 Primitive::Type ret_type = instruction_->GetType();
460 Location ret_loc = calling_convention.GetReturnLocation(ret_type);
461 mips_codegen->MoveLocation(locations->Out(), ret_loc, ret_type);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200462 } else {
463 DCHECK(instruction_->IsCheckCast());
Mathieu Chartierb99f4d62016-11-07 16:17:26 -0800464 mips_codegen->InvokeRuntime(kQuickCheckInstanceOf, instruction_, dex_pc, this);
465 CheckEntrypointTypes<kQuickCheckInstanceOf, void, mirror::Object*, mirror::Class*>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200466 }
467
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800468 if (!is_fatal_) {
469 RestoreLiveRegisters(codegen, locations);
470 __ B(GetExitLabel());
471 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200472 }
473
474 const char* GetDescription() const OVERRIDE { return "TypeCheckSlowPathMIPS"; }
475
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800476 bool IsFatal() const OVERRIDE { return is_fatal_; }
477
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200478 private:
Alexey Frunze66b69ad2017-02-24 00:51:44 -0800479 const bool is_fatal_;
480
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200481 DISALLOW_COPY_AND_ASSIGN(TypeCheckSlowPathMIPS);
482};
483
484class DeoptimizationSlowPathMIPS : public SlowPathCodeMIPS {
485 public:
Aart Bik42249c32016-01-07 15:33:50 -0800486 explicit DeoptimizationSlowPathMIPS(HDeoptimize* instruction)
David Srbecky9cd6d372016-02-09 15:24:47 +0000487 : SlowPathCodeMIPS(instruction) {}
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200488
489 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
Aart Bik42249c32016-01-07 15:33:50 -0800490 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200491 __ Bind(GetEntryLabel());
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100492 LocationSummary* locations = instruction_->GetLocations();
493 SaveLiveRegisters(codegen, locations);
494 InvokeRuntimeCallingConvention calling_convention;
495 __ LoadConst32(calling_convention.GetRegisterAt(0),
496 static_cast<uint32_t>(instruction_->AsDeoptimize()->GetDeoptimizationKind()));
Serban Constantinescufca16662016-07-14 09:21:59 +0100497 mips_codegen->InvokeRuntime(kQuickDeoptimize, instruction_, instruction_->GetDexPc(), this);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +0100498 CheckEntrypointTypes<kQuickDeoptimize, void, DeoptimizationKind>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200499 }
500
501 const char* GetDescription() const OVERRIDE { return "DeoptimizationSlowPathMIPS"; }
502
503 private:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +0200504 DISALLOW_COPY_AND_ASSIGN(DeoptimizationSlowPathMIPS);
505};
506
Alexey Frunze15958152017-02-09 19:08:30 -0800507class ArraySetSlowPathMIPS : public SlowPathCodeMIPS {
508 public:
509 explicit ArraySetSlowPathMIPS(HInstruction* instruction) : SlowPathCodeMIPS(instruction) {}
510
511 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
512 LocationSummary* locations = instruction_->GetLocations();
513 __ Bind(GetEntryLabel());
514 SaveLiveRegisters(codegen, locations);
515
516 InvokeRuntimeCallingConvention calling_convention;
517 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
518 parallel_move.AddMove(
519 locations->InAt(0),
520 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
521 Primitive::kPrimNot,
522 nullptr);
523 parallel_move.AddMove(
524 locations->InAt(1),
525 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
526 Primitive::kPrimInt,
527 nullptr);
528 parallel_move.AddMove(
529 locations->InAt(2),
530 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
531 Primitive::kPrimNot,
532 nullptr);
533 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
534
535 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
536 mips_codegen->InvokeRuntime(kQuickAputObject, instruction_, instruction_->GetDexPc(), this);
537 CheckEntrypointTypes<kQuickAputObject, void, mirror::Array*, int32_t, mirror::Object*>();
538 RestoreLiveRegisters(codegen, locations);
539 __ B(GetExitLabel());
540 }
541
542 const char* GetDescription() const OVERRIDE { return "ArraySetSlowPathMIPS"; }
543
544 private:
545 DISALLOW_COPY_AND_ASSIGN(ArraySetSlowPathMIPS);
546};
547
548// Slow path marking an object reference `ref` during a read
549// barrier. The field `obj.field` in the object `obj` holding this
550// reference does not get updated by this slow path after marking (see
551// ReadBarrierMarkAndUpdateFieldSlowPathMIPS below for that).
552//
553// This means that after the execution of this slow path, `ref` will
554// always be up-to-date, but `obj.field` may not; i.e., after the
555// flip, `ref` will be a to-space reference, but `obj.field` will
556// probably still be a from-space reference (unless it gets updated by
557// another thread, or if another thread installed another object
558// reference (different from `ref`) in `obj.field`).
559//
560// If `entrypoint` is a valid location it is assumed to already be
561// holding the entrypoint. The case where the entrypoint is passed in
562// is for the GcRoot read barrier.
563class ReadBarrierMarkSlowPathMIPS : public SlowPathCodeMIPS {
564 public:
565 ReadBarrierMarkSlowPathMIPS(HInstruction* instruction,
566 Location ref,
567 Location entrypoint = Location::NoLocation())
568 : SlowPathCodeMIPS(instruction), ref_(ref), entrypoint_(entrypoint) {
569 DCHECK(kEmitCompilerReadBarrier);
570 }
571
572 const char* GetDescription() const OVERRIDE { return "ReadBarrierMarkSlowPathMIPS"; }
573
574 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
575 LocationSummary* locations = instruction_->GetLocations();
576 Register ref_reg = ref_.AsRegister<Register>();
577 DCHECK(locations->CanCall());
578 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
579 DCHECK(instruction_->IsInstanceFieldGet() ||
580 instruction_->IsStaticFieldGet() ||
581 instruction_->IsArrayGet() ||
582 instruction_->IsArraySet() ||
583 instruction_->IsLoadClass() ||
584 instruction_->IsLoadString() ||
585 instruction_->IsInstanceOf() ||
586 instruction_->IsCheckCast() ||
587 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()) ||
588 (instruction_->IsInvokeStaticOrDirect() && instruction_->GetLocations()->Intrinsified()))
589 << "Unexpected instruction in read barrier marking slow path: "
590 << instruction_->DebugName();
591
592 __ Bind(GetEntryLabel());
593 // No need to save live registers; it's taken care of by the
594 // entrypoint. Also, there is no need to update the stack mask,
595 // as this runtime call will not trigger a garbage collection.
596 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
597 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
598 (S2 <= ref_reg && ref_reg <= S7) ||
599 (ref_reg == FP)) << ref_reg;
600 // "Compact" slow path, saving two moves.
601 //
602 // Instead of using the standard runtime calling convention (input
603 // and output in A0 and V0 respectively):
604 //
605 // A0 <- ref
606 // V0 <- ReadBarrierMark(A0)
607 // ref <- V0
608 //
609 // we just use rX (the register containing `ref`) as input and output
610 // of a dedicated entrypoint:
611 //
612 // rX <- ReadBarrierMarkRegX(rX)
613 //
614 if (entrypoint_.IsValid()) {
615 mips_codegen->ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction_, this);
616 DCHECK_EQ(entrypoint_.AsRegister<Register>(), T9);
617 __ Jalr(entrypoint_.AsRegister<Register>());
618 __ NopIfNoReordering();
619 } else {
620 int32_t entry_point_offset =
621 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
622 // This runtime call does not require a stack map.
623 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
624 instruction_,
625 this,
626 /* direct */ false);
627 }
628 __ B(GetExitLabel());
629 }
630
631 private:
632 // The location (register) of the marked object reference.
633 const Location ref_;
634
635 // The location of the entrypoint if already loaded.
636 const Location entrypoint_;
637
638 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkSlowPathMIPS);
639};
640
641// Slow path marking an object reference `ref` during a read barrier,
642// and if needed, atomically updating the field `obj.field` in the
643// object `obj` holding this reference after marking (contrary to
644// ReadBarrierMarkSlowPathMIPS above, which never tries to update
645// `obj.field`).
646//
647// This means that after the execution of this slow path, both `ref`
648// and `obj.field` will be up-to-date; i.e., after the flip, both will
649// hold the same to-space reference (unless another thread installed
650// another object reference (different from `ref`) in `obj.field`).
651class ReadBarrierMarkAndUpdateFieldSlowPathMIPS : public SlowPathCodeMIPS {
652 public:
653 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(HInstruction* instruction,
654 Location ref,
655 Register obj,
656 Location field_offset,
657 Register temp1)
658 : SlowPathCodeMIPS(instruction),
659 ref_(ref),
660 obj_(obj),
661 field_offset_(field_offset),
662 temp1_(temp1) {
663 DCHECK(kEmitCompilerReadBarrier);
664 }
665
666 const char* GetDescription() const OVERRIDE {
667 return "ReadBarrierMarkAndUpdateFieldSlowPathMIPS";
668 }
669
670 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
671 LocationSummary* locations = instruction_->GetLocations();
672 Register ref_reg = ref_.AsRegister<Register>();
673 DCHECK(locations->CanCall());
674 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(ref_reg)) << ref_reg;
675 // This slow path is only used by the UnsafeCASObject intrinsic.
676 DCHECK((instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
677 << "Unexpected instruction in read barrier marking and field updating slow path: "
678 << instruction_->DebugName();
679 DCHECK(instruction_->GetLocations()->Intrinsified());
680 DCHECK_EQ(instruction_->AsInvoke()->GetIntrinsic(), Intrinsics::kUnsafeCASObject);
681 DCHECK(field_offset_.IsRegisterPair()) << field_offset_;
682
683 __ Bind(GetEntryLabel());
684
685 // Save the old reference.
686 // Note that we cannot use AT or TMP to save the old reference, as those
687 // are used by the code that follows, but we need the old reference after
688 // the call to the ReadBarrierMarkRegX entry point.
689 DCHECK_NE(temp1_, AT);
690 DCHECK_NE(temp1_, TMP);
691 __ Move(temp1_, ref_reg);
692
693 // No need to save live registers; it's taken care of by the
694 // entrypoint. Also, there is no need to update the stack mask,
695 // as this runtime call will not trigger a garbage collection.
696 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
697 DCHECK((V0 <= ref_reg && ref_reg <= T7) ||
698 (S2 <= ref_reg && ref_reg <= S7) ||
699 (ref_reg == FP)) << ref_reg;
700 // "Compact" slow path, saving two moves.
701 //
702 // Instead of using the standard runtime calling convention (input
703 // and output in A0 and V0 respectively):
704 //
705 // A0 <- ref
706 // V0 <- ReadBarrierMark(A0)
707 // ref <- V0
708 //
709 // we just use rX (the register containing `ref`) as input and output
710 // of a dedicated entrypoint:
711 //
712 // rX <- ReadBarrierMarkRegX(rX)
713 //
714 int32_t entry_point_offset =
715 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(ref_reg - 1);
716 // This runtime call does not require a stack map.
717 mips_codegen->InvokeRuntimeWithoutRecordingPcInfo(entry_point_offset,
718 instruction_,
719 this,
720 /* direct */ false);
721
722 // If the new reference is different from the old reference,
723 // update the field in the holder (`*(obj_ + field_offset_)`).
724 //
725 // Note that this field could also hold a different object, if
726 // another thread had concurrently changed it. In that case, the
727 // the compare-and-set (CAS) loop below would abort, leaving the
728 // field as-is.
729 MipsLabel done;
730 __ Beq(temp1_, ref_reg, &done);
731
732 // Update the the holder's field atomically. This may fail if
733 // mutator updates before us, but it's OK. This is achieved
734 // using a strong compare-and-set (CAS) operation with relaxed
735 // memory synchronization ordering, where the expected value is
736 // the old reference and the desired value is the new reference.
737
738 // Convenience aliases.
739 Register base = obj_;
740 // The UnsafeCASObject intrinsic uses a register pair as field
741 // offset ("long offset"), of which only the low part contains
742 // data.
743 Register offset = field_offset_.AsRegisterPairLow<Register>();
744 Register expected = temp1_;
745 Register value = ref_reg;
746 Register tmp_ptr = TMP; // Pointer to actual memory.
747 Register tmp = AT; // Value in memory.
748
749 __ Addu(tmp_ptr, base, offset);
750
751 if (kPoisonHeapReferences) {
752 __ PoisonHeapReference(expected);
753 // Do not poison `value` if it is the same register as
754 // `expected`, which has just been poisoned.
755 if (value != expected) {
756 __ PoisonHeapReference(value);
757 }
758 }
759
760 // do {
761 // tmp = [r_ptr] - expected;
762 // } while (tmp == 0 && failure([r_ptr] <- r_new_value));
763
764 bool is_r6 = mips_codegen->GetInstructionSetFeatures().IsR6();
765 MipsLabel loop_head, exit_loop;
766 __ Bind(&loop_head);
767 if (is_r6) {
768 __ LlR6(tmp, tmp_ptr);
769 } else {
770 __ LlR2(tmp, tmp_ptr);
771 }
772 __ Bne(tmp, expected, &exit_loop);
773 __ Move(tmp, value);
774 if (is_r6) {
775 __ ScR6(tmp, tmp_ptr);
776 } else {
777 __ ScR2(tmp, tmp_ptr);
778 }
779 __ Beqz(tmp, &loop_head);
780 __ Bind(&exit_loop);
781
782 if (kPoisonHeapReferences) {
783 __ UnpoisonHeapReference(expected);
784 // Do not unpoison `value` if it is the same register as
785 // `expected`, which has just been unpoisoned.
786 if (value != expected) {
787 __ UnpoisonHeapReference(value);
788 }
789 }
790
791 __ Bind(&done);
792 __ B(GetExitLabel());
793 }
794
795 private:
796 // The location (register) of the marked object reference.
797 const Location ref_;
798 // The register containing the object holding the marked object reference field.
799 const Register obj_;
800 // The location of the offset of the marked reference field within `obj_`.
801 Location field_offset_;
802
803 const Register temp1_;
804
805 DISALLOW_COPY_AND_ASSIGN(ReadBarrierMarkAndUpdateFieldSlowPathMIPS);
806};
807
808// Slow path generating a read barrier for a heap reference.
809class ReadBarrierForHeapReferenceSlowPathMIPS : public SlowPathCodeMIPS {
810 public:
811 ReadBarrierForHeapReferenceSlowPathMIPS(HInstruction* instruction,
812 Location out,
813 Location ref,
814 Location obj,
815 uint32_t offset,
816 Location index)
817 : SlowPathCodeMIPS(instruction),
818 out_(out),
819 ref_(ref),
820 obj_(obj),
821 offset_(offset),
822 index_(index) {
823 DCHECK(kEmitCompilerReadBarrier);
824 // If `obj` is equal to `out` or `ref`, it means the initial object
825 // has been overwritten by (or after) the heap object reference load
826 // to be instrumented, e.g.:
827 //
828 // __ LoadFromOffset(kLoadWord, out, out, offset);
829 // codegen_->GenerateReadBarrierSlow(instruction, out_loc, out_loc, out_loc, offset);
830 //
831 // In that case, we have lost the information about the original
832 // object, and the emitted read barrier cannot work properly.
833 DCHECK(!obj.Equals(out)) << "obj=" << obj << " out=" << out;
834 DCHECK(!obj.Equals(ref)) << "obj=" << obj << " ref=" << ref;
835 }
836
837 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
838 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
839 LocationSummary* locations = instruction_->GetLocations();
840 Register reg_out = out_.AsRegister<Register>();
841 DCHECK(locations->CanCall());
842 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
843 DCHECK(instruction_->IsInstanceFieldGet() ||
844 instruction_->IsStaticFieldGet() ||
845 instruction_->IsArrayGet() ||
846 instruction_->IsInstanceOf() ||
847 instruction_->IsCheckCast() ||
848 (instruction_->IsInvokeVirtual() && instruction_->GetLocations()->Intrinsified()))
849 << "Unexpected instruction in read barrier for heap reference slow path: "
850 << instruction_->DebugName();
851
852 __ Bind(GetEntryLabel());
853 SaveLiveRegisters(codegen, locations);
854
855 // We may have to change the index's value, but as `index_` is a
856 // constant member (like other "inputs" of this slow path),
857 // introduce a copy of it, `index`.
858 Location index = index_;
859 if (index_.IsValid()) {
860 // Handle `index_` for HArrayGet and UnsafeGetObject/UnsafeGetObjectVolatile intrinsics.
861 if (instruction_->IsArrayGet()) {
862 // Compute the actual memory offset and store it in `index`.
863 Register index_reg = index_.AsRegister<Register>();
864 DCHECK(locations->GetLiveRegisters()->ContainsCoreRegister(index_reg));
865 if (codegen->IsCoreCalleeSaveRegister(index_reg)) {
866 // We are about to change the value of `index_reg` (see the
867 // calls to art::mips::MipsAssembler::Sll and
868 // art::mips::MipsAssembler::Addiu32 below), but it has
869 // not been saved by the previous call to
870 // art::SlowPathCode::SaveLiveRegisters, as it is a
871 // callee-save register --
872 // art::SlowPathCode::SaveLiveRegisters does not consider
873 // callee-save registers, as it has been designed with the
874 // assumption that callee-save registers are supposed to be
875 // handled by the called function. So, as a callee-save
876 // register, `index_reg` _would_ eventually be saved onto
877 // the stack, but it would be too late: we would have
878 // changed its value earlier. Therefore, we manually save
879 // it here into another freely available register,
880 // `free_reg`, chosen of course among the caller-save
881 // registers (as a callee-save `free_reg` register would
882 // exhibit the same problem).
883 //
884 // Note we could have requested a temporary register from
885 // the register allocator instead; but we prefer not to, as
886 // this is a slow path, and we know we can find a
887 // caller-save register that is available.
888 Register free_reg = FindAvailableCallerSaveRegister(codegen);
889 __ Move(free_reg, index_reg);
890 index_reg = free_reg;
891 index = Location::RegisterLocation(index_reg);
892 } else {
893 // The initial register stored in `index_` has already been
894 // saved in the call to art::SlowPathCode::SaveLiveRegisters
895 // (as it is not a callee-save register), so we can freely
896 // use it.
897 }
898 // Shifting the index value contained in `index_reg` by the scale
899 // factor (2) cannot overflow in practice, as the runtime is
900 // unable to allocate object arrays with a size larger than
901 // 2^26 - 1 (that is, 2^28 - 4 bytes).
902 __ Sll(index_reg, index_reg, TIMES_4);
903 static_assert(
904 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
905 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
906 __ Addiu32(index_reg, index_reg, offset_);
907 } else {
908 // In the case of the UnsafeGetObject/UnsafeGetObjectVolatile
909 // intrinsics, `index_` is not shifted by a scale factor of 2
910 // (as in the case of ArrayGet), as it is actually an offset
911 // to an object field within an object.
912 DCHECK(instruction_->IsInvoke()) << instruction_->DebugName();
913 DCHECK(instruction_->GetLocations()->Intrinsified());
914 DCHECK((instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObject) ||
915 (instruction_->AsInvoke()->GetIntrinsic() == Intrinsics::kUnsafeGetObjectVolatile))
916 << instruction_->AsInvoke()->GetIntrinsic();
917 DCHECK_EQ(offset_, 0U);
918 DCHECK(index_.IsRegisterPair());
919 // UnsafeGet's offset location is a register pair, the low
920 // part contains the correct offset.
921 index = index_.ToLow();
922 }
923 }
924
925 // We're moving two or three locations to locations that could
926 // overlap, so we need a parallel move resolver.
927 InvokeRuntimeCallingConvention calling_convention;
928 HParallelMove parallel_move(codegen->GetGraph()->GetArena());
929 parallel_move.AddMove(ref_,
930 Location::RegisterLocation(calling_convention.GetRegisterAt(0)),
931 Primitive::kPrimNot,
932 nullptr);
933 parallel_move.AddMove(obj_,
934 Location::RegisterLocation(calling_convention.GetRegisterAt(1)),
935 Primitive::kPrimNot,
936 nullptr);
937 if (index.IsValid()) {
938 parallel_move.AddMove(index,
939 Location::RegisterLocation(calling_convention.GetRegisterAt(2)),
940 Primitive::kPrimInt,
941 nullptr);
942 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
943 } else {
944 codegen->GetMoveResolver()->EmitNativeCode(&parallel_move);
945 __ LoadConst32(calling_convention.GetRegisterAt(2), offset_);
946 }
947 mips_codegen->InvokeRuntime(kQuickReadBarrierSlow,
948 instruction_,
949 instruction_->GetDexPc(),
950 this);
951 CheckEntrypointTypes<
952 kQuickReadBarrierSlow, mirror::Object*, mirror::Object*, mirror::Object*, uint32_t>();
953 mips_codegen->Move32(out_, calling_convention.GetReturnLocation(Primitive::kPrimNot));
954
955 RestoreLiveRegisters(codegen, locations);
956 __ B(GetExitLabel());
957 }
958
959 const char* GetDescription() const OVERRIDE { return "ReadBarrierForHeapReferenceSlowPathMIPS"; }
960
961 private:
962 Register FindAvailableCallerSaveRegister(CodeGenerator* codegen) {
963 size_t ref = static_cast<int>(ref_.AsRegister<Register>());
964 size_t obj = static_cast<int>(obj_.AsRegister<Register>());
965 for (size_t i = 0, e = codegen->GetNumberOfCoreRegisters(); i < e; ++i) {
966 if (i != ref &&
967 i != obj &&
968 !codegen->IsCoreCalleeSaveRegister(i) &&
969 !codegen->IsBlockedCoreRegister(i)) {
970 return static_cast<Register>(i);
971 }
972 }
973 // We shall never fail to find a free caller-save register, as
974 // there are more than two core caller-save registers on MIPS
975 // (meaning it is possible to find one which is different from
976 // `ref` and `obj`).
977 DCHECK_GT(codegen->GetNumberOfCoreCallerSaveRegisters(), 2u);
978 LOG(FATAL) << "Could not find a free caller-save register";
979 UNREACHABLE();
980 }
981
982 const Location out_;
983 const Location ref_;
984 const Location obj_;
985 const uint32_t offset_;
986 // An additional location containing an index to an array.
987 // Only used for HArrayGet and the UnsafeGetObject &
988 // UnsafeGetObjectVolatile intrinsics.
989 const Location index_;
990
991 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForHeapReferenceSlowPathMIPS);
992};
993
994// Slow path generating a read barrier for a GC root.
995class ReadBarrierForRootSlowPathMIPS : public SlowPathCodeMIPS {
996 public:
997 ReadBarrierForRootSlowPathMIPS(HInstruction* instruction, Location out, Location root)
998 : SlowPathCodeMIPS(instruction), out_(out), root_(root) {
999 DCHECK(kEmitCompilerReadBarrier);
1000 }
1001
1002 void EmitNativeCode(CodeGenerator* codegen) OVERRIDE {
1003 LocationSummary* locations = instruction_->GetLocations();
1004 Register reg_out = out_.AsRegister<Register>();
1005 DCHECK(locations->CanCall());
1006 DCHECK(!locations->GetLiveRegisters()->ContainsCoreRegister(reg_out));
1007 DCHECK(instruction_->IsLoadClass() || instruction_->IsLoadString())
1008 << "Unexpected instruction in read barrier for GC root slow path: "
1009 << instruction_->DebugName();
1010
1011 __ Bind(GetEntryLabel());
1012 SaveLiveRegisters(codegen, locations);
1013
1014 InvokeRuntimeCallingConvention calling_convention;
1015 CodeGeneratorMIPS* mips_codegen = down_cast<CodeGeneratorMIPS*>(codegen);
1016 mips_codegen->Move32(Location::RegisterLocation(calling_convention.GetRegisterAt(0)), root_);
1017 mips_codegen->InvokeRuntime(kQuickReadBarrierForRootSlow,
1018 instruction_,
1019 instruction_->GetDexPc(),
1020 this);
1021 CheckEntrypointTypes<kQuickReadBarrierForRootSlow, mirror::Object*, GcRoot<mirror::Object>*>();
1022 mips_codegen->Move32(out_, calling_convention.GetReturnLocation(Primitive::kPrimNot));
1023
1024 RestoreLiveRegisters(codegen, locations);
1025 __ B(GetExitLabel());
1026 }
1027
1028 const char* GetDescription() const OVERRIDE { return "ReadBarrierForRootSlowPathMIPS"; }
1029
1030 private:
1031 const Location out_;
1032 const Location root_;
1033
1034 DISALLOW_COPY_AND_ASSIGN(ReadBarrierForRootSlowPathMIPS);
1035};
1036
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001037CodeGeneratorMIPS::CodeGeneratorMIPS(HGraph* graph,
1038 const MipsInstructionSetFeatures& isa_features,
1039 const CompilerOptions& compiler_options,
1040 OptimizingCompilerStats* stats)
1041 : CodeGenerator(graph,
1042 kNumberOfCoreRegisters,
1043 kNumberOfFRegisters,
1044 kNumberOfRegisterPairs,
1045 ComputeRegisterMask(reinterpret_cast<const int*>(kCoreCalleeSaves),
1046 arraysize(kCoreCalleeSaves)),
1047 ComputeRegisterMask(reinterpret_cast<const int*>(kFpuCalleeSaves),
1048 arraysize(kFpuCalleeSaves)),
1049 compiler_options,
1050 stats),
1051 block_labels_(nullptr),
1052 location_builder_(graph, this),
1053 instruction_visitor_(graph, this),
1054 move_resolver_(graph->GetArena(), this),
Vladimir Marko93205e32016-04-13 11:59:46 +01001055 assembler_(graph->GetArena(), &isa_features),
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001056 isa_features_(isa_features),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001057 uint32_literals_(std::less<uint32_t>(),
1058 graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001059 pc_relative_method_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001060 method_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001061 pc_relative_type_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko1998cd02017-01-13 13:02:58 +00001062 type_bss_entry_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Vladimir Marko65979462017-05-19 17:25:12 +01001063 pc_relative_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze627c1a02017-01-30 19:28:14 -08001064 jit_string_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
1065 jit_class_patches_(graph->GetArena()->Adapter(kArenaAllocCodeGenerator)),
Alexey Frunze06a46c42016-07-19 15:00:40 -07001066 clobbered_ra_(false) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001067 // Save RA (containing the return address) to mimic Quick.
1068 AddAllocatedRegister(Location::RegisterLocation(RA));
1069}
1070
1071#undef __
Roland Levillain7cbd27f2016-08-11 23:53:33 +01001072// NOLINT on __ macro to suppress wrong warning/fix (misc-macro-parentheses) from clang-tidy.
1073#define __ down_cast<MipsAssembler*>(GetAssembler())-> // NOLINT
Andreas Gampe542451c2016-07-26 09:02:02 -07001074#define QUICK_ENTRY_POINT(x) QUICK_ENTRYPOINT_OFFSET(kMipsPointerSize, x).Int32Value()
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001075
1076void CodeGeneratorMIPS::Finalize(CodeAllocator* allocator) {
1077 // Ensure that we fix up branches.
1078 __ FinalizeCode();
1079
1080 // Adjust native pc offsets in stack maps.
1081 for (size_t i = 0, num = stack_map_stream_.GetNumberOfStackMaps(); i != num; ++i) {
Mathieu Chartiera2f526f2017-01-19 14:48:48 -08001082 uint32_t old_position =
1083 stack_map_stream_.GetStackMap(i).native_pc_code_offset.Uint32Value(kMips);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001084 uint32_t new_position = __ GetAdjustedPosition(old_position);
1085 DCHECK_GE(new_position, old_position);
1086 stack_map_stream_.SetStackMapNativePcOffset(i, new_position);
1087 }
1088
1089 // Adjust pc offsets for the disassembly information.
1090 if (disasm_info_ != nullptr) {
1091 GeneratedCodeInterval* frame_entry_interval = disasm_info_->GetFrameEntryInterval();
1092 frame_entry_interval->start = __ GetAdjustedPosition(frame_entry_interval->start);
1093 frame_entry_interval->end = __ GetAdjustedPosition(frame_entry_interval->end);
1094 for (auto& it : *disasm_info_->GetInstructionIntervals()) {
1095 it.second.start = __ GetAdjustedPosition(it.second.start);
1096 it.second.end = __ GetAdjustedPosition(it.second.end);
1097 }
1098 for (auto& it : *disasm_info_->GetSlowPathIntervals()) {
1099 it.code_interval.start = __ GetAdjustedPosition(it.code_interval.start);
1100 it.code_interval.end = __ GetAdjustedPosition(it.code_interval.end);
1101 }
1102 }
1103
1104 CodeGenerator::Finalize(allocator);
1105}
1106
1107MipsAssembler* ParallelMoveResolverMIPS::GetAssembler() const {
1108 return codegen_->GetAssembler();
1109}
1110
1111void ParallelMoveResolverMIPS::EmitMove(size_t index) {
1112 DCHECK_LT(index, moves_.size());
1113 MoveOperands* move = moves_[index];
1114 codegen_->MoveLocation(move->GetDestination(), move->GetSource(), move->GetType());
1115}
1116
1117void ParallelMoveResolverMIPS::EmitSwap(size_t index) {
1118 DCHECK_LT(index, moves_.size());
1119 MoveOperands* move = moves_[index];
1120 Primitive::Type type = move->GetType();
1121 Location loc1 = move->GetDestination();
1122 Location loc2 = move->GetSource();
1123
1124 DCHECK(!loc1.IsConstant());
1125 DCHECK(!loc2.IsConstant());
1126
1127 if (loc1.Equals(loc2)) {
1128 return;
1129 }
1130
1131 if (loc1.IsRegister() && loc2.IsRegister()) {
1132 // Swap 2 GPRs.
1133 Register r1 = loc1.AsRegister<Register>();
1134 Register r2 = loc2.AsRegister<Register>();
1135 __ Move(TMP, r2);
1136 __ Move(r2, r1);
1137 __ Move(r1, TMP);
1138 } else if (loc1.IsFpuRegister() && loc2.IsFpuRegister()) {
1139 FRegister f1 = loc1.AsFpuRegister<FRegister>();
1140 FRegister f2 = loc2.AsFpuRegister<FRegister>();
1141 if (type == Primitive::kPrimFloat) {
1142 __ MovS(FTMP, f2);
1143 __ MovS(f2, f1);
1144 __ MovS(f1, FTMP);
1145 } else {
1146 DCHECK_EQ(type, Primitive::kPrimDouble);
1147 __ MovD(FTMP, f2);
1148 __ MovD(f2, f1);
1149 __ MovD(f1, FTMP);
1150 }
1151 } else if ((loc1.IsRegister() && loc2.IsFpuRegister()) ||
1152 (loc1.IsFpuRegister() && loc2.IsRegister())) {
1153 // Swap FPR and GPR.
1154 DCHECK_EQ(type, Primitive::kPrimFloat); // Can only swap a float.
1155 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1156 : loc2.AsFpuRegister<FRegister>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001157 Register r2 = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001158 __ Move(TMP, r2);
1159 __ Mfc1(r2, f1);
1160 __ Mtc1(TMP, f1);
1161 } else if (loc1.IsRegisterPair() && loc2.IsRegisterPair()) {
1162 // Swap 2 GPR register pairs.
1163 Register r1 = loc1.AsRegisterPairLow<Register>();
1164 Register r2 = loc2.AsRegisterPairLow<Register>();
1165 __ Move(TMP, r2);
1166 __ Move(r2, r1);
1167 __ Move(r1, TMP);
1168 r1 = loc1.AsRegisterPairHigh<Register>();
1169 r2 = loc2.AsRegisterPairHigh<Register>();
1170 __ Move(TMP, r2);
1171 __ Move(r2, r1);
1172 __ Move(r1, TMP);
1173 } else if ((loc1.IsRegisterPair() && loc2.IsFpuRegister()) ||
1174 (loc1.IsFpuRegister() && loc2.IsRegisterPair())) {
1175 // Swap FPR and GPR register pair.
1176 DCHECK_EQ(type, Primitive::kPrimDouble);
1177 FRegister f1 = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1178 : loc2.AsFpuRegister<FRegister>();
1179 Register r2_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1180 : loc2.AsRegisterPairLow<Register>();
1181 Register r2_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1182 : loc2.AsRegisterPairHigh<Register>();
1183 // Use 2 temporary registers because we can't first swap the low 32 bits of an FPR and
1184 // then swap the high 32 bits of the same FPR. mtc1 makes the high 32 bits of an FPR
1185 // unpredictable and the following mfch1 will fail.
1186 __ Mfc1(TMP, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001187 __ MoveFromFpuHigh(AT, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001188 __ Mtc1(r2_l, f1);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001189 __ MoveToFpuHigh(r2_h, f1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001190 __ Move(r2_l, TMP);
1191 __ Move(r2_h, AT);
1192 } else if (loc1.IsStackSlot() && loc2.IsStackSlot()) {
1193 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ false);
1194 } else if (loc1.IsDoubleStackSlot() && loc2.IsDoubleStackSlot()) {
1195 Exchange(loc1.GetStackIndex(), loc2.GetStackIndex(), /* double_slot */ true);
David Brazdilcc0f3112016-01-28 17:14:52 +00001196 } else if ((loc1.IsRegister() && loc2.IsStackSlot()) ||
1197 (loc1.IsStackSlot() && loc2.IsRegister())) {
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001198 Register reg = loc1.IsRegister() ? loc1.AsRegister<Register>() : loc2.AsRegister<Register>();
1199 intptr_t offset = loc1.IsStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001200 __ Move(TMP, reg);
1201 __ LoadFromOffset(kLoadWord, reg, SP, offset);
1202 __ StoreToOffset(kStoreWord, TMP, SP, offset);
1203 } else if ((loc1.IsRegisterPair() && loc2.IsDoubleStackSlot()) ||
1204 (loc1.IsDoubleStackSlot() && loc2.IsRegisterPair())) {
1205 Register reg_l = loc1.IsRegisterPair() ? loc1.AsRegisterPairLow<Register>()
1206 : loc2.AsRegisterPairLow<Register>();
1207 Register reg_h = loc1.IsRegisterPair() ? loc1.AsRegisterPairHigh<Register>()
1208 : loc2.AsRegisterPairHigh<Register>();
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001209 intptr_t offset_l = loc1.IsDoubleStackSlot() ? loc1.GetStackIndex() : loc2.GetStackIndex();
David Brazdilcc0f3112016-01-28 17:14:52 +00001210 intptr_t offset_h = loc1.IsDoubleStackSlot() ? loc1.GetHighStackIndex(kMipsWordSize)
1211 : loc2.GetHighStackIndex(kMipsWordSize);
1212 __ Move(TMP, reg_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001213 __ LoadFromOffset(kLoadWord, reg_l, SP, offset_l);
David Brazdilcc0f3112016-01-28 17:14:52 +00001214 __ StoreToOffset(kStoreWord, TMP, SP, offset_l);
David Brazdil04d3e872016-01-29 09:50:09 +00001215 __ Move(TMP, reg_h);
1216 __ LoadFromOffset(kLoadWord, reg_h, SP, offset_h);
1217 __ StoreToOffset(kStoreWord, TMP, SP, offset_h);
Goran Jakovljevic35dfcaa2016-09-22 09:26:01 +02001218 } else if (loc1.IsFpuRegister() || loc2.IsFpuRegister()) {
1219 FRegister reg = loc1.IsFpuRegister() ? loc1.AsFpuRegister<FRegister>()
1220 : loc2.AsFpuRegister<FRegister>();
1221 intptr_t offset = loc1.IsFpuRegister() ? loc2.GetStackIndex() : loc1.GetStackIndex();
1222 if (type == Primitive::kPrimFloat) {
1223 __ MovS(FTMP, reg);
1224 __ LoadSFromOffset(reg, SP, offset);
1225 __ StoreSToOffset(FTMP, SP, offset);
1226 } else {
1227 DCHECK_EQ(type, Primitive::kPrimDouble);
1228 __ MovD(FTMP, reg);
1229 __ LoadDFromOffset(reg, SP, offset);
1230 __ StoreDToOffset(FTMP, SP, offset);
1231 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001232 } else {
1233 LOG(FATAL) << "Swap between " << loc1 << " and " << loc2 << " is unsupported";
1234 }
1235}
1236
1237void ParallelMoveResolverMIPS::RestoreScratch(int reg) {
1238 __ Pop(static_cast<Register>(reg));
1239}
1240
1241void ParallelMoveResolverMIPS::SpillScratch(int reg) {
1242 __ Push(static_cast<Register>(reg));
1243}
1244
1245void ParallelMoveResolverMIPS::Exchange(int index1, int index2, bool double_slot) {
1246 // Allocate a scratch register other than TMP, if available.
1247 // Else, spill V0 (arbitrary choice) and use it as a scratch register (it will be
1248 // automatically unspilled when the scratch scope object is destroyed).
1249 ScratchRegisterScope ensure_scratch(this, TMP, V0, codegen_->GetNumberOfCoreRegisters());
1250 // If V0 spills onto the stack, SP-relative offsets need to be adjusted.
1251 int stack_offset = ensure_scratch.IsSpilled() ? kMipsWordSize : 0;
1252 for (int i = 0; i <= (double_slot ? 1 : 0); i++, stack_offset += kMipsWordSize) {
1253 __ LoadFromOffset(kLoadWord,
1254 Register(ensure_scratch.GetRegister()),
1255 SP,
1256 index1 + stack_offset);
1257 __ LoadFromOffset(kLoadWord,
1258 TMP,
1259 SP,
1260 index2 + stack_offset);
1261 __ StoreToOffset(kStoreWord,
1262 Register(ensure_scratch.GetRegister()),
1263 SP,
1264 index2 + stack_offset);
1265 __ StoreToOffset(kStoreWord, TMP, SP, index1 + stack_offset);
1266 }
1267}
1268
Alexey Frunze73296a72016-06-03 22:51:46 -07001269void CodeGeneratorMIPS::ComputeSpillMask() {
1270 core_spill_mask_ = allocated_registers_.GetCoreRegisters() & core_callee_save_mask_;
1271 fpu_spill_mask_ = allocated_registers_.GetFloatingPointRegisters() & fpu_callee_save_mask_;
1272 DCHECK_NE(core_spill_mask_, 0u) << "At least the return address register must be saved";
1273 // If there're FPU callee-saved registers and there's an odd number of GPR callee-saved
1274 // registers, include the ZERO register to force alignment of FPU callee-saved registers
1275 // within the stack frame.
1276 if ((fpu_spill_mask_ != 0) && (POPCOUNT(core_spill_mask_) % 2 != 0)) {
1277 core_spill_mask_ |= (1 << ZERO);
1278 }
Alexey Frunze58320ce2016-08-30 21:40:46 -07001279}
1280
1281bool CodeGeneratorMIPS::HasAllocatedCalleeSaveRegisters() const {
Alexey Frunze06a46c42016-07-19 15:00:40 -07001282 // If RA is clobbered by PC-relative operations on R2 and it's the only spilled register
Alexey Frunze58320ce2016-08-30 21:40:46 -07001283 // (this can happen in leaf methods), force CodeGenerator::InitializeCodeGeneration()
1284 // into the path that creates a stack frame so that RA can be explicitly saved and restored.
1285 // RA can't otherwise be saved/restored when it's the only spilled register.
Alexey Frunze58320ce2016-08-30 21:40:46 -07001286 return CodeGenerator::HasAllocatedCalleeSaveRegisters() || clobbered_ra_;
Alexey Frunze73296a72016-06-03 22:51:46 -07001287}
1288
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001289static dwarf::Reg DWARFReg(Register reg) {
1290 return dwarf::Reg::MipsCore(static_cast<int>(reg));
1291}
1292
1293// TODO: mapping of floating-point registers to DWARF.
1294
1295void CodeGeneratorMIPS::GenerateFrameEntry() {
1296 __ Bind(&frame_entry_label_);
1297
1298 bool do_overflow_check = FrameNeedsStackCheck(GetFrameSize(), kMips) || !IsLeafMethod();
1299
1300 if (do_overflow_check) {
1301 __ LoadFromOffset(kLoadWord,
1302 ZERO,
1303 SP,
1304 -static_cast<int32_t>(GetStackOverflowReservedBytes(kMips)));
1305 RecordPcInfo(nullptr, 0);
1306 }
1307
1308 if (HasEmptyFrame()) {
Alexey Frunze58320ce2016-08-30 21:40:46 -07001309 CHECK_EQ(fpu_spill_mask_, 0u);
1310 CHECK_EQ(core_spill_mask_, 1u << RA);
1311 CHECK(!clobbered_ra_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001312 return;
1313 }
1314
1315 // Make sure the frame size isn't unreasonably large.
1316 if (GetFrameSize() > GetStackOverflowReservedBytes(kMips)) {
1317 LOG(FATAL) << "Stack frame larger than " << GetStackOverflowReservedBytes(kMips) << " bytes";
1318 }
1319
1320 // Spill callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001321
Alexey Frunze73296a72016-06-03 22:51:46 -07001322 uint32_t ofs = GetFrameSize();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001323 __ IncreaseFrameSize(ofs);
1324
Alexey Frunze73296a72016-06-03 22:51:46 -07001325 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1326 Register reg = static_cast<Register>(MostSignificantBit(mask));
1327 mask ^= 1u << reg;
1328 ofs -= kMipsWordSize;
1329 // The ZERO register is only included for alignment.
1330 if (reg != ZERO) {
1331 __ StoreToOffset(kStoreWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001332 __ cfi().RelOffset(DWARFReg(reg), ofs);
1333 }
1334 }
1335
Alexey Frunze73296a72016-06-03 22:51:46 -07001336 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1337 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1338 mask ^= 1u << reg;
1339 ofs -= kMipsDoublewordSize;
1340 __ StoreDToOffset(reg, SP, ofs);
1341 // TODO: __ cfi().RelOffset(DWARFReg(reg), ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001342 }
1343
Nicolas Geoffray96eeb4e2016-10-12 22:03:31 +01001344 // Save the current method if we need it. Note that we do not
1345 // do this in HCurrentMethod, as the instruction might have been removed
1346 // in the SSA graph.
1347 if (RequiresCurrentMethod()) {
1348 __ StoreToOffset(kStoreWord, kMethodRegisterArgument, SP, kCurrentMethodStackOffset);
1349 }
Goran Jakovljevicc6418422016-12-05 16:31:55 +01001350
1351 if (GetGraph()->HasShouldDeoptimizeFlag()) {
1352 // Initialize should deoptimize flag to 0.
1353 __ StoreToOffset(kStoreWord, ZERO, SP, GetStackOffsetOfShouldDeoptimizeFlag());
1354 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001355}
1356
1357void CodeGeneratorMIPS::GenerateFrameExit() {
1358 __ cfi().RememberState();
1359
1360 if (!HasEmptyFrame()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001361 // Restore callee-saved registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001362
Alexey Frunze73296a72016-06-03 22:51:46 -07001363 // For better instruction scheduling restore RA before other registers.
1364 uint32_t ofs = GetFrameSize();
1365 for (uint32_t mask = core_spill_mask_; mask != 0; ) {
1366 Register reg = static_cast<Register>(MostSignificantBit(mask));
1367 mask ^= 1u << reg;
1368 ofs -= kMipsWordSize;
1369 // The ZERO register is only included for alignment.
1370 if (reg != ZERO) {
1371 __ LoadFromOffset(kLoadWord, reg, SP, ofs);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001372 __ cfi().Restore(DWARFReg(reg));
1373 }
1374 }
1375
Alexey Frunze73296a72016-06-03 22:51:46 -07001376 for (uint32_t mask = fpu_spill_mask_; mask != 0; ) {
1377 FRegister reg = static_cast<FRegister>(MostSignificantBit(mask));
1378 mask ^= 1u << reg;
1379 ofs -= kMipsDoublewordSize;
1380 __ LoadDFromOffset(reg, SP, ofs);
1381 // TODO: __ cfi().Restore(DWARFReg(reg));
1382 }
1383
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001384 size_t frame_size = GetFrameSize();
1385 // Adjust the stack pointer in the delay slot if doing so doesn't break CFI.
1386 bool exchange = IsInt<16>(static_cast<int32_t>(frame_size));
1387 bool reordering = __ SetReorder(false);
1388 if (exchange) {
1389 __ Jr(RA);
1390 __ DecreaseFrameSize(frame_size); // Single instruction in delay slot.
1391 } else {
1392 __ DecreaseFrameSize(frame_size);
1393 __ Jr(RA);
1394 __ Nop(); // In delay slot.
1395 }
1396 __ SetReorder(reordering);
1397 } else {
1398 __ Jr(RA);
1399 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001400 }
1401
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001402 __ cfi().RestoreState();
1403 __ cfi().DefCFAOffset(GetFrameSize());
1404}
1405
1406void CodeGeneratorMIPS::Bind(HBasicBlock* block) {
1407 __ Bind(GetLabelOf(block));
1408}
1409
1410void CodeGeneratorMIPS::MoveLocation(Location dst, Location src, Primitive::Type dst_type) {
1411 if (src.Equals(dst)) {
1412 return;
1413 }
1414
1415 if (src.IsConstant()) {
1416 MoveConstant(dst, src.GetConstant());
1417 } else {
1418 if (Primitive::Is64BitType(dst_type)) {
1419 Move64(dst, src);
1420 } else {
1421 Move32(dst, src);
1422 }
1423 }
1424}
1425
1426void CodeGeneratorMIPS::Move32(Location destination, Location source) {
1427 if (source.Equals(destination)) {
1428 return;
1429 }
1430
1431 if (destination.IsRegister()) {
1432 if (source.IsRegister()) {
1433 __ Move(destination.AsRegister<Register>(), source.AsRegister<Register>());
1434 } else if (source.IsFpuRegister()) {
1435 __ Mfc1(destination.AsRegister<Register>(), source.AsFpuRegister<FRegister>());
1436 } else {
1437 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1438 __ LoadFromOffset(kLoadWord, destination.AsRegister<Register>(), SP, source.GetStackIndex());
1439 }
1440 } else if (destination.IsFpuRegister()) {
1441 if (source.IsRegister()) {
1442 __ Mtc1(source.AsRegister<Register>(), destination.AsFpuRegister<FRegister>());
1443 } else if (source.IsFpuRegister()) {
1444 __ MovS(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1445 } else {
1446 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1447 __ LoadSFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1448 }
1449 } else {
1450 DCHECK(destination.IsStackSlot()) << destination;
1451 if (source.IsRegister()) {
1452 __ StoreToOffset(kStoreWord, source.AsRegister<Register>(), SP, destination.GetStackIndex());
1453 } else if (source.IsFpuRegister()) {
1454 __ StoreSToOffset(source.AsFpuRegister<FRegister>(), SP, destination.GetStackIndex());
1455 } else {
1456 DCHECK(source.IsStackSlot()) << "Cannot move from " << source << " to " << destination;
1457 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1458 __ StoreToOffset(kStoreWord, TMP, SP, destination.GetStackIndex());
1459 }
1460 }
1461}
1462
1463void CodeGeneratorMIPS::Move64(Location destination, Location source) {
1464 if (source.Equals(destination)) {
1465 return;
1466 }
1467
1468 if (destination.IsRegisterPair()) {
1469 if (source.IsRegisterPair()) {
1470 __ Move(destination.AsRegisterPairHigh<Register>(), source.AsRegisterPairHigh<Register>());
1471 __ Move(destination.AsRegisterPairLow<Register>(), source.AsRegisterPairLow<Register>());
1472 } else if (source.IsFpuRegister()) {
1473 Register dst_high = destination.AsRegisterPairHigh<Register>();
1474 Register dst_low = destination.AsRegisterPairLow<Register>();
1475 FRegister src = source.AsFpuRegister<FRegister>();
1476 __ Mfc1(dst_low, src);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001477 __ MoveFromFpuHigh(dst_high, src);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001478 } else {
1479 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1480 int32_t off = source.GetStackIndex();
1481 Register r = destination.AsRegisterPairLow<Register>();
1482 __ LoadFromOffset(kLoadDoubleword, r, SP, off);
1483 }
1484 } else if (destination.IsFpuRegister()) {
1485 if (source.IsRegisterPair()) {
1486 FRegister dst = destination.AsFpuRegister<FRegister>();
1487 Register src_high = source.AsRegisterPairHigh<Register>();
1488 Register src_low = source.AsRegisterPairLow<Register>();
1489 __ Mtc1(src_low, dst);
Alexey Frunzebb9863a2016-01-11 15:51:16 -08001490 __ MoveToFpuHigh(src_high, dst);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001491 } else if (source.IsFpuRegister()) {
1492 __ MovD(destination.AsFpuRegister<FRegister>(), source.AsFpuRegister<FRegister>());
1493 } else {
1494 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1495 __ LoadDFromOffset(destination.AsFpuRegister<FRegister>(), SP, source.GetStackIndex());
1496 }
1497 } else {
1498 DCHECK(destination.IsDoubleStackSlot()) << destination;
1499 int32_t off = destination.GetStackIndex();
1500 if (source.IsRegisterPair()) {
1501 __ StoreToOffset(kStoreDoubleword, source.AsRegisterPairLow<Register>(), SP, off);
1502 } else if (source.IsFpuRegister()) {
1503 __ StoreDToOffset(source.AsFpuRegister<FRegister>(), SP, off);
1504 } else {
1505 DCHECK(source.IsDoubleStackSlot()) << "Cannot move from " << source << " to " << destination;
1506 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex());
1507 __ StoreToOffset(kStoreWord, TMP, SP, off);
1508 __ LoadFromOffset(kLoadWord, TMP, SP, source.GetStackIndex() + 4);
1509 __ StoreToOffset(kStoreWord, TMP, SP, off + 4);
1510 }
1511 }
1512}
1513
1514void CodeGeneratorMIPS::MoveConstant(Location destination, HConstant* c) {
1515 if (c->IsIntConstant() || c->IsNullConstant()) {
1516 // Move 32 bit constant.
1517 int32_t value = GetInt32ValueOf(c);
1518 if (destination.IsRegister()) {
1519 Register dst = destination.AsRegister<Register>();
1520 __ LoadConst32(dst, value);
1521 } else {
1522 DCHECK(destination.IsStackSlot())
1523 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001524 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001525 }
1526 } else if (c->IsLongConstant()) {
1527 // Move 64 bit constant.
1528 int64_t value = GetInt64ValueOf(c);
1529 if (destination.IsRegisterPair()) {
1530 Register r_h = destination.AsRegisterPairHigh<Register>();
1531 Register r_l = destination.AsRegisterPairLow<Register>();
1532 __ LoadConst64(r_h, r_l, value);
1533 } else {
1534 DCHECK(destination.IsDoubleStackSlot())
1535 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001536 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001537 }
1538 } else if (c->IsFloatConstant()) {
1539 // Move 32 bit float constant.
1540 int32_t value = GetInt32ValueOf(c);
1541 if (destination.IsFpuRegister()) {
1542 __ LoadSConst32(destination.AsFpuRegister<FRegister>(), value, TMP);
1543 } else {
1544 DCHECK(destination.IsStackSlot())
1545 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001546 __ StoreConstToOffset(kStoreWord, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001547 }
1548 } else {
1549 // Move 64 bit double constant.
1550 DCHECK(c->IsDoubleConstant()) << c->DebugName();
1551 int64_t value = GetInt64ValueOf(c);
1552 if (destination.IsFpuRegister()) {
1553 FRegister fd = destination.AsFpuRegister<FRegister>();
1554 __ LoadDConst64(fd, value, TMP);
1555 } else {
1556 DCHECK(destination.IsDoubleStackSlot())
1557 << "Cannot move " << c->DebugName() << " to " << destination;
Alexey Frunzef58b2482016-09-02 22:14:06 -07001558 __ StoreConstToOffset(kStoreDoubleword, value, SP, destination.GetStackIndex(), TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001559 }
1560 }
1561}
1562
1563void CodeGeneratorMIPS::MoveConstant(Location destination, int32_t value) {
1564 DCHECK(destination.IsRegister());
1565 Register dst = destination.AsRegister<Register>();
1566 __ LoadConst32(dst, value);
1567}
1568
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001569void CodeGeneratorMIPS::AddLocationAsTemp(Location location, LocationSummary* locations) {
1570 if (location.IsRegister()) {
1571 locations->AddTemp(location);
Alexey Frunzec9e94f32015-10-26 16:11:39 -07001572 } else if (location.IsRegisterPair()) {
1573 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairLow<Register>()));
1574 locations->AddTemp(Location::RegisterLocation(location.AsRegisterPairHigh<Register>()));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001575 } else {
1576 UNIMPLEMENTED(FATAL) << "AddLocationAsTemp not implemented for location " << location;
1577 }
1578}
1579
Vladimir Markoaad75c62016-10-03 08:46:48 +00001580template <LinkerPatch (*Factory)(size_t, const DexFile*, uint32_t, uint32_t)>
1581inline void CodeGeneratorMIPS::EmitPcRelativeLinkerPatches(
1582 const ArenaDeque<PcRelativePatchInfo>& infos,
1583 ArenaVector<LinkerPatch>* linker_patches) {
1584 for (const PcRelativePatchInfo& info : infos) {
1585 const DexFile& dex_file = info.target_dex_file;
1586 size_t offset_or_index = info.offset_or_index;
1587 DCHECK(info.high_label.IsBound());
1588 uint32_t high_offset = __ GetLabelLocation(&info.high_label);
1589 // On R2 we use HMipsComputeBaseMethodAddress and patch relative to
1590 // the assembler's base label used for PC-relative addressing.
1591 uint32_t pc_rel_offset = info.pc_rel_label.IsBound()
1592 ? __ GetLabelLocation(&info.pc_rel_label)
1593 : __ GetPcRelBaseLabelLocation();
1594 linker_patches->push_back(Factory(high_offset, &dex_file, pc_rel_offset, offset_or_index));
1595 }
1596}
1597
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001598void CodeGeneratorMIPS::EmitLinkerPatches(ArenaVector<LinkerPatch>* linker_patches) {
1599 DCHECK(linker_patches->empty());
1600 size_t size =
Vladimir Marko65979462017-05-19 17:25:12 +01001601 pc_relative_method_patches_.size() +
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001602 method_bss_entry_patches_.size() +
Alexey Frunze06a46c42016-07-19 15:00:40 -07001603 pc_relative_type_patches_.size() +
Vladimir Marko65979462017-05-19 17:25:12 +01001604 type_bss_entry_patches_.size() +
1605 pc_relative_string_patches_.size();
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001606 linker_patches->reserve(size);
Vladimir Marko65979462017-05-19 17:25:12 +01001607 if (GetCompilerOptions().IsBootImage()) {
1608 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeMethodPatch>(pc_relative_method_patches_,
Vladimir Markoaad75c62016-10-03 08:46:48 +00001609 linker_patches);
Vladimir Marko6bec91c2017-01-09 15:03:12 +00001610 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeTypePatch>(pc_relative_type_patches_,
1611 linker_patches);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001612 EmitPcRelativeLinkerPatches<LinkerPatch::RelativeStringPatch>(pc_relative_string_patches_,
1613 linker_patches);
Vladimir Marko65979462017-05-19 17:25:12 +01001614 } else {
1615 DCHECK(pc_relative_method_patches_.empty());
1616 DCHECK(pc_relative_type_patches_.empty());
1617 EmitPcRelativeLinkerPatches<LinkerPatch::StringBssEntryPatch>(pc_relative_string_patches_,
1618 linker_patches);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001619 }
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001620 EmitPcRelativeLinkerPatches<LinkerPatch::MethodBssEntryPatch>(method_bss_entry_patches_,
1621 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001622 EmitPcRelativeLinkerPatches<LinkerPatch::TypeBssEntryPatch>(type_bss_entry_patches_,
1623 linker_patches);
Vladimir Marko1998cd02017-01-13 13:02:58 +00001624 DCHECK_EQ(size, linker_patches->size());
Alexey Frunze06a46c42016-07-19 15:00:40 -07001625}
1626
Vladimir Marko65979462017-05-19 17:25:12 +01001627CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeMethodPatch(
1628 MethodReference target_method) {
1629 return NewPcRelativePatch(*target_method.dex_file,
1630 target_method.dex_method_index,
1631 &pc_relative_method_patches_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001632}
1633
Vladimir Marko0eb882b2017-05-15 13:39:18 +01001634CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewMethodBssEntryPatch(
1635 MethodReference target_method) {
1636 return NewPcRelativePatch(*target_method.dex_file,
1637 target_method.dex_method_index,
1638 &method_bss_entry_patches_);
1639}
1640
Alexey Frunze06a46c42016-07-19 15:00:40 -07001641CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeTypePatch(
Andreas Gampea5b09a62016-11-17 15:21:22 -08001642 const DexFile& dex_file, dex::TypeIndex type_index) {
1643 return NewPcRelativePatch(dex_file, type_index.index_, &pc_relative_type_patches_);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001644}
1645
Vladimir Marko1998cd02017-01-13 13:02:58 +00001646CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewTypeBssEntryPatch(
1647 const DexFile& dex_file, dex::TypeIndex type_index) {
1648 return NewPcRelativePatch(dex_file, type_index.index_, &type_bss_entry_patches_);
1649}
1650
Vladimir Marko65979462017-05-19 17:25:12 +01001651CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativeStringPatch(
1652 const DexFile& dex_file, dex::StringIndex string_index) {
1653 return NewPcRelativePatch(dex_file, string_index.index_, &pc_relative_string_patches_);
1654}
1655
Alexey Frunzee3fb2452016-05-10 16:08:05 -07001656CodeGeneratorMIPS::PcRelativePatchInfo* CodeGeneratorMIPS::NewPcRelativePatch(
1657 const DexFile& dex_file, uint32_t offset_or_index, ArenaDeque<PcRelativePatchInfo>* patches) {
1658 patches->emplace_back(dex_file, offset_or_index);
1659 return &patches->back();
1660}
1661
Alexey Frunze06a46c42016-07-19 15:00:40 -07001662Literal* CodeGeneratorMIPS::DeduplicateUint32Literal(uint32_t value, Uint32ToLiteralMap* map) {
1663 return map->GetOrCreate(
1664 value,
1665 [this, value]() { return __ NewLiteral<uint32_t>(value); });
1666}
1667
Alexey Frunze06a46c42016-07-19 15:00:40 -07001668Literal* CodeGeneratorMIPS::DeduplicateBootImageAddressLiteral(uint32_t address) {
Richard Uhlerc52f3032017-03-02 13:45:45 +00001669 return DeduplicateUint32Literal(dchecked_integral_cast<uint32_t>(address), &uint32_literals_);
Alexey Frunze06a46c42016-07-19 15:00:40 -07001670}
1671
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001672void CodeGeneratorMIPS::EmitPcRelativeAddressPlaceholderHigh(PcRelativePatchInfo* info,
1673 Register out,
1674 Register base) {
Alexey Frunze6079dca2017-05-28 19:10:28 -07001675 DCHECK_NE(out, base);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001676 if (GetInstructionSetFeatures().IsR6()) {
1677 DCHECK_EQ(base, ZERO);
1678 __ Bind(&info->high_label);
1679 __ Bind(&info->pc_rel_label);
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001680 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001681 __ Auipc(out, /* placeholder */ 0x1234);
Vladimir Markoaad75c62016-10-03 08:46:48 +00001682 } else {
1683 // If base is ZERO, emit NAL to obtain the actual base.
1684 if (base == ZERO) {
1685 // Generate a dummy PC-relative call to obtain PC.
1686 __ Nal();
1687 }
1688 __ Bind(&info->high_label);
1689 __ Lui(out, /* placeholder */ 0x1234);
1690 // If we emitted the NAL, bind the pc_rel_label, otherwise base is a register holding
1691 // the HMipsComputeBaseMethodAddress which has its own label stored in MipsAssembler.
1692 if (base == ZERO) {
1693 __ Bind(&info->pc_rel_label);
1694 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001695 // Add the high half of a 32-bit offset to PC.
Vladimir Markoaad75c62016-10-03 08:46:48 +00001696 __ Addu(out, out, (base == ZERO) ? RA : base);
1697 }
Alexey Frunze6b892cd2017-01-03 17:11:38 -08001698 // The immediately following instruction will add the sign-extended low half of the 32-bit
1699 // offset to `out` (e.g. lw, jialc, addiu).
Vladimir Markoaad75c62016-10-03 08:46:48 +00001700}
1701
Alexey Frunze627c1a02017-01-30 19:28:14 -08001702CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootStringPatch(
1703 const DexFile& dex_file,
1704 dex::StringIndex dex_index,
1705 Handle<mirror::String> handle) {
1706 jit_string_roots_.Overwrite(StringReference(&dex_file, dex_index),
1707 reinterpret_cast64<uint64_t>(handle.GetReference()));
1708 jit_string_patches_.emplace_back(dex_file, dex_index.index_);
1709 return &jit_string_patches_.back();
1710}
1711
1712CodeGeneratorMIPS::JitPatchInfo* CodeGeneratorMIPS::NewJitRootClassPatch(
1713 const DexFile& dex_file,
1714 dex::TypeIndex dex_index,
1715 Handle<mirror::Class> handle) {
1716 jit_class_roots_.Overwrite(TypeReference(&dex_file, dex_index),
1717 reinterpret_cast64<uint64_t>(handle.GetReference()));
1718 jit_class_patches_.emplace_back(dex_file, dex_index.index_);
1719 return &jit_class_patches_.back();
1720}
1721
1722void CodeGeneratorMIPS::PatchJitRootUse(uint8_t* code,
1723 const uint8_t* roots_data,
1724 const CodeGeneratorMIPS::JitPatchInfo& info,
1725 uint64_t index_in_table) const {
1726 uint32_t literal_offset = GetAssembler().GetLabelLocation(&info.high_label);
1727 uintptr_t address =
1728 reinterpret_cast<uintptr_t>(roots_data) + index_in_table * sizeof(GcRoot<mirror::Object>);
1729 uint32_t addr32 = dchecked_integral_cast<uint32_t>(address);
1730 // lui reg, addr32_high
1731 DCHECK_EQ(code[literal_offset + 0], 0x34);
1732 DCHECK_EQ(code[literal_offset + 1], 0x12);
1733 DCHECK_EQ((code[literal_offset + 2] & 0xE0), 0x00);
1734 DCHECK_EQ(code[literal_offset + 3], 0x3C);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001735 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001736 DCHECK_EQ(code[literal_offset + 4], 0x78);
1737 DCHECK_EQ(code[literal_offset + 5], 0x56);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001738 addr32 += (addr32 & 0x8000) << 1; // Account for sign extension in "instr reg, reg, addr32_low".
Alexey Frunze627c1a02017-01-30 19:28:14 -08001739 // lui reg, addr32_high
1740 code[literal_offset + 0] = static_cast<uint8_t>(addr32 >> 16);
1741 code[literal_offset + 1] = static_cast<uint8_t>(addr32 >> 24);
Alexey Frunzec61c0762017-04-10 13:54:23 -07001742 // instr reg, reg, addr32_low
Alexey Frunze627c1a02017-01-30 19:28:14 -08001743 code[literal_offset + 4] = static_cast<uint8_t>(addr32 >> 0);
1744 code[literal_offset + 5] = static_cast<uint8_t>(addr32 >> 8);
1745}
1746
1747void CodeGeneratorMIPS::EmitJitRootPatches(uint8_t* code, const uint8_t* roots_data) {
1748 for (const JitPatchInfo& info : jit_string_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001749 const auto it = jit_string_roots_.find(StringReference(&info.target_dex_file,
1750 dex::StringIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001751 DCHECK(it != jit_string_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001752 uint64_t index_in_table = it->second;
1753 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001754 }
1755 for (const JitPatchInfo& info : jit_class_patches_) {
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001756 const auto it = jit_class_roots_.find(TypeReference(&info.target_dex_file,
1757 dex::TypeIndex(info.index)));
Alexey Frunze627c1a02017-01-30 19:28:14 -08001758 DCHECK(it != jit_class_roots_.end());
Vladimir Marko7d157fc2017-05-10 16:29:23 +01001759 uint64_t index_in_table = it->second;
1760 PatchJitRootUse(code, roots_data, info, index_in_table);
Alexey Frunze627c1a02017-01-30 19:28:14 -08001761 }
1762}
1763
Goran Jakovljevice114da22016-12-26 14:21:43 +01001764void CodeGeneratorMIPS::MarkGCCard(Register object,
1765 Register value,
1766 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001767 MipsLabel done;
1768 Register card = AT;
1769 Register temp = TMP;
Goran Jakovljevice114da22016-12-26 14:21:43 +01001770 if (value_can_be_null) {
1771 __ Beqz(value, &done);
1772 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001773 __ LoadFromOffset(kLoadWord,
1774 card,
1775 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001776 Thread::CardTableOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001777 __ Srl(temp, object, gc::accounting::CardTable::kCardShift);
1778 __ Addu(temp, card, temp);
1779 __ Sb(card, temp, 0);
Goran Jakovljevice114da22016-12-26 14:21:43 +01001780 if (value_can_be_null) {
1781 __ Bind(&done);
1782 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001783}
1784
David Brazdil58282f42016-01-14 12:45:10 +00001785void CodeGeneratorMIPS::SetupBlockedRegisters() const {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001786 // ZERO, K0, K1, GP, SP, RA are always reserved and can't be allocated.
1787 blocked_core_registers_[ZERO] = true;
1788 blocked_core_registers_[K0] = true;
1789 blocked_core_registers_[K1] = true;
1790 blocked_core_registers_[GP] = true;
1791 blocked_core_registers_[SP] = true;
1792 blocked_core_registers_[RA] = true;
1793
1794 // AT and TMP(T8) are used as temporary/scratch registers
1795 // (similar to how AT is used by MIPS assemblers).
1796 blocked_core_registers_[AT] = true;
1797 blocked_core_registers_[TMP] = true;
1798 blocked_fpu_registers_[FTMP] = true;
1799
1800 // Reserve suspend and thread registers.
1801 blocked_core_registers_[S0] = true;
1802 blocked_core_registers_[TR] = true;
1803
1804 // Reserve T9 for function calls
1805 blocked_core_registers_[T9] = true;
1806
1807 // Reserve odd-numbered FPU registers.
1808 for (size_t i = 1; i < kNumberOfFRegisters; i += 2) {
1809 blocked_fpu_registers_[i] = true;
1810 }
1811
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02001812 if (GetGraph()->IsDebuggable()) {
1813 // Stubs do not save callee-save floating point registers. If the graph
1814 // is debuggable, we need to deal with these registers differently. For
1815 // now, just block them.
1816 for (size_t i = 0; i < arraysize(kFpuCalleeSaves); ++i) {
1817 blocked_fpu_registers_[kFpuCalleeSaves[i]] = true;
1818 }
1819 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001820}
1821
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001822size_t CodeGeneratorMIPS::SaveCoreRegister(size_t stack_index, uint32_t reg_id) {
1823 __ StoreToOffset(kStoreWord, Register(reg_id), SP, stack_index);
1824 return kMipsWordSize;
1825}
1826
1827size_t CodeGeneratorMIPS::RestoreCoreRegister(size_t stack_index, uint32_t reg_id) {
1828 __ LoadFromOffset(kLoadWord, Register(reg_id), SP, stack_index);
1829 return kMipsWordSize;
1830}
1831
1832size_t CodeGeneratorMIPS::SaveFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1833 __ StoreDToOffset(FRegister(reg_id), SP, stack_index);
1834 return kMipsDoublewordSize;
1835}
1836
1837size_t CodeGeneratorMIPS::RestoreFloatingPointRegister(size_t stack_index, uint32_t reg_id) {
1838 __ LoadDFromOffset(FRegister(reg_id), SP, stack_index);
1839 return kMipsDoublewordSize;
1840}
1841
1842void CodeGeneratorMIPS::DumpCoreRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001843 stream << Register(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001844}
1845
1846void CodeGeneratorMIPS::DumpFloatingPointRegister(std::ostream& stream, int reg) const {
Vladimir Marko623a7a22016-02-02 18:14:52 +00001847 stream << FRegister(reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001848}
1849
Serban Constantinescufca16662016-07-14 09:21:59 +01001850constexpr size_t kMipsDirectEntrypointRuntimeOffset = 16;
1851
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001852void CodeGeneratorMIPS::InvokeRuntime(QuickEntrypointEnum entrypoint,
1853 HInstruction* instruction,
1854 uint32_t dex_pc,
1855 SlowPathCode* slow_path) {
Alexandre Rames91a65162016-09-19 13:54:30 +01001856 ValidateInvokeRuntime(entrypoint, instruction, slow_path);
Alexey Frunze15958152017-02-09 19:08:30 -08001857 GenerateInvokeRuntime(GetThreadOffset<kMipsPointerSize>(entrypoint).Int32Value(),
1858 IsDirectEntrypoint(entrypoint));
1859 if (EntrypointRequiresStackMap(entrypoint)) {
1860 RecordPcInfo(instruction, dex_pc, slow_path);
1861 }
1862}
1863
1864void CodeGeneratorMIPS::InvokeRuntimeWithoutRecordingPcInfo(int32_t entry_point_offset,
1865 HInstruction* instruction,
1866 SlowPathCode* slow_path,
1867 bool direct) {
1868 ValidateInvokeRuntimeWithoutRecordingPcInfo(instruction, slow_path);
1869 GenerateInvokeRuntime(entry_point_offset, direct);
1870}
1871
1872void CodeGeneratorMIPS::GenerateInvokeRuntime(int32_t entry_point_offset, bool direct) {
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001873 bool reordering = __ SetReorder(false);
Alexey Frunze15958152017-02-09 19:08:30 -08001874 __ LoadFromOffset(kLoadWord, T9, TR, entry_point_offset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001875 __ Jalr(T9);
Alexey Frunze15958152017-02-09 19:08:30 -08001876 if (direct) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001877 // Reserve argument space on stack (for $a0-$a3) for
1878 // entrypoints that directly reference native implementations.
1879 // Called function may use this space to store $a0-$a3 regs.
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001880 __ IncreaseFrameSize(kMipsDirectEntrypointRuntimeOffset); // Single instruction in delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001881 __ DecreaseFrameSize(kMipsDirectEntrypointRuntimeOffset);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001882 } else {
1883 __ Nop(); // In delay slot.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001884 }
Alexey Frunze57eb0f52016-07-29 22:04:46 -07001885 __ SetReorder(reordering);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001886}
1887
1888void InstructionCodeGeneratorMIPS::GenerateClassInitializationCheck(SlowPathCodeMIPS* slow_path,
1889 Register class_reg) {
1890 __ LoadFromOffset(kLoadWord, TMP, class_reg, mirror::Class::StatusOffset().Int32Value());
1891 __ LoadConst32(AT, mirror::Class::kStatusInitialized);
1892 __ Blt(TMP, AT, slow_path->GetEntryLabel());
1893 // Even if the initialized flag is set, we need to ensure consistent memory ordering.
1894 __ Sync(0);
1895 __ Bind(slow_path->GetExitLabel());
1896}
1897
1898void InstructionCodeGeneratorMIPS::GenerateMemoryBarrier(MemBarrierKind kind ATTRIBUTE_UNUSED) {
1899 __ Sync(0); // Only stype 0 is supported.
1900}
1901
1902void InstructionCodeGeneratorMIPS::GenerateSuspendCheck(HSuspendCheck* instruction,
1903 HBasicBlock* successor) {
1904 SuspendCheckSlowPathMIPS* slow_path =
1905 new (GetGraph()->GetArena()) SuspendCheckSlowPathMIPS(instruction, successor);
1906 codegen_->AddSlowPath(slow_path);
1907
1908 __ LoadFromOffset(kLoadUnsignedHalfword,
1909 TMP,
1910 TR,
Andreas Gampe542451c2016-07-26 09:02:02 -07001911 Thread::ThreadFlagsOffset<kMipsPointerSize>().Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001912 if (successor == nullptr) {
1913 __ Bnez(TMP, slow_path->GetEntryLabel());
1914 __ Bind(slow_path->GetReturnLabel());
1915 } else {
1916 __ Beqz(TMP, codegen_->GetLabelOf(successor));
1917 __ B(slow_path->GetEntryLabel());
1918 // slow_path will return to GetLabelOf(successor).
1919 }
1920}
1921
1922InstructionCodeGeneratorMIPS::InstructionCodeGeneratorMIPS(HGraph* graph,
1923 CodeGeneratorMIPS* codegen)
Aart Bik42249c32016-01-07 15:33:50 -08001924 : InstructionCodeGenerator(graph, codegen),
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001925 assembler_(codegen->GetAssembler()),
1926 codegen_(codegen) {}
1927
1928void LocationsBuilderMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1929 DCHECK_EQ(instruction->InputCount(), 2U);
1930 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
1931 Primitive::Type type = instruction->GetResultType();
1932 switch (type) {
1933 case Primitive::kPrimInt: {
1934 locations->SetInAt(0, Location::RequiresRegister());
1935 HInstruction* right = instruction->InputAt(1);
1936 bool can_use_imm = false;
1937 if (right->IsConstant()) {
1938 int32_t imm = CodeGenerator::GetInt32ValueOf(right->AsConstant());
1939 if (instruction->IsAnd() || instruction->IsOr() || instruction->IsXor()) {
1940 can_use_imm = IsUint<16>(imm);
1941 } else if (instruction->IsAdd()) {
1942 can_use_imm = IsInt<16>(imm);
1943 } else {
1944 DCHECK(instruction->IsSub());
1945 can_use_imm = IsInt<16>(-imm);
1946 }
1947 }
1948 if (can_use_imm)
1949 locations->SetInAt(1, Location::ConstantLocation(right->AsConstant()));
1950 else
1951 locations->SetInAt(1, Location::RequiresRegister());
1952 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
1953 break;
1954 }
1955
1956 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001957 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze5c7aed32015-11-25 19:41:54 -08001958 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
1959 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02001960 break;
1961 }
1962
1963 case Primitive::kPrimFloat:
1964 case Primitive::kPrimDouble:
1965 DCHECK(instruction->IsAdd() || instruction->IsSub());
1966 locations->SetInAt(0, Location::RequiresFpuRegister());
1967 locations->SetInAt(1, Location::RequiresFpuRegister());
1968 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
1969 break;
1970
1971 default:
1972 LOG(FATAL) << "Unexpected " << instruction->DebugName() << " type " << type;
1973 }
1974}
1975
1976void InstructionCodeGeneratorMIPS::HandleBinaryOp(HBinaryOperation* instruction) {
1977 Primitive::Type type = instruction->GetType();
1978 LocationSummary* locations = instruction->GetLocations();
1979
1980 switch (type) {
1981 case Primitive::kPrimInt: {
1982 Register dst = locations->Out().AsRegister<Register>();
1983 Register lhs = locations->InAt(0).AsRegister<Register>();
1984 Location rhs_location = locations->InAt(1);
1985
1986 Register rhs_reg = ZERO;
1987 int32_t rhs_imm = 0;
1988 bool use_imm = rhs_location.IsConstant();
1989 if (use_imm) {
1990 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
1991 } else {
1992 rhs_reg = rhs_location.AsRegister<Register>();
1993 }
1994
1995 if (instruction->IsAnd()) {
1996 if (use_imm)
1997 __ Andi(dst, lhs, rhs_imm);
1998 else
1999 __ And(dst, lhs, rhs_reg);
2000 } else if (instruction->IsOr()) {
2001 if (use_imm)
2002 __ Ori(dst, lhs, rhs_imm);
2003 else
2004 __ Or(dst, lhs, rhs_reg);
2005 } else if (instruction->IsXor()) {
2006 if (use_imm)
2007 __ Xori(dst, lhs, rhs_imm);
2008 else
2009 __ Xor(dst, lhs, rhs_reg);
2010 } else if (instruction->IsAdd()) {
2011 if (use_imm)
2012 __ Addiu(dst, lhs, rhs_imm);
2013 else
2014 __ Addu(dst, lhs, rhs_reg);
2015 } else {
2016 DCHECK(instruction->IsSub());
2017 if (use_imm)
2018 __ Addiu(dst, lhs, -rhs_imm);
2019 else
2020 __ Subu(dst, lhs, rhs_reg);
2021 }
2022 break;
2023 }
2024
2025 case Primitive::kPrimLong: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002026 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2027 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2028 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2029 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002030 Location rhs_location = locations->InAt(1);
2031 bool use_imm = rhs_location.IsConstant();
2032 if (!use_imm) {
2033 Register rhs_high = rhs_location.AsRegisterPairHigh<Register>();
2034 Register rhs_low = rhs_location.AsRegisterPairLow<Register>();
2035 if (instruction->IsAnd()) {
2036 __ And(dst_low, lhs_low, rhs_low);
2037 __ And(dst_high, lhs_high, rhs_high);
2038 } else if (instruction->IsOr()) {
2039 __ Or(dst_low, lhs_low, rhs_low);
2040 __ Or(dst_high, lhs_high, rhs_high);
2041 } else if (instruction->IsXor()) {
2042 __ Xor(dst_low, lhs_low, rhs_low);
2043 __ Xor(dst_high, lhs_high, rhs_high);
2044 } else if (instruction->IsAdd()) {
2045 if (lhs_low == rhs_low) {
2046 // Special case for lhs = rhs and the sum potentially overwriting both lhs and rhs.
2047 __ Slt(TMP, lhs_low, ZERO);
2048 __ Addu(dst_low, lhs_low, rhs_low);
2049 } else {
2050 __ Addu(dst_low, lhs_low, rhs_low);
2051 // If the sum overwrites rhs, lhs remains unchanged, otherwise rhs remains unchanged.
2052 __ Sltu(TMP, dst_low, (dst_low == rhs_low) ? lhs_low : rhs_low);
2053 }
2054 __ Addu(dst_high, lhs_high, rhs_high);
2055 __ Addu(dst_high, dst_high, TMP);
2056 } else {
2057 DCHECK(instruction->IsSub());
2058 __ Sltu(TMP, lhs_low, rhs_low);
2059 __ Subu(dst_low, lhs_low, rhs_low);
2060 __ Subu(dst_high, lhs_high, rhs_high);
2061 __ Subu(dst_high, dst_high, TMP);
2062 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002063 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002064 int64_t value = CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()->AsConstant());
2065 if (instruction->IsOr()) {
2066 uint32_t low = Low32Bits(value);
2067 uint32_t high = High32Bits(value);
2068 if (IsUint<16>(low)) {
2069 if (dst_low != lhs_low || low != 0) {
2070 __ Ori(dst_low, lhs_low, low);
2071 }
2072 } else {
2073 __ LoadConst32(TMP, low);
2074 __ Or(dst_low, lhs_low, TMP);
2075 }
2076 if (IsUint<16>(high)) {
2077 if (dst_high != lhs_high || high != 0) {
2078 __ Ori(dst_high, lhs_high, high);
2079 }
2080 } else {
2081 if (high != low) {
2082 __ LoadConst32(TMP, high);
2083 }
2084 __ Or(dst_high, lhs_high, TMP);
2085 }
2086 } else if (instruction->IsXor()) {
2087 uint32_t low = Low32Bits(value);
2088 uint32_t high = High32Bits(value);
2089 if (IsUint<16>(low)) {
2090 if (dst_low != lhs_low || low != 0) {
2091 __ Xori(dst_low, lhs_low, low);
2092 }
2093 } else {
2094 __ LoadConst32(TMP, low);
2095 __ Xor(dst_low, lhs_low, TMP);
2096 }
2097 if (IsUint<16>(high)) {
2098 if (dst_high != lhs_high || high != 0) {
2099 __ Xori(dst_high, lhs_high, high);
2100 }
2101 } else {
2102 if (high != low) {
2103 __ LoadConst32(TMP, high);
2104 }
2105 __ Xor(dst_high, lhs_high, TMP);
2106 }
2107 } else if (instruction->IsAnd()) {
2108 uint32_t low = Low32Bits(value);
2109 uint32_t high = High32Bits(value);
2110 if (IsUint<16>(low)) {
2111 __ Andi(dst_low, lhs_low, low);
2112 } else if (low != 0xFFFFFFFF) {
2113 __ LoadConst32(TMP, low);
2114 __ And(dst_low, lhs_low, TMP);
2115 } else if (dst_low != lhs_low) {
2116 __ Move(dst_low, lhs_low);
2117 }
2118 if (IsUint<16>(high)) {
2119 __ Andi(dst_high, lhs_high, high);
2120 } else if (high != 0xFFFFFFFF) {
2121 if (high != low) {
2122 __ LoadConst32(TMP, high);
2123 }
2124 __ And(dst_high, lhs_high, TMP);
2125 } else if (dst_high != lhs_high) {
2126 __ Move(dst_high, lhs_high);
2127 }
2128 } else {
2129 if (instruction->IsSub()) {
2130 value = -value;
2131 } else {
2132 DCHECK(instruction->IsAdd());
2133 }
2134 int32_t low = Low32Bits(value);
2135 int32_t high = High32Bits(value);
2136 if (IsInt<16>(low)) {
2137 if (dst_low != lhs_low || low != 0) {
2138 __ Addiu(dst_low, lhs_low, low);
2139 }
2140 if (low != 0) {
2141 __ Sltiu(AT, dst_low, low);
2142 }
2143 } else {
2144 __ LoadConst32(TMP, low);
2145 __ Addu(dst_low, lhs_low, TMP);
2146 __ Sltu(AT, dst_low, TMP);
2147 }
2148 if (IsInt<16>(high)) {
2149 if (dst_high != lhs_high || high != 0) {
2150 __ Addiu(dst_high, lhs_high, high);
2151 }
2152 } else {
2153 if (high != low) {
2154 __ LoadConst32(TMP, high);
2155 }
2156 __ Addu(dst_high, lhs_high, TMP);
2157 }
2158 if (low != 0) {
2159 __ Addu(dst_high, dst_high, AT);
2160 }
2161 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002162 }
2163 break;
2164 }
2165
2166 case Primitive::kPrimFloat:
2167 case Primitive::kPrimDouble: {
2168 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
2169 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
2170 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
2171 if (instruction->IsAdd()) {
2172 if (type == Primitive::kPrimFloat) {
2173 __ AddS(dst, lhs, rhs);
2174 } else {
2175 __ AddD(dst, lhs, rhs);
2176 }
2177 } else {
2178 DCHECK(instruction->IsSub());
2179 if (type == Primitive::kPrimFloat) {
2180 __ SubS(dst, lhs, rhs);
2181 } else {
2182 __ SubD(dst, lhs, rhs);
2183 }
2184 }
2185 break;
2186 }
2187
2188 default:
2189 LOG(FATAL) << "Unexpected binary operation type " << type;
2190 }
2191}
2192
2193void LocationsBuilderMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002194 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002195
2196 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instr);
2197 Primitive::Type type = instr->GetResultType();
2198 switch (type) {
2199 case Primitive::kPrimInt:
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002200 locations->SetInAt(0, Location::RequiresRegister());
2201 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2202 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2203 break;
2204 case Primitive::kPrimLong:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002205 locations->SetInAt(0, Location::RequiresRegister());
2206 locations->SetInAt(1, Location::RegisterOrConstant(instr->InputAt(1)));
2207 locations->SetOut(Location::RequiresRegister());
2208 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002209 default:
2210 LOG(FATAL) << "Unexpected shift type " << type;
2211 }
2212}
2213
2214static constexpr size_t kMipsBitsPerWord = kMipsWordSize * kBitsPerByte;
2215
2216void InstructionCodeGeneratorMIPS::HandleShift(HBinaryOperation* instr) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002217 DCHECK(instr->IsShl() || instr->IsShr() || instr->IsUShr() || instr->IsRor());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002218 LocationSummary* locations = instr->GetLocations();
2219 Primitive::Type type = instr->GetType();
2220
2221 Location rhs_location = locations->InAt(1);
2222 bool use_imm = rhs_location.IsConstant();
2223 Register rhs_reg = use_imm ? ZERO : rhs_location.AsRegister<Register>();
2224 int64_t rhs_imm = use_imm ? CodeGenerator::GetInt64ValueOf(rhs_location.GetConstant()) : 0;
Roland Levillain5b5b9312016-03-22 14:57:31 +00002225 const uint32_t shift_mask =
2226 (type == Primitive::kPrimInt) ? kMaxIntShiftDistance : kMaxLongShiftDistance;
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002227 const uint32_t shift_value = rhs_imm & shift_mask;
Alexey Frunze92d90602015-12-18 18:16:36 -08002228 // Are the INS (Insert Bit Field) and ROTR instructions supported?
2229 bool has_ins_rotr = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002230
2231 switch (type) {
2232 case Primitive::kPrimInt: {
2233 Register dst = locations->Out().AsRegister<Register>();
2234 Register lhs = locations->InAt(0).AsRegister<Register>();
2235 if (use_imm) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002236 if (shift_value == 0) {
2237 if (dst != lhs) {
2238 __ Move(dst, lhs);
2239 }
2240 } else if (instr->IsShl()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002241 __ Sll(dst, lhs, shift_value);
2242 } else if (instr->IsShr()) {
2243 __ Sra(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002244 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002245 __ Srl(dst, lhs, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002246 } else {
2247 if (has_ins_rotr) {
2248 __ Rotr(dst, lhs, shift_value);
2249 } else {
2250 __ Sll(TMP, lhs, (kMipsBitsPerWord - shift_value) & shift_mask);
2251 __ Srl(dst, lhs, shift_value);
2252 __ Or(dst, dst, TMP);
2253 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002254 }
2255 } else {
2256 if (instr->IsShl()) {
2257 __ Sllv(dst, lhs, rhs_reg);
2258 } else if (instr->IsShr()) {
2259 __ Srav(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002260 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002261 __ Srlv(dst, lhs, rhs_reg);
Alexey Frunze92d90602015-12-18 18:16:36 -08002262 } else {
2263 if (has_ins_rotr) {
2264 __ Rotrv(dst, lhs, rhs_reg);
2265 } else {
2266 __ Subu(TMP, ZERO, rhs_reg);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002267 // 32-bit shift instructions use the 5 least significant bits of the shift count, so
2268 // shifting by `-rhs_reg` is equivalent to shifting by `(32 - rhs_reg) & 31`. The case
2269 // when `rhs_reg & 31 == 0` is OK even though we don't shift `lhs` left all the way out
2270 // by 32, because the result in this case is computed as `(lhs >> 0) | (lhs << 0)`,
2271 // IOW, the OR'd values are equal.
Alexey Frunze92d90602015-12-18 18:16:36 -08002272 __ Sllv(TMP, lhs, TMP);
2273 __ Srlv(dst, lhs, rhs_reg);
2274 __ Or(dst, dst, TMP);
2275 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002276 }
2277 }
2278 break;
2279 }
2280
2281 case Primitive::kPrimLong: {
2282 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
2283 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
2284 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
2285 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
2286 if (use_imm) {
2287 if (shift_value == 0) {
2288 codegen_->Move64(locations->Out(), locations->InAt(0));
2289 } else if (shift_value < kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002290 if (has_ins_rotr) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002291 if (instr->IsShl()) {
2292 __ Srl(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2293 __ Ins(dst_high, lhs_high, shift_value, kMipsBitsPerWord - shift_value);
2294 __ Sll(dst_low, lhs_low, shift_value);
2295 } else if (instr->IsShr()) {
2296 __ Srl(dst_low, lhs_low, shift_value);
2297 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2298 __ Sra(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002299 } else if (instr->IsUShr()) {
2300 __ Srl(dst_low, lhs_low, shift_value);
2301 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2302 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002303 } else {
2304 __ Srl(dst_low, lhs_low, shift_value);
2305 __ Ins(dst_low, lhs_high, kMipsBitsPerWord - shift_value, shift_value);
2306 __ Srl(dst_high, lhs_high, shift_value);
Alexey Frunze92d90602015-12-18 18:16:36 -08002307 __ Ins(dst_high, lhs_low, kMipsBitsPerWord - shift_value, shift_value);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002308 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002309 } else {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002310 if (instr->IsShl()) {
2311 __ Sll(dst_low, lhs_low, shift_value);
2312 __ Srl(TMP, lhs_low, kMipsBitsPerWord - shift_value);
2313 __ Sll(dst_high, lhs_high, shift_value);
2314 __ Or(dst_high, dst_high, TMP);
2315 } else if (instr->IsShr()) {
2316 __ Sra(dst_high, lhs_high, shift_value);
2317 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2318 __ Srl(dst_low, lhs_low, shift_value);
2319 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002320 } else if (instr->IsUShr()) {
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002321 __ Srl(dst_high, lhs_high, shift_value);
2322 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value);
2323 __ Srl(dst_low, lhs_low, shift_value);
2324 __ Or(dst_low, dst_low, TMP);
Alexey Frunze92d90602015-12-18 18:16:36 -08002325 } else {
2326 __ Srl(TMP, lhs_low, shift_value);
2327 __ Sll(dst_low, lhs_high, kMipsBitsPerWord - shift_value);
2328 __ Or(dst_low, dst_low, TMP);
2329 __ Srl(TMP, lhs_high, shift_value);
2330 __ Sll(dst_high, lhs_low, kMipsBitsPerWord - shift_value);
2331 __ Or(dst_high, dst_high, TMP);
Alexey Frunze5c7aed32015-11-25 19:41:54 -08002332 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002333 }
2334 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002335 const uint32_t shift_value_high = shift_value - kMipsBitsPerWord;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002336 if (instr->IsShl()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002337 __ Sll(dst_high, lhs_low, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002338 __ Move(dst_low, ZERO);
2339 } else if (instr->IsShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002340 __ Sra(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002341 __ Sra(dst_high, dst_low, kMipsBitsPerWord - 1);
Alexey Frunze92d90602015-12-18 18:16:36 -08002342 } else if (instr->IsUShr()) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002343 __ Srl(dst_low, lhs_high, shift_value_high);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002344 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002345 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002346 if (shift_value == kMipsBitsPerWord) {
Alexey Frunze92d90602015-12-18 18:16:36 -08002347 // 64-bit rotation by 32 is just a swap.
2348 __ Move(dst_low, lhs_high);
2349 __ Move(dst_high, lhs_low);
2350 } else {
2351 if (has_ins_rotr) {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002352 __ Srl(dst_low, lhs_high, shift_value_high);
2353 __ Ins(dst_low, lhs_low, kMipsBitsPerWord - shift_value_high, shift_value_high);
2354 __ Srl(dst_high, lhs_low, shift_value_high);
2355 __ Ins(dst_high, lhs_high, kMipsBitsPerWord - shift_value_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002356 } else {
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002357 __ Sll(TMP, lhs_low, kMipsBitsPerWord - shift_value_high);
2358 __ Srl(dst_low, lhs_high, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002359 __ Or(dst_low, dst_low, TMP);
Alexey Frunze0d9150b2016-01-13 16:24:25 -08002360 __ Sll(TMP, lhs_high, kMipsBitsPerWord - shift_value_high);
2361 __ Srl(dst_high, lhs_low, shift_value_high);
Alexey Frunze92d90602015-12-18 18:16:36 -08002362 __ Or(dst_high, dst_high, TMP);
2363 }
2364 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002365 }
2366 }
2367 } else {
2368 MipsLabel done;
2369 if (instr->IsShl()) {
2370 __ Sllv(dst_low, lhs_low, rhs_reg);
2371 __ Nor(AT, ZERO, rhs_reg);
2372 __ Srl(TMP, lhs_low, 1);
2373 __ Srlv(TMP, TMP, AT);
2374 __ Sllv(dst_high, lhs_high, rhs_reg);
2375 __ Or(dst_high, dst_high, TMP);
2376 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2377 __ Beqz(TMP, &done);
2378 __ Move(dst_high, dst_low);
2379 __ Move(dst_low, ZERO);
2380 } else if (instr->IsShr()) {
2381 __ Srav(dst_high, lhs_high, rhs_reg);
2382 __ Nor(AT, ZERO, rhs_reg);
2383 __ Sll(TMP, lhs_high, 1);
2384 __ Sllv(TMP, TMP, AT);
2385 __ Srlv(dst_low, lhs_low, rhs_reg);
2386 __ Or(dst_low, dst_low, TMP);
2387 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2388 __ Beqz(TMP, &done);
2389 __ Move(dst_low, dst_high);
2390 __ Sra(dst_high, dst_high, 31);
Alexey Frunze92d90602015-12-18 18:16:36 -08002391 } else if (instr->IsUShr()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002392 __ Srlv(dst_high, lhs_high, rhs_reg);
2393 __ Nor(AT, ZERO, rhs_reg);
2394 __ Sll(TMP, lhs_high, 1);
2395 __ Sllv(TMP, TMP, AT);
2396 __ Srlv(dst_low, lhs_low, rhs_reg);
2397 __ Or(dst_low, dst_low, TMP);
2398 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2399 __ Beqz(TMP, &done);
2400 __ Move(dst_low, dst_high);
2401 __ Move(dst_high, ZERO);
Alexey Frunze92d90602015-12-18 18:16:36 -08002402 } else {
2403 __ Nor(AT, ZERO, rhs_reg);
2404 __ Srlv(TMP, lhs_low, rhs_reg);
2405 __ Sll(dst_low, lhs_high, 1);
2406 __ Sllv(dst_low, dst_low, AT);
2407 __ Or(dst_low, dst_low, TMP);
2408 __ Srlv(TMP, lhs_high, rhs_reg);
2409 __ Sll(dst_high, lhs_low, 1);
2410 __ Sllv(dst_high, dst_high, AT);
2411 __ Or(dst_high, dst_high, TMP);
2412 __ Andi(TMP, rhs_reg, kMipsBitsPerWord);
2413 __ Beqz(TMP, &done);
2414 __ Move(TMP, dst_high);
2415 __ Move(dst_high, dst_low);
2416 __ Move(dst_low, TMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002417 }
2418 __ Bind(&done);
2419 }
2420 break;
2421 }
2422
2423 default:
2424 LOG(FATAL) << "Unexpected shift operation type " << type;
2425 }
2426}
2427
2428void LocationsBuilderMIPS::VisitAdd(HAdd* instruction) {
2429 HandleBinaryOp(instruction);
2430}
2431
2432void InstructionCodeGeneratorMIPS::VisitAdd(HAdd* instruction) {
2433 HandleBinaryOp(instruction);
2434}
2435
2436void LocationsBuilderMIPS::VisitAnd(HAnd* instruction) {
2437 HandleBinaryOp(instruction);
2438}
2439
2440void InstructionCodeGeneratorMIPS::VisitAnd(HAnd* instruction) {
2441 HandleBinaryOp(instruction);
2442}
2443
2444void LocationsBuilderMIPS::VisitArrayGet(HArrayGet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002445 Primitive::Type type = instruction->GetType();
2446 bool object_array_get_with_read_barrier =
2447 kEmitCompilerReadBarrier && (type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002448 LocationSummary* locations =
Alexey Frunze15958152017-02-09 19:08:30 -08002449 new (GetGraph()->GetArena()) LocationSummary(instruction,
2450 object_array_get_with_read_barrier
2451 ? LocationSummary::kCallOnSlowPath
2452 : LocationSummary::kNoCall);
Alexey Frunzec61c0762017-04-10 13:54:23 -07002453 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2454 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
2455 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002456 locations->SetInAt(0, Location::RequiresRegister());
2457 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
Alexey Frunze15958152017-02-09 19:08:30 -08002458 if (Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002459 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
2460 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002461 // The output overlaps in the case of an object array get with
2462 // read barriers enabled: we do not want the move to overwrite the
2463 // array's location, as we need it to emit the read barrier.
2464 locations->SetOut(Location::RequiresRegister(),
2465 object_array_get_with_read_barrier
2466 ? Location::kOutputOverlap
2467 : Location::kNoOutputOverlap);
2468 }
2469 // We need a temporary register for the read barrier marking slow
2470 // path in CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier.
2471 if (object_array_get_with_read_barrier && kUseBakerReadBarrier) {
2472 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002473 }
2474}
2475
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002476static auto GetImplicitNullChecker(HInstruction* instruction, CodeGeneratorMIPS* codegen) {
2477 auto null_checker = [codegen, instruction]() {
2478 codegen->MaybeRecordImplicitNullCheck(instruction);
Alexey Frunze2923db72016-08-20 01:55:47 -07002479 };
2480 return null_checker;
2481}
2482
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002483void InstructionCodeGeneratorMIPS::VisitArrayGet(HArrayGet* instruction) {
2484 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08002485 Location obj_loc = locations->InAt(0);
2486 Register obj = obj_loc.AsRegister<Register>();
2487 Location out_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002488 Location index = locations->InAt(1);
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002489 uint32_t data_offset = CodeGenerator::GetArrayDataOffset(instruction);
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002490 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002491
Vladimir Marko87f3fcb2016-04-28 15:52:11 +01002492 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002493 const bool maybe_compressed_char_at = mirror::kUseStringCompression &&
2494 instruction->IsStringCharAt();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002495 switch (type) {
2496 case Primitive::kPrimBoolean: {
Alexey Frunze15958152017-02-09 19:08:30 -08002497 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002498 if (index.IsConstant()) {
2499 size_t offset =
2500 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002501 __ LoadFromOffset(kLoadUnsignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002502 } else {
2503 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002504 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002505 }
2506 break;
2507 }
2508
2509 case Primitive::kPrimByte: {
Alexey Frunze15958152017-02-09 19:08:30 -08002510 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002511 if (index.IsConstant()) {
2512 size_t offset =
2513 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002514 __ LoadFromOffset(kLoadSignedByte, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002515 } else {
2516 __ Addu(TMP, obj, index.AsRegister<Register>());
Alexey Frunze2923db72016-08-20 01:55:47 -07002517 __ LoadFromOffset(kLoadSignedByte, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002518 }
2519 break;
2520 }
2521
2522 case Primitive::kPrimShort: {
Alexey Frunze15958152017-02-09 19:08:30 -08002523 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002524 if (index.IsConstant()) {
2525 size_t offset =
2526 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002527 __ LoadFromOffset(kLoadSignedHalfword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002528 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002529 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_2, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002530 __ LoadFromOffset(kLoadSignedHalfword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002531 }
2532 break;
2533 }
2534
2535 case Primitive::kPrimChar: {
Alexey Frunze15958152017-02-09 19:08:30 -08002536 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002537 if (maybe_compressed_char_at) {
2538 uint32_t count_offset = mirror::String::CountOffset().Uint32Value();
2539 __ LoadFromOffset(kLoadWord, TMP, obj, count_offset, null_checker);
2540 __ Sll(TMP, TMP, 31); // Extract compression flag into the most significant bit of TMP.
2541 static_assert(static_cast<uint32_t>(mirror::StringCompressionFlag::kCompressed) == 0u,
2542 "Expecting 0=compressed, 1=uncompressed");
2543 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002544 if (index.IsConstant()) {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002545 int32_t const_index = index.GetConstant()->AsIntConstant()->GetValue();
2546 if (maybe_compressed_char_at) {
2547 MipsLabel uncompressed_load, done;
2548 __ Bnez(TMP, &uncompressed_load);
2549 __ LoadFromOffset(kLoadUnsignedByte,
2550 out,
2551 obj,
2552 data_offset + (const_index << TIMES_1));
2553 __ B(&done);
2554 __ Bind(&uncompressed_load);
2555 __ LoadFromOffset(kLoadUnsignedHalfword,
2556 out,
2557 obj,
2558 data_offset + (const_index << TIMES_2));
2559 __ Bind(&done);
2560 } else {
2561 __ LoadFromOffset(kLoadUnsignedHalfword,
2562 out,
2563 obj,
2564 data_offset + (const_index << TIMES_2),
2565 null_checker);
2566 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002567 } else {
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002568 Register index_reg = index.AsRegister<Register>();
2569 if (maybe_compressed_char_at) {
2570 MipsLabel uncompressed_load, done;
2571 __ Bnez(TMP, &uncompressed_load);
2572 __ Addu(TMP, obj, index_reg);
2573 __ LoadFromOffset(kLoadUnsignedByte, out, TMP, data_offset);
2574 __ B(&done);
2575 __ Bind(&uncompressed_load);
Chris Larsencd0295d2017-03-31 15:26:54 -07002576 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002577 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset);
2578 __ Bind(&done);
2579 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002580 __ ShiftAndAdd(TMP, index_reg, obj, TIMES_2, TMP);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002581 __ LoadFromOffset(kLoadUnsignedHalfword, out, TMP, data_offset, null_checker);
2582 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002583 }
2584 break;
2585 }
2586
Alexey Frunze15958152017-02-09 19:08:30 -08002587 case Primitive::kPrimInt: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002588 DCHECK_EQ(sizeof(mirror::HeapReference<mirror::Object>), sizeof(int32_t));
Alexey Frunze15958152017-02-09 19:08:30 -08002589 Register out = out_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002590 if (index.IsConstant()) {
2591 size_t offset =
2592 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002593 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002594 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002595 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002596 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002597 }
2598 break;
2599 }
2600
Alexey Frunze15958152017-02-09 19:08:30 -08002601 case Primitive::kPrimNot: {
2602 static_assert(
2603 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
2604 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
2605 // /* HeapReference<Object> */ out =
2606 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
2607 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
2608 Location temp = locations->GetTemp(0);
2609 // Note that a potential implicit null check is handled in this
2610 // CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier call.
2611 codegen_->GenerateArrayLoadWithBakerReadBarrier(instruction,
2612 out_loc,
2613 obj,
2614 data_offset,
2615 index,
2616 temp,
2617 /* needs_null_check */ true);
2618 } else {
2619 Register out = out_loc.AsRegister<Register>();
2620 if (index.IsConstant()) {
2621 size_t offset =
2622 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
2623 __ LoadFromOffset(kLoadWord, out, obj, offset, null_checker);
2624 // If read barriers are enabled, emit read barriers other than
2625 // Baker's using a slow path (and also unpoison the loaded
2626 // reference, if heap poisoning is enabled).
2627 codegen_->MaybeGenerateReadBarrierSlow(instruction, out_loc, out_loc, obj_loc, offset);
2628 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002629 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08002630 __ LoadFromOffset(kLoadWord, out, TMP, data_offset, null_checker);
2631 // If read barriers are enabled, emit read barriers other than
2632 // Baker's using a slow path (and also unpoison the loaded
2633 // reference, if heap poisoning is enabled).
2634 codegen_->MaybeGenerateReadBarrierSlow(instruction,
2635 out_loc,
2636 out_loc,
2637 obj_loc,
2638 data_offset,
2639 index);
2640 }
2641 }
2642 break;
2643 }
2644
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002645 case Primitive::kPrimLong: {
Alexey Frunze15958152017-02-09 19:08:30 -08002646 Register out = out_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002647 if (index.IsConstant()) {
2648 size_t offset =
2649 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002650 __ LoadFromOffset(kLoadDoubleword, out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002651 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002652 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002653 __ LoadFromOffset(kLoadDoubleword, out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002654 }
2655 break;
2656 }
2657
2658 case Primitive::kPrimFloat: {
Alexey Frunze15958152017-02-09 19:08:30 -08002659 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002660 if (index.IsConstant()) {
2661 size_t offset =
2662 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002663 __ LoadSFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002664 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002665 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_4, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002666 __ LoadSFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002667 }
2668 break;
2669 }
2670
2671 case Primitive::kPrimDouble: {
Alexey Frunze15958152017-02-09 19:08:30 -08002672 FRegister out = out_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002673 if (index.IsConstant()) {
2674 size_t offset =
2675 (index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8) + data_offset;
Alexey Frunze2923db72016-08-20 01:55:47 -07002676 __ LoadDFromOffset(out, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002677 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002678 __ ShiftAndAdd(TMP, index.AsRegister<Register>(), obj, TIMES_8, TMP);
Alexey Frunze2923db72016-08-20 01:55:47 -07002679 __ LoadDFromOffset(out, TMP, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002680 }
2681 break;
2682 }
2683
2684 case Primitive::kPrimVoid:
2685 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2686 UNREACHABLE();
2687 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002688}
2689
2690void LocationsBuilderMIPS::VisitArrayLength(HArrayLength* instruction) {
2691 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
2692 locations->SetInAt(0, Location::RequiresRegister());
2693 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
2694}
2695
2696void InstructionCodeGeneratorMIPS::VisitArrayLength(HArrayLength* instruction) {
2697 LocationSummary* locations = instruction->GetLocations();
Vladimir Markodce016e2016-04-28 13:10:02 +01002698 uint32_t offset = CodeGenerator::GetArrayLengthOffset(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002699 Register obj = locations->InAt(0).AsRegister<Register>();
2700 Register out = locations->Out().AsRegister<Register>();
2701 __ LoadFromOffset(kLoadWord, out, obj, offset);
2702 codegen_->MaybeRecordImplicitNullCheck(instruction);
Goran Jakovljevicf94fa812017-02-10 17:48:52 +01002703 // Mask out compression flag from String's array length.
2704 if (mirror::kUseStringCompression && instruction->IsStringLength()) {
2705 __ Srl(out, out, 1u);
2706 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002707}
2708
Alexey Frunzef58b2482016-09-02 22:14:06 -07002709Location LocationsBuilderMIPS::RegisterOrZeroConstant(HInstruction* instruction) {
2710 return (instruction->IsConstant() && instruction->AsConstant()->IsZeroBitPattern())
2711 ? Location::ConstantLocation(instruction->AsConstant())
2712 : Location::RequiresRegister();
2713}
2714
2715Location LocationsBuilderMIPS::FpuRegisterOrConstantForStore(HInstruction* instruction) {
2716 // We can store 0.0 directly (from the ZERO register) without loading it into an FPU register.
2717 // We can store a non-zero float or double constant without first loading it into the FPU,
2718 // but we should only prefer this if the constant has a single use.
2719 if (instruction->IsConstant() &&
2720 (instruction->AsConstant()->IsZeroBitPattern() ||
2721 instruction->GetUses().HasExactlyOneElement())) {
2722 return Location::ConstantLocation(instruction->AsConstant());
2723 // Otherwise fall through and require an FPU register for the constant.
2724 }
2725 return Location::RequiresFpuRegister();
2726}
2727
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002728void LocationsBuilderMIPS::VisitArraySet(HArraySet* instruction) {
Alexey Frunze15958152017-02-09 19:08:30 -08002729 Primitive::Type value_type = instruction->GetComponentType();
2730
2731 bool needs_write_barrier =
2732 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
2733 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
2734
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002735 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
2736 instruction,
Alexey Frunze15958152017-02-09 19:08:30 -08002737 may_need_runtime_call_for_type_check ?
2738 LocationSummary::kCallOnSlowPath :
2739 LocationSummary::kNoCall);
2740
2741 locations->SetInAt(0, Location::RequiresRegister());
2742 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
2743 if (Primitive::IsFloatingPointType(instruction->InputAt(2)->GetType())) {
2744 locations->SetInAt(2, FpuRegisterOrConstantForStore(instruction->InputAt(2)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002745 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08002746 locations->SetInAt(2, RegisterOrZeroConstant(instruction->InputAt(2)));
2747 }
2748 if (needs_write_barrier) {
2749 // Temporary register for the write barrier.
2750 locations->AddTemp(Location::RequiresRegister()); // Possibly used for ref. poisoning too.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002751 }
2752}
2753
2754void InstructionCodeGeneratorMIPS::VisitArraySet(HArraySet* instruction) {
2755 LocationSummary* locations = instruction->GetLocations();
2756 Register obj = locations->InAt(0).AsRegister<Register>();
2757 Location index = locations->InAt(1);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002758 Location value_location = locations->InAt(2);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002759 Primitive::Type value_type = instruction->GetComponentType();
Alexey Frunze15958152017-02-09 19:08:30 -08002760 bool may_need_runtime_call_for_type_check = instruction->NeedsTypeCheck();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002761 bool needs_write_barrier =
2762 CodeGenerator::StoreNeedsWriteBarrier(value_type, instruction->GetValue());
Tijana Jakovljevic57433862017-01-17 16:59:03 +01002763 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002764 Register base_reg = index.IsConstant() ? obj : TMP;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002765
2766 switch (value_type) {
2767 case Primitive::kPrimBoolean:
2768 case Primitive::kPrimByte: {
2769 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint8_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002770 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002771 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_1;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002772 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002773 __ Addu(base_reg, obj, index.AsRegister<Register>());
2774 }
2775 if (value_location.IsConstant()) {
2776 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2777 __ StoreConstToOffset(kStoreByte, value, base_reg, data_offset, TMP, null_checker);
2778 } else {
2779 Register value = value_location.AsRegister<Register>();
2780 __ StoreToOffset(kStoreByte, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002781 }
2782 break;
2783 }
2784
2785 case Primitive::kPrimShort:
2786 case Primitive::kPrimChar: {
2787 uint32_t data_offset = mirror::Array::DataOffset(sizeof(uint16_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002788 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002789 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_2;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002790 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002791 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_2, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002792 }
2793 if (value_location.IsConstant()) {
2794 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2795 __ StoreConstToOffset(kStoreHalfword, value, base_reg, data_offset, TMP, null_checker);
2796 } else {
2797 Register value = value_location.AsRegister<Register>();
2798 __ StoreToOffset(kStoreHalfword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002799 }
2800 break;
2801 }
2802
Alexey Frunze15958152017-02-09 19:08:30 -08002803 case Primitive::kPrimInt: {
2804 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2805 if (index.IsConstant()) {
2806 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
2807 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002808 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002809 }
2810 if (value_location.IsConstant()) {
2811 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2812 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2813 } else {
2814 Register value = value_location.AsRegister<Register>();
2815 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2816 }
2817 break;
2818 }
2819
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002820 case Primitive::kPrimNot: {
Alexey Frunze15958152017-02-09 19:08:30 -08002821 if (value_location.IsConstant()) {
2822 // Just setting null.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002823 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002824 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002825 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002826 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002827 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002828 }
Alexey Frunze15958152017-02-09 19:08:30 -08002829 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2830 DCHECK_EQ(value, 0);
2831 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2832 DCHECK(!needs_write_barrier);
2833 DCHECK(!may_need_runtime_call_for_type_check);
2834 break;
2835 }
2836
2837 DCHECK(needs_write_barrier);
2838 Register value = value_location.AsRegister<Register>();
2839 Register temp1 = locations->GetTemp(0).AsRegister<Register>();
2840 Register temp2 = TMP; // Doesn't need to survive slow path.
2841 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
2842 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
2843 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
2844 MipsLabel done;
2845 SlowPathCodeMIPS* slow_path = nullptr;
2846
2847 if (may_need_runtime_call_for_type_check) {
2848 slow_path = new (GetGraph()->GetArena()) ArraySetSlowPathMIPS(instruction);
2849 codegen_->AddSlowPath(slow_path);
2850 if (instruction->GetValueCanBeNull()) {
2851 MipsLabel non_zero;
2852 __ Bnez(value, &non_zero);
2853 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2854 if (index.IsConstant()) {
2855 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Alexey Frunzec061de12017-02-14 13:27:23 -08002856 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002857 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzec061de12017-02-14 13:27:23 -08002858 }
Alexey Frunze15958152017-02-09 19:08:30 -08002859 __ StoreToOffset(kStoreWord, value, base_reg, data_offset, null_checker);
2860 __ B(&done);
2861 __ Bind(&non_zero);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002862 }
Alexey Frunze15958152017-02-09 19:08:30 -08002863
2864 // Note that when read barriers are enabled, the type checks
2865 // are performed without read barriers. This is fine, even in
2866 // the case where a class object is in the from-space after
2867 // the flip, as a comparison involving such a type would not
2868 // produce a false positive; it may of course produce a false
2869 // negative, in which case we would take the ArraySet slow
2870 // path.
2871
2872 // /* HeapReference<Class> */ temp1 = obj->klass_
2873 __ LoadFromOffset(kLoadWord, temp1, obj, class_offset, null_checker);
2874 __ MaybeUnpoisonHeapReference(temp1);
2875
2876 // /* HeapReference<Class> */ temp1 = temp1->component_type_
2877 __ LoadFromOffset(kLoadWord, temp1, temp1, component_offset);
2878 // /* HeapReference<Class> */ temp2 = value->klass_
2879 __ LoadFromOffset(kLoadWord, temp2, value, class_offset);
2880 // If heap poisoning is enabled, no need to unpoison `temp1`
2881 // nor `temp2`, as we are comparing two poisoned references.
2882
2883 if (instruction->StaticTypeOfArrayIsObjectArray()) {
2884 MipsLabel do_put;
2885 __ Beq(temp1, temp2, &do_put);
2886 // If heap poisoning is enabled, the `temp1` reference has
2887 // not been unpoisoned yet; unpoison it now.
2888 __ MaybeUnpoisonHeapReference(temp1);
2889
2890 // /* HeapReference<Class> */ temp1 = temp1->super_class_
2891 __ LoadFromOffset(kLoadWord, temp1, temp1, super_offset);
2892 // If heap poisoning is enabled, no need to unpoison
2893 // `temp1`, as we are comparing against null below.
2894 __ Bnez(temp1, slow_path->GetEntryLabel());
2895 __ Bind(&do_put);
2896 } else {
2897 __ Bne(temp1, temp2, slow_path->GetEntryLabel());
2898 }
2899 }
2900
2901 Register source = value;
2902 if (kPoisonHeapReferences) {
2903 // Note that in the case where `value` is a null reference,
2904 // we do not enter this block, as a null reference does not
2905 // need poisoning.
2906 __ Move(temp1, value);
2907 __ PoisonHeapReference(temp1);
2908 source = temp1;
2909 }
2910
2911 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int32_t)).Uint32Value();
2912 if (index.IsConstant()) {
2913 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002914 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002915 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunze15958152017-02-09 19:08:30 -08002916 }
2917 __ StoreToOffset(kStoreWord, source, base_reg, data_offset);
2918
2919 if (!may_need_runtime_call_for_type_check) {
2920 codegen_->MaybeRecordImplicitNullCheck(instruction);
2921 }
2922
2923 codegen_->MarkGCCard(obj, value, instruction->GetValueCanBeNull());
2924
2925 if (done.IsLinked()) {
2926 __ Bind(&done);
2927 }
2928
2929 if (slow_path != nullptr) {
2930 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002931 }
2932 break;
2933 }
2934
2935 case Primitive::kPrimLong: {
2936 uint32_t data_offset = mirror::Array::DataOffset(sizeof(int64_t)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002937 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002938 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002939 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002940 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002941 }
2942 if (value_location.IsConstant()) {
2943 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2944 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2945 } else {
2946 Register value = value_location.AsRegisterPairLow<Register>();
2947 __ StoreToOffset(kStoreDoubleword, value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002948 }
2949 break;
2950 }
2951
2952 case Primitive::kPrimFloat: {
2953 uint32_t data_offset = mirror::Array::DataOffset(sizeof(float)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002954 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002955 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_4;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002956 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002957 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_4, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002958 }
2959 if (value_location.IsConstant()) {
2960 int32_t value = CodeGenerator::GetInt32ValueOf(value_location.GetConstant());
2961 __ StoreConstToOffset(kStoreWord, value, base_reg, data_offset, TMP, null_checker);
2962 } else {
2963 FRegister value = value_location.AsFpuRegister<FRegister>();
2964 __ StoreSToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002965 }
2966 break;
2967 }
2968
2969 case Primitive::kPrimDouble: {
2970 uint32_t data_offset = mirror::Array::DataOffset(sizeof(double)).Uint32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002971 if (index.IsConstant()) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07002972 data_offset += index.GetConstant()->AsIntConstant()->GetValue() << TIMES_8;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002973 } else {
Chris Larsencd0295d2017-03-31 15:26:54 -07002974 __ ShiftAndAdd(base_reg, index.AsRegister<Register>(), obj, TIMES_8, base_reg);
Alexey Frunzef58b2482016-09-02 22:14:06 -07002975 }
2976 if (value_location.IsConstant()) {
2977 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
2978 __ StoreConstToOffset(kStoreDoubleword, value, base_reg, data_offset, TMP, null_checker);
2979 } else {
2980 FRegister value = value_location.AsFpuRegister<FRegister>();
2981 __ StoreDToOffset(value, base_reg, data_offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002982 }
2983 break;
2984 }
2985
2986 case Primitive::kPrimVoid:
2987 LOG(FATAL) << "Unreachable type " << instruction->GetType();
2988 UNREACHABLE();
2989 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002990}
2991
2992void LocationsBuilderMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01002993 RegisterSet caller_saves = RegisterSet::Empty();
2994 InvokeRuntimeCallingConvention calling_convention;
2995 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
2996 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
2997 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction, caller_saves);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02002998 locations->SetInAt(0, Location::RequiresRegister());
2999 locations->SetInAt(1, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003000}
3001
3002void InstructionCodeGeneratorMIPS::VisitBoundsCheck(HBoundsCheck* instruction) {
3003 LocationSummary* locations = instruction->GetLocations();
3004 BoundsCheckSlowPathMIPS* slow_path =
3005 new (GetGraph()->GetArena()) BoundsCheckSlowPathMIPS(instruction);
3006 codegen_->AddSlowPath(slow_path);
3007
3008 Register index = locations->InAt(0).AsRegister<Register>();
3009 Register length = locations->InAt(1).AsRegister<Register>();
3010
3011 // length is limited by the maximum positive signed 32-bit integer.
3012 // Unsigned comparison of length and index checks for index < 0
3013 // and for length <= index simultaneously.
3014 __ Bgeu(index, length, slow_path->GetEntryLabel());
3015}
3016
Alexey Frunze15958152017-02-09 19:08:30 -08003017// Temp is used for read barrier.
3018static size_t NumberOfInstanceOfTemps(TypeCheckKind type_check_kind) {
3019 if (kEmitCompilerReadBarrier &&
3020 (kUseBakerReadBarrier ||
3021 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3022 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3023 type_check_kind == TypeCheckKind::kArrayObjectCheck)) {
3024 return 1;
3025 }
3026 return 0;
3027}
3028
3029// Extra temp is used for read barrier.
3030static size_t NumberOfCheckCastTemps(TypeCheckKind type_check_kind) {
3031 return 1 + NumberOfInstanceOfTemps(type_check_kind);
3032}
3033
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003034void LocationsBuilderMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003035 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
3036 bool throws_into_catch = instruction->CanThrowIntoCatchBlock();
3037
3038 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
3039 switch (type_check_kind) {
3040 case TypeCheckKind::kExactCheck:
3041 case TypeCheckKind::kAbstractClassCheck:
3042 case TypeCheckKind::kClassHierarchyCheck:
3043 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08003044 call_kind = (throws_into_catch || kEmitCompilerReadBarrier)
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003045 ? LocationSummary::kCallOnSlowPath
3046 : LocationSummary::kNoCall; // In fact, call on a fatal (non-returning) slow path.
3047 break;
3048 case TypeCheckKind::kArrayCheck:
3049 case TypeCheckKind::kUnresolvedCheck:
3050 case TypeCheckKind::kInterfaceCheck:
3051 call_kind = LocationSummary::kCallOnSlowPath;
3052 break;
3053 }
3054
3055 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003056 locations->SetInAt(0, Location::RequiresRegister());
3057 locations->SetInAt(1, Location::RequiresRegister());
Alexey Frunze15958152017-02-09 19:08:30 -08003058 locations->AddRegisterTemps(NumberOfCheckCastTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003059}
3060
3061void InstructionCodeGeneratorMIPS::VisitCheckCast(HCheckCast* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003062 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003063 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08003064 Location obj_loc = locations->InAt(0);
3065 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003066 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08003067 Location temp_loc = locations->GetTemp(0);
3068 Register temp = temp_loc.AsRegister<Register>();
3069 const size_t num_temps = NumberOfCheckCastTemps(type_check_kind);
3070 DCHECK_LE(num_temps, 2u);
3071 Location maybe_temp2_loc = (num_temps >= 2) ? locations->GetTemp(1) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003072 const uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
3073 const uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
3074 const uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
3075 const uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
3076 const uint32_t iftable_offset = mirror::Class::IfTableOffset().Uint32Value();
3077 const uint32_t array_length_offset = mirror::Array::LengthOffset().Uint32Value();
3078 const uint32_t object_array_data_offset =
3079 mirror::Array::DataOffset(kHeapReferenceSize).Uint32Value();
3080 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003081
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003082 // Always false for read barriers since we may need to go to the entrypoint for non-fatal cases
3083 // from false negatives. The false negatives may come from avoiding read barriers below. Avoiding
3084 // read barriers is done for performance and code size reasons.
3085 bool is_type_check_slow_path_fatal = false;
3086 if (!kEmitCompilerReadBarrier) {
3087 is_type_check_slow_path_fatal =
3088 (type_check_kind == TypeCheckKind::kExactCheck ||
3089 type_check_kind == TypeCheckKind::kAbstractClassCheck ||
3090 type_check_kind == TypeCheckKind::kClassHierarchyCheck ||
3091 type_check_kind == TypeCheckKind::kArrayObjectCheck) &&
3092 !instruction->CanThrowIntoCatchBlock();
3093 }
3094 SlowPathCodeMIPS* slow_path =
3095 new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
3096 is_type_check_slow_path_fatal);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003097 codegen_->AddSlowPath(slow_path);
3098
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003099 // Avoid this check if we know `obj` is not null.
3100 if (instruction->MustDoNullCheck()) {
3101 __ Beqz(obj, &done);
3102 }
3103
3104 switch (type_check_kind) {
3105 case TypeCheckKind::kExactCheck:
3106 case TypeCheckKind::kArrayCheck: {
3107 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003108 GenerateReferenceLoadTwoRegisters(instruction,
3109 temp_loc,
3110 obj_loc,
3111 class_offset,
3112 maybe_temp2_loc,
3113 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003114 // Jump to slow path for throwing the exception or doing a
3115 // more involved array check.
3116 __ Bne(temp, cls, slow_path->GetEntryLabel());
3117 break;
3118 }
3119
3120 case TypeCheckKind::kAbstractClassCheck: {
3121 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003122 GenerateReferenceLoadTwoRegisters(instruction,
3123 temp_loc,
3124 obj_loc,
3125 class_offset,
3126 maybe_temp2_loc,
3127 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003128 // If the class is abstract, we eagerly fetch the super class of the
3129 // object to avoid doing a comparison we know will fail.
3130 MipsLabel loop;
3131 __ Bind(&loop);
3132 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003133 GenerateReferenceLoadOneRegister(instruction,
3134 temp_loc,
3135 super_offset,
3136 maybe_temp2_loc,
3137 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003138 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3139 // exception.
3140 __ Beqz(temp, slow_path->GetEntryLabel());
3141 // Otherwise, compare the classes.
3142 __ Bne(temp, cls, &loop);
3143 break;
3144 }
3145
3146 case TypeCheckKind::kClassHierarchyCheck: {
3147 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003148 GenerateReferenceLoadTwoRegisters(instruction,
3149 temp_loc,
3150 obj_loc,
3151 class_offset,
3152 maybe_temp2_loc,
3153 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003154 // Walk over the class hierarchy to find a match.
3155 MipsLabel loop;
3156 __ Bind(&loop);
3157 __ Beq(temp, cls, &done);
3158 // /* HeapReference<Class> */ temp = temp->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08003159 GenerateReferenceLoadOneRegister(instruction,
3160 temp_loc,
3161 super_offset,
3162 maybe_temp2_loc,
3163 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003164 // If the class reference currently in `temp` is null, jump to the slow path to throw the
3165 // exception. Otherwise, jump to the beginning of the loop.
3166 __ Bnez(temp, &loop);
3167 __ B(slow_path->GetEntryLabel());
3168 break;
3169 }
3170
3171 case TypeCheckKind::kArrayObjectCheck: {
3172 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003173 GenerateReferenceLoadTwoRegisters(instruction,
3174 temp_loc,
3175 obj_loc,
3176 class_offset,
3177 maybe_temp2_loc,
3178 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003179 // Do an exact check.
3180 __ Beq(temp, cls, &done);
3181 // Otherwise, we need to check that the object's class is a non-primitive array.
3182 // /* HeapReference<Class> */ temp = temp->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08003183 GenerateReferenceLoadOneRegister(instruction,
3184 temp_loc,
3185 component_offset,
3186 maybe_temp2_loc,
3187 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003188 // If the component type is null, jump to the slow path to throw the exception.
3189 __ Beqz(temp, slow_path->GetEntryLabel());
3190 // Otherwise, the object is indeed an array, further check that this component
3191 // type is not a primitive type.
3192 __ LoadFromOffset(kLoadUnsignedHalfword, temp, temp, primitive_offset);
3193 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
3194 __ Bnez(temp, slow_path->GetEntryLabel());
3195 break;
3196 }
3197
3198 case TypeCheckKind::kUnresolvedCheck:
3199 // We always go into the type check slow path for the unresolved check case.
3200 // We cannot directly call the CheckCast runtime entry point
3201 // without resorting to a type checking slow path here (i.e. by
3202 // calling InvokeRuntime directly), as it would require to
3203 // assign fixed registers for the inputs of this HInstanceOf
3204 // instruction (following the runtime calling convention), which
3205 // might be cluttered by the potential first read barrier
3206 // emission at the beginning of this method.
3207 __ B(slow_path->GetEntryLabel());
3208 break;
3209
3210 case TypeCheckKind::kInterfaceCheck: {
3211 // Avoid read barriers to improve performance of the fast path. We can not get false
3212 // positives by doing this.
3213 // /* HeapReference<Class> */ temp = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08003214 GenerateReferenceLoadTwoRegisters(instruction,
3215 temp_loc,
3216 obj_loc,
3217 class_offset,
3218 maybe_temp2_loc,
3219 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003220 // /* HeapReference<Class> */ temp = temp->iftable_
Alexey Frunze15958152017-02-09 19:08:30 -08003221 GenerateReferenceLoadTwoRegisters(instruction,
3222 temp_loc,
3223 temp_loc,
3224 iftable_offset,
3225 maybe_temp2_loc,
3226 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08003227 // Iftable is never null.
3228 __ Lw(TMP, temp, array_length_offset);
3229 // Loop through the iftable and check if any class matches.
3230 MipsLabel loop;
3231 __ Bind(&loop);
3232 __ Addiu(temp, temp, 2 * kHeapReferenceSize); // Possibly in delay slot on R2.
3233 __ Beqz(TMP, slow_path->GetEntryLabel());
3234 __ Lw(AT, temp, object_array_data_offset - 2 * kHeapReferenceSize);
3235 __ MaybeUnpoisonHeapReference(AT);
3236 // Go to next interface.
3237 __ Addiu(TMP, TMP, -2);
3238 // Compare the classes and continue the loop if they do not match.
3239 __ Bne(AT, cls, &loop);
3240 break;
3241 }
3242 }
3243
3244 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003245 __ Bind(slow_path->GetExitLabel());
3246}
3247
3248void LocationsBuilderMIPS::VisitClinitCheck(HClinitCheck* check) {
3249 LocationSummary* locations =
3250 new (GetGraph()->GetArena()) LocationSummary(check, LocationSummary::kCallOnSlowPath);
3251 locations->SetInAt(0, Location::RequiresRegister());
3252 if (check->HasUses()) {
3253 locations->SetOut(Location::SameAsFirstInput());
3254 }
3255}
3256
3257void InstructionCodeGeneratorMIPS::VisitClinitCheck(HClinitCheck* check) {
3258 // We assume the class is not null.
3259 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
3260 check->GetLoadClass(),
3261 check,
3262 check->GetDexPc(),
3263 true);
3264 codegen_->AddSlowPath(slow_path);
3265 GenerateClassInitializationCheck(slow_path,
3266 check->GetLocations()->InAt(0).AsRegister<Register>());
3267}
3268
3269void LocationsBuilderMIPS::VisitCompare(HCompare* compare) {
3270 Primitive::Type in_type = compare->InputAt(0)->GetType();
3271
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003272 LocationSummary* locations =
3273 new (GetGraph()->GetArena()) LocationSummary(compare, LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003274
3275 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003276 case Primitive::kPrimBoolean:
3277 case Primitive::kPrimByte:
3278 case Primitive::kPrimShort:
3279 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003280 case Primitive::kPrimInt:
Alexey Frunzee7697712016-09-15 21:37:49 -07003281 locations->SetInAt(0, Location::RequiresRegister());
3282 locations->SetInAt(1, Location::RequiresRegister());
3283 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3284 break;
3285
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003286 case Primitive::kPrimLong:
3287 locations->SetInAt(0, Location::RequiresRegister());
3288 locations->SetInAt(1, Location::RequiresRegister());
3289 // Output overlaps because it is written before doing the low comparison.
3290 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
3291 break;
3292
3293 case Primitive::kPrimFloat:
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003294 case Primitive::kPrimDouble:
3295 locations->SetInAt(0, Location::RequiresFpuRegister());
3296 locations->SetInAt(1, Location::RequiresFpuRegister());
3297 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003298 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003299
3300 default:
3301 LOG(FATAL) << "Unexpected type for compare operation " << in_type;
3302 }
3303}
3304
3305void InstructionCodeGeneratorMIPS::VisitCompare(HCompare* instruction) {
3306 LocationSummary* locations = instruction->GetLocations();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003307 Register res = locations->Out().AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003308 Primitive::Type in_type = instruction->InputAt(0)->GetType();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003309 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003310
3311 // 0 if: left == right
3312 // 1 if: left > right
3313 // -1 if: left < right
3314 switch (in_type) {
Roland Levillaina5c4a402016-03-15 15:02:50 +00003315 case Primitive::kPrimBoolean:
3316 case Primitive::kPrimByte:
3317 case Primitive::kPrimShort:
3318 case Primitive::kPrimChar:
Aart Bika19616e2016-02-01 18:57:58 -08003319 case Primitive::kPrimInt: {
3320 Register lhs = locations->InAt(0).AsRegister<Register>();
3321 Register rhs = locations->InAt(1).AsRegister<Register>();
3322 __ Slt(TMP, lhs, rhs);
3323 __ Slt(res, rhs, lhs);
3324 __ Subu(res, res, TMP);
3325 break;
3326 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003327 case Primitive::kPrimLong: {
3328 MipsLabel done;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003329 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
3330 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
3331 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
3332 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
3333 // TODO: more efficient (direct) comparison with a constant.
3334 __ Slt(TMP, lhs_high, rhs_high);
3335 __ Slt(AT, rhs_high, lhs_high); // Inverted: is actually gt.
3336 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3337 __ Bnez(res, &done); // If we compared ==, check if lower bits are also equal.
3338 __ Sltu(TMP, lhs_low, rhs_low);
3339 __ Sltu(AT, rhs_low, lhs_low); // Inverted: is actually gt.
3340 __ Subu(res, AT, TMP); // Result -1:1:0 for [ <, >, == ].
3341 __ Bind(&done);
3342 break;
3343 }
3344
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003345 case Primitive::kPrimFloat: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003346 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003347 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3348 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3349 MipsLabel done;
3350 if (isR6) {
3351 __ CmpEqS(FTMP, lhs, rhs);
3352 __ LoadConst32(res, 0);
3353 __ Bc1nez(FTMP, &done);
3354 if (gt_bias) {
3355 __ CmpLtS(FTMP, lhs, rhs);
3356 __ LoadConst32(res, -1);
3357 __ Bc1nez(FTMP, &done);
3358 __ LoadConst32(res, 1);
3359 } else {
3360 __ CmpLtS(FTMP, rhs, lhs);
3361 __ LoadConst32(res, 1);
3362 __ Bc1nez(FTMP, &done);
3363 __ LoadConst32(res, -1);
3364 }
3365 } else {
3366 if (gt_bias) {
3367 __ ColtS(0, lhs, rhs);
3368 __ LoadConst32(res, -1);
3369 __ Bc1t(0, &done);
3370 __ CeqS(0, lhs, rhs);
3371 __ LoadConst32(res, 1);
3372 __ Movt(res, ZERO, 0);
3373 } else {
3374 __ ColtS(0, rhs, lhs);
3375 __ LoadConst32(res, 1);
3376 __ Bc1t(0, &done);
3377 __ CeqS(0, lhs, rhs);
3378 __ LoadConst32(res, -1);
3379 __ Movt(res, ZERO, 0);
3380 }
3381 }
3382 __ Bind(&done);
3383 break;
3384 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003385 case Primitive::kPrimDouble: {
Roland Levillain32ca3752016-02-17 16:49:37 +00003386 bool gt_bias = instruction->IsGtBias();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003387 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3388 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3389 MipsLabel done;
3390 if (isR6) {
3391 __ CmpEqD(FTMP, lhs, rhs);
3392 __ LoadConst32(res, 0);
3393 __ Bc1nez(FTMP, &done);
3394 if (gt_bias) {
3395 __ CmpLtD(FTMP, lhs, rhs);
3396 __ LoadConst32(res, -1);
3397 __ Bc1nez(FTMP, &done);
3398 __ LoadConst32(res, 1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003399 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003400 __ CmpLtD(FTMP, rhs, lhs);
3401 __ LoadConst32(res, 1);
3402 __ Bc1nez(FTMP, &done);
3403 __ LoadConst32(res, -1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003404 }
3405 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003406 if (gt_bias) {
3407 __ ColtD(0, lhs, rhs);
3408 __ LoadConst32(res, -1);
3409 __ Bc1t(0, &done);
3410 __ CeqD(0, lhs, rhs);
3411 __ LoadConst32(res, 1);
3412 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003413 } else {
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003414 __ ColtD(0, rhs, lhs);
3415 __ LoadConst32(res, 1);
3416 __ Bc1t(0, &done);
3417 __ CeqD(0, lhs, rhs);
3418 __ LoadConst32(res, -1);
3419 __ Movt(res, ZERO, 0);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003420 }
3421 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003422 __ Bind(&done);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003423 break;
3424 }
3425
3426 default:
3427 LOG(FATAL) << "Unimplemented compare type " << in_type;
3428 }
3429}
3430
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003431void LocationsBuilderMIPS::HandleCondition(HCondition* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003432 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003433 switch (instruction->InputAt(0)->GetType()) {
3434 default:
3435 case Primitive::kPrimLong:
3436 locations->SetInAt(0, Location::RequiresRegister());
3437 locations->SetInAt(1, Location::RegisterOrConstant(instruction->InputAt(1)));
3438 break;
3439
3440 case Primitive::kPrimFloat:
3441 case Primitive::kPrimDouble:
3442 locations->SetInAt(0, Location::RequiresFpuRegister());
3443 locations->SetInAt(1, Location::RequiresFpuRegister());
3444 break;
3445 }
David Brazdilb3e773e2016-01-26 11:28:37 +00003446 if (!instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003447 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3448 }
3449}
3450
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00003451void InstructionCodeGeneratorMIPS::HandleCondition(HCondition* instruction) {
David Brazdilb3e773e2016-01-26 11:28:37 +00003452 if (instruction->IsEmittedAtUseSite()) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003453 return;
3454 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003455
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003456 Primitive::Type type = instruction->InputAt(0)->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003457 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003458
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003459 switch (type) {
3460 default:
3461 // Integer case.
3462 GenerateIntCompare(instruction->GetCondition(), locations);
3463 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003464
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003465 case Primitive::kPrimLong:
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01003466 GenerateLongCompare(instruction->GetCondition(), locations);
3467 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003468
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003469 case Primitive::kPrimFloat:
3470 case Primitive::kPrimDouble:
Alexey Frunze2ddb7172016-09-06 17:04:55 -07003471 GenerateFpCompare(instruction->GetCondition(), instruction->IsGtBias(), type, locations);
3472 return;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003473 }
3474}
3475
Alexey Frunze7e99e052015-11-24 19:28:01 -08003476void InstructionCodeGeneratorMIPS::DivRemOneOrMinusOne(HBinaryOperation* instruction) {
3477 DCHECK(instruction->IsDiv() || instruction->IsRem());
3478 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3479
3480 LocationSummary* locations = instruction->GetLocations();
3481 Location second = locations->InAt(1);
3482 DCHECK(second.IsConstant());
3483
3484 Register out = locations->Out().AsRegister<Register>();
3485 Register dividend = locations->InAt(0).AsRegister<Register>();
3486 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3487 DCHECK(imm == 1 || imm == -1);
3488
3489 if (instruction->IsRem()) {
3490 __ Move(out, ZERO);
3491 } else {
3492 if (imm == -1) {
3493 __ Subu(out, ZERO, dividend);
3494 } else if (out != dividend) {
3495 __ Move(out, dividend);
3496 }
3497 }
3498}
3499
3500void InstructionCodeGeneratorMIPS::DivRemByPowerOfTwo(HBinaryOperation* instruction) {
3501 DCHECK(instruction->IsDiv() || instruction->IsRem());
3502 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3503
3504 LocationSummary* locations = instruction->GetLocations();
3505 Location second = locations->InAt(1);
3506 DCHECK(second.IsConstant());
3507
3508 Register out = locations->Out().AsRegister<Register>();
3509 Register dividend = locations->InAt(0).AsRegister<Register>();
3510 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003511 uint32_t abs_imm = static_cast<uint32_t>(AbsOrMin(imm));
Alexey Frunze7e99e052015-11-24 19:28:01 -08003512 int ctz_imm = CTZ(abs_imm);
3513
3514 if (instruction->IsDiv()) {
3515 if (ctz_imm == 1) {
3516 // Fast path for division by +/-2, which is very common.
3517 __ Srl(TMP, dividend, 31);
3518 } else {
3519 __ Sra(TMP, dividend, 31);
3520 __ Srl(TMP, TMP, 32 - ctz_imm);
3521 }
3522 __ Addu(out, dividend, TMP);
3523 __ Sra(out, out, ctz_imm);
3524 if (imm < 0) {
3525 __ Subu(out, ZERO, out);
3526 }
3527 } else {
3528 if (ctz_imm == 1) {
3529 // Fast path for modulo +/-2, which is very common.
3530 __ Sra(TMP, dividend, 31);
3531 __ Subu(out, dividend, TMP);
3532 __ Andi(out, out, 1);
3533 __ Addu(out, out, TMP);
3534 } else {
3535 __ Sra(TMP, dividend, 31);
3536 __ Srl(TMP, TMP, 32 - ctz_imm);
3537 __ Addu(out, dividend, TMP);
3538 if (IsUint<16>(abs_imm - 1)) {
3539 __ Andi(out, out, abs_imm - 1);
3540 } else {
3541 __ Sll(out, out, 32 - ctz_imm);
3542 __ Srl(out, out, 32 - ctz_imm);
3543 }
3544 __ Subu(out, out, TMP);
3545 }
3546 }
3547}
3548
3549void InstructionCodeGeneratorMIPS::GenerateDivRemWithAnyConstant(HBinaryOperation* instruction) {
3550 DCHECK(instruction->IsDiv() || instruction->IsRem());
3551 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3552
3553 LocationSummary* locations = instruction->GetLocations();
3554 Location second = locations->InAt(1);
3555 DCHECK(second.IsConstant());
3556
3557 Register out = locations->Out().AsRegister<Register>();
3558 Register dividend = locations->InAt(0).AsRegister<Register>();
3559 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3560
3561 int64_t magic;
3562 int shift;
3563 CalculateMagicAndShiftForDivRem(imm, false /* is_long */, &magic, &shift);
3564
3565 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3566
3567 __ LoadConst32(TMP, magic);
3568 if (isR6) {
3569 __ MuhR6(TMP, dividend, TMP);
3570 } else {
3571 __ MultR2(dividend, TMP);
3572 __ Mfhi(TMP);
3573 }
3574 if (imm > 0 && magic < 0) {
3575 __ Addu(TMP, TMP, dividend);
3576 } else if (imm < 0 && magic > 0) {
3577 __ Subu(TMP, TMP, dividend);
3578 }
3579
3580 if (shift != 0) {
3581 __ Sra(TMP, TMP, shift);
3582 }
3583
3584 if (instruction->IsDiv()) {
3585 __ Sra(out, TMP, 31);
3586 __ Subu(out, TMP, out);
3587 } else {
3588 __ Sra(AT, TMP, 31);
3589 __ Subu(AT, TMP, AT);
3590 __ LoadConst32(TMP, imm);
3591 if (isR6) {
3592 __ MulR6(TMP, AT, TMP);
3593 } else {
3594 __ MulR2(TMP, AT, TMP);
3595 }
3596 __ Subu(out, dividend, TMP);
3597 }
3598}
3599
3600void InstructionCodeGeneratorMIPS::GenerateDivRemIntegral(HBinaryOperation* instruction) {
3601 DCHECK(instruction->IsDiv() || instruction->IsRem());
3602 DCHECK_EQ(instruction->GetResultType(), Primitive::kPrimInt);
3603
3604 LocationSummary* locations = instruction->GetLocations();
3605 Register out = locations->Out().AsRegister<Register>();
3606 Location second = locations->InAt(1);
3607
3608 if (second.IsConstant()) {
3609 int32_t imm = second.GetConstant()->AsIntConstant()->GetValue();
3610 if (imm == 0) {
3611 // Do not generate anything. DivZeroCheck would prevent any code to be executed.
3612 } else if (imm == 1 || imm == -1) {
3613 DivRemOneOrMinusOne(instruction);
Nicolas Geoffray68f62892016-01-04 08:39:49 +00003614 } else if (IsPowerOfTwo(AbsOrMin(imm))) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003615 DivRemByPowerOfTwo(instruction);
3616 } else {
3617 DCHECK(imm <= -2 || imm >= 2);
3618 GenerateDivRemWithAnyConstant(instruction);
3619 }
3620 } else {
3621 Register dividend = locations->InAt(0).AsRegister<Register>();
3622 Register divisor = second.AsRegister<Register>();
3623 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
3624 if (instruction->IsDiv()) {
3625 if (isR6) {
3626 __ DivR6(out, dividend, divisor);
3627 } else {
3628 __ DivR2(out, dividend, divisor);
3629 }
3630 } else {
3631 if (isR6) {
3632 __ ModR6(out, dividend, divisor);
3633 } else {
3634 __ ModR2(out, dividend, divisor);
3635 }
3636 }
3637 }
3638}
3639
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003640void LocationsBuilderMIPS::VisitDiv(HDiv* div) {
3641 Primitive::Type type = div->GetResultType();
3642 LocationSummary::CallKind call_kind = (type == Primitive::kPrimLong)
Serban Constantinescu54ff4822016-07-07 18:03:19 +01003643 ? LocationSummary::kCallOnMainOnly
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003644 : LocationSummary::kNoCall;
3645
3646 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(div, call_kind);
3647
3648 switch (type) {
3649 case Primitive::kPrimInt:
3650 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08003651 locations->SetInAt(1, Location::RegisterOrConstant(div->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003652 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
3653 break;
3654
3655 case Primitive::kPrimLong: {
3656 InvokeRuntimeCallingConvention calling_convention;
3657 locations->SetInAt(0, Location::RegisterPairLocation(
3658 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
3659 locations->SetInAt(1, Location::RegisterPairLocation(
3660 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
3661 locations->SetOut(calling_convention.GetReturnLocation(type));
3662 break;
3663 }
3664
3665 case Primitive::kPrimFloat:
3666 case Primitive::kPrimDouble:
3667 locations->SetInAt(0, Location::RequiresFpuRegister());
3668 locations->SetInAt(1, Location::RequiresFpuRegister());
3669 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
3670 break;
3671
3672 default:
3673 LOG(FATAL) << "Unexpected div type " << type;
3674 }
3675}
3676
3677void InstructionCodeGeneratorMIPS::VisitDiv(HDiv* instruction) {
3678 Primitive::Type type = instruction->GetType();
3679 LocationSummary* locations = instruction->GetLocations();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003680
3681 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08003682 case Primitive::kPrimInt:
3683 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003684 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003685 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01003686 codegen_->InvokeRuntime(kQuickLdiv, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003687 CheckEntrypointTypes<kQuickLdiv, int64_t, int64_t, int64_t>();
3688 break;
3689 }
3690 case Primitive::kPrimFloat:
3691 case Primitive::kPrimDouble: {
3692 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
3693 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
3694 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
3695 if (type == Primitive::kPrimFloat) {
3696 __ DivS(dst, lhs, rhs);
3697 } else {
3698 __ DivD(dst, lhs, rhs);
3699 }
3700 break;
3701 }
3702 default:
3703 LOG(FATAL) << "Unexpected div type " << type;
3704 }
3705}
3706
3707void LocationsBuilderMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01003708 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003709 locations->SetInAt(0, Location::RegisterOrConstant(instruction->InputAt(0)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003710}
3711
3712void InstructionCodeGeneratorMIPS::VisitDivZeroCheck(HDivZeroCheck* instruction) {
3713 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) DivZeroCheckSlowPathMIPS(instruction);
3714 codegen_->AddSlowPath(slow_path);
3715 Location value = instruction->GetLocations()->InAt(0);
3716 Primitive::Type type = instruction->GetType();
3717
3718 switch (type) {
Nicolas Geoffraye5671612016-03-16 11:03:54 +00003719 case Primitive::kPrimBoolean:
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02003720 case Primitive::kPrimByte:
3721 case Primitive::kPrimChar:
3722 case Primitive::kPrimShort:
3723 case Primitive::kPrimInt: {
3724 if (value.IsConstant()) {
3725 if (value.GetConstant()->AsIntConstant()->GetValue() == 0) {
3726 __ B(slow_path->GetEntryLabel());
3727 } else {
3728 // A division by a non-null constant is valid. We don't need to perform
3729 // any check, so simply fall through.
3730 }
3731 } else {
3732 DCHECK(value.IsRegister()) << value;
3733 __ Beqz(value.AsRegister<Register>(), slow_path->GetEntryLabel());
3734 }
3735 break;
3736 }
3737 case Primitive::kPrimLong: {
3738 if (value.IsConstant()) {
3739 if (value.GetConstant()->AsLongConstant()->GetValue() == 0) {
3740 __ B(slow_path->GetEntryLabel());
3741 } else {
3742 // A division by a non-null constant is valid. We don't need to perform
3743 // any check, so simply fall through.
3744 }
3745 } else {
3746 DCHECK(value.IsRegisterPair()) << value;
3747 __ Or(TMP, value.AsRegisterPairHigh<Register>(), value.AsRegisterPairLow<Register>());
3748 __ Beqz(TMP, slow_path->GetEntryLabel());
3749 }
3750 break;
3751 }
3752 default:
3753 LOG(FATAL) << "Unexpected type " << type << " for DivZeroCheck.";
3754 }
3755}
3756
3757void LocationsBuilderMIPS::VisitDoubleConstant(HDoubleConstant* constant) {
3758 LocationSummary* locations =
3759 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3760 locations->SetOut(Location::ConstantLocation(constant));
3761}
3762
3763void InstructionCodeGeneratorMIPS::VisitDoubleConstant(HDoubleConstant* cst ATTRIBUTE_UNUSED) {
3764 // Will be generated at use site.
3765}
3766
3767void LocationsBuilderMIPS::VisitExit(HExit* exit) {
3768 exit->SetLocations(nullptr);
3769}
3770
3771void InstructionCodeGeneratorMIPS::VisitExit(HExit* exit ATTRIBUTE_UNUSED) {
3772}
3773
3774void LocationsBuilderMIPS::VisitFloatConstant(HFloatConstant* constant) {
3775 LocationSummary* locations =
3776 new (GetGraph()->GetArena()) LocationSummary(constant, LocationSummary::kNoCall);
3777 locations->SetOut(Location::ConstantLocation(constant));
3778}
3779
3780void InstructionCodeGeneratorMIPS::VisitFloatConstant(HFloatConstant* constant ATTRIBUTE_UNUSED) {
3781 // Will be generated at use site.
3782}
3783
3784void LocationsBuilderMIPS::VisitGoto(HGoto* got) {
3785 got->SetLocations(nullptr);
3786}
3787
3788void InstructionCodeGeneratorMIPS::HandleGoto(HInstruction* got, HBasicBlock* successor) {
3789 DCHECK(!successor->IsExitBlock());
3790 HBasicBlock* block = got->GetBlock();
3791 HInstruction* previous = got->GetPrevious();
3792 HLoopInformation* info = block->GetLoopInformation();
3793
3794 if (info != nullptr && info->IsBackEdge(*block) && info->HasSuspendCheck()) {
3795 codegen_->ClearSpillSlotsFromLoopPhisInStackMap(info->GetSuspendCheck());
3796 GenerateSuspendCheck(info->GetSuspendCheck(), successor);
3797 return;
3798 }
3799 if (block->IsEntryBlock() && (previous != nullptr) && previous->IsSuspendCheck()) {
3800 GenerateSuspendCheck(previous->AsSuspendCheck(), nullptr);
3801 }
3802 if (!codegen_->GoesToNextBlock(block, successor)) {
3803 __ B(codegen_->GetLabelOf(successor));
3804 }
3805}
3806
3807void InstructionCodeGeneratorMIPS::VisitGoto(HGoto* got) {
3808 HandleGoto(got, got->GetSuccessor());
3809}
3810
3811void LocationsBuilderMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3812 try_boundary->SetLocations(nullptr);
3813}
3814
3815void InstructionCodeGeneratorMIPS::VisitTryBoundary(HTryBoundary* try_boundary) {
3816 HBasicBlock* successor = try_boundary->GetNormalFlowSuccessor();
3817 if (!successor->IsExitBlock()) {
3818 HandleGoto(try_boundary, successor);
3819 }
3820}
3821
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003822void InstructionCodeGeneratorMIPS::GenerateIntCompare(IfCondition cond,
3823 LocationSummary* locations) {
3824 Register dst = locations->Out().AsRegister<Register>();
3825 Register lhs = locations->InAt(0).AsRegister<Register>();
3826 Location rhs_location = locations->InAt(1);
3827 Register rhs_reg = ZERO;
3828 int64_t rhs_imm = 0;
3829 bool use_imm = rhs_location.IsConstant();
3830 if (use_imm) {
3831 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3832 } else {
3833 rhs_reg = rhs_location.AsRegister<Register>();
3834 }
3835
3836 switch (cond) {
3837 case kCondEQ:
3838 case kCondNE:
Alexey Frunzee7697712016-09-15 21:37:49 -07003839 if (use_imm && IsInt<16>(-rhs_imm)) {
3840 if (rhs_imm == 0) {
3841 if (cond == kCondEQ) {
3842 __ Sltiu(dst, lhs, 1);
3843 } else {
3844 __ Sltu(dst, ZERO, lhs);
3845 }
3846 } else {
3847 __ Addiu(dst, lhs, -rhs_imm);
3848 if (cond == kCondEQ) {
3849 __ Sltiu(dst, dst, 1);
3850 } else {
3851 __ Sltu(dst, ZERO, dst);
3852 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003853 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003854 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07003855 if (use_imm && IsUint<16>(rhs_imm)) {
3856 __ Xori(dst, lhs, rhs_imm);
3857 } else {
3858 if (use_imm) {
3859 rhs_reg = TMP;
3860 __ LoadConst32(rhs_reg, rhs_imm);
3861 }
3862 __ Xor(dst, lhs, rhs_reg);
3863 }
3864 if (cond == kCondEQ) {
3865 __ Sltiu(dst, dst, 1);
3866 } else {
3867 __ Sltu(dst, ZERO, dst);
3868 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08003869 }
3870 break;
3871
3872 case kCondLT:
3873 case kCondGE:
3874 if (use_imm && IsInt<16>(rhs_imm)) {
3875 __ Slti(dst, lhs, rhs_imm);
3876 } else {
3877 if (use_imm) {
3878 rhs_reg = TMP;
3879 __ LoadConst32(rhs_reg, rhs_imm);
3880 }
3881 __ Slt(dst, lhs, rhs_reg);
3882 }
3883 if (cond == kCondGE) {
3884 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3885 // only the slt instruction but no sge.
3886 __ Xori(dst, dst, 1);
3887 }
3888 break;
3889
3890 case kCondLE:
3891 case kCondGT:
3892 if (use_imm && IsInt<16>(rhs_imm + 1)) {
3893 // Simulate lhs <= rhs via lhs < rhs + 1.
3894 __ Slti(dst, lhs, rhs_imm + 1);
3895 if (cond == kCondGT) {
3896 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3897 // only the slti instruction but no sgti.
3898 __ Xori(dst, dst, 1);
3899 }
3900 } else {
3901 if (use_imm) {
3902 rhs_reg = TMP;
3903 __ LoadConst32(rhs_reg, rhs_imm);
3904 }
3905 __ Slt(dst, rhs_reg, lhs);
3906 if (cond == kCondLE) {
3907 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3908 // only the slt instruction but no sle.
3909 __ Xori(dst, dst, 1);
3910 }
3911 }
3912 break;
3913
3914 case kCondB:
3915 case kCondAE:
3916 if (use_imm && IsInt<16>(rhs_imm)) {
3917 // Sltiu sign-extends its 16-bit immediate operand before
3918 // the comparison and thus lets us compare directly with
3919 // unsigned values in the ranges [0, 0x7fff] and
3920 // [0xffff8000, 0xffffffff].
3921 __ Sltiu(dst, lhs, rhs_imm);
3922 } else {
3923 if (use_imm) {
3924 rhs_reg = TMP;
3925 __ LoadConst32(rhs_reg, rhs_imm);
3926 }
3927 __ Sltu(dst, lhs, rhs_reg);
3928 }
3929 if (cond == kCondAE) {
3930 // Simulate lhs >= rhs via !(lhs < rhs) since there's
3931 // only the sltu instruction but no sgeu.
3932 __ Xori(dst, dst, 1);
3933 }
3934 break;
3935
3936 case kCondBE:
3937 case kCondA:
3938 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
3939 // Simulate lhs <= rhs via lhs < rhs + 1.
3940 // Note that this only works if rhs + 1 does not overflow
3941 // to 0, hence the check above.
3942 // Sltiu sign-extends its 16-bit immediate operand before
3943 // the comparison and thus lets us compare directly with
3944 // unsigned values in the ranges [0, 0x7fff] and
3945 // [0xffff8000, 0xffffffff].
3946 __ Sltiu(dst, lhs, rhs_imm + 1);
3947 if (cond == kCondA) {
3948 // Simulate lhs > rhs via !(lhs <= rhs) since there's
3949 // only the sltiu instruction but no sgtiu.
3950 __ Xori(dst, dst, 1);
3951 }
3952 } else {
3953 if (use_imm) {
3954 rhs_reg = TMP;
3955 __ LoadConst32(rhs_reg, rhs_imm);
3956 }
3957 __ Sltu(dst, rhs_reg, lhs);
3958 if (cond == kCondBE) {
3959 // Simulate lhs <= rhs via !(rhs < lhs) since there's
3960 // only the sltu instruction but no sleu.
3961 __ Xori(dst, dst, 1);
3962 }
3963 }
3964 break;
3965 }
3966}
3967
Alexey Frunze674b9ee2016-09-20 14:54:15 -07003968bool InstructionCodeGeneratorMIPS::MaterializeIntCompare(IfCondition cond,
3969 LocationSummary* input_locations,
3970 Register dst) {
3971 Register lhs = input_locations->InAt(0).AsRegister<Register>();
3972 Location rhs_location = input_locations->InAt(1);
3973 Register rhs_reg = ZERO;
3974 int64_t rhs_imm = 0;
3975 bool use_imm = rhs_location.IsConstant();
3976 if (use_imm) {
3977 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
3978 } else {
3979 rhs_reg = rhs_location.AsRegister<Register>();
3980 }
3981
3982 switch (cond) {
3983 case kCondEQ:
3984 case kCondNE:
3985 if (use_imm && IsInt<16>(-rhs_imm)) {
3986 __ Addiu(dst, lhs, -rhs_imm);
3987 } else if (use_imm && IsUint<16>(rhs_imm)) {
3988 __ Xori(dst, lhs, rhs_imm);
3989 } else {
3990 if (use_imm) {
3991 rhs_reg = TMP;
3992 __ LoadConst32(rhs_reg, rhs_imm);
3993 }
3994 __ Xor(dst, lhs, rhs_reg);
3995 }
3996 return (cond == kCondEQ);
3997
3998 case kCondLT:
3999 case kCondGE:
4000 if (use_imm && IsInt<16>(rhs_imm)) {
4001 __ Slti(dst, lhs, rhs_imm);
4002 } else {
4003 if (use_imm) {
4004 rhs_reg = TMP;
4005 __ LoadConst32(rhs_reg, rhs_imm);
4006 }
4007 __ Slt(dst, lhs, rhs_reg);
4008 }
4009 return (cond == kCondGE);
4010
4011 case kCondLE:
4012 case kCondGT:
4013 if (use_imm && IsInt<16>(rhs_imm + 1)) {
4014 // Simulate lhs <= rhs via lhs < rhs + 1.
4015 __ Slti(dst, lhs, rhs_imm + 1);
4016 return (cond == kCondGT);
4017 } else {
4018 if (use_imm) {
4019 rhs_reg = TMP;
4020 __ LoadConst32(rhs_reg, rhs_imm);
4021 }
4022 __ Slt(dst, rhs_reg, lhs);
4023 return (cond == kCondLE);
4024 }
4025
4026 case kCondB:
4027 case kCondAE:
4028 if (use_imm && IsInt<16>(rhs_imm)) {
4029 // Sltiu sign-extends its 16-bit immediate operand before
4030 // the comparison and thus lets us compare directly with
4031 // unsigned values in the ranges [0, 0x7fff] and
4032 // [0xffff8000, 0xffffffff].
4033 __ Sltiu(dst, lhs, rhs_imm);
4034 } else {
4035 if (use_imm) {
4036 rhs_reg = TMP;
4037 __ LoadConst32(rhs_reg, rhs_imm);
4038 }
4039 __ Sltu(dst, lhs, rhs_reg);
4040 }
4041 return (cond == kCondAE);
4042
4043 case kCondBE:
4044 case kCondA:
4045 if (use_imm && (rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4046 // Simulate lhs <= rhs via lhs < rhs + 1.
4047 // Note that this only works if rhs + 1 does not overflow
4048 // to 0, hence the check above.
4049 // Sltiu sign-extends its 16-bit immediate operand before
4050 // the comparison and thus lets us compare directly with
4051 // unsigned values in the ranges [0, 0x7fff] and
4052 // [0xffff8000, 0xffffffff].
4053 __ Sltiu(dst, lhs, rhs_imm + 1);
4054 return (cond == kCondA);
4055 } else {
4056 if (use_imm) {
4057 rhs_reg = TMP;
4058 __ LoadConst32(rhs_reg, rhs_imm);
4059 }
4060 __ Sltu(dst, rhs_reg, lhs);
4061 return (cond == kCondBE);
4062 }
4063 }
4064}
4065
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004066void InstructionCodeGeneratorMIPS::GenerateIntCompareAndBranch(IfCondition cond,
4067 LocationSummary* locations,
4068 MipsLabel* label) {
4069 Register lhs = locations->InAt(0).AsRegister<Register>();
4070 Location rhs_location = locations->InAt(1);
4071 Register rhs_reg = ZERO;
Alexey Frunzee7697712016-09-15 21:37:49 -07004072 int64_t rhs_imm = 0;
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004073 bool use_imm = rhs_location.IsConstant();
4074 if (use_imm) {
4075 rhs_imm = CodeGenerator::GetInt32ValueOf(rhs_location.GetConstant());
4076 } else {
4077 rhs_reg = rhs_location.AsRegister<Register>();
4078 }
4079
4080 if (use_imm && rhs_imm == 0) {
4081 switch (cond) {
4082 case kCondEQ:
4083 case kCondBE: // <= 0 if zero
4084 __ Beqz(lhs, label);
4085 break;
4086 case kCondNE:
4087 case kCondA: // > 0 if non-zero
4088 __ Bnez(lhs, label);
4089 break;
4090 case kCondLT:
4091 __ Bltz(lhs, label);
4092 break;
4093 case kCondGE:
4094 __ Bgez(lhs, label);
4095 break;
4096 case kCondLE:
4097 __ Blez(lhs, label);
4098 break;
4099 case kCondGT:
4100 __ Bgtz(lhs, label);
4101 break;
4102 case kCondB: // always false
4103 break;
4104 case kCondAE: // always true
4105 __ B(label);
4106 break;
4107 }
4108 } else {
Alexey Frunzee7697712016-09-15 21:37:49 -07004109 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4110 if (isR6 || !use_imm) {
4111 if (use_imm) {
4112 rhs_reg = TMP;
4113 __ LoadConst32(rhs_reg, rhs_imm);
4114 }
4115 switch (cond) {
4116 case kCondEQ:
4117 __ Beq(lhs, rhs_reg, label);
4118 break;
4119 case kCondNE:
4120 __ Bne(lhs, rhs_reg, label);
4121 break;
4122 case kCondLT:
4123 __ Blt(lhs, rhs_reg, label);
4124 break;
4125 case kCondGE:
4126 __ Bge(lhs, rhs_reg, label);
4127 break;
4128 case kCondLE:
4129 __ Bge(rhs_reg, lhs, label);
4130 break;
4131 case kCondGT:
4132 __ Blt(rhs_reg, lhs, label);
4133 break;
4134 case kCondB:
4135 __ Bltu(lhs, rhs_reg, label);
4136 break;
4137 case kCondAE:
4138 __ Bgeu(lhs, rhs_reg, label);
4139 break;
4140 case kCondBE:
4141 __ Bgeu(rhs_reg, lhs, label);
4142 break;
4143 case kCondA:
4144 __ Bltu(rhs_reg, lhs, label);
4145 break;
4146 }
4147 } else {
4148 // Special cases for more efficient comparison with constants on R2.
4149 switch (cond) {
4150 case kCondEQ:
4151 __ LoadConst32(TMP, rhs_imm);
4152 __ Beq(lhs, TMP, label);
4153 break;
4154 case kCondNE:
4155 __ LoadConst32(TMP, rhs_imm);
4156 __ Bne(lhs, TMP, label);
4157 break;
4158 case kCondLT:
4159 if (IsInt<16>(rhs_imm)) {
4160 __ Slti(TMP, lhs, rhs_imm);
4161 __ Bnez(TMP, label);
4162 } else {
4163 __ LoadConst32(TMP, rhs_imm);
4164 __ Blt(lhs, TMP, label);
4165 }
4166 break;
4167 case kCondGE:
4168 if (IsInt<16>(rhs_imm)) {
4169 __ Slti(TMP, lhs, rhs_imm);
4170 __ Beqz(TMP, label);
4171 } else {
4172 __ LoadConst32(TMP, rhs_imm);
4173 __ Bge(lhs, TMP, label);
4174 }
4175 break;
4176 case kCondLE:
4177 if (IsInt<16>(rhs_imm + 1)) {
4178 // Simulate lhs <= rhs via lhs < rhs + 1.
4179 __ Slti(TMP, lhs, rhs_imm + 1);
4180 __ Bnez(TMP, label);
4181 } else {
4182 __ LoadConst32(TMP, rhs_imm);
4183 __ Bge(TMP, lhs, label);
4184 }
4185 break;
4186 case kCondGT:
4187 if (IsInt<16>(rhs_imm + 1)) {
4188 // Simulate lhs > rhs via !(lhs < rhs + 1).
4189 __ Slti(TMP, lhs, rhs_imm + 1);
4190 __ Beqz(TMP, label);
4191 } else {
4192 __ LoadConst32(TMP, rhs_imm);
4193 __ Blt(TMP, lhs, label);
4194 }
4195 break;
4196 case kCondB:
4197 if (IsInt<16>(rhs_imm)) {
4198 __ Sltiu(TMP, lhs, rhs_imm);
4199 __ Bnez(TMP, label);
4200 } else {
4201 __ LoadConst32(TMP, rhs_imm);
4202 __ Bltu(lhs, TMP, label);
4203 }
4204 break;
4205 case kCondAE:
4206 if (IsInt<16>(rhs_imm)) {
4207 __ Sltiu(TMP, lhs, rhs_imm);
4208 __ Beqz(TMP, label);
4209 } else {
4210 __ LoadConst32(TMP, rhs_imm);
4211 __ Bgeu(lhs, TMP, label);
4212 }
4213 break;
4214 case kCondBE:
4215 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4216 // Simulate lhs <= rhs via lhs < rhs + 1.
4217 // Note that this only works if rhs + 1 does not overflow
4218 // to 0, hence the check above.
4219 __ Sltiu(TMP, lhs, rhs_imm + 1);
4220 __ Bnez(TMP, label);
4221 } else {
4222 __ LoadConst32(TMP, rhs_imm);
4223 __ Bgeu(TMP, lhs, label);
4224 }
4225 break;
4226 case kCondA:
4227 if ((rhs_imm != -1) && IsInt<16>(rhs_imm + 1)) {
4228 // Simulate lhs > rhs via !(lhs < rhs + 1).
4229 // Note that this only works if rhs + 1 does not overflow
4230 // to 0, hence the check above.
4231 __ Sltiu(TMP, lhs, rhs_imm + 1);
4232 __ Beqz(TMP, label);
4233 } else {
4234 __ LoadConst32(TMP, rhs_imm);
4235 __ Bltu(TMP, lhs, label);
4236 }
4237 break;
4238 }
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004239 }
4240 }
4241}
4242
Tijana Jakovljevic6d482aa2017-02-03 13:24:08 +01004243void InstructionCodeGeneratorMIPS::GenerateLongCompare(IfCondition cond,
4244 LocationSummary* locations) {
4245 Register dst = locations->Out().AsRegister<Register>();
4246 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4247 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4248 Location rhs_location = locations->InAt(1);
4249 Register rhs_high = ZERO;
4250 Register rhs_low = ZERO;
4251 int64_t imm = 0;
4252 uint32_t imm_high = 0;
4253 uint32_t imm_low = 0;
4254 bool use_imm = rhs_location.IsConstant();
4255 if (use_imm) {
4256 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4257 imm_high = High32Bits(imm);
4258 imm_low = Low32Bits(imm);
4259 } else {
4260 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4261 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4262 }
4263 if (use_imm && imm == 0) {
4264 switch (cond) {
4265 case kCondEQ:
4266 case kCondBE: // <= 0 if zero
4267 __ Or(dst, lhs_high, lhs_low);
4268 __ Sltiu(dst, dst, 1);
4269 break;
4270 case kCondNE:
4271 case kCondA: // > 0 if non-zero
4272 __ Or(dst, lhs_high, lhs_low);
4273 __ Sltu(dst, ZERO, dst);
4274 break;
4275 case kCondLT:
4276 __ Slt(dst, lhs_high, ZERO);
4277 break;
4278 case kCondGE:
4279 __ Slt(dst, lhs_high, ZERO);
4280 __ Xori(dst, dst, 1);
4281 break;
4282 case kCondLE:
4283 __ Or(TMP, lhs_high, lhs_low);
4284 __ Sra(AT, lhs_high, 31);
4285 __ Sltu(dst, AT, TMP);
4286 __ Xori(dst, dst, 1);
4287 break;
4288 case kCondGT:
4289 __ Or(TMP, lhs_high, lhs_low);
4290 __ Sra(AT, lhs_high, 31);
4291 __ Sltu(dst, AT, TMP);
4292 break;
4293 case kCondB: // always false
4294 __ Andi(dst, dst, 0);
4295 break;
4296 case kCondAE: // always true
4297 __ Ori(dst, ZERO, 1);
4298 break;
4299 }
4300 } else if (use_imm) {
4301 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4302 switch (cond) {
4303 case kCondEQ:
4304 __ LoadConst32(TMP, imm_high);
4305 __ Xor(TMP, TMP, lhs_high);
4306 __ LoadConst32(AT, imm_low);
4307 __ Xor(AT, AT, lhs_low);
4308 __ Or(dst, TMP, AT);
4309 __ Sltiu(dst, dst, 1);
4310 break;
4311 case kCondNE:
4312 __ LoadConst32(TMP, imm_high);
4313 __ Xor(TMP, TMP, lhs_high);
4314 __ LoadConst32(AT, imm_low);
4315 __ Xor(AT, AT, lhs_low);
4316 __ Or(dst, TMP, AT);
4317 __ Sltu(dst, ZERO, dst);
4318 break;
4319 case kCondLT:
4320 case kCondGE:
4321 if (dst == lhs_low) {
4322 __ LoadConst32(TMP, imm_low);
4323 __ Sltu(dst, lhs_low, TMP);
4324 }
4325 __ LoadConst32(TMP, imm_high);
4326 __ Slt(AT, lhs_high, TMP);
4327 __ Slt(TMP, TMP, lhs_high);
4328 if (dst != lhs_low) {
4329 __ LoadConst32(dst, imm_low);
4330 __ Sltu(dst, lhs_low, dst);
4331 }
4332 __ Slt(dst, TMP, dst);
4333 __ Or(dst, dst, AT);
4334 if (cond == kCondGE) {
4335 __ Xori(dst, dst, 1);
4336 }
4337 break;
4338 case kCondGT:
4339 case kCondLE:
4340 if (dst == lhs_low) {
4341 __ LoadConst32(TMP, imm_low);
4342 __ Sltu(dst, TMP, lhs_low);
4343 }
4344 __ LoadConst32(TMP, imm_high);
4345 __ Slt(AT, TMP, lhs_high);
4346 __ Slt(TMP, lhs_high, TMP);
4347 if (dst != lhs_low) {
4348 __ LoadConst32(dst, imm_low);
4349 __ Sltu(dst, dst, lhs_low);
4350 }
4351 __ Slt(dst, TMP, dst);
4352 __ Or(dst, dst, AT);
4353 if (cond == kCondLE) {
4354 __ Xori(dst, dst, 1);
4355 }
4356 break;
4357 case kCondB:
4358 case kCondAE:
4359 if (dst == lhs_low) {
4360 __ LoadConst32(TMP, imm_low);
4361 __ Sltu(dst, lhs_low, TMP);
4362 }
4363 __ LoadConst32(TMP, imm_high);
4364 __ Sltu(AT, lhs_high, TMP);
4365 __ Sltu(TMP, TMP, lhs_high);
4366 if (dst != lhs_low) {
4367 __ LoadConst32(dst, imm_low);
4368 __ Sltu(dst, lhs_low, dst);
4369 }
4370 __ Slt(dst, TMP, dst);
4371 __ Or(dst, dst, AT);
4372 if (cond == kCondAE) {
4373 __ Xori(dst, dst, 1);
4374 }
4375 break;
4376 case kCondA:
4377 case kCondBE:
4378 if (dst == lhs_low) {
4379 __ LoadConst32(TMP, imm_low);
4380 __ Sltu(dst, TMP, lhs_low);
4381 }
4382 __ LoadConst32(TMP, imm_high);
4383 __ Sltu(AT, TMP, lhs_high);
4384 __ Sltu(TMP, lhs_high, TMP);
4385 if (dst != lhs_low) {
4386 __ LoadConst32(dst, imm_low);
4387 __ Sltu(dst, dst, lhs_low);
4388 }
4389 __ Slt(dst, TMP, dst);
4390 __ Or(dst, dst, AT);
4391 if (cond == kCondBE) {
4392 __ Xori(dst, dst, 1);
4393 }
4394 break;
4395 }
4396 } else {
4397 switch (cond) {
4398 case kCondEQ:
4399 __ Xor(TMP, lhs_high, rhs_high);
4400 __ Xor(AT, lhs_low, rhs_low);
4401 __ Or(dst, TMP, AT);
4402 __ Sltiu(dst, dst, 1);
4403 break;
4404 case kCondNE:
4405 __ Xor(TMP, lhs_high, rhs_high);
4406 __ Xor(AT, lhs_low, rhs_low);
4407 __ Or(dst, TMP, AT);
4408 __ Sltu(dst, ZERO, dst);
4409 break;
4410 case kCondLT:
4411 case kCondGE:
4412 __ Slt(TMP, rhs_high, lhs_high);
4413 __ Sltu(AT, lhs_low, rhs_low);
4414 __ Slt(TMP, TMP, AT);
4415 __ Slt(AT, lhs_high, rhs_high);
4416 __ Or(dst, AT, TMP);
4417 if (cond == kCondGE) {
4418 __ Xori(dst, dst, 1);
4419 }
4420 break;
4421 case kCondGT:
4422 case kCondLE:
4423 __ Slt(TMP, lhs_high, rhs_high);
4424 __ Sltu(AT, rhs_low, lhs_low);
4425 __ Slt(TMP, TMP, AT);
4426 __ Slt(AT, rhs_high, lhs_high);
4427 __ Or(dst, AT, TMP);
4428 if (cond == kCondLE) {
4429 __ Xori(dst, dst, 1);
4430 }
4431 break;
4432 case kCondB:
4433 case kCondAE:
4434 __ Sltu(TMP, rhs_high, lhs_high);
4435 __ Sltu(AT, lhs_low, rhs_low);
4436 __ Slt(TMP, TMP, AT);
4437 __ Sltu(AT, lhs_high, rhs_high);
4438 __ Or(dst, AT, TMP);
4439 if (cond == kCondAE) {
4440 __ Xori(dst, dst, 1);
4441 }
4442 break;
4443 case kCondA:
4444 case kCondBE:
4445 __ Sltu(TMP, lhs_high, rhs_high);
4446 __ Sltu(AT, rhs_low, lhs_low);
4447 __ Slt(TMP, TMP, AT);
4448 __ Sltu(AT, rhs_high, lhs_high);
4449 __ Or(dst, AT, TMP);
4450 if (cond == kCondBE) {
4451 __ Xori(dst, dst, 1);
4452 }
4453 break;
4454 }
4455 }
4456}
4457
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08004458void InstructionCodeGeneratorMIPS::GenerateLongCompareAndBranch(IfCondition cond,
4459 LocationSummary* locations,
4460 MipsLabel* label) {
4461 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
4462 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
4463 Location rhs_location = locations->InAt(1);
4464 Register rhs_high = ZERO;
4465 Register rhs_low = ZERO;
4466 int64_t imm = 0;
4467 uint32_t imm_high = 0;
4468 uint32_t imm_low = 0;
4469 bool use_imm = rhs_location.IsConstant();
4470 if (use_imm) {
4471 imm = rhs_location.GetConstant()->AsLongConstant()->GetValue();
4472 imm_high = High32Bits(imm);
4473 imm_low = Low32Bits(imm);
4474 } else {
4475 rhs_high = rhs_location.AsRegisterPairHigh<Register>();
4476 rhs_low = rhs_location.AsRegisterPairLow<Register>();
4477 }
4478
4479 if (use_imm && imm == 0) {
4480 switch (cond) {
4481 case kCondEQ:
4482 case kCondBE: // <= 0 if zero
4483 __ Or(TMP, lhs_high, lhs_low);
4484 __ Beqz(TMP, label);
4485 break;
4486 case kCondNE:
4487 case kCondA: // > 0 if non-zero
4488 __ Or(TMP, lhs_high, lhs_low);
4489 __ Bnez(TMP, label);
4490 break;
4491 case kCondLT:
4492 __ Bltz(lhs_high, label);
4493 break;
4494 case kCondGE:
4495 __ Bgez(lhs_high, label);
4496 break;
4497 case kCondLE:
4498 __ Or(TMP, lhs_high, lhs_low);
4499 __ Sra(AT, lhs_high, 31);
4500 __ Bgeu(AT, TMP, label);
4501 break;
4502 case kCondGT:
4503 __ Or(TMP, lhs_high, lhs_low);
4504 __ Sra(AT, lhs_high, 31);
4505 __ Bltu(AT, TMP, label);
4506 break;
4507 case kCondB: // always false
4508 break;
4509 case kCondAE: // always true
4510 __ B(label);
4511 break;
4512 }
4513 } else if (use_imm) {
4514 // TODO: more efficient comparison with constants without loading them into TMP/AT.
4515 switch (cond) {
4516 case kCondEQ:
4517 __ LoadConst32(TMP, imm_high);
4518 __ Xor(TMP, TMP, lhs_high);
4519 __ LoadConst32(AT, imm_low);
4520 __ Xor(AT, AT, lhs_low);
4521 __ Or(TMP, TMP, AT);
4522 __ Beqz(TMP, label);
4523 break;
4524 case kCondNE:
4525 __ LoadConst32(TMP, imm_high);
4526 __ Xor(TMP, TMP, lhs_high);
4527 __ LoadConst32(AT, imm_low);
4528 __ Xor(AT, AT, lhs_low);
4529 __ Or(TMP, TMP, AT);
4530 __ Bnez(TMP, label);
4531 break;
4532 case kCondLT:
4533 __ LoadConst32(TMP, imm_high);
4534 __ Blt(lhs_high, TMP, label);
4535 __ Slt(TMP, TMP, lhs_high);
4536 __ LoadConst32(AT, imm_low);
4537 __ Sltu(AT, lhs_low, AT);
4538 __ Blt(TMP, AT, label);
4539 break;
4540 case kCondGE:
4541 __ LoadConst32(TMP, imm_high);
4542 __ Blt(TMP, lhs_high, label);
4543 __ Slt(TMP, lhs_high, TMP);
4544 __ LoadConst32(AT, imm_low);
4545 __ Sltu(AT, lhs_low, AT);
4546 __ Or(TMP, TMP, AT);
4547 __ Beqz(TMP, label);
4548 break;
4549 case kCondLE:
4550 __ LoadConst32(TMP, imm_high);
4551 __ Blt(lhs_high, TMP, label);
4552 __ Slt(TMP, TMP, lhs_high);
4553 __ LoadConst32(AT, imm_low);
4554 __ Sltu(AT, AT, lhs_low);
4555 __ Or(TMP, TMP, AT);
4556 __ Beqz(TMP, label);
4557 break;
4558 case kCondGT:
4559 __ LoadConst32(TMP, imm_high);
4560 __ Blt(TMP, lhs_high, label);
4561 __ Slt(TMP, lhs_high, TMP);
4562 __ LoadConst32(AT, imm_low);
4563 __ Sltu(AT, AT, lhs_low);
4564 __ Blt(TMP, AT, label);
4565 break;
4566 case kCondB:
4567 __ LoadConst32(TMP, imm_high);
4568 __ Bltu(lhs_high, TMP, label);
4569 __ Sltu(TMP, TMP, lhs_high);
4570 __ LoadConst32(AT, imm_low);
4571 __ Sltu(AT, lhs_low, AT);
4572 __ Blt(TMP, AT, label);
4573 break;
4574 case kCondAE:
4575 __ LoadConst32(TMP, imm_high);
4576 __ Bltu(TMP, lhs_high, label);
4577 __ Sltu(TMP, lhs_high, TMP);
4578 __ LoadConst32(AT, imm_low);
4579 __ Sltu(AT, lhs_low, AT);
4580 __ Or(TMP, TMP, AT);
4581 __ Beqz(TMP, label);
4582 break;
4583 case kCondBE:
4584 __ LoadConst32(TMP, imm_high);
4585 __ Bltu(lhs_high, TMP, label);
4586 __ Sltu(TMP, TMP, lhs_high);
4587 __ LoadConst32(AT, imm_low);
4588 __ Sltu(AT, AT, lhs_low);
4589 __ Or(TMP, TMP, AT);
4590 __ Beqz(TMP, label);
4591 break;
4592 case kCondA:
4593 __ LoadConst32(TMP, imm_high);
4594 __ Bltu(TMP, lhs_high, label);
4595 __ Sltu(TMP, lhs_high, TMP);
4596 __ LoadConst32(AT, imm_low);
4597 __ Sltu(AT, AT, lhs_low);
4598 __ Blt(TMP, AT, label);
4599 break;
4600 }
4601 } else {
4602 switch (cond) {
4603 case kCondEQ:
4604 __ Xor(TMP, lhs_high, rhs_high);
4605 __ Xor(AT, lhs_low, rhs_low);
4606 __ Or(TMP, TMP, AT);
4607 __ Beqz(TMP, label);
4608 break;
4609 case kCondNE:
4610 __ Xor(TMP, lhs_high, rhs_high);
4611 __ Xor(AT, lhs_low, rhs_low);
4612 __ Or(TMP, TMP, AT);
4613 __ Bnez(TMP, label);
4614 break;
4615 case kCondLT:
4616 __ Blt(lhs_high, rhs_high, label);
4617 __ Slt(TMP, rhs_high, lhs_high);
4618 __ Sltu(AT, lhs_low, rhs_low);
4619 __ Blt(TMP, AT, label);
4620 break;
4621 case kCondGE:
4622 __ Blt(rhs_high, lhs_high, label);
4623 __ Slt(TMP, lhs_high, rhs_high);
4624 __ Sltu(AT, lhs_low, rhs_low);
4625 __ Or(TMP, TMP, AT);
4626 __ Beqz(TMP, label);
4627 break;
4628 case kCondLE:
4629 __ Blt(lhs_high, rhs_high, label);
4630 __ Slt(TMP, rhs_high, lhs_high);
4631 __ Sltu(AT, rhs_low, lhs_low);
4632 __ Or(TMP, TMP, AT);
4633 __ Beqz(TMP, label);
4634 break;
4635 case kCondGT:
4636 __ Blt(rhs_high, lhs_high, label);
4637 __ Slt(TMP, lhs_high, rhs_high);
4638 __ Sltu(AT, rhs_low, lhs_low);
4639 __ Blt(TMP, AT, label);
4640 break;
4641 case kCondB:
4642 __ Bltu(lhs_high, rhs_high, label);
4643 __ Sltu(TMP, rhs_high, lhs_high);
4644 __ Sltu(AT, lhs_low, rhs_low);
4645 __ Blt(TMP, AT, label);
4646 break;
4647 case kCondAE:
4648 __ Bltu(rhs_high, lhs_high, label);
4649 __ Sltu(TMP, lhs_high, rhs_high);
4650 __ Sltu(AT, lhs_low, rhs_low);
4651 __ Or(TMP, TMP, AT);
4652 __ Beqz(TMP, label);
4653 break;
4654 case kCondBE:
4655 __ Bltu(lhs_high, rhs_high, label);
4656 __ Sltu(TMP, rhs_high, lhs_high);
4657 __ Sltu(AT, rhs_low, lhs_low);
4658 __ Or(TMP, TMP, AT);
4659 __ Beqz(TMP, label);
4660 break;
4661 case kCondA:
4662 __ Bltu(rhs_high, lhs_high, label);
4663 __ Sltu(TMP, lhs_high, rhs_high);
4664 __ Sltu(AT, rhs_low, lhs_low);
4665 __ Blt(TMP, AT, label);
4666 break;
4667 }
4668 }
4669}
4670
Alexey Frunze2ddb7172016-09-06 17:04:55 -07004671void InstructionCodeGeneratorMIPS::GenerateFpCompare(IfCondition cond,
4672 bool gt_bias,
4673 Primitive::Type type,
4674 LocationSummary* locations) {
4675 Register dst = locations->Out().AsRegister<Register>();
4676 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
4677 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
4678 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
4679 if (type == Primitive::kPrimFloat) {
4680 if (isR6) {
4681 switch (cond) {
4682 case kCondEQ:
4683 __ CmpEqS(FTMP, lhs, rhs);
4684 __ Mfc1(dst, FTMP);
4685 __ Andi(dst, dst, 1);
4686 break;
4687 case kCondNE:
4688 __ CmpEqS(FTMP, lhs, rhs);
4689 __ Mfc1(dst, FTMP);
4690 __ Addiu(dst, dst, 1);
4691 break;
4692 case kCondLT:
4693 if (gt_bias) {
4694 __ CmpLtS(FTMP, lhs, rhs);
4695 } else {
4696 __ CmpUltS(FTMP, lhs, rhs);
4697 }
4698 __ Mfc1(dst, FTMP);
4699 __ Andi(dst, dst, 1);
4700 break;
4701 case kCondLE:
4702 if (gt_bias) {
4703 __ CmpLeS(FTMP, lhs, rhs);
4704 } else {
4705 __ CmpUleS(FTMP, lhs, rhs);
4706 }
4707 __ Mfc1(dst, FTMP);
4708 __ Andi(dst, dst, 1);
4709 break;
4710 case kCondGT:
4711 if (gt_bias) {
4712 __ CmpUltS(FTMP, rhs, lhs);
4713 } else {
4714 __ CmpLtS(FTMP, rhs, lhs);
4715 }
4716 __ Mfc1(dst, FTMP);
4717 __ Andi(dst, dst, 1);
4718 break;
4719 case kCondGE:
4720 if (gt_bias) {
4721 __ CmpUleS(FTMP, rhs, lhs);
4722 } else {
4723 __ CmpLeS(FTMP, rhs, lhs);
4724 }
4725 __ Mfc1(dst, FTMP);
4726 __ Andi(dst, dst, 1);
4727 break;
4728 default:
4729 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4730 UNREACHABLE();
4731 }
4732 } else {
4733 switch (cond) {
4734 case kCondEQ:
4735 __ CeqS(0, lhs, rhs);
4736 __ LoadConst32(dst, 1);
4737 __ Movf(dst, ZERO, 0);
4738 break;
4739 case kCondNE:
4740 __ CeqS(0, lhs, rhs);
4741 __ LoadConst32(dst, 1);
4742 __ Movt(dst, ZERO, 0);
4743 break;
4744 case kCondLT:
4745 if (gt_bias) {
4746 __ ColtS(0, lhs, rhs);
4747 } else {
4748 __ CultS(0, lhs, rhs);
4749 }
4750 __ LoadConst32(dst, 1);
4751 __ Movf(dst, ZERO, 0);
4752 break;
4753 case kCondLE:
4754 if (gt_bias) {
4755 __ ColeS(0, lhs, rhs);
4756 } else {
4757 __ CuleS(0, lhs, rhs);
4758 }
4759 __ LoadConst32(dst, 1);
4760 __ Movf(dst, ZERO, 0);
4761 break;
4762 case kCondGT:
4763 if (gt_bias) {
4764 __ CultS(0, rhs, lhs);
4765 } else {
4766 __ ColtS(0, rhs, lhs);
4767 }
4768 __ LoadConst32(dst, 1);
4769 __ Movf(dst, ZERO, 0);
4770 break;
4771 case kCondGE:
4772 if (gt_bias) {
4773 __ CuleS(0, rhs, lhs);
4774 } else {
4775 __ ColeS(0, rhs, lhs);
4776 }
4777 __ LoadConst32(dst, 1);
4778 __ Movf(dst, ZERO, 0);
4779 break;
4780 default:
4781 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4782 UNREACHABLE();
4783 }
4784 }
4785 } else {
4786 DCHECK_EQ(type, Primitive::kPrimDouble);
4787 if (isR6) {
4788 switch (cond) {
4789 case kCondEQ:
4790 __ CmpEqD(FTMP, lhs, rhs);
4791 __ Mfc1(dst, FTMP);
4792 __ Andi(dst, dst, 1);
4793 break;
4794 case kCondNE:
4795 __ CmpEqD(FTMP, lhs, rhs);
4796 __ Mfc1(dst, FTMP);
4797 __ Addiu(dst, dst, 1);
4798 break;
4799 case kCondLT:
4800 if (gt_bias) {
4801 __ CmpLtD(FTMP, lhs, rhs);
4802 } else {
4803 __ CmpUltD(FTMP, lhs, rhs);
4804 }
4805 __ Mfc1(dst, FTMP);
4806 __ Andi(dst, dst, 1);
4807 break;
4808 case kCondLE:
4809 if (gt_bias) {
4810 __ CmpLeD(FTMP, lhs, rhs);
4811 } else {
4812 __ CmpUleD(FTMP, lhs, rhs);
4813 }
4814 __ Mfc1(dst, FTMP);
4815 __ Andi(dst, dst, 1);
4816 break;
4817 case kCondGT:
4818 if (gt_bias) {
4819 __ CmpUltD(FTMP, rhs, lhs);
4820 } else {
4821 __ CmpLtD(FTMP, rhs, lhs);
4822 }
4823 __ Mfc1(dst, FTMP);
4824 __ Andi(dst, dst, 1);
4825 break;
4826 case kCondGE:
4827 if (gt_bias) {
4828 __ CmpUleD(FTMP, rhs, lhs);
4829 } else {
4830 __ CmpLeD(FTMP, rhs, lhs);
4831 }
4832 __ Mfc1(dst, FTMP);
4833 __ Andi(dst, dst, 1);
4834 break;
4835 default:
4836 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4837 UNREACHABLE();
4838 }
4839 } else {
4840 switch (cond) {
4841 case kCondEQ:
4842 __ CeqD(0, lhs, rhs);
4843 __ LoadConst32(dst, 1);
4844 __ Movf(dst, ZERO, 0);
4845 break;
4846 case kCondNE:
4847 __ CeqD(0, lhs, rhs);
4848 __ LoadConst32(dst, 1);
4849 __ Movt(dst, ZERO, 0);
4850 break;
4851 case kCondLT:
4852 if (gt_bias) {
4853 __ ColtD(0, lhs, rhs);
4854 } else {
4855 __ CultD(0, lhs, rhs);
4856 }
4857 __ LoadConst32(dst, 1);
4858 __ Movf(dst, ZERO, 0);
4859 break;
4860 case kCondLE:
4861 if (gt_bias) {
4862 __ ColeD(0, lhs, rhs);
4863 } else {
4864 __ CuleD(0, lhs, rhs);
4865 }
4866 __ LoadConst32(dst, 1);
4867 __ Movf(dst, ZERO, 0);
4868 break;
4869 case kCondGT:
4870 if (gt_bias) {
4871 __ CultD(0, rhs, lhs);
4872 } else {
4873 __ ColtD(0, rhs, lhs);
4874 }
4875 __ LoadConst32(dst, 1);
4876 __ Movf(dst, ZERO, 0);
4877 break;
4878 case kCondGE:
4879 if (gt_bias) {
4880 __ CuleD(0, rhs, lhs);
4881 } else {
4882 __ ColeD(0, rhs, lhs);
4883 }
4884 __ LoadConst32(dst, 1);
4885 __ Movf(dst, ZERO, 0);
4886 break;
4887 default:
4888 LOG(FATAL) << "Unexpected non-floating-point condition " << cond;
4889 UNREACHABLE();
4890 }
4891 }
4892 }
4893}
4894
Alexey Frunze674b9ee2016-09-20 14:54:15 -07004895bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR2(IfCondition cond,
4896 bool gt_bias,
4897 Primitive::Type type,
4898 LocationSummary* input_locations,
4899 int cc) {
4900 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4901 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4902 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
4903 if (type == Primitive::kPrimFloat) {
4904 switch (cond) {
4905 case kCondEQ:
4906 __ CeqS(cc, lhs, rhs);
4907 return false;
4908 case kCondNE:
4909 __ CeqS(cc, lhs, rhs);
4910 return true;
4911 case kCondLT:
4912 if (gt_bias) {
4913 __ ColtS(cc, lhs, rhs);
4914 } else {
4915 __ CultS(cc, lhs, rhs);
4916 }
4917 return false;
4918 case kCondLE:
4919 if (gt_bias) {
4920 __ ColeS(cc, lhs, rhs);
4921 } else {
4922 __ CuleS(cc, lhs, rhs);
4923 }
4924 return false;
4925 case kCondGT:
4926 if (gt_bias) {
4927 __ CultS(cc, rhs, lhs);
4928 } else {
4929 __ ColtS(cc, rhs, lhs);
4930 }
4931 return false;
4932 case kCondGE:
4933 if (gt_bias) {
4934 __ CuleS(cc, rhs, lhs);
4935 } else {
4936 __ ColeS(cc, rhs, lhs);
4937 }
4938 return false;
4939 default:
4940 LOG(FATAL) << "Unexpected non-floating-point condition";
4941 UNREACHABLE();
4942 }
4943 } else {
4944 DCHECK_EQ(type, Primitive::kPrimDouble);
4945 switch (cond) {
4946 case kCondEQ:
4947 __ CeqD(cc, lhs, rhs);
4948 return false;
4949 case kCondNE:
4950 __ CeqD(cc, lhs, rhs);
4951 return true;
4952 case kCondLT:
4953 if (gt_bias) {
4954 __ ColtD(cc, lhs, rhs);
4955 } else {
4956 __ CultD(cc, lhs, rhs);
4957 }
4958 return false;
4959 case kCondLE:
4960 if (gt_bias) {
4961 __ ColeD(cc, lhs, rhs);
4962 } else {
4963 __ CuleD(cc, lhs, rhs);
4964 }
4965 return false;
4966 case kCondGT:
4967 if (gt_bias) {
4968 __ CultD(cc, rhs, lhs);
4969 } else {
4970 __ ColtD(cc, rhs, lhs);
4971 }
4972 return false;
4973 case kCondGE:
4974 if (gt_bias) {
4975 __ CuleD(cc, rhs, lhs);
4976 } else {
4977 __ ColeD(cc, rhs, lhs);
4978 }
4979 return false;
4980 default:
4981 LOG(FATAL) << "Unexpected non-floating-point condition";
4982 UNREACHABLE();
4983 }
4984 }
4985}
4986
4987bool InstructionCodeGeneratorMIPS::MaterializeFpCompareR6(IfCondition cond,
4988 bool gt_bias,
4989 Primitive::Type type,
4990 LocationSummary* input_locations,
4991 FRegister dst) {
4992 FRegister lhs = input_locations->InAt(0).AsFpuRegister<FRegister>();
4993 FRegister rhs = input_locations->InAt(1).AsFpuRegister<FRegister>();
4994 CHECK(codegen_->GetInstructionSetFeatures().IsR6());
4995 if (type == Primitive::kPrimFloat) {
4996 switch (cond) {
4997 case kCondEQ:
4998 __ CmpEqS(dst, lhs, rhs);
4999 return false;
5000 case kCondNE:
5001 __ CmpEqS(dst, lhs, rhs);
5002 return true;
5003 case kCondLT:
5004 if (gt_bias) {
5005 __ CmpLtS(dst, lhs, rhs);
5006 } else {
5007 __ CmpUltS(dst, lhs, rhs);
5008 }
5009 return false;
5010 case kCondLE:
5011 if (gt_bias) {
5012 __ CmpLeS(dst, lhs, rhs);
5013 } else {
5014 __ CmpUleS(dst, lhs, rhs);
5015 }
5016 return false;
5017 case kCondGT:
5018 if (gt_bias) {
5019 __ CmpUltS(dst, rhs, lhs);
5020 } else {
5021 __ CmpLtS(dst, rhs, lhs);
5022 }
5023 return false;
5024 case kCondGE:
5025 if (gt_bias) {
5026 __ CmpUleS(dst, rhs, lhs);
5027 } else {
5028 __ CmpLeS(dst, rhs, lhs);
5029 }
5030 return false;
5031 default:
5032 LOG(FATAL) << "Unexpected non-floating-point condition";
5033 UNREACHABLE();
5034 }
5035 } else {
5036 DCHECK_EQ(type, Primitive::kPrimDouble);
5037 switch (cond) {
5038 case kCondEQ:
5039 __ CmpEqD(dst, lhs, rhs);
5040 return false;
5041 case kCondNE:
5042 __ CmpEqD(dst, lhs, rhs);
5043 return true;
5044 case kCondLT:
5045 if (gt_bias) {
5046 __ CmpLtD(dst, lhs, rhs);
5047 } else {
5048 __ CmpUltD(dst, lhs, rhs);
5049 }
5050 return false;
5051 case kCondLE:
5052 if (gt_bias) {
5053 __ CmpLeD(dst, lhs, rhs);
5054 } else {
5055 __ CmpUleD(dst, lhs, rhs);
5056 }
5057 return false;
5058 case kCondGT:
5059 if (gt_bias) {
5060 __ CmpUltD(dst, rhs, lhs);
5061 } else {
5062 __ CmpLtD(dst, rhs, lhs);
5063 }
5064 return false;
5065 case kCondGE:
5066 if (gt_bias) {
5067 __ CmpUleD(dst, rhs, lhs);
5068 } else {
5069 __ CmpLeD(dst, rhs, lhs);
5070 }
5071 return false;
5072 default:
5073 LOG(FATAL) << "Unexpected non-floating-point condition";
5074 UNREACHABLE();
5075 }
5076 }
5077}
5078
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005079void InstructionCodeGeneratorMIPS::GenerateFpCompareAndBranch(IfCondition cond,
5080 bool gt_bias,
5081 Primitive::Type type,
5082 LocationSummary* locations,
5083 MipsLabel* label) {
5084 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
5085 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
5086 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
5087 if (type == Primitive::kPrimFloat) {
5088 if (isR6) {
5089 switch (cond) {
5090 case kCondEQ:
5091 __ CmpEqS(FTMP, lhs, rhs);
5092 __ Bc1nez(FTMP, label);
5093 break;
5094 case kCondNE:
5095 __ CmpEqS(FTMP, lhs, rhs);
5096 __ Bc1eqz(FTMP, label);
5097 break;
5098 case kCondLT:
5099 if (gt_bias) {
5100 __ CmpLtS(FTMP, lhs, rhs);
5101 } else {
5102 __ CmpUltS(FTMP, lhs, rhs);
5103 }
5104 __ Bc1nez(FTMP, label);
5105 break;
5106 case kCondLE:
5107 if (gt_bias) {
5108 __ CmpLeS(FTMP, lhs, rhs);
5109 } else {
5110 __ CmpUleS(FTMP, lhs, rhs);
5111 }
5112 __ Bc1nez(FTMP, label);
5113 break;
5114 case kCondGT:
5115 if (gt_bias) {
5116 __ CmpUltS(FTMP, rhs, lhs);
5117 } else {
5118 __ CmpLtS(FTMP, rhs, lhs);
5119 }
5120 __ Bc1nez(FTMP, label);
5121 break;
5122 case kCondGE:
5123 if (gt_bias) {
5124 __ CmpUleS(FTMP, rhs, lhs);
5125 } else {
5126 __ CmpLeS(FTMP, rhs, lhs);
5127 }
5128 __ Bc1nez(FTMP, label);
5129 break;
5130 default:
5131 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005132 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005133 }
5134 } else {
5135 switch (cond) {
5136 case kCondEQ:
5137 __ CeqS(0, lhs, rhs);
5138 __ Bc1t(0, label);
5139 break;
5140 case kCondNE:
5141 __ CeqS(0, lhs, rhs);
5142 __ Bc1f(0, label);
5143 break;
5144 case kCondLT:
5145 if (gt_bias) {
5146 __ ColtS(0, lhs, rhs);
5147 } else {
5148 __ CultS(0, lhs, rhs);
5149 }
5150 __ Bc1t(0, label);
5151 break;
5152 case kCondLE:
5153 if (gt_bias) {
5154 __ ColeS(0, lhs, rhs);
5155 } else {
5156 __ CuleS(0, lhs, rhs);
5157 }
5158 __ Bc1t(0, label);
5159 break;
5160 case kCondGT:
5161 if (gt_bias) {
5162 __ CultS(0, rhs, lhs);
5163 } else {
5164 __ ColtS(0, rhs, lhs);
5165 }
5166 __ Bc1t(0, label);
5167 break;
5168 case kCondGE:
5169 if (gt_bias) {
5170 __ CuleS(0, rhs, lhs);
5171 } else {
5172 __ ColeS(0, rhs, lhs);
5173 }
5174 __ Bc1t(0, label);
5175 break;
5176 default:
5177 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005178 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005179 }
5180 }
5181 } else {
5182 DCHECK_EQ(type, Primitive::kPrimDouble);
5183 if (isR6) {
5184 switch (cond) {
5185 case kCondEQ:
5186 __ CmpEqD(FTMP, lhs, rhs);
5187 __ Bc1nez(FTMP, label);
5188 break;
5189 case kCondNE:
5190 __ CmpEqD(FTMP, lhs, rhs);
5191 __ Bc1eqz(FTMP, label);
5192 break;
5193 case kCondLT:
5194 if (gt_bias) {
5195 __ CmpLtD(FTMP, lhs, rhs);
5196 } else {
5197 __ CmpUltD(FTMP, lhs, rhs);
5198 }
5199 __ Bc1nez(FTMP, label);
5200 break;
5201 case kCondLE:
5202 if (gt_bias) {
5203 __ CmpLeD(FTMP, lhs, rhs);
5204 } else {
5205 __ CmpUleD(FTMP, lhs, rhs);
5206 }
5207 __ Bc1nez(FTMP, label);
5208 break;
5209 case kCondGT:
5210 if (gt_bias) {
5211 __ CmpUltD(FTMP, rhs, lhs);
5212 } else {
5213 __ CmpLtD(FTMP, rhs, lhs);
5214 }
5215 __ Bc1nez(FTMP, label);
5216 break;
5217 case kCondGE:
5218 if (gt_bias) {
5219 __ CmpUleD(FTMP, rhs, lhs);
5220 } else {
5221 __ CmpLeD(FTMP, rhs, lhs);
5222 }
5223 __ Bc1nez(FTMP, label);
5224 break;
5225 default:
5226 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005227 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005228 }
5229 } else {
5230 switch (cond) {
5231 case kCondEQ:
5232 __ CeqD(0, lhs, rhs);
5233 __ Bc1t(0, label);
5234 break;
5235 case kCondNE:
5236 __ CeqD(0, lhs, rhs);
5237 __ Bc1f(0, label);
5238 break;
5239 case kCondLT:
5240 if (gt_bias) {
5241 __ ColtD(0, lhs, rhs);
5242 } else {
5243 __ CultD(0, lhs, rhs);
5244 }
5245 __ Bc1t(0, label);
5246 break;
5247 case kCondLE:
5248 if (gt_bias) {
5249 __ ColeD(0, lhs, rhs);
5250 } else {
5251 __ CuleD(0, lhs, rhs);
5252 }
5253 __ Bc1t(0, label);
5254 break;
5255 case kCondGT:
5256 if (gt_bias) {
5257 __ CultD(0, rhs, lhs);
5258 } else {
5259 __ ColtD(0, rhs, lhs);
5260 }
5261 __ Bc1t(0, label);
5262 break;
5263 case kCondGE:
5264 if (gt_bias) {
5265 __ CuleD(0, rhs, lhs);
5266 } else {
5267 __ ColeD(0, rhs, lhs);
5268 }
5269 __ Bc1t(0, label);
5270 break;
5271 default:
5272 LOG(FATAL) << "Unexpected non-floating-point condition";
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005273 UNREACHABLE();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005274 }
5275 }
5276 }
5277}
5278
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005279void InstructionCodeGeneratorMIPS::GenerateTestAndBranch(HInstruction* instruction,
David Brazdil0debae72015-11-12 18:37:00 +00005280 size_t condition_input_index,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005281 MipsLabel* true_target,
David Brazdil0debae72015-11-12 18:37:00 +00005282 MipsLabel* false_target) {
5283 HInstruction* cond = instruction->InputAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005284
David Brazdil0debae72015-11-12 18:37:00 +00005285 if (true_target == nullptr && false_target == nullptr) {
5286 // Nothing to do. The code always falls through.
5287 return;
5288 } else if (cond->IsIntConstant()) {
Roland Levillain1a653882016-03-18 18:05:57 +00005289 // Constant condition, statically compared against "true" (integer value 1).
5290 if (cond->AsIntConstant()->IsTrue()) {
David Brazdil0debae72015-11-12 18:37:00 +00005291 if (true_target != nullptr) {
5292 __ B(true_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005293 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005294 } else {
Roland Levillain1a653882016-03-18 18:05:57 +00005295 DCHECK(cond->AsIntConstant()->IsFalse()) << cond->AsIntConstant()->GetValue();
David Brazdil0debae72015-11-12 18:37:00 +00005296 if (false_target != nullptr) {
5297 __ B(false_target);
5298 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005299 }
David Brazdil0debae72015-11-12 18:37:00 +00005300 return;
5301 }
5302
5303 // The following code generates these patterns:
5304 // (1) true_target == nullptr && false_target != nullptr
5305 // - opposite condition true => branch to false_target
5306 // (2) true_target != nullptr && false_target == nullptr
5307 // - condition true => branch to true_target
5308 // (3) true_target != nullptr && false_target != nullptr
5309 // - condition true => branch to true_target
5310 // - branch to false_target
5311 if (IsBooleanValueOrMaterializedCondition(cond)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005312 // The condition instruction has been materialized, compare the output to 0.
David Brazdil0debae72015-11-12 18:37:00 +00005313 Location cond_val = instruction->GetLocations()->InAt(condition_input_index);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005314 DCHECK(cond_val.IsRegister());
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005315 if (true_target == nullptr) {
David Brazdil0debae72015-11-12 18:37:00 +00005316 __ Beqz(cond_val.AsRegister<Register>(), false_target);
5317 } else {
5318 __ Bnez(cond_val.AsRegister<Register>(), true_target);
5319 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005320 } else {
5321 // The condition instruction has not been materialized, use its inputs as
5322 // the comparison and its condition as the branch condition.
David Brazdil0debae72015-11-12 18:37:00 +00005323 HCondition* condition = cond->AsCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005324 Primitive::Type type = condition->InputAt(0)->GetType();
5325 LocationSummary* locations = cond->GetLocations();
5326 IfCondition if_cond = condition->GetCondition();
5327 MipsLabel* branch_target = true_target;
David Brazdil0debae72015-11-12 18:37:00 +00005328
David Brazdil0debae72015-11-12 18:37:00 +00005329 if (true_target == nullptr) {
5330 if_cond = condition->GetOppositeCondition();
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005331 branch_target = false_target;
David Brazdil0debae72015-11-12 18:37:00 +00005332 }
5333
Alexey Frunzecd7b0ee2015-12-03 16:46:38 -08005334 switch (type) {
5335 default:
5336 GenerateIntCompareAndBranch(if_cond, locations, branch_target);
5337 break;
5338 case Primitive::kPrimLong:
5339 GenerateLongCompareAndBranch(if_cond, locations, branch_target);
5340 break;
5341 case Primitive::kPrimFloat:
5342 case Primitive::kPrimDouble:
5343 GenerateFpCompareAndBranch(if_cond, condition->IsGtBias(), type, locations, branch_target);
5344 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005345 }
5346 }
David Brazdil0debae72015-11-12 18:37:00 +00005347
5348 // If neither branch falls through (case 3), the conditional branch to `true_target`
5349 // was already emitted (case 2) and we need to emit a jump to `false_target`.
5350 if (true_target != nullptr && false_target != nullptr) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005351 __ B(false_target);
5352 }
5353}
5354
5355void LocationsBuilderMIPS::VisitIf(HIf* if_instr) {
5356 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(if_instr);
David Brazdil0debae72015-11-12 18:37:00 +00005357 if (IsBooleanValueOrMaterializedCondition(if_instr->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005358 locations->SetInAt(0, Location::RequiresRegister());
5359 }
5360}
5361
5362void InstructionCodeGeneratorMIPS::VisitIf(HIf* if_instr) {
David Brazdil0debae72015-11-12 18:37:00 +00005363 HBasicBlock* true_successor = if_instr->IfTrueSuccessor();
5364 HBasicBlock* false_successor = if_instr->IfFalseSuccessor();
5365 MipsLabel* true_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), true_successor) ?
5366 nullptr : codegen_->GetLabelOf(true_successor);
5367 MipsLabel* false_target = codegen_->GoesToNextBlock(if_instr->GetBlock(), false_successor) ?
5368 nullptr : codegen_->GetLabelOf(false_successor);
5369 GenerateTestAndBranch(if_instr, /* condition_input_index */ 0, true_target, false_target);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005370}
5371
5372void LocationsBuilderMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
5373 LocationSummary* locations = new (GetGraph()->GetArena())
5374 LocationSummary(deoptimize, LocationSummary::kCallOnSlowPath);
Nicolas Geoffray4e92c3c2017-05-08 09:34:26 +01005375 InvokeRuntimeCallingConvention calling_convention;
5376 RegisterSet caller_saves = RegisterSet::Empty();
5377 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5378 locations->SetCustomSlowPathCallerSaves(caller_saves);
David Brazdil0debae72015-11-12 18:37:00 +00005379 if (IsBooleanValueOrMaterializedCondition(deoptimize->InputAt(0))) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005380 locations->SetInAt(0, Location::RequiresRegister());
5381 }
5382}
5383
5384void InstructionCodeGeneratorMIPS::VisitDeoptimize(HDeoptimize* deoptimize) {
Aart Bik42249c32016-01-07 15:33:50 -08005385 SlowPathCodeMIPS* slow_path =
5386 deopt_slow_paths_.NewSlowPath<DeoptimizationSlowPathMIPS>(deoptimize);
David Brazdil0debae72015-11-12 18:37:00 +00005387 GenerateTestAndBranch(deoptimize,
5388 /* condition_input_index */ 0,
5389 slow_path->GetEntryLabel(),
5390 /* false_target */ nullptr);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005391}
5392
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005393// This function returns true if a conditional move can be generated for HSelect.
5394// Otherwise it returns false and HSelect must be implemented in terms of conditonal
5395// branches and regular moves.
5396//
5397// If `locations_to_set` isn't nullptr, its inputs and outputs are set for HSelect.
5398//
5399// While determining feasibility of a conditional move and setting inputs/outputs
5400// are two distinct tasks, this function does both because they share quite a bit
5401// of common logic.
5402static bool CanMoveConditionally(HSelect* select, bool is_r6, LocationSummary* locations_to_set) {
5403 bool materialized = IsBooleanValueOrMaterializedCondition(select->GetCondition());
5404 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5405 HCondition* condition = cond->AsCondition();
5406
5407 Primitive::Type cond_type = materialized ? Primitive::kPrimInt : condition->InputAt(0)->GetType();
5408 Primitive::Type dst_type = select->GetType();
5409
5410 HConstant* cst_true_value = select->GetTrueValue()->AsConstant();
5411 HConstant* cst_false_value = select->GetFalseValue()->AsConstant();
5412 bool is_true_value_zero_constant =
5413 (cst_true_value != nullptr && cst_true_value->IsZeroBitPattern());
5414 bool is_false_value_zero_constant =
5415 (cst_false_value != nullptr && cst_false_value->IsZeroBitPattern());
5416
5417 bool can_move_conditionally = false;
5418 bool use_const_for_false_in = false;
5419 bool use_const_for_true_in = false;
5420
5421 if (!cond->IsConstant()) {
5422 switch (cond_type) {
5423 default:
5424 switch (dst_type) {
5425 default:
5426 // Moving int on int condition.
5427 if (is_r6) {
5428 if (is_true_value_zero_constant) {
5429 // seleqz out_reg, false_reg, cond_reg
5430 can_move_conditionally = true;
5431 use_const_for_true_in = true;
5432 } else if (is_false_value_zero_constant) {
5433 // selnez out_reg, true_reg, cond_reg
5434 can_move_conditionally = true;
5435 use_const_for_false_in = true;
5436 } else if (materialized) {
5437 // Not materializing unmaterialized int conditions
5438 // to keep the instruction count low.
5439 // selnez AT, true_reg, cond_reg
5440 // seleqz TMP, false_reg, cond_reg
5441 // or out_reg, AT, TMP
5442 can_move_conditionally = true;
5443 }
5444 } else {
5445 // movn out_reg, true_reg/ZERO, cond_reg
5446 can_move_conditionally = true;
5447 use_const_for_true_in = is_true_value_zero_constant;
5448 }
5449 break;
5450 case Primitive::kPrimLong:
5451 // Moving long on int condition.
5452 if (is_r6) {
5453 if (is_true_value_zero_constant) {
5454 // seleqz out_reg_lo, false_reg_lo, cond_reg
5455 // seleqz out_reg_hi, false_reg_hi, cond_reg
5456 can_move_conditionally = true;
5457 use_const_for_true_in = true;
5458 } else if (is_false_value_zero_constant) {
5459 // selnez out_reg_lo, true_reg_lo, cond_reg
5460 // selnez out_reg_hi, true_reg_hi, cond_reg
5461 can_move_conditionally = true;
5462 use_const_for_false_in = true;
5463 }
5464 // Other long conditional moves would generate 6+ instructions,
5465 // which is too many.
5466 } else {
5467 // movn out_reg_lo, true_reg_lo/ZERO, cond_reg
5468 // movn out_reg_hi, true_reg_hi/ZERO, cond_reg
5469 can_move_conditionally = true;
5470 use_const_for_true_in = is_true_value_zero_constant;
5471 }
5472 break;
5473 case Primitive::kPrimFloat:
5474 case Primitive::kPrimDouble:
5475 // Moving float/double on int condition.
5476 if (is_r6) {
5477 if (materialized) {
5478 // Not materializing unmaterialized int conditions
5479 // to keep the instruction count low.
5480 can_move_conditionally = true;
5481 if (is_true_value_zero_constant) {
5482 // sltu TMP, ZERO, cond_reg
5483 // mtc1 TMP, temp_cond_reg
5484 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5485 use_const_for_true_in = true;
5486 } else if (is_false_value_zero_constant) {
5487 // sltu TMP, ZERO, cond_reg
5488 // mtc1 TMP, temp_cond_reg
5489 // selnez.fmt out_reg, true_reg, temp_cond_reg
5490 use_const_for_false_in = true;
5491 } else {
5492 // sltu TMP, ZERO, cond_reg
5493 // mtc1 TMP, temp_cond_reg
5494 // sel.fmt temp_cond_reg, false_reg, true_reg
5495 // mov.fmt out_reg, temp_cond_reg
5496 }
5497 }
5498 } else {
5499 // movn.fmt out_reg, true_reg, cond_reg
5500 can_move_conditionally = true;
5501 }
5502 break;
5503 }
5504 break;
5505 case Primitive::kPrimLong:
5506 // We don't materialize long comparison now
5507 // and use conditional branches instead.
5508 break;
5509 case Primitive::kPrimFloat:
5510 case Primitive::kPrimDouble:
5511 switch (dst_type) {
5512 default:
5513 // Moving int on float/double condition.
5514 if (is_r6) {
5515 if (is_true_value_zero_constant) {
5516 // mfc1 TMP, temp_cond_reg
5517 // seleqz out_reg, false_reg, TMP
5518 can_move_conditionally = true;
5519 use_const_for_true_in = true;
5520 } else if (is_false_value_zero_constant) {
5521 // mfc1 TMP, temp_cond_reg
5522 // selnez out_reg, true_reg, TMP
5523 can_move_conditionally = true;
5524 use_const_for_false_in = true;
5525 } else {
5526 // mfc1 TMP, temp_cond_reg
5527 // selnez AT, true_reg, TMP
5528 // seleqz TMP, false_reg, TMP
5529 // or out_reg, AT, TMP
5530 can_move_conditionally = true;
5531 }
5532 } else {
5533 // movt out_reg, true_reg/ZERO, cc
5534 can_move_conditionally = true;
5535 use_const_for_true_in = is_true_value_zero_constant;
5536 }
5537 break;
5538 case Primitive::kPrimLong:
5539 // Moving long on float/double condition.
5540 if (is_r6) {
5541 if (is_true_value_zero_constant) {
5542 // mfc1 TMP, temp_cond_reg
5543 // seleqz out_reg_lo, false_reg_lo, TMP
5544 // seleqz out_reg_hi, false_reg_hi, TMP
5545 can_move_conditionally = true;
5546 use_const_for_true_in = true;
5547 } else if (is_false_value_zero_constant) {
5548 // mfc1 TMP, temp_cond_reg
5549 // selnez out_reg_lo, true_reg_lo, TMP
5550 // selnez out_reg_hi, true_reg_hi, TMP
5551 can_move_conditionally = true;
5552 use_const_for_false_in = true;
5553 }
5554 // Other long conditional moves would generate 6+ instructions,
5555 // which is too many.
5556 } else {
5557 // movt out_reg_lo, true_reg_lo/ZERO, cc
5558 // movt out_reg_hi, true_reg_hi/ZERO, cc
5559 can_move_conditionally = true;
5560 use_const_for_true_in = is_true_value_zero_constant;
5561 }
5562 break;
5563 case Primitive::kPrimFloat:
5564 case Primitive::kPrimDouble:
5565 // Moving float/double on float/double condition.
5566 if (is_r6) {
5567 can_move_conditionally = true;
5568 if (is_true_value_zero_constant) {
5569 // seleqz.fmt out_reg, false_reg, temp_cond_reg
5570 use_const_for_true_in = true;
5571 } else if (is_false_value_zero_constant) {
5572 // selnez.fmt out_reg, true_reg, temp_cond_reg
5573 use_const_for_false_in = true;
5574 } else {
5575 // sel.fmt temp_cond_reg, false_reg, true_reg
5576 // mov.fmt out_reg, temp_cond_reg
5577 }
5578 } else {
5579 // movt.fmt out_reg, true_reg, cc
5580 can_move_conditionally = true;
5581 }
5582 break;
5583 }
5584 break;
5585 }
5586 }
5587
5588 if (can_move_conditionally) {
5589 DCHECK(!use_const_for_false_in || !use_const_for_true_in);
5590 } else {
5591 DCHECK(!use_const_for_false_in);
5592 DCHECK(!use_const_for_true_in);
5593 }
5594
5595 if (locations_to_set != nullptr) {
5596 if (use_const_for_false_in) {
5597 locations_to_set->SetInAt(0, Location::ConstantLocation(cst_false_value));
5598 } else {
5599 locations_to_set->SetInAt(0,
5600 Primitive::IsFloatingPointType(dst_type)
5601 ? Location::RequiresFpuRegister()
5602 : Location::RequiresRegister());
5603 }
5604 if (use_const_for_true_in) {
5605 locations_to_set->SetInAt(1, Location::ConstantLocation(cst_true_value));
5606 } else {
5607 locations_to_set->SetInAt(1,
5608 Primitive::IsFloatingPointType(dst_type)
5609 ? Location::RequiresFpuRegister()
5610 : Location::RequiresRegister());
5611 }
5612 if (materialized) {
5613 locations_to_set->SetInAt(2, Location::RequiresRegister());
5614 }
5615 // On R6 we don't require the output to be the same as the
5616 // first input for conditional moves unlike on R2.
5617 bool is_out_same_as_first_in = !can_move_conditionally || !is_r6;
5618 if (is_out_same_as_first_in) {
5619 locations_to_set->SetOut(Location::SameAsFirstInput());
5620 } else {
5621 locations_to_set->SetOut(Primitive::IsFloatingPointType(dst_type)
5622 ? Location::RequiresFpuRegister()
5623 : Location::RequiresRegister());
5624 }
5625 }
5626
5627 return can_move_conditionally;
5628}
5629
5630void InstructionCodeGeneratorMIPS::GenConditionalMoveR2(HSelect* select) {
5631 LocationSummary* locations = select->GetLocations();
5632 Location dst = locations->Out();
5633 Location src = locations->InAt(1);
5634 Register src_reg = ZERO;
5635 Register src_reg_high = ZERO;
5636 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5637 Register cond_reg = TMP;
5638 int cond_cc = 0;
5639 Primitive::Type cond_type = Primitive::kPrimInt;
5640 bool cond_inverted = false;
5641 Primitive::Type dst_type = select->GetType();
5642
5643 if (IsBooleanValueOrMaterializedCondition(cond)) {
5644 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5645 } else {
5646 HCondition* condition = cond->AsCondition();
5647 LocationSummary* cond_locations = cond->GetLocations();
5648 IfCondition if_cond = condition->GetCondition();
5649 cond_type = condition->InputAt(0)->GetType();
5650 switch (cond_type) {
5651 default:
5652 DCHECK_NE(cond_type, Primitive::kPrimLong);
5653 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5654 break;
5655 case Primitive::kPrimFloat:
5656 case Primitive::kPrimDouble:
5657 cond_inverted = MaterializeFpCompareR2(if_cond,
5658 condition->IsGtBias(),
5659 cond_type,
5660 cond_locations,
5661 cond_cc);
5662 break;
5663 }
5664 }
5665
5666 DCHECK(dst.Equals(locations->InAt(0)));
5667 if (src.IsRegister()) {
5668 src_reg = src.AsRegister<Register>();
5669 } else if (src.IsRegisterPair()) {
5670 src_reg = src.AsRegisterPairLow<Register>();
5671 src_reg_high = src.AsRegisterPairHigh<Register>();
5672 } else if (src.IsConstant()) {
5673 DCHECK(src.GetConstant()->IsZeroBitPattern());
5674 }
5675
5676 switch (cond_type) {
5677 default:
5678 switch (dst_type) {
5679 default:
5680 if (cond_inverted) {
5681 __ Movz(dst.AsRegister<Register>(), src_reg, cond_reg);
5682 } else {
5683 __ Movn(dst.AsRegister<Register>(), src_reg, cond_reg);
5684 }
5685 break;
5686 case Primitive::kPrimLong:
5687 if (cond_inverted) {
5688 __ Movz(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5689 __ Movz(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5690 } else {
5691 __ Movn(dst.AsRegisterPairLow<Register>(), src_reg, cond_reg);
5692 __ Movn(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_reg);
5693 }
5694 break;
5695 case Primitive::kPrimFloat:
5696 if (cond_inverted) {
5697 __ MovzS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5698 } else {
5699 __ MovnS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5700 }
5701 break;
5702 case Primitive::kPrimDouble:
5703 if (cond_inverted) {
5704 __ MovzD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5705 } else {
5706 __ MovnD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_reg);
5707 }
5708 break;
5709 }
5710 break;
5711 case Primitive::kPrimLong:
5712 LOG(FATAL) << "Unreachable";
5713 UNREACHABLE();
5714 case Primitive::kPrimFloat:
5715 case Primitive::kPrimDouble:
5716 switch (dst_type) {
5717 default:
5718 if (cond_inverted) {
5719 __ Movf(dst.AsRegister<Register>(), src_reg, cond_cc);
5720 } else {
5721 __ Movt(dst.AsRegister<Register>(), src_reg, cond_cc);
5722 }
5723 break;
5724 case Primitive::kPrimLong:
5725 if (cond_inverted) {
5726 __ Movf(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5727 __ Movf(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5728 } else {
5729 __ Movt(dst.AsRegisterPairLow<Register>(), src_reg, cond_cc);
5730 __ Movt(dst.AsRegisterPairHigh<Register>(), src_reg_high, cond_cc);
5731 }
5732 break;
5733 case Primitive::kPrimFloat:
5734 if (cond_inverted) {
5735 __ MovfS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5736 } else {
5737 __ MovtS(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5738 }
5739 break;
5740 case Primitive::kPrimDouble:
5741 if (cond_inverted) {
5742 __ MovfD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5743 } else {
5744 __ MovtD(dst.AsFpuRegister<FRegister>(), src.AsFpuRegister<FRegister>(), cond_cc);
5745 }
5746 break;
5747 }
5748 break;
5749 }
5750}
5751
5752void InstructionCodeGeneratorMIPS::GenConditionalMoveR6(HSelect* select) {
5753 LocationSummary* locations = select->GetLocations();
5754 Location dst = locations->Out();
5755 Location false_src = locations->InAt(0);
5756 Location true_src = locations->InAt(1);
5757 HInstruction* cond = select->InputAt(/* condition_input_index */ 2);
5758 Register cond_reg = TMP;
5759 FRegister fcond_reg = FTMP;
5760 Primitive::Type cond_type = Primitive::kPrimInt;
5761 bool cond_inverted = false;
5762 Primitive::Type dst_type = select->GetType();
5763
5764 if (IsBooleanValueOrMaterializedCondition(cond)) {
5765 cond_reg = locations->InAt(/* condition_input_index */ 2).AsRegister<Register>();
5766 } else {
5767 HCondition* condition = cond->AsCondition();
5768 LocationSummary* cond_locations = cond->GetLocations();
5769 IfCondition if_cond = condition->GetCondition();
5770 cond_type = condition->InputAt(0)->GetType();
5771 switch (cond_type) {
5772 default:
5773 DCHECK_NE(cond_type, Primitive::kPrimLong);
5774 cond_inverted = MaterializeIntCompare(if_cond, cond_locations, cond_reg);
5775 break;
5776 case Primitive::kPrimFloat:
5777 case Primitive::kPrimDouble:
5778 cond_inverted = MaterializeFpCompareR6(if_cond,
5779 condition->IsGtBias(),
5780 cond_type,
5781 cond_locations,
5782 fcond_reg);
5783 break;
5784 }
5785 }
5786
5787 if (true_src.IsConstant()) {
5788 DCHECK(true_src.GetConstant()->IsZeroBitPattern());
5789 }
5790 if (false_src.IsConstant()) {
5791 DCHECK(false_src.GetConstant()->IsZeroBitPattern());
5792 }
5793
5794 switch (dst_type) {
5795 default:
5796 if (Primitive::IsFloatingPointType(cond_type)) {
5797 __ Mfc1(cond_reg, fcond_reg);
5798 }
5799 if (true_src.IsConstant()) {
5800 if (cond_inverted) {
5801 __ Selnez(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5802 } else {
5803 __ Seleqz(dst.AsRegister<Register>(), false_src.AsRegister<Register>(), cond_reg);
5804 }
5805 } else if (false_src.IsConstant()) {
5806 if (cond_inverted) {
5807 __ Seleqz(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5808 } else {
5809 __ Selnez(dst.AsRegister<Register>(), true_src.AsRegister<Register>(), cond_reg);
5810 }
5811 } else {
5812 DCHECK_NE(cond_reg, AT);
5813 if (cond_inverted) {
5814 __ Seleqz(AT, true_src.AsRegister<Register>(), cond_reg);
5815 __ Selnez(TMP, false_src.AsRegister<Register>(), cond_reg);
5816 } else {
5817 __ Selnez(AT, true_src.AsRegister<Register>(), cond_reg);
5818 __ Seleqz(TMP, false_src.AsRegister<Register>(), cond_reg);
5819 }
5820 __ Or(dst.AsRegister<Register>(), AT, TMP);
5821 }
5822 break;
5823 case Primitive::kPrimLong: {
5824 if (Primitive::IsFloatingPointType(cond_type)) {
5825 __ Mfc1(cond_reg, fcond_reg);
5826 }
5827 Register dst_lo = dst.AsRegisterPairLow<Register>();
5828 Register dst_hi = dst.AsRegisterPairHigh<Register>();
5829 if (true_src.IsConstant()) {
5830 Register src_lo = false_src.AsRegisterPairLow<Register>();
5831 Register src_hi = false_src.AsRegisterPairHigh<Register>();
5832 if (cond_inverted) {
5833 __ Selnez(dst_lo, src_lo, cond_reg);
5834 __ Selnez(dst_hi, src_hi, cond_reg);
5835 } else {
5836 __ Seleqz(dst_lo, src_lo, cond_reg);
5837 __ Seleqz(dst_hi, src_hi, cond_reg);
5838 }
5839 } else {
5840 DCHECK(false_src.IsConstant());
5841 Register src_lo = true_src.AsRegisterPairLow<Register>();
5842 Register src_hi = true_src.AsRegisterPairHigh<Register>();
5843 if (cond_inverted) {
5844 __ Seleqz(dst_lo, src_lo, cond_reg);
5845 __ Seleqz(dst_hi, src_hi, cond_reg);
5846 } else {
5847 __ Selnez(dst_lo, src_lo, cond_reg);
5848 __ Selnez(dst_hi, src_hi, cond_reg);
5849 }
5850 }
5851 break;
5852 }
5853 case Primitive::kPrimFloat: {
5854 if (!Primitive::IsFloatingPointType(cond_type)) {
5855 // sel*.fmt tests bit 0 of the condition register, account for that.
5856 __ Sltu(TMP, ZERO, cond_reg);
5857 __ Mtc1(TMP, fcond_reg);
5858 }
5859 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5860 if (true_src.IsConstant()) {
5861 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5862 if (cond_inverted) {
5863 __ SelnezS(dst_reg, src_reg, fcond_reg);
5864 } else {
5865 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5866 }
5867 } else if (false_src.IsConstant()) {
5868 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5869 if (cond_inverted) {
5870 __ SeleqzS(dst_reg, src_reg, fcond_reg);
5871 } else {
5872 __ SelnezS(dst_reg, src_reg, fcond_reg);
5873 }
5874 } else {
5875 if (cond_inverted) {
5876 __ SelS(fcond_reg,
5877 true_src.AsFpuRegister<FRegister>(),
5878 false_src.AsFpuRegister<FRegister>());
5879 } else {
5880 __ SelS(fcond_reg,
5881 false_src.AsFpuRegister<FRegister>(),
5882 true_src.AsFpuRegister<FRegister>());
5883 }
5884 __ MovS(dst_reg, fcond_reg);
5885 }
5886 break;
5887 }
5888 case Primitive::kPrimDouble: {
5889 if (!Primitive::IsFloatingPointType(cond_type)) {
5890 // sel*.fmt tests bit 0 of the condition register, account for that.
5891 __ Sltu(TMP, ZERO, cond_reg);
5892 __ Mtc1(TMP, fcond_reg);
5893 }
5894 FRegister dst_reg = dst.AsFpuRegister<FRegister>();
5895 if (true_src.IsConstant()) {
5896 FRegister src_reg = false_src.AsFpuRegister<FRegister>();
5897 if (cond_inverted) {
5898 __ SelnezD(dst_reg, src_reg, fcond_reg);
5899 } else {
5900 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5901 }
5902 } else if (false_src.IsConstant()) {
5903 FRegister src_reg = true_src.AsFpuRegister<FRegister>();
5904 if (cond_inverted) {
5905 __ SeleqzD(dst_reg, src_reg, fcond_reg);
5906 } else {
5907 __ SelnezD(dst_reg, src_reg, fcond_reg);
5908 }
5909 } else {
5910 if (cond_inverted) {
5911 __ SelD(fcond_reg,
5912 true_src.AsFpuRegister<FRegister>(),
5913 false_src.AsFpuRegister<FRegister>());
5914 } else {
5915 __ SelD(fcond_reg,
5916 false_src.AsFpuRegister<FRegister>(),
5917 true_src.AsFpuRegister<FRegister>());
5918 }
5919 __ MovD(dst_reg, fcond_reg);
5920 }
5921 break;
5922 }
5923 }
5924}
5925
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005926void LocationsBuilderMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5927 LocationSummary* locations = new (GetGraph()->GetArena())
5928 LocationSummary(flag, LocationSummary::kNoCall);
5929 locations->SetOut(Location::RequiresRegister());
Mingyao Yang063fc772016-08-02 11:02:54 -07005930}
5931
Goran Jakovljevicc6418422016-12-05 16:31:55 +01005932void InstructionCodeGeneratorMIPS::VisitShouldDeoptimizeFlag(HShouldDeoptimizeFlag* flag) {
5933 __ LoadFromOffset(kLoadWord,
5934 flag->GetLocations()->Out().AsRegister<Register>(),
5935 SP,
5936 codegen_->GetStackOffsetOfShouldDeoptimizeFlag());
Mingyao Yang063fc772016-08-02 11:02:54 -07005937}
5938
David Brazdil74eb1b22015-12-14 11:44:01 +00005939void LocationsBuilderMIPS::VisitSelect(HSelect* select) {
5940 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(select);
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005941 CanMoveConditionally(select, codegen_->GetInstructionSetFeatures().IsR6(), locations);
David Brazdil74eb1b22015-12-14 11:44:01 +00005942}
5943
5944void InstructionCodeGeneratorMIPS::VisitSelect(HSelect* select) {
Alexey Frunze674b9ee2016-09-20 14:54:15 -07005945 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
5946 if (CanMoveConditionally(select, is_r6, /* locations_to_set */ nullptr)) {
5947 if (is_r6) {
5948 GenConditionalMoveR6(select);
5949 } else {
5950 GenConditionalMoveR2(select);
5951 }
5952 } else {
5953 LocationSummary* locations = select->GetLocations();
5954 MipsLabel false_target;
5955 GenerateTestAndBranch(select,
5956 /* condition_input_index */ 2,
5957 /* true_target */ nullptr,
5958 &false_target);
5959 codegen_->MoveLocation(locations->Out(), locations->InAt(1), select->GetType());
5960 __ Bind(&false_target);
5961 }
David Brazdil74eb1b22015-12-14 11:44:01 +00005962}
5963
David Srbecky0cf44932015-12-09 14:09:59 +00005964void LocationsBuilderMIPS::VisitNativeDebugInfo(HNativeDebugInfo* info) {
5965 new (GetGraph()->GetArena()) LocationSummary(info);
5966}
5967
David Srbeckyd28f4a02016-03-14 17:14:24 +00005968void InstructionCodeGeneratorMIPS::VisitNativeDebugInfo(HNativeDebugInfo*) {
5969 // MaybeRecordNativeDebugInfo is already called implicitly in CodeGenerator::Compile.
David Srbeckyc7098ff2016-02-09 14:30:11 +00005970}
5971
5972void CodeGeneratorMIPS::GenerateNop() {
5973 __ Nop();
David Srbecky0cf44932015-12-09 14:09:59 +00005974}
5975
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005976void LocationsBuilderMIPS::HandleFieldGet(HInstruction* instruction, const FieldInfo& field_info) {
5977 Primitive::Type field_type = field_info.GetFieldType();
5978 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
5979 bool generate_volatile = field_info.IsVolatile() && is_wide;
Alexey Frunze15958152017-02-09 19:08:30 -08005980 bool object_field_get_with_read_barrier =
5981 kEmitCompilerReadBarrier && (field_type == Primitive::kPrimNot);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005982 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Alexey Frunze15958152017-02-09 19:08:30 -08005983 instruction,
5984 generate_volatile
5985 ? LocationSummary::kCallOnMainOnly
5986 : (object_field_get_with_read_barrier
5987 ? LocationSummary::kCallOnSlowPath
5988 : LocationSummary::kNoCall));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005989
Alexey Frunzec61c0762017-04-10 13:54:23 -07005990 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
5991 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
5992 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02005993 locations->SetInAt(0, Location::RequiresRegister());
5994 if (generate_volatile) {
5995 InvokeRuntimeCallingConvention calling_convention;
5996 // need A0 to hold base + offset
5997 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
5998 if (field_type == Primitive::kPrimLong) {
5999 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimLong));
6000 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006001 // Use Location::Any() to prevent situations when running out of available fp registers.
6002 locations->SetOut(Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006003 // Need some temp core regs since FP results are returned in core registers
6004 Location reg = calling_convention.GetReturnLocation(Primitive::kPrimLong);
6005 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairLow<Register>()));
6006 locations->AddTemp(Location::RegisterLocation(reg.AsRegisterPairHigh<Register>()));
6007 }
6008 } else {
6009 if (Primitive::IsFloatingPointType(instruction->GetType())) {
6010 locations->SetOut(Location::RequiresFpuRegister());
6011 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006012 // The output overlaps in the case of an object field get with
6013 // read barriers enabled: we do not want the move to overwrite the
6014 // object's location, as we need it to emit the read barrier.
6015 locations->SetOut(Location::RequiresRegister(),
6016 object_field_get_with_read_barrier
6017 ? Location::kOutputOverlap
6018 : Location::kNoOutputOverlap);
6019 }
6020 if (object_field_get_with_read_barrier && kUseBakerReadBarrier) {
6021 // We need a temporary register for the read barrier marking slow
6022 // path in CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier.
6023 locations->AddTemp(Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006024 }
6025 }
6026}
6027
6028void InstructionCodeGeneratorMIPS::HandleFieldGet(HInstruction* instruction,
6029 const FieldInfo& field_info,
6030 uint32_t dex_pc) {
6031 Primitive::Type type = field_info.GetFieldType();
6032 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006033 Location obj_loc = locations->InAt(0);
6034 Register obj = obj_loc.AsRegister<Register>();
6035 Location dst_loc = locations->Out();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006036 LoadOperandType load_type = kLoadUnsignedByte;
6037 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006038 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006039 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006040
6041 switch (type) {
6042 case Primitive::kPrimBoolean:
6043 load_type = kLoadUnsignedByte;
6044 break;
6045 case Primitive::kPrimByte:
6046 load_type = kLoadSignedByte;
6047 break;
6048 case Primitive::kPrimShort:
6049 load_type = kLoadSignedHalfword;
6050 break;
6051 case Primitive::kPrimChar:
6052 load_type = kLoadUnsignedHalfword;
6053 break;
6054 case Primitive::kPrimInt:
6055 case Primitive::kPrimFloat:
6056 case Primitive::kPrimNot:
6057 load_type = kLoadWord;
6058 break;
6059 case Primitive::kPrimLong:
6060 case Primitive::kPrimDouble:
6061 load_type = kLoadDoubleword;
6062 break;
6063 case Primitive::kPrimVoid:
6064 LOG(FATAL) << "Unreachable type " << type;
6065 UNREACHABLE();
6066 }
6067
6068 if (is_volatile && load_type == kLoadDoubleword) {
6069 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006070 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006071 // Do implicit Null check
6072 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6073 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
Serban Constantinescufca16662016-07-14 09:21:59 +01006074 codegen_->InvokeRuntime(kQuickA64Load, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006075 CheckEntrypointTypes<kQuickA64Load, int64_t, volatile const int64_t*>();
6076 if (type == Primitive::kPrimDouble) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006077 // FP results are returned in core registers. Need to move them.
Alexey Frunze15958152017-02-09 19:08:30 -08006078 if (dst_loc.IsFpuRegister()) {
6079 __ Mtc1(locations->GetTemp(1).AsRegister<Register>(), dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006080 __ MoveToFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunze15958152017-02-09 19:08:30 -08006081 dst_loc.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006082 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006083 DCHECK(dst_loc.IsDoubleStackSlot());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006084 __ StoreToOffset(kStoreWord,
6085 locations->GetTemp(1).AsRegister<Register>(),
6086 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006087 dst_loc.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006088 __ StoreToOffset(kStoreWord,
6089 locations->GetTemp(2).AsRegister<Register>(),
6090 SP,
Alexey Frunze15958152017-02-09 19:08:30 -08006091 dst_loc.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006092 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006093 }
6094 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006095 if (type == Primitive::kPrimNot) {
6096 // /* HeapReference<Object> */ dst = *(obj + offset)
6097 if (kEmitCompilerReadBarrier && kUseBakerReadBarrier) {
6098 Location temp_loc = locations->GetTemp(0);
6099 // Note that a potential implicit null check is handled in this
6100 // CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier call.
6101 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6102 dst_loc,
6103 obj,
6104 offset,
6105 temp_loc,
6106 /* needs_null_check */ true);
6107 if (is_volatile) {
6108 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6109 }
6110 } else {
6111 __ LoadFromOffset(kLoadWord, dst_loc.AsRegister<Register>(), obj, offset, null_checker);
6112 if (is_volatile) {
6113 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6114 }
6115 // If read barriers are enabled, emit read barriers other than
6116 // Baker's using a slow path (and also unpoison the loaded
6117 // reference, if heap poisoning is enabled).
6118 codegen_->MaybeGenerateReadBarrierSlow(instruction, dst_loc, dst_loc, obj_loc, offset);
6119 }
6120 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006121 Register dst;
6122 if (type == Primitive::kPrimLong) {
Alexey Frunze15958152017-02-09 19:08:30 -08006123 DCHECK(dst_loc.IsRegisterPair());
6124 dst = dst_loc.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006125 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006126 DCHECK(dst_loc.IsRegister());
6127 dst = dst_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006128 }
Alexey Frunze2923db72016-08-20 01:55:47 -07006129 __ LoadFromOffset(load_type, dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006130 } else {
Alexey Frunze15958152017-02-09 19:08:30 -08006131 DCHECK(dst_loc.IsFpuRegister());
6132 FRegister dst = dst_loc.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006133 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006134 __ LoadSFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006135 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006136 __ LoadDFromOffset(dst, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006137 }
6138 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006139 }
6140
Alexey Frunze15958152017-02-09 19:08:30 -08006141 // Memory barriers, in the case of references, are handled in the
6142 // previous switch statement.
6143 if (is_volatile && (type != Primitive::kPrimNot)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006144 GenerateMemoryBarrier(MemBarrierKind::kLoadAny);
6145 }
6146}
6147
6148void LocationsBuilderMIPS::HandleFieldSet(HInstruction* instruction, const FieldInfo& field_info) {
6149 Primitive::Type field_type = field_info.GetFieldType();
6150 bool is_wide = (field_type == Primitive::kPrimLong) || (field_type == Primitive::kPrimDouble);
6151 bool generate_volatile = field_info.IsVolatile() && is_wide;
6152 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(
Serban Constantinescu54ff4822016-07-07 18:03:19 +01006153 instruction, generate_volatile ? LocationSummary::kCallOnMainOnly : LocationSummary::kNoCall);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006154
6155 locations->SetInAt(0, Location::RequiresRegister());
6156 if (generate_volatile) {
6157 InvokeRuntimeCallingConvention calling_convention;
6158 // need A0 to hold base + offset
6159 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
6160 if (field_type == Primitive::kPrimLong) {
6161 locations->SetInAt(1, Location::RegisterPairLocation(
6162 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
6163 } else {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006164 // Use Location::Any() to prevent situations when running out of available fp registers.
6165 locations->SetInAt(1, Location::Any());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006166 // Pass FP parameters in core registers.
6167 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(2)));
6168 locations->AddTemp(Location::RegisterLocation(calling_convention.GetRegisterAt(3)));
6169 }
6170 } else {
6171 if (Primitive::IsFloatingPointType(field_type)) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006172 locations->SetInAt(1, FpuRegisterOrConstantForStore(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006173 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006174 locations->SetInAt(1, RegisterOrZeroConstant(instruction->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006175 }
6176 }
6177}
6178
6179void InstructionCodeGeneratorMIPS::HandleFieldSet(HInstruction* instruction,
6180 const FieldInfo& field_info,
Goran Jakovljevice114da22016-12-26 14:21:43 +01006181 uint32_t dex_pc,
6182 bool value_can_be_null) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006183 Primitive::Type type = field_info.GetFieldType();
6184 LocationSummary* locations = instruction->GetLocations();
6185 Register obj = locations->InAt(0).AsRegister<Register>();
Alexey Frunzef58b2482016-09-02 22:14:06 -07006186 Location value_location = locations->InAt(1);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006187 StoreOperandType store_type = kStoreByte;
6188 bool is_volatile = field_info.IsVolatile();
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006189 uint32_t offset = field_info.GetFieldOffset().Uint32Value();
Alexey Frunzec061de12017-02-14 13:27:23 -08006190 bool needs_write_barrier = CodeGenerator::StoreNeedsWriteBarrier(type, instruction->InputAt(1));
Tijana Jakovljevic57433862017-01-17 16:59:03 +01006191 auto null_checker = GetImplicitNullChecker(instruction, codegen_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006192
6193 switch (type) {
6194 case Primitive::kPrimBoolean:
6195 case Primitive::kPrimByte:
6196 store_type = kStoreByte;
6197 break;
6198 case Primitive::kPrimShort:
6199 case Primitive::kPrimChar:
6200 store_type = kStoreHalfword;
6201 break;
6202 case Primitive::kPrimInt:
6203 case Primitive::kPrimFloat:
6204 case Primitive::kPrimNot:
6205 store_type = kStoreWord;
6206 break;
6207 case Primitive::kPrimLong:
6208 case Primitive::kPrimDouble:
6209 store_type = kStoreDoubleword;
6210 break;
6211 case Primitive::kPrimVoid:
6212 LOG(FATAL) << "Unreachable type " << type;
6213 UNREACHABLE();
6214 }
6215
6216 if (is_volatile) {
6217 GenerateMemoryBarrier(MemBarrierKind::kAnyStore);
6218 }
6219
6220 if (is_volatile && store_type == kStoreDoubleword) {
6221 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevic73a42652015-11-20 17:22:57 +01006222 __ Addiu32(locations->GetTemp(0).AsRegister<Register>(), obj, offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006223 // Do implicit Null check.
6224 __ Lw(ZERO, locations->GetTemp(0).AsRegister<Register>(), 0);
6225 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
6226 if (type == Primitive::kPrimDouble) {
6227 // Pass FP parameters in core registers.
Alexey Frunzef58b2482016-09-02 22:14:06 -07006228 if (value_location.IsFpuRegister()) {
6229 __ Mfc1(locations->GetTemp(1).AsRegister<Register>(),
6230 value_location.AsFpuRegister<FRegister>());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006231 __ MoveFromFpuHigh(locations->GetTemp(2).AsRegister<Register>(),
Alexey Frunzef58b2482016-09-02 22:14:06 -07006232 value_location.AsFpuRegister<FRegister>());
6233 } else if (value_location.IsDoubleStackSlot()) {
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006234 __ LoadFromOffset(kLoadWord,
6235 locations->GetTemp(1).AsRegister<Register>(),
6236 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006237 value_location.GetStackIndex());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006238 __ LoadFromOffset(kLoadWord,
6239 locations->GetTemp(2).AsRegister<Register>(),
6240 SP,
Alexey Frunzef58b2482016-09-02 22:14:06 -07006241 value_location.GetStackIndex() + 4);
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006242 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006243 DCHECK(value_location.IsConstant());
6244 DCHECK(value_location.GetConstant()->IsDoubleConstant());
6245 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
Goran Jakovljeviccdd822f2016-07-22 09:46:43 +02006246 __ LoadConst64(locations->GetTemp(2).AsRegister<Register>(),
6247 locations->GetTemp(1).AsRegister<Register>(),
6248 value);
6249 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006250 }
Serban Constantinescufca16662016-07-14 09:21:59 +01006251 codegen_->InvokeRuntime(kQuickA64Store, instruction, dex_pc);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006252 CheckEntrypointTypes<kQuickA64Store, void, volatile int64_t *, int64_t>();
6253 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006254 if (value_location.IsConstant()) {
6255 int64_t value = CodeGenerator::GetInt64ValueOf(value_location.GetConstant());
6256 __ StoreConstToOffset(store_type, value, obj, offset, TMP, null_checker);
6257 } else if (!Primitive::IsFloatingPointType(type)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006258 Register src;
6259 if (type == Primitive::kPrimLong) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006260 src = value_location.AsRegisterPairLow<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006261 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006262 src = value_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006263 }
Alexey Frunzec061de12017-02-14 13:27:23 -08006264 if (kPoisonHeapReferences && needs_write_barrier) {
6265 // Note that in the case where `value` is a null reference,
6266 // we do not enter this block, as a null reference does not
6267 // need poisoning.
6268 DCHECK_EQ(type, Primitive::kPrimNot);
6269 __ PoisonHeapReference(TMP, src);
6270 __ StoreToOffset(store_type, TMP, obj, offset, null_checker);
6271 } else {
6272 __ StoreToOffset(store_type, src, obj, offset, null_checker);
6273 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006274 } else {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006275 FRegister src = value_location.AsFpuRegister<FRegister>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006276 if (type == Primitive::kPrimFloat) {
Alexey Frunze2923db72016-08-20 01:55:47 -07006277 __ StoreSToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006278 } else {
Alexey Frunze2923db72016-08-20 01:55:47 -07006279 __ StoreDToOffset(src, obj, offset, null_checker);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006280 }
6281 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006282 }
6283
Alexey Frunzec061de12017-02-14 13:27:23 -08006284 if (needs_write_barrier) {
Alexey Frunzef58b2482016-09-02 22:14:06 -07006285 Register src = value_location.AsRegister<Register>();
Goran Jakovljevice114da22016-12-26 14:21:43 +01006286 codegen_->MarkGCCard(obj, src, value_can_be_null);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006287 }
6288
6289 if (is_volatile) {
6290 GenerateMemoryBarrier(MemBarrierKind::kAnyAny);
6291 }
6292}
6293
6294void LocationsBuilderMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6295 HandleFieldGet(instruction, instruction->GetFieldInfo());
6296}
6297
6298void InstructionCodeGeneratorMIPS::VisitInstanceFieldGet(HInstanceFieldGet* instruction) {
6299 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
6300}
6301
6302void LocationsBuilderMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
6303 HandleFieldSet(instruction, instruction->GetFieldInfo());
6304}
6305
6306void InstructionCodeGeneratorMIPS::VisitInstanceFieldSet(HInstanceFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01006307 HandleFieldSet(instruction,
6308 instruction->GetFieldInfo(),
6309 instruction->GetDexPc(),
6310 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006311}
6312
Alexey Frunze15958152017-02-09 19:08:30 -08006313void InstructionCodeGeneratorMIPS::GenerateReferenceLoadOneRegister(
6314 HInstruction* instruction,
6315 Location out,
6316 uint32_t offset,
6317 Location maybe_temp,
6318 ReadBarrierOption read_barrier_option) {
6319 Register out_reg = out.AsRegister<Register>();
6320 if (read_barrier_option == kWithReadBarrier) {
6321 CHECK(kEmitCompilerReadBarrier);
6322 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6323 if (kUseBakerReadBarrier) {
6324 // Load with fast path based Baker's read barrier.
6325 // /* HeapReference<Object> */ out = *(out + offset)
6326 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6327 out,
6328 out_reg,
6329 offset,
6330 maybe_temp,
6331 /* needs_null_check */ false);
6332 } else {
6333 // Load with slow path based read barrier.
6334 // Save the value of `out` into `maybe_temp` before overwriting it
6335 // in the following move operation, as we will need it for the
6336 // read barrier below.
6337 __ Move(maybe_temp.AsRegister<Register>(), out_reg);
6338 // /* HeapReference<Object> */ out = *(out + offset)
6339 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6340 codegen_->GenerateReadBarrierSlow(instruction, out, out, maybe_temp, offset);
6341 }
6342 } else {
6343 // Plain load with no read barrier.
6344 // /* HeapReference<Object> */ out = *(out + offset)
6345 __ LoadFromOffset(kLoadWord, out_reg, out_reg, offset);
6346 __ MaybeUnpoisonHeapReference(out_reg);
6347 }
6348}
6349
6350void InstructionCodeGeneratorMIPS::GenerateReferenceLoadTwoRegisters(
6351 HInstruction* instruction,
6352 Location out,
6353 Location obj,
6354 uint32_t offset,
6355 Location maybe_temp,
6356 ReadBarrierOption read_barrier_option) {
6357 Register out_reg = out.AsRegister<Register>();
6358 Register obj_reg = obj.AsRegister<Register>();
6359 if (read_barrier_option == kWithReadBarrier) {
6360 CHECK(kEmitCompilerReadBarrier);
6361 if (kUseBakerReadBarrier) {
6362 DCHECK(maybe_temp.IsRegister()) << maybe_temp;
6363 // Load with fast path based Baker's read barrier.
6364 // /* HeapReference<Object> */ out = *(obj + offset)
6365 codegen_->GenerateFieldLoadWithBakerReadBarrier(instruction,
6366 out,
6367 obj_reg,
6368 offset,
6369 maybe_temp,
6370 /* needs_null_check */ false);
6371 } else {
6372 // Load with slow path based read barrier.
6373 // /* HeapReference<Object> */ out = *(obj + offset)
6374 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6375 codegen_->GenerateReadBarrierSlow(instruction, out, out, obj, offset);
6376 }
6377 } else {
6378 // Plain load with no read barrier.
6379 // /* HeapReference<Object> */ out = *(obj + offset)
6380 __ LoadFromOffset(kLoadWord, out_reg, obj_reg, offset);
6381 __ MaybeUnpoisonHeapReference(out_reg);
6382 }
6383}
6384
6385void InstructionCodeGeneratorMIPS::GenerateGcRootFieldLoad(HInstruction* instruction,
6386 Location root,
6387 Register obj,
6388 uint32_t offset,
6389 ReadBarrierOption read_barrier_option) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07006390 Register root_reg = root.AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006391 if (read_barrier_option == kWithReadBarrier) {
6392 DCHECK(kEmitCompilerReadBarrier);
6393 if (kUseBakerReadBarrier) {
6394 // Fast path implementation of art::ReadBarrier::BarrierForRoot when
6395 // Baker's read barrier are used:
6396 //
6397 // root = obj.field;
6398 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6399 // if (temp != null) {
6400 // root = temp(root)
6401 // }
6402
6403 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6404 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6405 static_assert(
6406 sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(GcRoot<mirror::Object>),
6407 "art::mirror::CompressedReference<mirror::Object> and art::GcRoot<mirror::Object> "
6408 "have different sizes.");
6409 static_assert(sizeof(mirror::CompressedReference<mirror::Object>) == sizeof(int32_t),
6410 "art::mirror::CompressedReference<mirror::Object> and int32_t "
6411 "have different sizes.");
6412
6413 // Slow path marking the GC root `root`.
6414 Location temp = Location::RegisterLocation(T9);
6415 SlowPathCodeMIPS* slow_path =
6416 new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(
6417 instruction,
6418 root,
6419 /*entrypoint*/ temp);
6420 codegen_->AddSlowPath(slow_path);
6421
6422 // temp = Thread::Current()->pReadBarrierMarkReg ## root.reg()
6423 const int32_t entry_point_offset =
6424 CodeGenerator::GetReadBarrierMarkEntryPointsOffset<kMipsPointerSize>(root.reg() - 1);
6425 // Loading the entrypoint does not require a load acquire since it is only changed when
6426 // threads are suspended or running a checkpoint.
6427 __ LoadFromOffset(kLoadWord, temp.AsRegister<Register>(), TR, entry_point_offset);
6428 // The entrypoint is null when the GC is not marking, this prevents one load compared to
6429 // checking GetIsGcMarking.
6430 __ Bnez(temp.AsRegister<Register>(), slow_path->GetEntryLabel());
6431 __ Bind(slow_path->GetExitLabel());
6432 } else {
6433 // GC root loaded through a slow path for read barriers other
6434 // than Baker's.
6435 // /* GcRoot<mirror::Object>* */ root = obj + offset
6436 __ Addiu32(root_reg, obj, offset);
6437 // /* mirror::Object* */ root = root->Read()
6438 codegen_->GenerateReadBarrierForRootSlow(instruction, root, root);
6439 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07006440 } else {
6441 // Plain GC root load with no read barrier.
6442 // /* GcRoot<mirror::Object> */ root = *(obj + offset)
6443 __ LoadFromOffset(kLoadWord, root_reg, obj, offset);
6444 // Note that GC roots are not affected by heap poisoning, thus we
6445 // do not have to unpoison `root_reg` here.
6446 }
6447}
6448
Alexey Frunze15958152017-02-09 19:08:30 -08006449void CodeGeneratorMIPS::GenerateFieldLoadWithBakerReadBarrier(HInstruction* instruction,
6450 Location ref,
6451 Register obj,
6452 uint32_t offset,
6453 Location temp,
6454 bool needs_null_check) {
6455 DCHECK(kEmitCompilerReadBarrier);
6456 DCHECK(kUseBakerReadBarrier);
6457
6458 // /* HeapReference<Object> */ ref = *(obj + offset)
6459 Location no_index = Location::NoLocation();
6460 ScaleFactor no_scale_factor = TIMES_1;
6461 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6462 ref,
6463 obj,
6464 offset,
6465 no_index,
6466 no_scale_factor,
6467 temp,
6468 needs_null_check);
6469}
6470
6471void CodeGeneratorMIPS::GenerateArrayLoadWithBakerReadBarrier(HInstruction* instruction,
6472 Location ref,
6473 Register obj,
6474 uint32_t data_offset,
6475 Location index,
6476 Location temp,
6477 bool needs_null_check) {
6478 DCHECK(kEmitCompilerReadBarrier);
6479 DCHECK(kUseBakerReadBarrier);
6480
6481 static_assert(
6482 sizeof(mirror::HeapReference<mirror::Object>) == sizeof(int32_t),
6483 "art::mirror::HeapReference<art::mirror::Object> and int32_t have different sizes.");
6484 // /* HeapReference<Object> */ ref =
6485 // *(obj + data_offset + index * sizeof(HeapReference<Object>))
6486 ScaleFactor scale_factor = TIMES_4;
6487 GenerateReferenceLoadWithBakerReadBarrier(instruction,
6488 ref,
6489 obj,
6490 data_offset,
6491 index,
6492 scale_factor,
6493 temp,
6494 needs_null_check);
6495}
6496
6497void CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier(HInstruction* instruction,
6498 Location ref,
6499 Register obj,
6500 uint32_t offset,
6501 Location index,
6502 ScaleFactor scale_factor,
6503 Location temp,
6504 bool needs_null_check,
6505 bool always_update_field) {
6506 DCHECK(kEmitCompilerReadBarrier);
6507 DCHECK(kUseBakerReadBarrier);
6508
6509 // In slow path based read barriers, the read barrier call is
6510 // inserted after the original load. However, in fast path based
6511 // Baker's read barriers, we need to perform the load of
6512 // mirror::Object::monitor_ *before* the original reference load.
6513 // This load-load ordering is required by the read barrier.
6514 // The fast path/slow path (for Baker's algorithm) should look like:
6515 //
6516 // uint32_t rb_state = Lockword(obj->monitor_).ReadBarrierState();
6517 // lfence; // Load fence or artificial data dependency to prevent load-load reordering
6518 // HeapReference<Object> ref = *src; // Original reference load.
6519 // bool is_gray = (rb_state == ReadBarrier::GrayState());
6520 // if (is_gray) {
6521 // ref = ReadBarrier::Mark(ref); // Performed by runtime entrypoint slow path.
6522 // }
6523 //
6524 // Note: the original implementation in ReadBarrier::Barrier is
6525 // slightly more complex as it performs additional checks that we do
6526 // not do here for performance reasons.
6527
6528 Register ref_reg = ref.AsRegister<Register>();
6529 Register temp_reg = temp.AsRegister<Register>();
6530 uint32_t monitor_offset = mirror::Object::MonitorOffset().Int32Value();
6531
6532 // /* int32_t */ monitor = obj->monitor_
6533 __ LoadFromOffset(kLoadWord, temp_reg, obj, monitor_offset);
6534 if (needs_null_check) {
6535 MaybeRecordImplicitNullCheck(instruction);
6536 }
6537 // /* LockWord */ lock_word = LockWord(monitor)
6538 static_assert(sizeof(LockWord) == sizeof(int32_t),
6539 "art::LockWord and int32_t have different sizes.");
6540
6541 __ Sync(0); // Barrier to prevent load-load reordering.
6542
6543 // The actual reference load.
6544 if (index.IsValid()) {
6545 // Load types involving an "index": ArrayGet,
6546 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6547 // intrinsics.
6548 // /* HeapReference<Object> */ ref = *(obj + offset + (index << scale_factor))
6549 if (index.IsConstant()) {
6550 size_t computed_offset =
6551 (index.GetConstant()->AsIntConstant()->GetValue() << scale_factor) + offset;
6552 __ LoadFromOffset(kLoadWord, ref_reg, obj, computed_offset);
6553 } else {
6554 // Handle the special case of the
6555 // UnsafeGetObject/UnsafeGetObjectVolatile and UnsafeCASObject
6556 // intrinsics, which use a register pair as index ("long
6557 // offset"), of which only the low part contains data.
6558 Register index_reg = index.IsRegisterPair()
6559 ? index.AsRegisterPairLow<Register>()
6560 : index.AsRegister<Register>();
Chris Larsencd0295d2017-03-31 15:26:54 -07006561 __ ShiftAndAdd(TMP, index_reg, obj, scale_factor, TMP);
Alexey Frunze15958152017-02-09 19:08:30 -08006562 __ LoadFromOffset(kLoadWord, ref_reg, TMP, offset);
6563 }
6564 } else {
6565 // /* HeapReference<Object> */ ref = *(obj + offset)
6566 __ LoadFromOffset(kLoadWord, ref_reg, obj, offset);
6567 }
6568
6569 // Object* ref = ref_addr->AsMirrorPtr()
6570 __ MaybeUnpoisonHeapReference(ref_reg);
6571
6572 // Slow path marking the object `ref` when it is gray.
6573 SlowPathCodeMIPS* slow_path;
6574 if (always_update_field) {
6575 // ReadBarrierMarkAndUpdateFieldSlowPathMIPS only supports address
6576 // of the form `obj + field_offset`, where `obj` is a register and
6577 // `field_offset` is a register pair (of which only the lower half
6578 // is used). Thus `offset` and `scale_factor` above are expected
6579 // to be null in this code path.
6580 DCHECK_EQ(offset, 0u);
6581 DCHECK_EQ(scale_factor, ScaleFactor::TIMES_1);
6582 slow_path = new (GetGraph()->GetArena())
6583 ReadBarrierMarkAndUpdateFieldSlowPathMIPS(instruction,
6584 ref,
6585 obj,
6586 /* field_offset */ index,
6587 temp_reg);
6588 } else {
6589 slow_path = new (GetGraph()->GetArena()) ReadBarrierMarkSlowPathMIPS(instruction, ref);
6590 }
6591 AddSlowPath(slow_path);
6592
6593 // if (rb_state == ReadBarrier::GrayState())
6594 // ref = ReadBarrier::Mark(ref);
6595 // Given the numeric representation, it's enough to check the low bit of the
6596 // rb_state. We do that by shifting the bit into the sign bit (31) and
6597 // performing a branch on less than zero.
6598 static_assert(ReadBarrier::WhiteState() == 0, "Expecting white to have value 0");
6599 static_assert(ReadBarrier::GrayState() == 1, "Expecting gray to have value 1");
6600 static_assert(LockWord::kReadBarrierStateSize == 1, "Expecting 1-bit read barrier state size");
6601 __ Sll(temp_reg, temp_reg, 31 - LockWord::kReadBarrierStateShift);
6602 __ Bltz(temp_reg, slow_path->GetEntryLabel());
6603 __ Bind(slow_path->GetExitLabel());
6604}
6605
6606void CodeGeneratorMIPS::GenerateReadBarrierSlow(HInstruction* instruction,
6607 Location out,
6608 Location ref,
6609 Location obj,
6610 uint32_t offset,
6611 Location index) {
6612 DCHECK(kEmitCompilerReadBarrier);
6613
6614 // Insert a slow path based read barrier *after* the reference load.
6615 //
6616 // If heap poisoning is enabled, the unpoisoning of the loaded
6617 // reference will be carried out by the runtime within the slow
6618 // path.
6619 //
6620 // Note that `ref` currently does not get unpoisoned (when heap
6621 // poisoning is enabled), which is alright as the `ref` argument is
6622 // not used by the artReadBarrierSlow entry point.
6623 //
6624 // TODO: Unpoison `ref` when it is used by artReadBarrierSlow.
6625 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena())
6626 ReadBarrierForHeapReferenceSlowPathMIPS(instruction, out, ref, obj, offset, index);
6627 AddSlowPath(slow_path);
6628
6629 __ B(slow_path->GetEntryLabel());
6630 __ Bind(slow_path->GetExitLabel());
6631}
6632
6633void CodeGeneratorMIPS::MaybeGenerateReadBarrierSlow(HInstruction* instruction,
6634 Location out,
6635 Location ref,
6636 Location obj,
6637 uint32_t offset,
6638 Location index) {
6639 if (kEmitCompilerReadBarrier) {
6640 // Baker's read barriers shall be handled by the fast path
6641 // (CodeGeneratorMIPS::GenerateReferenceLoadWithBakerReadBarrier).
6642 DCHECK(!kUseBakerReadBarrier);
6643 // If heap poisoning is enabled, unpoisoning will be taken care of
6644 // by the runtime within the slow path.
6645 GenerateReadBarrierSlow(instruction, out, ref, obj, offset, index);
6646 } else if (kPoisonHeapReferences) {
6647 __ UnpoisonHeapReference(out.AsRegister<Register>());
6648 }
6649}
6650
6651void CodeGeneratorMIPS::GenerateReadBarrierForRootSlow(HInstruction* instruction,
6652 Location out,
6653 Location root) {
6654 DCHECK(kEmitCompilerReadBarrier);
6655
6656 // Insert a slow path based read barrier *after* the GC root load.
6657 //
6658 // Note that GC roots are not affected by heap poisoning, so we do
6659 // not need to do anything special for this here.
6660 SlowPathCodeMIPS* slow_path =
6661 new (GetGraph()->GetArena()) ReadBarrierForRootSlowPathMIPS(instruction, out, root);
6662 AddSlowPath(slow_path);
6663
6664 __ B(slow_path->GetEntryLabel());
6665 __ Bind(slow_path->GetExitLabel());
6666}
6667
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006668void LocationsBuilderMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006669 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
6670 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07006671 bool baker_read_barrier_slow_path = false;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006672 switch (type_check_kind) {
6673 case TypeCheckKind::kExactCheck:
6674 case TypeCheckKind::kAbstractClassCheck:
6675 case TypeCheckKind::kClassHierarchyCheck:
6676 case TypeCheckKind::kArrayObjectCheck:
Alexey Frunze15958152017-02-09 19:08:30 -08006677 call_kind =
6678 kEmitCompilerReadBarrier ? LocationSummary::kCallOnSlowPath : LocationSummary::kNoCall;
Alexey Frunzec61c0762017-04-10 13:54:23 -07006679 baker_read_barrier_slow_path = kUseBakerReadBarrier;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006680 break;
6681 case TypeCheckKind::kArrayCheck:
6682 case TypeCheckKind::kUnresolvedCheck:
6683 case TypeCheckKind::kInterfaceCheck:
6684 call_kind = LocationSummary::kCallOnSlowPath;
6685 break;
6686 }
6687
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006688 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07006689 if (baker_read_barrier_slow_path) {
6690 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
6691 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006692 locations->SetInAt(0, Location::RequiresRegister());
6693 locations->SetInAt(1, Location::RequiresRegister());
6694 // The output does overlap inputs.
6695 // Note that TypeCheckSlowPathMIPS uses this register too.
6696 locations->SetOut(Location::RequiresRegister(), Location::kOutputOverlap);
Alexey Frunze15958152017-02-09 19:08:30 -08006697 locations->AddRegisterTemps(NumberOfInstanceOfTemps(type_check_kind));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006698}
6699
6700void InstructionCodeGeneratorMIPS::VisitInstanceOf(HInstanceOf* instruction) {
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006701 TypeCheckKind type_check_kind = instruction->GetTypeCheckKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006702 LocationSummary* locations = instruction->GetLocations();
Alexey Frunze15958152017-02-09 19:08:30 -08006703 Location obj_loc = locations->InAt(0);
6704 Register obj = obj_loc.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006705 Register cls = locations->InAt(1).AsRegister<Register>();
Alexey Frunze15958152017-02-09 19:08:30 -08006706 Location out_loc = locations->Out();
6707 Register out = out_loc.AsRegister<Register>();
6708 const size_t num_temps = NumberOfInstanceOfTemps(type_check_kind);
6709 DCHECK_LE(num_temps, 1u);
6710 Location maybe_temp_loc = (num_temps >= 1) ? locations->GetTemp(0) : Location::NoLocation();
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006711 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
6712 uint32_t super_offset = mirror::Class::SuperClassOffset().Int32Value();
6713 uint32_t component_offset = mirror::Class::ComponentTypeOffset().Int32Value();
6714 uint32_t primitive_offset = mirror::Class::PrimitiveTypeOffset().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006715 MipsLabel done;
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006716 SlowPathCodeMIPS* slow_path = nullptr;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006717
6718 // Return 0 if `obj` is null.
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006719 // Avoid this check if we know `obj` is not null.
6720 if (instruction->MustDoNullCheck()) {
6721 __ Move(out, ZERO);
6722 __ Beqz(obj, &done);
6723 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006724
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006725 switch (type_check_kind) {
6726 case TypeCheckKind::kExactCheck: {
6727 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006728 GenerateReferenceLoadTwoRegisters(instruction,
6729 out_loc,
6730 obj_loc,
6731 class_offset,
6732 maybe_temp_loc,
6733 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006734 // Classes must be equal for the instanceof to succeed.
6735 __ Xor(out, out, cls);
6736 __ Sltiu(out, out, 1);
6737 break;
6738 }
6739
6740 case TypeCheckKind::kAbstractClassCheck: {
6741 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006742 GenerateReferenceLoadTwoRegisters(instruction,
6743 out_loc,
6744 obj_loc,
6745 class_offset,
6746 maybe_temp_loc,
6747 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006748 // If the class is abstract, we eagerly fetch the super class of the
6749 // object to avoid doing a comparison we know will fail.
6750 MipsLabel loop;
6751 __ Bind(&loop);
6752 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006753 GenerateReferenceLoadOneRegister(instruction,
6754 out_loc,
6755 super_offset,
6756 maybe_temp_loc,
6757 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006758 // If `out` is null, we use it for the result, and jump to `done`.
6759 __ Beqz(out, &done);
6760 __ Bne(out, cls, &loop);
6761 __ LoadConst32(out, 1);
6762 break;
6763 }
6764
6765 case TypeCheckKind::kClassHierarchyCheck: {
6766 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006767 GenerateReferenceLoadTwoRegisters(instruction,
6768 out_loc,
6769 obj_loc,
6770 class_offset,
6771 maybe_temp_loc,
6772 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006773 // Walk over the class hierarchy to find a match.
6774 MipsLabel loop, success;
6775 __ Bind(&loop);
6776 __ Beq(out, cls, &success);
6777 // /* HeapReference<Class> */ out = out->super_class_
Alexey Frunze15958152017-02-09 19:08:30 -08006778 GenerateReferenceLoadOneRegister(instruction,
6779 out_loc,
6780 super_offset,
6781 maybe_temp_loc,
6782 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006783 __ Bnez(out, &loop);
6784 // If `out` is null, we use it for the result, and jump to `done`.
6785 __ B(&done);
6786 __ Bind(&success);
6787 __ LoadConst32(out, 1);
6788 break;
6789 }
6790
6791 case TypeCheckKind::kArrayObjectCheck: {
6792 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006793 GenerateReferenceLoadTwoRegisters(instruction,
6794 out_loc,
6795 obj_loc,
6796 class_offset,
6797 maybe_temp_loc,
6798 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006799 // Do an exact check.
6800 MipsLabel success;
6801 __ Beq(out, cls, &success);
6802 // Otherwise, we need to check that the object's class is a non-primitive array.
6803 // /* HeapReference<Class> */ out = out->component_type_
Alexey Frunze15958152017-02-09 19:08:30 -08006804 GenerateReferenceLoadOneRegister(instruction,
6805 out_loc,
6806 component_offset,
6807 maybe_temp_loc,
6808 kCompilerReadBarrierOption);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006809 // If `out` is null, we use it for the result, and jump to `done`.
6810 __ Beqz(out, &done);
6811 __ LoadFromOffset(kLoadUnsignedHalfword, out, out, primitive_offset);
6812 static_assert(Primitive::kPrimNot == 0, "Expected 0 for kPrimNot");
6813 __ Sltiu(out, out, 1);
6814 __ B(&done);
6815 __ Bind(&success);
6816 __ LoadConst32(out, 1);
6817 break;
6818 }
6819
6820 case TypeCheckKind::kArrayCheck: {
6821 // No read barrier since the slow path will retry upon failure.
6822 // /* HeapReference<Class> */ out = obj->klass_
Alexey Frunze15958152017-02-09 19:08:30 -08006823 GenerateReferenceLoadTwoRegisters(instruction,
6824 out_loc,
6825 obj_loc,
6826 class_offset,
6827 maybe_temp_loc,
6828 kWithoutReadBarrier);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006829 DCHECK(locations->OnlyCallsOnSlowPath());
6830 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6831 /* is_fatal */ false);
6832 codegen_->AddSlowPath(slow_path);
6833 __ Bne(out, cls, slow_path->GetEntryLabel());
6834 __ LoadConst32(out, 1);
6835 break;
6836 }
6837
6838 case TypeCheckKind::kUnresolvedCheck:
6839 case TypeCheckKind::kInterfaceCheck: {
6840 // Note that we indeed only call on slow path, but we always go
6841 // into the slow path for the unresolved and interface check
6842 // cases.
6843 //
6844 // We cannot directly call the InstanceofNonTrivial runtime
6845 // entry point without resorting to a type checking slow path
6846 // here (i.e. by calling InvokeRuntime directly), as it would
6847 // require to assign fixed registers for the inputs of this
6848 // HInstanceOf instruction (following the runtime calling
6849 // convention), which might be cluttered by the potential first
6850 // read barrier emission at the beginning of this method.
6851 //
6852 // TODO: Introduce a new runtime entry point taking the object
6853 // to test (instead of its class) as argument, and let it deal
6854 // with the read barrier issues. This will let us refactor this
6855 // case of the `switch` code as it was previously (with a direct
6856 // call to the runtime not using a type checking slow path).
6857 // This should also be beneficial for the other cases above.
6858 DCHECK(locations->OnlyCallsOnSlowPath());
6859 slow_path = new (GetGraph()->GetArena()) TypeCheckSlowPathMIPS(instruction,
6860 /* is_fatal */ false);
6861 codegen_->AddSlowPath(slow_path);
6862 __ B(slow_path->GetEntryLabel());
6863 break;
6864 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006865 }
6866
6867 __ Bind(&done);
Alexey Frunze66b69ad2017-02-24 00:51:44 -08006868
6869 if (slow_path != nullptr) {
6870 __ Bind(slow_path->GetExitLabel());
6871 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006872}
6873
6874void LocationsBuilderMIPS::VisitIntConstant(HIntConstant* constant) {
6875 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6876 locations->SetOut(Location::ConstantLocation(constant));
6877}
6878
6879void InstructionCodeGeneratorMIPS::VisitIntConstant(HIntConstant* constant ATTRIBUTE_UNUSED) {
6880 // Will be generated at use site.
6881}
6882
6883void LocationsBuilderMIPS::VisitNullConstant(HNullConstant* constant) {
6884 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
6885 locations->SetOut(Location::ConstantLocation(constant));
6886}
6887
6888void InstructionCodeGeneratorMIPS::VisitNullConstant(HNullConstant* constant ATTRIBUTE_UNUSED) {
6889 // Will be generated at use site.
6890}
6891
6892void LocationsBuilderMIPS::HandleInvoke(HInvoke* invoke) {
6893 InvokeDexCallingConventionVisitorMIPS calling_convention_visitor;
6894 CodeGenerator::CreateCommonInvokeLocationSummary(invoke, &calling_convention_visitor);
6895}
6896
6897void LocationsBuilderMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6898 HandleInvoke(invoke);
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006899 // The register T7 is required to be used for the hidden argument in
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006900 // art_quick_imt_conflict_trampoline, so add the hidden argument.
Alexey Frunze1b8464d2016-11-12 17:22:05 -08006901 invoke->GetLocations()->AddTemp(Location::RegisterLocation(T7));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006902}
6903
6904void InstructionCodeGeneratorMIPS::VisitInvokeInterface(HInvokeInterface* invoke) {
6905 // TODO: b/18116999, our IMTs can miss an IncompatibleClassChangeError.
6906 Register temp = invoke->GetLocations()->GetTemp(0).AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006907 Location receiver = invoke->GetLocations()->InAt(0);
6908 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07006909 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006910
6911 // Set the hidden argument.
6912 __ LoadConst32(invoke->GetLocations()->GetTemp(1).AsRegister<Register>(),
6913 invoke->GetDexMethodIndex());
6914
6915 // temp = object->GetClass();
6916 if (receiver.IsStackSlot()) {
6917 __ LoadFromOffset(kLoadWord, temp, SP, receiver.GetStackIndex());
6918 __ LoadFromOffset(kLoadWord, temp, temp, class_offset);
6919 } else {
6920 __ LoadFromOffset(kLoadWord, temp, receiver.AsRegister<Register>(), class_offset);
6921 }
6922 codegen_->MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08006923 // Instead of simply (possibly) unpoisoning `temp` here, we should
6924 // emit a read barrier for the previous class reference load.
6925 // However this is not required in practice, as this is an
6926 // intermediate/temporary reference and because the current
6927 // concurrent copying collector keeps the from-space memory
6928 // intact/accessible until the end of the marking phase (the
6929 // concurrent copying collector may not in the future).
6930 __ MaybeUnpoisonHeapReference(temp);
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00006931 __ LoadFromOffset(kLoadWord, temp, temp,
6932 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
6933 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00006934 invoke->GetImtIndex(), kMipsPointerSize));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006935 // temp = temp->GetImtEntryAt(method_offset);
6936 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
6937 // T9 = temp->GetEntryPoint();
6938 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
6939 // T9();
6940 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07006941 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006942 DCHECK(!codegen_->IsLeafMethod());
6943 codegen_->RecordPcInfo(invoke, invoke->GetDexPc());
6944}
6945
6946void LocationsBuilderMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
Chris Larsen701566a2015-10-27 15:29:13 -07006947 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6948 if (intrinsic.TryDispatch(invoke)) {
6949 return;
6950 }
6951
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006952 HandleInvoke(invoke);
6953}
6954
6955void LocationsBuilderMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00006956 // Explicit clinit checks triggered by static invokes must have been pruned by
6957 // art::PrepareForRegisterAllocation.
6958 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006959
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006960 bool is_r6 = codegen_->GetInstructionSetFeatures().IsR6();
Vladimir Marko65979462017-05-19 17:25:12 +01006961 bool has_extra_input = invoke->HasPcRelativeMethodLoadKind() && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006962
Chris Larsen701566a2015-10-27 15:29:13 -07006963 IntrinsicLocationsBuilderMIPS intrinsic(codegen_);
6964 if (intrinsic.TryDispatch(invoke)) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006965 if (invoke->GetLocations()->CanCall() && has_extra_input) {
6966 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::Any());
6967 }
Chris Larsen701566a2015-10-27 15:29:13 -07006968 return;
6969 }
6970
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006971 HandleInvoke(invoke);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07006972
6973 // Add the extra input register if either the dex cache array base register
6974 // or the PC-relative base register for accessing literals is needed.
6975 if (has_extra_input) {
6976 invoke->GetLocations()->SetInAt(invoke->GetSpecialInputIndex(), Location::RequiresRegister());
6977 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006978}
6979
Orion Hodsonac141392017-01-13 11:53:47 +00006980void LocationsBuilderMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6981 HandleInvoke(invoke);
6982}
6983
6984void InstructionCodeGeneratorMIPS::VisitInvokePolymorphic(HInvokePolymorphic* invoke) {
6985 codegen_->GenerateInvokePolymorphicCall(invoke);
6986}
6987
Chris Larsen701566a2015-10-27 15:29:13 -07006988static bool TryGenerateIntrinsicCode(HInvoke* invoke, CodeGeneratorMIPS* codegen) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006989 if (invoke->GetLocations()->Intrinsified()) {
Chris Larsen701566a2015-10-27 15:29:13 -07006990 IntrinsicCodeGeneratorMIPS intrinsic(codegen);
6991 intrinsic.Dispatch(invoke);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02006992 return true;
6993 }
6994 return false;
6995}
6996
Vladimir Markocac5a7e2016-02-22 10:39:50 +00006997HLoadString::LoadKind CodeGeneratorMIPS::GetSupportedLoadStringKind(
Alexey Frunze06a46c42016-07-19 15:00:40 -07006998 HLoadString::LoadKind desired_string_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08006999 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007000 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007001 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
Vladimir Markoaad75c62016-10-03 08:46:48 +00007002 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007003 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007004 bool is_r6 = GetInstructionSetFeatures().IsR6();
7005 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007006 switch (desired_string_load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007007 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007008 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007009 DCHECK(!Runtime::Current()->UseJitCompilation());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007010 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007011 case HLoadString::LoadKind::kBootImageAddress:
7012 break;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007013 case HLoadString::LoadKind::kJitTableAddress:
7014 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007015 fallback_load = false;
Nicolas Geoffray132d8362016-11-16 09:19:42 +00007016 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007017 case HLoadString::LoadKind::kRuntimeCall:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007018 fallback_load = false;
7019 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007020 }
7021 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007022 desired_string_load_kind = HLoadString::LoadKind::kRuntimeCall;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007023 }
7024 return desired_string_load_kind;
Vladimir Markocac5a7e2016-02-22 10:39:50 +00007025}
7026
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007027HLoadClass::LoadKind CodeGeneratorMIPS::GetSupportedLoadClassKind(
7028 HLoadClass::LoadKind desired_class_load_kind) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007029 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunze06a46c42016-07-19 15:00:40 -07007030 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007031 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
7032 // with irreducible loops.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007033 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007034 bool is_r6 = GetInstructionSetFeatures().IsR6();
7035 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007036 switch (desired_class_load_kind) {
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007037 case HLoadClass::LoadKind::kInvalid:
7038 LOG(FATAL) << "UNREACHABLE";
7039 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007040 case HLoadClass::LoadKind::kReferrersClass:
7041 fallback_load = false;
7042 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007043 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007044 case HLoadClass::LoadKind::kBssEntry:
7045 DCHECK(!Runtime::Current()->UseJitCompilation());
7046 break;
Vladimir Marko764d4542017-05-16 10:31:41 +01007047 case HLoadClass::LoadKind::kBootImageAddress:
7048 break;
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007049 case HLoadClass::LoadKind::kJitTableAddress:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007050 DCHECK(Runtime::Current()->UseJitCompilation());
Alexey Frunze627c1a02017-01-30 19:28:14 -08007051 fallback_load = false;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007052 break;
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007053 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007054 fallback_load = false;
7055 break;
7056 }
7057 if (fallback_load) {
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007058 desired_class_load_kind = HLoadClass::LoadKind::kRuntimeCall;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007059 }
7060 return desired_class_load_kind;
Vladimir Markodbb7f5b2016-03-30 13:23:58 +01007061}
7062
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007063Register CodeGeneratorMIPS::GetInvokeStaticOrDirectExtraParameter(HInvokeStaticOrDirect* invoke,
7064 Register temp) {
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007065 CHECK(!GetInstructionSetFeatures().IsR6());
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007066 CHECK_EQ(invoke->InputCount(), invoke->GetNumberOfArguments() + 1u);
7067 Location location = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
7068 if (!invoke->GetLocations()->Intrinsified()) {
7069 return location.AsRegister<Register>();
7070 }
7071 // For intrinsics we allow any location, so it may be on the stack.
7072 if (!location.IsRegister()) {
7073 __ LoadFromOffset(kLoadWord, temp, SP, location.GetStackIndex());
7074 return temp;
7075 }
7076 // For register locations, check if the register was saved. If so, get it from the stack.
7077 // Note: There is a chance that the register was saved but not overwritten, so we could
7078 // save one load. However, since this is just an intrinsic slow path we prefer this
7079 // simple and more robust approach rather that trying to determine if that's the case.
7080 SlowPathCode* slow_path = GetCurrentSlowPath();
7081 DCHECK(slow_path != nullptr); // For intrinsified invokes the call is emitted on the slow path.
7082 if (slow_path->IsCoreRegisterSaved(location.AsRegister<Register>())) {
7083 int stack_offset = slow_path->GetStackOffsetOfCoreRegister(location.AsRegister<Register>());
7084 __ LoadFromOffset(kLoadWord, temp, SP, stack_offset);
7085 return temp;
7086 }
7087 return location.AsRegister<Register>();
7088}
7089
Vladimir Markodc151b22015-10-15 18:02:30 +01007090HInvokeStaticOrDirect::DispatchInfo CodeGeneratorMIPS::GetSupportedInvokeStaticOrDirectDispatch(
7091 const HInvokeStaticOrDirect::DispatchInfo& desired_dispatch_info,
Nicolas Geoffray5e4e11e2016-09-22 13:17:41 +01007092 HInvokeStaticOrDirect* invoke ATTRIBUTE_UNUSED) {
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007093 HInvokeStaticOrDirect::DispatchInfo dispatch_info = desired_dispatch_info;
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007094 // We disable PC-relative load on pre-R6 when there is an irreducible loop, as the optimization
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007095 // is incompatible with it.
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007096 // TODO: Create as many HMipsComputeBaseMethodAddress instructions as needed for methods
7097 // with irreducible loops.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007098 bool has_irreducible_loops = GetGraph()->HasIrreducibleLoops();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007099 bool is_r6 = GetInstructionSetFeatures().IsR6();
7100 bool fallback_load = has_irreducible_loops && !is_r6;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007101 switch (dispatch_info.method_load_kind) {
Vladimir Marko65979462017-05-19 17:25:12 +01007102 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007103 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007104 break;
Vladimir Markodc151b22015-10-15 18:02:30 +01007105 default:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007106 fallback_load = false;
Vladimir Markodc151b22015-10-15 18:02:30 +01007107 break;
7108 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007109 if (fallback_load) {
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007110 dispatch_info.method_load_kind = HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall;
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007111 dispatch_info.method_load_data = 0;
7112 }
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007113 return dispatch_info;
Vladimir Markodc151b22015-10-15 18:02:30 +01007114}
7115
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007116void CodeGeneratorMIPS::GenerateStaticOrDirectCall(
7117 HInvokeStaticOrDirect* invoke, Location temp, SlowPathCode* slow_path) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007118 // All registers are assumed to be correctly set up per the calling convention.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007119 Location callee_method = temp; // For all kinds except kRecursive, callee will be in temp.
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007120 HInvokeStaticOrDirect::MethodLoadKind method_load_kind = invoke->GetMethodLoadKind();
7121 HInvokeStaticOrDirect::CodePtrLocation code_ptr_location = invoke->GetCodePtrLocation();
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007122 bool is_r6 = GetInstructionSetFeatures().IsR6();
Vladimir Marko65979462017-05-19 17:25:12 +01007123 Register base_reg = (invoke->HasPcRelativeMethodLoadKind() && !is_r6)
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007124 ? GetInvokeStaticOrDirectExtraParameter(invoke, temp.AsRegister<Register>())
7125 : ZERO;
7126
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007127 switch (method_load_kind) {
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007128 case HInvokeStaticOrDirect::MethodLoadKind::kStringInit: {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007129 // temp = thread->string_init_entrypoint
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007130 uint32_t offset =
7131 GetThreadOffset<kMipsPointerSize>(invoke->GetStringInitEntryPoint()).Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007132 __ LoadFromOffset(kLoadWord,
7133 temp.AsRegister<Register>(),
7134 TR,
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007135 offset);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007136 break;
Nicolas Geoffrayda079bb2016-09-26 17:56:07 +01007137 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007138 case HInvokeStaticOrDirect::MethodLoadKind::kRecursive:
Vladimir Markoc53c0792015-11-19 15:48:33 +00007139 callee_method = invoke->GetLocations()->InAt(invoke->GetSpecialInputIndex());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007140 break;
Vladimir Marko65979462017-05-19 17:25:12 +01007141 case HInvokeStaticOrDirect::MethodLoadKind::kBootImageLinkTimePcRelative: {
7142 DCHECK(GetCompilerOptions().IsBootImage());
7143 PcRelativePatchInfo* info = NewPcRelativeMethodPatch(invoke->GetTargetMethod());
7144 bool reordering = __ SetReorder(false);
7145 Register temp_reg = temp.AsRegister<Register>();
Alexey Frunze6079dca2017-05-28 19:10:28 -07007146 EmitPcRelativeAddressPlaceholderHigh(info, TMP, base_reg);
7147 __ Addiu(temp_reg, TMP, /* placeholder */ 0x5678);
Vladimir Marko65979462017-05-19 17:25:12 +01007148 __ SetReorder(reordering);
7149 break;
7150 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007151 case HInvokeStaticOrDirect::MethodLoadKind::kDirectAddress:
7152 __ LoadConst32(temp.AsRegister<Register>(), invoke->GetMethodAddress());
7153 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007154 case HInvokeStaticOrDirect::MethodLoadKind::kBssEntry: {
7155 PcRelativePatchInfo* info = NewMethodBssEntryPatch(
7156 MethodReference(&GetGraph()->GetDexFile(), invoke->GetDexMethodIndex()));
7157 Register temp_reg = temp.AsRegister<Register>();
7158 bool reordering = __ SetReorder(false);
7159 EmitPcRelativeAddressPlaceholderHigh(info, TMP, base_reg);
7160 __ Lw(temp_reg, TMP, /* placeholder */ 0x5678);
7161 __ SetReorder(reordering);
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007162 break;
Vladimir Marko0eb882b2017-05-15 13:39:18 +01007163 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007164 case HInvokeStaticOrDirect::MethodLoadKind::kRuntimeCall: {
7165 GenerateInvokeStaticOrDirectRuntimeCall(invoke, temp, slow_path);
7166 return; // No code pointer retrieval; the runtime performs the call directly.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007167 }
7168 }
7169
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007170 switch (code_ptr_location) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007171 case HInvokeStaticOrDirect::CodePtrLocation::kCallSelf:
Alexey Frunzee3fb2452016-05-10 16:08:05 -07007172 __ Bal(&frame_entry_label_);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007173 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007174 case HInvokeStaticOrDirect::CodePtrLocation::kCallArtMethod:
7175 // T9 = callee_method->entry_point_from_quick_compiled_code_;
Goran Jakovljevic1a878372015-10-26 14:28:52 +01007176 __ LoadFromOffset(kLoadWord,
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007177 T9,
7178 callee_method.AsRegister<Register>(),
7179 ArtMethod::EntryPointFromQuickCompiledCodeOffset(
Andreas Gampe542451c2016-07-26 09:02:02 -07007180 kMipsPointerSize).Int32Value());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007181 // T9()
7182 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007183 __ NopIfNoReordering();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007184 break;
7185 }
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007186 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
7187
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007188 DCHECK(!IsLeafMethod());
7189}
7190
7191void InstructionCodeGeneratorMIPS::VisitInvokeStaticOrDirect(HInvokeStaticOrDirect* invoke) {
David Brazdil58282f42016-01-14 12:45:10 +00007192 // Explicit clinit checks triggered by static invokes must have been pruned by
7193 // art::PrepareForRegisterAllocation.
7194 DCHECK(!invoke->IsStaticWithExplicitClinitCheck());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007195
7196 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7197 return;
7198 }
7199
7200 LocationSummary* locations = invoke->GetLocations();
7201 codegen_->GenerateStaticOrDirectCall(invoke,
7202 locations->HasTemps()
7203 ? locations->GetTemp(0)
7204 : Location::NoLocation());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007205}
7206
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007207void CodeGeneratorMIPS::GenerateVirtualCall(
7208 HInvokeVirtual* invoke, Location temp_location, SlowPathCode* slow_path) {
Goran Jakovljevice919b072016-10-04 10:17:34 +02007209 // Use the calling convention instead of the location of the receiver, as
7210 // intrinsics may have put the receiver in a different register. In the intrinsics
7211 // slow path, the arguments have been moved to the right place, so here we are
7212 // guaranteed that the receiver is the first register of the calling convention.
7213 InvokeDexCallingConvention calling_convention;
7214 Register receiver = calling_convention.GetRegisterAt(0);
7215
Chris Larsen3acee732015-11-18 13:31:08 -08007216 Register temp = temp_location.AsRegister<Register>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007217 size_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
7218 invoke->GetVTableIndex(), kMipsPointerSize).SizeValue();
7219 uint32_t class_offset = mirror::Object::ClassOffset().Int32Value();
Andreas Gampe542451c2016-07-26 09:02:02 -07007220 Offset entry_point = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007221
7222 // temp = object->GetClass();
Goran Jakovljevice919b072016-10-04 10:17:34 +02007223 __ LoadFromOffset(kLoadWord, temp, receiver, class_offset);
Chris Larsen3acee732015-11-18 13:31:08 -08007224 MaybeRecordImplicitNullCheck(invoke);
Alexey Frunzec061de12017-02-14 13:27:23 -08007225 // Instead of simply (possibly) unpoisoning `temp` here, we should
7226 // emit a read barrier for the previous class reference load.
7227 // However this is not required in practice, as this is an
7228 // intermediate/temporary reference and because the current
7229 // concurrent copying collector keeps the from-space memory
7230 // intact/accessible until the end of the marking phase (the
7231 // concurrent copying collector may not in the future).
7232 __ MaybeUnpoisonHeapReference(temp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007233 // temp = temp->GetMethodAt(method_offset);
7234 __ LoadFromOffset(kLoadWord, temp, temp, method_offset);
7235 // T9 = temp->GetEntryPoint();
7236 __ LoadFromOffset(kLoadWord, T9, temp, entry_point.Int32Value());
7237 // T9();
7238 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007239 __ NopIfNoReordering();
Vladimir Markoe7197bf2017-06-02 17:00:23 +01007240 RecordPcInfo(invoke, invoke->GetDexPc(), slow_path);
Chris Larsen3acee732015-11-18 13:31:08 -08007241}
7242
7243void InstructionCodeGeneratorMIPS::VisitInvokeVirtual(HInvokeVirtual* invoke) {
7244 if (TryGenerateIntrinsicCode(invoke, codegen_)) {
7245 return;
7246 }
7247
7248 codegen_->GenerateVirtualCall(invoke, invoke->GetLocations()->GetTemp(0));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007249 DCHECK(!codegen_->IsLeafMethod());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007250}
7251
7252void LocationsBuilderMIPS::VisitLoadClass(HLoadClass* cls) {
Vladimir Marko41559982017-01-06 14:04:23 +00007253 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007254 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007255 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007256 Location loc = Location::RegisterLocation(calling_convention.GetRegisterAt(0));
7257 CodeGenerator::CreateLoadClassRuntimeCallLocationSummary(cls, loc, loc);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007258 return;
7259 }
Vladimir Marko41559982017-01-06 14:04:23 +00007260 DCHECK(!cls->NeedsAccessCheck());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007261 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze15958152017-02-09 19:08:30 -08007262 const bool requires_read_barrier = kEmitCompilerReadBarrier && !cls->IsInBootImage();
7263 LocationSummary::CallKind call_kind = (cls->NeedsEnvironment() || requires_read_barrier)
Alexey Frunze06a46c42016-07-19 15:00:40 -07007264 ? LocationSummary::kCallOnSlowPath
7265 : LocationSummary::kNoCall;
7266 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(cls, call_kind);
Alexey Frunzec61c0762017-04-10 13:54:23 -07007267 if (kUseBakerReadBarrier && requires_read_barrier && !cls->NeedsEnvironment()) {
7268 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
7269 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007270 switch (load_kind) {
7271 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007272 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007273 case HLoadClass::LoadKind::kBootImageAddress:
7274 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007275 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007276 break;
7277 }
7278 FALLTHROUGH_INTENDED;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007279 case HLoadClass::LoadKind::kReferrersClass:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007280 locations->SetInAt(0, Location::RequiresRegister());
7281 break;
7282 default:
7283 break;
7284 }
7285 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007286 if (load_kind == HLoadClass::LoadKind::kBssEntry) {
7287 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7288 // Rely on the type resolution or initialization and marking to save everything we need.
7289 // Request a temp to hold the BSS entry location for the slow path on R2
7290 // (no benefit for R6).
7291 if (!isR6) {
7292 locations->AddTemp(Location::RequiresRegister());
7293 }
7294 RegisterSet caller_saves = RegisterSet::Empty();
7295 InvokeRuntimeCallingConvention calling_convention;
7296 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7297 locations->SetCustomSlowPathCallerSaves(caller_saves);
7298 } else {
7299 // For non-Baker read barriers we have a temp-clobbering call.
7300 }
7301 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007302}
7303
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007304// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7305// move.
7306void InstructionCodeGeneratorMIPS::VisitLoadClass(HLoadClass* cls) NO_THREAD_SAFETY_ANALYSIS {
Vladimir Marko41559982017-01-06 14:04:23 +00007307 HLoadClass::LoadKind load_kind = cls->GetLoadKind();
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007308 if (load_kind == HLoadClass::LoadKind::kRuntimeCall) {
Vladimir Marko41559982017-01-06 14:04:23 +00007309 codegen_->GenerateLoadClassRuntimeCall(cls);
Pavle Batutae87a7182015-10-28 13:10:42 +01007310 return;
7311 }
Vladimir Marko41559982017-01-06 14:04:23 +00007312 DCHECK(!cls->NeedsAccessCheck());
Pavle Batutae87a7182015-10-28 13:10:42 +01007313
Vladimir Marko41559982017-01-06 14:04:23 +00007314 LocationSummary* locations = cls->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007315 Location out_loc = locations->Out();
7316 Register out = out_loc.AsRegister<Register>();
7317 Register base_or_current_method_reg;
7318 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7319 switch (load_kind) {
7320 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007321 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007322 case HLoadClass::LoadKind::kBootImageAddress:
7323 case HLoadClass::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007324 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7325 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007326 case HLoadClass::LoadKind::kReferrersClass:
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007327 case HLoadClass::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007328 base_or_current_method_reg = locations->InAt(0).AsRegister<Register>();
7329 break;
7330 default:
7331 base_or_current_method_reg = ZERO;
7332 break;
7333 }
Nicolas Geoffray42e372e2015-11-24 15:48:56 +00007334
Alexey Frunze15958152017-02-09 19:08:30 -08007335 const ReadBarrierOption read_barrier_option = cls->IsInBootImage()
7336 ? kWithoutReadBarrier
7337 : kCompilerReadBarrierOption;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007338 bool generate_null_check = false;
7339 switch (load_kind) {
7340 case HLoadClass::LoadKind::kReferrersClass: {
7341 DCHECK(!cls->CanCallRuntime());
7342 DCHECK(!cls->MustGenerateClinitCheck());
7343 // /* GcRoot<mirror::Class> */ out = current_method->declaring_class_
7344 GenerateGcRootFieldLoad(cls,
7345 out_loc,
7346 base_or_current_method_reg,
Alexey Frunze15958152017-02-09 19:08:30 -08007347 ArtMethod::DeclaringClassOffset().Int32Value(),
7348 read_barrier_option);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007349 break;
7350 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007351 case HLoadClass::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007352 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze15958152017-02-09 19:08:30 -08007353 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007354 CodeGeneratorMIPS::PcRelativePatchInfo* info =
7355 codegen_->NewPcRelativeTypePatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007356 bool reordering = __ SetReorder(false);
7357 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7358 __ Addiu(out, out, /* placeholder */ 0x5678);
7359 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007360 break;
7361 }
7362 case HLoadClass::LoadKind::kBootImageAddress: {
Alexey Frunze15958152017-02-09 19:08:30 -08007363 DCHECK_EQ(read_barrier_option, kWithoutReadBarrier);
Nicolas Geoffray5247c082017-01-13 14:17:29 +00007364 uint32_t address = dchecked_integral_cast<uint32_t>(
7365 reinterpret_cast<uintptr_t>(cls->GetClass().Get()));
7366 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007367 __ LoadLiteral(out,
7368 base_or_current_method_reg,
7369 codegen_->DeduplicateBootImageAddressLiteral(address));
7370 break;
7371 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007372 case HLoadClass::LoadKind::kBssEntry: {
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007373 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko1998cd02017-01-13 13:02:58 +00007374 codegen_->NewTypeBssEntryPatch(cls->GetDexFile(), cls->GetTypeIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007375 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7376 if (isR6 || non_baker_read_barrier) {
7377 bool reordering = __ SetReorder(false);
7378 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7379 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
7380 __ SetReorder(reordering);
7381 } else {
7382 // On R2 save the BSS entry address in a temporary register instead of
7383 // recalculating it in the slow path.
7384 Register temp = locations->GetTemp(0).AsRegister<Register>();
7385 bool reordering = __ SetReorder(false);
7386 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7387 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7388 __ SetReorder(reordering);
7389 GenerateGcRootFieldLoad(cls, out_loc, temp, /* offset */ 0, read_barrier_option);
7390 }
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007391 generate_null_check = true;
7392 break;
7393 }
Nicolas Geoffray22384ae2016-12-12 22:33:36 +00007394 case HLoadClass::LoadKind::kJitTableAddress: {
Alexey Frunze627c1a02017-01-30 19:28:14 -08007395 CodeGeneratorMIPS::JitPatchInfo* info = codegen_->NewJitRootClassPatch(cls->GetDexFile(),
7396 cls->GetTypeIndex(),
7397 cls->GetClass());
7398 bool reordering = __ SetReorder(false);
7399 __ Bind(&info->high_label);
7400 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007401 GenerateGcRootFieldLoad(cls, out_loc, out, /* placeholder */ 0x5678, read_barrier_option);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007402 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007403 break;
7404 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007405 case HLoadClass::LoadKind::kRuntimeCall:
Nicolas Geoffray83c8e272017-01-31 14:36:37 +00007406 case HLoadClass::LoadKind::kInvalid:
Vladimir Marko41559982017-01-06 14:04:23 +00007407 LOG(FATAL) << "UNREACHABLE";
7408 UNREACHABLE();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007409 }
7410
7411 if (generate_null_check || cls->MustGenerateClinitCheck()) {
7412 DCHECK(cls->CanCallRuntime());
7413 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadClassSlowPathMIPS(
7414 cls, cls, cls->GetDexPc(), cls->MustGenerateClinitCheck());
7415 codegen_->AddSlowPath(slow_path);
7416 if (generate_null_check) {
7417 __ Beqz(out, slow_path->GetEntryLabel());
7418 }
7419 if (cls->MustGenerateClinitCheck()) {
7420 GenerateClassInitializationCheck(slow_path, out);
7421 } else {
7422 __ Bind(slow_path->GetExitLabel());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007423 }
7424 }
7425}
7426
7427static int32_t GetExceptionTlsOffset() {
Andreas Gampe542451c2016-07-26 09:02:02 -07007428 return Thread::ExceptionOffset<kMipsPointerSize>().Int32Value();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007429}
7430
7431void LocationsBuilderMIPS::VisitLoadException(HLoadException* load) {
7432 LocationSummary* locations =
7433 new (GetGraph()->GetArena()) LocationSummary(load, LocationSummary::kNoCall);
7434 locations->SetOut(Location::RequiresRegister());
7435}
7436
7437void InstructionCodeGeneratorMIPS::VisitLoadException(HLoadException* load) {
7438 Register out = load->GetLocations()->Out().AsRegister<Register>();
7439 __ LoadFromOffset(kLoadWord, out, TR, GetExceptionTlsOffset());
7440}
7441
7442void LocationsBuilderMIPS::VisitClearException(HClearException* clear) {
7443 new (GetGraph()->GetArena()) LocationSummary(clear, LocationSummary::kNoCall);
7444}
7445
7446void InstructionCodeGeneratorMIPS::VisitClearException(HClearException* clear ATTRIBUTE_UNUSED) {
7447 __ StoreToOffset(kStoreWord, ZERO, TR, GetExceptionTlsOffset());
7448}
7449
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007450void LocationsBuilderMIPS::VisitLoadString(HLoadString* load) {
Alexey Frunzef63f5692016-12-13 17:43:11 -08007451 LocationSummary::CallKind call_kind = CodeGenerator::GetLoadStringCallKind(load);
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007452 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(load, call_kind);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007453 HLoadString::LoadKind load_kind = load->GetLoadKind();
Alexey Frunzec61c0762017-04-10 13:54:23 -07007454 const bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007455 switch (load_kind) {
7456 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007457 case HLoadString::LoadKind::kBootImageAddress:
7458 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007459 case HLoadString::LoadKind::kBssEntry:
Alexey Frunzec61c0762017-04-10 13:54:23 -07007460 if (isR6) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007461 break;
7462 }
7463 FALLTHROUGH_INTENDED;
7464 // We need an extra register for PC-relative dex cache accesses.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007465 case HLoadString::LoadKind::kRuntimeCall:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007466 locations->SetInAt(0, Location::RequiresRegister());
7467 break;
7468 default:
7469 break;
7470 }
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007471 if (load_kind == HLoadString::LoadKind::kRuntimeCall) {
Alexey Frunzebb51df82016-11-01 16:07:32 -07007472 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007473 locations->SetOut(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
Alexey Frunzebb51df82016-11-01 16:07:32 -07007474 } else {
7475 locations->SetOut(Location::RequiresRegister());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007476 if (load_kind == HLoadString::LoadKind::kBssEntry) {
7477 if (!kUseReadBarrier || kUseBakerReadBarrier) {
7478 // Rely on the pResolveString and marking to save everything we need.
7479 // Request a temp to hold the BSS entry location for the slow path on R2
7480 // (no benefit for R6).
7481 if (!isR6) {
7482 locations->AddTemp(Location::RequiresRegister());
7483 }
7484 RegisterSet caller_saves = RegisterSet::Empty();
7485 InvokeRuntimeCallingConvention calling_convention;
7486 caller_saves.Add(Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7487 locations->SetCustomSlowPathCallerSaves(caller_saves);
7488 } else {
7489 // For non-Baker read barriers we have a temp-clobbering call.
7490 }
7491 }
Alexey Frunzebb51df82016-11-01 16:07:32 -07007492 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007493}
7494
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007495// NO_THREAD_SAFETY_ANALYSIS as we manipulate handles whose internal object we know does not
7496// move.
7497void InstructionCodeGeneratorMIPS::VisitLoadString(HLoadString* load) NO_THREAD_SAFETY_ANALYSIS {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007498 HLoadString::LoadKind load_kind = load->GetLoadKind();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007499 LocationSummary* locations = load->GetLocations();
Alexey Frunze06a46c42016-07-19 15:00:40 -07007500 Location out_loc = locations->Out();
7501 Register out = out_loc.AsRegister<Register>();
7502 Register base_or_current_method_reg;
7503 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7504 switch (load_kind) {
7505 // We need an extra register for PC-relative literals on R2.
Alexey Frunze06a46c42016-07-19 15:00:40 -07007506 case HLoadString::LoadKind::kBootImageAddress:
7507 case HLoadString::LoadKind::kBootImageLinkTimePcRelative:
Vladimir Markoaad75c62016-10-03 08:46:48 +00007508 case HLoadString::LoadKind::kBssEntry:
Alexey Frunze06a46c42016-07-19 15:00:40 -07007509 base_or_current_method_reg = isR6 ? ZERO : locations->InAt(0).AsRegister<Register>();
7510 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007511 default:
7512 base_or_current_method_reg = ZERO;
7513 break;
7514 }
7515
7516 switch (load_kind) {
Alexey Frunze06a46c42016-07-19 15:00:40 -07007517 case HLoadString::LoadKind::kBootImageLinkTimePcRelative: {
Vladimir Markoaad75c62016-10-03 08:46:48 +00007518 DCHECK(codegen_->GetCompilerOptions().IsBootImage());
Alexey Frunze06a46c42016-07-19 15:00:40 -07007519 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007520 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunze6b892cd2017-01-03 17:11:38 -08007521 bool reordering = __ SetReorder(false);
7522 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7523 __ Addiu(out, out, /* placeholder */ 0x5678);
7524 __ SetReorder(reordering);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007525 return; // No dex cache slow path.
7526 }
7527 case HLoadString::LoadKind::kBootImageAddress: {
Nicolas Geoffrayf0acfe72017-01-09 20:54:52 +00007528 uint32_t address = dchecked_integral_cast<uint32_t>(
7529 reinterpret_cast<uintptr_t>(load->GetString().Get()));
7530 DCHECK_NE(address, 0u);
Alexey Frunze06a46c42016-07-19 15:00:40 -07007531 __ LoadLiteral(out,
7532 base_or_current_method_reg,
7533 codegen_->DeduplicateBootImageAddressLiteral(address));
7534 return; // No dex cache slow path.
7535 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007536 case HLoadString::LoadKind::kBssEntry: {
7537 DCHECK(!codegen_->GetCompilerOptions().IsBootImage());
7538 CodeGeneratorMIPS::PcRelativePatchInfo* info =
Vladimir Marko6bec91c2017-01-09 15:03:12 +00007539 codegen_->NewPcRelativeStringPatch(load->GetDexFile(), load->GetStringIndex());
Alexey Frunzec61c0762017-04-10 13:54:23 -07007540 constexpr bool non_baker_read_barrier = kUseReadBarrier && !kUseBakerReadBarrier;
7541 if (isR6 || non_baker_read_barrier) {
7542 bool reordering = __ SetReorder(false);
7543 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, out, base_or_current_method_reg);
7544 GenerateGcRootFieldLoad(load,
7545 out_loc,
7546 out,
7547 /* placeholder */ 0x5678,
7548 kCompilerReadBarrierOption);
7549 __ SetReorder(reordering);
7550 } else {
7551 // On R2 save the BSS entry address in a temporary register instead of
7552 // recalculating it in the slow path.
7553 Register temp = locations->GetTemp(0).AsRegister<Register>();
7554 bool reordering = __ SetReorder(false);
7555 codegen_->EmitPcRelativeAddressPlaceholderHigh(info, temp, base_or_current_method_reg);
7556 __ Addiu(temp, temp, /* placeholder */ 0x5678);
7557 __ SetReorder(reordering);
7558 GenerateGcRootFieldLoad(load, out_loc, temp, /* offset */ 0, kCompilerReadBarrierOption);
7559 }
Vladimir Markoaad75c62016-10-03 08:46:48 +00007560 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) LoadStringSlowPathMIPS(load);
7561 codegen_->AddSlowPath(slow_path);
7562 __ Beqz(out, slow_path->GetEntryLabel());
7563 __ Bind(slow_path->GetExitLabel());
7564 return;
7565 }
Alexey Frunze627c1a02017-01-30 19:28:14 -08007566 case HLoadString::LoadKind::kJitTableAddress: {
7567 CodeGeneratorMIPS::JitPatchInfo* info =
7568 codegen_->NewJitRootStringPatch(load->GetDexFile(),
7569 load->GetStringIndex(),
7570 load->GetString());
7571 bool reordering = __ SetReorder(false);
7572 __ Bind(&info->high_label);
7573 __ Lui(out, /* placeholder */ 0x1234);
Alexey Frunze15958152017-02-09 19:08:30 -08007574 GenerateGcRootFieldLoad(load,
7575 out_loc,
7576 out,
7577 /* placeholder */ 0x5678,
7578 kCompilerReadBarrierOption);
Alexey Frunze627c1a02017-01-30 19:28:14 -08007579 __ SetReorder(reordering);
7580 return;
7581 }
Alexey Frunze06a46c42016-07-19 15:00:40 -07007582 default:
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007583 break;
Alexey Frunze06a46c42016-07-19 15:00:40 -07007584 }
Nicolas Geoffray917d0162015-11-24 18:25:35 +00007585
Christina Wadsworthbf44e0e2016-08-18 10:37:42 -07007586 // TODO: Re-add the compiler code to do string dex cache lookup again.
Vladimir Marko847e6ce2017-06-02 13:55:07 +01007587 DCHECK(load_kind == HLoadString::LoadKind::kRuntimeCall);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007588 InvokeRuntimeCallingConvention calling_convention;
Alexey Frunzec61c0762017-04-10 13:54:23 -07007589 DCHECK_EQ(calling_convention.GetRegisterAt(0), out);
Andreas Gampe8a0128a2016-11-28 07:38:35 -08007590 __ LoadConst32(calling_convention.GetRegisterAt(0), load->GetStringIndex().index_);
Vladimir Markoaad75c62016-10-03 08:46:48 +00007591 codegen_->InvokeRuntime(kQuickResolveString, load, load->GetDexPc());
7592 CheckEntrypointTypes<kQuickResolveString, void*, uint32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007593}
7594
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007595void LocationsBuilderMIPS::VisitLongConstant(HLongConstant* constant) {
7596 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(constant);
7597 locations->SetOut(Location::ConstantLocation(constant));
7598}
7599
7600void InstructionCodeGeneratorMIPS::VisitLongConstant(HLongConstant* constant ATTRIBUTE_UNUSED) {
7601 // Will be generated at use site.
7602}
7603
7604void LocationsBuilderMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7605 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007606 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007607 InvokeRuntimeCallingConvention calling_convention;
7608 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7609}
7610
7611void InstructionCodeGeneratorMIPS::VisitMonitorOperation(HMonitorOperation* instruction) {
7612 if (instruction->IsEnter()) {
Serban Constantinescufca16662016-07-14 09:21:59 +01007613 codegen_->InvokeRuntime(kQuickLockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007614 CheckEntrypointTypes<kQuickLockObject, void, mirror::Object*>();
7615 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007616 codegen_->InvokeRuntime(kQuickUnlockObject, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007617 }
7618 CheckEntrypointTypes<kQuickUnlockObject, void, mirror::Object*>();
7619}
7620
7621void LocationsBuilderMIPS::VisitMul(HMul* mul) {
7622 LocationSummary* locations =
7623 new (GetGraph()->GetArena()) LocationSummary(mul, LocationSummary::kNoCall);
7624 switch (mul->GetResultType()) {
7625 case Primitive::kPrimInt:
7626 case Primitive::kPrimLong:
7627 locations->SetInAt(0, Location::RequiresRegister());
7628 locations->SetInAt(1, Location::RequiresRegister());
7629 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7630 break;
7631
7632 case Primitive::kPrimFloat:
7633 case Primitive::kPrimDouble:
7634 locations->SetInAt(0, Location::RequiresFpuRegister());
7635 locations->SetInAt(1, Location::RequiresFpuRegister());
7636 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7637 break;
7638
7639 default:
7640 LOG(FATAL) << "Unexpected mul type " << mul->GetResultType();
7641 }
7642}
7643
7644void InstructionCodeGeneratorMIPS::VisitMul(HMul* instruction) {
7645 Primitive::Type type = instruction->GetType();
7646 LocationSummary* locations = instruction->GetLocations();
7647 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
7648
7649 switch (type) {
7650 case Primitive::kPrimInt: {
7651 Register dst = locations->Out().AsRegister<Register>();
7652 Register lhs = locations->InAt(0).AsRegister<Register>();
7653 Register rhs = locations->InAt(1).AsRegister<Register>();
7654
7655 if (isR6) {
7656 __ MulR6(dst, lhs, rhs);
7657 } else {
7658 __ MulR2(dst, lhs, rhs);
7659 }
7660 break;
7661 }
7662 case Primitive::kPrimLong: {
7663 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7664 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7665 Register lhs_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7666 Register lhs_low = locations->InAt(0).AsRegisterPairLow<Register>();
7667 Register rhs_high = locations->InAt(1).AsRegisterPairHigh<Register>();
7668 Register rhs_low = locations->InAt(1).AsRegisterPairLow<Register>();
7669
7670 // Extra checks to protect caused by the existance of A1_A2.
7671 // The algorithm is wrong if dst_high is either lhs_lo or rhs_lo:
7672 // (e.g. lhs=a0_a1, rhs=a2_a3 and dst=a1_a2).
7673 DCHECK_NE(dst_high, lhs_low);
7674 DCHECK_NE(dst_high, rhs_low);
7675
7676 // A_B * C_D
7677 // dst_hi: [ low(A*D) + low(B*C) + hi(B*D) ]
7678 // dst_lo: [ low(B*D) ]
7679 // Note: R2 and R6 MUL produce the low 32 bit of the multiplication result.
7680
7681 if (isR6) {
7682 __ MulR6(TMP, lhs_high, rhs_low);
7683 __ MulR6(dst_high, lhs_low, rhs_high);
7684 __ Addu(dst_high, dst_high, TMP);
7685 __ MuhuR6(TMP, lhs_low, rhs_low);
7686 __ Addu(dst_high, dst_high, TMP);
7687 __ MulR6(dst_low, lhs_low, rhs_low);
7688 } else {
7689 __ MulR2(TMP, lhs_high, rhs_low);
7690 __ MulR2(dst_high, lhs_low, rhs_high);
7691 __ Addu(dst_high, dst_high, TMP);
7692 __ MultuR2(lhs_low, rhs_low);
7693 __ Mfhi(TMP);
7694 __ Addu(dst_high, dst_high, TMP);
7695 __ Mflo(dst_low);
7696 }
7697 break;
7698 }
7699 case Primitive::kPrimFloat:
7700 case Primitive::kPrimDouble: {
7701 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7702 FRegister lhs = locations->InAt(0).AsFpuRegister<FRegister>();
7703 FRegister rhs = locations->InAt(1).AsFpuRegister<FRegister>();
7704 if (type == Primitive::kPrimFloat) {
7705 __ MulS(dst, lhs, rhs);
7706 } else {
7707 __ MulD(dst, lhs, rhs);
7708 }
7709 break;
7710 }
7711 default:
7712 LOG(FATAL) << "Unexpected mul type " << type;
7713 }
7714}
7715
7716void LocationsBuilderMIPS::VisitNeg(HNeg* neg) {
7717 LocationSummary* locations =
7718 new (GetGraph()->GetArena()) LocationSummary(neg, LocationSummary::kNoCall);
7719 switch (neg->GetResultType()) {
7720 case Primitive::kPrimInt:
7721 case Primitive::kPrimLong:
7722 locations->SetInAt(0, Location::RequiresRegister());
7723 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7724 break;
7725
7726 case Primitive::kPrimFloat:
7727 case Primitive::kPrimDouble:
7728 locations->SetInAt(0, Location::RequiresFpuRegister());
7729 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
7730 break;
7731
7732 default:
7733 LOG(FATAL) << "Unexpected neg type " << neg->GetResultType();
7734 }
7735}
7736
7737void InstructionCodeGeneratorMIPS::VisitNeg(HNeg* instruction) {
7738 Primitive::Type type = instruction->GetType();
7739 LocationSummary* locations = instruction->GetLocations();
7740
7741 switch (type) {
7742 case Primitive::kPrimInt: {
7743 Register dst = locations->Out().AsRegister<Register>();
7744 Register src = locations->InAt(0).AsRegister<Register>();
7745 __ Subu(dst, ZERO, src);
7746 break;
7747 }
7748 case Primitive::kPrimLong: {
7749 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7750 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7751 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7752 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7753 __ Subu(dst_low, ZERO, src_low);
7754 __ Sltu(TMP, ZERO, dst_low);
7755 __ Subu(dst_high, ZERO, src_high);
7756 __ Subu(dst_high, dst_high, TMP);
7757 break;
7758 }
7759 case Primitive::kPrimFloat:
7760 case Primitive::kPrimDouble: {
7761 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
7762 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
7763 if (type == Primitive::kPrimFloat) {
7764 __ NegS(dst, src);
7765 } else {
7766 __ NegD(dst, src);
7767 }
7768 break;
7769 }
7770 default:
7771 LOG(FATAL) << "Unexpected neg type " << type;
7772 }
7773}
7774
7775void LocationsBuilderMIPS::VisitNewArray(HNewArray* instruction) {
7776 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007777 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007778 InvokeRuntimeCallingConvention calling_convention;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007779 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007780 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
7781 locations->SetInAt(1, Location::RegisterLocation(calling_convention.GetRegisterAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007782}
7783
7784void InstructionCodeGeneratorMIPS::VisitNewArray(HNewArray* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007785 // Note: if heap poisoning is enabled, the entry point takes care
7786 // of poisoning the reference.
Nicolas Geoffraye761bcc2017-01-19 08:59:37 +00007787 codegen_->InvokeRuntime(kQuickAllocArrayResolved, instruction, instruction->GetDexPc());
7788 CheckEntrypointTypes<kQuickAllocArrayResolved, void*, mirror::Class*, int32_t>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007789}
7790
7791void LocationsBuilderMIPS::VisitNewInstance(HNewInstance* instruction) {
7792 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007793 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007794 InvokeRuntimeCallingConvention calling_convention;
David Brazdil6de19382016-01-08 17:37:10 +00007795 if (instruction->IsStringAlloc()) {
7796 locations->AddTemp(Location::RegisterLocation(kMethodRegisterArgument));
7797 } else {
7798 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
David Brazdil6de19382016-01-08 17:37:10 +00007799 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007800 locations->SetOut(calling_convention.GetReturnLocation(Primitive::kPrimNot));
7801}
7802
7803void InstructionCodeGeneratorMIPS::VisitNewInstance(HNewInstance* instruction) {
Alexey Frunzec061de12017-02-14 13:27:23 -08007804 // Note: if heap poisoning is enabled, the entry point takes care
7805 // of poisoning the reference.
David Brazdil6de19382016-01-08 17:37:10 +00007806 if (instruction->IsStringAlloc()) {
7807 // String is allocated through StringFactory. Call NewEmptyString entry point.
7808 Register temp = instruction->GetLocations()->GetTemp(0).AsRegister<Register>();
Andreas Gampe542451c2016-07-26 09:02:02 -07007809 MemberOffset code_offset = ArtMethod::EntryPointFromQuickCompiledCodeOffset(kMipsPointerSize);
David Brazdil6de19382016-01-08 17:37:10 +00007810 __ LoadFromOffset(kLoadWord, temp, TR, QUICK_ENTRY_POINT(pNewEmptyString));
7811 __ LoadFromOffset(kLoadWord, T9, temp, code_offset.Int32Value());
7812 __ Jalr(T9);
Alexey Frunze57eb0f52016-07-29 22:04:46 -07007813 __ NopIfNoReordering();
David Brazdil6de19382016-01-08 17:37:10 +00007814 codegen_->RecordPcInfo(instruction, instruction->GetDexPc());
7815 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01007816 codegen_->InvokeRuntime(instruction->GetEntrypoint(), instruction, instruction->GetDexPc());
Nicolas Geoffray0d3998b2017-01-12 15:35:12 +00007817 CheckEntrypointTypes<kQuickAllocObjectWithChecks, void*, mirror::Class*>();
David Brazdil6de19382016-01-08 17:37:10 +00007818 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007819}
7820
7821void LocationsBuilderMIPS::VisitNot(HNot* instruction) {
7822 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7823 locations->SetInAt(0, Location::RequiresRegister());
7824 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7825}
7826
7827void InstructionCodeGeneratorMIPS::VisitNot(HNot* instruction) {
7828 Primitive::Type type = instruction->GetType();
7829 LocationSummary* locations = instruction->GetLocations();
7830
7831 switch (type) {
7832 case Primitive::kPrimInt: {
7833 Register dst = locations->Out().AsRegister<Register>();
7834 Register src = locations->InAt(0).AsRegister<Register>();
7835 __ Nor(dst, src, ZERO);
7836 break;
7837 }
7838
7839 case Primitive::kPrimLong: {
7840 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
7841 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
7842 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
7843 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
7844 __ Nor(dst_high, src_high, ZERO);
7845 __ Nor(dst_low, src_low, ZERO);
7846 break;
7847 }
7848
7849 default:
7850 LOG(FATAL) << "Unexpected type for not operation " << instruction->GetResultType();
7851 }
7852}
7853
7854void LocationsBuilderMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7855 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7856 locations->SetInAt(0, Location::RequiresRegister());
7857 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7858}
7859
7860void InstructionCodeGeneratorMIPS::VisitBooleanNot(HBooleanNot* instruction) {
7861 LocationSummary* locations = instruction->GetLocations();
7862 __ Xori(locations->Out().AsRegister<Register>(),
7863 locations->InAt(0).AsRegister<Register>(),
7864 1);
7865}
7866
7867void LocationsBuilderMIPS::VisitNullCheck(HNullCheck* instruction) {
Vladimir Marko804b03f2016-09-14 16:26:36 +01007868 LocationSummary* locations = codegen_->CreateThrowingSlowPathLocations(instruction);
7869 locations->SetInAt(0, Location::RequiresRegister());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007870}
7871
Calin Juravle2ae48182016-03-16 14:05:09 +00007872void CodeGeneratorMIPS::GenerateImplicitNullCheck(HNullCheck* instruction) {
7873 if (CanMoveNullCheckToUser(instruction)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007874 return;
7875 }
7876 Location obj = instruction->GetLocations()->InAt(0);
7877
7878 __ Lw(ZERO, obj.AsRegister<Register>(), 0);
Calin Juravle2ae48182016-03-16 14:05:09 +00007879 RecordPcInfo(instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007880}
7881
Calin Juravle2ae48182016-03-16 14:05:09 +00007882void CodeGeneratorMIPS::GenerateExplicitNullCheck(HNullCheck* instruction) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007883 SlowPathCodeMIPS* slow_path = new (GetGraph()->GetArena()) NullCheckSlowPathMIPS(instruction);
Calin Juravle2ae48182016-03-16 14:05:09 +00007884 AddSlowPath(slow_path);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007885
7886 Location obj = instruction->GetLocations()->InAt(0);
7887
7888 __ Beqz(obj.AsRegister<Register>(), slow_path->GetEntryLabel());
7889}
7890
7891void InstructionCodeGeneratorMIPS::VisitNullCheck(HNullCheck* instruction) {
Calin Juravle2ae48182016-03-16 14:05:09 +00007892 codegen_->GenerateNullCheck(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007893}
7894
7895void LocationsBuilderMIPS::VisitOr(HOr* instruction) {
7896 HandleBinaryOp(instruction);
7897}
7898
7899void InstructionCodeGeneratorMIPS::VisitOr(HOr* instruction) {
7900 HandleBinaryOp(instruction);
7901}
7902
7903void LocationsBuilderMIPS::VisitParallelMove(HParallelMove* instruction ATTRIBUTE_UNUSED) {
7904 LOG(FATAL) << "Unreachable";
7905}
7906
7907void InstructionCodeGeneratorMIPS::VisitParallelMove(HParallelMove* instruction) {
7908 codegen_->GetMoveResolver()->EmitNativeCode(instruction);
7909}
7910
7911void LocationsBuilderMIPS::VisitParameterValue(HParameterValue* instruction) {
7912 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
7913 Location location = parameter_visitor_.GetNextLocation(instruction->GetType());
7914 if (location.IsStackSlot()) {
7915 location = Location::StackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7916 } else if (location.IsDoubleStackSlot()) {
7917 location = Location::DoubleStackSlot(location.GetStackIndex() + codegen_->GetFrameSize());
7918 }
7919 locations->SetOut(location);
7920}
7921
7922void InstructionCodeGeneratorMIPS::VisitParameterValue(HParameterValue* instruction
7923 ATTRIBUTE_UNUSED) {
7924 // Nothing to do, the parameter is already at its location.
7925}
7926
7927void LocationsBuilderMIPS::VisitCurrentMethod(HCurrentMethod* instruction) {
7928 LocationSummary* locations =
7929 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
7930 locations->SetOut(Location::RegisterLocation(kMethodRegisterArgument));
7931}
7932
7933void InstructionCodeGeneratorMIPS::VisitCurrentMethod(HCurrentMethod* instruction
7934 ATTRIBUTE_UNUSED) {
7935 // Nothing to do, the method is already at its location.
7936}
7937
7938void LocationsBuilderMIPS::VisitPhi(HPhi* instruction) {
7939 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(instruction);
Vladimir Marko372f10e2016-05-17 16:30:10 +01007940 for (size_t i = 0, e = locations->GetInputCount(); i < e; ++i) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007941 locations->SetInAt(i, Location::Any());
7942 }
7943 locations->SetOut(Location::Any());
7944}
7945
7946void InstructionCodeGeneratorMIPS::VisitPhi(HPhi* instruction ATTRIBUTE_UNUSED) {
7947 LOG(FATAL) << "Unreachable";
7948}
7949
7950void LocationsBuilderMIPS::VisitRem(HRem* rem) {
7951 Primitive::Type type = rem->GetResultType();
7952 LocationSummary::CallKind call_kind =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01007953 (type == Primitive::kPrimInt) ? LocationSummary::kNoCall : LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007954 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(rem, call_kind);
7955
7956 switch (type) {
7957 case Primitive::kPrimInt:
7958 locations->SetInAt(0, Location::RequiresRegister());
Alexey Frunze7e99e052015-11-24 19:28:01 -08007959 locations->SetInAt(1, Location::RegisterOrConstant(rem->InputAt(1)));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007960 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
7961 break;
7962
7963 case Primitive::kPrimLong: {
7964 InvokeRuntimeCallingConvention calling_convention;
7965 locations->SetInAt(0, Location::RegisterPairLocation(
7966 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
7967 locations->SetInAt(1, Location::RegisterPairLocation(
7968 calling_convention.GetRegisterAt(2), calling_convention.GetRegisterAt(3)));
7969 locations->SetOut(calling_convention.GetReturnLocation(type));
7970 break;
7971 }
7972
7973 case Primitive::kPrimFloat:
7974 case Primitive::kPrimDouble: {
7975 InvokeRuntimeCallingConvention calling_convention;
7976 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
7977 locations->SetInAt(1, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(1)));
7978 locations->SetOut(calling_convention.GetReturnLocation(type));
7979 break;
7980 }
7981
7982 default:
7983 LOG(FATAL) << "Unexpected rem type " << type;
7984 }
7985}
7986
7987void InstructionCodeGeneratorMIPS::VisitRem(HRem* instruction) {
7988 Primitive::Type type = instruction->GetType();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007989
7990 switch (type) {
Alexey Frunze7e99e052015-11-24 19:28:01 -08007991 case Primitive::kPrimInt:
7992 GenerateDivRemIntegral(instruction);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007993 break;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007994 case Primitive::kPrimLong: {
Serban Constantinescufca16662016-07-14 09:21:59 +01007995 codegen_->InvokeRuntime(kQuickLmod, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02007996 CheckEntrypointTypes<kQuickLmod, int64_t, int64_t, int64_t>();
7997 break;
7998 }
7999 case Primitive::kPrimFloat: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008000 codegen_->InvokeRuntime(kQuickFmodf, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008001 CheckEntrypointTypes<kQuickFmodf, float, float, float>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008002 break;
8003 }
8004 case Primitive::kPrimDouble: {
Serban Constantinescufca16662016-07-14 09:21:59 +01008005 codegen_->InvokeRuntime(kQuickFmod, instruction, instruction->GetDexPc());
Roland Levillain888d0672015-11-23 18:53:50 +00008006 CheckEntrypointTypes<kQuickFmod, double, double, double>();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008007 break;
8008 }
8009 default:
8010 LOG(FATAL) << "Unexpected rem type " << type;
8011 }
8012}
8013
Igor Murashkind01745e2017-04-05 16:40:31 -07008014void LocationsBuilderMIPS::VisitConstructorFence(HConstructorFence* constructor_fence) {
8015 constructor_fence->SetLocations(nullptr);
8016}
8017
8018void InstructionCodeGeneratorMIPS::VisitConstructorFence(
8019 HConstructorFence* constructor_fence ATTRIBUTE_UNUSED) {
8020 GenerateMemoryBarrier(MemBarrierKind::kStoreStore);
8021}
8022
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008023void LocationsBuilderMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8024 memory_barrier->SetLocations(nullptr);
8025}
8026
8027void InstructionCodeGeneratorMIPS::VisitMemoryBarrier(HMemoryBarrier* memory_barrier) {
8028 GenerateMemoryBarrier(memory_barrier->GetBarrierKind());
8029}
8030
8031void LocationsBuilderMIPS::VisitReturn(HReturn* ret) {
8032 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(ret);
8033 Primitive::Type return_type = ret->InputAt(0)->GetType();
8034 locations->SetInAt(0, MipsReturnLocation(return_type));
8035}
8036
8037void InstructionCodeGeneratorMIPS::VisitReturn(HReturn* ret ATTRIBUTE_UNUSED) {
8038 codegen_->GenerateFrameExit();
8039}
8040
8041void LocationsBuilderMIPS::VisitReturnVoid(HReturnVoid* ret) {
8042 ret->SetLocations(nullptr);
8043}
8044
8045void InstructionCodeGeneratorMIPS::VisitReturnVoid(HReturnVoid* ret ATTRIBUTE_UNUSED) {
8046 codegen_->GenerateFrameExit();
8047}
8048
Alexey Frunze92d90602015-12-18 18:16:36 -08008049void LocationsBuilderMIPS::VisitRor(HRor* ror) {
8050 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008051}
8052
Alexey Frunze92d90602015-12-18 18:16:36 -08008053void InstructionCodeGeneratorMIPS::VisitRor(HRor* ror) {
8054 HandleShift(ror);
Scott Wakeling40a04bf2015-12-11 09:50:36 +00008055}
8056
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008057void LocationsBuilderMIPS::VisitShl(HShl* shl) {
8058 HandleShift(shl);
8059}
8060
8061void InstructionCodeGeneratorMIPS::VisitShl(HShl* shl) {
8062 HandleShift(shl);
8063}
8064
8065void LocationsBuilderMIPS::VisitShr(HShr* shr) {
8066 HandleShift(shr);
8067}
8068
8069void InstructionCodeGeneratorMIPS::VisitShr(HShr* shr) {
8070 HandleShift(shr);
8071}
8072
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008073void LocationsBuilderMIPS::VisitSub(HSub* instruction) {
8074 HandleBinaryOp(instruction);
8075}
8076
8077void InstructionCodeGeneratorMIPS::VisitSub(HSub* instruction) {
8078 HandleBinaryOp(instruction);
8079}
8080
8081void LocationsBuilderMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8082 HandleFieldGet(instruction, instruction->GetFieldInfo());
8083}
8084
8085void InstructionCodeGeneratorMIPS::VisitStaticFieldGet(HStaticFieldGet* instruction) {
8086 HandleFieldGet(instruction, instruction->GetFieldInfo(), instruction->GetDexPc());
8087}
8088
8089void LocationsBuilderMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
8090 HandleFieldSet(instruction, instruction->GetFieldInfo());
8091}
8092
8093void InstructionCodeGeneratorMIPS::VisitStaticFieldSet(HStaticFieldSet* instruction) {
Goran Jakovljevice114da22016-12-26 14:21:43 +01008094 HandleFieldSet(instruction,
8095 instruction->GetFieldInfo(),
8096 instruction->GetDexPc(),
8097 instruction->GetValueCanBeNull());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008098}
8099
8100void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldGet(
8101 HUnresolvedInstanceFieldGet* instruction) {
8102 FieldAccessCallingConventionMIPS calling_convention;
8103 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8104 instruction->GetFieldType(),
8105 calling_convention);
8106}
8107
8108void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldGet(
8109 HUnresolvedInstanceFieldGet* instruction) {
8110 FieldAccessCallingConventionMIPS calling_convention;
8111 codegen_->GenerateUnresolvedFieldAccess(instruction,
8112 instruction->GetFieldType(),
8113 instruction->GetFieldIndex(),
8114 instruction->GetDexPc(),
8115 calling_convention);
8116}
8117
8118void LocationsBuilderMIPS::VisitUnresolvedInstanceFieldSet(
8119 HUnresolvedInstanceFieldSet* instruction) {
8120 FieldAccessCallingConventionMIPS calling_convention;
8121 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8122 instruction->GetFieldType(),
8123 calling_convention);
8124}
8125
8126void InstructionCodeGeneratorMIPS::VisitUnresolvedInstanceFieldSet(
8127 HUnresolvedInstanceFieldSet* instruction) {
8128 FieldAccessCallingConventionMIPS calling_convention;
8129 codegen_->GenerateUnresolvedFieldAccess(instruction,
8130 instruction->GetFieldType(),
8131 instruction->GetFieldIndex(),
8132 instruction->GetDexPc(),
8133 calling_convention);
8134}
8135
8136void LocationsBuilderMIPS::VisitUnresolvedStaticFieldGet(
8137 HUnresolvedStaticFieldGet* instruction) {
8138 FieldAccessCallingConventionMIPS calling_convention;
8139 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8140 instruction->GetFieldType(),
8141 calling_convention);
8142}
8143
8144void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldGet(
8145 HUnresolvedStaticFieldGet* instruction) {
8146 FieldAccessCallingConventionMIPS calling_convention;
8147 codegen_->GenerateUnresolvedFieldAccess(instruction,
8148 instruction->GetFieldType(),
8149 instruction->GetFieldIndex(),
8150 instruction->GetDexPc(),
8151 calling_convention);
8152}
8153
8154void LocationsBuilderMIPS::VisitUnresolvedStaticFieldSet(
8155 HUnresolvedStaticFieldSet* instruction) {
8156 FieldAccessCallingConventionMIPS calling_convention;
8157 codegen_->CreateUnresolvedFieldLocationSummary(instruction,
8158 instruction->GetFieldType(),
8159 calling_convention);
8160}
8161
8162void InstructionCodeGeneratorMIPS::VisitUnresolvedStaticFieldSet(
8163 HUnresolvedStaticFieldSet* instruction) {
8164 FieldAccessCallingConventionMIPS calling_convention;
8165 codegen_->GenerateUnresolvedFieldAccess(instruction,
8166 instruction->GetFieldType(),
8167 instruction->GetFieldIndex(),
8168 instruction->GetDexPc(),
8169 calling_convention);
8170}
8171
8172void LocationsBuilderMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
Vladimir Marko70e97462016-08-09 11:04:26 +01008173 LocationSummary* locations =
8174 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnSlowPath);
Vladimir Marko804b03f2016-09-14 16:26:36 +01008175 locations->SetCustomSlowPathCallerSaves(RegisterSet::Empty()); // No caller-save registers.
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008176}
8177
8178void InstructionCodeGeneratorMIPS::VisitSuspendCheck(HSuspendCheck* instruction) {
8179 HBasicBlock* block = instruction->GetBlock();
8180 if (block->GetLoopInformation() != nullptr) {
8181 DCHECK(block->GetLoopInformation()->GetSuspendCheck() == instruction);
8182 // The back edge will generate the suspend check.
8183 return;
8184 }
8185 if (block->IsEntryBlock() && instruction->GetNext()->IsGoto()) {
8186 // The goto will generate the suspend check.
8187 return;
8188 }
8189 GenerateSuspendCheck(instruction, nullptr);
8190}
8191
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008192void LocationsBuilderMIPS::VisitThrow(HThrow* instruction) {
8193 LocationSummary* locations =
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008194 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kCallOnMainOnly);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008195 InvokeRuntimeCallingConvention calling_convention;
8196 locations->SetInAt(0, Location::RegisterLocation(calling_convention.GetRegisterAt(0)));
8197}
8198
8199void InstructionCodeGeneratorMIPS::VisitThrow(HThrow* instruction) {
Serban Constantinescufca16662016-07-14 09:21:59 +01008200 codegen_->InvokeRuntime(kQuickDeliverException, instruction, instruction->GetDexPc());
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008201 CheckEntrypointTypes<kQuickDeliverException, void, mirror::Object*>();
8202}
8203
8204void LocationsBuilderMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8205 Primitive::Type input_type = conversion->GetInputType();
8206 Primitive::Type result_type = conversion->GetResultType();
8207 DCHECK_NE(input_type, result_type);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008208 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008209
8210 if ((input_type == Primitive::kPrimNot) || (input_type == Primitive::kPrimVoid) ||
8211 (result_type == Primitive::kPrimNot) || (result_type == Primitive::kPrimVoid)) {
8212 LOG(FATAL) << "Unexpected type conversion from " << input_type << " to " << result_type;
8213 }
8214
8215 LocationSummary::CallKind call_kind = LocationSummary::kNoCall;
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008216 if (!isR6 &&
8217 ((Primitive::IsFloatingPointType(result_type) && input_type == Primitive::kPrimLong) ||
8218 (result_type == Primitive::kPrimLong && Primitive::IsFloatingPointType(input_type)))) {
Serban Constantinescu54ff4822016-07-07 18:03:19 +01008219 call_kind = LocationSummary::kCallOnMainOnly;
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008220 }
8221
8222 LocationSummary* locations = new (GetGraph()->GetArena()) LocationSummary(conversion, call_kind);
8223
8224 if (call_kind == LocationSummary::kNoCall) {
8225 if (Primitive::IsFloatingPointType(input_type)) {
8226 locations->SetInAt(0, Location::RequiresFpuRegister());
8227 } else {
8228 locations->SetInAt(0, Location::RequiresRegister());
8229 }
8230
8231 if (Primitive::IsFloatingPointType(result_type)) {
8232 locations->SetOut(Location::RequiresFpuRegister(), Location::kNoOutputOverlap);
8233 } else {
8234 locations->SetOut(Location::RequiresRegister(), Location::kNoOutputOverlap);
8235 }
8236 } else {
8237 InvokeRuntimeCallingConvention calling_convention;
8238
8239 if (Primitive::IsFloatingPointType(input_type)) {
8240 locations->SetInAt(0, Location::FpuRegisterLocation(calling_convention.GetFpuRegisterAt(0)));
8241 } else {
8242 DCHECK_EQ(input_type, Primitive::kPrimLong);
8243 locations->SetInAt(0, Location::RegisterPairLocation(
8244 calling_convention.GetRegisterAt(0), calling_convention.GetRegisterAt(1)));
8245 }
8246
8247 locations->SetOut(calling_convention.GetReturnLocation(result_type));
8248 }
8249}
8250
8251void InstructionCodeGeneratorMIPS::VisitTypeConversion(HTypeConversion* conversion) {
8252 LocationSummary* locations = conversion->GetLocations();
8253 Primitive::Type result_type = conversion->GetResultType();
8254 Primitive::Type input_type = conversion->GetInputType();
8255 bool has_sign_extension = codegen_->GetInstructionSetFeatures().IsMipsIsaRevGreaterThanEqual2();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008256 bool isR6 = codegen_->GetInstructionSetFeatures().IsR6();
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008257
8258 DCHECK_NE(input_type, result_type);
8259
8260 if (result_type == Primitive::kPrimLong && Primitive::IsIntegralType(input_type)) {
8261 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8262 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
8263 Register src = locations->InAt(0).AsRegister<Register>();
8264
Alexey Frunzea871ef12016-06-27 15:20:11 -07008265 if (dst_low != src) {
8266 __ Move(dst_low, src);
8267 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008268 __ Sra(dst_high, src, 31);
8269 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsIntegralType(input_type)) {
8270 Register dst = locations->Out().AsRegister<Register>();
8271 Register src = (input_type == Primitive::kPrimLong)
8272 ? locations->InAt(0).AsRegisterPairLow<Register>()
8273 : locations->InAt(0).AsRegister<Register>();
8274
8275 switch (result_type) {
8276 case Primitive::kPrimChar:
8277 __ Andi(dst, src, 0xFFFF);
8278 break;
8279 case Primitive::kPrimByte:
8280 if (has_sign_extension) {
8281 __ Seb(dst, src);
8282 } else {
8283 __ Sll(dst, src, 24);
8284 __ Sra(dst, dst, 24);
8285 }
8286 break;
8287 case Primitive::kPrimShort:
8288 if (has_sign_extension) {
8289 __ Seh(dst, src);
8290 } else {
8291 __ Sll(dst, src, 16);
8292 __ Sra(dst, dst, 16);
8293 }
8294 break;
8295 case Primitive::kPrimInt:
Alexey Frunzea871ef12016-06-27 15:20:11 -07008296 if (dst != src) {
8297 __ Move(dst, src);
8298 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008299 break;
8300
8301 default:
8302 LOG(FATAL) << "Unexpected type conversion from " << input_type
8303 << " to " << result_type;
8304 }
8305 } else if (Primitive::IsFloatingPointType(result_type) && Primitive::IsIntegralType(input_type)) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008306 if (input_type == Primitive::kPrimLong) {
8307 if (isR6) {
8308 // cvt.s.l/cvt.d.l requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8309 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8310 Register src_high = locations->InAt(0).AsRegisterPairHigh<Register>();
8311 Register src_low = locations->InAt(0).AsRegisterPairLow<Register>();
8312 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8313 __ Mtc1(src_low, FTMP);
8314 __ Mthc1(src_high, FTMP);
8315 if (result_type == Primitive::kPrimFloat) {
8316 __ Cvtsl(dst, FTMP);
8317 } else {
8318 __ Cvtdl(dst, FTMP);
8319 }
8320 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008321 QuickEntrypointEnum entrypoint = (result_type == Primitive::kPrimFloat) ? kQuickL2f
8322 : kQuickL2d;
8323 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008324 if (result_type == Primitive::kPrimFloat) {
8325 CheckEntrypointTypes<kQuickL2f, float, int64_t>();
8326 } else {
8327 CheckEntrypointTypes<kQuickL2d, double, int64_t>();
8328 }
8329 }
8330 } else {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008331 Register src = locations->InAt(0).AsRegister<Register>();
8332 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8333 __ Mtc1(src, FTMP);
8334 if (result_type == Primitive::kPrimFloat) {
8335 __ Cvtsw(dst, FTMP);
8336 } else {
8337 __ Cvtdw(dst, FTMP);
8338 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008339 }
8340 } else if (Primitive::IsIntegralType(result_type) && Primitive::IsFloatingPointType(input_type)) {
8341 CHECK(result_type == Primitive::kPrimInt || result_type == Primitive::kPrimLong);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008342
8343 // When NAN2008=1 (R6), the truncate instruction caps the output at the minimum/maximum
8344 // value of the output type if the input is outside of the range after the truncation or
8345 // produces 0 when the input is a NaN. IOW, the three special cases produce three distinct
8346 // results. This matches the desired float/double-to-int/long conversion exactly.
8347 //
8348 // When NAN2008=0 (R2 and before), the truncate instruction produces the maximum positive
8349 // value when the input is either a NaN or is outside of the range of the output type
8350 // after the truncation. IOW, the three special cases (NaN, too small, too big) produce
8351 // the same result.
8352 //
8353 // The code takes care of the different behaviors by first comparing the input to the
8354 // minimum output value (-2**-63 for truncating to long, -2**-31 for truncating to int).
8355 // If the input is greater than or equal to the minimum, it procedes to the truncate
8356 // instruction, which will handle such an input the same way irrespective of NAN2008.
8357 // Otherwise the input is compared to itself to determine whether it is a NaN or not
8358 // in order to return either zero or the minimum value.
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008359 if (result_type == Primitive::kPrimLong) {
8360 if (isR6) {
8361 // trunc.l.s/trunc.l.d requires MIPSR2+ with FR=1. MIPS32R6 is implemented as a secondary
8362 // architecture on top of MIPS64R6, which has FR=1, and therefore can use the instruction.
8363 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8364 Register dst_high = locations->Out().AsRegisterPairHigh<Register>();
8365 Register dst_low = locations->Out().AsRegisterPairLow<Register>();
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008366
8367 if (input_type == Primitive::kPrimFloat) {
8368 __ TruncLS(FTMP, src);
8369 } else {
8370 __ TruncLD(FTMP, src);
8371 }
8372 __ Mfc1(dst_low, FTMP);
8373 __ Mfhc1(dst_high, FTMP);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008374 } else {
Serban Constantinescufca16662016-07-14 09:21:59 +01008375 QuickEntrypointEnum entrypoint = (input_type == Primitive::kPrimFloat) ? kQuickF2l
8376 : kQuickD2l;
8377 codegen_->InvokeRuntime(entrypoint, conversion, conversion->GetDexPc());
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008378 if (input_type == Primitive::kPrimFloat) {
8379 CheckEntrypointTypes<kQuickF2l, int64_t, float>();
8380 } else {
8381 CheckEntrypointTypes<kQuickD2l, int64_t, double>();
8382 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008383 }
8384 } else {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008385 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8386 Register dst = locations->Out().AsRegister<Register>();
8387 MipsLabel truncate;
8388 MipsLabel done;
8389
Lena Djokicf4e23a82017-05-09 15:43:45 +02008390 if (!isR6) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008391 if (input_type == Primitive::kPrimFloat) {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008392 uint32_t min_val = bit_cast<uint32_t, float>(std::numeric_limits<int32_t>::min());
8393 __ LoadConst32(TMP, min_val);
8394 __ Mtc1(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008395 } else {
Lena Djokicf4e23a82017-05-09 15:43:45 +02008396 uint64_t min_val = bit_cast<uint64_t, double>(std::numeric_limits<int32_t>::min());
8397 __ LoadConst32(TMP, High32Bits(min_val));
8398 __ Mtc1(ZERO, FTMP);
8399 __ MoveToFpuHigh(TMP, FTMP);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008400 }
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008401
8402 if (input_type == Primitive::kPrimFloat) {
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008403 __ ColeS(0, FTMP, src);
8404 } else {
8405 __ ColeD(0, FTMP, src);
8406 }
8407 __ Bc1t(0, &truncate);
8408
8409 if (input_type == Primitive::kPrimFloat) {
8410 __ CeqS(0, src, src);
8411 } else {
8412 __ CeqD(0, src, src);
8413 }
8414 __ LoadConst32(dst, std::numeric_limits<int32_t>::min());
8415 __ Movf(dst, ZERO, 0);
Lena Djokicf4e23a82017-05-09 15:43:45 +02008416
8417 __ B(&done);
8418
8419 __ Bind(&truncate);
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008420 }
8421
Alexey Frunzebaf60b72015-12-22 15:15:03 -08008422 if (input_type == Primitive::kPrimFloat) {
8423 __ TruncWS(FTMP, src);
8424 } else {
8425 __ TruncWD(FTMP, src);
8426 }
8427 __ Mfc1(dst, FTMP);
8428
Lena Djokicf4e23a82017-05-09 15:43:45 +02008429 if (!isR6) {
8430 __ Bind(&done);
8431 }
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008432 }
8433 } else if (Primitive::IsFloatingPointType(result_type) &&
8434 Primitive::IsFloatingPointType(input_type)) {
8435 FRegister dst = locations->Out().AsFpuRegister<FRegister>();
8436 FRegister src = locations->InAt(0).AsFpuRegister<FRegister>();
8437 if (result_type == Primitive::kPrimFloat) {
8438 __ Cvtsd(dst, src);
8439 } else {
8440 __ Cvtds(dst, src);
8441 }
8442 } else {
8443 LOG(FATAL) << "Unexpected or unimplemented type conversion from " << input_type
8444 << " to " << result_type;
8445 }
8446}
8447
8448void LocationsBuilderMIPS::VisitUShr(HUShr* ushr) {
8449 HandleShift(ushr);
8450}
8451
8452void InstructionCodeGeneratorMIPS::VisitUShr(HUShr* ushr) {
8453 HandleShift(ushr);
8454}
8455
8456void LocationsBuilderMIPS::VisitXor(HXor* instruction) {
8457 HandleBinaryOp(instruction);
8458}
8459
8460void InstructionCodeGeneratorMIPS::VisitXor(HXor* instruction) {
8461 HandleBinaryOp(instruction);
8462}
8463
8464void LocationsBuilderMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8465 // Nothing to do, this should be removed during prepare for register allocator.
8466 LOG(FATAL) << "Unreachable";
8467}
8468
8469void InstructionCodeGeneratorMIPS::VisitBoundType(HBoundType* instruction ATTRIBUTE_UNUSED) {
8470 // Nothing to do, this should be removed during prepare for register allocator.
8471 LOG(FATAL) << "Unreachable";
8472}
8473
8474void LocationsBuilderMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008475 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008476}
8477
8478void InstructionCodeGeneratorMIPS::VisitEqual(HEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008479 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008480}
8481
8482void LocationsBuilderMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008483 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008484}
8485
8486void InstructionCodeGeneratorMIPS::VisitNotEqual(HNotEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008487 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008488}
8489
8490void LocationsBuilderMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008491 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008492}
8493
8494void InstructionCodeGeneratorMIPS::VisitLessThan(HLessThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008495 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008496}
8497
8498void LocationsBuilderMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008499 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008500}
8501
8502void InstructionCodeGeneratorMIPS::VisitLessThanOrEqual(HLessThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008503 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008504}
8505
8506void LocationsBuilderMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008507 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008508}
8509
8510void InstructionCodeGeneratorMIPS::VisitGreaterThan(HGreaterThan* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008511 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008512}
8513
8514void LocationsBuilderMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008515 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008516}
8517
8518void InstructionCodeGeneratorMIPS::VisitGreaterThanOrEqual(HGreaterThanOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008519 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008520}
8521
8522void LocationsBuilderMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008523 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008524}
8525
8526void InstructionCodeGeneratorMIPS::VisitBelow(HBelow* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008527 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008528}
8529
8530void LocationsBuilderMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008531 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008532}
8533
8534void InstructionCodeGeneratorMIPS::VisitBelowOrEqual(HBelowOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008535 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008536}
8537
8538void LocationsBuilderMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008539 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008540}
8541
8542void InstructionCodeGeneratorMIPS::VisitAbove(HAbove* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008543 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008544}
8545
8546void LocationsBuilderMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008547 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008548}
8549
8550void InstructionCodeGeneratorMIPS::VisitAboveOrEqual(HAboveOrEqual* comp) {
Vladimir Marko5f7b58e2015-11-23 19:49:34 +00008551 HandleCondition(comp);
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008552}
8553
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008554void LocationsBuilderMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8555 LocationSummary* locations =
8556 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8557 locations->SetInAt(0, Location::RequiresRegister());
8558}
8559
Alexey Frunze96b66822016-09-10 02:32:44 -07008560void InstructionCodeGeneratorMIPS::GenPackedSwitchWithCompares(Register value_reg,
8561 int32_t lower_bound,
8562 uint32_t num_entries,
8563 HBasicBlock* switch_block,
8564 HBasicBlock* default_block) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008565 // Create a set of compare/jumps.
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008566 Register temp_reg = TMP;
8567 __ Addiu32(temp_reg, value_reg, -lower_bound);
8568 // Jump to default if index is negative
8569 // Note: We don't check the case that index is positive while value < lower_bound, because in
8570 // this case, index >= num_entries must be true. So that we can save one branch instruction.
8571 __ Bltz(temp_reg, codegen_->GetLabelOf(default_block));
8572
Alexey Frunze96b66822016-09-10 02:32:44 -07008573 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008574 // Jump to successors[0] if value == lower_bound.
8575 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[0]));
8576 int32_t last_index = 0;
8577 for (; num_entries - last_index > 2; last_index += 2) {
8578 __ Addiu(temp_reg, temp_reg, -2);
8579 // Jump to successors[last_index + 1] if value < case_value[last_index + 2].
8580 __ Bltz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
8581 // Jump to successors[last_index + 2] if value == case_value[last_index + 2].
8582 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 2]));
8583 }
8584 if (num_entries - last_index == 2) {
8585 // The last missing case_value.
8586 __ Addiu(temp_reg, temp_reg, -1);
8587 __ Beqz(temp_reg, codegen_->GetLabelOf(successors[last_index + 1]));
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008588 }
8589
Vladimir Markof3e0ee22015-12-17 15:23:13 +00008590 // And the default for any other value.
Alexey Frunze96b66822016-09-10 02:32:44 -07008591 if (!codegen_->GoesToNextBlock(switch_block, default_block)) {
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008592 __ B(codegen_->GetLabelOf(default_block));
8593 }
8594}
8595
Alexey Frunze96b66822016-09-10 02:32:44 -07008596void InstructionCodeGeneratorMIPS::GenTableBasedPackedSwitch(Register value_reg,
8597 Register constant_area,
8598 int32_t lower_bound,
8599 uint32_t num_entries,
8600 HBasicBlock* switch_block,
8601 HBasicBlock* default_block) {
8602 // Create a jump table.
8603 std::vector<MipsLabel*> labels(num_entries);
8604 const ArenaVector<HBasicBlock*>& successors = switch_block->GetSuccessors();
8605 for (uint32_t i = 0; i < num_entries; i++) {
8606 labels[i] = codegen_->GetLabelOf(successors[i]);
8607 }
8608 JumpTable* table = __ CreateJumpTable(std::move(labels));
8609
8610 // Is the value in range?
8611 __ Addiu32(TMP, value_reg, -lower_bound);
8612 if (IsInt<16>(static_cast<int32_t>(num_entries))) {
8613 __ Sltiu(AT, TMP, num_entries);
8614 __ Beqz(AT, codegen_->GetLabelOf(default_block));
8615 } else {
8616 __ LoadConst32(AT, num_entries);
8617 __ Bgeu(TMP, AT, codegen_->GetLabelOf(default_block));
8618 }
8619
8620 // We are in the range of the table.
8621 // Load the target address from the jump table, indexing by the value.
8622 __ LoadLabelAddress(AT, constant_area, table->GetLabel());
Chris Larsencd0295d2017-03-31 15:26:54 -07008623 __ ShiftAndAdd(TMP, TMP, AT, 2, TMP);
Alexey Frunze96b66822016-09-10 02:32:44 -07008624 __ Lw(TMP, TMP, 0);
8625 // Compute the absolute target address by adding the table start address
8626 // (the table contains offsets to targets relative to its start).
8627 __ Addu(TMP, TMP, AT);
8628 // And jump.
8629 __ Jr(TMP);
8630 __ NopIfNoReordering();
8631}
8632
8633void InstructionCodeGeneratorMIPS::VisitPackedSwitch(HPackedSwitch* switch_instr) {
8634 int32_t lower_bound = switch_instr->GetStartValue();
8635 uint32_t num_entries = switch_instr->GetNumEntries();
8636 LocationSummary* locations = switch_instr->GetLocations();
8637 Register value_reg = locations->InAt(0).AsRegister<Register>();
8638 HBasicBlock* switch_block = switch_instr->GetBlock();
8639 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8640
8641 if (codegen_->GetInstructionSetFeatures().IsR6() &&
8642 num_entries > kPackedSwitchJumpTableThreshold) {
8643 // R6 uses PC-relative addressing to access the jump table.
8644 // R2, OTOH, requires an HMipsComputeBaseMethodAddress input to access
8645 // the jump table and it is implemented by changing HPackedSwitch to
8646 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress.
8647 // See VisitMipsPackedSwitch() for the table-based implementation on R2.
8648 GenTableBasedPackedSwitch(value_reg,
8649 ZERO,
8650 lower_bound,
8651 num_entries,
8652 switch_block,
8653 default_block);
8654 } else {
8655 GenPackedSwitchWithCompares(value_reg,
8656 lower_bound,
8657 num_entries,
8658 switch_block,
8659 default_block);
8660 }
8661}
8662
8663void LocationsBuilderMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8664 LocationSummary* locations =
8665 new (GetGraph()->GetArena()) LocationSummary(switch_instr, LocationSummary::kNoCall);
8666 locations->SetInAt(0, Location::RequiresRegister());
8667 // Constant area pointer (HMipsComputeBaseMethodAddress).
8668 locations->SetInAt(1, Location::RequiresRegister());
8669}
8670
8671void InstructionCodeGeneratorMIPS::VisitMipsPackedSwitch(HMipsPackedSwitch* switch_instr) {
8672 int32_t lower_bound = switch_instr->GetStartValue();
8673 uint32_t num_entries = switch_instr->GetNumEntries();
8674 LocationSummary* locations = switch_instr->GetLocations();
8675 Register value_reg = locations->InAt(0).AsRegister<Register>();
8676 Register constant_area = locations->InAt(1).AsRegister<Register>();
8677 HBasicBlock* switch_block = switch_instr->GetBlock();
8678 HBasicBlock* default_block = switch_instr->GetDefaultBlock();
8679
8680 // This is an R2-only path. HPackedSwitch has been changed to
8681 // HMipsPackedSwitch, which bears HMipsComputeBaseMethodAddress
8682 // required to address the jump table relative to PC.
8683 GenTableBasedPackedSwitch(value_reg,
8684 constant_area,
8685 lower_bound,
8686 num_entries,
8687 switch_block,
8688 default_block);
8689}
8690
Alexey Frunzee3fb2452016-05-10 16:08:05 -07008691void LocationsBuilderMIPS::VisitMipsComputeBaseMethodAddress(
8692 HMipsComputeBaseMethodAddress* insn) {
8693 LocationSummary* locations =
8694 new (GetGraph()->GetArena()) LocationSummary(insn, LocationSummary::kNoCall);
8695 locations->SetOut(Location::RequiresRegister());
8696}
8697
8698void InstructionCodeGeneratorMIPS::VisitMipsComputeBaseMethodAddress(
8699 HMipsComputeBaseMethodAddress* insn) {
8700 LocationSummary* locations = insn->GetLocations();
8701 Register reg = locations->Out().AsRegister<Register>();
8702
8703 CHECK(!codegen_->GetInstructionSetFeatures().IsR6());
8704
8705 // Generate a dummy PC-relative call to obtain PC.
8706 __ Nal();
8707 // Grab the return address off RA.
8708 __ Move(reg, RA);
8709
8710 // Remember this offset (the obtained PC value) for later use with constant area.
8711 __ BindPcRelBaseLabel();
8712}
8713
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008714void LocationsBuilderMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8715 // The trampoline uses the same calling convention as dex calling conventions,
8716 // except instead of loading arg0/r0 with the target Method*, arg0/r0 will contain
8717 // the method_idx.
8718 HandleInvoke(invoke);
8719}
8720
8721void InstructionCodeGeneratorMIPS::VisitInvokeUnresolved(HInvokeUnresolved* invoke) {
8722 codegen_->GenerateInvokeUnresolvedRuntimeCall(invoke);
8723}
8724
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008725void LocationsBuilderMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8726 LocationSummary* locations =
8727 new (GetGraph()->GetArena()) LocationSummary(instruction, LocationSummary::kNoCall);
8728 locations->SetInAt(0, Location::RequiresRegister());
8729 locations->SetOut(Location::RequiresRegister());
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008730}
8731
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008732void InstructionCodeGeneratorMIPS::VisitClassTableGet(HClassTableGet* instruction) {
8733 LocationSummary* locations = instruction->GetLocations();
Vladimir Markoa1de9182016-02-25 11:37:38 +00008734 if (instruction->GetTableKind() == HClassTableGet::TableKind::kVTable) {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008735 uint32_t method_offset = mirror::Class::EmbeddedVTableEntryOffset(
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008736 instruction->GetIndex(), kMipsPointerSize).SizeValue();
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008737 __ LoadFromOffset(kLoadWord,
8738 locations->Out().AsRegister<Register>(),
8739 locations->InAt(0).AsRegister<Register>(),
8740 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008741 } else {
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008742 uint32_t method_offset = static_cast<uint32_t>(ImTable::OffsetOfElement(
Matthew Gharrity465ecc82016-07-19 21:32:52 +00008743 instruction->GetIndex(), kMipsPointerSize));
Artem Udovichenkoa62cb9b2016-06-30 09:18:25 +00008744 __ LoadFromOffset(kLoadWord,
8745 locations->Out().AsRegister<Register>(),
8746 locations->InAt(0).AsRegister<Register>(),
8747 mirror::Class::ImtPtrOffset(kMipsPointerSize).Uint32Value());
Nicolas Geoffrayff484b92016-07-13 14:13:48 +01008748 __ LoadFromOffset(kLoadWord,
8749 locations->Out().AsRegister<Register>(),
8750 locations->Out().AsRegister<Register>(),
8751 method_offset);
Roland Levillain2aba7cd2016-02-03 12:27:20 +00008752 }
Nicolas Geoffraya42363f2015-12-17 14:57:09 +00008753}
8754
Goran Jakovljevicf652cec2015-08-25 16:11:42 +02008755#undef __
8756#undef QUICK_ENTRY_POINT
8757
8758} // namespace mips
8759} // namespace art